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) { | |
|
Primiano Tucci (use gerrit)
2016/05/06 13:58:38
Maybe just add a comment saying:
// From chromium
| |
| 64 if (!n) return -1; | |
| 65 int log = 0; | |
| 66 for (int i = 5; i >= 0; ++i) { | |
| 67 int shift = 1 << i; | |
| 68 if (n >> shift) { | |
| 69 log += shift; | |
| 70 if (!(n >>= shift)) break; | |
| 71 } | |
| 72 } | |
| 73 return log; | |
| 74 } | |
| 75 | |
| 76 bool MappingsAreAdjacent(const MappingInfo* a, const MappingInfo* b) { | |
| 77 // Because of load biasing, we can end up with a situation where two | |
| 78 // mappings actually overlap. So we will define adjacency to also include a | |
| 79 // b start address that lies within a's address range (including starting | |
| 80 // immediately after a). | |
| 81 // Because load biasing only ever moves the start address backwards, the end | |
| 82 // address should still increase. | |
| 83 return a->start_addr <= b->start_addr && | |
| 84 a->start_addr + a->size >= b->start_addr; | |
| 85 } | |
| 86 | |
| 87 size_t NextOrderedMapping( | |
| 88 const google_breakpad::wasteful_vector<MappingInfo*>& mappings, | |
| 89 size_t curr) { | |
| 90 size_t best = mappings.size(); | |
| 91 for (size_t next = 0; next < mappings.size(); ++next) { | |
| 92 if (mappings[curr]->start_addr < mappings[next]->start_addr && | |
| 93 (best == mappings.size() || | |
| 94 mappings[next]->start_addr < mappings[best]->start_addr)) { | |
| 95 best = next; | |
| 96 } | |
| 97 } | |
| 98 return best; | |
| 99 } | |
| 100 | |
| 64 class MicrodumpWriter { | 101 class MicrodumpWriter { |
| 65 public: | 102 public: |
| 66 MicrodumpWriter(const ExceptionHandler::CrashContext* context, | 103 MicrodumpWriter(const ExceptionHandler::CrashContext* context, |
| 67 const MappingList& mappings, | 104 const MappingList& mappings, |
| 68 const MicrodumpExtraInfo& microdump_extra_info, | 105 const MicrodumpExtraInfo& microdump_extra_info, |
| 69 LinuxDumper* dumper) | 106 LinuxDumper* dumper) |
| 70 : ucontext_(context ? &context->context : NULL), | 107 : ucontext_(context ? &context->context : NULL), |
| 71 #if !defined(__ARM_EABI__) && !defined(__mips__) | 108 #if !defined(__ARM_EABI__) && !defined(__mips__) |
| 72 float_state_(context ? &context->float_state : NULL), | 109 float_state_(context ? &context->float_state : NULL), |
| 73 #endif | 110 #endif |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 91 return false; | 128 return false; |
| 92 return dumper_->ThreadsSuspend() && dumper_->LateInit(); | 129 return dumper_->ThreadsSuspend() && dumper_->LateInit(); |
| 93 } | 130 } |
| 94 | 131 |
| 95 bool Dump() { | 132 bool Dump() { |
| 96 bool success; | 133 bool success; |
| 97 LogLine("-----BEGIN BREAKPAD MICRODUMP-----"); | 134 LogLine("-----BEGIN BREAKPAD MICRODUMP-----"); |
| 98 DumpProductInformation(); | 135 DumpProductInformation(); |
| 99 DumpOSInformation(); | 136 DumpOSInformation(); |
| 100 DumpGPUInformation(); | 137 DumpGPUInformation(); |
| 138 DumpFreeSpace(); | |
| 101 success = DumpCrashingThread(); | 139 success = DumpCrashingThread(); |
| 102 if (success) | 140 if (success) |
| 103 success = DumpMappings(); | 141 success = DumpMappings(); |
| 104 LogLine("-----END BREAKPAD MICRODUMP-----"); | 142 LogLine("-----END BREAKPAD MICRODUMP-----"); |
| 105 dumper_->ThreadsResume(); | 143 dumper_->ThreadsResume(); |
| 106 return success; | 144 return success; |
| 107 } | 145 } |
| 108 | 146 |
| 109 private: | 147 private: |
| 110 // Writes one line to the system log. | 148 // Writes one line to the system log. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 121 void LogAppend(const char* str) { | 159 void LogAppend(const char* str) { |
| 122 my_strlcat(log_line_, str, kLineBufferSize); | 160 my_strlcat(log_line_, str, kLineBufferSize); |
| 123 } | 161 } |
| 124 | 162 |
| 125 // As above (required to take precedence over template specialization below). | 163 // As above (required to take precedence over template specialization below). |
| 126 void LogAppend(char* str) { | 164 void LogAppend(char* str) { |
| 127 LogAppend(const_cast<const char*>(str)); | 165 LogAppend(const_cast<const char*>(str)); |
| 128 } | 166 } |
| 129 | 167 |
| 130 // Stages the hex repr. of the given int type in the current line buffer. | 168 // Stages the hex repr. of the given int type in the current line buffer. |
| 131 template<typename T> | 169 enum LeadingZeros { kKeepLeadingZeros, kDiscardLeadingZeros }; |
| 132 void LogAppend(T value) { | 170 template <typename T> |
| 171 void LogAppend(T value, const LeadingZeros leading_zeros = | |
| 172 LeadingZeros::kKeepLeadingZeros) { | |
| 133 // Make enough room to hex encode the largest int type + NUL. | 173 // 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', | 174 static const char HEX[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 135 'A', 'B', 'C', 'D', 'E', 'F'}; | 175 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 136 char hexstr[sizeof(T) * 2 + 1]; | 176 char hexstr[sizeof(T) * 2 + 1]; |
| 137 for (int i = sizeof(T) * 2 - 1; i >= 0; --i, value >>= 4) | 177 int i = sizeof(T) * 2; |
| 138 hexstr[i] = HEX[static_cast<uint8_t>(value) & 0x0F]; | 178 hexstr[i] = '\0'; |
| 139 hexstr[sizeof(T) * 2] = '\0'; | 179 do { |
| 140 LogAppend(hexstr); | 180 hexstr[--i] = HEX[static_cast<uint8_t>(value) & 0x0F]; |
| 181 value >>= 4; | |
| 182 } while (value || leading_zeros == kKeepLeadingZeros && i > 0); | |
| 183 LogAppend(hexstr + i); | |
| 141 } | 184 } |
| 142 | 185 |
| 143 // Stages the buffer content hex-encoded in the current line buffer. | 186 // Stages the buffer content hex-encoded in the current line buffer. |
| 144 void LogAppend(const void* buf, size_t length) { | 187 void LogAppend(const void* buf, size_t length) { |
| 145 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(buf); | 188 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(buf); |
| 146 for (size_t i = 0; i < length; ++i, ++ptr) | 189 for (size_t i = 0; i < length; ++i, ++ptr) |
| 147 LogAppend(*ptr); | 190 LogAppend(*ptr); |
| 148 } | 191 } |
| 149 | 192 |
| 150 // Writes out the current line buffer on the system log. | 193 // 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]); | 427 LogAppend(module_identifier.data4[3]); |
| 385 LogAppend(module_identifier.data4[4]); | 428 LogAppend(module_identifier.data4[4]); |
| 386 LogAppend(module_identifier.data4[5]); | 429 LogAppend(module_identifier.data4[5]); |
| 387 LogAppend(module_identifier.data4[6]); | 430 LogAppend(module_identifier.data4[6]); |
| 388 LogAppend(module_identifier.data4[7]); | 431 LogAppend(module_identifier.data4[7]); |
| 389 LogAppend("0 "); // Age is always 0 on Linux. | 432 LogAppend("0 "); // Age is always 0 on Linux. |
| 390 LogAppend(file_name); | 433 LogAppend(file_name); |
| 391 LogCommitLine(); | 434 LogCommitLine(); |
| 392 } | 435 } |
| 393 | 436 |
| 437 bool DumpFreeSpace() { | |
| 438 const google_breakpad::wasteful_vector<MappingInfo*>& mappings = | |
| 439 dumper_->mappings(); | |
| 440 if (mappings.size() == 0) return false; | |
| 441 | |
| 442 // This is complicated by the fact that mappings is not in order. It should | |
| 443 // be mostly in order, however the mapping that contains the entry point for | |
| 444 // the process is always at the front of the vector. | |
| 445 | |
| 446 static const int HBITS = sizeof(size_t) * 8; | |
| 447 unsigned int hole_histogram[HBITS]; | |
| 448 my_memset(hole_histogram, 0, sizeof(hole_histogram)); | |
| 449 | |
| 450 // Find the lowest address mapping. | |
| 451 size_t curr = 0; | |
| 452 for (size_t i = 1; i < mappings.size(); ++i) { | |
| 453 if (mappings[i]->start_addr < mappings[curr]->start_addr) curr = i; | |
| 454 } | |
| 455 | |
| 456 uintptr_t lo_addr = mappings[curr]->start_addr; | |
| 457 | |
| 458 unsigned int hole_cnt = 0; | |
| 459 size_t hole_max = 0; | |
| 460 size_t hole_sum = 0; | |
| 461 | |
| 462 while (true) { | |
| 463 // Skip to the end of an adjacent run of mappings. This is an optimization | |
| 464 // for the fact that mappings is mostly sorted. | |
| 465 while (curr != mappings.size() - 1 && | |
| 466 MappingsAreAdjacent(mappings[curr], mappings[curr + 1])) | |
| 467 ++curr; | |
| 468 | |
| 469 size_t next = NextOrderedMapping(mappings, curr); | |
| 470 if (next == mappings.size()) | |
| 471 break; | |
| 472 | |
| 473 uintptr_t hole_lo = mappings[curr]->start_addr + mappings[curr]->size; | |
| 474 uintptr_t hole_hi = mappings[next]->start_addr; | |
| 475 | |
| 476 if (hole_hi > hole_lo) { | |
| 477 size_t hole_sz = hole_hi - hole_lo; | |
| 478 hole_sum += hole_sz; | |
| 479 hole_max = std::max(hole_sz, hole_max); | |
| 480 ++hole_cnt; | |
| 481 ++hole_histogram[Log2Floor(hole_sz)]; | |
| 482 } | |
| 483 curr = next; | |
| 484 } | |
| 485 | |
| 486 uintptr_t hi_addr = mappings[curr]->start_addr + mappings[curr]->size; | |
| 487 | |
| 488 LogAppend("H "); | |
| 489 LogAppend(lo_addr); | |
| 490 LogAppend(" "); | |
| 491 LogAppend(hi_addr); | |
| 492 LogAppend(" "); | |
| 493 LogAppend(hole_cnt, kDiscardLeadingZeros); | |
| 494 LogAppend(" "); | |
| 495 LogAppend(hole_max, kDiscardLeadingZeros); | |
| 496 LogAppend(" "); | |
| 497 LogAppend(hole_sum, kDiscardLeadingZeros); | |
| 498 for (unsigned int i = 0; i < HBITS; ++i) { | |
| 499 if (!hole_histogram[i]) continue; | |
| 500 LogAppend(" "); | |
| 501 LogAppend(i, kDiscardLeadingZeros); | |
| 502 LogAppend(":"); | |
| 503 LogAppend(hole_histogram[i], kDiscardLeadingZeros); | |
| 504 } | |
| 505 LogCommitLine(); | |
| 506 return true; | |
| 507 } | |
| 508 | |
| 394 // Write information about the mappings in effect. | 509 // Write information about the mappings in effect. |
| 395 bool DumpMappings() { | 510 bool DumpMappings() { |
| 396 // First write all the mappings from the dumper | 511 // First write all the mappings from the dumper |
| 397 for (unsigned i = 0; i < dumper_->mappings().size(); ++i) { | 512 for (unsigned i = 0; i < dumper_->mappings().size(); ++i) { |
| 398 const MappingInfo& mapping = *dumper_->mappings()[i]; | 513 const MappingInfo& mapping = *dumper_->mappings()[i]; |
| 399 if (mapping.name[0] == 0 || // only want modules with filenames. | 514 if (mapping.name[0] == 0 || // only want modules with filenames. |
| 400 !mapping.exec || // only want executable mappings. | 515 !mapping.exec || // only want executable mappings. |
| 401 mapping.size < 4096 || // too small to get a signature for. | 516 mapping.size < 4096 || // too small to get a signature for. |
| 402 HaveMappingInfo(mapping)) { | 517 HaveMappingInfo(mapping)) { |
| 403 continue; | 518 continue; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 dumper.set_crash_signal(context->siginfo.si_signo); | 560 dumper.set_crash_signal(context->siginfo.si_signo); |
| 446 dumper.set_crash_thread(context->tid); | 561 dumper.set_crash_thread(context->tid); |
| 447 } | 562 } |
| 448 MicrodumpWriter writer(context, mappings, microdump_extra_info, &dumper); | 563 MicrodumpWriter writer(context, mappings, microdump_extra_info, &dumper); |
| 449 if (!writer.Init()) | 564 if (!writer.Init()) |
| 450 return false; | 565 return false; |
| 451 return writer.Dump(); | 566 return writer.Dump(); |
| 452 } | 567 } |
| 453 | 568 |
| 454 } // namespace google_breakpad | 569 } // namespace google_breakpad |
| OLD | NEW |