以前寫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