1

I am debugging typical C++ Apps that use shared libraries.

For better diagnostics I want to call my own debugging helper functions (eg. creating a new std::string) in gdb that are contained in a shared library.

How can I load my own shared library in gdb?

I am looking for something like this:

gdb myApp
(gdb) load mySharedLibrary.so  # this command does not exist in gdb!
(gdb) breakpoint somewhere
(gdb) run
(gdb) # breakpoint hit
(gdb) print myDebug_function...

Note: Since I don't have the source code I cannot modify the App code to put my debug functions in there...

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • why don't you just `export LD_LIBRARY_PATH`? So, let's say your shared library is positioned inside the `/home/yoda/Desktop` folder. Then this command should help you: `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/yoda/Desktop` – NutCracker Nov 16 '19 at 08:37
  • I am not quite sure if setting the `LD_LIBRARY_PATH` is enough to load a library into `gdb`... The debugging helper library is **not** part of the application... – R Yoda Nov 16 '19 at 08:46
  • I suppose you have tried and it does not work? Have you tried [this](https://stackoverflow.com/questions/28352379/cannot-load-shared-libraries-in-gdb)? – NutCracker Nov 16 '19 at 08:54
  • 1
    On linux you might want to try the [LD_PRELOAD trick](https://stackoverflow.com/questions/10448254/how-to-use-gdb-with-ld-preload/10448351#10448351). – G.M. Nov 16 '19 at 08:54
  • @NutCracker It did not work (I wouldn't understand how it should work without explicitly saying the name of the additional shared lib to be loaded since it is not referenced in the binaries of the application I am debugging). I guess `LD_PRELOAD` is the way it should work (I am just trying it...) – R Yoda Nov 16 '19 at 09:00
  • @RYoda i am not the expert but if you add the path where your `.so` is being stored to the `LD_LIBRARY_PATH` then this directory is going to be searched at the time of the library being used – NutCracker Nov 16 '19 at 09:04
  • @G.M. Thanks, `LD_PRELOAD` works basically (I am just having problems with my library but that should be solvable at my side: `ERROR: ld.so: object './CppDebugHelper.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.`) – R Yoda Nov 16 '19 at 09:05
  • @G.M. Damn it works (it was just a typo in my lib file name :-) `(gdb) info sharedlibrary` shows my lib as loaded. Please post this as an answer so I can accept this finally... THX! – R Yoda Nov 16 '19 at 09:14
  • @G.M. Up to you, that's fine. – R Yoda Nov 16 '19 at 09:21

0 Answers0