Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, Google Inc. | 1 // Copyright (c) 2014, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | 29 |
| 30 // This translation unit generates microdumps into the console (logcat on | 30 // This translation unit generates microdumps into the console (logcat on |
| 31 // Android). See crbug.com/410294 for more info and design docs. | 31 // Android). See crbug.com/410294 for more info and design docs. |
| 32 | 32 |
| 33 #include "client/linux/microdump_writer/microdump_writer.h" | 33 #include "client/linux/microdump_writer/microdump_writer.h" |
| 34 | 34 |
| 35 #include <sys/utsname.h> | 35 #include <sys/utsname.h> |
| 36 | 36 |
| 37 #include <algorithm> | |
| 38 | |
| 39 #include "client/linux/dump_writer_common/thread_info.h" | 37 #include "client/linux/dump_writer_common/thread_info.h" |
| 40 #include "client/linux/dump_writer_common/ucontext_reader.h" | 38 #include "client/linux/dump_writer_common/ucontext_reader.h" |
| 41 #include "client/linux/handler/exception_handler.h" | 39 #include "client/linux/handler/exception_handler.h" |
| 42 #include "client/linux/handler/microdump_extra_info.h" | 40 #include "client/linux/handler/microdump_extra_info.h" |
| 43 #include "client/linux/log/log.h" | 41 #include "client/linux/log/log.h" |
| 44 #include "client/linux/minidump_writer/linux_ptrace_dumper.h" | 42 #include "client/linux/minidump_writer/linux_ptrace_dumper.h" |
| 45 #include "common/linux/file_id.h" | 43 #include "common/linux/file_id.h" |
| 46 #include "common/linux/linux_libc_support.h" | 44 #include "common/linux/linux_libc_support.h" |
| 45 #include "common/memory.h" | |
| 47 | 46 |
| 48 namespace { | 47 namespace { |
| 49 | 48 |
| 50 using google_breakpad::auto_wasteful_vector; | 49 using google_breakpad::auto_wasteful_vector; |
| 51 using google_breakpad::ExceptionHandler; | 50 using google_breakpad::ExceptionHandler; |
| 52 using google_breakpad::kDefaultBuildIdSize; | 51 using google_breakpad::kDefaultBuildIdSize; |
| 53 using google_breakpad::LinuxDumper; | 52 using google_breakpad::LinuxDumper; |
| 54 using google_breakpad::LinuxPtraceDumper; | 53 using google_breakpad::LinuxPtraceDumper; |
| 55 using google_breakpad::MappingInfo; | 54 using google_breakpad::MappingInfo; |
| 56 using google_breakpad::MappingList; | 55 using google_breakpad::MappingList; |
| 57 using google_breakpad::MicrodumpExtraInfo; | 56 using google_breakpad::MicrodumpExtraInfo; |
| 58 using google_breakpad::RawContextCPU; | 57 using google_breakpad::RawContextCPU; |
| 59 using google_breakpad::ThreadInfo; | 58 using google_breakpad::ThreadInfo; |
| 60 using google_breakpad::UContextReader; | 59 using google_breakpad::UContextReader; |
| 61 | 60 |
| 62 const size_t kLineBufferSize = 2048; | 61 const size_t kLineBufferSize = 2048; |
| 63 | 62 |
| 63 int Log2Floor(uint64_t n) { | |
| 64 // Copied from chromium src/base/bits.h | |
| 65 if (n == 0) | |
| 66 return -1; | |
| 67 int log = 0; | |
| 68 uint64_t value = n; | |
| 69 for (int i = 5; i >= 0; --i) { | |
| 70 int shift = (1 << i); | |
| 71 uint64_t x = value >> shift; | |
| 72 if (x != 0) { | |
| 73 value = x; | |
| 74 log += shift; | |
| 75 } | |
| 76 } | |
| 77 assert(value == 1u); | |
| 78 return log; | |
| 79 } | |
| 80 | |
| 81 bool MappingsAreAdjacent(const MappingInfo* a, const MappingInfo* b) { | |
| 82 // Because of load biasing, we can end up with a situation where two | |
| 83 // mappings actually overlap. So we will define adjacency to also include a | |
| 84 // b start address that lies within a's address range (including starting | |
| 85 // immediately after a). | |
| 86 // Because load biasing only ever moves the start address backwards, the end | |
| 87 // address should still increase. | |
| 88 return a->start_addr <= b->start_addr && | |
| 89 a->start_addr + a->size >= b->start_addr; | |
| 90 } | |
| 91 | |
| 92 size_t NextOrderedMapping( | |
| 93 const google_breakpad::wasteful_vector<MappingInfo*>& mappings, | |
| 94 size_t curr) { | |
| 95 size_t best = curr; | |
| 96 for (size_t next = 0; next < mappings.size(); ++next) { | |
| 97 if (mappings[next]->start_addr > mappings[curr]->start_addr && | |
| 98 mappings[next]->start_addr < mappings[best]->start_addr) { | |
|
Tobias Sargeant
2016/05/13 16:16:54
Testing showed this is wrong. I rewrote it (actual
| |
| 99 best = next; | |
| 100 } | |
| 101 } | |
| 102 return best; | |
| 103 } | |
| 104 | |
| 64 class MicrodumpWriter { | 105 class MicrodumpWriter { |
| 65 public: | 106 public: |
| 66 MicrodumpWriter(const ExceptionHandler::CrashContext* context, | 107 MicrodumpWriter(const ExceptionHandler::CrashContext* context, |
| 67 const MappingList& mappings, | 108 const MappingList& mappings, |
| 68 const MicrodumpExtraInfo& microdump_extra_info, | 109 const MicrodumpExtraInfo& microdump_extra_info, |
| 69 LinuxDumper* dumper) | 110 LinuxDumper* dumper) |
| 70 : ucontext_(context ? &context->context : NULL), | 111 : ucontext_(context ? &context->context : NULL), |
| 71 #if !defined(__ARM_EABI__) && !defined(__mips__) | 112 #if !defined(__ARM_EABI__) && !defined(__mips__) |
| 72 float_state_(context ? &context->float_state : NULL), | 113 float_state_(context ? &context->float_state : NULL), |
| 73 #endif | 114 #endif |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 91 return false; | 132 return false; |
| 92 return dumper_->ThreadsSuspend() && dumper_->LateInit(); | 133 return dumper_->ThreadsSuspend() && dumper_->LateInit(); |
| 93 } | 134 } |
| 94 | 135 |
| 95 bool Dump() { | 136 bool Dump() { |
| 96 bool success; | 137 bool success; |
| 97 LogLine("-----BEGIN BREAKPAD MICRODUMP-----"); | 138 LogLine("-----BEGIN BREAKPAD MICRODUMP-----"); |
| 98 DumpProductInformation(); | 139 DumpProductInformation(); |
| 99 DumpOSInformation(); | 140 DumpOSInformation(); |
| 100 DumpGPUInformation(); | 141 DumpGPUInformation(); |
| 142 #if !defined(__LP64__) | |
| 143 DumpFreeSpace(); | |
| 144 #endif | |
| 101 success = DumpCrashingThread(); | 145 success = DumpCrashingThread(); |
| 102 if (success) | 146 if (success) |
| 103 success = DumpMappings(); | 147 success = DumpMappings(); |
| 104 LogLine("-----END BREAKPAD MICRODUMP-----"); | 148 LogLine("-----END BREAKPAD MICRODUMP-----"); |
| 105 dumper_->ThreadsResume(); | 149 dumper_->ThreadsResume(); |
| 106 return success; | 150 return success; |
| 107 } | 151 } |
| 108 | 152 |
| 109 private: | 153 private: |
| 110 // Writes one line to the system log. | 154 // Writes one line to the system log. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 121 void LogAppend(const char* str) { | 165 void LogAppend(const char* str) { |
| 122 my_strlcat(log_line_, str, kLineBufferSize); | 166 my_strlcat(log_line_, str, kLineBufferSize); |
| 123 } | 167 } |
| 124 | 168 |
| 125 // As above (required to take precedence over template specialization below). | 169 // As above (required to take precedence over template specialization below). |
| 126 void LogAppend(char* str) { | 170 void LogAppend(char* str) { |
| 127 LogAppend(const_cast<const char*>(str)); | 171 LogAppend(const_cast<const char*>(str)); |
| 128 } | 172 } |
| 129 | 173 |
| 130 // Stages the hex repr. of the given int type in the current line buffer. | 174 // Stages the hex repr. of the given int type in the current line buffer. |
| 131 template<typename T> | 175 enum LeadingZeros { kKeepLeadingZeros, kDiscardLeadingZeros }; |
|
Primiano Tucci (use gerrit)
2016/05/10 16:20:49
can we avoid complicating this logic? I don't thin
Tobias Sargeant
2016/05/13 16:16:54
Done. I made the keys and values in the histogram
| |
| 132 void LogAppend(T value) { | 176 template <typename T> |
| 177 void LogAppend(T value, const LeadingZeros leading_zeros = | |
| 178 LeadingZeros::kKeepLeadingZeros) { | |
| 133 // Make enough room to hex encode the largest int type + NUL. | 179 // Make enough room to hex encode the largest int type + NUL. |
| 134 static const char HEX[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | 180 static const char HEX[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 135 'A', 'B', 'C', 'D', 'E', 'F'}; | 181 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 136 char hexstr[sizeof(T) * 2 + 1]; | 182 char hexstr[sizeof(T) * 2 + 1]; |
| 137 for (int i = sizeof(T) * 2 - 1; i >= 0; --i, value >>= 4) | 183 int i = sizeof(T) * 2; |
| 138 hexstr[i] = HEX[static_cast<uint8_t>(value) & 0x0F]; | 184 hexstr[i] = '\0'; |
| 139 hexstr[sizeof(T) * 2] = '\0'; | 185 do { |
| 140 LogAppend(hexstr); | 186 hexstr[--i] = HEX[static_cast<uint8_t>(value) & 0x0F]; |
| 187 value >>= 4; | |
| 188 } while (value || leading_zeros == kKeepLeadingZeros && i > 0); | |
| 189 LogAppend(hexstr + i); | |
| 141 } | 190 } |
| 142 | 191 |
| 143 // Stages the buffer content hex-encoded in the current line buffer. | 192 // Stages the buffer content hex-encoded in the current line buffer. |
| 144 void LogAppend(const void* buf, size_t length) { | 193 void LogAppend(const void* buf, size_t length) { |
| 145 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(buf); | 194 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(buf); |
| 146 for (size_t i = 0; i < length; ++i, ++ptr) | 195 for (size_t i = 0; i < length; ++i, ++ptr) |
| 147 LogAppend(*ptr); | 196 LogAppend(*ptr); |
| 148 } | 197 } |
| 149 | 198 |
| 150 // Writes out the current line buffer on the system log. | 199 // Writes out the current line buffer on the system log. |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 LogAppend(module_identifier.data4[3]); | 433 LogAppend(module_identifier.data4[3]); |
| 385 LogAppend(module_identifier.data4[4]); | 434 LogAppend(module_identifier.data4[4]); |
| 386 LogAppend(module_identifier.data4[5]); | 435 LogAppend(module_identifier.data4[5]); |
| 387 LogAppend(module_identifier.data4[6]); | 436 LogAppend(module_identifier.data4[6]); |
| 388 LogAppend(module_identifier.data4[7]); | 437 LogAppend(module_identifier.data4[7]); |
| 389 LogAppend("0 "); // Age is always 0 on Linux. | 438 LogAppend("0 "); // Age is always 0 on Linux. |
| 390 LogAppend(file_name); | 439 LogAppend(file_name); |
| 391 LogCommitLine(); | 440 LogCommitLine(); |
| 392 } | 441 } |
| 393 | 442 |
| 443 #if !defined(__LP64__) | |
| 444 bool DumpFreeSpace() { | |
| 445 const google_breakpad::wasteful_vector<MappingInfo*>& mappings = | |
| 446 dumper_->mappings(); | |
| 447 if (mappings.size() == 0) return false; | |
| 448 | |
| 449 // This is complicated by the fact that mappings is not in order. It should | |
| 450 // be mostly in order, however the mapping that contains the entry point for | |
| 451 // the process is always at the front of the vector. | |
| 452 | |
| 453 static const int HBITS = sizeof(size_t) * 8; | |
| 454 unsigned int hole_histogram[HBITS]; | |
| 455 my_memset(hole_histogram, 0, sizeof(hole_histogram)); | |
| 456 | |
| 457 // Find the lowest address mapping. | |
| 458 size_t curr = 0; | |
| 459 for (size_t i = 1; i < mappings.size(); ++i) { | |
| 460 if (mappings[i]->start_addr < mappings[curr]->start_addr) curr = i; | |
| 461 } | |
| 462 | |
| 463 uintptr_t lo_addr = mappings[curr]->start_addr; | |
| 464 | |
| 465 unsigned int hole_cnt = 0; | |
| 466 size_t hole_max = 0; | |
| 467 size_t hole_sum = 0; | |
| 468 | |
| 469 while (true) { | |
| 470 // Skip to the end of an adjacent run of mappings. This is an optimization | |
| 471 // for the fact that mappings is mostly sorted. | |
| 472 while (curr != mappings.size() - 1 && | |
| 473 MappingsAreAdjacent(mappings[curr], mappings[curr + 1])) | |
|
Primiano Tucci (use gerrit)
2016/05/10 16:20:49
add curly braces around this loop (the while is mu
Tobias Sargeant
2016/05/13 16:16:54
Done.
| |
| 474 ++curr; | |
| 475 | |
| 476 size_t next = NextOrderedMapping(mappings, curr); | |
| 477 if (next == curr) | |
| 478 break; | |
| 479 | |
| 480 uintptr_t hole_lo = mappings[curr]->start_addr + mappings[curr]->size; | |
| 481 uintptr_t hole_hi = mappings[next]->start_addr; | |
| 482 | |
| 483 if (hole_hi > hole_lo) { | |
| 484 size_t hole_sz = hole_hi - hole_lo; | |
| 485 hole_sum += hole_sz; | |
| 486 hole_max = std::max(hole_sz, hole_max); | |
| 487 ++hole_cnt; | |
| 488 ++hole_histogram[Log2Floor(hole_sz)]; | |
| 489 } | |
| 490 curr = next; | |
| 491 } | |
| 492 | |
| 493 uintptr_t hi_addr = mappings[curr]->start_addr + mappings[curr]->size; | |
| 494 | |
| 495 LogAppend("H "); | |
| 496 LogAppend(lo_addr); | |
| 497 LogAppend(" "); | |
| 498 LogAppend(hi_addr); | |
| 499 LogAppend(" "); | |
| 500 LogAppend(hole_cnt, kDiscardLeadingZeros); | |
| 501 LogAppend(" "); | |
| 502 LogAppend(hole_max, kDiscardLeadingZeros); | |
| 503 LogAppend(" "); | |
| 504 LogAppend(hole_sum, kDiscardLeadingZeros); | |
| 505 for (unsigned int i = 0; i < HBITS; ++i) { | |
| 506 if (!hole_histogram[i]) continue; | |
| 507 LogAppend(" "); | |
| 508 LogAppend(i, kDiscardLeadingZeros); | |
| 509 LogAppend(":"); | |
| 510 LogAppend(hole_histogram[i], kDiscardLeadingZeros); | |
| 511 } | |
| 512 LogCommitLine(); | |
| 513 return true; | |
| 514 } | |
| 515 #endif | |
| 516 | |
| 394 // Write information about the mappings in effect. | 517 // Write information about the mappings in effect. |
| 395 bool DumpMappings() { | 518 bool DumpMappings() { |
| 396 // First write all the mappings from the dumper | 519 // First write all the mappings from the dumper |
| 397 for (unsigned i = 0; i < dumper_->mappings().size(); ++i) { | 520 for (unsigned i = 0; i < dumper_->mappings().size(); ++i) { |
| 398 const MappingInfo& mapping = *dumper_->mappings()[i]; | 521 const MappingInfo& mapping = *dumper_->mappings()[i]; |
| 399 if (mapping.name[0] == 0 || // only want modules with filenames. | 522 if (mapping.name[0] == 0 || // only want modules with filenames. |
| 400 !mapping.exec || // only want executable mappings. | 523 !mapping.exec || // only want executable mappings. |
| 401 mapping.size < 4096 || // too small to get a signature for. | 524 mapping.size < 4096 || // too small to get a signature for. |
| 402 HaveMappingInfo(mapping)) { | 525 HaveMappingInfo(mapping)) { |
| 403 continue; | 526 continue; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 dumper.set_crash_signal(context->siginfo.si_signo); | 568 dumper.set_crash_signal(context->siginfo.si_signo); |
| 446 dumper.set_crash_thread(context->tid); | 569 dumper.set_crash_thread(context->tid); |
| 447 } | 570 } |
| 448 MicrodumpWriter writer(context, mappings, microdump_extra_info, &dumper); | 571 MicrodumpWriter writer(context, mappings, microdump_extra_info, &dumper); |
| 449 if (!writer.Init()) | 572 if (!writer.Init()) |
| 450 return false; | 573 return false; |
| 451 return writer.Dump(); | 574 return writer.Dump(); |
| 452 } | 575 } |
| 453 | 576 |
| 454 } // namespace google_breakpad | 577 } // namespace google_breakpad |
| OLD | NEW |