Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a shared library using GDB?

I want to debug a SharedLib which is called by Main. I have the sources of the SharedLib and of Main. I compile and link Main and SharedLib with the option –g3. The variables PATH and LD_LIBRARY_PATH include /PathToSharedLib. I set a breakpoint in b FileOfSharedLib.c at NNN.

(gdb) file /PathToMain/Main
Reading symbols from /PathToMain/Main...done.
(gdb) set directories /PathToSourceOfSharedLib
(gdb) set solib-search-path /PathToSharedLib
(gdb) sharedlibrary SharedLib
No loaded shared libraries match the pattern `SharedLib`
(gdb) b FileOfSharedLib.c:NNN
No source file named FileOfSharedLib.c.
Make breakpoint pending on future shared library load? (y or [n]) y
(gdb) run

It never stops at the break point which is called several times by Main. I see it by printf statements in the log of the SharedLib. It looks like that the SharedLib is not considered by gdb.

I checked the SharedLib and the FileOfSharedLib.c:

(gdb) file /PathToSharedLib/SharedLib
Reading symbols from /PathToSharedLib/SharedLibl...done.
(gdb) b FileOfSharedLib.c:123
Breakpoint 1 at 0x43d7f: file FileOfSharedLib.c, line 123.
(gdb)

I am running Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-66-generic i686) with gcc 4.8.4 and gdb 7.7.1.

like image 585
musbach Avatar asked Feb 07 '23 20:02

musbach


1 Answers

It never stops at the break point which is called several times by Main.

This implies a bug in GDB. Unfortunately your version of GDB is too old for developers to care. Try reproducing this behavior with current GDB 7.11.1, and file a bug in GDB bugzilla if it does reproduce.

If you don't want to use GDB 7.11.1, you can work around this bug by setting a breakpoint after the shared library has been loaded.

If the library is linked into the main executable directly, the following sequence should work:

(gdb) start
# GDB stop on entry to main

(gdb) b FileOfSharedLib.c:NNN  # should find the source now

If the library is dlopened, you'll need to set a temporary breakpoint in the main exectable somewhere after that dlopen.

Another workaround: set stop-on-solib-events 1. This will make GDB stop after any new shared library is added (or removed).

like image 126
Employed Russian Avatar answered Feb 15 '23 10:02

Employed Russian