我对python和pdb比较陌生,但我对gdb有很多经验。
我的问题是,如果我在某些时候在我的代码中设置了一些断点,我会想要改变一些东西并重新运行我的调试会话,保留这些断点。但是,在我的pdb会话中输入“run”会使我的会话终止,并使用以下输出
(Pdb) run
Traceback (most recent call last):
File "/usr/lib64/python2.6/runpy.py", line 122, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib64/python2.6/runpy.py", line 34, in _run_code
exec code in run_globals
File "/usr/lib64/python2.6/pdb.py", line 1319, in <module>
pdb.main()
File "/usr/lib64/python2.6/pdb.py", line 1312, in main
pdb.interaction(None, t)
File "/usr/lib64/python2.6/pdb.py", line 198, in interaction
self.cmdloop()
File "/usr/lib64/python2.6/cmd.py", line 142, in cmdloop
stop = self.onecmd(line)
File "/usr/lib64/python2.6/pdb.py", line 267, in onecmd
return cmd.Cmd.onecmd(self, line)
File "/usr/lib64/python2.6/cmd.py", line 219, in onecmd
return func(arg)
File "/usr/lib64/python2.6/pdb.py", line 661, in do_run
raise Restart
pdb.Restart
]$
我在两个独立的Linux平台上尝试了这个并且得到了相同的结果,但我在文档中找不到任何更正。
答案 0 :(得分:9)
因此,如果有人关心问题是pdb中的“run”与gdb中的“run”不完全相同。在gdb中,如果我输入run,程序将重新启动并继续到第一个断点。在pdb中,程序重新启动并转到文件的开头。然后我必须按'n'然后按'c'才能进入第一个断点。如果,试图让程序运行,我会在调试器崩溃的文件开头第二次输入,如上所示。
答案 1 :(得分:1)
遇到异常情况(即程序中的错误),pdb更有帮助地说:
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
在这里,输入“run”或“restart”会导致pdb进程毫不客气地终止为OP状态(但“cont”或“step”将按照承诺重新启动)。
据推测,许多不耐烦的pythonistas宁愿“运行”或“重启”工作,无论我们是否已进入“事后调试”,但我确信现有的重启语义是更清洁实现的副产品。
答案 2 :(得分:1)
这是我的文件 test.py :
import pdb
pdb.set_trace()
print('1 line')
当我使用命令&#39; python3 test.py&#39;时出现同样的错误:
> /home/wangpq/program_note/test.py(3)<module>()
-> print('1 line')
(Pdb) run
Traceback (most recent call last):
File "program_note/test.py", line 3, in <module>
print('1 line')
File "program_note/test.py", line 3, in <module>
print('1 line')
File "/usr/lib/python3.5/bdb.py", line 48, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib/python3.5/bdb.py", line 66, in dispatch_line
self.user_line(frame)
File "/usr/lib/python3.5/pdb.py", line 259, in user_line
self.interaction(frame, None)
File "/usr/lib/python3.5/pdb.py", line 346, in interaction
self._cmdloop()
File "/usr/lib/python3.5/pdb.py", line 319, in _cmdloop
self.cmdloop()
File "/usr/lib/python3.5/cmd.py", line 138, in cmdloop
stop = self.onecmd(line)
File "/usr/lib/python3.5/pdb.py", line 412, in onecmd
return cmd.Cmd.onecmd(self, line)
File "/usr/lib/python3.5/cmd.py", line 217, in onecmd
return func(arg)
File "/usr/lib/python3.5/pdb.py", line 1022, in do_run
raise Restart
pdb.Restart
然后我使用命令&#39; python3 -m pdb test.py&#39;
wangpq@wangpq:~$ python3 -m pdb program_note/test.py
> /home/wangpq/program_note/test.py(1)<module>()
-> import pdb
(Pdb) restart
Restarting program_note/test.py with arguments:
program_note/test.py
> /home/wangpq/program_note/test.py(1)<module>()
-> import pdb
(Pdb)
有效。到目前为止,我不确定为什么会这样。