GP2X Anyone Got A Backtrace Working In Code?


slygamer

Active Member
Joined
Sep 19, 2005
Messages
795
Location
Brisbane, Australia
Website
Visit site
I'm trying to output the callstack, or backtrace, in my programs if an assert fails. I'm using the following code which seems to be the proper way to call the backtrace functions.

Code:
  void *stack[32];
  unsigned long stackSize = backtrace(stack, 32);
  char **strings = backtrace_symbols(stack, stackSize);
  for (int j = 1; j < stackSize; ++j)
	printf(strings[j]);
This only gives me the hex addresses of the functions, even with the -rdynamic flag passed to the linker as suggested by the documentation found on the web. Yet gdb can give the addresses plus the function names and parameter values with the 'bt' command.

Does anyone have any ideas on this? Does anyone else even use the backtrace functions?
 
Yes, using -g for the compiler and -rdynamic for the linker. -rdynamic is recommended by the GNU documentation, but I don't know if devkitGP2X supports it because it is not listed by "arm-linux-gcc -v --help".
 
Ok. I'll give that a try when I get home.

Edit: Unfortunately, it did not work. I still only get the function addresses from backtrace_symbols(). Oh well, I guess that backtrace_symbols() just cannot do it, even though gdb can do it with the same executable.
 
Maybe you need to use a newer version of gcc or something like that?
 
Having never used backtrace I have to ask, is there something you need to do to enable the population of the backtrace_symbols? I would assume that you would need to enable it in the compile so the compiler would auto-insert the "push" and "pop" calls into the structure. If not auto then some manual code must be inserted. The manual method is all I have used.
 
According to the documentation, it should be as simple as compiling with debug information (-g) and not stripping the debug information out during the link.
 
slygamer posted on Dec 7 2006 at 11:19 PM said:
According to the documentation, it should be as simple as compiling with debug information (-g) and not stripping the debug information out during the link.

man gcc :

-rdynamic
Pass the flag -export-dynamic to the ELF linker, on targets that support it. This instructs the linker to add all symbols, not only used ones,
to the dynamic symbol table. This option is needed for some uses of "dlopen" or to allow obtaining backtraces from within a program.


So it appears that its '-rdynamic' for the compiler, or -export-dynamic to the linker.
I tested and it fixed the problem on my test.
 
Last edited by a moderator:
This is what I was reading in the glibc documentation:
Also, you may need to pass additional flags to the linker to make the function names available to the program. (For example, on systems using GNU ld, you must pass (-rdynamic.)

Still doesn't work for me (using -g and/or -rdynamic with gcc and --export-dynamic with the linker). I run the linker separately after compiling each source file with gcc. I'm using devkitPro arm-linux-gcc 4.0.2.
 
slygamer posted on Dec 9 2006 at 09:04 AM said:
Still doesn't work for me (using -g and/or -rdynamic with gcc and --export-dynamic with the linker). I run the linker separately after compiling each source file with gcc. I'm using devkitPro arm-linux-gcc 4.0.2.

Uh?

I don't have the GP2X here so I can only test on my regular Unix box. Do you have the same problem when
compiling for PC ?
 
Last edited by a moderator:
I can't build for PC because I'm using the hardware registers directly in my GP2X application and I do not have gcc for Win32. I have even tried just the simplest example that the GNU docs provide, with -g, -rdynamic, -export-dynamic and all combinations of said parameters and there is still no function names in the backtrace_symbols() output.
 
Make sure you are not stripping your executables (don't use -s and don't call strip).
backtrace_symbols() does not work with static functions.
The version of glibc you are using may have no proper support for backtrace_symbols() compiled in, try another devkit with different glibc.
 
I'm not using -s or arm-linux-strip. I guess glibc supplied with devkitGP2X just simply does not support function names in backtrace_symbols(). devkitGP2X seems to have been dropped and forgotten by devkitPro, which is a shame because it was a breeze to get working.

See my other thread about getting the Open2x toolchain on Windows for my pain in that area. I really want to try another toolchain, but the Open2x source code can not be downloaded from SVN on Windows due to conflicting filenames.
 
Try setting it up in cygwin?

Edit: Nevermind, I just read the other thread, and this won't work. Maybe the easiest solution would be colinux or another virtualization strategy to run linux.
 
Back
Top