PDB.run - restarting a pdb session

2019-04-06 21:31发布

问题:

I'm relatively new to python and pdb, but I have a lot of experience with gdb.

My problem is that if I set a number of breakpoints in my code at some point I will want to change something and re-run my debug session retaining these break points. However entering "run" in my pdb session cases my session to terminate with the following output

(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
]$

I've tried this on two independent linux platforms and had the same result but I cannot find any corrections in the documentation.

回答1:

So if anyone cares the problem was that "run" in pdb is not the exact same as "run" in gdb. In gdb if I enter run the program restarts and continues to the first break point. In pdb the program restarts and goes to the start of the file. I then have to press 'n' followed by 'c' to get to the first break point. If, attempting to get the program to run, I type run a second time at the start of the file the debugger crashes, as shown above.



回答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.



回答3:

Here is my file test.py:

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

I get the same error when I using command '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

Then I use command '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) 

It works. So far, I am not sure why this happens.



标签: python pdb