From 2f1e0660ef0a3dcfa35c8003b0806bae3acd411d Mon Sep 17 00:00:00 2001 From: Ben Wagner Date: Tue, 11 Feb 2020 14:45:46 -0500 Subject: Add timeout and interrupt handling to debugserver. Fix debugserver_client_handle_response --- tools/idevicedebug.c | 100 ++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 66 deletions(-) (limited to 'tools/idevicedebug.c') diff --git a/tools/idevicedebug.c b/tools/idevicedebug.c index ca4cb18..4c1ca89 100644 --- a/tools/idevicedebug.c +++ b/tools/idevicedebug.c @@ -61,6 +61,11 @@ static void on_signal(int sig) quit_flag++; } +static void cancel_receive() +{ + return quit_flag; +} + static instproxy_error_t instproxy_client_get_object_by_key_from_info_dictionary_for_bundle_identifier(instproxy_client_t client, const char* appid, const char* key, plist_t* node) { if (!client || !appid || !key) @@ -108,10 +113,9 @@ static instproxy_error_t instproxy_client_get_object_by_key_from_info_dictionary return INSTPROXY_E_SUCCESS; } -static debugserver_error_t debugserver_client_handle_response(debugserver_client_t client, char** response, int send_reply, int* exit_status) +static debugserver_error_t debugserver_client_handle_response(debugserver_client_t client, char** response, int* exit_status) { debugserver_error_t dres = DEBUGSERVER_E_SUCCESS; - debugserver_command_t command = NULL; char* o = NULL; char* r = *response; @@ -123,49 +127,13 @@ static debugserver_error_t debugserver_client_handle_response(debugserver_client debugserver_decode_string(r + 1, strlen(r) - 1, &o); printf("%s", o); fflush(stdout); - if (o != NULL) { - free(o); - o = NULL; - } - - free(*response); - *response = NULL; - - if (!send_reply) - return dres; - - /* send reply */ - debugserver_command_new("OK", 0, NULL, &command); - dres = debugserver_client_send_command(client, command, response, NULL); - log_debug("result: %d", dres); - debugserver_command_free(command); - command = NULL; } else if (r[0] == 'T') { /* thread stopped information */ log_debug("Thread stopped. Details:\n%s", r + 1); - - free(*response); - *response = NULL; - - if (!send_reply) - return dres; - + /* Break out of the loop. */ dres = DEBUGSERVER_E_UNKNOWN_ERROR; } else if (r[0] == 'E') { printf("ERROR: %s\n", r + 1); - - free(*response); - *response = NULL; - - if (!send_reply) - return dres; - - /* send reply */ - debugserver_command_new("OK", 0, NULL, &command); - dres = debugserver_client_send_command(client, command, response, NULL); - log_debug("result: %d", dres); - debugserver_command_free(command); - command = NULL; } else if (r[0] == 'W' || r[0] == 'X') { /* process exited */ debugserver_decode_string(r + 1, strlen(r) - 1, &o); @@ -173,29 +141,23 @@ static debugserver_error_t debugserver_client_handle_response(debugserver_client printf("Exit %s: %u\n", (r[0] == 'W' ? "status" : "due to signal"), o[0]); /* Use bash convention where signals cause an exit status of 128 + signal */ *exit_status = o[0] + (r[0] == 'W' ? 0 : 128); - free(o); - o = NULL; - } - free(*response); - *response = NULL; - return dres; + } else { + debug_info("Unable to decode exit status from %s", r); + dres = DEBUGSERVER_E_UNKNOWN_ERROR; + } } else if (r && strlen(r) == 0) { - if (!send_reply) - return dres; - - free(*response); - *response = NULL; - - /* no command */ - debugserver_command_new("OK", 0, NULL, &command); - dres = debugserver_client_send_command(client, command, response, NULL); - log_debug("result: %d", dres); - debugserver_command_free(command); - command = NULL; + log_debug("empty response"); } else { log_debug("ERROR: unhandled response '%s'", r); } + if (o != NULL) { + free(o); + o = NULL; + } + + free(*response); + *response = NULL; return dres; } @@ -383,6 +345,12 @@ int main(int argc, char *argv[]) goto cleanup; } + /* set receive params */ + if (debugserver_client_set_receive_params(debugserver_client, cancel_receive, 250) != DEBUGSERVER_E_SUCCESS) { + fprintf(stderr, "Error in debugserver_client_set_receive_params\n"); + goto cleanup; + } + /* enable logging for the session in debug mode */ if (debug_level) { log_debug("Setting logging bitmask..."); @@ -392,7 +360,7 @@ int main(int argc, char *argv[]) command = NULL; if (response) { if (strncmp(response, "OK", 2)) { - debugserver_client_handle_response(debugserver_client, &response, 0, NULL); + debugserver_client_handle_response(debugserver_client, &response, NULL); goto cleanup; } free(response); @@ -410,7 +378,7 @@ int main(int argc, char *argv[]) command = NULL; if (response) { if (strncmp(response, "OK", 2)) { - debugserver_client_handle_response(debugserver_client, &response, 0, NULL); + debugserver_client_handle_response(debugserver_client, &response, NULL); goto cleanup; } free(response); @@ -426,7 +394,7 @@ int main(int argc, char *argv[]) command = NULL; if (response) { if (strncmp(response, "OK", 2)) { - debugserver_client_handle_response(debugserver_client, &response, 0, NULL); + debugserver_client_handle_response(debugserver_client, &response, NULL); goto cleanup; } free(response); @@ -467,7 +435,7 @@ int main(int argc, char *argv[]) command = NULL; if (response) { if (strncmp(response, "OK", 2)) { - debugserver_client_handle_response(debugserver_client, &response, 0, NULL); + debugserver_client_handle_response(debugserver_client, &response, NULL); goto cleanup; } free(response); @@ -493,7 +461,7 @@ int main(int argc, char *argv[]) command = NULL; if (response) { if (strncmp(response, "OK", 2)) { - debugserver_client_handle_response(debugserver_client, &response, 0, NULL); + debugserver_client_handle_response(debugserver_client, &response, NULL); goto cleanup; } free(response); @@ -511,21 +479,21 @@ int main(int argc, char *argv[]) log_debug("Entering run loop..."); while (!quit_flag) { if (dres != DEBUGSERVER_E_SUCCESS) { - log_debug("failed to receive response"); + log_debug("failed to receive response; error %d", dres); break; } if (response) { log_debug("response: %s", response); if (strncmp(response, "OK", 2)) { - dres = debugserver_client_handle_response(debugserver_client, &response, 1, &res); + dres = debugserver_client_handle_response(debugserver_client, &response, &res); } } if (res >= 0) { goto cleanup; } - sleep(1); + dres = debugserver_client_receive(debugserver_client, &response, NULL); } /* kill process after we finished */ @@ -536,7 +504,7 @@ int main(int argc, char *argv[]) command = NULL; if (response) { if (strncmp(response, "OK", 2)) { - debugserver_client_handle_response(debugserver_client, &response, 0, NULL); + debugserver_client_handle_response(debugserver_client, &response, NULL); goto cleanup; } free(response); -- cgit v1.1-32-gdbae