Monday, August 24, 2009

HOW TO DEBUG shared library using GDB

HOW TO DEBUG shared library using GDB

------------------------------------------------------

[bhushan@Shared_Lib_Debug]$ gcc -fpic -shared -o foo.so foo.c

[bhushan@Shared_Lib_Debug]$ gcc -o main main.c ./foo.so -g

[bhushan@Shared_Lib_Debug]$ gdb main
GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".


(gdb) b foo
Function "foo" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (foo) pending.

(gdb) r

Starting program: /home/bhushan/RD/Shared_Lib_Debug/main
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0x470000
Breakpoint 2 at 0xdb7493

Pending breakpoint "foo" resolved
Breakpoint 2, 0x00db7493 in foo () from ./foo.so

(gdb) s

Single stepping until exit from function foo,
which has no line number information.
main () at main.c:7
7 printf("inside main i = %d\n", i);
(gdb) s
inside main i = 4
8 return 0;

NOW if you build the shared libarary using -g option
[bhushan@Shared_Lib_Debug]$ gcc -fpic -shared -o foo.so foo.c -g

[bhushan@Shared_Lib_Debug]$ gcc -o main main.c ./foo.so -g

[bhushan@Shared_Lib_Debug]$ gdb main
GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".


(gdb) b foo

Function "foo" not defined.

Make breakpoint pending on future shared library load? (y or [n]) y

Breakpoint 1 (foo) pending.

(gdb) r

Starting program: /home/bhushan/RD/Shared_Lib_Debug/main
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0x470000
Breakpoint 2 at 0x1c5493: file foo.c, line 5.
Pending breakpoint "foo" resolved

Breakpoint 2, foo () at foo.c:5
5 return 2*2;

(gdb) s

7 }

(gdb) s

main () at main.c:7
7 printf("inside main i = %d\n", i);

(gdb) s

inside main i = 4

8 return 0;

(gdb)

U can see the differences in bold lines.

No comments: