Missing auto-load script in gdb

f13c11d
Opened by Simonas Kazlauskas at 2023-04-05 17:45:45

When compiled with -g, we produce binaries which contain a gdb_load_rust_pretty_printers.py in .debug_gdb_scripts section. This makes gdb complain about missing scripts when run under plain gdb as opposed to the rust-gdb wrapper script.

Distribution puts this script into $INSTALL_ROOT/lib/rustlib/etc, which is someplace gdb wouldn’t ever look at by default. Some experimentation suggests that placing the scripts at gdb’s DATA-DIRECTORY/python/gdb/printer/ will at least make gdb detect the presence of the script, but then it fails to load the module due to import errors.

  1. Looking into GDB's source code, GDB uses the function find_and_open_script to search for the files listed in the .debug_gdb_scripts ELF section. This function actually uses the source file search path (the one set with GDB's dir command) to find the script, apparently on the assumption that the .debug_gdb_scripts section is associated with the specific executable, not the toolchain.

    As an experiment, I added this to my .gdbinit:

    add-auto-load-safe-path /home/jimb/.rustup/toolchains
    dir /home/jimb/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/etc
    

    This did help GDB find gdb_load_rust_pretty_printers.py, but then of course that file doesn't add its directory to Python's path, so the import fails:

    Traceback (most recent call last):
      File "/home/jimb/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/etc/gdb_load_rust_pretty_printers.py", line 11, in <module>
        import gdb_rust_pretty_printing
    ImportError: No module named gdb_rust_pretty_printing
    (gdb) 
    

    CC @tromey

    Jim Blandy at 2017-03-10 01:00:54

  2. Traceback (most recent call last):
      File "/home/kelvin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/etc/gdb_load_rust_pretty_printers.py", line 11, in <module>
        import gdb_rust_pretty_printing
    ImportError: No module named 'gdb_rust_pretty_printing'
    

    sed at 2017-06-17 03:14:28

  3. This super hacky patch will let GDB load the files correctly, at least on my machine

    diff --git a/src/etc/gdb_load_rust_pretty_printers.py b/src/etc/gdb_load_rust_pretty_printers.py
    index 755cac153d..48eba096fc 100644
    --- a/src/etc/gdb_load_rust_pretty_printers.py
    +++ b/src/etc/gdb_load_rust_pretty_printers.py
    @@ -8,5 +8,12 @@
     # option. This file may not be copied, modified, or distributed
     # except according to those terms.
     
    +# Hacky fix for paths being annoying
    +import sys
    +from os import path
    +self_dir = path.dirname(path.realpath(__file__))
    +sys.path.append(self_dir)
    +
     import gdb_rust_pretty_printing
     gdb_rust_pretty_printing.register_printers(gdb.current_objfile())
    

    Sapphire Koser at 2018-03-22 18:38:43

  4. For info, I've been experimenting debugging Rust programs using https://github.com/cs01/gdbgui Just got the basic setup working, I can confirm that the two hacks here (edit the py file, create .gdbinit) suppressed the warning.

    I'm not sure what the pretty printers are capable of though, is there documentation anywhere?

    Philip Daniels at 2018-04-24 20:42:59

  5. I had the same problem and found out that you can also just set the PYTHONPATH environment variable instead of changin the python script.

    export PYTHONPATH=$PYTHONPATH:~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/etc

    Mrknister at 2018-05-29 09:44:45

  6. I have this problem when I debug rust programs directly with gdb.

    But when I use rust-gdb debugging, I don't have that problem.

    wanglei01 at 2021-07-23 09:06:04

  7. @Mrknister

    export PYTHONPATH=$PYTHONPATH:~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/etc

    export PYTHONPATH=$PYTHONPATH:~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/etc for me, it doesn't help, I got the same error message, and gdb_load_rust_pretty_printers.py is not loaded if executing "info auto-load python-scripts" in gdb prompt.

    @wllenyj

    But when I use rust-gdb debugging, I don't have that problem.

    The warning auto-load script is gone, and gdb_load_rust_pretty_printers.py is loaded from ~/.rustup directory, but a lot of commands I wrote in my gdbinit print errors or don't work as expected like when I debug C/C++ binaries. For example, when you try to dashboard source -output $tty to output the source code part into another terminal or tmux pane, next line in gdb prompt will case the source code terminal append the source code instead of reloading the screen with the new code.

    c02y at 2021-12-08 20:57:20

  8. export PYTHONPATH=$PYTHONPATH:~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/etc for me, it doesn't help, I got the same error message, and gdb_load_rust_pretty_printers.py is not loaded if executing "info auto-load python-scripts" in gdb prompt.

    In addition to setting PYTHONPATH you also need to add the lines jimblandy gave to your .gdbinit:

    add-auto-load-safe-path ~/.rustup/toolchains                                    
    dir ~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/etc        
    

    Jonathan Watt at 2021-12-25 01:18:46

  9. I've hit a similar issue while running rust-gdb on Ubuntu 20.04 (I have version 1.59.0+dfsg1~ubuntu1~llvm-1~ubuntu1~20.04.2):

    Traceback (most recent call last):
      File "/usr/lib/rustlib/etc/gdb_load_rust_pretty_printers.py", line 2, in <module>
        import gdb_lookup
      File "/usr/lib/rustlib/etc/gdb_lookup.py", line 5, in <module>
        from rust_types import *
    ModuleNotFoundError: No module named 'rust_types'
    

    My issue, however, seems to be related to packaging, since the rust-gdb script applies the 2 workarounds mentioned above (setting PYTHONPATH and running the 2 gdb commands). It seems that in my case, the packager forgot to put rust_types.py in the deb package. Manually copying the file from a local install of the toolchain to /usr/lib/rustlib/etc/ made the error go away.

    fencekicker at 2022-09-14 08:34:44