Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 2 | |
| 3 | #include "runtime.h" |
| 4 | |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 5 | #include <cxxabi.h> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 6 | #include <execinfo.h> |
| 7 | |
| 8 | #include "logging.h" |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 9 | #include "stringprintf.h" |
| 10 | |
| 11 | namespace art { |
| 12 | |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 13 | std::string Demangle(const std::string& mangled_name) { |
| 14 | if (mangled_name.empty()) { |
| 15 | return "??"; |
| 16 | } |
| 17 | |
| 18 | // http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html |
| 19 | int status; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 20 | char* name(abi::__cxa_demangle(mangled_name.c_str(), NULL, NULL, &status)); |
| 21 | if (name != NULL) { |
| 22 | std::string result(name); |
| 23 | free(name); |
| 24 | return result; |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | return mangled_name + "()"; |
| 28 | } |
| 29 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 30 | void Runtime::PlatformAbort(const char* file, int line) { |
| 31 | // On the host, we don't have debuggerd to dump a stack for us. |
| 32 | |
| 33 | // Get the raw stack frames. |
| 34 | size_t MAX_STACK_FRAMES = 64; |
| 35 | void* frames[MAX_STACK_FRAMES]; |
| 36 | size_t frame_count = backtrace(frames, MAX_STACK_FRAMES); |
| 37 | |
| 38 | // Turn them into something human-readable with symbols. |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 39 | char** symbols = backtrace_symbols(frames, frame_count); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 40 | if (symbols == NULL) { |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 41 | PLOG(ERROR) << "backtrace_symbols failed"; |
| 42 | return; |
| 43 | } |
| 44 | |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 45 | // backtrace_symbols(3) gives us lines like this: |
| 46 | // "/usr/local/google/home/enh/a1/out/host/linux-x86/bin/../lib/libartd.so(_ZN3art7Runtime13PlatformAbortEPKci+0x15b) [0xf76c5af3]" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 47 | // "[0xf7b62057]" |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 48 | |
| 49 | // We extract the pieces and demangle, so we can produce output like this: |
| 50 | // libartd.so:-1] #00 art::Runtime::PlatformAbort(char const*, int) +0x15b [0xf770dd51] |
| 51 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 52 | for (size_t i = 0; i < frame_count; ++i) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 53 | std::string text(symbols[i]); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 54 | std::string filename("??"); |
| 55 | std::string function_name; |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 56 | |
| 57 | size_t index = text.find('('); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 58 | if (index != std::string::npos) { |
| 59 | filename = text.substr(0, index); |
| 60 | text.erase(0, index + 1); |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 62 | index = text.find_first_of("+)"); |
| 63 | function_name = Demangle(text.substr(0, index)); |
| 64 | text.erase(0, index); |
| 65 | index = text.find(')'); |
| 66 | text.erase(index, 1); |
| 67 | } |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 68 | std::string log_line(StringPrintf("\t#%02d ", i) + function_name + text); |
| 69 | LogMessage(filename.c_str(), -1, ERROR, -1).stream() << log_line; |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 70 | } |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 71 | |
| 72 | free(symbols); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } // namespace art |