Skip to content

Commit c8779cd

Browse files
committed
Refactor to avoid duplicate error handling
1 parent 5c0b8b9 commit c8779cd

File tree

1 file changed

+32
-50
lines changed

1 file changed

+32
-50
lines changed

Python/remote_debugging.c

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -691,70 +691,45 @@ read_offsets(
691691
return 0;
692692
}
693693

694-
int
695-
_PySysRemoteDebug_SendExec(int pid, int tid, const char *debugger_script_path)
694+
static int
695+
send_exec_to_proc_handle(proc_handle_t *handle, int tid, const char *debugger_script_path)
696696
{
697-
#if (!defined(__linux__) && !defined(__APPLE__) && !defined(MS_WINDOWS)) || (defined(__linux__) && !HAVE_PROCESS_VM_READV)
698-
PyErr_SetString(PyExc_RuntimeError, "Remote debugging is not supported on this platform");
699-
return -1;
700-
#endif
701-
702-
proc_handle_t handle;
703-
if (init_proc_handle(&handle, pid) < 0) {
704-
return -1;
705-
}
706-
707-
#ifdef MS_WINDOWS
708-
if (debugger_script_path != NULL && strlen(debugger_script_path) > MAX_PATH) {
709-
#else
710-
if (debugger_script_path != NULL && strlen(debugger_script_path) > PATH_MAX) {
711-
#endif
712-
PyErr_SetString(PyExc_ValueError, "Debugger script path is too long");
713-
cleanup_proc_handle(&handle);
714-
return -1;
715-
}
716-
717697
uintptr_t runtime_start_address;
718698
struct _Py_DebugOffsets local_debug_offsets;
719699

720-
if (read_offsets(&handle, &runtime_start_address, &local_debug_offsets)) {
721-
cleanup_proc_handle(&handle);
700+
if (read_offsets(handle, &runtime_start_address, &local_debug_offsets)) {
722701
return -1;
723702
}
724703

725704
uintptr_t interpreter_state_list_head = local_debug_offsets.runtime_state.interpreters_head;
726705

727706
uintptr_t address_of_interpreter_state;
728707
Py_ssize_t bytes = read_memory(
729-
&handle,
708+
handle,
730709
runtime_start_address + interpreter_state_list_head,
731710
sizeof(void*),
732711
&address_of_interpreter_state);
733712
if (bytes == -1) {
734-
cleanup_proc_handle(&handle);
735713
return -1;
736714
}
737715

738716
if (address_of_interpreter_state == 0) {
739717
PyErr_SetString(PyExc_RuntimeError, "No interpreter state found");
740-
cleanup_proc_handle(&handle);
741718
return -1;
742719
}
743720

744721
int is_remote_debugging_enabled = 0;
745722
bytes = read_memory(
746-
&handle,
723+
handle,
747724
address_of_interpreter_state + local_debug_offsets.debugger_support.remote_debugging_enabled,
748725
sizeof(int),
749726
&is_remote_debugging_enabled);
750727
if (bytes == -1) {
751-
cleanup_proc_handle(&handle);
752728
return -1;
753729
}
754730

755731
if (is_remote_debugging_enabled == 0) {
756732
PyErr_SetString(PyExc_RuntimeError, "Remote debugging is not enabled in the remote process");
757-
cleanup_proc_handle(&handle);
758733
return -1;
759734
}
760735

@@ -763,22 +738,20 @@ _PySysRemoteDebug_SendExec(int pid, int tid, const char *debugger_script_path)
763738

764739
if (tid != 0) {
765740
bytes = read_memory(
766-
&handle,
741+
handle,
767742
address_of_interpreter_state + local_debug_offsets.interpreter_state.threads_head,
768743
sizeof(void*),
769744
&address_of_thread);
770745
if (bytes == -1) {
771-
cleanup_proc_handle(&handle);
772746
return -1;
773747
}
774748
while (address_of_thread != 0) {
775749
bytes = read_memory(
776-
&handle,
750+
handle,
777751
address_of_thread + local_debug_offsets.thread_state.native_thread_id,
778752
sizeof(pid_t),
779753
&this_tid);
780754
if (bytes == -1) {
781-
cleanup_proc_handle(&handle);
782755
return -1;
783756
}
784757

@@ -787,41 +760,37 @@ _PySysRemoteDebug_SendExec(int pid, int tid, const char *debugger_script_path)
787760
}
788761

789762
bytes = read_memory(
790-
&handle,
763+
handle,
791764
address_of_thread + local_debug_offsets.thread_state.next,
792765
sizeof(void*),
793766
&address_of_thread);
794767
if (bytes == -1) {
795-
cleanup_proc_handle(&handle);
796768
return -1;
797769
}
798770
}
799771
} else {
800772
bytes = read_memory(
801-
&handle,
773+
handle,
802774
address_of_interpreter_state + local_debug_offsets.interpreter_state.threads_main,
803775
sizeof(void*),
804776
&address_of_thread);
805777
if (bytes == -1) {
806-
cleanup_proc_handle(&handle);
807778
return -1;
808779
}
809780
}
810781

811782
if (address_of_thread == 0) {
812783
PyErr_SetString(PyExc_RuntimeError, "No thread state found");
813-
cleanup_proc_handle(&handle);
814784
return -1;
815785
}
816786

817787
uintptr_t eval_breaker;
818788
bytes = read_memory(
819-
&handle,
789+
handle,
820790
address_of_thread + local_debug_offsets.debugger_support.eval_breaker,
821791
sizeof(uintptr_t),
822792
&eval_breaker);
823793
if (bytes == -1) {
824-
cleanup_proc_handle(&handle);
825794
return -1;
826795
}
827796

@@ -830,7 +799,6 @@ _PySysRemoteDebug_SendExec(int pid, int tid, const char *debugger_script_path)
830799
// Ensure our path is not too long
831800
if (local_debug_offsets.debugger_support.debugger_script_path_size <= strlen(debugger_script_path)) {
832801
PyErr_SetString(PyExc_ValueError, "Debugger script path is too long");
833-
cleanup_proc_handle(&handle);
834802
return -1;
835803
}
836804

@@ -840,12 +808,11 @@ _PySysRemoteDebug_SendExec(int pid, int tid, const char *debugger_script_path)
840808
local_debug_offsets.debugger_support.remote_debugger_support +
841809
local_debug_offsets.debugger_support.debugger_script_path);
842810
bytes = write_memory(
843-
&handle,
811+
handle,
844812
debugger_script_path_addr,
845813
strlen(debugger_script_path) + 1,
846814
debugger_script_path);
847815
if (bytes == -1) {
848-
cleanup_proc_handle(&handle);
849816
return -1;
850817
}
851818
}
@@ -856,35 +823,50 @@ _PySysRemoteDebug_SendExec(int pid, int tid, const char *debugger_script_path)
856823
local_debug_offsets.debugger_support.remote_debugger_support +
857824
local_debug_offsets.debugger_support.debugger_pending_call);
858825
bytes = write_memory(
859-
&handle,
826+
handle,
860827
debugger_pending_call_addr,
861828
sizeof(int),
862829
&pending_call);
863830

864831
if (bytes == -1) {
865-
cleanup_proc_handle(&handle);
866832
return -1;
867833
}
868834

869835
bytes = write_memory(
870-
&handle,
836+
handle,
871837
address_of_thread + local_debug_offsets.debugger_support.eval_breaker,
872838
sizeof(uintptr_t),
873839
&eval_breaker);
874840

875841
if (bytes == -1) {
876-
cleanup_proc_handle(&handle);
877842
return -1;
878843
}
879844

880845
bytes = read_memory(
881-
&handle,
846+
handle,
882847
address_of_thread + local_debug_offsets.debugger_support.eval_breaker,
883848
sizeof(uintptr_t),
884849
&eval_breaker);
885850

886851
printf("Eval breaker: %p\n", (void*)eval_breaker);
887852

888-
cleanup_proc_handle(&handle);
889853
return 0;
854+
}
855+
856+
int
857+
_PySysRemoteDebug_SendExec(int pid, int tid, const char *debugger_script_path)
858+
{
859+
#if (!defined(__linux__) && !defined(__APPLE__) && !defined(MS_WINDOWS)) || (defined(__linux__) && !HAVE_PROCESS_VM_READV)
860+
PyErr_SetString(PyExc_RuntimeError, "Remote debugging is not supported on this platform");
861+
return -1;
862+
#endif
863+
864+
proc_handle_t handle;
865+
if (init_proc_handle(&handle, pid) < 0) {
866+
return -1;
890867
}
868+
869+
int rc = send_exec_to_proc_handle(&handle, tid, debugger_script_path);
870+
cleanup_proc_handle(&handle);
871+
return rc;
872+
}

0 commit comments

Comments
 (0)