Tuesday, January 31, 2012

Exploring the Gdb Python API with IPython

Exploring the Gdb Python API with IPython

I've found a way to explore GDB's nice Python API in a comfortable programming environment with Python name completion by using IPython.

Save the following content in $HOME/bin/gdbipy:

#!/usr/bin/gdb --python
# vim: set filetype=python:

from IPython.zmq.ipkernel import IPKernelApp

app = IPKernelApp.instance()

Running gdbipy would start an ipython "kernel" within the gdb process:

$ chmod +x ~/bin/gdbipy
$ gdbipy 
[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-23135.json

In another terminal, run:

$ ipython console --existing kernel-23135.json

In [1]: import gdb

In [2]: gdb.<TAB>
Display all 166 possibilities? (y or n)
gdb.ARCH_FRAME                   gdb.SYMBOL_LOC_REGPARM_ADDR

You can quit the ipython console and restart it at anytime without losing state.

Here's an example session:

$ ipython console --existing kernel-23735.json

In [2]: gdb.execute('file /bin/cat')

In [3]: gdb.execute('start')

In [4]: o = gdb.execute('disassemble exit', to_string=True); print o
Dump of assembler code for function __GI_exit:
   0x0000003c902399a0 <+0>: lea    0x375cc1(%rip),%rsi        # 0x3c905af668 <__exit_funcs>
   0x0000003c902399a7 <+7>: sub    $0x8,%rsp
   0x0000003c902399ab <+11>: mov    $0x1,%edx
   0x0000003c902399b0 <+16>: callq  0x3c902398a0 <__run_exit_handlers>
End of assembler dump.

In [5]: print gdb.parse_and_eval('main').type
int (int, char **)

Having an interactive Python environment with name completion makes exploring and learning the gdb.Value API quite a bit easier.

The reason we need to run "ipython console" in a separate process from GDB is so that the two don't fight over the terminal settings. You can experience what that's like by changing the content of the gdbipy script to just "import IPython; IPython.embed()" which embeds an IPython read-eval-print loop in-process. The result is partially garbled terminal output and non functional TAB completion. The two process IPython console solution presented requires IPython 0.12+ and works out of the box on Fedora 16.

See Also

Update Feb 2: changed IPython version requirement from 0.11 to 0.12. After Paul Ivanov pointed it out in the comments.


  1. minor correction: your examples require IPython 0.12 - the .json connection files did not happen until after 0.11 was released, and the same for the two-process terminal based 'console'.

    1. @Paul Ivanov, thanks for the corrections. Before writing down the IPython version requirement, I took a quick look in git to make sure the zmq.IPKernelApp snippet would work in 0.11 but missed the fact that I'm using other features introduced in 0.12.

    2. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

  2. Thanks for this article Scott.

    I've just stumbled on this API, and I can't believe I didn't know about it before. I wish the documentation out there was a bit better.

    I'm throwing together a repo for example scripts and such. I've been focused on stack exploration and data plotting (using matplotlib).

    If anyone has good scripts worth throwing in there, or a better way to do things. I'd be glad to hear about it.



  3. Scott, I couldnt get my gdb to respond to the
    #!/usr/bin/gdb --python
    line. Im using v gdb 7.4 and it doesnt understand --python?

    1. @johnwoodinfinityphase, "gdb --python" should show: "gdb: Python script file name required". If it doesn't, your gdb is compiled without Python support.

      One easy way to fix this is to clone http://sourceware.org/git/?p=gdb.git and checkout the gdb_7_5-branch and build from source while passing "--with-python" to configure. (You can see how Fedora builds its gdb here: http://pkgs.fedoraproject.org/cgit/gdb.git/tree/gdb.spec
      Search for "./configure")

      This way, you get to try the new GDB 7.5 features like "dprintf" as well (http://sourceware.org/git/?p=gdb.git;a=blob;f=gdb/NEWS;h=dba6937459e8b8e6d617acf420ca281abd450b44;hb=HEAD)

      Where did you get your existing gdb from?

  4. Keep in mind that the chances in video poker are equivalent to the customary poker game – so in case you're great at the gameFeature Articles, you should try video poker out.machine learning certification

  5. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Best Data Science training in Mumbai

    Data Science training in Mumbai

  6. I wish that I could take an idea, research it like you and put it on paper in the same fashion that I have just read. Your ideas are fantastic.
    SAP training in Kolkata
    SAP training Kolkata
    Best SAP training in Kolkata
    SAP course in Kolkata
    SAP training institute Kolkata

  7. A few years later, this approach still works but the IPython / Jupyter API has changed. To launch the kernel from the embeded Python in GDB, you simply have to do:

    from IPython import embed_kernel

    And to connect to the kernel:

    jupyter console --existing kernel-13245.json

    References: https://ipython.readthedocs.io/en/stable/interactive/reference.html#embedding-ipython

  8. Hi to everybody, here everyone is sharing such knowledge, so it’s fastidious to see this site, and I used to visit this blog daily. ExcelR Data Analyst Course

  9. Good Post! it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article, I want to say that also a well-written article with some very good information which is very useful for the readers.... thanks for sharing it and do share more posts like this.
    Data Science Training in Hyderabad
    Data Science Course in Hyderabad

  10. I want to leave a little comment to support and wish you the best of luck. We wish you the best of luck in all your blogging endeavours.
    Python Training in Hyderabad
    Python Course in Hyderabad

  11. Dubai Fun Club for luxurious Dubai Escorts and entertainment services. You can easily find the best Escorts in Dubai on our website

  12. Hard to ignore such an amazing article like this. You really amazed me with your writing talent. Thank you for sharing again.


    Feel free to visit my blog :

  13. Nice to be visiting your blog again. it has been months for me. Well this article that i’ve been waited for so long. I need this article to complete my assignment in the college. and it has same topic with your article. Thanks. great share
    Feel free to visit my blog :일본야동

  14. I’m going to bookmark your site and keep checking for new details about once per week.
    Feel free to visit my blog : 국산야동


  15. I subscribed to your Feed too.
    Feel free to visit my blog : 일본야동

  16. Hi there! This article could not be written much better!
    Feel free to visit my blog : 야설

  17. Additionally, you can make a booking anytime of the night or day we make it easy to make it easy for you to book the most desirable models for your special occasion in rudrapur. With our 24/7 operating hours, it won't matter when you decide to meet with one of our models. You'll also have an enjoyable and stimulating time thanks to our fantastic models. They won't be missed in a flash.
    what's app rudrapur girls number
    phone call lucknow girls
    hot call Mount Abu girls number.
    call dehradun girls phone number
    call haridwar girls phone number
    what's app beawar girls number