@@ -386,8 +386,7 @@ parse_coro_chain(
386386 // Parse the previous frame using the gi_iframe from local copy
387387 uintptr_t prev_frame ;
388388 uintptr_t gi_iframe_addr = coro_address + (uintptr_t )unwinder -> debug_offsets .gen_object .gi_iframe ;
389- uintptr_t address_of_code_object = 0 ;
390- if (parse_frame_object (unwinder , & name , gi_iframe_addr , & address_of_code_object , & prev_frame ) < 0 ) {
389+ if (parse_frame_object (unwinder , & name , gi_iframe_addr , & prev_frame ) < 0 ) {
391390 set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to parse frame object in coro chain" );
392391 return -1 ;
393392 }
@@ -422,13 +421,12 @@ parse_coro_chain(
422421static PyObject *
423422create_task_result (
424423 RemoteUnwinderObject * unwinder ,
425- uintptr_t task_address
424+ uintptr_t task_address ,
425+ char is_task
426426) {
427427 PyObject * result = NULL ;
428428 PyObject * call_stack = NULL ;
429429 PyObject * tn = NULL ;
430- char task_obj [SIZEOF_TASK_OBJ ];
431- uintptr_t coro_addr ;
432430
433431 // Create call_stack first since it's the first tuple element
434432 call_stack = PyList_New (0 );
@@ -444,25 +442,27 @@ create_task_result(
444442 goto error ;
445443 }
446444
447- // Parse coroutine chain
448- if (_Py_RemoteDebug_PagedReadRemoteMemory (& unwinder -> handle , task_address ,
449- (size_t )unwinder -> async_debug_offsets .asyncio_task_object .size ,
450- task_obj ) < 0 ) {
451- set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to read task object for coro chain" );
452- goto error ;
453- }
454-
455- coro_addr = GET_MEMBER_NO_TAG (uintptr_t , task_obj , unwinder -> async_debug_offsets .asyncio_task_object .task_coro );
456-
457- if ((void * )coro_addr != NULL ) {
458- if (parse_coro_chain (unwinder , coro_addr , call_stack ) < 0 ) {
459- set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to parse coroutine chain" );
445+ if (is_task ) {
446+ char task_obj [SIZEOF_TASK_OBJ ];
447+ if (_Py_RemoteDebug_PagedReadRemoteMemory (& unwinder -> handle , task_address ,
448+ (size_t )unwinder -> async_debug_offsets .asyncio_task_object .size ,
449+ task_obj ) < 0 ) {
450+ set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to read task object for coro chain" );
460451 goto error ;
461452 }
462453
463- if (PyList_Reverse (call_stack )) {
464- set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to reverse call stack" );
465- goto error ;
454+ uintptr_t coro_addr = GET_MEMBER_NO_TAG (uintptr_t , task_obj , unwinder -> async_debug_offsets .asyncio_task_object .task_coro );
455+
456+ if ((void * )coro_addr != NULL ) {
457+ if (parse_coro_chain (unwinder , coro_addr , call_stack ) < 0 ) {
458+ set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to parse coroutine chain" );
459+ goto error ;
460+ }
461+
462+ if (PyList_Reverse (call_stack )) {
463+ set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to reverse call stack" );
464+ goto error ;
465+ }
466466 }
467467 }
468468
@@ -506,33 +506,10 @@ parse_task(
506506 goto error ;
507507 }
508508
509- if (is_task ) {
510- result = create_task_result (unwinder , task_address );
511- if (!result ) {
512- set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to create task result" );
513- goto error ;
514- }
515- } else {
516- // Create an empty CoroInfo for non-task objects
517- RemoteDebuggingState * state = RemoteDebugging_GetStateFromObject ((PyObject * )unwinder );
518- result = PyStructSequence_New (state -> CoroInfo_Type );
519- if (result == NULL ) {
520- set_exception_cause (unwinder , PyExc_MemoryError , "Failed to create empty CoroInfo" );
521- goto error ;
522- }
523- PyObject * empty_list = PyList_New (0 );
524- if (empty_list == NULL ) {
525- set_exception_cause (unwinder , PyExc_MemoryError , "Failed to create empty list" );
526- goto error ;
527- }
528- PyObject * task_name = PyLong_FromUnsignedLongLong (task_address );
529- if (task_name == NULL ) {
530- Py_DECREF (empty_list );
531- set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to create task name" );
532- goto error ;
533- }
534- PyStructSequence_SetItem (result , 0 , empty_list ); // This steals the reference
535- PyStructSequence_SetItem (result , 1 , task_name ); // This steals the reference
509+ result = create_task_result (unwinder , task_address , is_task );
510+ if (result == NULL ) {
511+ set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to create task result" );
512+ goto error ;
536513 }
537514 if (PyList_Append (render_to , result )) {
538515 set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to append task result to render list" );
@@ -798,14 +775,12 @@ parse_async_frame_chain(
798775 return -1 ;
799776 }
800777 PyObject * frame_info = NULL ;
801- uintptr_t address_of_code_object ;
802778
803779 uintptr_t this_frame = address_of_current_frame ;
804780 int res = parse_frame_object (
805781 unwinder ,
806782 & frame_info ,
807783 address_of_current_frame ,
808- & address_of_code_object ,
809784 & address_of_current_frame
810785 );
811786
@@ -894,11 +869,11 @@ append_awaited_by_for_thread(
894869 return 0 ;
895870}
896871
897- int
898- append_awaited_by (
872+ static int
873+ append_awaited_info (
899874 RemoteUnwinderObject * unwinder ,
900875 unsigned long tid ,
901- uintptr_t head_addr ,
876+ PyObject * task_list ,
902877 PyObject * result )
903878{
904879 PyObject * tid_py = PyLong_FromUnsignedLong (tid );
@@ -907,33 +882,47 @@ append_awaited_by(
907882 return -1 ;
908883 }
909884
910- PyObject * awaited_by_for_thread = PyList_New (0 );
911- if (awaited_by_for_thread == NULL ) {
912- Py_DECREF (tid_py );
913- set_exception_cause (unwinder , PyExc_MemoryError , "Failed to create awaited_by thread list" );
914- return -1 ;
915- }
916-
917885 RemoteDebuggingState * state = RemoteDebugging_GetStateFromObject ((PyObject * )unwinder );
918886 PyObject * result_item = PyStructSequence_New (state -> AwaitedInfo_Type );
919887 if (result_item == NULL ) {
920888 Py_DECREF (tid_py );
921- Py_DECREF (awaited_by_for_thread );
922889 set_exception_cause (unwinder , PyExc_MemoryError , "Failed to create AwaitedInfo" );
923890 return -1 ;
924891 }
925892
926- PyStructSequence_SetItem (result_item , 0 , tid_py ); // steals ref
927- PyStructSequence_SetItem (result_item , 1 , awaited_by_for_thread ); // steals ref
893+ PyStructSequence_SetItem (result_item , 0 , tid_py );
894+ PyStructSequence_SetItem (result_item , 1 , Py_NewRef ( task_list ));
928895 if (PyList_Append (result , result_item )) {
929896 Py_DECREF (result_item );
930897 set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to append awaited_by result item" );
931898 return -1 ;
932899 }
933900 Py_DECREF (result_item );
934901
935- if (append_awaited_by_for_thread (unwinder , head_addr , awaited_by_for_thread ))
936- {
902+ return 0 ;
903+ }
904+
905+ int
906+ append_awaited_by (
907+ RemoteUnwinderObject * unwinder ,
908+ unsigned long tid ,
909+ uintptr_t head_addr ,
910+ PyObject * result )
911+ {
912+ PyObject * task_list = PyList_New (0 );
913+ if (task_list == NULL ) {
914+ set_exception_cause (unwinder , PyExc_MemoryError , "Failed to create awaited_by thread list" );
915+ return -1 ;
916+ }
917+
918+ if (append_awaited_info (unwinder , tid , task_list , result ) < 0 ) {
919+ Py_DECREF (task_list );
920+ return -1 ;
921+ }
922+
923+ int err = append_awaited_by_for_thread (unwinder , head_addr , task_list );
924+ Py_DECREF (task_list );
925+ if (err < 0 ) {
937926 set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to append awaited_by for thread" );
938927 return -1 ;
939928 }
@@ -1038,32 +1027,9 @@ process_thread_for_async_stack_trace(
10381027 return -1 ;
10391028 }
10401029
1041- // Create AwaitedInfo structure for this thread
1042- PyObject * tid_py = PyLong_FromUnsignedLong (tid );
1043- if (tid_py == NULL ) {
1044- Py_DECREF (task_list );
1045- set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to create thread ID" );
1046- return -1 ;
1047- }
1048-
1049- RemoteDebuggingState * state = RemoteDebugging_GetStateFromObject ((PyObject * )unwinder );
1050- PyObject * awaited_info = PyStructSequence_New (state -> AwaitedInfo_Type );
1051- if (awaited_info == NULL ) {
1052- Py_DECREF (tid_py );
1053- Py_DECREF (task_list );
1054- set_exception_cause (unwinder , PyExc_MemoryError , "Failed to create AwaitedInfo" );
1055- return -1 ;
1056- }
1057-
1058- PyStructSequence_SetItem (awaited_info , 0 , tid_py ); // steals ref
1059- PyStructSequence_SetItem (awaited_info , 1 , task_list ); // steals ref
1060-
1061- if (PyList_Append (result , awaited_info )) {
1062- Py_DECREF (awaited_info );
1063- set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to append AwaitedInfo to result" );
1064- return -1 ;
1065- }
1066- Py_DECREF (awaited_info );
1030+ int err = append_awaited_info (unwinder , tid , task_list , result );
1031+ Py_DECREF (task_list );
1032+ return err ;
10671033 }
10681034
10691035 return 0 ;
0 commit comments