2

I'm trying to remotely-debug a Linux's kernel. I've created a VM (using VMware) and connected to it from my PC using gdb, and everything works fine.

The problem is that gdb fails to load vmlinux-gdb.py script. I've tried to add it using the source command on gdb, and got the following error:

Traceback (most recent call last):
  File "~/workspace/kernels/default-kernel/scripts/gdb/vmlinux-gdb.py", line 28, in <module>
ImportError: No module named 'linux'

The directory tree:

drwxr-xr-x  2 iofek iofek 4096 Mar 22 19:59 linux
-rwxr-xr-x  1 iofek iofek  577 Mar 22 19:43 Makefile
-rwxrwxr-x  1 iofek iofek    0 Mar 22 19:43 modules.order
-rwxr-xr-x  1 iofek iofek  759 Mar 22 20:00 vmlinux-gdb.py

Now I can't understand why the module fails to find the linux directory. I've updated the PYTHONPATH, as well as added the path using sys.path.append. Additionaly, all files under linux has the right permissions.

Any ideas?

2
  • What's the output of the gdb command python os.system("ls " + sys.path[-1] + "/linux/__init__.py"), where -1 is the appropriate subscript to get the path you appended to sys.path ? Commented Mar 22, 2020 at 18:29
  • It prints the whole path since it exists.... Commented Mar 22, 2020 at 18:34

3 Answers 3

4

Short answer

Never use ...linux.../scripts/gdb/vmlinux-gdb.py.Use the file vmlinux-gdb.py that is in the root directory of your kernel build output, alongside your vmlinux file.

If this file does not exist, you need to:

Activate CONFIG_GDB_SCRIPTS in your kernel configuration


Long tutorial

First make sure the gdb-scripts will be created during the kernel build:

make menuconfig
Enable CONFIG_GDB_SCRIPTS
make

Then find out if your kernel build is using a separate build output folder and then follow ONE (xor) of the following chapters:

Either: Building in-place without a build binary dir

If you compile your kernel with .o and .ko files cluttered inside the source (which is e.g. the way Ubuntu recommends it on wiki.ubuntu.com) you can cd into the source root folder, let's for example assume you built in the folder ~/gdbenv, start gdb from there and then loading should be possible out-of-the-box:

cd ~/gdbenv
gdb ./vmlinux
(gdb) add-auto-load-safe-path ~/gdbenv
(gdb) source ~/gdbenv/vmlinux-gdb.py

Or: When your way of building a kernel outputs binaries in a separate build dir

Which is done e.g. in a Yocto build, where all binaries end up in a different folder, not mixed with the source folder. In such environments you need to grab everything together in one environment (vmlinux, gdb-scripts and optionally the kernel sources).

tar -xf ~/Downloads/linux-blabla.tgz -C ~/gdbenv       (optional)
cp .../build/vmlinux-gdb.py ~/gdbenv
mkdir ~/gdbenv/scripts
cp -r .../build/scripts/gdb ~/gdbenv/scripts
cp .../build/vmlinux ~/gdbenv

Then procede like in the preceeding chapter (cd ~/gdbenv, gdb ./vmlinux ...)
Sign up to request clarification or add additional context in comments.

Comments

3

For latest kernels, you need build gdb scripts:

<root of kernel source>: make scripts_gdb

After making, a symlink of vmlinux-gdb.py will be created at the root of kernel source. Then:

<root of kernel source>: gdb vmlinux

<gdb cmd>: add-auto-load-safe-path root-of-kernel-source

<gdb cmd:> source vmlinux-gdb.py

Comments

0

I ran into this today, the other solutions weren't working for me. What I found was, if you look at vmlinux-gdb.py it has this line:

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/scripts/gdb")

This takes the path of the file you're loading, then adds scripts/gdb to it to the path. But we're already inside scripts/gdb.

So I looked at the parent linux source dir, and there is a symlink of vmlinux-gdb.py to scripts/gdb/vmlinux-gdb.py. We need to load the symlink, so this works.

(gdb) source ~/src/linux/upstream/vmlinux-gdb.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.