Korcan Karaokçu
Korcan Karaokçu

Reputation: 547

Is there a better way to control gdb other than using command-line tools/libraries such as pexpect in python?

I'm trying to develop a program that uses gdb for it's basic debugging purposes. It executes the gdb from the command line, attaches to the target process and gives some specific commands, then reads the std output. Everything seemed good on paper at first so I started out with python and pexpect. But recently, while thinking about future implementations, I've encountered a problem. Since I can only execute one command at a time from the command-line(there can be only one gdb instance per process), the threads that request data constantly to refresh some UI element will lead to chaos eventually. Think about it:

1-)GDB stops the program to execute commands
2-)blocks the other threads while executing the code
3-)GDB continues the program after execution finishes
4-)One of the waiting threads will try to use GDB immediately
5-)go to 1 and repeat

The process we'll work on will freeze every 0.5 sec, this would be unbearable.

So, the thing I want to achieve is multi-threading while executing the commands. How can I do it? I thought about using gdb libraries but since I use python and those codes are written in C, it left a question mark on my head about compatibility.

Upvotes: 0

Views: 245

Answers (2)

Tom Tromey
Tom Tromey

Reputation: 22519

There are two main ways to script gdb.

One way is to use the gdb MI ("Machine Interface") protocol. This is a specialized input and output mode that gdb has that is intended for programmatic use. It has some warts but is "usable enough" - it is what most of the gdb GUIs use.

The other way to do this is to write Python scripts that run inside gdb, using gdb's Python API. This approach is often simpler to program, but on the downside the Python API is missing some useful pieces, so sometimes this can't be done, depending on exactly what you're trying to accomplish.

Upvotes: 1

Korcan Karaokçu
Korcan Karaokçu

Reputation: 547

I found a library called python-ptrace, it seems working for now(with a few problems I never faced while using gdb).

Upvotes: 0

Related Questions