Search code examples
gdbshared-libraries

How to make GDB verbose about its solib search process


I'm having trouble making GDB load a particular library as a replacement for a library used in a core file.

How can I make so GDB list which paths it tries for each library? Something like set debug auto-load on, but for shared libraries.


Solution

  • I'm having trouble making GDB load a particular library as a replacement for a library used in a core file.

    GDB will look up absolute paths it obtains from the core.

    Because of this, setting solib-search-path is ineffective, and only setting sysroot or solib-absolute-prefix is.

    In addition, you need to set sysroot or solib-absolute-prefix before loading the core. That is, this works:

    gdb -ex 'set sysroot /tmp/sysroot' -ex 'file a.out' -ex 'core core'
    

    whereas this doesn't (sysroot setting happens after the file and core are already loaded):

    gdb -ex 'set sysroot /tmp/sysroot` a.out core
    

    How can I make so GDB list which paths it tries for each library?

    There doesn't appear to be such an option. In particular, set debug solib on doesn't actually help here. Using trunk GDB:

    gdb/gdb
    GNU gdb (GDB) 14.0.50.20230115-git
    ...
    (gdb) set debug solib on
    (gdb) file /tmp/a.out
    Reading symbols from /tmp/a.out...
    (gdb) core /tmp/core
    [New LWP 29158]
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib64/libthread_db.so.1".
    Core was generated by `./a.out'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  0x0000000000401116 in main () at crash.c:3
    3         return *ip;
    (gdb)
    

    You can determine where GDB is looking for shared libraries by running it under strace:

    strace -e open,openat -o /tmp/strace.out  gdb/gdb -ex quit /tmp/a.out /tmp/core
    

    In /tmp/strace.out:

    ...
    --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=29289, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
    openat(AT_FDCWD, "/sys/devices/system/cpu/online", O_RDONLY|O_CLOEXEC) = 3
    openat(AT_FDCWD, "/tmp/a.out", O_RDONLY|O_CLOEXEC) = 9
    openat(AT_FDCWD, "/tmp/a.out", O_RDONLY|O_CLOEXEC) = 9
    openat(AT_FDCWD, "/tmp/a.out", O_RDONLY) = 9
    openat(AT_FDCWD, "/tmp/core", O_RDONLY|O_CLOEXEC) = 9
    openat(AT_FDCWD, "/tmp/a.out", O_RDONLY|O_CLOEXEC) = 10
    openat(AT_FDCWD, "/tmp/a.out", O_RDONLY) = 10
    openat(AT_FDCWD, "/tmp/a.out", O_RDONLY|O_CLOEXEC) = 11
    openat(AT_FDCWD, "/usr/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 11
    openat(AT_FDCWD, "/usr/lib64/libc.so.6", O_RDONLY) = 11
    openat(AT_FDCWD, "/usr/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 12
    openat(AT_FDCWD, "/usr/lib64/ld-linux-x86-64.so.2", O_RDONLY|O_CLOEXEC) = 12
    openat(AT_FDCWD, "/usr/lib64/ld-linux-x86-64.so.2", O_RDONLY) = 12
    openat(AT_FDCWD, "/usr/lib64/ld-linux-x86-64.so.2", O_RDONLY|O_CLOEXEC) = 13
    ...