Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing auto-load script in gdb #33159

Open
nagisa opened this issue Apr 22, 2016 · 9 comments
Open

Missing auto-load script in gdb #33159

nagisa opened this issue Apr 22, 2016 · 9 comments
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nagisa
Copy link
Member

nagisa commented Apr 22, 2016

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.

@nagisa nagisa added the A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) label Apr 22, 2016
@jimblandy
Copy link
Contributor

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

@bsed
Copy link

bsed commented Jun 17, 2017

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'

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 25, 2017
@sapphire-arches
Copy link
Contributor

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())

@PhilipDaniels
Copy link
Contributor

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?

@Mrknister
Copy link

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

@wllenyj
Copy link

wllenyj commented Jul 23, 2021

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.

@c02y
Copy link

c02y commented Dec 8, 2021

@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.

@jwatt
Copy link

jwatt commented Dec 25, 2021

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        

@fencekicker
Copy link

fencekicker commented Sep 14, 2022

I've hit a similar issue while running rust-gdb on Ubuntu 20.04 (I have version 1.59.0+dfsg1ubuntu1llvm-1ubuntu120.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.

@Nilstrieb Nilstrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests