@@ -54,15 +54,77 @@ static PyObject * do_call_core(
5454
5555#ifdef LLTRACE
5656static int lltrace ;
57- static int prtrace (PyThreadState * , PyObject * , const char * );
58- static void lltrace_instruction (_PyInterpreterFrame * frame , int opcode , int oparg )
57+ static void
58+ dump_stack (_PyInterpreterFrame * frame , PyObject * * stack_pointer )
59+ {
60+ PyObject * * stack_base = _PyFrame_Stackbase (frame );
61+ PyObject * type , * value , * traceback ;
62+ PyErr_Fetch (& type , & value , & traceback );
63+ printf (" stack=[" );
64+ for (PyObject * * ptr = stack_base ; ptr < stack_pointer ; ptr ++ ) {
65+ if (ptr != stack_base ) {
66+ printf (", " );
67+ }
68+ if (PyObject_Print (* ptr , stdout , 0 ) != 0 ) {
69+ PyErr_Clear ();
70+ printf ("<%s object at %p>" ,
71+ Py_TYPE (* ptr )-> tp_name , (void * )(* ptr ));
72+ }
73+ }
74+ printf ("]\n" );
75+ fflush (stdout );
76+ PyErr_Restore (type , value , traceback );
77+ }
78+
79+ static void
80+ lltrace_instruction (_PyInterpreterFrame * frame ,
81+ PyObject * * stack_pointer ,
82+ _Py_CODEUNIT * next_instr )
5983{
84+ dump_stack (frame , stack_pointer );
85+ int oparg = _Py_OPARG (* next_instr );
86+ int opcode = _Py_OPCODE (* next_instr );
87+ const char * opname = _PyOpcode_OpName [opcode ];
88+ assert (opname != NULL );
89+ int offset = (int )(next_instr - _PyCode_CODE (frame -> f_code ));
6090 if (HAS_ARG (opcode )) {
61- printf ("%d: %d, %d\n" , _PyInterpreterFrame_LASTI ( frame ), opcode , oparg );
91+ printf ("%d: %s %d\n" , offset * 2 , opname , oparg );
6292 }
6393 else {
64- printf ("%d: %d\n" , _PyInterpreterFrame_LASTI (frame ), opcode );
94+ printf ("%d: %s\n" , offset * 2 , opname );
95+ }
96+ fflush (stdout );
97+ }
98+ static void
99+ lltrace_resume_frame (_PyInterpreterFrame * frame )
100+ {
101+ PyFunctionObject * f = frame -> f_func ;
102+ if (f == NULL ) {
103+ printf ("\nResuming frame." );
104+ return ;
105+ }
106+ PyObject * type , * value , * traceback ;
107+ PyErr_Fetch (& type , & value , & traceback );
108+ PyObject * name = f -> func_qualname ;
109+ if (name == NULL ) {
110+ name = f -> func_name ;
111+ }
112+ printf ("\nResuming frame" );
113+ if (name ) {
114+ printf (" for " );
115+ if (PyObject_Print (name , stdout , 0 ) < 0 ) {
116+ PyErr_Clear ();
117+ }
65118 }
119+ if (f -> func_module ) {
120+ printf (" in module " );
121+ if (PyObject_Print (f -> func_module , stdout , 0 ) < 0 ) {
122+ PyErr_Clear ();
123+ }
124+ }
125+ printf ("\n" );
126+ fflush (stdout );
127+ PyErr_Restore (type , value , traceback );
66128}
67129#endif
68130static int call_trace (Py_tracefunc , PyObject * ,
@@ -1266,7 +1328,8 @@ eval_frame_handle_pending(PyThreadState *tstate)
12661328
12671329/* PRE_DISPATCH_GOTO() does lltrace if enabled. Normally a no-op */
12681330#ifdef LLTRACE
1269- #define PRE_DISPATCH_GOTO () if (lltrace) { lltrace_instruction(frame, opcode, oparg); }
1331+ #define PRE_DISPATCH_GOTO () if (lltrace) { \
1332+ lltrace_instruction(frame, stack_pointer, next_instr); }
12701333#else
12711334#define PRE_DISPATCH_GOTO () ((void)0)
12721335#endif
@@ -1375,6 +1438,7 @@ eval_frame_handle_pending(PyThreadState *tstate)
13751438/* The stack can grow at most MAXINT deep, as co_nlocals and
13761439 co_stacksize are ints. */
13771440#define STACK_LEVEL () ((int)(stack_pointer - _PyFrame_Stackbase(frame)))
1441+ #define STACK_SIZE () (frame->f_code->co_stacksize)
13781442#define EMPTY () (STACK_LEVEL() == 0)
13791443#define TOP () (stack_pointer[-1])
13801444#define SECOND () (stack_pointer[-2])
@@ -1387,23 +1451,21 @@ eval_frame_handle_pending(PyThreadState *tstate)
13871451#define BASIC_PUSH (v ) (*stack_pointer++ = (v))
13881452#define BASIC_POP () (*--stack_pointer)
13891453
1390- #ifdef LLTRACE
1391- #define PUSH (v ) { (void)(BASIC_PUSH(v), \
1392- lltrace && prtrace(tstate, TOP(), "push") ); \
1393- assert(STACK_LEVEL() <= frame->f_code->co_stacksize); }
1394- #define POP () ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
1395- BASIC_POP())
1454+ #ifdef Py_DEBUG
1455+ #define PUSH (v ) do { \
1456+ BASIC_PUSH(v ); \
1457+ assert(STACK_LEVEL() <= STACK_SIZE()); \
1458+ } while (0)
1459+ #define POP () (assert(STACK_LEVEL() > 0), BASIC_POP())
13961460#define STACK_GROW (n ) do { \
1397- assert(n >= 0); \
1398- (void)(BASIC_STACKADJ(n), \
1399- lltrace && prtrace(tstate, TOP(), "stackadj")); \
1400- assert(STACK_LEVEL() <= frame->f_code->co_stacksize); \
1461+ assert(n >= 0); \
1462+ BASIC_STACKADJ(n); \
1463+ assert(STACK_LEVEL() <= STACK_SIZE()); \
14011464 } while (0)
14021465#define STACK_SHRINK (n ) do { \
14031466 assert(n >= 0); \
1404- (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
1405- (void)(BASIC_STACKADJ(-(n))); \
1406- assert(STACK_LEVEL() <= frame->f_code->co_stacksize); \
1467+ assert(STACK_LEVEL() >= n); \
1468+ BASIC_STACKADJ(-(n)); \
14071469 } while (0)
14081470#else
14091471#define PUSH (v ) BASIC_PUSH(v)
@@ -1673,6 +1735,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
16731735 }
16741736 lltrace = r ;
16751737 }
1738+ if (lltrace ) {
1739+ lltrace_resume_frame (frame );
1740+ }
16761741#endif
16771742
16781743#ifdef Py_DEBUG
@@ -6663,23 +6728,6 @@ unpack_iterable(PyThreadState *tstate, PyObject *v,
66636728 return 0 ;
66646729}
66656730
6666- #ifdef LLTRACE
6667- static int
6668- prtrace (PyThreadState * tstate , PyObject * v , const char * str )
6669- {
6670- printf ("%s " , str );
6671- PyObject * type , * value , * traceback ;
6672- PyErr_Fetch (& type , & value , & traceback );
6673- if (PyObject_Print (v , stdout , 0 ) != 0 ) {
6674- /* Don't know what else to do */
6675- _PyErr_Clear (tstate );
6676- }
6677- printf ("\n" );
6678- PyErr_Restore (type , value , traceback );
6679- return 1 ;
6680- }
6681- #endif
6682-
66836731static void
66846732call_exc_trace (Py_tracefunc func , PyObject * self ,
66856733 PyThreadState * tstate ,
0 commit comments