|
| 1 | +/* GDB control layer for coroutine-based multi-hart execution */ |
| 2 | + |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include "coro.h" |
| 6 | +#include "gdbctrl.h" |
| 7 | + |
| 8 | +bool gdb_debug_init(vm_t *vm) |
| 9 | +{ |
| 10 | + if (!vm) |
| 11 | + return false; |
| 12 | + |
| 13 | + /* Initialize debug context */ |
| 14 | + memset(&vm->debug_ctx, 0, sizeof(vm_debug_ctx_t)); |
| 15 | + |
| 16 | + /* Initialize debug info for all harts */ |
| 17 | + for (uint32_t i = 0; i < vm->n_hart; i++) { |
| 18 | + hart_t *hart = vm->hart[i]; |
| 19 | + if (!hart) |
| 20 | + continue; |
| 21 | + |
| 22 | + memset(&hart->debug_info, 0, sizeof(hart_debug_info_t)); |
| 23 | + hart->debug_info.state = HART_STATE_RUNNING; |
| 24 | + hart->debug_info.single_step_mode = false; |
| 25 | + hart->debug_info.breakpoint_pending = false; |
| 26 | + } |
| 27 | + |
| 28 | + return true; |
| 29 | +} |
| 30 | + |
| 31 | +void gdb_debug_cleanup(vm_t *vm) |
| 32 | +{ |
| 33 | + if (!vm) |
| 34 | + return; |
| 35 | + |
| 36 | + /* Clear all breakpoints */ |
| 37 | + gdb_clear_all_breakpoints(vm); |
| 38 | + |
| 39 | + /* Reset all hart debug states */ |
| 40 | + for (uint32_t i = 0; i < vm->n_hart; i++) { |
| 41 | + hart_t *hart = vm->hart[i]; |
| 42 | + if (!hart) |
| 43 | + continue; |
| 44 | + |
| 45 | + hart->debug_info.state = HART_STATE_RUNNING; |
| 46 | + hart->debug_info.single_step_mode = false; |
| 47 | + hart->debug_info.breakpoint_pending = false; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +bool gdb_check_breakpoint(hart_t *hart) |
| 52 | +{ |
| 53 | + if (!hart || !hart->vm) |
| 54 | + return false; |
| 55 | + |
| 56 | + vm_t *vm = hart->vm; |
| 57 | + uint32_t pc = hart->pc; |
| 58 | + |
| 59 | + /* Check if current PC matches any enabled breakpoint */ |
| 60 | + for (uint32_t i = 0; i < vm->debug_ctx.bp_count; i++) { |
| 61 | + breakpoint_t *bp = &vm->debug_ctx.breakpoints[i]; |
| 62 | + if (bp->enabled && bp->addr == pc) { |
| 63 | + /* Breakpoint hit */ |
| 64 | + hart->debug_info.breakpoint_pending = true; |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | +} |
| 71 | + |
| 72 | +bool gdb_set_breakpoint(vm_t *vm, uint32_t addr) |
| 73 | +{ |
| 74 | + if (!vm) |
| 75 | + return false; |
| 76 | + |
| 77 | + /* Check if breakpoint already exists at this address */ |
| 78 | + for (uint32_t i = 0; i < vm->debug_ctx.bp_count; i++) { |
| 79 | + if (vm->debug_ctx.breakpoints[i].addr == addr) { |
| 80 | + /* Already exists, just ensure it's enabled */ |
| 81 | + vm->debug_ctx.breakpoints[i].enabled = true; |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /* Check if we have room for a new breakpoint */ |
| 87 | + if (vm->debug_ctx.bp_count >= MAX_BREAKPOINTS) |
| 88 | + return false; |
| 89 | + |
| 90 | + /* Add new breakpoint */ |
| 91 | + breakpoint_t *bp = &vm->debug_ctx.breakpoints[vm->debug_ctx.bp_count]; |
| 92 | + bp->addr = addr; |
| 93 | + bp->enabled = true; |
| 94 | + vm->debug_ctx.bp_count++; |
| 95 | + |
| 96 | + return true; |
| 97 | +} |
| 98 | + |
| 99 | +bool gdb_del_breakpoint(vm_t *vm, uint32_t addr) |
| 100 | +{ |
| 101 | + if (!vm) |
| 102 | + return false; |
| 103 | + |
| 104 | + /* Find the breakpoint */ |
| 105 | + for (uint32_t i = 0; i < vm->debug_ctx.bp_count; i++) { |
| 106 | + if (vm->debug_ctx.breakpoints[i].addr == addr) { |
| 107 | + /* Found it - remove by shifting remaining breakpoints down */ |
| 108 | + uint32_t remaining = vm->debug_ctx.bp_count - i - 1; |
| 109 | + if (remaining > 0) { |
| 110 | + memmove(&vm->debug_ctx.breakpoints[i], |
| 111 | + &vm->debug_ctx.breakpoints[i + 1], |
| 112 | + remaining * sizeof(breakpoint_t)); |
| 113 | + } |
| 114 | + vm->debug_ctx.bp_count--; |
| 115 | + return true; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return false; |
| 120 | +} |
| 121 | + |
| 122 | +void gdb_clear_all_breakpoints(vm_t *vm) |
| 123 | +{ |
| 124 | + if (!vm) |
| 125 | + return; |
| 126 | + |
| 127 | + memset(&vm->debug_ctx.breakpoints, 0, sizeof(vm->debug_ctx.breakpoints)); |
| 128 | + vm->debug_ctx.bp_count = 0; |
| 129 | +} |
| 130 | + |
| 131 | +uint32_t gdb_get_breakpoint_count(vm_t *vm) |
| 132 | +{ |
| 133 | + if (!vm) |
| 134 | + return 0; |
| 135 | + |
| 136 | + return vm->debug_ctx.bp_count; |
| 137 | +} |
| 138 | + |
| 139 | +void gdb_suspend_hart(hart_t *hart) |
| 140 | +{ |
| 141 | + if (!hart) |
| 142 | + return; |
| 143 | + |
| 144 | + /* Mark hart as suspended for debugging */ |
| 145 | + hart->debug_info.state = HART_STATE_DEBUG_BREAK; |
| 146 | + |
| 147 | + /* Suspend the coroutine (only in SMP mode) */ |
| 148 | + if (hart->vm && hart->vm->n_hart > 1) |
| 149 | + coro_suspend_hart_debug(hart->mhartid); |
| 150 | +} |
| 151 | + |
| 152 | +void gdb_resume_hart(hart_t *hart) |
| 153 | +{ |
| 154 | + if (!hart) |
| 155 | + return; |
| 156 | + |
| 157 | + /* Clear breakpoint pending flag */ |
| 158 | + hart->debug_info.breakpoint_pending = false; |
| 159 | + |
| 160 | + /* If single-step mode is enabled, mark as DEBUG_STEP instead of RUNNING */ |
| 161 | + if (hart->debug_info.single_step_mode) { |
| 162 | + hart->debug_info.state = HART_STATE_DEBUG_STEP; |
| 163 | + } else { |
| 164 | + hart->debug_info.state = HART_STATE_RUNNING; |
| 165 | + } |
| 166 | + |
| 167 | + /* Resume the coroutine (only in SMP mode) */ |
| 168 | + if (hart->vm && hart->vm->n_hart > 1) { |
| 169 | + coro_resume_hart_debug(hart->mhartid); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +void gdb_enable_single_step(hart_t *hart) |
| 174 | +{ |
| 175 | + if (!hart) |
| 176 | + return; |
| 177 | + |
| 178 | + hart->debug_info.single_step_mode = true; |
| 179 | + hart->debug_info.state = HART_STATE_DEBUG_STEP; |
| 180 | +} |
| 181 | + |
| 182 | +void gdb_disable_single_step(hart_t *hart) |
| 183 | +{ |
| 184 | + if (!hart) |
| 185 | + return; |
| 186 | + |
| 187 | + hart->debug_info.single_step_mode = false; |
| 188 | + if (hart->debug_info.state == HART_STATE_DEBUG_STEP) |
| 189 | + hart->debug_info.state = HART_STATE_RUNNING; |
| 190 | +} |
| 191 | + |
| 192 | +bool gdb_is_single_stepping(hart_t *hart) |
| 193 | +{ |
| 194 | + if (!hart) |
| 195 | + return false; |
| 196 | + |
| 197 | + return hart->debug_info.single_step_mode; |
| 198 | +} |
0 commit comments