| 1208 | } |
| 1209 | |
| 1210 | std::string stacktrace_as_stdstring(int skip) |
| 1211 | { |
| 1212 | // From https://gist.github.com/fmela/591333 |
| 1213 | void* callstack[128]; |
| 1214 | const auto max_frames = sizeof(callstack) / sizeof(callstack[0]); |
| 1215 | int num_frames = backtrace(callstack, max_frames); |
| 1216 | char** symbols = backtrace_symbols(callstack, num_frames); |
| 1217 | |
| 1218 | std::string result; |
| 1219 | // Print stack traces so the most relevant ones are written last |
| 1220 | // Rationale: http://yellerapp.com/posts/2015-01-22-upside-down-stacktraces.html |
| 1221 | for (int i = num_frames - 1; i >= skip; --i) { |
| 1222 | char buf[1024]; |
| 1223 | Dl_info info; |
| 1224 | if (dladdr(callstack[i], &info) && info.dli_sname) { |
| 1225 | char* demangled = NULL; |
| 1226 | int status = -1; |
| 1227 | if (info.dli_sname[0] == '_') { |
| 1228 | demangled = abi::__cxa_demangle(info.dli_sname, 0, 0, &status); |
| 1229 | } |
| 1230 | snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n", |
| 1231 | i - skip, int(2 + sizeof(void*) * 2), callstack[i], |
| 1232 | status == 0 ? demangled : |
| 1233 | info.dli_sname == 0 ? symbols[i] : info.dli_sname, |
| 1234 | static_cast<char*>(callstack[i]) - static_cast<char*>(info.dli_saddr)); |
| 1235 | free(demangled); |
| 1236 | } |
| 1237 | else { |
| 1238 | snprintf(buf, sizeof(buf), "%-3d %*p %s\n", |
| 1239 | i - skip, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]); |
| 1240 | } |
| 1241 | result += buf; |
| 1242 | } |
| 1243 | free(symbols); |
| 1244 | |
| 1245 | if (num_frames == max_frames) { |
| 1246 | result = "[truncated]\n" + result; |
| 1247 | } |
| 1248 | |
| 1249 | if (!result.empty() && result[result.size() - 1] == '\n') { |
| 1250 | result.resize(result.size() - 1); |
| 1251 | } |
| 1252 | |
| 1253 | return prettify_stacktrace(result); |
| 1254 | } |
| 1255 | |
| 1256 | #else // LOGURU_STACKTRACES |
| 1257 | Text demangle(const char* name) |
no test coverage detected