Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: chrome/app/breakpad_linuxish.cc

Issue 10532178: Rename back _linuxish to linux. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed a typo Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/app/breakpad_linuxish.h ('k') | chrome/app/chrome_main_delegate.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // For linux_syscall_support.h. This makes it safe to call embedded system
6 // calls when in seccomp mode.
7 #define SYS_SYSCALL_ENTRYPOINT "playground$syscallEntryPoint"
8
9 #include "chrome/app/breakpad_linuxish.h"
10
11 #include <fcntl.h>
12 #include <poll.h>
13 #include <stdlib.h>
14 #include <sys/socket.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/uio.h>
19 #include <time.h>
20 #include <unistd.h>
21
22 #include <algorithm>
23 #include <string>
24
25 #include "base/command_line.h"
26 #include "base/eintr_wrapper.h"
27 #include "base/file_path.h"
28 #include "base/global_descriptors_posix.h"
29 #include "base/linux_util.h"
30 #include "base/path_service.h"
31 #include "base/process_util.h"
32 #include "base/string_util.h"
33 #include "breakpad/src/client/linux/handler/exception_handler.h"
34 #include "breakpad/src/client/linux/minidump_writer/directory_reader.h"
35 #include "breakpad/src/common/linux/linux_libc_support.h"
36 #include "breakpad/src/common/memory.h"
37 #include "chrome/browser/crash_upload_list.h"
38 #include "chrome/common/child_process_logging.h"
39 #include "chrome/common/chrome_paths.h"
40 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/chrome_version_info_posix.h"
42 #include "chrome/common/env_vars.h"
43 #include "chrome/common/logging_chrome.h"
44 #include "content/public/common/content_descriptors.h"
45
46 #if defined(OS_ANDROID)
47 #include <android/log.h>
48 #include <sys/stat.h>
49
50 #include "base/android/build_info.h"
51 #include "base/android/path_utils.h"
52 #include "third_party/lss/linux_syscall_support.h"
53 #else
54 #include "seccompsandbox/linux_syscall_support.h"
55 #endif
56
57 #if defined(OS_ANDROID)
58 #define STAT_STRUCT struct stat
59 #define FSTAT_FUNC fstat
60 #else
61 #define STAT_STRUCT struct kernel_stat
62 #define FSTAT_FUNC sys_fstat
63 #endif
64
65 // Some versions of gcc are prone to warn about unused return values. In cases
66 // where we either a) know the call cannot fail, or b) there is nothing we
67 // can do when a call fails, we mark the return code as ignored. This avoids
68 // spurious compiler warnings.
69 #define IGNORE_RET(x) do { if (x); } while (0)
70
71 static const char kUploadURL[] =
72 "https://clients2.google.com/cr/report";
73
74 static bool g_is_crash_reporter_enabled = false;
75 static uint64_t g_process_start_time = 0;
76 static char* g_crash_log_path = NULL;
77 static google_breakpad::ExceptionHandler* g_breakpad = NULL;
78
79 // Writes the value |v| as 16 hex characters to the memory pointed at by
80 // |output|.
81 static void write_uint64_hex(char* output, uint64_t v) {
82 static const char hextable[] = "0123456789abcdef";
83
84 for (int i = 15; i >= 0; --i) {
85 output[i] = hextable[v & 15];
86 v >>= 4;
87 }
88 }
89
90 // The following helper functions are for calculating uptime.
91
92 // Converts a struct timeval to milliseconds.
93 static uint64_t timeval_to_ms(struct timeval *tv) {
94 uint64_t ret = tv->tv_sec; // Avoid overflow by explicitly using a uint64_t.
95 ret *= 1000;
96 ret += tv->tv_usec / 1000;
97 return ret;
98 }
99
100 // Converts a struct timeval to milliseconds.
101 static uint64_t kernel_timeval_to_ms(struct kernel_timeval *tv) {
102 uint64_t ret = tv->tv_sec; // Avoid overflow by explicitly using a uint64_t.
103 ret *= 1000;
104 ret += tv->tv_usec / 1000;
105 return ret;
106 }
107
108 // String buffer size to use to convert a uint64_t to string.
109 static size_t kUint64StringSize = 21;
110
111 // uint64_t version of my_int_len() from
112 // breakpad/src/common/linux/linux_libc_support.h. Return the length of the
113 // given, non-negative integer when expressed in base 10.
114 static unsigned my_uint64_len(uint64_t i) {
115 if (!i)
116 return 1;
117
118 unsigned len = 0;
119 while (i) {
120 len++;
121 i /= 10;
122 }
123
124 return len;
125 }
126
127 // uint64_t version of my_itos() from
128 // breakpad/src/common/linux/linux_libc_support.h. Convert a non-negative
129 // integer to a string (not null-terminated).
130 static void my_uint64tos(char* output, uint64_t i, unsigned i_len) {
131 for (unsigned index = i_len; index; --index, i /= 10)
132 output[index - 1] = '0' + (i % 10);
133 }
134
135 #if defined(OS_ANDROID)
136 static char* my_strncpy(char* dst, const char* src, size_t len) {
137 int i = len;
138 char* p = dst;
139 if (!dst || !src)
140 return dst;
141 while (i != 0 && *src != '\0') {
142 *p++ = *src++;
143 i--;
144 }
145 while (i != 0) {
146 *p++ = '\0';
147 i--;
148 }
149 return dst;
150 }
151
152 static char* my_strncat(char *dest, const char* src, size_t len) {
153 char* ret = dest;
154 while (*dest)
155 dest++;
156 while (len--)
157 if (!(*dest++ = *src++))
158 return ret;
159 *dest = 0;
160 return ret;
161 }
162 #endif
163
164 namespace {
165
166 // MIME substrings.
167 const char g_rn[] = "\r\n";
168 const char g_form_data_msg[] = "Content-Disposition: form-data; name=\"";
169 const char g_quote_msg[] = "\"";
170 const char g_dashdash_msg[] = "--";
171 const char g_dump_msg[] = "upload_file_minidump\"; filename=\"dump\"";
172 const char g_content_type_msg[] = "Content-Type: application/octet-stream";
173
174 // MimeWriter manages an iovec for writing MIMEs to a file.
175 class MimeWriter {
176 public:
177 static const int kIovCapacity = 30;
178 static const size_t kMaxCrashChunkSize = 64;
179
180 MimeWriter(int fd, const char* const mime_boundary);
181 ~MimeWriter();
182
183 // Append boundary.
184 void AddBoundary();
185
186 // Append end of file boundary.
187 void AddEnd();
188
189 // Append key/value pair with specified sizes.
190 void AddPairData(const char* msg_type,
191 size_t msg_type_size,
192 const char* msg_data,
193 size_t msg_data_size);
194
195 // Append key/value pair.
196 void AddPairString(const char* msg_type,
197 const char* msg_data) {
198 AddPairData(msg_type, my_strlen(msg_type), msg_data, my_strlen(msg_data));
199 }
200
201 // Append key/value pair, splitting value into chunks no larger than
202 // |chunk_size|. |chunk_size| cannot be greater than |kMaxCrashChunkSize|.
203 // The msg_type string will have a counter suffix to distinguish each chunk.
204 void AddPairDataInChunks(const char* msg_type,
205 size_t msg_type_size,
206 const char* msg_data,
207 size_t msg_data_size,
208 size_t chunk_size,
209 bool strip_trailing_spaces);
210
211 // Add binary file dump. Currently this is only done once, so the name is
212 // fixed.
213 void AddFileDump(uint8_t* file_data,
214 size_t file_size);
215
216 // Flush any pending iovecs to the output file.
217 void Flush() {
218 IGNORE_RET(sys_writev(fd_, iov_, iov_index_));
219 iov_index_ = 0;
220 }
221
222 private:
223 void AddItem(const void* base, size_t size);
224 // Minor performance trade-off for easier-to-maintain code.
225 void AddString(const char* str) {
226 AddItem(str, my_strlen(str));
227 }
228 void AddItemWithoutTrailingSpaces(const void* base, size_t size);
229
230 struct kernel_iovec iov_[kIovCapacity];
231 int iov_index_;
232
233 // Output file descriptor.
234 int fd_;
235
236 const char* const mime_boundary_;
237
238 DISALLOW_COPY_AND_ASSIGN(MimeWriter);
239 };
240
241 MimeWriter::MimeWriter(int fd, const char* const mime_boundary)
242 : iov_index_(0),
243 fd_(fd),
244 mime_boundary_(mime_boundary) {
245 }
246
247 MimeWriter::~MimeWriter() {
248 }
249
250 void MimeWriter::AddBoundary() {
251 AddString(mime_boundary_);
252 AddString(g_rn);
253 }
254
255 void MimeWriter::AddEnd() {
256 AddString(mime_boundary_);
257 AddString(g_dashdash_msg);
258 AddString(g_rn);
259 }
260
261 void MimeWriter::AddPairData(const char* msg_type,
262 size_t msg_type_size,
263 const char* msg_data,
264 size_t msg_data_size) {
265 AddString(g_form_data_msg);
266 AddItem(msg_type, msg_type_size);
267 AddString(g_quote_msg);
268 AddString(g_rn);
269 AddString(g_rn);
270 AddItem(msg_data, msg_data_size);
271 AddString(g_rn);
272 }
273
274 void MimeWriter::AddPairDataInChunks(const char* msg_type,
275 size_t msg_type_size,
276 const char* msg_data,
277 size_t msg_data_size,
278 size_t chunk_size,
279 bool strip_trailing_spaces) {
280 if (chunk_size > kMaxCrashChunkSize)
281 return;
282
283 unsigned i = 0;
284 size_t done = 0, msg_length = msg_data_size;
285
286 while (msg_length) {
287 char num[16];
288 const unsigned num_len = my_int_len(++i);
289 my_itos(num, i, num_len);
290
291 size_t chunk_len = std::min(chunk_size, msg_length);
292
293 AddString(g_form_data_msg);
294 AddItem(msg_type, msg_type_size);
295 AddItem(num, num_len);
296 AddString(g_quote_msg);
297 AddString(g_rn);
298 AddString(g_rn);
299 if (strip_trailing_spaces) {
300 AddItemWithoutTrailingSpaces(msg_data + done, chunk_len);
301 } else {
302 AddItem(msg_data + done, chunk_len);
303 }
304 AddString(g_rn);
305 AddBoundary();
306 Flush();
307
308 done += chunk_len;
309 msg_length -= chunk_len;
310 }
311 }
312
313 void MimeWriter::AddFileDump(uint8_t* file_data,
314 size_t file_size) {
315 AddString(g_form_data_msg);
316 AddString(g_dump_msg);
317 AddString(g_rn);
318 AddString(g_content_type_msg);
319 AddString(g_rn);
320 AddString(g_rn);
321 AddItem(file_data, file_size);
322 AddString(g_rn);
323 }
324
325 void MimeWriter::AddItem(const void* base, size_t size) {
326 // Check if the iovec is full and needs to be flushed to output file.
327 if (iov_index_ == kIovCapacity) {
328 Flush();
329 }
330 iov_[iov_index_].iov_base = const_cast<void*>(base);
331 iov_[iov_index_].iov_len = size;
332 ++iov_index_;
333 }
334
335 void MimeWriter::AddItemWithoutTrailingSpaces(const void* base, size_t size) {
336 while (size > 0) {
337 const char* c = static_cast<const char*>(base) + size - 1;
338 if (*c != ' ')
339 break;
340 size--;
341 }
342 AddItem(base, size);
343 }
344
345 void DumpProcess() {
346 if (g_breakpad)
347 g_breakpad->WriteMinidump();
348 }
349
350 const char kGoogleBreakpad[] = "google-breakpad";
351
352 size_t WriteLog(const char* buf, size_t nbytes) {
353 #if defined(OS_ANDROID)
354 return __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, buf);
355 #else
356 return sys_write(2, buf, nbytes);
357 #endif
358 }
359
360 } // namespace
361
362 void HandleCrashDump(const BreakpadInfo& info) {
363 // WARNING: this code runs in a compromised context. It may not call into
364 // libc nor allocate memory normally.
365
366 const int dumpfd = sys_open(info.filename, O_RDONLY, 0);
367 if (dumpfd < 0) {
368 static const char msg[] = "Cannot upload crash dump: failed to open\n";
369 WriteLog(msg, sizeof(msg));
370 return;
371 }
372 STAT_STRUCT st;
373 if (FSTAT_FUNC(dumpfd, &st) != 0) {
374 static const char msg[] = "Cannot upload crash dump: stat failed\n";
375 WriteLog(msg, sizeof(msg));
376 IGNORE_RET(sys_close(dumpfd));
377 return;
378 }
379
380 google_breakpad::PageAllocator allocator;
381
382 uint8_t* dump_data = reinterpret_cast<uint8_t*>(allocator.Alloc(st.st_size));
383 if (!dump_data) {
384 static const char msg[] = "Cannot upload crash dump: cannot alloc\n";
385 WriteLog(msg, sizeof(msg));
386 IGNORE_RET(sys_close(dumpfd));
387 return;
388 }
389
390 sys_read(dumpfd, dump_data, st.st_size);
391 IGNORE_RET(sys_close(dumpfd));
392
393 // We need to build a MIME block for uploading to the server. Since we are
394 // going to fork and run wget, it needs to be written to a temp file.
395
396 const int ufd = sys_open("/dev/urandom", O_RDONLY, 0);
397 if (ufd < 0) {
398 static const char msg[] = "Cannot upload crash dump because /dev/urandom"
399 " is missing\n";
400 WriteLog(msg, sizeof(msg) - 1);
401 return;
402 }
403
404 static const char temp_file_template[] =
405 "/tmp/chromium-upload-XXXXXXXXXXXXXXXX";
406 char temp_file[sizeof(temp_file_template)];
407 int temp_file_fd = -1;
408 if (info.upload) {
409 memcpy(temp_file, temp_file_template, sizeof(temp_file_template));
410
411 for (unsigned i = 0; i < 10; ++i) {
412 uint64_t t;
413 sys_read(ufd, &t, sizeof(t));
414 write_uint64_hex(temp_file + sizeof(temp_file) - (16 + 1), t);
415
416 temp_file_fd = sys_open(temp_file, O_WRONLY | O_CREAT | O_EXCL, 0600);
417 if (temp_file_fd >= 0)
418 break;
419 }
420
421 if (temp_file_fd < 0) {
422 static const char msg[] = "Failed to create temporary file in /tmp: "
423 "cannot upload crash dump\n";
424 WriteLog(msg, sizeof(msg) - 1);
425 IGNORE_RET(sys_close(ufd));
426 return;
427 }
428 } else {
429 temp_file_fd = sys_open(info.filename, O_WRONLY, 0600);
430 if (temp_file_fd < 0) {
431 static const char msg[] = "Failed to save crash dump: failed to open\n";
432 WriteLog(msg, sizeof(msg) - 1);
433 IGNORE_RET(sys_close(ufd));
434 return;
435 }
436 }
437
438 // The MIME boundary is 28 hyphens, followed by a 64-bit nonce and a NUL.
439 char mime_boundary[28 + 16 + 1];
440 my_memset(mime_boundary, '-', 28);
441 uint64_t boundary_rand;
442 sys_read(ufd, &boundary_rand, sizeof(boundary_rand));
443 write_uint64_hex(mime_boundary + 28, boundary_rand);
444 mime_boundary[28 + 16] = 0;
445 IGNORE_RET(sys_close(ufd));
446
447 // The MIME block looks like this:
448 // BOUNDARY \r\n
449 // Content-Disposition: form-data; name="prod" \r\n \r\n
450 // Chrome_Linux \r\n
451 // BOUNDARY \r\n
452 // Content-Disposition: form-data; name="ver" \r\n \r\n
453 // 1.2.3.4 \r\n
454 // BOUNDARY \r\n
455 // Content-Disposition: form-data; name="guid" \r\n \r\n
456 // 1.2.3.4 \r\n
457 // BOUNDARY \r\n
458 //
459 // zero or one:
460 // Content-Disposition: form-data; name="ptime" \r\n \r\n
461 // abcdef \r\n
462 // BOUNDARY \r\n
463 //
464 // zero or one:
465 // Content-Disposition: form-data; name="ptype" \r\n \r\n
466 // abcdef \r\n
467 // BOUNDARY \r\n
468 //
469 // zero or more gpu entries:
470 // Content-Disposition: form-data; name="gpu-xxxxx" \r\n \r\n
471 // <gpu-xxxxx> \r\n
472 // BOUNDARY \r\n
473 //
474 // zero or one:
475 // Content-Disposition: form-data; name="lsb-release" \r\n \r\n
476 // abcdef \r\n
477 // BOUNDARY \r\n
478 //
479 // zero or more:
480 // Content-Disposition: form-data; name="url-chunk-1" \r\n \r\n
481 // abcdef \r\n
482 // BOUNDARY \r\n
483 //
484 // zero or one:
485 // Content-Disposition: form-data; name="channel" \r\n \r\n
486 // beta \r\n
487 // BOUNDARY \r\n
488 //
489 // zero or one:
490 // Content-Disposition: form-data; name="num-views" \r\n \r\n
491 // 3 \r\n
492 // BOUNDARY \r\n
493 //
494 // zero or one:
495 // Content-Disposition: form-data; name="num-extensions" \r\n \r\n
496 // 5 \r\n
497 // BOUNDARY \r\n
498 //
499 // zero to 10:
500 // Content-Disposition: form-data; name="extension-1" \r\n \r\n
501 // abcdefghijklmnopqrstuvwxyzabcdef \r\n
502 // BOUNDARY \r\n
503 //
504 // zero to 4:
505 // Content-Disposition: form-data; name="prn-info-1" \r\n \r\n
506 // abcdefghijklmnopqrstuvwxyzabcdef \r\n
507 // BOUNDARY \r\n
508 //
509 // zero or one:
510 // Content-Disposition: form-data; name="num-switches" \r\n \r\n
511 // 5 \r\n
512 // BOUNDARY \r\n
513 //
514 // zero to 15:
515 // Content-Disposition: form-data; name="switch-1" \r\n \r\n
516 // --foo \r\n
517 // BOUNDARY \r\n
518 //
519 // zero or one:
520 // Content-Disposition: form-data; name="oom-size" \r\n \r\n
521 // 1234567890 \r\n
522 // BOUNDARY \r\n
523 //
524 // Content-Disposition: form-data; name="dump"; filename="dump" \r\n
525 // Content-Type: application/octet-stream \r\n \r\n
526 // <dump contents>
527 // \r\n BOUNDARY -- \r\n
528
529 MimeWriter writer(temp_file_fd, mime_boundary);
530 {
531 #if defined(OS_ANDROID)
532 static const char chrome_product_msg[] = "Chrome_Android";
533 #elif defined(OS_CHROMEOS)
534 static const char chrome_product_msg[] = "Chrome_ChromeOS";
535 #else // OS_LINUX
536 static const char chrome_product_msg[] = "Chrome_Linux";
537 #endif
538
539 #if defined (OS_ANDROID)
540 base::android::BuildInfo* android_build_info =
541 base::android::BuildInfo::GetInstance();
542 static const char* version_msg =
543 android_build_info->package_version_code();
544 #else
545 static const char version_msg[] = PRODUCT_VERSION;
546 #endif
547
548 writer.AddBoundary();
549 writer.AddPairString("prod", chrome_product_msg);
550 writer.AddBoundary();
551 writer.AddPairString("ver", version_msg);
552 writer.AddBoundary();
553 writer.AddPairString("guid", info.guid);
554 writer.AddBoundary();
555 if (info.pid > 0) {
556 char pid_value_buf[kUint64StringSize];
557 uint64_t pid_value_len = my_uint64_len(info.pid);
558 my_uint64tos(pid_value_buf, info.pid, pid_value_len);
559 static const char pid_key_name[] = "pid";
560 writer.AddPairData(pid_key_name, sizeof(pid_key_name) - 1,
561 pid_value_buf, pid_value_len);
562 writer.AddBoundary();
563 }
564 #if defined(OS_ANDROID)
565 // Addtional MIME blocks are added for logging on Android devices.
566 static const char android_build_id[] = "android_build_id";
567 static const char android_build_fp[] = "android_build_fp";
568 static const char device[] = "device";
569 static const char model[] = "model";
570 static const char brand[] = "brand";
571
572 writer.AddPairString(
573 android_build_id, android_build_info->android_build_id());
574 writer.AddBoundary();
575 writer.AddPairString(
576 android_build_fp, android_build_info->android_build_fp());
577 writer.AddBoundary();
578 writer.AddPairString(device, android_build_info->device());
579 writer.AddBoundary();
580 writer.AddPairString(model, android_build_info->model());
581 writer.AddBoundary();
582 writer.AddPairString(brand, android_build_info->brand());
583 writer.AddBoundary();
584 #endif
585 writer.Flush();
586 }
587
588 if (info.process_start_time > 0) {
589 struct kernel_timeval tv;
590 if (!sys_gettimeofday(&tv, NULL)) {
591 uint64_t time = kernel_timeval_to_ms(&tv);
592 if (time > info.process_start_time) {
593 time -= info.process_start_time;
594 char time_str[kUint64StringSize];
595 const unsigned time_len = my_uint64_len(time);
596 my_uint64tos(time_str, time, time_len);
597
598 static const char process_time_msg[] = "ptime";
599 writer.AddPairData(process_time_msg, sizeof(process_time_msg) - 1,
600 time_str, time_len);
601 writer.AddBoundary();
602 writer.Flush();
603 }
604 }
605 }
606
607 if (info.process_type_length) {
608 writer.AddPairString("ptype", info.process_type);
609 writer.AddBoundary();
610 writer.Flush();
611 }
612
613 // If GPU info is known, send it.
614 unsigned gpu_vendor_len = my_strlen(child_process_logging::g_gpu_vendor_id);
615 if (gpu_vendor_len) {
616 static const char vendor_msg[] = "gpu-venid";
617 static const char device_msg[] = "gpu-devid";
618 static const char driver_msg[] = "gpu-driver";
619 static const char psver_msg[] = "gpu-psver";
620 static const char vsver_msg[] = "gpu-vsver";
621
622 writer.AddPairString(vendor_msg, child_process_logging::g_gpu_vendor_id);
623 writer.AddBoundary();
624 writer.AddPairString(device_msg, child_process_logging::g_gpu_device_id);
625 writer.AddBoundary();
626 writer.AddPairString(driver_msg, child_process_logging::g_gpu_driver_ver);
627 writer.AddBoundary();
628 writer.AddPairString(psver_msg, child_process_logging::g_gpu_ps_ver);
629 writer.AddBoundary();
630 writer.AddPairString(vsver_msg, child_process_logging::g_gpu_vs_ver);
631 writer.AddBoundary();
632 writer.Flush();
633 }
634
635 if (info.distro_length) {
636 static const char distro_msg[] = "lsb-release";
637 writer.AddPairString(distro_msg, info.distro);
638 writer.AddBoundary();
639 writer.Flush();
640 }
641
642 // For renderers and plugins.
643 if (info.crash_url_length) {
644 static const char url_chunk_msg[] = "url-chunk-";
645 static const unsigned kMaxUrlLength = 8 * MimeWriter::kMaxCrashChunkSize;
646 writer.AddPairDataInChunks(url_chunk_msg, sizeof(url_chunk_msg) - 1,
647 info.crash_url, std::min(info.crash_url_length, kMaxUrlLength),
648 MimeWriter::kMaxCrashChunkSize, false /* Don't strip whitespaces. */);
649 }
650
651 if (my_strlen(child_process_logging::g_channel)) {
652 writer.AddPairString("channel", child_process_logging::g_channel);
653 writer.AddBoundary();
654 writer.Flush();
655 }
656
657 if (my_strlen(child_process_logging::g_num_views)) {
658 writer.AddPairString("num-views", child_process_logging::g_num_views);
659 writer.AddBoundary();
660 writer.Flush();
661 }
662
663 if (my_strlen(child_process_logging::g_num_extensions)) {
664 writer.AddPairString("num-extensions",
665 child_process_logging::g_num_extensions);
666 writer.AddBoundary();
667 writer.Flush();
668 }
669
670 unsigned extension_ids_len =
671 my_strlen(child_process_logging::g_extension_ids);
672 if (extension_ids_len) {
673 static const char extension_msg[] = "extension-";
674 static const unsigned kMaxExtensionsLen =
675 kMaxReportedActiveExtensions * child_process_logging::kExtensionLen;
676 writer.AddPairDataInChunks(extension_msg, sizeof(extension_msg) - 1,
677 child_process_logging::g_extension_ids,
678 std::min(extension_ids_len, kMaxExtensionsLen),
679 child_process_logging::kExtensionLen,
680 false /* Don't strip whitespace. */);
681 }
682
683 unsigned printer_info_len =
684 my_strlen(child_process_logging::g_printer_info);
685 if (printer_info_len) {
686 static const char printer_info_msg[] = "prn-info-";
687 static const unsigned kMaxPrnInfoLen =
688 kMaxReportedPrinterRecords * child_process_logging::kPrinterInfoStrLen;
689 writer.AddPairDataInChunks(printer_info_msg, sizeof(printer_info_msg) - 1,
690 child_process_logging::g_printer_info,
691 std::min(printer_info_len, kMaxPrnInfoLen),
692 child_process_logging::kPrinterInfoStrLen,
693 true);
694 }
695
696 if (my_strlen(child_process_logging::g_num_switches)) {
697 writer.AddPairString("num-switches",
698 child_process_logging::g_num_switches);
699 writer.AddBoundary();
700 writer.Flush();
701 }
702
703 unsigned switches_len =
704 my_strlen(child_process_logging::g_switches);
705 if (switches_len) {
706 static const char switch_msg[] = "switch-";
707 static const unsigned kMaxSwitchLen =
708 kMaxSwitches * child_process_logging::kSwitchLen;
709 writer.AddPairDataInChunks(switch_msg, sizeof(switch_msg) - 1,
710 child_process_logging::g_switches,
711 std::min(switches_len, kMaxSwitchLen),
712 child_process_logging::kSwitchLen,
713 true /* Strip whitespace since switches are padded to kSwitchLen. */);
714 }
715
716 if (info.oom_size) {
717 char oom_size_str[kUint64StringSize];
718 const unsigned oom_size_len = my_uint64_len(info.oom_size);
719 my_uint64tos(oom_size_str, info.oom_size, oom_size_len);
720 static const char oom_size_msg[] = "oom-size";
721 writer.AddPairData(oom_size_msg, sizeof(oom_size_msg) - 1,
722 oom_size_str, oom_size_len);
723 writer.AddBoundary();
724 writer.Flush();
725 }
726
727 writer.AddFileDump(dump_data, st.st_size);
728 writer.AddEnd();
729 writer.Flush();
730
731 IGNORE_RET(sys_close(temp_file_fd));
732
733 #if defined(OS_ANDROID)
734 __android_log_write(ANDROID_LOG_WARN,
735 kGoogleBreakpad,
736 "Output crash dump file:");
737 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, info.filename);
738
739 char pid_buf[kUint64StringSize];
740 uint64_t pid_str_len = my_uint64_len(info.pid);
741 my_uint64tos(pid_buf, info.pid, pid_str_len);
742
743 // -1 because we won't need the null terminator on the original filename.
744 size_t done_filename_len = my_strlen(info.filename) + pid_str_len - 1;
745 char* done_filename = reinterpret_cast<char*>(
746 allocator.Alloc(done_filename_len));
747 // Rename the file such that the pid is the suffix in order to signal other
748 // processes that the minidump is complete. The advantage of using the pid as
749 // the suffix is that it is trivial to associate the minidump with the
750 // crashed process.
751 // Finally, note strncpy prevents null terminators from
752 // being copied. Pad the rest with 0's.
753 my_strncpy(done_filename, info.filename, done_filename_len);
754 // Append the suffix a null terminator should be added.
755 my_strncat(done_filename, pid_buf, pid_str_len);
756 // Rename the minidump file to signal that it is complete.
757 if (rename(info.filename, done_filename)) {
758 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, "Failed to rename:");
759 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, info.filename);
760 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, "to");
761 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, done_filename);
762 }
763 #endif
764
765 if (!info.upload)
766 return;
767
768 // The --header argument to wget looks like:
769 // --header=Content-Type: multipart/form-data; boundary=XYZ
770 // where the boundary has two fewer leading '-' chars
771 static const char header_msg[] =
772 "--header=Content-Type: multipart/form-data; boundary=";
773 char* const header = reinterpret_cast<char*>(allocator.Alloc(
774 sizeof(header_msg) - 1 + sizeof(mime_boundary) - 2));
775 memcpy(header, header_msg, sizeof(header_msg) - 1);
776 memcpy(header + sizeof(header_msg) - 1, mime_boundary + 2,
777 sizeof(mime_boundary) - 2);
778 // We grab the NUL byte from the end of |mime_boundary|.
779
780 // The --post-file argument to wget looks like:
781 // --post-file=/tmp/...
782 static const char post_file_msg[] = "--post-file=";
783 char* const post_file = reinterpret_cast<char*>(allocator.Alloc(
784 sizeof(post_file_msg) - 1 + sizeof(temp_file)));
785 memcpy(post_file, post_file_msg, sizeof(post_file_msg) - 1);
786 memcpy(post_file + sizeof(post_file_msg) - 1, temp_file, sizeof(temp_file));
787
788 const pid_t child = sys_fork();
789 if (!child) {
790 // Spawned helper process.
791 //
792 // This code is called both when a browser is crashing (in which case,
793 // nothing really matters any more) and when a renderer/plugin crashes, in
794 // which case we need to continue.
795 //
796 // Since we are a multithreaded app, if we were just to fork(), we might
797 // grab file descriptors which have just been created in another thread and
798 // hold them open for too long.
799 //
800 // Thus, we have to loop and try and close everything.
801 const int fd = sys_open("/proc/self/fd", O_DIRECTORY | O_RDONLY, 0);
802 if (fd < 0) {
803 for (unsigned i = 3; i < 8192; ++i)
804 IGNORE_RET(sys_close(i));
805 } else {
806 google_breakpad::DirectoryReader reader(fd);
807 const char* name;
808 while (reader.GetNextEntry(&name)) {
809 int i;
810 if (my_strtoui(&i, name) && i > 2 && i != fd)
811 IGNORE_RET(sys_close(i));
812 reader.PopEntry();
813 }
814
815 IGNORE_RET(sys_close(fd));
816 }
817
818 IGNORE_RET(sys_setsid());
819
820 // Leave one end of a pipe in the wget process and watch for it getting
821 // closed by the wget process exiting.
822 int fds[2];
823 if (sys_pipe(fds) >= 0) {
824 const pid_t wget_child = sys_fork();
825 if (!wget_child) {
826 // Wget process.
827 IGNORE_RET(sys_close(fds[0]));
828 IGNORE_RET(sys_dup2(fds[1], 3));
829 static const char kWgetBinary[] = "/usr/bin/wget";
830 const char* args[] = {
831 kWgetBinary,
832 header,
833 post_file,
834 kUploadURL,
835 "--timeout=10", // Set a timeout so we don't hang forever.
836 "--tries=1", // Don't retry if the upload fails.
837 "-O", // output reply to fd 3
838 "/dev/fd/3",
839 NULL,
840 };
841
842 execve(kWgetBinary, const_cast<char**>(args), environ);
843 static const char msg[] = "Cannot upload crash dump: cannot exec "
844 "/usr/bin/wget\n";
845 WriteLog(msg, sizeof(msg) - 1);
846 sys__exit(1);
847 }
848
849 // Helper process.
850 if (wget_child > 0) {
851 IGNORE_RET(sys_close(fds[1]));
852 char id_buf[17]; // Crash report IDs are expected to be 16 chars.
853 ssize_t len = -1;
854 // Wget should finish in about 10 seconds. Add a few more 500 ms
855 // internals to account for process startup time.
856 for (size_t wait_count = 0; wait_count < 24; ++wait_count) {
857 struct kernel_pollfd poll_fd;
858 poll_fd.fd = fds[0];
859 poll_fd.events = POLLIN | POLLPRI | POLLERR;
860 int ret = sys_poll(&poll_fd, 1, 500);
861 if (ret < 0) {
862 // Error
863 break;
864 } else if (ret > 0) {
865 // There is data to read.
866 len = HANDLE_EINTR(sys_read(fds[0], id_buf, sizeof(id_buf) - 1));
867 break;
868 }
869 // ret == 0 -> timed out, continue waiting.
870 }
871 if (len > 0) {
872 // Write crash dump id to stderr.
873 id_buf[len] = 0;
874 static const char msg[] = "\nCrash dump id: ";
875 WriteLog(msg, sizeof(msg) - 1);
876 WriteLog(id_buf, my_strlen(id_buf));
877 WriteLog("\n", 1);
878
879 // Write crash dump id to crash log as: seconds_since_epoch,crash_id
880 struct kernel_timeval tv;
881 if (g_crash_log_path && !sys_gettimeofday(&tv, NULL)) {
882 uint64_t time = kernel_timeval_to_ms(&tv) / 1000;
883 char time_str[kUint64StringSize];
884 const unsigned time_len = my_uint64_len(time);
885 my_uint64tos(time_str, time, time_len);
886
887 int log_fd = sys_open(g_crash_log_path,
888 O_CREAT | O_WRONLY | O_APPEND,
889 0600);
890 if (log_fd > 0) {
891 sys_write(log_fd, time_str, time_len);
892 sys_write(log_fd, ",", 1);
893 sys_write(log_fd, id_buf, my_strlen(id_buf));
894 sys_write(log_fd, "\n", 1);
895 IGNORE_RET(sys_close(log_fd));
896 }
897 }
898 }
899 if (sys_waitpid(wget_child, NULL, WNOHANG) == 0) {
900 // Wget process is still around, kill it.
901 sys_kill(wget_child, SIGKILL);
902 }
903 }
904 }
905
906 // Helper process.
907 IGNORE_RET(sys_unlink(info.filename));
908 IGNORE_RET(sys_unlink(temp_file));
909 sys__exit(0);
910 }
911
912 // Main browser process.
913 if (child <= 0)
914 return;
915 (void) HANDLE_EINTR(sys_waitpid(child, NULL, 0));
916 }
917
918 static bool CrashDone(const char* dump_path,
919 const char* minidump_id,
920 const bool upload,
921 const bool succeeded) {
922 // WARNING: this code runs in a compromised context. It may not call into
923 // libc nor allocate memory normally.
924 if (!succeeded)
925 return false;
926
927 google_breakpad::PageAllocator allocator;
928 const unsigned dump_path_len = my_strlen(dump_path);
929 const unsigned minidump_id_len = my_strlen(minidump_id);
930 char* const path = reinterpret_cast<char*>(allocator.Alloc(
931 dump_path_len + 1 /* '/' */ + minidump_id_len +
932 4 /* ".dmp" */ + 1 /* NUL */));
933 memcpy(path, dump_path, dump_path_len);
934 path[dump_path_len] = '/';
935 memcpy(path + dump_path_len + 1, minidump_id, minidump_id_len);
936 memcpy(path + dump_path_len + 1 + minidump_id_len, ".dmp", 4);
937 path[dump_path_len + 1 + minidump_id_len + 4] = 0;
938
939 BreakpadInfo info;
940 info.filename = path;
941 info.process_type = "browser";
942 info.process_type_length = 7;
943 info.crash_url = NULL;
944 info.crash_url_length = 0;
945 info.guid = child_process_logging::g_client_id;
946 info.guid_length = my_strlen(child_process_logging::g_client_id);
947 info.distro = base::g_linux_distro;
948 info.distro_length = my_strlen(base::g_linux_distro);
949 info.upload = upload;
950 info.process_start_time = g_process_start_time;
951 info.oom_size = base::g_oom_size;
952 info.pid = 0;
953 HandleCrashDump(info);
954 return true;
955 }
956
957 // Wrapper function, do not add more code here.
958 static bool CrashDoneNoUpload(const char* dump_path,
959 const char* minidump_id,
960 void* context,
961 bool succeeded) {
962 return CrashDone(dump_path, minidump_id, false, succeeded);
963 }
964
965 #if !defined(OS_ANDROID)
966 // Wrapper function, do not add more code here.
967 static bool CrashDoneUpload(const char* dump_path,
968 const char* minidump_id,
969 void* context,
970 bool succeeded) {
971 return CrashDone(dump_path, minidump_id, true, succeeded);
972 }
973 #endif
974
975 static void EnableCrashDumping(bool unattended) {
976 g_is_crash_reporter_enabled = true;
977
978 FilePath tmp_path("/tmp");
979 PathService::Get(base::DIR_TEMP, &tmp_path);
980
981 FilePath dumps_path(tmp_path);
982 if (PathService::Get(chrome::DIR_CRASH_DUMPS, &dumps_path)) {
983 FilePath logfile =
984 dumps_path.AppendASCII(CrashUploadList::kReporterLogFilename);
985 std::string logfile_str = logfile.value();
986 const size_t crash_log_path_len = logfile_str.size() + 1;
987 g_crash_log_path = new char[crash_log_path_len];
988 strncpy(g_crash_log_path, logfile_str.c_str(), crash_log_path_len);
989 }
990 DCHECK(!g_breakpad);
991 #if defined(OS_ANDROID)
992 unattended = true; // Android never uploads directly.
993 #endif
994 if (unattended) {
995 g_breakpad = new google_breakpad::ExceptionHandler(
996 dumps_path.value().c_str(),
997 NULL,
998 CrashDoneNoUpload,
999 NULL,
1000 true /* install handlers */);
1001 return;
1002 }
1003
1004 #if !defined(OS_ANDROID)
1005 // Attended mode
1006 g_breakpad = new google_breakpad::ExceptionHandler(
1007 tmp_path.value().c_str(),
1008 NULL,
1009 CrashDoneUpload,
1010 NULL,
1011 true /* install handlers */);
1012 #endif
1013 }
1014
1015 // Non-Browser = Extension, Gpu, Plugins, Ppapi and Renderer
1016 static bool NonBrowserCrashHandler(const void* crash_context,
1017 size_t crash_context_size,
1018 void* context) {
1019 const int fd = reinterpret_cast<intptr_t>(context);
1020 int fds[2] = { -1, -1 };
1021 if (sys_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
1022 static const char msg[] = "Failed to create socket for crash dumping.\n";
1023 WriteLog(msg, sizeof(msg)-1);
1024 return false;
1025 }
1026
1027 // Start constructing the message to send to the browser.
1028 char guid[kGuidSize + 1] = {0};
1029 char crash_url[kMaxActiveURLSize + 1] = {0};
1030 char distro[kDistroSize + 1] = {0};
1031 const size_t guid_len =
1032 std::min(my_strlen(child_process_logging::g_client_id), kGuidSize);
1033 const size_t crash_url_len =
1034 std::min(my_strlen(child_process_logging::g_active_url),
1035 kMaxActiveURLSize);
1036 const size_t distro_len =
1037 std::min(my_strlen(base::g_linux_distro), kDistroSize);
1038 memcpy(guid, child_process_logging::g_client_id, guid_len);
1039 memcpy(crash_url, child_process_logging::g_active_url, crash_url_len);
1040 memcpy(distro, base::g_linux_distro, distro_len);
1041
1042 char b; // Dummy variable for sys_read below.
1043 const char* b_addr = &b; // Get the address of |b| so we can create the
1044 // expected /proc/[pid]/syscall content in the
1045 // browser to convert namespace tids.
1046
1047 // The length of the control message:
1048 static const unsigned kControlMsgSize = sizeof(fds);
1049 static const unsigned kControlMsgSpaceSize = CMSG_SPACE(kControlMsgSize);
1050 static const unsigned kControlMsgLenSize = CMSG_LEN(kControlMsgSize);
1051
1052 const size_t kIovSize = 8;
1053 struct kernel_msghdr msg;
1054 my_memset(&msg, 0, sizeof(struct kernel_msghdr));
1055 struct kernel_iovec iov[kIovSize];
1056 iov[0].iov_base = const_cast<void*>(crash_context);
1057 iov[0].iov_len = crash_context_size;
1058 iov[1].iov_base = guid;
1059 iov[1].iov_len = kGuidSize + 1;
1060 iov[2].iov_base = crash_url;
1061 iov[2].iov_len = kMaxActiveURLSize + 1;
1062 iov[3].iov_base = distro;
1063 iov[3].iov_len = kDistroSize + 1;
1064 iov[4].iov_base = &b_addr;
1065 iov[4].iov_len = sizeof(b_addr);
1066 iov[5].iov_base = &fds[0];
1067 iov[5].iov_len = sizeof(fds[0]);
1068 iov[6].iov_base = &g_process_start_time;
1069 iov[6].iov_len = sizeof(g_process_start_time);
1070 iov[7].iov_base = &base::g_oom_size;
1071 iov[7].iov_len = sizeof(base::g_oom_size);
1072
1073 msg.msg_iov = iov;
1074 msg.msg_iovlen = kIovSize;
1075 char cmsg[kControlMsgSpaceSize];
1076 my_memset(cmsg, 0, kControlMsgSpaceSize);
1077 msg.msg_control = cmsg;
1078 msg.msg_controllen = sizeof(cmsg);
1079
1080 struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
1081 hdr->cmsg_level = SOL_SOCKET;
1082 hdr->cmsg_type = SCM_RIGHTS;
1083 hdr->cmsg_len = kControlMsgLenSize;
1084 ((int*) CMSG_DATA(hdr))[0] = fds[0];
1085 ((int*) CMSG_DATA(hdr))[1] = fds[1];
1086
1087 if (HANDLE_EINTR(sys_sendmsg(fd, &msg, 0)) < 0) {
1088 static const char errmsg[] = "Failed to tell parent about crash.\n";
1089 WriteLog(errmsg, sizeof(errmsg)-1);
1090 IGNORE_RET(sys_close(fds[1]));
1091 return false;
1092 }
1093 IGNORE_RET(sys_close(fds[1]));
1094
1095 if (HANDLE_EINTR(sys_read(fds[0], &b, 1)) != 1) {
1096 static const char errmsg[] = "Parent failed to complete crash dump.\n";
1097 WriteLog(errmsg, sizeof(errmsg)-1);
1098 }
1099
1100 return true;
1101 }
1102
1103 static void EnableNonBrowserCrashDumping() {
1104 const int fd = base::GlobalDescriptors::GetInstance()->Get(kCrashDumpSignal);
1105 g_is_crash_reporter_enabled = true;
1106 // We deliberately leak this object.
1107 DCHECK(!g_breakpad);
1108 g_breakpad = new google_breakpad::ExceptionHandler(
1109 "" /* unused */, NULL, NULL, reinterpret_cast<void*>(fd), true);
1110 g_breakpad->set_crash_handler(NonBrowserCrashHandler);
1111 }
1112
1113 void InitCrashReporter() {
1114 #if defined(OS_ANDROID)
1115 // This will guarantee that the BuildInfo has been initialized and subsequent
1116 // calls will not require memory allocation.
1117 base::android::BuildInfo::GetInstance();
1118 #endif
1119 // Determine the process type and take appropriate action.
1120 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
1121 if (parsed_command_line.HasSwitch(switches::kDisableBreakpad))
1122 return;
1123
1124 const std::string process_type =
1125 parsed_command_line.GetSwitchValueASCII(switches::kProcessType);
1126 if (process_type.empty()) {
1127 EnableCrashDumping(getenv(env_vars::kHeadless) != NULL);
1128 } else if (process_type == switches::kRendererProcess ||
1129 process_type == switches::kPluginProcess ||
1130 process_type == switches::kPpapiPluginProcess ||
1131 process_type == switches::kZygoteProcess ||
1132 process_type == switches::kGpuProcess) {
1133 #if defined(OS_ANDROID)
1134 child_process_logging::SetClientId("Android");
1135 #endif
1136 // We might be chrooted in a zygote or renderer process so we cannot call
1137 // GetCollectStatsConsent because that needs access the the user's home
1138 // dir. Instead, we set a command line flag for these processes.
1139 // Even though plugins are not chrooted, we share the same code path for
1140 // simplicity.
1141 if (!parsed_command_line.HasSwitch(switches::kEnableCrashReporter))
1142 return;
1143 // Get the guid and linux distro from the command line switch.
1144 std::string switch_value =
1145 parsed_command_line.GetSwitchValueASCII(switches::kEnableCrashReporter);
1146 size_t separator = switch_value.find(",");
1147 if (separator != std::string::npos) {
1148 child_process_logging::SetClientId(switch_value.substr(0, separator));
1149 base::SetLinuxDistro(switch_value.substr(separator + 1));
1150 } else {
1151 child_process_logging::SetClientId(switch_value);
1152 }
1153 EnableNonBrowserCrashDumping();
1154 }
1155
1156 // Set the base process start time value.
1157 struct timeval tv;
1158 if (!gettimeofday(&tv, NULL))
1159 g_process_start_time = timeval_to_ms(&tv);
1160 else
1161 g_process_start_time = 0;
1162
1163 logging::SetDumpWithoutCrashingFunction(&DumpProcess);
1164 }
1165
1166 bool IsCrashReporterEnabled() {
1167 return g_is_crash_reporter_enabled;
1168 }
OLDNEW
« no previous file with comments | « chrome/app/breakpad_linuxish.h ('k') | chrome/app/chrome_main_delegate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698