Author Topic: Debugging C++ called from Python  (Read 10499 times)

Offline akats

  • Single posting newcomer
  • *
  • Posts: 3
Debugging C++ called from Python
« on: August 25, 2010, 10:19:36 pm »
Hi,

I have a C++ library, and a SWIG interface that allows me to call functions from the library from Python.  I want to debug this library from C::B, but I can't find a way.

I have managed to find a way to call GDB from the terminal and set a breakpoint.  I follow these steps:

1.  Start python, and in the Python interpreter, ">>>import segmentation"
2.  gdb;  "attach ####" //Python's PID
3.  (gdb) break seg.cpp:44 //entry point for the library
4.  (gdb) continue
5.  At this point, the Python interpreter can respond to input again
6.  >>> segmentation.run() #launch the program

This is a bit tricky, and order of steps matters, but it works -- the breakpoint is triggered, and now I can gdb away at the terminal.  But, I can't do that with C::B.  I start the python process, and then from C::B I do Debug->Attach to process to mimic step 2.  At this point the Python interpreter crashes, and C::B goes into this funny mode where I cannot detach or stop the debugger.  I am using C::B 10.05 installed from the repository on Ubuntu 10.04.  I am pretty sure some of these are C::B bugs, but maybe I am also doing something wrong.  I am a n00b to C::B and gdb, so I don't want to submit a bug report just yet.  Could someone help me sort out what is going on here?

Thanks!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Debugging C++ called from Python
« Reply #1 on: August 26, 2010, 12:08:40 am »
Have you tried to specify the host application for your extension?

Project -> Set program's arguments -> Host application -> set it to /usr/bin/python (or the real path to the python's executable)

Also you could specify some arguments that will be passed to python. ( python test.py worked from the command line, so you could that from C::B, too)

Unfortunately 10.05 is missing the "Run host in terminal" checkbox, present only in the debugger's branch.
Without this checkbox you won't have a terminal and you won't be able to pass any input to python.

Hope it works if not do not hesitate to post again :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline akats

  • Single posting newcomer
  • *
  • Posts: 3
Re: Debugging C++ called from Python
« Reply #2 on: August 26, 2010, 08:26:04 pm »
Thanks!  That did not get me all the way there, but it was a start.

I set the host application like you suggested, to /usr/bin/python /my/path/start_debug.py .  Then I would debug it, with the breakpoint set.  When the debugger loaded, if I press "continue" or "next line", it exits nearly immediately with code 0.  Instead, I have to:

1.  Start debugger
2.  In the user commands, 'file /usr/bin/python'
3.  In the user commands, ''set args /my/path/start_debug.py' .  By the way, the paste function does not work in the user commands, so I have to type this in
4.  In the user commands, 'run'

Then it stops at the breakpoint. 

Is this a bug, that I have to go through all that?  If so, I'd like to report it, but I don't know nearly enough about C::B (or gdb, for that matter) to determine whether it's them, or me.  According to the spec, what does the setting of the host translate to in GDB commands?  I'll try to replicate it on naked GDB then, and see if it fails there too before submitting a bug.

Thanks!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Debugging C++ called from Python
« Reply #3 on: August 26, 2010, 09:04:00 pm »
I set the host application like you suggested, to /usr/bin/python /my/path/start_debug.py . 
No, I suggested to set the host app to /usr/bin/python
and then you should have used the box labeled "Program arguments:" to specify the args. :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline akats

  • Single posting newcomer
  • *
  • Posts: 3
Re: Debugging C++ called from Python
« Reply #4 on: August 26, 2010, 09:31:01 pm »
oooo!  lol, thanks again :D