gdb_main.c revision 139778
1139778Simp/*- 2131899Smarcel * Copyright (c) 2004 Marcel Moolenaar 3131899Smarcel * All rights reserved. 4131899Smarcel * 5131899Smarcel * Redistribution and use in source and binary forms, with or without 6131899Smarcel * modification, are permitted provided that the following conditions 7131899Smarcel * are met: 8131899Smarcel * 9131899Smarcel * 1. Redistributions of source code must retain the above copyright 10131899Smarcel * notice, this list of conditions and the following disclaimer. 11131899Smarcel * 2. Redistributions in binary form must reproduce the above copyright 12131899Smarcel * notice, this list of conditions and the following disclaimer in the 13131899Smarcel * documentation and/or other materials provided with the distribution. 14131899Smarcel * 15131899Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16131899Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17131899Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18131899Smarcel * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 19131899Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20131899Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21131899Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22131899Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23131899Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24131899Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25131899Smarcel */ 26131899Smarcel 27131899Smarcel#include <sys/cdefs.h> 28131899Smarcel__FBSDID("$FreeBSD: head/sys/gdb/gdb_main.c 139778 2005-01-06 18:27:30Z imp $"); 29131899Smarcel 30131899Smarcel#include <sys/param.h> 31131899Smarcel#include <sys/systm.h> 32131899Smarcel#include <sys/kdb.h> 33131899Smarcel#include <sys/kernel.h> 34131899Smarcel#include <sys/pcpu.h> 35131899Smarcel#include <sys/proc.h> 36131899Smarcel#include <sys/reboot.h> 37131899Smarcel 38131899Smarcel#include <machine/gdb_machdep.h> 39131899Smarcel#include <machine/kdb.h> 40131899Smarcel 41131899Smarcel#include <gdb/gdb.h> 42131899Smarcel#include <gdb/gdb_int.h> 43131899Smarcel 44131899Smarcelstatic dbbe_init_f gdb_init; 45131899Smarcelstatic dbbe_trap_f gdb_trap; 46131899Smarcel 47131899SmarcelKDB_BACKEND(gdb, gdb_init, NULL, gdb_trap); 48131899Smarcel 49131899SmarcelGDB_DBGPORT(null, NULL, NULL, NULL, NULL, NULL, NULL); 50131899SmarcelSET_DECLARE(gdb_dbgport_set, struct gdb_dbgport); 51131899Smarcel 52131899Smarcelstruct gdb_dbgport *gdb_cur = NULL; 53131899Smarcel 54131899Smarcelstatic int 55131899Smarcelgdb_init(void) 56131899Smarcel{ 57131899Smarcel struct gdb_dbgport *dp, **iter; 58131899Smarcel int cur_pri, pri; 59131899Smarcel 60131899Smarcel gdb_cur = NULL; 61131899Smarcel cur_pri = -1; 62131899Smarcel SET_FOREACH(iter, gdb_dbgport_set) { 63131899Smarcel dp = *iter; 64131899Smarcel pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1; 65131899Smarcel dp->gdb_active = (pri >= 0) ? 0 : -1; 66131899Smarcel if (pri > cur_pri) { 67131899Smarcel cur_pri = pri; 68131899Smarcel gdb_cur = dp; 69131899Smarcel } 70131899Smarcel } 71131899Smarcel if (gdb_cur != NULL) { 72131899Smarcel printf("GDB: debug ports:"); 73131899Smarcel SET_FOREACH(iter, gdb_dbgport_set) { 74131899Smarcel dp = *iter; 75131899Smarcel if (dp->gdb_active == 0) 76131899Smarcel printf(" %s", dp->gdb_name); 77131899Smarcel } 78131899Smarcel printf("\n"); 79131899Smarcel } else 80131899Smarcel printf("GDB: no debug ports present\n"); 81131899Smarcel if (gdb_cur != NULL) { 82131899Smarcel gdb_cur->gdb_init(); 83131899Smarcel printf("GDB: current port: %s\n", gdb_cur->gdb_name); 84131899Smarcel } 85131899Smarcel if (gdb_cur != NULL) 86131899Smarcel cur_pri = (boothowto & RB_GDB) ? 2 : 0; 87131899Smarcel else 88131899Smarcel cur_pri = -1; 89131899Smarcel return (cur_pri); 90131899Smarcel} 91131899Smarcel 92131899Smarcelstatic int 93131899Smarcelgdb_trap(int type, int code) 94131899Smarcel{ 95131899Smarcel struct thread *thr_iter; 96131899Smarcel 97131899Smarcel /* 98131899Smarcel * Send a T packet. We currently do not support watchpoints (the 99131899Smarcel * awatch, rwatch or watch elements). 100131899Smarcel */ 101131899Smarcel gdb_tx_begin('T'); 102131899Smarcel gdb_tx_hex(gdb_cpu_signal(type, code), 2); 103131899Smarcel gdb_tx_varhex(GDB_REG_PC); 104131899Smarcel gdb_tx_char(':'); 105131899Smarcel gdb_tx_reg(GDB_REG_PC); 106131899Smarcel gdb_tx_char(';'); 107131899Smarcel gdb_tx_str("thread:"); 108131899Smarcel gdb_tx_varhex((long)kdb_thread->td_tid); 109131899Smarcel gdb_tx_char(';'); 110131899Smarcel gdb_tx_end(); /* XXX check error condition. */ 111131899Smarcel 112131899Smarcel thr_iter = NULL; 113131899Smarcel while (gdb_rx_begin() == 0) { 114133446Smarcel /* printf("GDB: got '%s'\n", gdb_rxp); */ 115131899Smarcel switch (gdb_rx_char()) { 116131899Smarcel case '?': /* Last signal. */ 117131899Smarcel gdb_tx_begin('S'); 118131899Smarcel gdb_tx_hex(gdb_cpu_signal(type, code), 2); 119131899Smarcel gdb_tx_end(); 120131899Smarcel break; 121131899Smarcel case 'c': { /* Continue. */ 122131899Smarcel uintmax_t addr; 123138253Smarcel register_t pc; 124138253Smarcel if (!gdb_rx_varhex(&addr)) { 125138253Smarcel pc = addr; 126138253Smarcel gdb_cpu_setreg(GDB_REG_PC, &pc); 127138253Smarcel } 128131899Smarcel kdb_cpu_clear_singlestep(); 129131899Smarcel return (1); 130131899Smarcel } 131131899Smarcel case 'C': { /* Continue with signal. */ 132131899Smarcel uintmax_t addr, sig; 133138253Smarcel register_t pc; 134131899Smarcel if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 135138253Smarcel !gdb_rx_varhex(&addr)) { 136138253Smarcel pc = addr; 137138253Smarcel gdb_cpu_setreg(GDB_REG_PC, &pc); 138138253Smarcel } 139131899Smarcel kdb_cpu_clear_singlestep(); 140131899Smarcel return (1); 141131899Smarcel } 142131899Smarcel case 'g': { /* Read registers. */ 143131899Smarcel size_t r; 144131899Smarcel gdb_tx_begin(0); 145131899Smarcel for (r = 0; r < GDB_NREGS; r++) 146131899Smarcel gdb_tx_reg(r); 147131899Smarcel gdb_tx_end(); 148131899Smarcel break; 149131899Smarcel } 150131899Smarcel case 'G': /* Write registers. */ 151131899Smarcel gdb_tx_err(0); 152131899Smarcel break; 153131899Smarcel case 'H': { /* Set thread. */ 154131899Smarcel intmax_t tid; 155131899Smarcel struct thread *thr; 156131899Smarcel gdb_rx_char(); 157131899Smarcel gdb_rx_varhex(&tid); 158131899Smarcel if (tid > 0) { 159131899Smarcel thr = kdb_thr_lookup(tid); 160131899Smarcel if (thr == NULL) { 161131899Smarcel gdb_tx_err(ENOENT); 162131899Smarcel break; 163131899Smarcel } 164131899Smarcel kdb_thr_select(thr); 165131899Smarcel } 166131899Smarcel gdb_tx_ok(); 167131899Smarcel break; 168131899Smarcel } 169131899Smarcel case 'k': /* Kill request. */ 170131899Smarcel kdb_cpu_clear_singlestep(); 171131899Smarcel return (1); 172131899Smarcel case 'm': { /* Read memory. */ 173131899Smarcel uintmax_t addr, size; 174131899Smarcel if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 175131899Smarcel gdb_rx_varhex(&size)) { 176131899Smarcel gdb_tx_err(EINVAL); 177131899Smarcel break; 178131899Smarcel } 179131899Smarcel gdb_tx_begin(0); 180131899Smarcel if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 181131899Smarcel gdb_tx_end(); 182131899Smarcel else 183131899Smarcel gdb_tx_err(EIO); 184131899Smarcel break; 185131899Smarcel } 186131899Smarcel case 'M': { /* Write memory. */ 187131899Smarcel uintmax_t addr, size; 188131899Smarcel if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 189131899Smarcel gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 190131899Smarcel gdb_tx_err(EINVAL); 191131899Smarcel break; 192131899Smarcel } 193131899Smarcel if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 194131899Smarcel gdb_tx_err(EIO); 195131899Smarcel else 196131899Smarcel gdb_tx_ok(); 197131899Smarcel break; 198131899Smarcel } 199131899Smarcel case 'P': { /* Write register. */ 200138253Smarcel char *val; 201138253Smarcel uintmax_t reg; 202138253Smarcel val = gdb_rxp; 203131899Smarcel if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 204138253Smarcel !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 205131899Smarcel gdb_tx_err(EINVAL); 206131899Smarcel break; 207131899Smarcel } 208131899Smarcel gdb_cpu_setreg(reg, val); 209131899Smarcel gdb_tx_ok(); 210131899Smarcel break; 211131899Smarcel } 212131899Smarcel case 'q': /* General query. */ 213131899Smarcel if (gdb_rx_equal("fThreadInfo")) { 214131899Smarcel thr_iter = kdb_thr_first(); 215131899Smarcel gdb_tx_begin('m'); 216131899Smarcel gdb_tx_hex((long)thr_iter->td_tid, 8); 217131899Smarcel gdb_tx_end(); 218131899Smarcel } else if (gdb_rx_equal("sThreadInfo")) { 219131899Smarcel if (thr_iter == NULL) { 220131899Smarcel gdb_tx_err(ENXIO); 221131899Smarcel break; 222131899Smarcel } 223131899Smarcel thr_iter = kdb_thr_next(thr_iter); 224131899Smarcel if (thr_iter != NULL) { 225131899Smarcel gdb_tx_begin('m'); 226131899Smarcel gdb_tx_hex((long)thr_iter->td_tid, 8); 227131899Smarcel gdb_tx_end(); 228131899Smarcel } else { 229131899Smarcel gdb_tx_begin('l'); 230131899Smarcel gdb_tx_end(); 231131899Smarcel } 232131899Smarcel } else if (!gdb_cpu_query()) 233131899Smarcel gdb_tx_empty(); 234131899Smarcel break; 235131899Smarcel case 's': { /* Step. */ 236131899Smarcel uintmax_t addr; 237138253Smarcel register_t pc; 238138253Smarcel if (!gdb_rx_varhex(&addr)) { 239138253Smarcel pc = addr; 240138253Smarcel gdb_cpu_setreg(GDB_REG_PC, &pc); 241138253Smarcel } 242131899Smarcel kdb_cpu_set_singlestep(); 243131899Smarcel return (1); 244131899Smarcel } 245131899Smarcel case 'S': { /* Step with signal. */ 246131899Smarcel uintmax_t addr, sig; 247138253Smarcel register_t pc; 248131899Smarcel if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 249138253Smarcel !gdb_rx_varhex(&addr)) { 250138253Smarcel pc = addr; 251138253Smarcel gdb_cpu_setreg(GDB_REG_PC, &pc); 252138253Smarcel } 253131899Smarcel kdb_cpu_set_singlestep(); 254131899Smarcel return (1); 255131899Smarcel } 256131899Smarcel case 'T': { /* Thread alive. */ 257131899Smarcel intmax_t tid; 258131899Smarcel gdb_rx_varhex(&tid); 259131899Smarcel if (kdb_thr_lookup(tid) != NULL) 260131899Smarcel gdb_tx_ok(); 261131899Smarcel else 262131899Smarcel gdb_tx_err(ENOENT); 263131899Smarcel break; 264131899Smarcel } 265131899Smarcel case -1: 266131899Smarcel /* Empty command. Treat as unknown command. */ 267131899Smarcel /* FALLTHROUGH */ 268131899Smarcel default: 269131899Smarcel /* Unknown command. Send empty response. */ 270131899Smarcel gdb_tx_empty(); 271131899Smarcel break; 272131899Smarcel } 273131899Smarcel } 274131899Smarcel return (0); 275131899Smarcel} 276