Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 16 | |
| 17 | #include "runtime.h" |
| 18 | |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 19 | #include <cxxabi.h> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 20 | #include <execinfo.h> |
| 21 | |
| 22 | #include "logging.h" |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 23 | #include "stringprintf.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 27 | std::string Demangle(const std::string& mangled_name) { |
| 28 | if (mangled_name.empty()) { |
| 29 | return "??"; |
| 30 | } |
| 31 | |
| 32 | // http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html |
| 33 | int status; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 34 | char* name(abi::__cxa_demangle(mangled_name.c_str(), NULL, NULL, &status)); |
| 35 | if (name != NULL) { |
| 36 | std::string result(name); |
| 37 | free(name); |
| 38 | return result; |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | return mangled_name + "()"; |
| 42 | } |
| 43 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 44 | void Runtime::PlatformAbort(const char* /*file*/, int /*line_number*/) { |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 45 | // On the host, we don't have debuggerd to dump a stack for us. |
| 46 | |
| 47 | // Get the raw stack frames. |
| 48 | size_t MAX_STACK_FRAMES = 64; |
| 49 | void* frames[MAX_STACK_FRAMES]; |
| 50 | size_t frame_count = backtrace(frames, MAX_STACK_FRAMES); |
| 51 | |
| 52 | // Turn them into something human-readable with symbols. |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 53 | char** symbols = backtrace_symbols(frames, frame_count); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 54 | if (symbols == NULL) { |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 55 | PLOG(ERROR) << "backtrace_symbols failed"; |
| 56 | return; |
| 57 | } |
| 58 | |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 59 | // backtrace_symbols(3) gives us lines like this: |
| 60 | // "/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] | 61 | // "[0xf7b62057]" |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 62 | |
| 63 | // We extract the pieces and demangle, so we can produce output like this: |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 64 | // libartd.so:-1] #00 art::Runtime::PlatformAbort(char const*, int) +0x15b [0xf770dd51] |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 66 | for (size_t i = 0; i < frame_count; ++i) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 67 | std::string text(symbols[i]); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 68 | std::string filename("??"); |
| 69 | std::string function_name; |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 70 | |
| 71 | size_t index = text.find('('); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 72 | if (index != std::string::npos) { |
| 73 | filename = text.substr(0, index); |
| 74 | text.erase(0, index + 1); |
Elliott Hughes | 8287072 | 2011-08-29 19:04:51 -0700 | [diff] [blame] | 75 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 76 | index = text.find_first_of("+)"); |
| 77 | function_name = Demangle(text.substr(0, index)); |
| 78 | text.erase(0, index); |
| 79 | index = text.find(')'); |
| 80 | text.erase(index, 1); |
| 81 | } |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 82 | std::string log_line(StringPrintf("\t#%02zd ", i) + function_name + text); |
Elliott Hughes | 9ee5f9c | 2012-02-03 18:33:16 -0800 | [diff] [blame] | 83 | LogMessage(filename.c_str(), -1, INTERNAL_FATAL, -1).stream() << log_line; |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 84 | } |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 85 | |
| 86 | free(symbols); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace art |