Author dmalcolm
Recipients dmalcolm, vstinner
Date 2010-08-18.23:43:21
SpamBayes Score 6.80944e-12
Marked as misclassified No
Message-id <>
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()) {

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

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 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:
(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.
Date User Action Args
2010-08-18 23:43:25dmalcolmsetrecipients: + dmalcolm, vstinner
2010-08-18 23:43:25dmalcolmsetmessageid: <>
2010-08-18 23:43:23dmalcolmlinkissue9635 messages
2010-08-18 23:43:22dmalcolmcreate