服务器上调试程序 pdb命令调试

以前写python一直用pycharm,调试啥的比较方便,最近要在远程服务器上调试一些程序,只有一个控制台就可以用pdb进行调试了。常用的只有几个命令。


  • break 或 b           设置断点
  • continue 或 c           继续执行程序
  • list 或 l                   查看当前行的代码段
  • step 或 s                   进入函数
  • return 或 r        执行代码直到从当前函数返回
  • exit 或 q                  中止并退出
  • next 或 n              执行下一行
  • pp                        打印变量的值
  • help                          帮助

pdb 是 python 自带的一个包,为 python 程序提供了一种交互的源代码调试功能,主要特性包括设置断点、单步调试、进入函数调试、查看当前代码、查看栈片段、动态改变变量的值等。

使用的时候要import pdb再用pdb.set_trace()设置一个断点,运行程序的时候就会停在这。

调试步骤:

1、在调试的地方设置断点

import pdb


pdb.set_trace() #  设置断点

2、(Pdb) n 进入下一步
3、(Pdb) list 打印代码
4、(Pdb) p b 打印变量
5、(Pdb) s 进入函数
6、(Pdb) !b=“afdfd” !动态改变变量值
7 、(Pdb) q 退出

常用命令:

  • l : 查看运行的代码段。
(Pdb) l
125                 if self.is_cuda:
126                     outputs.append(Variable(data.cuda()))
127                 else:
128                     outputs.append(Variable(data))
129             pdb.set_trace()
130  ->         return tuple(outputs)
131  
132  
133     class CaffeMultiBoxLoss(nn.Module):
134         def __init__(self, layer, *input_shapes):
135             super(CaffeMultiBoxLoss, self).__init__()
(Pdb) c
forward_backward time: 1948.931108 1948.931108
> /data/zyj/caffe2pytorch/caffe_layers.py(119)forward()
-> data = self.net.blobs[name].data

l + 起始行号,就可以查看行号的上下五行。

l + 起始行号 结束行号,可以查看这么一段。

按完l继续按回车键可以继续往下查看。

  • n :运行到下一条语句。
(Pdb) n
> /data/zyj/caffe2pytorch/caffe_layers.py(120)forward()
-> if self.data_save_debug:
  • s : 跳转到函数内部。
  • pp + 变量 : 打印变量的值
  • c : 运行到下个断点
  • r : 运行到函数退出。
(Pdb) r
--Return--
>n^H /data/zyj/caffe2pytorch/caffe_layers.py(130)forward()->(Variable...(GPU 0)]
, Variable...(GPU 0)]
)
-> return tuple(outputs)
  • help + [命令]: 帮助。
(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) help n
n(ext)
Continue execution until the next line in the current function
is reached or it returns.
  • b + 行号:设置断点。
(Pdb) b 125
Breakpoint 2 at /data/zyj/caffe2pytorch/train_wider.py:125

就一个b就是是打印断点在哪。

(Pdb) b 
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /data/zyj/caffe2pytorch/train_wider.py:121
2   breakpoint   keep yes   at /data/zyj/caffe2pytorch/train_wider.py:125
  • q : 退出。
  • j + 行号 :执行到行号
(Pdb) j 119
> /data/zyj/caffe2pytorch/train_wider.py(119)<module>()
-> for test_device_id in test_device_ids:
  • w : 打印函数栈
(Pdb) w
  /data/zyj/caffe2pytorch/train_wider.py(112)<module>()
-> net.cuda()
> /home/zyj/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.py(216)cuda()
-> return self._apply(lambda t: t.cuda(device))
  • cl + 断点号: 清除断点
(Pdb) b 
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /data/zyj/caffe2pytorch/train_wider.py:121
2   breakpoint   keep yes   at /data/zyj/caffe2pytorch/train_wider.py:125
(Pdb) cl 1
Deleted breakpoint 1
(Pdb) b
Num Type         Disp Enb   Where
2   breakpoint   keep yes   at /data/zyj/caffe2pytorch/train_wider.py:125

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章