classification
Title: on terminating, Pdb debugs itself
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, georg.brandl, xdegaye
Priority: normal Keywords: needs review, patch

Created on 2012-05-07 19:48 by xdegaye, last changed 2019-03-16 00:13 by BreamoreBoy.

Files
File name Uploaded Description Edit
pdb_botframe_default.patch xdegaye, 2012-05-07 19:48 review
pdb_botframe_default_2.patch xdegaye, 2012-05-08 11:39 review
pdb_botframe_default_3.patch xdegaye, 2012-05-11 09:00 review
Messages (5)
msg160166 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2012-05-07 19:48
All the problems raised in this issue are caused by self.botframe
being set in a Pdb frame.

In the following pdb session run with python 3.2.2, the first two
frames in the printed stack are Pdb frames, this is wrong. And the
second step command steps incorrectly into the exec() frame that is
called by the Bdb run() method. What prevents the third step command
to step into the run() frame, is that this frame does not have a trace
function setup initially.
===   foo.py    =================================
i = 1
=================================================
$ python3 -m pdb foo.py
> path_to/foo.py(1)<module>()
-> i = 1
(Pdb) import sys; print(sys.version)
3.2.2 (default, Dec 27 2011, 17:35:55) 
[GCC 4.3.2]
(Pdb) where
  /usr/local/lib/python3.2/bdb.py(392)run()
-> exec(cmd, globals, locals)
  <string>(1)<module>()
> path_to/foo.py(1)<module>()
-> i = 1
(Pdb) step
--Return--
> path_to/foo.py(1)<module>()->None
-> i = 1
(Pdb) step
--Return--
> <string>(1)<module>()->None
(Pdb) step
The program finished and will be restarted
> path_to/foo.py(1)<module>()
-> i = 1
(Pdb) quit
=================================================


In the following pdb session run with python built from the default
branch (i.e. after issue 13183 has been fixed) the third step command
steps into the run() method of Bdb and the backtrace printed after the
quit command shows duplicate entries. Pdb is trying to debug itself !
Pdb steps in the run() method because after the fix in issue 13183,
Pdb knows now how to step when returning into the caller frame with no
trace function.
===   foo.py    =================================
i = 1
=================================================
$ python -m pdb foo.py 
> path_to/foo.py(1)<module>()
-> i = 1
(Pdb) step
--Return--
> path_to/foo.py(1)<module>()->None
-> i = 1
(Pdb) step
--Return--
> <string>(1)<module>()->None
(Pdb) step
> path_to/cpython/Lib/bdb.py(409)run()
-> self.quitting = True
(Pdb) quit
Traceback (most recent call last):
  File "path_to/cpython/Lib/pdb.py", line 1651, in main
    pdb._runscript(mainpyfile)
  File "path_to/cpython/Lib/pdb.py", line 1532, in _runscript
    self.run(statement)
  File "path_to/cpython/Lib/bdb.py", line 409, in run
    self.quitting = True
  File "path_to/cpython/Lib/bdb.py", line 409, in run
    self.quitting = True
  File "path_to/cpython/Lib/bdb.py", line 47, in trace_dispatch
    return self.dispatch_line(frame)
  File "path_to/cpython/Lib/bdb.py", line 66, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> path_to/cpython/Lib/bdb.py(66)dispatch_line()
-> if self.quitting: raise BdbQuit
(Pdb) quit
Post mortem debugger finished. The foo.py will be restarted
> path_to/foo.py(1)<module>()
-> i = 1
(Pdb) quit
=================================================


In the following pdb session run with python 3.2.2, the session is
interrupted with <Ctl-C>. The same problems occur as in the previous
case.
=================================================
import time
i = 1
while i:
    time.sleep(.100)
i = 0
=================================================
$ python3 -m pdb foo.py
> path_to/foo.py(1)<module>()
-> import time
(Pdb) import sys; print(sys.version)
3.2.2 (default, Dec 27 2011, 17:35:55) 
[GCC 4.3.2]
(Pdb) continue
^C
Program interrupted. (Use 'cont' to resume).
> path_to/foo.py(3)<module>()
-> while i:
(Pdb) !i=0
(Pdb) step
> path_to/foo.py(5)<module>()
-> i = 0
(Pdb) step
--Return--
> path_to/foo.py(5)<module>()->None
-> i = 0
(Pdb) step
--Return--
> <string>(1)<module>()->None
(Pdb) step
> /usr/local/lib/python3.2/bdb.py(396)run()
-> self.quitting = True
(Pdb) quit
Traceback (most recent call last):
  File "/usr/local/lib/python3.2/pdb.py", line 1556, in main
    pdb._runscript(mainpyfile)
  File "/usr/local/lib/python3.2/pdb.py", line 1437, in _runscript
    self.run(statement)
  File "/usr/local/lib/python3.2/bdb.py", line 396, in run
    self.quitting = True
  File "/usr/local/lib/python3.2/bdb.py", line 396, in run
    self.quitting = True
  File "/usr/local/lib/python3.2/bdb.py", line 46, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/local/lib/python3.2/bdb.py", line 65, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/local/lib/python3.2/bdb.py(65)dispatch_line()
-> if self.quitting: raise BdbQuit
(Pdb) quit
Post mortem debugger finished. The foo.py will be restarted
> path_to/foo.py(1)<module>()
-> import time
(Pdb) quit
=================================================


The attached patch fixes all those problems. The patch applies to the
default branch.
msg160197 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2012-05-08 11:39
Uploaded a new patch, pdb_botframe_default_2.patch (that applies to
the current tip of the default branch) with:

    * a correction to the initial change made to fix sigint_handler()

    * the two test cases
msg160403 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2012-05-11 09:00
The previous patch only fixed the problem when the debugger is started
from its main function. Uploaded new patch
pdb_botframe_default_3.patch that fixes pdb.main and the pdb.run*
function.

This patch also corrects pdb.runcall(): in the following session, the
3.1 debugger starts the debugging session at the second line of foo()
and not at the --Call-- event. A test case for this problem is also
included in the patch.


===   main.py   =================================
import pdb, sys
print(sys.version)

def foo():
    pass

pdb.runcall(foo)
=================================================
$ python3.1 main.py
3.1.2 (r312:79147, Apr  4 2010, 17:46:48) 
[GCC 4.3.2]
> /path_to/main.py(5)foo()
-> pass
(Pdb) quit
=================================================
msg176276 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2012-11-24 10:55
See also how this is fixed at
http://code.google.com/p/pdb-clone/source/detail?r=625d61e3494d3b7e2a3e8578ddd2f204e21f1800
msg222268 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-04 07:39
Can we have a formal patch review please, but note the similar patch on #14788.
History
Date User Action Args
2019-03-16 00:13:59BreamoreBoysetnosy: - BreamoreBoy
2014-07-04 07:39:08BreamoreBoysetnosy: + BreamoreBoy

messages: + msg222268
versions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
2012-12-05 11:08:05asvetlovsetnosy: + asvetlov
2012-11-24 10:55:22xdegayesetmessages: + msg176276
2012-05-11 09:00:36xdegayesetfiles: + pdb_botframe_default_3.patch

messages: + msg160403
2012-05-08 11:39:37xdegayesetfiles: + pdb_botframe_default_2.patch

messages: + msg160197
2012-05-08 03:36:11eric.araujosetkeywords: + needs review
nosy: + georg.brandl

stage: test needed
2012-05-07 19:48:30xdegayecreate