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语言的堆栈了。