~bzr-pqm/bzr/bzr.dev

5273.1.5 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
2423.3.4 by Martin Pool
Late load pdb for breakin; fix copyright
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2423.3.1 by Martin Pool
C-\ drops bzr into the debugger
16
6379.6.1 by Jelmer Vernooij
Import absolute_import in a few places.
17
from __future__ import absolute_import
18
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
19
import os
2423.3.5 by Martin Pool
Second sigquit goes through to the default handler
20
import signal
21
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
22
23
_breakin_signal_number = None
24
_breakin_signal_name = None
25
26
2423.3.1 by Martin Pool
C-\ drops bzr into the debugger
27
def _debug(signal_number, interrupted_frame):
2423.3.4 by Martin Pool
Late load pdb for breakin; fix copyright
28
    import pdb
29
    import sys
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
30
    sys.stderr.write("** %s received, entering debugger\n"
2423.3.5 by Martin Pool
Second sigquit goes through to the default handler
31
            "** Type 'c' to continue or 'q' to stop the process\n"
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
32
            "** Or %s again to quit (and possibly dump core)\n"
33
            % (_breakin_signal_name, _breakin_signal_name))
34
    # It seems that on Windows, when sys.stderr is to a PIPE, then we need to
35
    # flush. Not sure why it is buffered, but that seems to be the case.
36
    sys.stderr.flush()
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
37
    # restore default meaning so that you can kill the process by hitting it
38
    # twice
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
39
    signal.signal(_breakin_signal_number, signal.SIG_DFL)
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
40
    try:
41
        pdb.set_trace()
42
    finally:
4797.37.4 by Andrew Bennetts
Revert change to SIGQUIT signal handler; allowing EINTR to happen on SIGQUIT seems to be the lesser of two evils.
43
        signal.signal(_breakin_signal_number, _debug)
2423.3.5 by Martin Pool
Second sigquit goes through to the default handler
44
2423.3.1 by Martin Pool
C-\ drops bzr into the debugger
45
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
46
def determine_signal():
47
    global _breakin_signal_number
48
    global _breakin_signal_name
49
    if _breakin_signal_number is not None:
50
        return _breakin_signal_number
51
    # Note: As near as I can tell, Windows is the only one to define SIGBREAK,
52
    #       and other platforms defined SIGQUIT. There doesn't seem to be a
53
    #       platform that defines both.
54
    #       -- jam 2009-07-30
55
    sigquit = getattr(signal, 'SIGQUIT', None)
56
    sigbreak = getattr(signal, 'SIGBREAK', None)
57
    if sigquit is not None:
58
        _breakin_signal_number = sigquit
59
        _breakin_signal_name = 'SIGQUIT'
60
    elif sigbreak is not None:
61
        _breakin_signal_number = sigbreak
62
        _breakin_signal_name = 'SIGBREAK'
63
64
    return _breakin_signal_number
65
66
67
def hook_debugger_to_signal():
68
    """Add a signal handler so we drop into the debugger.
69
5278.1.2 by Martin Pool
Don't say 'Linux' except when specifically talking about the kernel
70
    On Unix, this is hooked into SIGQUIT (C-\\), and on Windows, this is
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
71
    hooked into SIGBREAK (C-Pause).
72
    """
73
74
    # when sigquit (C-\) or sigbreak (C-Pause) is received go into pdb
75
    if os.environ.get('BZR_SIGQUIT_PDB', '1') == '0':
76
        # User explicitly requested we don't support this
77
        return
78
    sig = determine_signal()
79
    if sig is None:
80
        return
81
    # print 'hooking into %s' % (_breakin_signal_name,)
4797.37.4 by Andrew Bennetts
Revert change to SIGQUIT signal handler; allowing EINTR to happen on SIGQUIT seems to be the lesser of two evils.
82
    signal.signal(sig, _debug)