Skip to content

Instantly share code, notes, and snippets.

@korniltsev
Forked from fmela/stacktrace.cxx
Last active November 5, 2020 10:07
Show Gist options
  • Save korniltsev/59743b9e1db3f6ba83b2dac6fd65c181 to your computer and use it in GitHub Desktop.
Save korniltsev/59743b9e1db3f6ba83b2dac6fd65c181 to your computer and use it in GitHub Desktop.
A C++ function that produces a stack backtrace with demangled function & method names.
/*
* Copyright (c) 2009-2017, Farooq Mela
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <execinfo.h> // for backtrace
#include <dlfcn.h> // for dladdr
#include <cxxabi.h> // for __cxa_demangle
#include <cstdio>
#include <cstdlib>
#include <string>
#include <sstream>
/*
* based on https://gist.github.com/fmela/591333
* modified to make bt allocation free(or less?) and defer symbolication
* */
class MyStacktrace {
public:
MyStacktrace() :nFrames(0) {
memset(callstack, 0, sizeof(callstack));
memset(extraMessage, 0, sizeof(extraMessage));
}
void bt() {
nFrames = backtrace(callstack, nMaxFrames);
}
std::string symbolize(int skip=2) {
char **symbols = backtrace_symbols(callstack, nFrames);
char buf[1024];
std::ostringstream trace_buf;
for (int i = skip; i < nFrames; i++) {
//printf("%s\n", symbols[i]);
Dl_info info;
if (dladdr(callstack[i], &info) && info.dli_sname) {
char *demangled = NULL;
int status = -1;
if (info.dli_sname[0] == '_')
demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n",
i, int(2 + sizeof(void*) * 2), callstack[i],
status == 0 ? demangled :
info.dli_sname == 0 ? symbols[i] : info.dli_sname,
(char *)callstack[i] - (char *)info.dli_saddr);
free(demangled);
} else {
snprintf(buf, sizeof(buf), "%-3d %*p %s\n",
i, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]);
}
trace_buf << buf;
}
free(symbols);
if (nFrames == nMaxFrames)
trace_buf << "[truncated]\n";
return trace_buf.str();
}
int nFrames;
void *callstack[20];
const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]);
char extraMessage[1024];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment