python debug with gdb

1,一般的情况下可以用python自带的debug包pdb来debug一个简单的routine。

  用法如下:

    python -m pdb /path/script_name.py

[root@cdntest-2 ~]# python -m pdb gdb.py
> /root/gdb.py(1)<module>()
-> import sys
(Pdb) help

Documented commands (type help <topic>):
========================================
EOF bt cont enable jump pp run unt
a c continue exit l q s until
alias cl d h list quit step up
args clear debug help n r tbreak w
b commands disable ignore next restart u whatis
break condition down j p return unalias where

Miscellaneous help topics:
==========================
exec pdb

Undocumented commands:
======================
retval rv

(Pdb)

  但是一些bug很难用这种python自带的debug模块来进行调试,比如如下的几种:

  1,段错误(不是没有捕获的python Exception)

  2,挂起的进程(这种情况是不可能是debug with python -m pdb的)

  3,失控的守护进程(coredump)

  碰到这些类似的情况,那么我们就需要更强大的debug工具GDB了。

2,gdb python

  有两种方法来attach gdb 到 python 进程

  1,在gdb的环境下运行python程序,等到问题发生 

[root@cdntest-2 jenkins]# gdb python
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-23.el5)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /usr/local/bin/python...done.
(gdb) run script_name.py [argv]

这会运行这个程序直到它结束,或者碰到段错误或者终止这个程序(ctrl+c)

 

  2,attach 住python的进程号

  gdb python [pid of python process]

  attach 一个进程就像让这个进程停止一样,你可以让它继续运行。

 

部分内容参考自python官网:http://wiki.python.org/moin/DebuggingWithGdb

  

  

 

 

 

 

 

 

posted @ 2013-05-07 14:11  jiezhao  阅读(684)  评论(0编辑  收藏  举报