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.
There are two ways
addr2lineis used in the script in the article you linked:and:
The first one uses the
-foption, which causesaddr2lineto 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 throughhead -1).atosalways outputs the function name, so there's no need for an equivalent to that-foption. [Whereasaddr2lineis short for "address to line" (filename and line number), making the function name ancillary to its main purpose,atosis short for "address to symbol", so producing the symbol name is its core purpose.]The next option used for
addr2lineis-e ${EXECUTABLE}. The equivalent foratosis-o ${EXECUTABLE}.After that, the arguments are addresses. That's the same between
addr2lineandatos.So, the
atoscommand that corresponds toaddr2line -f -e ${EXECUTABLE} ${FADDR}isatos -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 ofatos, you can pipe it throughperl -lne 'print "$1" if m/^(.*) \(in .*\)/'.The second type of
addr2linecommand does not use the-foption, so it doesn't print the function name. It's just used to get the filename and line number. As mentioned before,atosalways prints the function name. So, theatoscommand is the same as before. To get just the file name and line number from its output, you can pipe it throughperl -lne 'print "$1" if m/^.* \(in .*\) \((.*)\)$/'.This
addr2linecommand also uses the-soption. That makes it print only the basename of the file path, not the whole path. That's whatatosdoes anyway, so there's no need to translate that option to anything.