Skip to content

Commit 646853d

Browse files
alexmalyshevencukouvstinner
authored
gh-145559: Add PyUnstable_DumpTraceback() and PyUnstable_DumpTracebackThreads() (#148145)
Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 7b6c248 commit 646853d

12 files changed

Lines changed: 109 additions & 76 deletions

File tree

Doc/c-api/exceptions.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,3 +1348,67 @@ Tracebacks
13481348
13491349
This function returns ``0`` on success, and returns ``-1`` with an
13501350
exception set on failure.
1351+
1352+
.. c:function:: const char* PyUnstable_DumpTraceback(int fd, PyThreadState *tstate)
1353+
1354+
Write a trace of the Python stack in *tstate* into the file *fd*. The format
1355+
looks like::
1356+
1357+
Traceback (most recent call first):
1358+
File "xxx", line xxx in <xxx>
1359+
File "xxx", line xxx in <xxx>
1360+
...
1361+
File "xxx", line xxx in <xxx>
1362+
1363+
This function is meant to debug situations such as segfaults, fatal errors,
1364+
and similar. The file and function names it outputs are encoded to ASCII with
1365+
backslashreplace and truncated to 500 characters. It writes only the first
1366+
100 frames; further frames are truncated with the line ``...``.
1367+
1368+
This function will return ``NULL`` on success, or an error message on error.
1369+
1370+
This function is intended for use in crash scenarios such as signal handlers
1371+
for SIGSEGV, where the interpreter may be in an inconsistent state. Given
1372+
that it reads interpreter data structures that may be partially modified, the
1373+
function might produce incomplete output or it may even crash itself.
1374+
1375+
The caller does not need to hold an :term:`attached thread state`, nor does
1376+
*tstate* need to be attached.
1377+
1378+
.. versionadded:: next
1379+
1380+
.. c:function:: const char* PyUnstable_DumpTracebackThreads(int fd, PyInterpreterState *interp, PyThreadState *current_tstate, Py_ssize_t max_threads)
1381+
1382+
Write the traces of all Python threads in *interp* into the file *fd*.
1383+
1384+
If *interp* is ``NULL`` then this function will try to identify the current
1385+
interpreter using thread-specific storage. If it cannot, it will return an
1386+
error.
1387+
1388+
If *current_tstate* is not ``NULL`` then it will be used to identify what the
1389+
current thread is in the written output. If it is ``NULL`` then this function
1390+
will identify the current thread using thread-specific storage. It is not an
1391+
error if the function is unable to get the current Python thread state.
1392+
1393+
This function will return ``NULL`` on success, or an error message on error.
1394+
1395+
This function is meant to debug debug situations such as segfaults, fatal
1396+
errors, and similar. It calls :c:func:`PyUnstable_DumpTraceback` for each
1397+
thread. It only writes the tracebacks of the first *max_threads* threads,
1398+
further output is truncated with the line ``...``. If *max_threads* is 0, the
1399+
function will use a default value of 100 for the argument.
1400+
1401+
This function is intended for use in crash scenarios such as signal handlers
1402+
for SIGSEGV, where the interpreter may be in an inconsistent state. Given
1403+
that it reads interpreter data structures that may be partially modified, the
1404+
function might produce incomplete output or it may even crash itself.
1405+
1406+
The caller does not need to hold an :term:`attached thread state`, nor does
1407+
*current_tstate* need to be attached.
1408+
1409+
.. warning::
1410+
On the :term:`free-threaded build`, this function is not thread-safe. If
1411+
another thread deletes its :term:`thread state` while this function is being
1412+
called, the process will likely crash.
1413+
1414+
.. versionadded:: next

Doc/whatsnew/3.15.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,11 @@ New features
23522352
Python 3.14.
23532353
(Contributed by Victor Stinner in :gh:`142417`.)
23542354

2355+
* Add :c:func:`PyUnstable_DumpTraceback` and
2356+
:c:func:`PyUnstable_DumpTracebackThreads` functions to output Python
2357+
stacktraces.
2358+
(Contributed by Alex Malyshev in :gh:`145559`.)
2359+
23552360
Changed C APIs
23562361
--------------
23572362

Include/cpython/traceback.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ struct _traceback {
1111
int tb_lasti;
1212
int tb_lineno;
1313
};
14+
15+
PyAPI_FUNC(const char*) PyUnstable_DumpTraceback(int fd, PyThreadState *tstate);
16+
17+
PyAPI_FUNC(const char*) PyUnstable_DumpTracebackThreads(
18+
int fd,
19+
PyInterpreterState *interp,
20+
PyThreadState *current_tstate,
21+
Py_ssize_t max_threads);

Include/internal/pycore_traceback.h

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,6 @@ PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int, int *, P
1414
// Export for 'pyexact' shared extension
1515
PyAPI_FUNC(void) _PyTraceback_Add(const char *, const char *, int);
1616

17-
/* Write the Python traceback into the file 'fd'. For example:
18-
19-
Traceback (most recent call first):
20-
File "xxx", line xxx in <xxx>
21-
File "xxx", line xxx in <xxx>
22-
...
23-
File "xxx", line xxx in <xxx>
24-
25-
This function is written for debug purpose only, to dump the traceback in
26-
the worst case: after a segmentation fault, at fatal error, etc. That's why,
27-
it is very limited. Strings are truncated to 100 characters and encoded to
28-
ASCII with backslashreplace. It doesn't write the source code, only the
29-
function name, filename and line number of each frame. Write only the first
30-
100 frames: if the traceback is truncated, write the line " ...".
31-
32-
This function is signal safe. */
33-
34-
extern void _Py_DumpTraceback(
35-
int fd,
36-
PyThreadState *tstate);
37-
38-
/* Write the traceback of all threads into the file 'fd'. current_thread can be
39-
NULL.
40-
41-
Return NULL on success, or an error message on error.
42-
43-
This function is written for debug purpose only. It calls
44-
_Py_DumpTraceback() for each thread, and so has the same limitations. It
45-
only write the traceback of the first 100 threads: write "..." if there are
46-
more threads.
47-
48-
If current_tstate is NULL, the function tries to get the Python thread state
49-
of the current thread. It is not an error if the function is unable to get
50-
the current Python thread state.
51-
52-
If interp is NULL, the function tries to get the interpreter state from
53-
the current Python thread state, or from
54-
_PyGILState_GetInterpreterStateUnsafe() in last resort.
55-
56-
It is better to pass NULL to interp and current_tstate, the function tries
57-
different options to retrieve this information.
58-
59-
This function is signal safe. */
60-
61-
extern const char* _Py_DumpTracebackThreads(
62-
int fd,
63-
PyInterpreterState *interp,
64-
PyThreadState *current_tstate,
65-
Py_ssize_t max_threads);
66-
6717
/* Write a Unicode object into the file descriptor fd. Encode the string to
6818
ASCII using the backslashreplace error handler.
6919
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Rename ``_Py_DumpTraceback`` and ``_Py_DumpTracebackThreads`` to
2+
:c:func:`PyUnstable_DumpTraceback` and
3+
:c:func:`PyUnstable_DumpTracebackThreads`.

Modules/faulthandler.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "pycore_runtime.h" // _Py_ID()
88
#include "pycore_signal.h" // Py_NSIG
99
#include "pycore_time.h" // _PyTime_FromSecondsObject()
10-
#include "pycore_traceback.h" // _Py_DumpTracebackThreads
10+
#include "pycore_traceback.h" // _Py_DumpStack()
1111
#ifdef HAVE_UNISTD_H
1212
# include <unistd.h> // _exit()
1313
#endif
@@ -206,14 +206,15 @@ faulthandler_dump_traceback(int fd, int all_threads,
206206
PyThreadState *tstate = PyGILState_GetThisThreadState();
207207

208208
if (all_threads == 1) {
209-
(void)_Py_DumpTracebackThreads(fd, NULL, tstate, max_threads);
209+
(void)PyUnstable_DumpTracebackThreads(fd, NULL, tstate, max_threads);
210210
}
211211
else {
212212
if (all_threads == FT_IGNORE_ALL_THREADS) {
213213
PUTS(fd, "<Cannot show all threads while the GIL is disabled>\n");
214214
}
215-
if (tstate != NULL)
216-
_Py_DumpTraceback(fd, tstate);
215+
if (tstate != NULL) {
216+
PyUnstable_DumpTraceback(fd, tstate);
217+
}
217218
}
218219

219220
reentrant = 0;
@@ -277,17 +278,18 @@ faulthandler_dump_traceback_py_impl(PyObject *module, PyObject *file,
277278
/* gh-128400: Accessing other thread states while they're running
278279
* isn't safe if those threads are running. */
279280
_PyEval_StopTheWorld(interp);
280-
errmsg = _Py_DumpTracebackThreads(fd, NULL, tstate, max_threads);
281+
errmsg = PyUnstable_DumpTracebackThreads(fd, NULL, tstate, max_threads);
281282
_PyEval_StartTheWorld(interp);
282-
if (errmsg != NULL) {
283-
PyErr_SetString(PyExc_RuntimeError, errmsg);
284-
Py_XDECREF(file);
285-
return NULL;
286-
}
287283
}
288284
else {
289-
_Py_DumpTraceback(fd, tstate);
285+
errmsg = PyUnstable_DumpTraceback(fd, tstate);
290286
}
287+
if (errmsg != NULL) {
288+
PyErr_SetString(PyExc_RuntimeError, errmsg);
289+
Py_XDECREF(file);
290+
return NULL;
291+
}
292+
291293
Py_XDECREF(file);
292294

293295
if (PyErr_CheckSignals())
@@ -713,8 +715,8 @@ faulthandler_thread(void *unused)
713715

714716
(void)_Py_write_noraise(thread.fd, thread.header, (int)thread.header_len);
715717

716-
errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, NULL,
717-
thread.max_threads);
718+
errmsg = PyUnstable_DumpTracebackThreads(thread.fd, thread.interp, NULL,
719+
thread.max_threads);
718720
ok = (errmsg == NULL);
719721

720722
if (thread.exit)

Platforms/emscripten/node_entry.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ try {
5757
// Show JavaScript exception and traceback
5858
console.warn(e);
5959
// Show Python exception and traceback
60-
Module.__Py_DumpTraceback(2, Module._PyGILState_GetThisThreadState());
60+
Module.PyUnstable_DumpTraceback(2, Module._PyGILState_GetThisThreadState());
6161
process.exit(1);
6262
}

Platforms/emscripten/web_example_pyrepl_jspi/src.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,6 @@ try {
189189
// Show JavaScript exception and traceback
190190
console.warn(e);
191191
// Show Python exception and traceback
192-
Module.__Py_DumpTraceback(2, Module._PyGILState_GetThisThreadState());
192+
Module.PyUnstable_DumpTraceback(2, Module._PyGILState_GetThisThreadState());
193193
process.exit(1);
194194
}

Python/pylifecycle.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "pycore_setobject.h" // _PySet_NextEntry()
3030
#include "pycore_stats.h" // _PyStats_InterpInit()
3131
#include "pycore_sysmodule.h" // _PySys_ClearAttrString()
32-
#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
32+
#include "pycore_traceback.h" // PyUnstable_TracebackThreads()
3333
#include "pycore_tuple.h" // _PyTuple_FromPair
3434
#include "pycore_typeobject.h" // _PyTypes_InitTypes()
3535
#include "pycore_typevarobject.h" // _Py_clear_generic_types()
@@ -3348,9 +3348,9 @@ _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
33483348

33493349
/* display the current Python stack */
33503350
#ifndef Py_GIL_DISABLED
3351-
_Py_DumpTracebackThreads(fd, interp, tstate, 0);
3351+
PyUnstable_DumpTracebackThreads(fd, interp, tstate, 0);
33523352
#else
3353-
_Py_DumpTraceback(fd, tstate);
3353+
PyUnstable_DumpTraceback(fd, tstate);
33543354
#endif
33553355
}
33563356

Python/traceback.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,10 +1167,11 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
11671167
11681168
The caller is responsible to call PyErr_CheckSignals() to call Python signal
11691169
handlers if signals were received. */
1170-
void
1171-
_Py_DumpTraceback(int fd, PyThreadState *tstate)
1170+
const char*
1171+
PyUnstable_DumpTraceback(int fd, PyThreadState *tstate)
11721172
{
11731173
dump_traceback(fd, tstate, 1);
1174+
return NULL;
11741175
}
11751176

11761177
#if defined(HAVE_PTHREAD_GETNAME_NP) || defined(HAVE_PTHREAD_GET_NAME_NP)
@@ -1264,16 +1265,16 @@ write_thread_id(int fd, PyThreadState *tstate, int is_current)
12641265
The caller is responsible to call PyErr_CheckSignals() to call Python signal
12651266
handlers if signals were received. */
12661267
const char* _Py_NO_SANITIZE_THREAD
1267-
_Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
1268-
PyThreadState *current_tstate,
1269-
Py_ssize_t max_threads)
1268+
PyUnstable_DumpTracebackThreads(int fd, PyInterpreterState *interp,
1269+
PyThreadState *current_tstate,
1270+
Py_ssize_t max_threads)
12701271
{
12711272
if (max_threads == 0) {
12721273
max_threads = DEFAULT_MAX_NTHREADS;
12731274
}
12741275

12751276
if (current_tstate == NULL) {
1276-
/* _Py_DumpTracebackThreads() is called from signal handlers by
1277+
/* PyUnstable_DumpTracebackThreads() is called from signal handlers by
12771278
faulthandler.
12781279
12791280
SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals

0 commit comments

Comments
 (0)