13

我对 python 和 pdb 比较陌生,但是我对 gdb 有很多经验。

我的问题是,如果我在某个时间点在我的代码中设置了多个断点,我将想要更改某些内容并重新运行我的调试会话并保留这些断点。但是在我的 pdb 会话中输入“运行”会导致我的会话以以下输出终止

(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 平台上尝试过这个并且得到了相同的结果,但是我在文档中找不到任何更正。

4

3 回答 3

13

因此,如果有人关心问题是 pdb 中的“运行”与 gdb 中的“运行”不完全相同。在 gdb 中,如果我输入 run,程序将重新启动并继续到第一个断点。在 pdb 中,程序重新启动并转到文件的开头。然后我必须按“n”,然后按“c”才能到达第一个断点。如果试图让程序运行,我在文件开头再次键入 run,调试器会崩溃,如上所示。

于 2012-10-19T16:04:22.923 回答
4

这是我的文件test.py

import pdb
pdb.set_trace()
print('1 line')

当我使用命令'python3 test.py'时,我得到了同样的错误:

> /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

然后我使用命令'python3 -m pdb test.py'

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) 

它有效。到目前为止,我不确定为什么会发生这种情况。

于 2018-06-10T11:09:55.153 回答
2

Upon encountering an abnormal condition (i.e., a bug in your program), pdb rather helpfully says:

Uncaught exception. Entering post mortem debugging 
Running 'cont' or 'step' will restart the program

Here, typing "run" or "restart" will cause the pdb process to unceremoniously terminate as OP states (but "cont" or "step" will restart as promised).

Presumably many impatient pythonistas would rather "run" or "restart" work regardless of whether we've entered "post mortem debugging" but I'm sure the existing restart semantics is the byproduct of a cleaner implementation.

于 2016-03-03T22:07:59.440 回答