python pdb调试方法分享


Posted in Python onJanuary 21, 2014
import pdbdef pdb_test(arg):
    for i in range(arg):
        print(i)
    return arg
pdb.run("pdb_test(3)")
 

 b 函数名、行号:

打断点,b可以查询所有的断点。

(Pdb) b pdb_test
Breakpoint 1 at c:\users\plpcc\desktop\pdbtest.py:3
(Pdb) b
Num Type         Disp Enb   Where
   breakpoint   keep yes   at c:\users\plpcc\desktop\pdbtest.py:3
 

  c:

运行程序,直到遇到断点。

(Pdb) c
> c:\users\plpcc\desktop\pdbtest.py(4)pdb_test()
-> for i in range(arg):

   l:

     查看断点周围的代码

(Pdb) l
    import pdbB   def pdb_test(arg):
  ->      for i in range(arg):
             print(i)
       return arg
     pdb.run("pdb_test(3)")

 a:

    查看参数

(Pdb) a
arg = 3
 s, n:

    单步运行,区别s会进入路径中的函数,n不会进入

 p:

    查看表达式的值

(Pdb) p i
 condition:
 

条件断点,只有条件为true断点才命中

> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)
(Pdb) l
    import pdb    def pdb_test(arg):
        for i in range(arg):
B->          print(i)
        return arg
    pdb.run("pdb_test(3)")
[EOF]
(Pdb) b
Num Type         Disp Enb   Where
breakpoint   keep yes   at c:\users\plpcc\desktop\pdbtest.py:5
(Pdb) condition 2 i==1   //i==1时才触发断点2
New condition set for breakpoint 2.
(Pdb) b
Num Type         Disp Enb   Where
breakpoint   keep yes   at c:\users\plpcc\desktop\pdbtest.py:5
 stop only if i==1
(Pdb) c
                    //i==0直接打印未断住
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)             //触发断点,i==1
(Pdb) p i

bt:

查看调用堆栈

(Pdb) bt
c:\python33\lib\bdb.py(405)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)
r:

   执行到函数返回
(Pdb) r
--Return--
> c:\users\plpcc\desktop\pdbtest.py(6)pdb_test()->3 //代码位置、函数返回值->3
-> return arg                                       //代码位置的语句
(Pdb) l
    import pdb
    def pdb_test(arg):
        for i in range(arg):
            print(i)
 ->      return arg
    pdb.run("pdb_test(3)")

通过pdb.set_trace() 在代码中指定位置嵌入一个断点,通常可以通过调试开关来控制

import pdb
__DEBUG__ = True
def pdb_test(arg):
 if True == __DEBUG__:
     pdb.set_trace()
 for i in range(arg):
     print(i)
 return arg
pdb_test(3)

运行后在pdb.set_trace()位置被断住,当__DEBUG__ = False,代码正常运行

> c:\users\plpcc\desktop\pdbtest.py(8)pdb_test()
-> for i in range(arg):
(Pdb) l
    __DEBUG__ = True    def pdb_test(arg):
        if True == __DEBUG__:
            pdb.set_trace()
 ->      for i in range(arg):
            print(i)
       return arg
   pdb_test(3)
[EOF]

通过pdb.pm()进行事后调试,可以跟踪异常程序最后的堆载信息:

Traceback (most recent call last):
File "C:\Users\plpcc\Desktop\pdbTest.py", line 13, in <module>
 pdb_test(3)
File "C:\Users\plpcc\Desktop\pdbTest.py", line 10, in pdb_test
 1/0
ZeroDivisionError: division by zero
>>> import pdb
>>> pdb.pm()
> c:\users\plpcc\desktop\pdbtest.py(10)pdb_test()
-> 1/0
(Pdb) l
    def pdb_test(arg):
        if True == __DEBUG__:
            pdb.set_trace()
        for i in range(arg):
            print(i)
->          1/0
       return arg   pdb_test(3)
Python 相关文章推荐
python发布模块的步骤分享
Feb 21 Python
11个并不被常用但对开发非常有帮助的Python库
Mar 31 Python
使用Python发送邮件附件以定时备份MySQL的教程
Apr 25 Python
python从入门到精通(DAY 2)
Dec 20 Python
利用pyinstaller将py文件打包为exe的方法
May 14 Python
TensorFlow实现卷积神经网络
May 24 Python
Python数据分析matplotlib设置多个子图的间距方法
Aug 03 Python
python模拟鼠标点击和键盘输入的操作
Aug 04 Python
python区分不同数据类型的方法
Oct 14 Python
pytorch 数据处理:定义自己的数据集合实例
Dec 31 Python
python实现实时视频流播放代码实例
Jan 11 Python
PyQt5实现仿QQ贴边隐藏功能的实例代码
May 24 Python
python发送邮件接收邮件示例分享
Jan 21 #Python
python逐行读取文件内容的三种方法
Jan 20 #Python
c++生成dll使用python调用dll的方法
Jan 20 #Python
python中getattr函数使用方法 getattr实现工厂模式
Jan 20 #Python
python字符串加密解密的三种方法分享(base64 win32com)
Jan 19 #Python
python实现人人网登录示例分享
Jan 19 #Python
使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例
Jan 19 #Python
You might like
PHP中显示格式化的用户输入
2006/10/09 PHP
php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释
2010/03/23 PHP
php用数组返回无限分类的列表数据的代码
2010/08/08 PHP
php使用PDO方法详解
2014/12/27 PHP
JavaScript多线程的实现方法
2007/05/08 Javascript
JS event使用方法详解
2008/04/28 Javascript
javascript生成随机数的方法
2014/05/16 Javascript
input点击后placeholder中的提示消息消失
2016/01/15 Javascript
jQuery 跨域访问解决原理案例详解
2016/07/09 Javascript
通过原生JS实现为元素添加事件的方法
2016/11/23 Javascript
原生JS实现图片轮播效果
2016/12/26 Javascript
webpack学习教程之前端性能优化总结
2017/12/05 Javascript
在vue中动态修改css其中一个属性值操作
2020/12/07 Vue.js
[49:08]OpTic vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
python中lambda()的用法
2017/11/16 Python
为什么入门大数据选择Python而不是Java?
2018/03/07 Python
利用python对Excel中的特定数据提取并写入新表的方法
2018/06/14 Python
python3 property装饰器实现原理与用法示例
2019/05/15 Python
Python之修改图片像素值的方法
2019/07/03 Python
Ranorex通过Python将报告发送到邮箱的方法
2020/01/12 Python
Python基础进阶之海量表情包多线程爬虫功能的实现
2020/12/17 Python
英国自行车商店:AW Cycles
2021/02/24 全球购物
美国家居装饰购物网站:Amanda Lindroth
2020/03/25 全球购物
澳大利亚在线床零售商:Bedworks
2020/09/01 全球购物
大学生农村教师实习自我鉴定
2013/09/21 职场文书
工程地质勘察专业大学生求职信
2013/10/13 职场文书
外贸业务员求职信范文
2013/12/12 职场文书
食品行业求职人的自我评价
2014/01/19 职场文书
教研活动总结
2014/04/28 职场文书
好学生评语大全
2014/05/05 职场文书
在职员工证明书
2014/09/19 职场文书
2014年防汛工作总结
2014/12/08 职场文书
团代会开幕词
2015/01/28 职场文书
2016庆祝教师节新闻稿
2015/11/25 职场文书
《倍数和因数》教学反思
2016/02/23 职场文书
关于Python OS模块常用文件/目录函数详解
2021/07/01 Python