Search code examples
gccgdblibcurl

Step into libcurl functions while debugging a program that uses the library with gdb


I am trying to debug a program that uses libcurl library and step into the library functions with gdb.

What I've tried so far. I downloaded curl's sources

git clone https://github.com/curl/curl.git

and built it with autotools (following this post https://askubuntu.com/questions/27677/cannot-find-install-sh-install-sh-or-shtool-in-ac-aux):

libtoolize --force
aclocal
autoheader
automake --force-missing --add-missing
autoconf

and configured like:

./configure --prefix=$HOME/curl --enable-debug

Finally

make
make install

Now I can debug the curl program generated with gdb $HOME/curl/bin/curl.

What I want though is to debug my own program and be able to debug it accessing the libcurl internals. So I tried with this simple example https://everything.curl.dev/libcurl/examples/get , which I built with

gcc -g example.c -L$HOME/curl/lib -lcurl 

It looks like it is linking the sources I've just built because

gcc -g -Wl,--verbose example.c -L${HOME}/curl/lib -lcurl | grep "libcurl.so succeeded" 

gives

attempt to open /home/user//curl/lib/libcurl.so succeeded

But when I debug the program gdb ./a.out I am unable to jump into the libcurl functions.

Is there a way to achieve this?


Solution

  • But when I debug the program gdb ./a.out I am unable to jump into the libcurl functions.

    This is because the library used at (static) link time and the library used at runtime don't necessarily match, and don't match in your case.

    To make them match, you need to tell the runtime loader where to look for libcurl.so.

    You can do this with export LD_LIBRARY_PATH=$HOME/curl/lib, but that is suboptimal -- this environment variable affects all programs, and may cause them to crash.

    A better way is to set the RUNPATH of the particular program:

    gcc -g example.c -L$HOME/curl/lib -lcurl -Wl,-rpath=$HOME/curl/lib
    

    With above, the a.out executable linked against $HOME/curl/lib/libcurl.so will also use that library at runtime, while other binaries will continue to use the system /lib/x86_64-linux-gnu/libcurl.so.