Display Python backtrace on SIGSEGV, SIGFPE and fatal error - Code Review (original) (raw)

OLD

NEW

1

1

2 /* Python interpreter top-level routines, including init/exit */

2 /* Python interpreter top-level routines, including init/exit */

3

3

4 #include "Python.h"

4 #include "Python.h"

5

5

6 #include "Python-ast.h"

6 #include "Python-ast.h"

7 #undef Yield /* undefine macro conflicting with winbase.h */

7 #undef Yield /* undefine macro conflicting with winbase.h */

8 #include "grammar.h"

8 #include "grammar.h"

9 #include "node.h"

9 #include "node.h"

10 #include "token.h"

10 #include "token.h"

(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

73 extern void _PyUnicode_Init(void);

73 extern void _PyUnicode_Init(void);

74 extern void _PyUnicode_Fini(void);

74 extern void _PyUnicode_Fini(void);

75 extern int _PyLong_Init(void);

75 extern int _PyLong_Init(void);

76 extern void PyLong_Fini(void);

76 extern void PyLong_Fini(void);

77

77

78 #ifdef WITH_THREAD

78 #ifdef WITH_THREAD

79 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);

79 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);

80 extern void _PyGILState_Fini(void);

80 extern void _PyGILState_Fini(void);

81 #endif /* WITH_THREAD */

81 #endif /* WITH_THREAD */

82

82

83 extern void _Py_InitSegfault(void);

84 extern void _Py_FiniSegfault(void);

85 extern int _Py_DumpBacktrace(void);

86

87

83 int Py_DebugFlag; /* Needed by parser.c */

88 int Py_DebugFlag; /* Needed by parser.c */

84 int Py_VerboseFlag; /* Needed by import.c */

89 int Py_VerboseFlag; /* Needed by import.c */

85 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */

90 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */

86 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */

91 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */

87 int Py_NoSiteFlag; /* Suppress 'import site' */

92 int Py_NoSiteFlag; /* Suppress 'import site' */

88 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */

93 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */

89 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */

94 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */

90 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */

95 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */

91 int Py_FrozenFlag; /* Needed by getpath.c */

96 int Py_FrozenFlag; /* Needed by getpath.c */

92 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */

97 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */

(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

390 initialized = 0;

395 initialized = 0;

391

396

392 /* Flush stdout+stderr */

397 /* Flush stdout+stderr */

393 flush_std_files();

398 flush_std_files();

394

399

395 /* Get current thread state and interpreter pointer */

400 /* Get current thread state and interpreter pointer */

396 tstate = PyThreadState_GET();

401 tstate = PyThreadState_GET();

397 interp = tstate->interp;

402 interp = tstate->interp;

398

403

399 /* Disable signal handling */

404 /* Disable signal handling */

405 _Py_FiniSegfault();

400 PyOS_FiniInterrupts();

406 PyOS_FiniInterrupts();

401

407

402 /* Clear type lookup cache */

408 /* Clear type lookup cache */

403 PyType_ClearCache();

409 PyType_ClearCache();

404

410

405 /* Collect garbage. This may call finalizers; it's nice to call these

411 /* Collect garbage. This may call finalizers; it's nice to call these

406 * before all modules are destroyed.

412 * before all modules are destroyed.

407 * XXX If a __del__ or weakref callback is triggered here, and tries to

413 * XXX If a __del__ or weakref callback is triggered here, and tries to

408 * XXX import a module, bad things can happen, because Python no

414 * XXX import a module, bad things can happen, because Python no

409 * XXX longer believes it's initialized.

415 * XXX longer believes it's initialized.

(...skipping 1928 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

2338 PyObject_FREE(err->text);

2344 PyObject_FREE(err->text);

2339 err->text = NULL;

2345 err->text = NULL;

2340 }

2346 }

2341 }

2347 }

2342

2348

2343 /* Print fatal error message and abort */

2349 /* Print fatal error message and abort */

2344

2350

2345 void

2351 void

2346 Py_FatalError(const char *msg)

2352 Py_FatalError(const char *msg)

2347 {

2353 {

2348 fprintf(stderr, "Fatal Python error: %s\n", msg);

2354 fputs("Fatal Python error: ", stderr);

2349 fflush(stderr); /* it helps in Windows debug build */

2355 fputs(msg, stderr);

2350 if (PyErr_Occurred()) {

2356 fputc('\n', stderr);

2357 fflush(stderr);

2358

2359 if (PyErr_Occurred())

2351 PyErr_PrintEx(0);

2360 PyErr_PrintEx(0);

2361 else {

2362 fputc('\n', stderr);

2363 fflush(stderr);

2364 if (!_Py_DumpBacktrace()) {

2365 fputs(msg, stderr);

2366 fputc('\n', stderr);

2367 fflush(stderr);

2368 }

2352 }

2369 }

2353 #ifdef MS_WINDOWS

2370 #ifdef MS_WINDOWS

2354 {

2371 {

2355 size_t len = strlen(msg);

2372 size_t len = strlen(msg);

2356 WCHAR* buffer;

2373 WCHAR* buffer;

2357 size_t i;

2374 size_t i;

2358

2375

2359 /* Convert the message to wchar_t. This uses a simple one-to-one

2376 /* Convert the message to wchar_t. This uses a simple one-to-one

2360 conversion, assuming that the this error message actually uses ASCII

2377 conversion, assuming that the this error message actually uses ASCII

2361 only. If this ceases to be true, we will have to convert. */

2378 only. If this ceases to be true, we will have to convert. */

(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

2460 #ifdef SIGPIPE

2477 #ifdef SIGPIPE

2461 PyOS_setsig(SIGPIPE, SIG_IGN);

2478 PyOS_setsig(SIGPIPE, SIG_IGN);

2462 #endif

2479 #endif

2463 #ifdef SIGXFZ

2480 #ifdef SIGXFZ

2464 PyOS_setsig(SIGXFZ, SIG_IGN);

2481 PyOS_setsig(SIGXFZ, SIG_IGN);

2465 #endif

2482 #endif

2466 #ifdef SIGXFSZ

2483 #ifdef SIGXFSZ

2467 PyOS_setsig(SIGXFSZ, SIG_IGN);

2484 PyOS_setsig(SIGXFSZ, SIG_IGN);

2468 #endif

2485 #endif

2469 PyOS_InitInterrupts(); /* May imply initsignal() */

2486 PyOS_InitInterrupts(); /* May imply initsignal() */

2487 _Py_InitSegfault();

2470 }

2488 }

2471

2489

2472

2490

2473 /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.

2491 /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.

2474 *

2492 *

2475 * All of the code in this function must only use async-signal-safe functions,

2493 * All of the code in this function must only use async-signal-safe functions,

2476 * listed at `man 7 signal` or

2494 * listed at `man 7 signal` or

2477 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh\_chap02\_04.html.

2495 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh\_chap02\_04.html.

2478 */

2496 */

2479 void

2497 void

(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

2719 #undef PyRun_InteractiveLoop

2737 #undef PyRun_InteractiveLoop

2720 PyAPI_FUNC(int)

2738 PyAPI_FUNC(int)

2721 PyRun_InteractiveLoop(FILE *f, const char *p)

2739 PyRun_InteractiveLoop(FILE *f, const char *p)

2722 {

2740 {

2723 return PyRun_InteractiveLoopFlags(f, p, NULL);

2741 return PyRun_InteractiveLoopFlags(f, p, NULL);

2724 }

2742 }

2725

2743

2726 #ifdef __cplusplus

2744 #ifdef __cplusplus

2727 }

2745 }

2728 #endif

2746 #endif

OLD

NEW