In this article I’ll try to give you an overview of the Python API accessible from GDB, and to do that we will create a new command that works approximatively as the DPS’ WinDBG Command.

Here is the introduction:

Since the version 7 of the Gnu Debugger, I’m pretty sure you already know that, but the python interpreter is accessible from GDB. The person behind this work is Tom Tromey: that guy made python available inside GDB, thanks! If you are interested by the implementation of the API, you should check out the sources of GDB, and take a look a the gdb/python directory (also consultable online here).

There are a lot of nice functions you can use to extend your debugger, they are all documented here: GDB Python-API. Indeed, with this API you will be able to do things like:

  • Define new (prefixed) commands
  • Create pretty-printing modules
  • Manipulate breakpoints
  • Access the stack frames
  • Read/Write/Search directly in the process’ memory
  • A lot more!

The article is available there: Hi GDB, this is Python, and the python script on my github account just right here. I hope you guys will enjoy it!

5 commentaires pour le moment

Ajoutez votre commentaire
  1. Thanks 0vercl0k..:-)
    I am very new to this world but really your doc ‘Hi GDB, this is pyhon’ helped me a lot. its awesome.. thanks again..!!

  2. Thank you for the kind words man!

  3. Hi 0vercl0k,
    is there anyway to print all the global variables(with name & corresponding values) in a human readable form of any C code.?
    Thanks in advance.

  4. Hi 0vercl0k,
    Can you tell me a way to write a pretty printer to traverse the kernel linked list (e.g. list_head) which is embedded into a structure. I mean while traversing the main structure, how would the script know there is a list and print the whole list? for example:
    using gdb-python script, i am trying to print data structure which includes kernel data structures and lists(e.g. struct list_head), the structure is

    struct my_struct {
    struct my_hardware_context ahw;
    struct net_device *netdev;
    struct pci_dev *pdev;
    struct list_head mac_list;
    struct list_head wait_list;
    So while iterating this struct my_struct, how to identify there is a linked list inside this structure as There is no any TYPE_CODE_ constant for Linked list in gdb manual and if identified, after identification how to print the dereferenced struct while iterating the list.

  5. Hello,

    Hum, I haven’t been playing with GDB Python since that article (1 year old) so I don’t really remember much about it. Though, if you didn’t find a way to write like a « generic » pretty printer, why didn’t you write a pretty printer for each structures you’re interesting on ? I must admit it’s less cool, but it should do the job.

    You should have a look at the PEDA Project, the guy is doing a lot of GDB Python, so he may be able to help you. The github repository of the project is here: https://github.com/longld/peda.

    Have fun :).

Get Adobe Flash player