@@ -106,6 +106,7 @@ PyStructSequence_Desc ThreadInfo_desc = {
106106// InterpreterInfo structseq type
107107static PyStructSequence_Field InterpreterInfo_fields [] = {
108108 {"interpreter_id" , "Interpreter ID" },
109+ {"main_thread_id" , "Main thread ID" },
109110 {"threads" , "List of threads in this interpreter" },
110111 {NULL }
111112};
@@ -114,7 +115,7 @@ PyStructSequence_Desc InterpreterInfo_desc = {
114115 "_remote_debugging.InterpreterInfo" ,
115116 "Information about an interpreter" ,
116117 InterpreterInfo_fields ,
117- 2
118+ 3
118119};
119120
120121// AwaitedInfo structseq type
@@ -583,11 +584,19 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self
583584 current_tstate = self -> tstate_addr ;
584585 }
585586
587+ // Acquire main thread state information
588+ uintptr_t main_thread_tstate = GET_MEMBER (uintptr_t , interp_state_buffer ,
589+ self -> debug_offsets .interpreter_state .threads_main );
590+
591+ PyObject * main_thread_id = NULL ;
592+
593+ uint64_t prev_thread_id = 0 ;
586594 while (current_tstate != 0 ) {
587595 uintptr_t prev_tstate = current_tstate ;
588596 PyObject * frame_info = unwind_stack_for_thread (self , & current_tstate ,
589597 gil_holder_tstate ,
590- gc_frame );
598+ gc_frame ,
599+ & prev_thread_id );
591600 if (!frame_info ) {
592601 // Check if this was an intentional skip due to mode-based filtering
593602 if ((self -> mode == PROFILING_MODE_CPU || self -> mode == PROFILING_MODE_GIL ||
@@ -613,6 +622,10 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self
613622 goto exit ;
614623 }
615624
625+ if (prev_tstate == main_thread_tstate ) {
626+ main_thread_id = PyLong_FromUnsignedLongLong (prev_thread_id );
627+ }
628+
616629 if (PyList_Append (interpreter_threads , frame_info ) == -1 ) {
617630 Py_DECREF (frame_info );
618631 Py_DECREF (interpreter_threads );
@@ -648,7 +661,8 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self
648661 }
649662
650663 PyStructSequence_SetItem (interpreter_info , 0 , interp_id ); // steals reference
651- PyStructSequence_SetItem (interpreter_info , 1 , interpreter_threads ); // steals reference
664+ PyStructSequence_SetItem (interpreter_info , 1 , main_thread_id ); // steals reference
665+ PyStructSequence_SetItem (interpreter_info , 2 , interpreter_threads ); // steals reference
652666
653667 // Add this interpreter to the result list
654668 if (PyList_Append (result , interpreter_info ) == -1 ) {
0 commit comments