idevicedebug: Process "Wxx" response as "exited with code xx"
diff --git a/tools/idevicedebug.c b/tools/idevicedebug.c
index a9ef5c6..7b49a46 100644
--- a/tools/idevicedebug.c
+++ b/tools/idevicedebug.c
@@ -108,7 +108,7 @@
 	return INSTPROXY_E_SUCCESS;
 }
 
-static debugserver_error_t debugserver_client_handle_response(debugserver_client_t client, char** response, int send_reply)
+static debugserver_error_t debugserver_client_handle_response(debugserver_client_t client, char** response, int send_reply, int* exit_status)
 {
 	debugserver_error_t dres = DEBUGSERVER_E_SUCCESS;
 	debugserver_command_t command = NULL;
@@ -149,12 +149,24 @@
 
 		dres = DEBUGSERVER_E_UNKNOWN_ERROR;
 	} else if (r[0] == 'E' || r[0] == 'W') {
-		printf("%s: %s\n", (r[0] == 'E' ? "ERROR": "WARNING") , r + 1);
-
+		debugserver_decode_string(r + 1, strlen(r) - 1, &o);
+		/* dogben: 'W' + byte seems to mean "process exited with this status." */
+		if (r[0] == 'W' && strlen(o) == 1) {
+			printf("Exit status: %u\n", o[0]);
+			if (exit_status != NULL) {
+				*exit_status = o[0];
+			}
+		} else {
+			printf("%s: %s\n", (r[0] == 'E' ? "ERROR": "WARNING") , o);
+		}
+		if (o != NULL) {
+			free(o);
+			o = NULL;
+		}
 		free(*response);
 		*response = NULL;
 
-		if (!send_reply)
+		if (!send_reply || (exit_status != NULL && *exit_status >= 0))
 			return dres;
 
 		/* send reply */
@@ -229,6 +241,7 @@
 	char* response = NULL;
 	debugserver_command_t command = NULL;
 	debugserver_error_t dres = DEBUGSERVER_E_UNKNOWN_ERROR;
+        int exit_status = -1;
 
 	/* map signals */
 	signal(SIGINT, on_signal);
@@ -376,7 +389,7 @@
 				command = NULL;
 				if (response) {
 					if (strncmp(response, "OK", 2)) {
-						debugserver_client_handle_response(debugserver_client, &response, 0);
+						debugserver_client_handle_response(debugserver_client, &response, 0, NULL);
 						goto cleanup;
 					}
 					free(response);
@@ -394,7 +407,7 @@
 			command = NULL;
 			if (response) {
 				if (strncmp(response, "OK", 2)) {
-					debugserver_client_handle_response(debugserver_client, &response, 0);
+					debugserver_client_handle_response(debugserver_client, &response, 0, NULL);
 					goto cleanup;
 				}
 				free(response);
@@ -410,7 +423,7 @@
 			command = NULL;
 			if (response) {
 				if (strncmp(response, "OK", 2)) {
-					debugserver_client_handle_response(debugserver_client, &response, 0);
+					debugserver_client_handle_response(debugserver_client, &response, 0, NULL);
 					goto cleanup;
 				}
 				free(response);
@@ -451,7 +464,7 @@
 			command = NULL;
 			if (response) {
 				if (strncmp(response, "OK", 2)) {
-					debugserver_client_handle_response(debugserver_client, &response, 0);
+					debugserver_client_handle_response(debugserver_client, &response, 0, NULL);
 					goto cleanup;
 				}
 				free(response);
@@ -477,7 +490,7 @@
 			command = NULL;
 			if (response) {
 				if (strncmp(response, "OK", 2)) {
-					debugserver_client_handle_response(debugserver_client, &response, 0);
+					debugserver_client_handle_response(debugserver_client, &response, 0, NULL);
 					goto cleanup;
 				}
 				free(response);
@@ -502,9 +515,12 @@
 				if (response) {
 					log_debug("response: %s", response);
 					if (strncmp(response, "OK", 2)) {
-						dres = debugserver_client_handle_response(debugserver_client, &response, 1);
+						dres = debugserver_client_handle_response(debugserver_client, &response, 1, &exit_status);
 					}
 				}
+				if (exit_status > 0) {
+					goto cleanup;
+				}
 
 				sleep(1);
 			}
@@ -517,7 +533,7 @@
 			command = NULL;
 			if (response) {
 				if (strncmp(response, "OK", 2)) {
-					debugserver_client_handle_response(debugserver_client, &response, 0);
+					debugserver_client_handle_response(debugserver_client, &response, 0, NULL);
 					goto cleanup;
 				}
 				free(response);
@@ -552,5 +568,9 @@
 	if (device)
 		idevice_free(device);
 
-	return res;
+	if (exit_status > 0) {
+		return exit_status;
+	} else {
+		return res;
+	}
 }