* Parse a mangled symbol and offset in the backtrace_symbols format: * * executable(function+offset) [global offset] */
| 40 | * executable(function+offset) [global offset] |
| 41 | */ |
| 42 | demangling_result demangle_function(std::string symbol) |
| 43 | { |
| 44 | demangling_result result; |
| 45 | size_t offset_begin = symbol.find_first_of("["); |
| 46 | size_t offset_end = symbol.find_first_of("]"); |
| 47 | |
| 48 | if ((offset_begin != std::string::npos) && (offset_end != std::string::npos) && |
| 49 | (offset_begin < offset_end)) |
| 50 | { |
| 51 | offset_begin++; |
| 52 | result.address = symbol.substr(offset_begin, offset_end - offset_begin); |
| 53 | } |
| 54 | |
| 55 | size_t function_begin = symbol.find_first_of("("); |
| 56 | if (function_begin != std::string::npos) |
| 57 | { |
| 58 | result.executable = symbol.substr(0, function_begin); |
| 59 | } |
| 60 | |
| 61 | size_t function_end = symbol.find_first_of("+"); |
| 62 | if ((function_begin == std::string::npos) || |
| 63 | (function_end == std::string::npos) || |
| 64 | (function_begin >= function_end)) |
| 65 | { |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | ++function_begin; |
| 70 | std::string function_name = symbol.substr(function_begin, |
| 71 | function_end - function_begin); |
| 72 | |
| 73 | int status; |
| 74 | char *demangled = abi::__cxa_demangle(function_name.data(), NULL, NULL, &status); |
| 75 | if (status != 0) |
| 76 | { |
| 77 | free(demangled); |
| 78 | |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | result.function_name = demangled; |
| 83 | free(demangled); |
| 84 | |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Execute the given command and read the first line of its output. |