OLD | NEW |
---|---|
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include <errno.h> | 15 #include <errno.h> |
16 #include <getopt.h> | 16 #include <getopt.h> |
17 #include <libgen.h> | |
18 #include <stdio.h> | 17 #include <stdio.h> |
19 #include <stdlib.h> | 18 #include <stdlib.h> |
20 #include <string.h> | 19 #include <string.h> |
21 #include <sys/types.h> | 20 #include <sys/types.h> |
22 #include <time.h> | 21 #include <time.h> |
23 | 22 |
24 #include <string> | 23 #include <string> |
25 #include <vector> | 24 #include <vector> |
26 | 25 |
27 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
28 #include "base/logging.h" | 27 #include "base/logging.h" |
29 #include "base/files/file_path.h" | 28 #include "base/files/file_path.h" |
30 #include "base/memory/scoped_ptr.h" | 29 #include "base/memory/scoped_ptr.h" |
31 #include "base/numerics/safe_conversions.h" | 30 #include "base/numerics/safe_conversions.h" |
31 #include "base/strings/string_util.h" | |
32 #include "base/strings/utf_string_conversions.h" | |
33 #include "build/build_config.h" | |
32 #include "client/crash_report_database.h" | 34 #include "client/crash_report_database.h" |
33 #include "client/settings.h" | 35 #include "client/settings.h" |
34 #include "tools/tool_support.h" | 36 #include "tools/tool_support.h" |
35 #include "util/file/file_io.h" | 37 #include "util/file/file_io.h" |
36 #include "util/file/file_reader.h" | 38 #include "util/file/file_reader.h" |
37 #include "util/misc/uuid.h" | 39 #include "util/misc/uuid.h" |
38 | 40 |
41 #if defined(OS_MACOSX) | |
42 #include <libgen.h> | |
43 #endif // OS_MACOSX | |
44 | |
39 namespace crashpad { | 45 namespace crashpad { |
40 namespace { | 46 namespace { |
41 | 47 |
48 #if defined(OS_POSIX) | |
49 time_t TimeGM(struct tm* tm) { | |
50 return timegm(tm); | |
51 } | |
52 | |
53 std::string Basename(const std::string& path) { | |
54 std::string copy(path); | |
55 return std::string(basename(©[0])); | |
56 } | |
57 | |
58 const char* Strptime(const char* buf, const char* format, struct tm* tm) { | |
59 return strptime(buf, format, tm); | |
60 } | |
61 #elif defined(OS_WIN) | |
62 time_t TimeGM(struct tm* tm) { | |
63 return _mkgmtime(tm); | |
Mark Mentovai
2015/05/04 21:04:29
Write a timegm wrapper in compat/win/time.{cc,h} i
scottmg
2015/05/04 23:26:36
I did timegm and strptime, but basename seems icky
| |
64 } | |
65 | |
66 std::string Basename(const std::string& path) { | |
67 char fname[_MAX_FNAME]; | |
68 char ext[_MAX_EXT]; | |
69 _splitpath(path.c_str(), nullptr, nullptr, fname, ext); | |
70 return std::string(fname) + std::string(ext); | |
71 } | |
72 | |
73 const char* Strptime(const char* buf, const char* format, struct tm* tm) { | |
74 // TODO(scottmg): strptime implementation. | |
75 NOTREACHED(); | |
76 return nullptr; | |
77 } | |
78 #endif // OS_POSIX | |
79 | |
42 void Usage(const std::string& me) { | 80 void Usage(const std::string& me) { |
43 fprintf(stderr, | 81 fprintf(stderr, |
44 "Usage: %s [OPTION]... PID\n" | 82 "Usage: %s [OPTION]... PID\n" |
45 "Operate on Crashpad crash report databases.\n" | 83 "Operate on Crashpad crash report databases.\n" |
46 "\n" | 84 "\n" |
47 " -d, --database=PATH operate on the crash report database at PATH\ n" | 85 " -d, --database=PATH operate on the crash report database at PATH\ n" |
48 " --show-client-id show the client ID\n" | 86 " --show-client-id show the client ID\n" |
49 " --show-uploads-enabled show whether uploads are enabled\n" | 87 " --show-uploads-enabled show whether uploads are enabled\n" |
50 " --show-last-upload-attempt-time\n" | 88 " --show-last-upload-attempt-time\n" |
51 " show the last-upload-attempt time\n" | 89 " show the last-upload-attempt time\n" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 const char* const kTrueWords[] = { | 135 const char* const kTrueWords[] = { |
98 "1", | 136 "1", |
99 "true", | 137 "true", |
100 "yes", | 138 "yes", |
101 "on", | 139 "on", |
102 "enabled", | 140 "enabled", |
103 "set", | 141 "set", |
104 }; | 142 }; |
105 | 143 |
106 for (size_t index = 0; index < arraysize(kFalseWords); ++index) { | 144 for (size_t index = 0; index < arraysize(kFalseWords); ++index) { |
107 if (strcasecmp(string, kFalseWords[index]) == 0) { | 145 if (base::strcasecmp(string, kFalseWords[index]) == 0) { |
108 *boolean = false; | 146 *boolean = false; |
109 return true; | 147 return true; |
110 } | 148 } |
111 } | 149 } |
112 | 150 |
113 for (size_t index = 0; index < arraysize(kTrueWords); ++index) { | 151 for (size_t index = 0; index < arraysize(kTrueWords); ++index) { |
114 if (strcasecmp(string, kTrueWords[index]) == 0) { | 152 if (base::strcasecmp(string, kTrueWords[index]) == 0) { |
115 *boolean = true; | 153 *boolean = true; |
116 return true; | 154 return true; |
117 } | 155 } |
118 } | 156 } |
119 | 157 |
120 return false; | 158 return false; |
121 } | 159 } |
122 | 160 |
123 // Converts |boolean| to a string, either "true" or "false". | 161 // Converts |boolean| to a string, either "true" or "false". |
124 std::string BoolToString(bool boolean) { | 162 std::string BoolToString(bool boolean) { |
125 return std::string(boolean ? "true" : "false"); | 163 return std::string(boolean ? "true" : "false"); |
126 } | 164 } |
127 | 165 |
128 // Converts |string| to |time|, returning true if a conversion could be | 166 // Converts |string| to |time|, returning true if a conversion could be |
129 // performed, and false without setting |boolean| if no conversion could be | 167 // performed, and false without setting |boolean| if no conversion could be |
130 // performed. Various time formats are recognized, including several string | 168 // performed. Various time formats are recognized, including several string |
131 // representations and a numeric time_t representation. The special string | 169 // representations and a numeric time_t representation. The special string |
132 // "never" is recognized as |string| and converts to a |time| value of 0. |utc|, | 170 // "never" is recognized as |string| and converts to a |time| value of 0. |utc|, |
133 // when true, causes |string| to be interpreted as a UTC time rather than a | 171 // when true, causes |string| to be interpreted as a UTC time rather than a |
134 // local time when the time zone is ambiguous. | 172 // local time when the time zone is ambiguous. |
135 bool StringToTime(const char* string, time_t* time, bool utc) { | 173 bool StringToTime(const char* string, time_t* time, bool utc) { |
136 if (strcasecmp(string, "never") == 0) { | 174 if (base::strcasecmp(string, "never") == 0) { |
137 *time = 0; | 175 *time = 0; |
138 return true; | 176 return true; |
139 } | 177 } |
140 | 178 |
141 const char* end = string + strlen(string); | 179 const char* end = string + strlen(string); |
142 | 180 |
143 const char* const kFormats[] = { | 181 const char* const kFormats[] = { |
144 "%Y-%m-%d %H:%M:%S %Z", | 182 "%Y-%m-%d %H:%M:%S %Z", |
145 "%Y-%m-%d %H:%M:%S", | 183 "%Y-%m-%d %H:%M:%S", |
146 "%+", | 184 "%+", |
147 }; | 185 }; |
148 | 186 |
149 for (size_t index = 0; index < arraysize(kFormats); ++index) { | 187 for (size_t index = 0; index < arraysize(kFormats); ++index) { |
150 tm time_tm; | 188 tm time_tm; |
151 const char* strptime_result = strptime(string, kFormats[index], &time_tm); | 189 const char* strptime_result = Strptime(string, kFormats[index], &time_tm); |
152 if (strptime_result == end) { | 190 if (strptime_result == end) { |
153 if (utc) { | 191 if (utc) { |
154 *time = timegm(&time_tm); | 192 *time = TimeGM(&time_tm); |
155 } else { | 193 } else { |
156 *time = timelocal(&time_tm); | 194 *time = mktime(&time_tm); |
157 } | 195 } |
158 | 196 |
159 return true; | 197 return true; |
160 } | 198 } |
161 } | 199 } |
162 | 200 |
163 char* end_result; | 201 char* end_result; |
164 errno = 0; | 202 errno = 0; |
165 long long strtoll_result = strtoll(string, &end_result, 0); | 203 long long strtoll_result = strtoll(string, &end_result, 0); |
166 if (end_result == end && errno == 0 && | 204 if (end_result == end && errno == 0 && |
167 base::IsValueInRangeForNumericType<time_t>(strtoll_result)) { | 205 base::IsValueInRangeForNumericType<time_t>(strtoll_result)) { |
168 *time = strtoll_result; | 206 *time = strtoll_result; |
169 return true; | 207 return true; |
170 } | 208 } |
171 | 209 |
172 return false; | 210 return false; |
173 } | 211 } |
174 | 212 |
175 // Converst |time_tt| to a string, and returns it. |utc| determines whether the | 213 // Converts |time_tt| to a string, and returns it. |utc| determines whether the |
176 // converted time will reference local time or UTC. If |time_tt| is 0, the | 214 // converted time will reference local time or UTC. If |time_tt| is 0, the |
177 // string "never" will be returned as a special case. | 215 // string "never" will be returned as a special case. |
178 std::string TimeToString(time_t time_tt, bool utc) { | 216 std::string TimeToString(time_t time_tt, bool utc) { |
179 if (time_tt == 0) { | 217 if (time_tt == 0) { |
180 return std::string("never"); | 218 return std::string("never"); |
181 } | 219 } |
182 | 220 |
183 tm time_tm; | 221 tm time_tm; |
184 if (utc) { | 222 if (utc) { |
223 #if defined(OS_POSIX) | |
185 gmtime_r(&time_tt, &time_tm); | 224 gmtime_r(&time_tt, &time_tm); |
225 #elif defined(OS_WIN) | |
226 gmtime_s(&time_tm, &time_tt); | |
227 #endif // OS_POSIX | |
186 } else { | 228 } else { |
229 #if defined(OS_POSIX) | |
187 localtime_r(&time_tt, &time_tm); | 230 localtime_r(&time_tt, &time_tm); |
231 #elif defined(OS_WIN) | |
232 localtime_s(&time_tm, &time_tt); | |
233 #endif // OS_POSIX | |
188 } | 234 } |
189 | 235 |
190 char string[64]; | 236 char string[64]; |
191 CHECK_NE( | 237 CHECK_NE( |
192 strftime(string, arraysize(string), "%Y-%m-%d %H:%M:%S %Z", &time_tm), | 238 strftime(string, arraysize(string), "%Y-%m-%d %H:%M:%S %Z", &time_tm), |
193 0u); | 239 0u); |
194 | 240 |
195 return std::string(string); | 241 return std::string(string); |
196 } | 242 } |
197 | 243 |
198 // Shows information about a single |report|. |space_count| is the number of | 244 // Shows information about a single |report|. |space_count| is the number of |
199 // spaces to print before each line that is printed. |utc| determines whether | 245 // spaces to print before each line that is printed. |utc| determines whether |
200 // times should be shown in UTC or the local time zone. | 246 // times should be shown in UTC or the local time zone. |
201 void ShowReport(const CrashReportDatabase::Report& report, | 247 void ShowReport(const CrashReportDatabase::Report& report, |
202 size_t space_count, | 248 size_t space_count, |
203 bool utc) { | 249 bool utc) { |
204 std::string spaces(space_count, ' '); | 250 std::string spaces(space_count, ' '); |
205 | 251 |
206 printf("%sPath: %s\n", spaces.c_str(), report.file_path.value().c_str()); | 252 printf("%sPath: %s\n", |
Mark Mentovai
2015/05/04 21:04:30
Does it work to use PRFilePath here so that the ar
scottmg
2015/05/04 23:26:36
Done.
| |
253 spaces.c_str(), | |
254 #if defined(OS_POSIX) | |
255 report.file_path.value().c_str() | |
256 #elif defined(OS_WIN) | |
257 base::UTF16ToUTF8(report.file_path.value()).c_str() | |
258 #endif | |
259 ); | |
207 if (!report.id.empty()) { | 260 if (!report.id.empty()) { |
208 printf("%sRemote ID: %s\n", spaces.c_str(), report.id.c_str()); | 261 printf("%sRemote ID: %s\n", spaces.c_str(), report.id.c_str()); |
209 } | 262 } |
210 printf("%sCreation time: %s\n", | 263 printf("%sCreation time: %s\n", |
211 spaces.c_str(), | 264 spaces.c_str(), |
212 TimeToString(report.creation_time, utc).c_str()); | 265 TimeToString(report.creation_time, utc).c_str()); |
213 printf("%sUploaded: %s\n", | 266 printf("%sUploaded: %s\n", |
214 spaces.c_str(), | 267 spaces.c_str(), |
215 BoolToString(report.uploaded).c_str()); | 268 BoolToString(report.uploaded).c_str()); |
216 printf("%sLast upload attempt time: %s\n", | 269 printf("%sLast upload attempt time: %s\n", |
(...skipping 15 matching lines...) Expand all Loading... | |
232 | 285 |
233 for (const CrashReportDatabase::Report& report : reports) { | 286 for (const CrashReportDatabase::Report& report : reports) { |
234 printf("%s%s%s\n", spaces.c_str(), report.uuid.ToString().c_str(), colon); | 287 printf("%s%s%s\n", spaces.c_str(), report.uuid.ToString().c_str(), colon); |
235 if (options.show_all_report_info) { | 288 if (options.show_all_report_info) { |
236 ShowReport(report, space_count + 2, options.utc); | 289 ShowReport(report, space_count + 2, options.utc); |
237 } | 290 } |
238 } | 291 } |
239 } | 292 } |
240 | 293 |
241 int DatabaseUtilMain(int argc, char* argv[]) { | 294 int DatabaseUtilMain(int argc, char* argv[]) { |
242 const std::string me(basename(argv[0])); | 295 const std::string me(Basename(argv[0])); |
243 | 296 |
244 enum OptionFlags { | 297 enum OptionFlags { |
245 // “Short” (single-character) options. | 298 // “Short” (single-character) options. |
246 kOptionDatabase = 'd', | 299 kOptionDatabase = 'd', |
247 | 300 |
248 // Long options without short equivalents. | 301 // Long options without short equivalents. |
249 kOptionLastChar = 255, | 302 kOptionLastChar = 255, |
250 kOptionShowClientID, | 303 kOptionShowClientID, |
251 kOptionShowUploadsEnabled, | 304 kOptionShowUploadsEnabled, |
252 kOptionShowLastUploadAttemptTime, | 305 kOptionShowLastUploadAttemptTime, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 return EXIT_FAILURE; | 395 return EXIT_FAILURE; |
343 } | 396 } |
344 options.has_set_uploads_enabled = true; | 397 options.has_set_uploads_enabled = true; |
345 break; | 398 break; |
346 } | 399 } |
347 case kOptionSetLastUploadAttemptTime: { | 400 case kOptionSetLastUploadAttemptTime: { |
348 options.set_last_upload_attempt_time_string = optarg; | 401 options.set_last_upload_attempt_time_string = optarg; |
349 break; | 402 break; |
350 } | 403 } |
351 case kOptionNewReport: { | 404 case kOptionNewReport: { |
352 options.new_report_paths.push_back(base::FilePath(optarg)); | 405 #if defined(OS_POSIX) |
406 base::FilePath new_report_path(optarg); | |
407 #elif defined(OS_WIN) | |
408 base::FilePath new_report_path(base::UTF8ToUTF16(optarg)); | |
Mark Mentovai
2015/05/04 21:04:30
Similarly, when the entry point is main() and not
scottmg
2015/05/04 23:26:36
Yes, that's right. I looked at switching to wmain,
| |
409 #endif // OS_POSIX | |
410 options.new_report_paths.push_back(new_report_path); | |
353 break; | 411 break; |
354 } | 412 } |
355 case kOptionUTC: { | 413 case kOptionUTC: { |
356 options.utc = true; | 414 options.utc = true; |
357 break; | 415 break; |
358 } | 416 } |
359 case kOptionHelp: { | 417 case kOptionHelp: { |
360 Usage(me); | 418 Usage(me); |
361 return EXIT_SUCCESS; | 419 return EXIT_SUCCESS; |
362 } | 420 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 options.new_report_paths.size(); | 459 options.new_report_paths.size(); |
402 const size_t set_operations = | 460 const size_t set_operations = |
403 options.has_set_uploads_enabled + | 461 options.has_set_uploads_enabled + |
404 (options.set_last_upload_attempt_time_string != nullptr); | 462 (options.set_last_upload_attempt_time_string != nullptr); |
405 | 463 |
406 if (show_operations + set_operations == 0) { | 464 if (show_operations + set_operations == 0) { |
407 ToolSupport::UsageHint(me, "nothing to do"); | 465 ToolSupport::UsageHint(me, "nothing to do"); |
408 return EXIT_FAILURE; | 466 return EXIT_FAILURE; |
409 } | 467 } |
410 | 468 |
469 #if defined(OS_POSIX) | |
470 base::FilePath database_path(options.database); | |
471 #elif defined(OS_WIN) | |
472 base::FilePath database_path(base::UTF8ToUTF16(options.database)); | |
473 #endif // OS_POSIX | |
411 scoped_ptr<CrashReportDatabase> database( | 474 scoped_ptr<CrashReportDatabase> database( |
412 CrashReportDatabase::Initialize(base::FilePath(options.database))); | 475 CrashReportDatabase::Initialize(database_path)); |
413 if (!database) { | 476 if (!database) { |
414 return EXIT_FAILURE; | 477 return EXIT_FAILURE; |
415 } | 478 } |
416 | 479 |
417 Settings* settings = database->GetSettings(); | 480 Settings* settings = database->GetSettings(); |
418 | 481 |
419 // Handle the “show” options before the “set” options so that when they’re | 482 // Handle the “show” options before the “set” options so that when they’re |
420 // specified together, the “show” option reflects the initial state. | 483 // specified together, the “show” option reflects the initial state. |
421 | 484 |
422 if (options.show_client_id) { | 485 if (options.show_client_id) { |
(...skipping 23 matching lines...) Expand all Loading... | |
446 if (!settings->GetLastUploadAttemptTime(&last_upload_attempt_time)) { | 509 if (!settings->GetLastUploadAttemptTime(&last_upload_attempt_time)) { |
447 return EXIT_FAILURE; | 510 return EXIT_FAILURE; |
448 } | 511 } |
449 | 512 |
450 const char* prefix = | 513 const char* prefix = |
451 (show_operations > 1) ? "Last upload attempt time: " : ""; | 514 (show_operations > 1) ? "Last upload attempt time: " : ""; |
452 | 515 |
453 printf("%s%s (%ld)\n", | 516 printf("%s%s (%ld)\n", |
454 prefix, | 517 prefix, |
455 TimeToString(last_upload_attempt_time, options.utc).c_str(), | 518 TimeToString(last_upload_attempt_time, options.utc).c_str(), |
456 implicit_cast<long>(last_upload_attempt_time)); | 519 static_cast<long>(last_upload_attempt_time)); |
457 } | 520 } |
458 | 521 |
459 if (options.show_pending_reports) { | 522 if (options.show_pending_reports) { |
460 std::vector<CrashReportDatabase::Report> pending_reports; | 523 std::vector<CrashReportDatabase::Report> pending_reports; |
461 if (database->GetPendingReports(&pending_reports) != | 524 if (database->GetPendingReports(&pending_reports) != |
462 CrashReportDatabase::kNoError) { | 525 CrashReportDatabase::kNoError) { |
463 return EXIT_FAILURE; | 526 return EXIT_FAILURE; |
464 } | 527 } |
465 | 528 |
466 if (show_operations > 1) { | 529 if (show_operations > 1) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
558 | 621 |
559 return EXIT_SUCCESS; | 622 return EXIT_SUCCESS; |
560 } | 623 } |
561 | 624 |
562 } // namespace | 625 } // namespace |
563 } // namespace crashpad | 626 } // namespace crashpad |
564 | 627 |
565 int main(int argc, char* argv[]) { | 628 int main(int argc, char* argv[]) { |
566 return crashpad::DatabaseUtilMain(argc, argv); | 629 return crashpad::DatabaseUtilMain(argc, argv); |
567 } | 630 } |
OLD | NEW |