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

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

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