python的调试

1 python的logging模块

具备log4的基本功能,可以设置流类型,文本格式,等级。

2 python的traceback模块

一般catch住异常,打印堆栈

    threads = {}
    for thread in threading.enumerate():
        threads[thread.ident] = thread.name
    if not threads:
        return
    info = []
    for thread_id, stack in sys._current_frames().items():
        thread_name = threads.get(thread_id, "")
        info.append("# Thread: %s(%d)" % (thread_name, thread_id))
        if "MainThread" == thread_name:
            continue
        for filename, lineno, name, line in traceback.extract_stack(stack):
            info.append('File: "%s", line %d, in %s' % (filename, lineno, name))
            if line:
                info.append("  %s" % (line.strip()))
    return "\n".join(info)

3 gdb

官方文档:http://wiki.python.org/moin/DebuggingWithGdb

使用gnu的gdb来调试,gdb是需要symbols的,先下载python的symbols

apt-get install python2.7-dbg

与调试c/c++程序一样,可以直接在gdb中run a new process或者attach a running process

gdb python
(gdb) run <programname>.py <arguments>
gdb python <pid of running process>
然后试试gdb的一些函数

查看线程信息

info thread

选择线程

thread 1

查看堆栈

bt

bt显示的堆栈是c语言的,如果需要查看python语言的堆栈

从下面的链接拷贝代码,创建文件".gdbinit"并保存,如果已经有了.gdbinit的,可以添加内容。

http://svn.python.org/view/*checkout*/python/branches/release27-maint/Misc/gdbinit

在gdb的敲命令运行这个shell脚本,source .gdbinit

就可以调用pystack等函数来查看python语言的堆栈了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值