2

I see the following example using addr2line. But the interfaces of atos and addr2line are different. I am not sure how to make it work using atos. Could anybody show me how to convert it to using atos?

https://balau82.wordpress.com/2010/10/06/trace-and-profile-function-calls-with-gcc/

Alternatively, is there a way to make addr2line work on Mac (It is known that addr2line does not work well on Mac OS X)? I just get ?? instead of function names using addr2line. Thanks.

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • The “`??`” symbol indicates that addr2line has no debug information on that address ... – l'L'l Jan 23 '19 at 04:33
  • Possible duplicate of [readelf-like tool for Mac OS X?](https://stackoverflow.com/questions/3286675/readelf-like-tool-for-mac-os-x) – l'L'l Jan 23 '19 at 04:35
  • Why it is duplicated? The link you referred mentioned nothing about atos. – user1424739 Jan 23 '19 at 04:43
  • It’s “possible duplicate”; there’s an answer there that mentioned installing MacPorts binutils, maybe have a look at that and if it doesn’t solve your problem report back and I’ll reverse the close vote. – l'L'l Jan 23 '19 at 05:56
  • It is known that addr2line does not work well on Mac OS X. – user1424739 Jan 23 '19 at 14:26

1 Answers1

3

There are two ways addr2line is used in the script in the article you linked:

addr2line -f -e ${EXECUTABLE} ${FADDR}

and:

addr2line -s -e ${EXECUTABLE} ${CADDR}

The first one uses the -f option, which causes addr2line to output the function name on a line by itself before showing the filename and line number on a second line. In that script, only the first line is used (it's piped through head -1).

atos always outputs the function name, so there's no need for an equivalent to that -f option. [Whereas addr2line is short for "address to line" (filename and line number), making the function name ancillary to its main purpose, atos is short for "address to symbol", so producing the symbol name is its core purpose.]

The next option used for addr2line is -e ${EXECUTABLE}. The equivalent for atos is -o ${EXECUTABLE}.

After that, the arguments are addresses. That's the same between addr2line and atos.

So, the atos command that corresponds to addr2line -f -e ${EXECUTABLE} ${FADDR} is atos -o ${EXECUTABLE} ${FADDR}. However, the script is "parsing" the output from the command and the two programs produce output in different formats. To get just the function name from the output of atos, you can pipe it through perl -lne 'print "$1" if m/^(.*) \(in .*\)/'.

The second type of addr2line command does not use the -f option, so it doesn't print the function name. It's just used to get the filename and line number. As mentioned before, atos always prints the function name. So, the atos command is the same as before. To get just the file name and line number from its output, you can pipe it through perl -lne 'print "$1" if m/^.* \(in .*\) \((.*)\)$/'.

This addr2line command also uses the -s option. That makes it print only the basename of the file path, not the whole path. That's what atos does anyway, so there's no need to translate that option to anything.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • 1
    I tried it. But it just print the original address `$ atos -o main.exe 0x7fff569f0015\n 0x7fff569f0015`. Is there anything wrong? – user1424739 Jan 23 '19 at 14:20
  • 1
    My guess is that your program was loaded at a (somewhat) randomized address due to ASLR. If that happens, you have to pass either the load address or the slide to `atos` or it won't be able to match addresses to symbols. You can prevent it by linking the program with the `-no_pie` linker option. (If the compiler is driving the linker, as it often is, you would pass it as `-Wl,-no_pie`.) – Ken Thomases Jan 23 '19 at 16:11
  • OK. This works most of the time. But it occasionally says `atos[79546]: [fatal] child process status could not be determined; exit code unavailable.` Do you know what is wrong? – user1424739 Jan 23 '19 at 16:21
  • I'm not familiar with that error. Are you using `-o ` or `-p `? The mention of a child process makes me think the latter, but maybe it runs some internal helper process and that's what it's talking about. – Ken Thomases Jan 23 '19 at 19:03
  • I used the command that you mentioned with -o. Since the process is finished, there is no way to specify the pid. – user1424739 Jan 23 '19 at 20:05
  • Are you maybe running a gazillion instances of `atos` simultaneously? – Ken Thomases Jan 23 '19 at 21:15