Python-Debugging

logo

 

** Steps to debug a python script

* apt-get install gdb python2.7-dbg

* We need a gdb  compiled with –with-python
wget http://ftp.gnu.org/gnu/gdb/gdb-7.6.2.tar.gz
./configure –with-python=/usr/bin
make
make install
It will install in /usr/local/bin/

* On Ubuntu we have to install python-dbg, Further for debug session we have to use python-dbg and not python

apt-get install python-dbg

* Also we need python debug build for symbols to be loaded in gdb session

So clone python and build it, we need below path for debug session

/usr/local/src/Python-2.7.3/Tools/gdb

* To check if gdb support python
gdb –batch –eval-command=”python print gdb”

** A Sample session

root@proxy1:~/sumit# /usr/local/bin/gdb –args python-dbg ~/sumit/example.py
GNU gdb (GDB) 7.6.2
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type “show copying”
and “show warranty” for details.
This GDB was configured as “x86_64-unknown-linux-gnu”.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>&#8230;
Reading symbols from /usr/bin/python2.7…done.
(gdb) run
Starting program: /usr/local/bin/python /root/sumit/example.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library “/lib/x86_64-linux-gnu/libthread_db.so.1”.
Program received signal SIGSEGV, Segmentation fault.
0x000000000046f06f in PyString_FromString (str=0xdeadbeef <Address 0xdeadbeef out of bounds>) at Objects/stringobject.c:121
121 size = strlen(str);
(gdb) py-bt
Undefined command: “py-bt”. Try “help”.
(gdb) python import sys;
(gdb) python sys.path.insert(0, “/usr/local/src/Python-2.7.3/Tools/gdb”);
(gdb) python import libpython;
(gdb) py-bt
#10 Frame 0xa932e0, for file /usr/lib/python2.7/ctypes/__init__.py, line 509, in string_at (ptr=3735928559, size=-1)
return _string_at(ptr, size)
#14 Frame 0xa8e920, for file /root/sumit/example.py, line 5, in bar (self=<Foo(someattr=42, someotherattr={‘three’: [(), (None,), (None, None)], ‘two’: 2L, ‘one’: 1}) at remote 0xa32768>, string_at=<function at remote 0xaa73a8>)
string_at(0xDEADBEEF) # this code will cause Python to segfault
#17 Frame 0xa8e710, for file /root/sumit/example.py, line 12, in main (f=<Foo(someattr=42, someotherattr={‘three’: [(), (None,), (None, None)], ‘two’: 2L, ‘one’: 1}) at remote 0xa32768>)
f.bar()
#20 Frame 0xa8e010, for file /root/sumit/example.py, line 16, in <module> ()
main()

(gdb) py-list

(gdb) thread apply all py-list

 py-locals , py-up, py-down,

** Further reading 

https://docs.python.org/devguide/gdb.html

** Steps to debug a python daemon

Example Session

* Start daemon with python-dbg

python-dbg daemon.py

root@object1:~/sumit# /usr/local/bin/gdb –args python-dbg
GNU gdb (GDB) 7.6.2
::::::::::::::::::::::::::::::::
Reading symbols from /usr/bin/python2.7-dbg…done.
(gdb) python import sys;
(gdb) python sys.path.insert(0,”/root/sumit/Python-2.7.3/Tools/gdb”);
(gdb) python import libpython;
(gdb) attach 14268
Attaching to program: /usr/bin/python-dbg, process 14268
Reading symbols from /lib/x86_64-linux-gnu/libpthread.so.0…(no debugging symbols found)…done.
[Thread debugging using libthread_db enabled]
Using host libthread_db library “/lib/x86_64-linux-gnu/libthread_db.so.1”.
Loaded symbols for /lib/x86_64-linux-gnu/libpthread.so.0
Reading symbols from /lib/x86_64-linux-gnu/libdl.so.2…(no debugging symbols found)…done.
Loaded symbols for /lib/x86_64-linux-gnu/libdl.so.2
Reading symbols from /lib/x86_64-linux-gnu/libutil.so.1…(no debugging symbols found)…done.
Loaded symbols for /lib/x86_64-linux-gnu/libutil.so.1
Reading symbols from /lib/x86_64-linux-gnu/libssl.so.1.0.0…(no debugging symbols found)…done.
Loaded symbols for /lib/x86_64-linux-gnu/libssl.so.1.0.0
Reading symbols from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0…(no debugging symbols found)…done.
Loaded symbols for /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
Reading symbols from /lib/x86_64-linux-gnu/libz.so.1…(no debugging symbols found)…done.
Loaded symbols for /lib/x86_64-linux-gnu/libz.so.1
Reading symbols from /lib/x86_64-linux-gnu/libm.so.6…(no debugging symbols found)…done.
Loaded symbols for /lib/x86_64-linux-gnu/libm.so.6
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6…(no debugging symbols found)…done.
Loaded symbols for /lib/x86_64-linux-gnu/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2…(no debugging symbols found)…done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007fdcf1213743 in select () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) py-bt
#5 Frame 0xd88480, for file /root/sumit/temp.py, line 4, in next (i=1)
time.sleep(10)
#8 Frame 0xd85d70, for file /root/sumit/temp.py, line 9, in <module> ()
next(i)

(gdb) py-list
1 import time
2
3 def next(i):
>4 time.sleep(10)
5 i = 1 – i
6
7 i = 1
8 while True:
9 next(i)

(gdb) finish
Run till exit from #0 0x00007f9849a6a743 in select () from /lib/x86_64-linux-gnu/libc.so.6
0x000000000058d1f3 in floatsleep (secs=10) at ../Modules/timemodule.c:943
943 ../Modules/timemodule.c: No such file or directory.
(gdb) next
954 in ../Modules/timemodule.c
(gdb) cd Python-2.7.3/Python/
Working directory /root/sumit/Python-2.7.3/Python.
(gdb) next
1051 return 0;
(gdb) list
1046 Py_BEGIN_ALLOW_THREADS
1047 sleep((int)secs);
1048 Py_END_ALLOW_THREADS
1049 #endif
1050
1051 return 0;
1052 }
1053

*** Aattch gdb to openstack swift process

swift-init stop all

apt-get install python-dbg

apt-get install python-greenlet-dbg

apt-get install python-netifaces-dbg

ln -s /usr/bin/python2.7-dbg /usr/bin/python

swift-init start main

and then attach gdb as above to the swift process

Advertisements
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s