Browse Source

gdbserver wait for launched program to stop before attaching
otherwise when we try to get the segments of the program we're debugging, we could get those of the forked process *before* the exec. Which really isn't what we want.

Signed-off-by: Graham MacDonald <grahamamacdonald@gmail.com>

Graham MacDonald 4 years ago
parent
commit
7042eb5726
1 changed files with 18 additions and 1 deletions
  1. 18 1
      sys/src/cmd/gdbserver/gdbserver.c

+ 18 - 1
sys/src/cmd/gdbserver/gdbserver.c

@@ -1310,6 +1310,13 @@ main(int argc, char **argv)
 	}
 
 	if (argc > 0) {
+		uint8_t statbuf[STATMAX];
+		if (stat(argv[0], statbuf, sizeof(statbuf)) < 0) {
+			print("program does not exist %s\n", argv[0]);
+			werrstr("program does not exist %s", argv[0]);
+			return;
+		}
+
 		// Load process 'exe', then attach
 		ks.threadid = fork();
 
@@ -1342,7 +1349,17 @@ main(int argc, char **argv)
 			// Set to 0 if we eventually support creating a new
 			// process - really?
 			attached_to_existing_pid = 0;
-			print("Process %d launched.  Waiting for remote gdb connection...\n", ks.threadid);
+			print("Process %d launched.\n", ks.threadid);
+
+			// Wait for process to exec and enter Stopped state
+			// before trying to attach
+			while (1) {
+				const char *status = getstatus(ks.threadid);
+				if (status && strcmp(status, "Stopped")) {
+					break;
+				}
+			}
+			print("Process stopped.  Waiting for remote gdb connection...");
 			break;
 		}
 	} else if (pid != nil) {