Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug python script in C level using GDB. Give me a simple example for this

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.

like image 279
ankit jain Avatar asked Jul 26 '18 11:07

ankit jain


2 Answers

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

  1. Start gdb:

    gdb -ex r --args python demo.py
    
  2. Apply breakpoint:

    (gdb) b func_name
    
  3. Run:

    (gdb) run
    

This should work for your case.

like image 55
sumit_suthar Avatar answered Oct 16 '22 18:10

sumit_suthar


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.

like image 33
Oo.oO Avatar answered Oct 16 '22 20:10

Oo.oO