How to debug python script in C level using GDB. Give me a simple example for this.My primary goal is to get the trace of libc function called from my python script.
Asked
Active
Viewed 3,031 times
7
-
1Please say you're using `ctypes` or trying to do a low-level hack... And that you've got the debug version of Python. – wizzwizz4 Jul 26 '18 at 11:43
-
Yes, I am using python-devel version. Also I want to do a low-level hack – ankit jain Jul 26 '18 at 11:48
-
Oh, good. You get an up vote then. Any particular reason you've picked Python 2 for your hack? – wizzwizz4 Jul 26 '18 at 11:50
-
I need to go for both python 2 and 3.. starting from python 2 for now – ankit jain Jul 26 '18 at 11:57
-
You can follow instructions on https://wiki.python.org/moin/DebuggingWithGdb to debug python script. To get the trace of libc function you can set a breakpoint on it with `b
`. – ks1322 Jul 27 '18 at 14:35
2 Answers
4
See the thing is for binary(.so) files, you surely cannot apply breakpoint to a particular line, but surely you can apply breakpoint to a function. Say using
(gdb) b func_name
you can easily apply breakpoint to a function and thereby obtain the backtrace whenever the breakpoint is found. First run your python sc
Start gdb:
gdb -ex r --args python demo.py
Apply breakpoint:
(gdb) b func_name
Run:
(gdb) run
This should work for your case.

laike9m
- 18,344
- 20
- 107
- 140

sumit_suthar
- 412
- 3
- 15
-
So where would this look for function "func_name". I need to know this. This would play a crucial role in my case. – ankit jain Jul 27 '18 at 11:44
1
You can always Python using gdb and set breakpoints as you like
gdb -ex r --args python script.py args
If you want to look what happens in Python while running Python script I suggest to use mixed mode
gdb -ex r --args python -m pdb script.py
This way, you can break in pdb, then press Ctrl-C
and end up inside gdb. Then, bt
will give you stack trace inside Python.

Oo.oO
- 12,464
- 3
- 23
- 45
-
So where to add breakpoints in such a case? say for a c file abc.c, I add "b abc.c:10"... but for the python file, I cannot add the breakpoint directly to python as far as my understanding goes. The c files used here would be the binary(.so) files... how to add breakpoint to them now? – ankit jain Jul 27 '18 at 11:04
-