python debug with gdb

本文介绍了如何使用GDB调试Python程序,特别是针对那些无法通过Python内置pdb调试的问题,例如段错误、挂起的进程及失控的守护进程等。文章提供了两种使用GDB的方法:在GDB环境下运行Python程序以及通过进程ID附加到Python进程。

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

  

  

 

 

 

 

 

 

转载于:https://www.cnblogs.com/zhao-jie/archive/2013/05/07/3064662.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值