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

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

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