Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I upvoted <a href="https://stackoverflow.com/a/11062088/6160">Nik B.'s answer</a> for pointing me in the right direction, and here's what I actually did using <code>libunwind</code> instead, since the linked stack trace suggestion can only get function names for linked libraries. This code is available on GitHub at <a href="https://github.com/landtuna/opnew-stacktraces" rel="nofollow noreferrer">https://github.com/landtuna/opnew-stacktraces</a></p> <p>newdelete.cpp:</p> <pre><code>#include &lt;exception&gt; #include &lt;new&gt; #include &lt;cstdlib&gt; #include &lt;iostream&gt; #include "stacktrace.hpp" void* operator new (size_t size) { void* p = malloc(size); if (p == 0) { throw std::bad_alloc(); } std::cout &lt;&lt; "allocated " &lt;&lt; size &lt;&lt; std::endl; printTrace(std::cout); return p; } void operator delete (void* p) { free(p); } </code></pre> <p>stacktrace.cpp:</p> <pre><code>#include &lt;cxxabi.h&gt; #include &lt;libunwind.h&gt; #include &lt;ostream&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; using namespace std; #include "stacktrace.hpp" void printTrace(ostream&amp; out) { unw_cursor_t cursor; unw_context_t context; unw_getcontext(&amp;context); unw_init_local(&amp;cursor, &amp;context); while (unw_step(&amp;cursor) &gt; 0) { unw_word_t offset, pc; char fname[200]; size_t demangledSize = 200; char* demangled = (char*) malloc(demangledSize); unw_get_reg(&amp;cursor, UNW_REG_IP, &amp;pc); fname[0] = '\0'; unw_get_proc_name(&amp;cursor, fname, sizeof(fname), &amp;offset); int status; char *ret = abi::__cxa_demangle(fname, demangled, &amp;demangledSize, &amp;status); if (ret) { // return value may be a realloc() of the input demangled = ret; } else { // demangling failed, just pretend it's a C demangled with no args strncpy(demangled, fname, demangledSize); strncat(demangled, "()", demangledSize); demangled[demangledSize-1] = '\0'; } out &lt;&lt; hex &lt;&lt; demangled &lt;&lt; "+0x" &lt;&lt; offset &lt;&lt; " [" &lt;&lt; pc &lt;&lt; "]" &lt;&lt; dec &lt;&lt; '\n'; free(demangled); } out &lt;&lt; endl; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload