0

This is my code:

/* backtrace_foo1.c */
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

#define BACKTRACE() \
do {\
    void *array[20];\
    size_t size;\
    char **strings;\
    size_t i;\
    size = backtrace(array, 20);\
    strings = backtrace_symbols(array, size);\
    for (i = 0; i < size; i++) {\
        printf ("%s\n", strings[i]);\
    }\
    free (strings);\
} while(0)

void func1()
{
    BACKTRACE();
}

void func()
{
    func1();
}

int main(int argc, char **argv)
{
    func();
    return 0;
}

I compiled it by gcc -g -rdynamic and got

./a.out(func1+0x1f) [0x400905]

./a.out(func+0xe) [0x40097a]

./a.out(main+0x19) [0x400996]

/lib64/libc.so.6(__libc_start_main+0xfd) [0x318ae1ecdd]

./a.out() [0x4007f9]

Then i use addr2line -e ./a.out -f 0x4007f9, i got

_start

??:0

This is my platform

gcc version 5.3.0 (GCC)

Linux 3.10.0_1-0-0-8

Community
  • 1
  • 1
Wonter
  • 293
  • 1
  • 5
  • 15
  • For future questions, please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). And don't forget to actually *ask* one. – Some programmer dude Aug 25 '17 at 08:36

1 Answers1

2

I shouldn't really answer this, since you don't really have a question or a stated problem. But sometimes I'm feeling nice...

While the code you write will start execution with the main function, the actual starting point is somewhere before that. There is startup code that will initialize the stdio system (stdin, stdout etc.) and initialize other things. This startup code then calls your main function like any other function.

The "problem" is that the startup code is not really part of your code, it's often a precompiled object file that the frontend program links your program with. And that object file probably doesn't have any kind of debug information, so you can't get any location information about it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    I was ready to hammer this as a dupe to https://stackoverflow.com/questions/7648642/how-to-use-addr2line-command-in-linux but then it looks to me that your answer differs somehow. Since you have more knowledge on this subject use your judgment for closing it as a dupe or not. – bolov Aug 25 '17 at 08:29
  • 1
    @bolov The problem in that question is really the same, but the question is different. That question asks about how to use `addr2line` without asking about why information could not be gotten, and the answers follow that line. This question (I assume) is about why `addr2line` provides no information. – Some programmer dude Aug 25 '17 at 08:34