0

I am trying to write a Python script for GDB that uses the events gdb.events.inferior_call_pre and events.inferior_call_post. However, those events are not defined in gdb.events:

>(gdb) python print(dir(gdb.events))
['__doc__', '__name__', '__package__', 'cont', 'exited', 'new_objfile', 'stop']

The above was done without a program loaded. I have also loaded a C program, run it until a breakpoint, and then executed the command with the same result.

I am running CentOS 7 with gdb 7.6.1.

I downloaded and compiled the source (with --with-python=yes) for gdb 7.12 with less success:

(gdb) python print(dir(gdb.events))
Traceback (most recent call last):
   File "<string>", line 1, in <module>
NameError: name 'gdb' is not defined
Error while executing Python code.

Any help is greatly appreciated.

0

1 Answer 1

1

According to gdb's NEWS file, support for the events gdb.events.inferior_call_pre and gdb.events.inferior_call_post was added in gdb 7.9.

If you're compiling gdb from source, the gdb python module can be found in the directory gdb-7.12.1/gdb/python/lib/gdb, which gets copied to gdb-7.12.1/gdb/data-directory/python/gdb.

When you do make install, it gets copied to <prefix directory>/share/gdb/python/gdb.

If the latter directory can't be found at runtime - for instance, if you run gdb from the source directory without having done make install - gdb ought to complain at startup:

$ ./gdb
Python Exception <type 'exceptions.ImportError'> No module named gdb: 
./gdb: warning: 
Could not load the Python gdb module from `<prefix directory>/share/gdb/python'.
Limited Python support is available from the _gdb module.
Suggest passing --data-directory=/path/to/gdb/data-directory.

The data directory also contains files with lists of available system calls for various target architectures. It's worth making sure that gdb has access to it.

If you can't run make install but have to run gdb directly from the source directory, cd to gdb-7.12.1/gdb and run ./gdb --data-directory=./data-directory .

Sign up to request clarification or add additional context in comments.

2 Comments

That did it! Thanks! I want to add that when specifying --data-directory, gdb looks in a subdirectory called python, so specifying --data-directory=./python/lib/gdb won't work. To get it to work in my build directory, I created a symlink. So I ran: ln -s ./python/lib ./python/python then ./gdb --data-directory=./python
If you can't run make install before running gdb from the source directory, you can run ./gdb --data-directory=./data-directory . This will give gdb access to some needed python classes as well as the architecture's system call list (for the catch syscall command). I'll add that to the answer.

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.