Skip to contentSkip to conversation

scripts/gdb: Ensure the absolute path is generated on initial source

PATCH Applied Aaron Tomlin 3 participants 5 messages Jul 12, 2022 – Dec 14, 2022 GDB kernel debugging helper scripts

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 scripts/gdb: Ensure the absolute path is generated on initial source

Post 'make scripts_gdb' a symbolic link to scripts/gdb/vmlinux-gdb.py is created. Currently 'os.path.dirname(__file__)' does not generate the absolute path to scripts/gdb resulting in the following:

(gdb) source vmlinux-gdb.py
Traceback (most recent call last):
File "scripts/gdb/vmlinux-gdb.py", line 25, in <module>
import linux.utils
ModuleNotFoundError: No module named 'linux'

This patch ensures that the absolute path to scripts/gdb in relation to the given file is generated so each module can be located accordingly.

---
 scripts/gdb/vmlinux-gdb.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/vmlinux-gdb.py +1 -1
index 4136dc2c59df..3e8d3669f0ce 100644
@@ -13,7 +13,7 @@
 
 import os
 
-sys.path.insert(0, os.path.dirname(__file__) + "/scripts/gdb")
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/scripts/gdb")
 
 try:
     gdb.parse_and_eval("0")
Signature · 1 line
2.34.3
#1 lore raw
Quoted 33 lines · Aaron Tomlin

Post 'make scripts_gdb' a symbolic link to scripts/gdb/vmlinux-gdb.py is created. Currently 'os.path.dirname(__file__)' does not generate the absolute path to scripts/gdb resulting in the following:

(gdb) source vmlinux-gdb.py
Traceback (most recent call last):
File "scripts/gdb/vmlinux-gdb.py", line 25, in <module>
import linux.utils
ModuleNotFoundError: No module named 'linux'

This patch ensures that the absolute path to scripts/gdb in relation to the given file is generated so each module can be located accordingly.

---
 scripts/gdb/vmlinux-gdb.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/vmlinux-gdb.py +1 -1
index 4136dc2c59df..3e8d3669f0ce 100644
@@ -13,7 +13,7 @@
 
 import os
 
-sys.path.insert(0, os.path.dirname(__file__) + "/scripts/gdb")
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/scripts/gdb")
 
 try:
     gdb.parse_and_eval("0")
Signature · 1 line
2.34.3

Any feedback?

Kind regards,

Signature · 1 line
Aaron Tomlin
Reviewed-by on v2 1/1
#2 lore raw
In reply to Aaron Tomlin

Hi,

Quoted 24 lines · Aaron Tomlin

Post 'make scripts_gdb' a symbolic link to scripts/gdb/vmlinux-gdb.py is created. Currently 'os.path.dirname(__file__)' does not generate the absolute path to scripts/gdb resulting in the following:

(gdb) source vmlinux-gdb.py
Traceback (most recent call last):
File "scripts/gdb/vmlinux-gdb.py", line 25, in <module>
import linux.utils
ModuleNotFoundError: No module named 'linux'

This patch ensures that the absolute path to scripts/gdb in relation to the given file is generated so each module can be located accordingly.

---
 scripts/gdb/vmlinux-gdb.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/vmlinux-gdb.py +0 -0
index 4136dc2c59df..3e8d3669f0ce 100644

scripts/gdb/vmlinux-gdb.py line 16

Quoted 5 lines
scripts/gdb/vmlinux-gdb.py +1 -1
@@ -13,7 +13,7 @@
 
 import os
 
-sys.path.insert(0, os.path.dirname(__file__) + "/scripts/gdb")
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/scripts/gdb")

It seems to generate an absolute path for me, but some quick web searches show that it's not guaranteed so I guess you must be hitting whatever corner cases it ends up being relative.

The fix seems reasonable to me, thus:

#3 lore raw
In reply to Aaron Tomlin
Quoted 9 lines · Aaron Tomlin

Post 'make scripts_gdb' a symbolic link to scripts/gdb/vmlinux-gdb.py is created. Currently 'os.path.dirname(__file__)' does not generate the absolute path to scripts/gdb resulting in the following:

(gdb) source vmlinux-gdb.py
Traceback (most recent call last):
File "scripts/gdb/vmlinux-gdb.py", line 25, in <module>
import linux.utils
ModuleNotFoundError: No module named 'linux'

So I recently moved to a new machine, and lost my previous .gdbinit.

I've re-added:
add-auto-load-safe-path /path/to/linux/

to my new ~/.gdbinit.

Upon startup, lx-dmesg and friends aren't available.

If I try:
(gdb) source scripts/gdb/vmlinux-gdb.py Traceback (most recent call last):
File "scripts/gdb/vmlinux-gdb.py", line 25, in <module>
import linux.utils
ModuleNotFoundError: No module named 'linux'

Which is exactly the error claimed to be fixed by
commit b99695580bfc ("scripts/gdb: ensure the absolute path is generated on initial source") even though I have b99695580bfc, which is how I found this thread to
reply to.

(gdb) show auto-load safe-path
List of directories from which it is safe to auto-load files is $debugdir:$datadir/auto-load:/path/to/linux/.

$ gdb-multiarch --version | head -n1
GNU gdb (Debian 12.1-3) 12.1
$ python3 --version
Python 3.10.8

I've also RTFM @
Documentation/dev-tools/gdb-kernel-debugging.rst
though perhaps there's something I've missed?

$ grep -rn GDB_SCRIPTS .config
4162:CONFIG_GDB_SCRIPTS=y

This definitely has worked in the past for me, on my previous workstation.

(gdb) show configuration
...
--with-python=/usr (relocatable) --with-python-libdir=/usr/lib (relocatable)
...
(gdb) apropos lx
(gdb)

No warnings from GDB either about the autoload path not being set. Running gdb in my top level kernel sources.

Am I holding it wrong?

Any tips on how to go about debugging this?

Quoted 26 lines

This patch ensures that the absolute path to scripts/gdb in relation to the given file is generated so each module can be located accordingly.

---
 scripts/gdb/vmlinux-gdb.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/vmlinux-gdb.py +1 -1
index 4136dc2c59df..3e8d3669f0ce 100644
@@ -13,7 +13,7 @@
 
 import os
 
-sys.path.insert(0, os.path.dirname(__file__) + "/scripts/gdb")
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/scripts/gdb")
 
 try:
     gdb.parse_and_eval("0")
Signature · 1 line
2.34.3
#4 lore raw
Quoted 59 lines · Nick Desaulniers
Quoted 9 lines · Aaron Tomlin

Post 'make scripts_gdb' a symbolic link to scripts/gdb/vmlinux-gdb.py is created. Currently 'os.path.dirname(__file__)' does not generate the absolute path to scripts/gdb resulting in the following:

(gdb) source vmlinux-gdb.py
Traceback (most recent call last):
File "scripts/gdb/vmlinux-gdb.py", line 25, in <module> import linux.utils
ModuleNotFoundError: No module named 'linux'

So I recently moved to a new machine, and lost my previous .gdbinit.

I've re-added:
add-auto-load-safe-path /path/to/linux/

to my new ~/.gdbinit.

Upon startup, lx-dmesg and friends aren't available.

If I try:
(gdb) source scripts/gdb/vmlinux-gdb.py Traceback (most recent call last):
File "scripts/gdb/vmlinux-gdb.py", line 25, in <module>
import linux.utils
ModuleNotFoundError: No module named 'linux'

Which is exactly the error claimed to be fixed by
commit b99695580bfc ("scripts/gdb: ensure the absolute path is generated on initial source") even though I have b99695580bfc, which is how I found this thread to
reply to.

(gdb) show auto-load safe-path
List of directories from which it is safe to auto-load files is $debugdir:$datadir/auto-load:/path/to/linux/.

$ gdb-multiarch --version | head -n1
GNU gdb (Debian 12.1-3) 12.1
$ python3 --version
Python 3.10.8

I've also RTFM @
Documentation/dev-tools/gdb-kernel-debugging.rst
though perhaps there's something I've missed?

$ grep -rn GDB_SCRIPTS .config
4162:CONFIG_GDB_SCRIPTS=y

This definitely has worked in the past for me, on my previous workstation.

(gdb) show configuration
...
--with-python=/usr (relocatable) --with-python-libdir=/usr/lib (relocatable)
...
(gdb) apropos lx
(gdb)

Quoted 6 lines

No warnings from GDB either about the autoload path not being set. Running gdb in my top level kernel sources.

Am I holding it wrong?

Any tips on how to go about debugging this?

PEBKAC; looks like I was building vmlinux and not all. D'oh!

Quoted 27 lines
Quoted 27 lines

This patch ensures that the absolute path to scripts/gdb in relation to the given file is generated so each module can be located accordingly.

---
 scripts/gdb/vmlinux-gdb.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/vmlinux-gdb.py +1 -2
index 4136dc2c59df..3e8d3669f0ce 100644
@@ -13,7 +13,7 @@
 
 import os
 
-sys.path.insert(0, os.path.dirname(__file__) + "/scripts/gdb")
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/scripts/gdb")
 
 try:
     gdb.parse_and_eval("0")
--

2.34.3

Signature · 2 lines
Thanks,
~Nick Desaulniers