help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

backtrace does not print symbols


From: Martin Woudstra
Subject: backtrace does not print symbols
Date: Mon, 14 Jan 2002 17:17:19 +0100

Hello,

I would like to print a stacktrace in my program.
I implemented the code from
http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_653.html
(see end of mail for my piece of code)
but the program only prints the adresses and not the names
of the functions, except one:

[lxplus049] % teststack.exe
Calling func1()
Calling func2()
Calling func3()
Calling func4()
Calling func5() giving backtrace
( 0)  [0x8048c38]
( 1)  [0x8048dbb]
( 2)  [0x8048e13]
( 3)  [0x8048e6b]
( 4)  [0x8048ec3]
( 5)  [0x8048ee7]
( 6)  /lib/libc.so.6(__libc_start_main+0xff) [0x2ab5e9cb]
( 7)  [0x8048b21]

The symbols are, however, present in the executable, because 'nm' can find them:

[lxplus049] % nm -C -a teststack.exe | grep func
08048e78 T func1(void)
08048e20 T func2(void)
08048dc8 T func3(void)
08048d70 T func4(void)
08048bd4 T func5(void)

Could anybody please help me solve this problem?
What am I doing wrong or forgetting?
Do I need some special compile or link options?

Regards,
Martin Woudstra.

I am running on
Red Hat Linux release 6.1 (Cartman)
Kernel 2.2.19-6.2.1.1smp on a 2-processor i686
I tried gcc versions 2.91.66 and 2.95.2 (both give same result).

I used the following compile/link command:
c++ -Wl,-export-dynamic -o teststack.exe ../test/testStackTrace.cxx

testStackTrace.cxx:

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <execinfo.h>

void func5() { cout << "Calling func5() giving backtrace" << endl;

   const int maxTrace = 1000;
   void* buffer[maxTrace];
   int nTrace = backtrace ( buffer, maxTrace );
   char** trace = backtrace_symbols ( buffer, nTrace );
   if ( trace ) {
      for ( int i = 0; i < nTrace; ++i ) {
         cout << "(" << setw(2) << i << ")  " << trace[ i ] << endl;
      }
      // free memory
      free( trace );
   }
}

void func4() { cout << "Calling func4()" << endl; func5(); }
void func3() { cout << "Calling func3()" << endl; func4(); }
void func2() { cout << "Calling func2()" << endl; func3(); }
void func1() { cout << "Calling func1()" << endl; func2(); };

int main () {

   func1();
   
   return 0;
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]