Line Number Info in ltrace and strace tools
Is it possible that I can view the line number and file name (for my program running with ltrace/strace) along with the library call/system call information.
Eg:
code section :: ptr = malloc(sizeof(int)*5); (file:code.c, li开发者_如何学运维ne:21)
ltrace or any other tool: malloc(20) :: code.c::21
I have tried all the options of ltrace/strace but cannot figure out a way to get this info.
If not possible through ltrace/strace, do we have any parallel tool option for GNU/Linux?
You may be able to use the -i
option (to output the instruction pointer at the time of the call) in strace
and ltrace
, combined with addr2line
to resolve the calls to lines of code.
No It's not possible. Why don't you use gdb for this purpose?
When you are compiling application with gcc use -ggdb flags to get debugger info into your program and then run your program with gdb or equivalent frontend (ddd or similar)
Here is quick gdb manual to help you out a bit. http://www.cs.cmu.edu/~gilpin/tutorial/
You can use strace-plus that can collects stack traces associated with each system call. http://code.google.com/p/strace-plus/
Pretty old question, but I found a way to accomplish what OP wanted:
First use strace with -k
option, which will generate a stack trace like this:
openat(AT_FDCWD, NULL, O_RDONLY) = -1 EFAULT (Bad address) > /usr/lib/libc-2.33.so(__open64+0x5b) [0xefeab] > /usr/lib/libc-2.33.so(_IO_file_open+0x26) [0x816f6] > /usr/lib/libc-2.33.so(_IO_file_fopen+0x10a) [0x818ca] > /usr/lib/libc-2.33.so(__fopen_internal+0x7d) [0x7527d] > /mnt/r/build/tests/main(main+0x90) [0x1330] > /usr/lib/libc-2.33.so(__libc_start_main+0xd5) [0x27b25] > /mnt/r/build/tests/main(_start+0x2e) [0x114e]
The address of each function call are displayed at the end of each line, and you can paste it to addr2line
to retrieve the file and line. For example, we want to locate the call in main()
(fifth line of the stack trace).
addr2line -e tests/main 0x1330
It will show something like this:
/mnt/r/main.c:55
精彩评论