Creating C ++ BackTraces on OS / X (10.5.7)

I use backtrace and backtrace_symbols to create trace software stacks for registration / diagnostic purposes. This seems to work roughly, however, I will cripple a bit, and there are no file / line numbers associated with each function call (as I would expect in a gdb bt call or something else). Here is an example:

1 leonardo 0x00006989 _ZN9ExceptionC2E13ExceptionType + 111
2 leonardo 0x00006a20 _ZN9ExceptionC1E13ExceptionType + 24
3 leonardo 0x0000ab64 _ZN5Rules11ApplyActionER16ApplicableActionR9GameState + 1060
4 leonardo 0x0000ed15 _ZN9Simulator8SimulateEv + 2179
5 leonardo 0x0000eec9 _ZN9Simulator8SimulateEi + 37
6 main leonardo 0x00009729 + 45
7 leonardo 0x000025c6 start + 54

Is there something I'm missing out on, doing something stupid, or is that all I can expect from backtrace on OS / X?

Some other tidbits:

I do not see the link rdynamicfor the g ++ version (4.0.1) that I am using.
-g/-g3it does not matter. abi::__cxa__demangledoesn't seem to be doing anything
+3
source share
2 answers

Backtracks are usually returned from backtrace_symbols in the following format:

./MyApp (_ZN4test3fooEv + 0x8) [0x821c874]

abi :: __ cxa_demangle expects only the name of the function. Thus, you must first perform some analysis on the track:

      std::string trace(backtrace[idx]);

      // attempt to demangle
      {
         std::string::size_type begin, end;

         // find the beginning and the end of the useful part of the trace
         begin = trace.find_first_of('(') + 1;
         end = trace.find_last_of('+');

         // if they were found, we'll go ahead and demangle
         if (begin != std::string::npos && end != std::string::npos) {
            trace = trace.substr(begin, end - begin);

            size_t maxName = 1024;
            int demangleStatus;

            char* demangledName = (char*) malloc(maxName);
            if ((demangledName = abi::__cxa_demangle(trace.c_str(), demangledName, &maxName,
                  &demangleStatus)) && demangleStatus == 0) {
               trace = demangledName; // the demangled name is now in our trace string
            }
            free(demangledName);
         }
      }

, :

:: Foo()

, , , .

+4

backtrace_symbols(), , + .

abi:: __cxa__demangle, , + line, .

+2

Source: https://habr.com/ru/post/1715110/


All Articles