[lug] good assert library

Chan Kar Heng karheng at softhome.net
Sun Sep 22 15:04:05 MDT 2002


At 2002/09/19 15:17, you wrote:
>Chan Kar Heng wrote:
>>:)
>>thanks.. it's cool...
>>i tried this... gcc -rdynamic -g -O0 a.c
>>and i get a lot of hex addresses instead of function names...
>>is this the expected output?
>
>I haven't used this code in about a year, I recall having only hex addresses until the right option combination was used. You might experiment with leaving out any -O#; but once it is found, it will give you not hex, but plain English function names. You might also find it varies with the version of glibc and gcc/g++ that you use, but I am not positive. If you can't find the right arguments to get symbolic output, I can experiment with some of my old code and find out. I doubt it matters, but one detail of the last project I used this on compiled and linked in separate Makefile stages, where the -rdynamic was only in the linker stage; the -g was only in the intermediate object compile stage. Separation of stages without going directly from .c to executeable could allow a finer-grained testing of compile switches.

I did a little searching... didn't do me much good though...

This's through gcc -v :

Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

Here's another version i got..:

--bt.c---------------------------------------------------------------------
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

/* Obtain a backtrace and print it to stdout. */
void
print_trace (void)
{
  void *array[10];
  size_t size;
  char **strings;
  size_t i;

  size = backtrace (array, 10);
  strings = backtrace_symbols (array, size);

  printf ("Obtained %zd stack frames.\n", size);

  for (i = 0; i < size; i++)
     printf ("%s\n", strings[i]);

  free (strings);
}

/* A dummy function to make the backtrace more interesting. */
void
dummy_function (void)
{
  print_trace ();
}

int
main (void)
{
  dummy_function ();
  return 0;
}
-----------------------------------------------------------------------

This's what i used to compile... 

gcc -Wl,--export-dynamic -g -Wall -O0 -o bt bt.c

or

gcc -rdynamic -g -Wall -O0 -o bt bt.c

When i started the program, i got this:

-----------------------------------------------------------------------
Obtained 5 stack frames.
[0x80486b1]
[0x804872c]
[0x8048738]
/lib/libc.so.6(__libc_start_main+0xff) [0x400329cb]
[0x8048611]
-----------------------------------------------------------------------

I thought the symbols were stripped or weren't there...
but this's what i got when I do nm bt  :

0804980c A _DYNAMIC
080497e0 A _GLOBAL_OFFSET_TABLE_
0804879c R _IO_stdin_used
080497d4 ? __CTOR_END__
080497d0 ? __CTOR_LIST__
080497dc ? __DTOR_END__
080497d8 ? __DTOR_LIST__
080497cc ? __EH_FRAME_BEGIN__
080497cc ? __FRAME_END__
080498ac A __bss_start
080497c0 D __data_start
         w __deregister_frame_info@@GLIBC_2.0
08048750 t __do_global_ctors_aux
08048620 t __do_global_dtors_aux
         w __gmon_start__
         U __libc_start_main@@GLIBC_2.0
         w __register_frame_info@@GLIBC_2.0
080498ac A _edata
080498c4 A _end
0804877c A _etext
0804877c ? _fini
         U _fp_hw
08048540 ? _init
080485f0 T _start
         U backtrace@@GLIBC_2.1
         U backtrace_symbols@@GLIBC_2.1
080497c8 d completed.3
080497c0 W data_start
08048724 T dummy_function
08048668 t fini_dummy
080497cc d force_to_data
080497cc d force_to_data
08048670 t frame_dummy
         U free@@GLIBC_2.0
08048614 t gcc2_compiled.
08048620 t gcc2_compiled.
08048750 t gcc2_compiled.
0804877c t gcc2_compiled.
08048690 t init_dummy
08048774 t init_dummy
08048730 T main
080498ac b object.8
080497c4 d p.2
080486a0 T print_trace
         U printf@@GLIBC_2.0


Hmm.. I'm not sure where to start looking to get function names instead...
Actually, since the executable contain all the symbols, wouldn't it get loaded
up into memory where it could be referenced too?



rgds,

kh




More information about the LUG mailing list