user277465
user277465

Reputation:

inspecting where a function is located

I'm debugging a daemon in gdb by attaching gdb to the daemon process, and setting gdb to follow the child when it forks. It would be very useful to somehow figure out where the function I am in currently, is located - i.e. if the function were present in the daemon binary or a shared object that were loaded. If it were a shared object, it would be useful to have its name or path.

Can this be done?

Upvotes: 0

Views: 258

Answers (2)

Employed Russian
Employed Russian

Reputation: 213506

(gdb) info symbol 0x002a4995
_IO_new_file_write + 7 in section .text of /lib/i386-linux-gnu/libc.so.6

Upvotes: 5

ks1322
ks1322

Reputation: 35716

It would be very useful to somehow figure out where the function I am in currently, is located

You can do it by comparing function address with addresses of currently loaded shared libraries.

(gdb) bt
#0  0x00130416 in __kernel_vsyscall ()
#1  0x002fc683 in __write_nocancel () at ../sysdeps/unix/syscall-template.S:82
#2  0x002a4995 in _IO_new_file_write (f=0x3b4500, data=0xb7ffd000, n=4) at fileops.c:1276
#3  0x002a4874 in new_do_write (fp=0x3b4500, data=0xb7ffd000 "123\n", to_do=4) at fileops.c:530
#4  0x002a5eee in _IO_new_do_write (fp=0x3b4500, data=0xb7ffd000 "123\n", to_do=4) at fileops.c:503
#5  0x002a6235 in _IO_new_file_overflow (f=0x3b4500, ch=10) at fileops.c:889
#6  0x002a6e4b in __GI___overflow (f=0x3b4500, ch=10) at genops.c:248
#7  0x0029da6f in _IO_putc (c=10, fp=0x3b4500) at putc.c:33
#8  0x001b835b in __gnu_cxx::stdio_sync_filebuf<char, std::char_traits<char> >::overflow(int) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#9  0x001b966e in std::ostream::put(char) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#10 0x001b98e4 in std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#11 0x001b8cae in std::ostream::operator<<(std::ostream& (*)(std::ostream&)) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#12 0x08048ac5 in main () at so-test.cpp:10
(gdb) info sharedlibrary 
From        To          Syms Read   Shared Object Library
0x00110830  0x001275af  Yes (*)     /lib/ld-linux.so.2
0x00176e50  0x001e93a8  Yes (*)     /usr/lib/i386-linux-gnu/libstdc++.so.6
0x0021df50  0x00234cf8  Yes (*)     /lib/i386-linux-gnu/libgcc_s.so.1
0x00250be0  0x0035ebd4  Yes         /lib/i386-linux-gnu/libc.so.6
0x003bb4b0  0x003d6ab8  Yes         /lib/i386-linux-gnu/libm.so.6
(*): Shared library is missing debugging information.
(gdb) 

For example _IO_new_file_write address is 0x002a4995, which lies between 0x00250be0 and 0x0035ebd4 where /lib/i386-linux-gnu/libc.so.6 was loaded. That is _IO_new_file_write belongs to libc.so.6 library.

Upvotes: 1

Related Questions