gdb_main.c revision 157059
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 157059 2006-03-23 23:06:14Z sam $"); 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; 53157059Ssamint gdb_listening = 0; 54131899Smarcel 55131899Smarcelstatic int 56131899Smarcelgdb_init(void) 57131899Smarcel{ 58131899Smarcel struct gdb_dbgport *dp, **iter; 59131899Smarcel int cur_pri, pri; 60131899Smarcel 61131899Smarcel gdb_cur = NULL; 62131899Smarcel cur_pri = -1; 63131899Smarcel SET_FOREACH(iter, gdb_dbgport_set) { 64131899Smarcel dp = *iter; 65131899Smarcel pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1; 66131899Smarcel dp->gdb_active = (pri >= 0) ? 0 : -1; 67131899Smarcel if (pri > cur_pri) { 68131899Smarcel cur_pri = pri; 69131899Smarcel gdb_cur = dp; 70131899Smarcel } 71131899Smarcel } 72131899Smarcel if (gdb_cur != NULL) { 73131899Smarcel printf("GDB: debug ports:"); 74131899Smarcel SET_FOREACH(iter, gdb_dbgport_set) { 75131899Smarcel dp = *iter; 76131899Smarcel if (dp->gdb_active == 0) 77131899Smarcel printf(" %s", dp->gdb_name); 78131899Smarcel } 79131899Smarcel printf("\n"); 80131899Smarcel } else 81131899Smarcel printf("GDB: no debug ports present\n"); 82131899Smarcel if (gdb_cur != NULL) { 83131899Smarcel gdb_cur->gdb_init(); 84131899Smarcel printf("GDB: current port: %s\n", gdb_cur->gdb_name); 85131899Smarcel } 86157059Ssam if (gdb_cur != NULL) { 87131899Smarcel cur_pri = (boothowto & RB_GDB) ? 2 : 0; 88157059Ssam gdb_consinit(); 89157059Ssam } else 90131899Smarcel cur_pri = -1; 91131899Smarcel return (cur_pri); 92131899Smarcel} 93131899Smarcel 94131899Smarcelstatic int 95131899Smarcelgdb_trap(int type, int code) 96131899Smarcel{ 97131899Smarcel struct thread *thr_iter; 98131899Smarcel 99157059Ssam gdb_listening = 0; 100131899Smarcel /* 101131899Smarcel * Send a T packet. We currently do not support watchpoints (the 102131899Smarcel * awatch, rwatch or watch elements). 103131899Smarcel */ 104131899Smarcel gdb_tx_begin('T'); 105131899Smarcel gdb_tx_hex(gdb_cpu_signal(type, code), 2); 106131899Smarcel gdb_tx_varhex(GDB_REG_PC); 107131899Smarcel gdb_tx_char(':'); 108131899Smarcel gdb_tx_reg(GDB_REG_PC); 109131899Smarcel gdb_tx_char(';'); 110131899Smarcel gdb_tx_str("thread:"); 111131899Smarcel gdb_tx_varhex((long)kdb_thread->td_tid); 112131899Smarcel gdb_tx_char(';'); 113131899Smarcel gdb_tx_end(); /* XXX check error condition. */ 114131899Smarcel 115131899Smarcel thr_iter = NULL; 116131899Smarcel while (gdb_rx_begin() == 0) { 117133446Smarcel /* printf("GDB: got '%s'\n", gdb_rxp); */ 118131899Smarcel switch (gdb_rx_char()) { 119131899Smarcel case '?': /* Last signal. */ 120131899Smarcel gdb_tx_begin('S'); 121131899Smarcel gdb_tx_hex(gdb_cpu_signal(type, code), 2); 122131899Smarcel gdb_tx_end(); 123131899Smarcel break; 124131899Smarcel case 'c': { /* Continue. */ 125131899Smarcel uintmax_t addr; 126138253Smarcel register_t pc; 127138253Smarcel if (!gdb_rx_varhex(&addr)) { 128138253Smarcel pc = addr; 129138253Smarcel gdb_cpu_setreg(GDB_REG_PC, &pc); 130138253Smarcel } 131131899Smarcel kdb_cpu_clear_singlestep(); 132157059Ssam gdb_listening = 1; 133131899Smarcel return (1); 134131899Smarcel } 135131899Smarcel case 'C': { /* Continue with signal. */ 136131899Smarcel uintmax_t addr, sig; 137138253Smarcel register_t pc; 138131899Smarcel if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 139138253Smarcel !gdb_rx_varhex(&addr)) { 140138253Smarcel pc = addr; 141138253Smarcel gdb_cpu_setreg(GDB_REG_PC, &pc); 142138253Smarcel } 143131899Smarcel kdb_cpu_clear_singlestep(); 144157059Ssam gdb_listening = 1; 145131899Smarcel return (1); 146131899Smarcel } 147131899Smarcel case 'g': { /* Read registers. */ 148131899Smarcel size_t r; 149131899Smarcel gdb_tx_begin(0); 150131899Smarcel for (r = 0; r < GDB_NREGS; r++) 151131899Smarcel gdb_tx_reg(r); 152131899Smarcel gdb_tx_end(); 153131899Smarcel break; 154131899Smarcel } 155131899Smarcel case 'G': /* Write registers. */ 156131899Smarcel gdb_tx_err(0); 157131899Smarcel break; 158131899Smarcel case 'H': { /* Set thread. */ 159131899Smarcel intmax_t tid; 160131899Smarcel struct thread *thr; 161131899Smarcel gdb_rx_char(); 162144245Ssam if (gdb_rx_varhex(&tid)) { 163144245Ssam gdb_tx_err(EINVAL); 164144245Ssam break; 165144245Ssam } 166131899Smarcel if (tid > 0) { 167131899Smarcel thr = kdb_thr_lookup(tid); 168131899Smarcel if (thr == NULL) { 169131899Smarcel gdb_tx_err(ENOENT); 170131899Smarcel break; 171131899Smarcel } 172131899Smarcel kdb_thr_select(thr); 173131899Smarcel } 174131899Smarcel gdb_tx_ok(); 175131899Smarcel break; 176131899Smarcel } 177131899Smarcel case 'k': /* Kill request. */ 178131899Smarcel kdb_cpu_clear_singlestep(); 179157059Ssam gdb_listening = 1; 180131899Smarcel return (1); 181131899Smarcel case 'm': { /* Read memory. */ 182131899Smarcel uintmax_t addr, size; 183131899Smarcel if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 184131899Smarcel gdb_rx_varhex(&size)) { 185131899Smarcel gdb_tx_err(EINVAL); 186131899Smarcel break; 187131899Smarcel } 188131899Smarcel gdb_tx_begin(0); 189131899Smarcel if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 190131899Smarcel gdb_tx_end(); 191131899Smarcel else 192131899Smarcel gdb_tx_err(EIO); 193131899Smarcel break; 194131899Smarcel } 195131899Smarcel case 'M': { /* Write memory. */ 196131899Smarcel uintmax_t addr, size; 197131899Smarcel if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 198131899Smarcel gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 199131899Smarcel gdb_tx_err(EINVAL); 200131899Smarcel break; 201131899Smarcel } 202131899Smarcel if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 203131899Smarcel gdb_tx_err(EIO); 204131899Smarcel else 205131899Smarcel gdb_tx_ok(); 206131899Smarcel break; 207131899Smarcel } 208131899Smarcel case 'P': { /* Write register. */ 209138253Smarcel char *val; 210138253Smarcel uintmax_t reg; 211138253Smarcel val = gdb_rxp; 212131899Smarcel if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 213138253Smarcel !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 214131899Smarcel gdb_tx_err(EINVAL); 215131899Smarcel break; 216131899Smarcel } 217131899Smarcel gdb_cpu_setreg(reg, val); 218131899Smarcel gdb_tx_ok(); 219131899Smarcel break; 220131899Smarcel } 221131899Smarcel case 'q': /* General query. */ 222131899Smarcel if (gdb_rx_equal("fThreadInfo")) { 223131899Smarcel thr_iter = kdb_thr_first(); 224131899Smarcel gdb_tx_begin('m'); 225131899Smarcel gdb_tx_hex((long)thr_iter->td_tid, 8); 226131899Smarcel gdb_tx_end(); 227131899Smarcel } else if (gdb_rx_equal("sThreadInfo")) { 228131899Smarcel if (thr_iter == NULL) { 229131899Smarcel gdb_tx_err(ENXIO); 230131899Smarcel break; 231131899Smarcel } 232131899Smarcel thr_iter = kdb_thr_next(thr_iter); 233131899Smarcel if (thr_iter != NULL) { 234131899Smarcel gdb_tx_begin('m'); 235131899Smarcel gdb_tx_hex((long)thr_iter->td_tid, 8); 236131899Smarcel gdb_tx_end(); 237131899Smarcel } else { 238131899Smarcel gdb_tx_begin('l'); 239131899Smarcel gdb_tx_end(); 240131899Smarcel } 241131899Smarcel } else if (!gdb_cpu_query()) 242131899Smarcel gdb_tx_empty(); 243131899Smarcel break; 244131899Smarcel case 's': { /* Step. */ 245131899Smarcel uintmax_t addr; 246138253Smarcel register_t pc; 247138253Smarcel if (!gdb_rx_varhex(&addr)) { 248138253Smarcel pc = addr; 249138253Smarcel gdb_cpu_setreg(GDB_REG_PC, &pc); 250138253Smarcel } 251131899Smarcel kdb_cpu_set_singlestep(); 252157059Ssam gdb_listening = 1; 253131899Smarcel return (1); 254131899Smarcel } 255131899Smarcel case 'S': { /* Step with signal. */ 256131899Smarcel uintmax_t addr, sig; 257138253Smarcel register_t pc; 258131899Smarcel if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 259138253Smarcel !gdb_rx_varhex(&addr)) { 260138253Smarcel pc = addr; 261138253Smarcel gdb_cpu_setreg(GDB_REG_PC, &pc); 262138253Smarcel } 263131899Smarcel kdb_cpu_set_singlestep(); 264157059Ssam gdb_listening = 1; 265131899Smarcel return (1); 266131899Smarcel } 267131899Smarcel case 'T': { /* Thread alive. */ 268131899Smarcel intmax_t tid; 269144245Ssam if (gdb_rx_varhex(&tid)) { 270144245Ssam gdb_tx_err(EINVAL); 271144245Ssam break; 272144245Ssam } 273131899Smarcel if (kdb_thr_lookup(tid) != NULL) 274131899Smarcel gdb_tx_ok(); 275131899Smarcel else 276131899Smarcel gdb_tx_err(ENOENT); 277131899Smarcel break; 278131899Smarcel } 279131899Smarcel case -1: 280131899Smarcel /* Empty command. Treat as unknown command. */ 281131899Smarcel /* FALLTHROUGH */ 282131899Smarcel default: 283131899Smarcel /* Unknown command. Send empty response. */ 284131899Smarcel gdb_tx_empty(); 285131899Smarcel break; 286131899Smarcel } 287131899Smarcel } 288131899Smarcel return (0); 289131899Smarcel} 290