@@ -54,15 +54,77 @@ static PyObject * do_call_core(
54
54
55
55
#ifdef LLTRACE
56
56
static 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 )
59
83
{
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 ));
60
90
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 );
62
92
}
63
93
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
+ }
65
118
}
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 );
66
128
}
67
129
#endif
68
130
static int call_trace (Py_tracefunc , PyObject * ,
@@ -1266,7 +1328,8 @@ eval_frame_handle_pending(PyThreadState *tstate)
1266
1328
1267
1329
/* PRE_DISPATCH_GOTO() does lltrace if enabled. Normally a no-op */
1268
1330
#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); }
1270
1333
#else
1271
1334
#define PRE_DISPATCH_GOTO () ((void)0)
1272
1335
#endif
@@ -1375,6 +1438,7 @@ eval_frame_handle_pending(PyThreadState *tstate)
1375
1438
/* The stack can grow at most MAXINT deep, as co_nlocals and
1376
1439
co_stacksize are ints. */
1377
1440
#define STACK_LEVEL () ((int)(stack_pointer - _PyFrame_Stackbase(frame)))
1441
+ #define STACK_SIZE () (frame->f_code->co_stacksize)
1378
1442
#define EMPTY () (STACK_LEVEL() == 0)
1379
1443
#define TOP () (stack_pointer[-1])
1380
1444
#define SECOND () (stack_pointer[-2])
@@ -1387,23 +1451,21 @@ eval_frame_handle_pending(PyThreadState *tstate)
1387
1451
#define BASIC_PUSH (v ) (*stack_pointer++ = (v))
1388
1452
#define BASIC_POP () (*--stack_pointer)
1389
1453
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())
1396
1460
#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()); \
1401
1464
} while (0)
1402
1465
#define STACK_SHRINK (n ) do { \
1403
1466
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)); \
1407
1469
} while (0)
1408
1470
#else
1409
1471
#define PUSH (v ) BASIC_PUSH(v)
@@ -1673,6 +1735,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
1673
1735
}
1674
1736
lltrace = r ;
1675
1737
}
1738
+ if (lltrace ) {
1739
+ lltrace_resume_frame (frame );
1740
+ }
1676
1741
#endif
1677
1742
1678
1743
#ifdef Py_DEBUG
@@ -6663,23 +6728,6 @@ unpack_iterable(PyThreadState *tstate, PyObject *v,
6663
6728
return 0 ;
6664
6729
}
6665
6730
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
-
6683
6731
static void
6684
6732
call_exc_trace (Py_tracefunc func , PyObject * self ,
6685
6733
PyThreadState * tstate ,
0 commit comments