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]" |
| 47 | |
| 48 | // We extract the pieces and demangle, so we can produce output like this: |
| 49 | // libartd.so:-1] #00 art::Runtime::PlatformAbort(char const*, int) +0x15b [0xf770dd51] |
| 50 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 51 | for (size_t i = 0; i < frame_count; ++i) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame^] | 52 | std::string text(symbols[i]); |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 53 | |
| 54 | size_t index = text.find('('); |
| 55 | std::string filename(text.substr(0, index)); |
| 56 | text.erase(0, index + 1); |
| 57 | |
| 58 | index = text.find_first_of("+)"); |
| 59 | std::string function_name(Demangle(text.substr(0, index))); |
| 60 | text.erase(0, index); |
| 61 | index = text.find(')'); |
| 62 | text.erase(index, 1); |
| 63 | |
| 64 | std::string log_line(StringPrintf("\t#%02d ", i) + function_name + text); |
| 65 | LogMessage(filename.c_str(), -1, ERROR, -1).stream() << log_line; |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 66 | } |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame^] | 67 | |
| 68 | free(symbols); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } // namespace art |