Stack Printing in Mingw

I need to modify an existing C application and print stacktrace in a specific place. How can i do this?

I can not compile this source:

#ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #ifndef __USE_GNU #define __USE_GNU #endif #include <execinfo.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ucontext.h> #include <unistd.h> int main(int argc, char ** argv) { void * array[50]; void * caller_address; char ** messages; int size = backtrace(array, 50); /* overwrite sigaction with caller address */ array[1] = caller_address; messages = backtrace_symbols(array, size); /* skip first stack frame (points here) */ for (int i = 1; i < size && messages != NULL; ++i) { fprintf(stderr, "[bt]: (%d) %s\n", i, messages[i]); } free(messages); } 

because it lacks characters:

 $ i586-mingw32msvc-gcc -rdynamic ./trace.cpp -I/usr/include/ i586-mingw32msvc-gcc: unrecognized option '-rdynamic' ./trace.cpp:39:2: warning: no newline at end of file /tmp/cc6hCJtU.o:trace.cpp:(.text+0x26): undefined reference to `_backtrace' /tmp/cc6hCJtU.o:trace.cpp:(.text+0x47): undefined reference to `_backtrace_symbols' /tmp/cc6hCJtU.o:trace.cpp:(.text+0x67): undefined reference to `_stderr' 

I was unable to load the .a file.

The following answers, which can be found here on stackoverflow, are almost unusable:

Is there any working solution? Thanks in advance.

+4
source share
1 answer

Your MinGW toolchain does not support the -rdynamic flag and cannot compile your application using backtrace() or backtrace_symbols()

Not sure what MinGW you are using, but I would recommend getting a fresh one from here: http://mingw-w64.sourceforge.net/

+1
source

All Articles