6

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, line: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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sandeep Singh
  • 4,941
  • 8
  • 36
  • 56

4 Answers4

6

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.

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
  • 3
    How can this help with `strace`, since the instruction pointer will be inside libc? You'd need a couple levels of backtrace for it to be useful... – R.. GitHub STOP HELPING ICE Jul 24 '11 at 13:11
  • @R..: hence the _may_, I originally thought of adding a caveat re: calls from libraries, which could also equally apply to `ltrace`, but couldn't find adequate phrasing. This is the closest one will get with ltrace/strace, though. – Hasturkun Jul 24 '11 at 13:31
  • yes, for ltrace it works well. For strace, it is not able to find the address. Do we have any way to map strace o/p to code as well? – Sandeep Singh Jul 24 '11 at 13:34
  • @SandeepSingh: The problem is that unless your code is doing syscalls on its own (or your libc is inlined into your code), the addresses will correspond to parts of the C library, and not your code. you may want to use the ltrace `-S` option to display system calls as well as library calls – Hasturkun Jul 24 '11 at 13:44
  • Now, I am badly trapped. Suppose my code includes a Wrapper Function: mymalloc() for malloc. Now, addr2line will map malloc to mymalloc only, not to the code section from where it is called. – Sandeep Singh Jul 24 '11 at 13:56
  • @SandeepSingh: in that case use ltrace, you'll see the call to mymalloc and where it originated. alternately, you can emit the call address from your wrapper and skip strace/ltrace completely. – Hasturkun Jul 24 '11 at 14:24
1

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/

damir
  • 1,898
  • 3
  • 16
  • 23
  • 1
    Thanks, I always use gdb while running my programs. But the problem is somewhat different: Suppose I am having a large code base, and I am receiving Memory Leaks. If I can figure out the line numbers, I can catch the offending code more easily. Purify provides this info, but I find it somewhat less effective in tracing down the exact point of problem – Sandeep Singh Jul 24 '11 at 10:08
0

You can use strace-plus that can collects stack traces associated with each system call. http://code.google.com/p/strace-plus/

m7yang
  • 1
0

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
SdtElectronics
  • 566
  • 4
  • 8