diff gdb_remote.c @ 2107:f80c6111e1ae

Move some debugger state to a per-CPU structure. Add m command for returning to main CPU from sub CPU
author Michael Pavone <pavone@retrodev.com>
date Sat, 12 Feb 2022 15:20:43 -0800
parents 3a46ff899fa6
children 8554751f17b5
line wrap: on
line diff
--- a/gdb_remote.c	Sat Feb 12 14:50:59 2022 -0800
+++ b/gdb_remote.c	Sat Feb 12 15:20:43 2022 -0800
@@ -50,13 +50,6 @@
 uint32_t resume_pc;
 
 
-static uint16_t branch_t;
-static uint16_t branch_f;
-
-static bp_def * breakpoints = NULL;
-static uint32_t bp_index = 0;
-
-
 void hex_32(uint32_t num, char * out)
 {
 	for (int32_t shift = 28; shift >= 0; shift -= 4)
@@ -171,6 +164,10 @@
 {
 	char send_buf[512];
 	dfprintf(stderr, "Received command %s\n", command);
+	debug_root *root = find_root(context);
+	if (!root) {
+		fatal_error("Could not find debug root for CPU %p\n", context);
+	}
 	switch(*command)
 	{
 
@@ -202,13 +199,13 @@
 			after = (read_dma_value((context->aregs[7]+2)/2) << 16) | read_dma_value((context->aregs[7]+2)/2 + 1);
 		} else if(m68k_is_branch(&inst)) {
 			if (inst.op == M68K_BCC && inst.extra.cond != COND_TRUE) {
-				branch_f = after;
-				branch_t = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
-				insert_breakpoint(context, branch_t, gdb_debug_enter);
+				root->branch_f = after;
+				root->branch_t = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
+				insert_breakpoint(context, root->branch_t, gdb_debug_enter);
 			} else if(inst.op == M68K_DBCC && inst.extra.cond != COND_FALSE) {
-				branch_t = after;
-				branch_f = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
-				insert_breakpoint(context, branch_f, gdb_debug_enter);
+				root->branch_t = after;
+				root->branch_f = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
+				insert_breakpoint(context, root->branch_f, gdb_debug_enter);
 			} else {
 				after = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
 			}
@@ -233,10 +230,10 @@
 			uint32_t address = strtoul(command+3, NULL, 16);
 			insert_breakpoint(context, address, gdb_debug_enter);
 			bp_def *new_bp = malloc(sizeof(bp_def));
-			new_bp->next = breakpoints;
+			new_bp->next = root->breakpoints;
 			new_bp->address = address;
-			new_bp->index = bp_index++;
-			breakpoints = new_bp;
+			new_bp->index = root->bp_index++;
+			root->breakpoints = new_bp;
 			gdb_send_command("OK");
 		} else {
 			//watchpoints are not currently supported
@@ -249,7 +246,7 @@
 		if (type < '2') {
 			uint32_t address = strtoul(command+3, NULL, 16);
 			remove_breakpoint(context, address);
-			bp_def **found = find_breakpoint(&breakpoints, address);
+			bp_def **found = find_breakpoint(&root->breakpoints, address);
 			if (*found)
 			{
 				bp_def * to_remove = *found;
@@ -430,13 +427,13 @@
 					after = (read_dma_value((context->aregs[7]+2)/2) << 16) | read_dma_value((context->aregs[7]+2)/2 + 1);
 				} else if(m68k_is_branch(&inst)) {
 					if (inst.op == M68K_BCC && inst.extra.cond != COND_TRUE) {
-						branch_f = after;
-						branch_t = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
-						insert_breakpoint(context, branch_t, gdb_debug_enter);
+						root->branch_f = after;
+						root->branch_t = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
+						insert_breakpoint(context, root->branch_t, gdb_debug_enter);
 					} else if(inst.op == M68K_DBCC && inst.extra.cond != COND_FALSE) {
-						branch_t = after;
-						branch_f = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
-						insert_breakpoint(context, branch_f, gdb_debug_enter);
+						root->branch_t = after;
+						root->branch_f = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
+						insert_breakpoint(context, root->branch_f, gdb_debug_enter);
 					} else {
 						after = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
 					}
@@ -473,21 +470,25 @@
 		gdb_send_command("S05");
 		expect_break_response = 0;
 	}
-	if ((pc & 0xFFFFFF) == branch_t) {
-		bp_def ** f_bp = find_breakpoint(&breakpoints, branch_f);
+	debug_root *root = find_root(context);
+	if (!root) {
+		fatal_error("Could not find debug root for CPU %p\n", context);
+	}
+	if ((pc & 0xFFFFFF) == root->branch_t) {
+		bp_def ** f_bp = find_breakpoint(&root->breakpoints, root->branch_f);
 		if (!*f_bp) {
-			remove_breakpoint(context, branch_f);
+			remove_breakpoint(context, root->branch_f);
 		}
-		branch_t = branch_f = 0;
-	} else if((pc & 0xFFFFFF) == branch_f) {
-		bp_def ** t_bp = find_breakpoint(&breakpoints, branch_t);
+		root->branch_t = root->branch_f = 0;
+	} else if((pc & 0xFFFFFF) == root->branch_f) {
+		bp_def ** t_bp = find_breakpoint(&root->breakpoints, root->branch_t);
 		if (!*t_bp) {
-			remove_breakpoint(context, branch_t);
+			remove_breakpoint(context, root->branch_t);
 		}
-		branch_t = branch_f = 0;
+		root->branch_t = root->branch_f = 0;
 	}
 	//Check if this is a user set breakpoint, or just a temporary one
-	bp_def ** this_bp = find_breakpoint(&breakpoints, pc & 0xFFFFFF);
+	bp_def ** this_bp = find_breakpoint(&root->breakpoints, pc & 0xFFFFFF);
 	if (!*this_bp) {
 		remove_breakpoint(context, pc & 0xFFFFFF);
 	}