classification
Title: Add Py_BREAKPOINT and sys._breakpoint hooks
Type: enhancement Stage: patch review
Components: Interpreter Core, Library (Lib) Versions: Python 3.3, Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: dmalcolm, haypo, loewis, pitrou
Priority: normal Keywords: patch

Created on 2010-08-18 23:43 by dmalcolm, last changed 2011-03-15 18:40 by dmalcolm.

Files
File name Uploaded Description Edit
add-sys.breakpoint.patch dmalcolm, 2010-08-18 23:43 Patch against py3k branch review
py3k-add-breakpoint-2010-11-01-001.patch dmalcolm, 2010-11-01 21:14 review
Messages (5)
msg114301 - (view) Author: Dave Malcolm (dmalcolm) (Python committer) Date: 2010-08-18 23:43
It's sometimes useful to be able to programatically inject a breakpoint when debugging CPython.

For example, sometimes you want a conditional breakpoint, but the logic involved is too complex to be expressed in the debugger (e.g. runtime complexity of evaluating the conditional in the debugger process, or deficiency of the debugger itself).

I'm attaching a patch which:
  - adds a Py_BREAKPOINT macro to pyport.h   This is available as a quick and dirty way of hardcoding a breakpoint in code (e.g. in extension modules); so that when you need to you can put of these in (perhaps guarded by C-level conditionals):
       if (complex_conditional()) {
           Py_BREAKPOINT();
       }

  - when Py_BREAKPOINT is defined, adds a sys.breakpoint() method.  This means that you can add C-level breakpoints to Python scripts, perhaps guarded by python-level conditionals:
       if foo and bar and not baz:
          sys.breakpoint()

Naturally this is highly system-dependent.   Only tested on Linux (Fedora 13 x86_64 with gcc-4.4.4).  The Windows implementation was copied from http://bugs.python.org/issue8863 but I don't have a Windows box to test it on.

I note that the GLib library within GNOME has a similar idea with a G_BREAKPOINT macro, which has accumulated a collection of platform-specific logic:
  http://git.gnome.org/browse/glib/tree/glib/gbacktrace.h
(unfortunately that's LGPLv2+ licensed)

Doesn't yet have a unit test.

Note that when running on Linux when _not_ under a debugger, the default for SIGTRAP is to get a coredump:
   Trace/breakpoint trap (core dumped)
so people should be strongly discouraged from adding these calls to their code.
msg114303 - (view) Author: Dave Malcolm (dmalcolm) (Python committer) Date: 2010-08-18 23:50
> Note that when running on Linux when _not_ under a debugger, the 
> default for SIGTRAP is to get a coredump:
>   Trace/breakpoint trap (core dumped)
> so people should be strongly discouraged from adding these calls to
> their code.
Looks like Windows' DebugBreak has similar behavior here; according to:
  http://msdn.microsoft.com/en-us/library/ms679297(VS.85).aspx
"If the process is not being debugged, the function uses the search logic of a standard exception handler. In most cases, this causes the calling process to terminate because of an unhandled breakpoint exception."
msg120174 - (view) Author: Dave Malcolm (dmalcolm) (Python committer) Date: 2010-11-01 21:14
Adding updated version of patch, which adds documentation to sys.rst and adds a unit test.

I'm a little wary of this: it seems useful but also too much like a self-destruct button for my taste.
msg120175 - (view) Author: Dave Malcolm (dmalcolm) (Python committer) Date: 2010-11-01 21:15
I renamed it from sys.breakpoint to sys._breakpoint, since this is CPython-specific
msg120241 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-11-02 17:25
I would rename Py_BREAKPOINT to _Py_BREAKPOINT since we don't really want to support this. Also, why do you allow any arguments to sys._breakpoint()?
History
Date User Action Args
2011-03-15 18:40:16dmalcolmsetnosy: + loewis
2010-11-02 17:25:37pitrousetnosy: + pitrou
messages: + msg120241
2010-11-01 21:15:02dmalcolmsetmessages: + msg120175
title: Add Py_BREAKPOINT and sys.breakpoint hooks -> Add Py_BREAKPOINT and sys._breakpoint hooks
2010-11-01 21:14:06dmalcolmsetfiles: + py3k-add-breakpoint-2010-11-01-001.patch

messages: + msg120174
2010-08-19 11:46:42eric.araujosetkeywords: patch, patch
title: RFE(patch): add Py_BREAKPOINT and sys.breakpoint hooks -> Add Py_BREAKPOINT and sys.breakpoint hooks
2010-08-18 23:50:54dmalcolmsetkeywords: patch, patch

messages: + msg114303
2010-08-18 23:43:23dmalcolmcreate