153197fc4SJason Wessel /* 253197fc4SJason Wessel * Kernel Debug Core 353197fc4SJason Wessel * 453197fc4SJason Wessel * Maintainer: Jason Wessel <jason.wessel@windriver.com> 553197fc4SJason Wessel * 653197fc4SJason Wessel * Copyright (C) 2000-2001 VERITAS Software Corporation. 753197fc4SJason Wessel * Copyright (C) 2002-2004 Timesys Corporation 853197fc4SJason Wessel * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> 953197fc4SJason Wessel * Copyright (C) 2004 Pavel Machek <pavel@suse.cz> 1053197fc4SJason Wessel * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> 1153197fc4SJason Wessel * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. 1253197fc4SJason Wessel * Copyright (C) 2005-2009 Wind River Systems, Inc. 1353197fc4SJason Wessel * Copyright (C) 2007 MontaVista Software, Inc. 1453197fc4SJason Wessel * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 1553197fc4SJason Wessel * 1653197fc4SJason Wessel * Contributors at various stages not listed above: 1753197fc4SJason Wessel * Jason Wessel ( jason.wessel@windriver.com ) 1853197fc4SJason Wessel * George Anzinger <george@mvista.com> 1953197fc4SJason Wessel * Anurekh Saxena (anurekh.saxena@timesys.com) 2053197fc4SJason Wessel * Lake Stevens Instrument Division (Glenn Engel) 2153197fc4SJason Wessel * Jim Kingdon, Cygnus Support. 2253197fc4SJason Wessel * 2353197fc4SJason Wessel * Original KGDB stub: David Grothe <dave@gcom.com>, 2453197fc4SJason Wessel * Tigran Aivazian <tigran@sco.com> 2553197fc4SJason Wessel * 2653197fc4SJason Wessel * This file is licensed under the terms of the GNU General Public License 2753197fc4SJason Wessel * version 2. This program is licensed "as is" without any warranty of any 2853197fc4SJason Wessel * kind, whether express or implied. 2953197fc4SJason Wessel */ 3053197fc4SJason Wessel 3153197fc4SJason Wessel #include <linux/kernel.h> 3253197fc4SJason Wessel #include <linux/kgdb.h> 33f5316b4aSJason Wessel #include <linux/kdb.h> 3453197fc4SJason Wessel #include <linux/reboot.h> 3553197fc4SJason Wessel #include <linux/uaccess.h> 3653197fc4SJason Wessel #include <asm/cacheflush.h> 3753197fc4SJason Wessel #include <asm/unaligned.h> 3853197fc4SJason Wessel #include "debug_core.h" 3953197fc4SJason Wessel 4053197fc4SJason Wessel #define KGDB_MAX_THREAD_QUERY 17 4153197fc4SJason Wessel 4253197fc4SJason Wessel /* Our I/O buffers. */ 4353197fc4SJason Wessel static char remcom_in_buffer[BUFMAX]; 4453197fc4SJason Wessel static char remcom_out_buffer[BUFMAX]; 4553197fc4SJason Wessel 4653197fc4SJason Wessel /* Storage for the registers, in GDB format. */ 4753197fc4SJason Wessel static unsigned long gdb_regs[(NUMREGBYTES + 4853197fc4SJason Wessel sizeof(unsigned long) - 1) / 4953197fc4SJason Wessel sizeof(unsigned long)]; 5053197fc4SJason Wessel 5153197fc4SJason Wessel /* 5253197fc4SJason Wessel * GDB remote protocol parser: 5353197fc4SJason Wessel */ 5453197fc4SJason Wessel 55f5316b4aSJason Wessel #ifdef CONFIG_KGDB_KDB 56f5316b4aSJason Wessel static int gdbstub_read_wait(void) 57f5316b4aSJason Wessel { 58f5316b4aSJason Wessel int ret = -1; 59f5316b4aSJason Wessel int i; 60f5316b4aSJason Wessel 61f5316b4aSJason Wessel /* poll any additional I/O interfaces that are defined */ 62f5316b4aSJason Wessel while (ret < 0) 63f5316b4aSJason Wessel for (i = 0; kdb_poll_funcs[i] != NULL; i++) { 64f5316b4aSJason Wessel ret = kdb_poll_funcs[i](); 65f5316b4aSJason Wessel if (ret > 0) 66f5316b4aSJason Wessel break; 67f5316b4aSJason Wessel } 68f5316b4aSJason Wessel return ret; 69f5316b4aSJason Wessel } 70f5316b4aSJason Wessel #else 71f5316b4aSJason Wessel static int gdbstub_read_wait(void) 72f5316b4aSJason Wessel { 73f5316b4aSJason Wessel int ret = dbg_io_ops->read_char(); 74f5316b4aSJason Wessel while (ret == NO_POLL_CHAR) 75f5316b4aSJason Wessel ret = dbg_io_ops->read_char(); 76f5316b4aSJason Wessel return ret; 77f5316b4aSJason Wessel } 78f5316b4aSJason Wessel #endif 7953197fc4SJason Wessel /* scan for the sequence $<data>#<checksum> */ 8053197fc4SJason Wessel static void get_packet(char *buffer) 8153197fc4SJason Wessel { 8253197fc4SJason Wessel unsigned char checksum; 8353197fc4SJason Wessel unsigned char xmitcsum; 8453197fc4SJason Wessel int count; 8553197fc4SJason Wessel char ch; 8653197fc4SJason Wessel 8753197fc4SJason Wessel do { 8853197fc4SJason Wessel /* 8953197fc4SJason Wessel * Spin and wait around for the start character, ignore all 9053197fc4SJason Wessel * other characters: 9153197fc4SJason Wessel */ 92f5316b4aSJason Wessel while ((ch = (gdbstub_read_wait())) != '$') 9353197fc4SJason Wessel /* nothing */; 9453197fc4SJason Wessel 9553197fc4SJason Wessel kgdb_connected = 1; 9653197fc4SJason Wessel checksum = 0; 9753197fc4SJason Wessel xmitcsum = -1; 9853197fc4SJason Wessel 9953197fc4SJason Wessel count = 0; 10053197fc4SJason Wessel 10153197fc4SJason Wessel /* 10253197fc4SJason Wessel * now, read until a # or end of buffer is found: 10353197fc4SJason Wessel */ 10453197fc4SJason Wessel while (count < (BUFMAX - 1)) { 105f5316b4aSJason Wessel ch = gdbstub_read_wait(); 10653197fc4SJason Wessel if (ch == '#') 10753197fc4SJason Wessel break; 10853197fc4SJason Wessel checksum = checksum + ch; 10953197fc4SJason Wessel buffer[count] = ch; 11053197fc4SJason Wessel count = count + 1; 11153197fc4SJason Wessel } 11253197fc4SJason Wessel buffer[count] = 0; 11353197fc4SJason Wessel 11453197fc4SJason Wessel if (ch == '#') { 115*a9fa20a7SAndy Shevchenko xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4; 116*a9fa20a7SAndy Shevchenko xmitcsum += hex_to_bin(gdbstub_read_wait()); 11753197fc4SJason Wessel 11853197fc4SJason Wessel if (checksum != xmitcsum) 11953197fc4SJason Wessel /* failed checksum */ 12053197fc4SJason Wessel dbg_io_ops->write_char('-'); 12153197fc4SJason Wessel else 12253197fc4SJason Wessel /* successful transfer */ 12353197fc4SJason Wessel dbg_io_ops->write_char('+'); 12453197fc4SJason Wessel if (dbg_io_ops->flush) 12553197fc4SJason Wessel dbg_io_ops->flush(); 12653197fc4SJason Wessel } 12753197fc4SJason Wessel } while (checksum != xmitcsum); 12853197fc4SJason Wessel } 12953197fc4SJason Wessel 13053197fc4SJason Wessel /* 13153197fc4SJason Wessel * Send the packet in buffer. 13253197fc4SJason Wessel * Check for gdb connection if asked for. 13353197fc4SJason Wessel */ 13453197fc4SJason Wessel static void put_packet(char *buffer) 13553197fc4SJason Wessel { 13653197fc4SJason Wessel unsigned char checksum; 13753197fc4SJason Wessel int count; 13853197fc4SJason Wessel char ch; 13953197fc4SJason Wessel 14053197fc4SJason Wessel /* 14153197fc4SJason Wessel * $<packet info>#<checksum>. 14253197fc4SJason Wessel */ 14353197fc4SJason Wessel while (1) { 14453197fc4SJason Wessel dbg_io_ops->write_char('$'); 14553197fc4SJason Wessel checksum = 0; 14653197fc4SJason Wessel count = 0; 14753197fc4SJason Wessel 14853197fc4SJason Wessel while ((ch = buffer[count])) { 14953197fc4SJason Wessel dbg_io_ops->write_char(ch); 15053197fc4SJason Wessel checksum += ch; 15153197fc4SJason Wessel count++; 15253197fc4SJason Wessel } 15353197fc4SJason Wessel 15453197fc4SJason Wessel dbg_io_ops->write_char('#'); 15553197fc4SJason Wessel dbg_io_ops->write_char(hex_asc_hi(checksum)); 15653197fc4SJason Wessel dbg_io_ops->write_char(hex_asc_lo(checksum)); 15753197fc4SJason Wessel if (dbg_io_ops->flush) 15853197fc4SJason Wessel dbg_io_ops->flush(); 15953197fc4SJason Wessel 16053197fc4SJason Wessel /* Now see what we get in reply. */ 161f5316b4aSJason Wessel ch = gdbstub_read_wait(); 16253197fc4SJason Wessel 16353197fc4SJason Wessel if (ch == 3) 164f5316b4aSJason Wessel ch = gdbstub_read_wait(); 16553197fc4SJason Wessel 16653197fc4SJason Wessel /* If we get an ACK, we are done. */ 16753197fc4SJason Wessel if (ch == '+') 16853197fc4SJason Wessel return; 16953197fc4SJason Wessel 17053197fc4SJason Wessel /* 17153197fc4SJason Wessel * If we get the start of another packet, this means 17253197fc4SJason Wessel * that GDB is attempting to reconnect. We will NAK 17353197fc4SJason Wessel * the packet being sent, and stop trying to send this 17453197fc4SJason Wessel * packet. 17553197fc4SJason Wessel */ 17653197fc4SJason Wessel if (ch == '$') { 17753197fc4SJason Wessel dbg_io_ops->write_char('-'); 17853197fc4SJason Wessel if (dbg_io_ops->flush) 17953197fc4SJason Wessel dbg_io_ops->flush(); 18053197fc4SJason Wessel return; 18153197fc4SJason Wessel } 18253197fc4SJason Wessel } 18353197fc4SJason Wessel } 18453197fc4SJason Wessel 18553197fc4SJason Wessel static char gdbmsgbuf[BUFMAX + 1]; 18653197fc4SJason Wessel 18753197fc4SJason Wessel void gdbstub_msg_write(const char *s, int len) 18853197fc4SJason Wessel { 18953197fc4SJason Wessel char *bufptr; 19053197fc4SJason Wessel int wcount; 19153197fc4SJason Wessel int i; 19253197fc4SJason Wessel 193a0de055cSJason Wessel if (len == 0) 194a0de055cSJason Wessel len = strlen(s); 195a0de055cSJason Wessel 19653197fc4SJason Wessel /* 'O'utput */ 19753197fc4SJason Wessel gdbmsgbuf[0] = 'O'; 19853197fc4SJason Wessel 19953197fc4SJason Wessel /* Fill and send buffers... */ 20053197fc4SJason Wessel while (len > 0) { 20153197fc4SJason Wessel bufptr = gdbmsgbuf + 1; 20253197fc4SJason Wessel 20353197fc4SJason Wessel /* Calculate how many this time */ 20453197fc4SJason Wessel if ((len << 1) > (BUFMAX - 2)) 20553197fc4SJason Wessel wcount = (BUFMAX - 2) >> 1; 20653197fc4SJason Wessel else 20753197fc4SJason Wessel wcount = len; 20853197fc4SJason Wessel 20953197fc4SJason Wessel /* Pack in hex chars */ 21053197fc4SJason Wessel for (i = 0; i < wcount; i++) 21153197fc4SJason Wessel bufptr = pack_hex_byte(bufptr, s[i]); 21253197fc4SJason Wessel *bufptr = '\0'; 21353197fc4SJason Wessel 21453197fc4SJason Wessel /* Move up */ 21553197fc4SJason Wessel s += wcount; 21653197fc4SJason Wessel len -= wcount; 21753197fc4SJason Wessel 21853197fc4SJason Wessel /* Write packet */ 21953197fc4SJason Wessel put_packet(gdbmsgbuf); 22053197fc4SJason Wessel } 22153197fc4SJason Wessel } 22253197fc4SJason Wessel 22353197fc4SJason Wessel /* 22453197fc4SJason Wessel * Convert the memory pointed to by mem into hex, placing result in 22553197fc4SJason Wessel * buf. Return a pointer to the last char put in buf (null). May 22653197fc4SJason Wessel * return an error. 22753197fc4SJason Wessel */ 22853197fc4SJason Wessel int kgdb_mem2hex(char *mem, char *buf, int count) 22953197fc4SJason Wessel { 23053197fc4SJason Wessel char *tmp; 23153197fc4SJason Wessel int err; 23253197fc4SJason Wessel 23353197fc4SJason Wessel /* 23453197fc4SJason Wessel * We use the upper half of buf as an intermediate buffer for the 23553197fc4SJason Wessel * raw memory copy. Hex conversion will work against this one. 23653197fc4SJason Wessel */ 23753197fc4SJason Wessel tmp = buf + count; 23853197fc4SJason Wessel 23953197fc4SJason Wessel err = probe_kernel_read(tmp, mem, count); 24053197fc4SJason Wessel if (!err) { 24153197fc4SJason Wessel while (count > 0) { 24253197fc4SJason Wessel buf = pack_hex_byte(buf, *tmp); 24353197fc4SJason Wessel tmp++; 24453197fc4SJason Wessel count--; 24553197fc4SJason Wessel } 24653197fc4SJason Wessel 24753197fc4SJason Wessel *buf = 0; 24853197fc4SJason Wessel } 24953197fc4SJason Wessel 25053197fc4SJason Wessel return err; 25153197fc4SJason Wessel } 25253197fc4SJason Wessel 25353197fc4SJason Wessel /* 25453197fc4SJason Wessel * Convert the hex array pointed to by buf into binary to be placed in 25553197fc4SJason Wessel * mem. Return a pointer to the character AFTER the last byte 25653197fc4SJason Wessel * written. May return an error. 25753197fc4SJason Wessel */ 25853197fc4SJason Wessel int kgdb_hex2mem(char *buf, char *mem, int count) 25953197fc4SJason Wessel { 26053197fc4SJason Wessel char *tmp_raw; 26153197fc4SJason Wessel char *tmp_hex; 26253197fc4SJason Wessel 26353197fc4SJason Wessel /* 26453197fc4SJason Wessel * We use the upper half of buf as an intermediate buffer for the 26553197fc4SJason Wessel * raw memory that is converted from hex. 26653197fc4SJason Wessel */ 26753197fc4SJason Wessel tmp_raw = buf + count * 2; 26853197fc4SJason Wessel 26953197fc4SJason Wessel tmp_hex = tmp_raw - 1; 27053197fc4SJason Wessel while (tmp_hex >= buf) { 27153197fc4SJason Wessel tmp_raw--; 272*a9fa20a7SAndy Shevchenko *tmp_raw = hex_to_bin(*tmp_hex--); 273*a9fa20a7SAndy Shevchenko *tmp_raw |= hex_to_bin(*tmp_hex--) << 4; 27453197fc4SJason Wessel } 27553197fc4SJason Wessel 27653197fc4SJason Wessel return probe_kernel_write(mem, tmp_raw, count); 27753197fc4SJason Wessel } 27853197fc4SJason Wessel 27953197fc4SJason Wessel /* 28053197fc4SJason Wessel * While we find nice hex chars, build a long_val. 28153197fc4SJason Wessel * Return number of chars processed. 28253197fc4SJason Wessel */ 28353197fc4SJason Wessel int kgdb_hex2long(char **ptr, unsigned long *long_val) 28453197fc4SJason Wessel { 28553197fc4SJason Wessel int hex_val; 28653197fc4SJason Wessel int num = 0; 28753197fc4SJason Wessel int negate = 0; 28853197fc4SJason Wessel 28953197fc4SJason Wessel *long_val = 0; 29053197fc4SJason Wessel 29153197fc4SJason Wessel if (**ptr == '-') { 29253197fc4SJason Wessel negate = 1; 29353197fc4SJason Wessel (*ptr)++; 29453197fc4SJason Wessel } 29553197fc4SJason Wessel while (**ptr) { 296*a9fa20a7SAndy Shevchenko hex_val = hex_to_bin(**ptr); 29753197fc4SJason Wessel if (hex_val < 0) 29853197fc4SJason Wessel break; 29953197fc4SJason Wessel 30053197fc4SJason Wessel *long_val = (*long_val << 4) | hex_val; 30153197fc4SJason Wessel num++; 30253197fc4SJason Wessel (*ptr)++; 30353197fc4SJason Wessel } 30453197fc4SJason Wessel 30553197fc4SJason Wessel if (negate) 30653197fc4SJason Wessel *long_val = -*long_val; 30753197fc4SJason Wessel 30853197fc4SJason Wessel return num; 30953197fc4SJason Wessel } 31053197fc4SJason Wessel 31153197fc4SJason Wessel /* 31253197fc4SJason Wessel * Copy the binary array pointed to by buf into mem. Fix $, #, and 31353197fc4SJason Wessel * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success. 31453197fc4SJason Wessel * The input buf is overwitten with the result to write to mem. 31553197fc4SJason Wessel */ 31653197fc4SJason Wessel static int kgdb_ebin2mem(char *buf, char *mem, int count) 31753197fc4SJason Wessel { 31853197fc4SJason Wessel int size = 0; 31953197fc4SJason Wessel char *c = buf; 32053197fc4SJason Wessel 32153197fc4SJason Wessel while (count-- > 0) { 32253197fc4SJason Wessel c[size] = *buf++; 32353197fc4SJason Wessel if (c[size] == 0x7d) 32453197fc4SJason Wessel c[size] = *buf++ ^ 0x20; 32553197fc4SJason Wessel size++; 32653197fc4SJason Wessel } 32753197fc4SJason Wessel 32853197fc4SJason Wessel return probe_kernel_write(mem, c, size); 32953197fc4SJason Wessel } 33053197fc4SJason Wessel 33153197fc4SJason Wessel /* Write memory due to an 'M' or 'X' packet. */ 33253197fc4SJason Wessel static int write_mem_msg(int binary) 33353197fc4SJason Wessel { 33453197fc4SJason Wessel char *ptr = &remcom_in_buffer[1]; 33553197fc4SJason Wessel unsigned long addr; 33653197fc4SJason Wessel unsigned long length; 33753197fc4SJason Wessel int err; 33853197fc4SJason Wessel 33953197fc4SJason Wessel if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' && 34053197fc4SJason Wessel kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') { 34153197fc4SJason Wessel if (binary) 34253197fc4SJason Wessel err = kgdb_ebin2mem(ptr, (char *)addr, length); 34353197fc4SJason Wessel else 34453197fc4SJason Wessel err = kgdb_hex2mem(ptr, (char *)addr, length); 34553197fc4SJason Wessel if (err) 34653197fc4SJason Wessel return err; 34753197fc4SJason Wessel if (CACHE_FLUSH_IS_SAFE) 34853197fc4SJason Wessel flush_icache_range(addr, addr + length); 34953197fc4SJason Wessel return 0; 35053197fc4SJason Wessel } 35153197fc4SJason Wessel 35253197fc4SJason Wessel return -EINVAL; 35353197fc4SJason Wessel } 35453197fc4SJason Wessel 35553197fc4SJason Wessel static void error_packet(char *pkt, int error) 35653197fc4SJason Wessel { 35753197fc4SJason Wessel error = -error; 35853197fc4SJason Wessel pkt[0] = 'E'; 35953197fc4SJason Wessel pkt[1] = hex_asc[(error / 10)]; 36053197fc4SJason Wessel pkt[2] = hex_asc[(error % 10)]; 36153197fc4SJason Wessel pkt[3] = '\0'; 36253197fc4SJason Wessel } 36353197fc4SJason Wessel 36453197fc4SJason Wessel /* 36553197fc4SJason Wessel * Thread ID accessors. We represent a flat TID space to GDB, where 36653197fc4SJason Wessel * the per CPU idle threads (which under Linux all have PID 0) are 36753197fc4SJason Wessel * remapped to negative TIDs. 36853197fc4SJason Wessel */ 36953197fc4SJason Wessel 37053197fc4SJason Wessel #define BUF_THREAD_ID_SIZE 16 37153197fc4SJason Wessel 37253197fc4SJason Wessel static char *pack_threadid(char *pkt, unsigned char *id) 37353197fc4SJason Wessel { 37453197fc4SJason Wessel char *limit; 37553197fc4SJason Wessel 37653197fc4SJason Wessel limit = pkt + BUF_THREAD_ID_SIZE; 37753197fc4SJason Wessel while (pkt < limit) 37853197fc4SJason Wessel pkt = pack_hex_byte(pkt, *id++); 37953197fc4SJason Wessel 38053197fc4SJason Wessel return pkt; 38153197fc4SJason Wessel } 38253197fc4SJason Wessel 38353197fc4SJason Wessel static void int_to_threadref(unsigned char *id, int value) 38453197fc4SJason Wessel { 38553197fc4SJason Wessel unsigned char *scan; 38653197fc4SJason Wessel int i = 4; 38753197fc4SJason Wessel 38853197fc4SJason Wessel scan = (unsigned char *)id; 38953197fc4SJason Wessel while (i--) 39053197fc4SJason Wessel *scan++ = 0; 39153197fc4SJason Wessel put_unaligned_be32(value, scan); 39253197fc4SJason Wessel } 39353197fc4SJason Wessel 39453197fc4SJason Wessel static struct task_struct *getthread(struct pt_regs *regs, int tid) 39553197fc4SJason Wessel { 39653197fc4SJason Wessel /* 39753197fc4SJason Wessel * Non-positive TIDs are remapped to the cpu shadow information 39853197fc4SJason Wessel */ 39953197fc4SJason Wessel if (tid == 0 || tid == -1) 40053197fc4SJason Wessel tid = -atomic_read(&kgdb_active) - 2; 40153197fc4SJason Wessel if (tid < -1 && tid > -NR_CPUS - 2) { 40253197fc4SJason Wessel if (kgdb_info[-tid - 2].task) 40353197fc4SJason Wessel return kgdb_info[-tid - 2].task; 40453197fc4SJason Wessel else 40553197fc4SJason Wessel return idle_task(-tid - 2); 40653197fc4SJason Wessel } 40753197fc4SJason Wessel if (tid <= 0) { 40853197fc4SJason Wessel printk(KERN_ERR "KGDB: Internal thread select error\n"); 40953197fc4SJason Wessel dump_stack(); 41053197fc4SJason Wessel return NULL; 41153197fc4SJason Wessel } 41253197fc4SJason Wessel 41353197fc4SJason Wessel /* 41453197fc4SJason Wessel * find_task_by_pid_ns() does not take the tasklist lock anymore 41553197fc4SJason Wessel * but is nicely RCU locked - hence is a pretty resilient 41653197fc4SJason Wessel * thing to use: 41753197fc4SJason Wessel */ 41853197fc4SJason Wessel return find_task_by_pid_ns(tid, &init_pid_ns); 41953197fc4SJason Wessel } 42053197fc4SJason Wessel 42153197fc4SJason Wessel 42253197fc4SJason Wessel /* 42353197fc4SJason Wessel * Remap normal tasks to their real PID, 42453197fc4SJason Wessel * CPU shadow threads are mapped to -CPU - 2 42553197fc4SJason Wessel */ 42653197fc4SJason Wessel static inline int shadow_pid(int realpid) 42753197fc4SJason Wessel { 42853197fc4SJason Wessel if (realpid) 42953197fc4SJason Wessel return realpid; 43053197fc4SJason Wessel 43153197fc4SJason Wessel return -raw_smp_processor_id() - 2; 43253197fc4SJason Wessel } 43353197fc4SJason Wessel 43453197fc4SJason Wessel /* 43553197fc4SJason Wessel * All the functions that start with gdb_cmd are the various 43653197fc4SJason Wessel * operations to implement the handlers for the gdbserial protocol 43753197fc4SJason Wessel * where KGDB is communicating with an external debugger 43853197fc4SJason Wessel */ 43953197fc4SJason Wessel 44053197fc4SJason Wessel /* Handle the '?' status packets */ 44153197fc4SJason Wessel static void gdb_cmd_status(struct kgdb_state *ks) 44253197fc4SJason Wessel { 44353197fc4SJason Wessel /* 44453197fc4SJason Wessel * We know that this packet is only sent 44553197fc4SJason Wessel * during initial connect. So to be safe, 44653197fc4SJason Wessel * we clear out our breakpoints now in case 44753197fc4SJason Wessel * GDB is reconnecting. 44853197fc4SJason Wessel */ 44953197fc4SJason Wessel dbg_remove_all_break(); 45053197fc4SJason Wessel 45153197fc4SJason Wessel remcom_out_buffer[0] = 'S'; 45253197fc4SJason Wessel pack_hex_byte(&remcom_out_buffer[1], ks->signo); 45353197fc4SJason Wessel } 45453197fc4SJason Wessel 45553197fc4SJason Wessel /* Handle the 'g' get registers request */ 45653197fc4SJason Wessel static void gdb_cmd_getregs(struct kgdb_state *ks) 45753197fc4SJason Wessel { 45853197fc4SJason Wessel struct task_struct *thread; 45953197fc4SJason Wessel void *local_debuggerinfo; 46053197fc4SJason Wessel int i; 46153197fc4SJason Wessel 46253197fc4SJason Wessel thread = kgdb_usethread; 46353197fc4SJason Wessel if (!thread) { 46453197fc4SJason Wessel thread = kgdb_info[ks->cpu].task; 46553197fc4SJason Wessel local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo; 46653197fc4SJason Wessel } else { 46753197fc4SJason Wessel local_debuggerinfo = NULL; 46853197fc4SJason Wessel for_each_online_cpu(i) { 46953197fc4SJason Wessel /* 47053197fc4SJason Wessel * Try to find the task on some other 47153197fc4SJason Wessel * or possibly this node if we do not 47253197fc4SJason Wessel * find the matching task then we try 47353197fc4SJason Wessel * to approximate the results. 47453197fc4SJason Wessel */ 47553197fc4SJason Wessel if (thread == kgdb_info[i].task) 47653197fc4SJason Wessel local_debuggerinfo = kgdb_info[i].debuggerinfo; 47753197fc4SJason Wessel } 47853197fc4SJason Wessel } 47953197fc4SJason Wessel 48053197fc4SJason Wessel /* 48153197fc4SJason Wessel * All threads that don't have debuggerinfo should be 48253197fc4SJason Wessel * in schedule() sleeping, since all other CPUs 48353197fc4SJason Wessel * are in kgdb_wait, and thus have debuggerinfo. 48453197fc4SJason Wessel */ 48553197fc4SJason Wessel if (local_debuggerinfo) { 48653197fc4SJason Wessel pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo); 48753197fc4SJason Wessel } else { 48853197fc4SJason Wessel /* 48953197fc4SJason Wessel * Pull stuff saved during switch_to; nothing 49053197fc4SJason Wessel * else is accessible (or even particularly 49153197fc4SJason Wessel * relevant). 49253197fc4SJason Wessel * 49353197fc4SJason Wessel * This should be enough for a stack trace. 49453197fc4SJason Wessel */ 49553197fc4SJason Wessel sleeping_thread_to_gdb_regs(gdb_regs, thread); 49653197fc4SJason Wessel } 49753197fc4SJason Wessel kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES); 49853197fc4SJason Wessel } 49953197fc4SJason Wessel 50053197fc4SJason Wessel /* Handle the 'G' set registers request */ 50153197fc4SJason Wessel static void gdb_cmd_setregs(struct kgdb_state *ks) 50253197fc4SJason Wessel { 50353197fc4SJason Wessel kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES); 50453197fc4SJason Wessel 50553197fc4SJason Wessel if (kgdb_usethread && kgdb_usethread != current) { 50653197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 50753197fc4SJason Wessel } else { 50853197fc4SJason Wessel gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs); 50953197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 51053197fc4SJason Wessel } 51153197fc4SJason Wessel } 51253197fc4SJason Wessel 51353197fc4SJason Wessel /* Handle the 'm' memory read bytes */ 51453197fc4SJason Wessel static void gdb_cmd_memread(struct kgdb_state *ks) 51553197fc4SJason Wessel { 51653197fc4SJason Wessel char *ptr = &remcom_in_buffer[1]; 51753197fc4SJason Wessel unsigned long length; 51853197fc4SJason Wessel unsigned long addr; 51953197fc4SJason Wessel int err; 52053197fc4SJason Wessel 52153197fc4SJason Wessel if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' && 52253197fc4SJason Wessel kgdb_hex2long(&ptr, &length) > 0) { 52353197fc4SJason Wessel err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length); 52453197fc4SJason Wessel if (err) 52553197fc4SJason Wessel error_packet(remcom_out_buffer, err); 52653197fc4SJason Wessel } else { 52753197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 52853197fc4SJason Wessel } 52953197fc4SJason Wessel } 53053197fc4SJason Wessel 53153197fc4SJason Wessel /* Handle the 'M' memory write bytes */ 53253197fc4SJason Wessel static void gdb_cmd_memwrite(struct kgdb_state *ks) 53353197fc4SJason Wessel { 53453197fc4SJason Wessel int err = write_mem_msg(0); 53553197fc4SJason Wessel 53653197fc4SJason Wessel if (err) 53753197fc4SJason Wessel error_packet(remcom_out_buffer, err); 53853197fc4SJason Wessel else 53953197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 54053197fc4SJason Wessel } 54153197fc4SJason Wessel 54253197fc4SJason Wessel /* Handle the 'X' memory binary write bytes */ 54353197fc4SJason Wessel static void gdb_cmd_binwrite(struct kgdb_state *ks) 54453197fc4SJason Wessel { 54553197fc4SJason Wessel int err = write_mem_msg(1); 54653197fc4SJason Wessel 54753197fc4SJason Wessel if (err) 54853197fc4SJason Wessel error_packet(remcom_out_buffer, err); 54953197fc4SJason Wessel else 55053197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 55153197fc4SJason Wessel } 55253197fc4SJason Wessel 55353197fc4SJason Wessel /* Handle the 'D' or 'k', detach or kill packets */ 55453197fc4SJason Wessel static void gdb_cmd_detachkill(struct kgdb_state *ks) 55553197fc4SJason Wessel { 55653197fc4SJason Wessel int error; 55753197fc4SJason Wessel 55853197fc4SJason Wessel /* The detach case */ 55953197fc4SJason Wessel if (remcom_in_buffer[0] == 'D') { 56053197fc4SJason Wessel error = dbg_remove_all_break(); 56153197fc4SJason Wessel if (error < 0) { 56253197fc4SJason Wessel error_packet(remcom_out_buffer, error); 56353197fc4SJason Wessel } else { 56453197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 56553197fc4SJason Wessel kgdb_connected = 0; 56653197fc4SJason Wessel } 56753197fc4SJason Wessel put_packet(remcom_out_buffer); 56853197fc4SJason Wessel } else { 56953197fc4SJason Wessel /* 57053197fc4SJason Wessel * Assume the kill case, with no exit code checking, 57153197fc4SJason Wessel * trying to force detach the debugger: 57253197fc4SJason Wessel */ 57353197fc4SJason Wessel dbg_remove_all_break(); 57453197fc4SJason Wessel kgdb_connected = 0; 57553197fc4SJason Wessel } 57653197fc4SJason Wessel } 57753197fc4SJason Wessel 57853197fc4SJason Wessel /* Handle the 'R' reboot packets */ 57953197fc4SJason Wessel static int gdb_cmd_reboot(struct kgdb_state *ks) 58053197fc4SJason Wessel { 58153197fc4SJason Wessel /* For now, only honor R0 */ 58253197fc4SJason Wessel if (strcmp(remcom_in_buffer, "R0") == 0) { 58353197fc4SJason Wessel printk(KERN_CRIT "Executing emergency reboot\n"); 58453197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 58553197fc4SJason Wessel put_packet(remcom_out_buffer); 58653197fc4SJason Wessel 58753197fc4SJason Wessel /* 58853197fc4SJason Wessel * Execution should not return from 58953197fc4SJason Wessel * machine_emergency_restart() 59053197fc4SJason Wessel */ 59153197fc4SJason Wessel machine_emergency_restart(); 59253197fc4SJason Wessel kgdb_connected = 0; 59353197fc4SJason Wessel 59453197fc4SJason Wessel return 1; 59553197fc4SJason Wessel } 59653197fc4SJason Wessel return 0; 59753197fc4SJason Wessel } 59853197fc4SJason Wessel 59953197fc4SJason Wessel /* Handle the 'q' query packets */ 60053197fc4SJason Wessel static void gdb_cmd_query(struct kgdb_state *ks) 60153197fc4SJason Wessel { 60253197fc4SJason Wessel struct task_struct *g; 60353197fc4SJason Wessel struct task_struct *p; 60453197fc4SJason Wessel unsigned char thref[8]; 60553197fc4SJason Wessel char *ptr; 60653197fc4SJason Wessel int i; 60753197fc4SJason Wessel int cpu; 60853197fc4SJason Wessel int finished = 0; 60953197fc4SJason Wessel 61053197fc4SJason Wessel switch (remcom_in_buffer[1]) { 61153197fc4SJason Wessel case 's': 61253197fc4SJason Wessel case 'f': 613fb82c0ffSJason Wessel if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) 61453197fc4SJason Wessel break; 61553197fc4SJason Wessel 61653197fc4SJason Wessel i = 0; 61753197fc4SJason Wessel remcom_out_buffer[0] = 'm'; 61853197fc4SJason Wessel ptr = remcom_out_buffer + 1; 61953197fc4SJason Wessel if (remcom_in_buffer[1] == 'f') { 62053197fc4SJason Wessel /* Each cpu is a shadow thread */ 62153197fc4SJason Wessel for_each_online_cpu(cpu) { 62253197fc4SJason Wessel ks->thr_query = 0; 62353197fc4SJason Wessel int_to_threadref(thref, -cpu - 2); 62453197fc4SJason Wessel pack_threadid(ptr, thref); 62553197fc4SJason Wessel ptr += BUF_THREAD_ID_SIZE; 62653197fc4SJason Wessel *(ptr++) = ','; 62753197fc4SJason Wessel i++; 62853197fc4SJason Wessel } 62953197fc4SJason Wessel } 63053197fc4SJason Wessel 63153197fc4SJason Wessel do_each_thread(g, p) { 63253197fc4SJason Wessel if (i >= ks->thr_query && !finished) { 63353197fc4SJason Wessel int_to_threadref(thref, p->pid); 63453197fc4SJason Wessel pack_threadid(ptr, thref); 63553197fc4SJason Wessel ptr += BUF_THREAD_ID_SIZE; 63653197fc4SJason Wessel *(ptr++) = ','; 63753197fc4SJason Wessel ks->thr_query++; 63853197fc4SJason Wessel if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0) 63953197fc4SJason Wessel finished = 1; 64053197fc4SJason Wessel } 64153197fc4SJason Wessel i++; 64253197fc4SJason Wessel } while_each_thread(g, p); 64353197fc4SJason Wessel 64453197fc4SJason Wessel *(--ptr) = '\0'; 64553197fc4SJason Wessel break; 64653197fc4SJason Wessel 64753197fc4SJason Wessel case 'C': 64853197fc4SJason Wessel /* Current thread id */ 64953197fc4SJason Wessel strcpy(remcom_out_buffer, "QC"); 65053197fc4SJason Wessel ks->threadid = shadow_pid(current->pid); 65153197fc4SJason Wessel int_to_threadref(thref, ks->threadid); 65253197fc4SJason Wessel pack_threadid(remcom_out_buffer + 2, thref); 65353197fc4SJason Wessel break; 65453197fc4SJason Wessel case 'T': 655fb82c0ffSJason Wessel if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) 65653197fc4SJason Wessel break; 657fb82c0ffSJason Wessel 65853197fc4SJason Wessel ks->threadid = 0; 65953197fc4SJason Wessel ptr = remcom_in_buffer + 17; 66053197fc4SJason Wessel kgdb_hex2long(&ptr, &ks->threadid); 66153197fc4SJason Wessel if (!getthread(ks->linux_regs, ks->threadid)) { 66253197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 66353197fc4SJason Wessel break; 66453197fc4SJason Wessel } 66553197fc4SJason Wessel if ((int)ks->threadid > 0) { 66653197fc4SJason Wessel kgdb_mem2hex(getthread(ks->linux_regs, 66753197fc4SJason Wessel ks->threadid)->comm, 66853197fc4SJason Wessel remcom_out_buffer, 16); 66953197fc4SJason Wessel } else { 67053197fc4SJason Wessel static char tmpstr[23 + BUF_THREAD_ID_SIZE]; 67153197fc4SJason Wessel 67253197fc4SJason Wessel sprintf(tmpstr, "shadowCPU%d", 67353197fc4SJason Wessel (int)(-ks->threadid - 2)); 67453197fc4SJason Wessel kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr)); 67553197fc4SJason Wessel } 67653197fc4SJason Wessel break; 677a0de055cSJason Wessel #ifdef CONFIG_KGDB_KDB 678a0de055cSJason Wessel case 'R': 679a0de055cSJason Wessel if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) { 680a0de055cSJason Wessel int len = strlen(remcom_in_buffer + 6); 681a0de055cSJason Wessel 682a0de055cSJason Wessel if ((len % 2) != 0) { 683a0de055cSJason Wessel strcpy(remcom_out_buffer, "E01"); 684a0de055cSJason Wessel break; 685a0de055cSJason Wessel } 686a0de055cSJason Wessel kgdb_hex2mem(remcom_in_buffer + 6, 687a0de055cSJason Wessel remcom_out_buffer, len); 688a0de055cSJason Wessel len = len / 2; 689a0de055cSJason Wessel remcom_out_buffer[len++] = 0; 690a0de055cSJason Wessel 691a0de055cSJason Wessel kdb_parse(remcom_out_buffer); 692a0de055cSJason Wessel strcpy(remcom_out_buffer, "OK"); 693a0de055cSJason Wessel } 694a0de055cSJason Wessel break; 695a0de055cSJason Wessel #endif 69653197fc4SJason Wessel } 69753197fc4SJason Wessel } 69853197fc4SJason Wessel 69953197fc4SJason Wessel /* Handle the 'H' task query packets */ 70053197fc4SJason Wessel static void gdb_cmd_task(struct kgdb_state *ks) 70153197fc4SJason Wessel { 70253197fc4SJason Wessel struct task_struct *thread; 70353197fc4SJason Wessel char *ptr; 70453197fc4SJason Wessel 70553197fc4SJason Wessel switch (remcom_in_buffer[1]) { 70653197fc4SJason Wessel case 'g': 70753197fc4SJason Wessel ptr = &remcom_in_buffer[2]; 70853197fc4SJason Wessel kgdb_hex2long(&ptr, &ks->threadid); 70953197fc4SJason Wessel thread = getthread(ks->linux_regs, ks->threadid); 71053197fc4SJason Wessel if (!thread && ks->threadid > 0) { 71153197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 71253197fc4SJason Wessel break; 71353197fc4SJason Wessel } 71453197fc4SJason Wessel kgdb_usethread = thread; 71553197fc4SJason Wessel ks->kgdb_usethreadid = ks->threadid; 71653197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 71753197fc4SJason Wessel break; 71853197fc4SJason Wessel case 'c': 71953197fc4SJason Wessel ptr = &remcom_in_buffer[2]; 72053197fc4SJason Wessel kgdb_hex2long(&ptr, &ks->threadid); 72153197fc4SJason Wessel if (!ks->threadid) { 72253197fc4SJason Wessel kgdb_contthread = NULL; 72353197fc4SJason Wessel } else { 72453197fc4SJason Wessel thread = getthread(ks->linux_regs, ks->threadid); 72553197fc4SJason Wessel if (!thread && ks->threadid > 0) { 72653197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 72753197fc4SJason Wessel break; 72853197fc4SJason Wessel } 72953197fc4SJason Wessel kgdb_contthread = thread; 73053197fc4SJason Wessel } 73153197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 73253197fc4SJason Wessel break; 73353197fc4SJason Wessel } 73453197fc4SJason Wessel } 73553197fc4SJason Wessel 73653197fc4SJason Wessel /* Handle the 'T' thread query packets */ 73753197fc4SJason Wessel static void gdb_cmd_thread(struct kgdb_state *ks) 73853197fc4SJason Wessel { 73953197fc4SJason Wessel char *ptr = &remcom_in_buffer[1]; 74053197fc4SJason Wessel struct task_struct *thread; 74153197fc4SJason Wessel 74253197fc4SJason Wessel kgdb_hex2long(&ptr, &ks->threadid); 74353197fc4SJason Wessel thread = getthread(ks->linux_regs, ks->threadid); 74453197fc4SJason Wessel if (thread) 74553197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 74653197fc4SJason Wessel else 74753197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 74853197fc4SJason Wessel } 74953197fc4SJason Wessel 75053197fc4SJason Wessel /* Handle the 'z' or 'Z' breakpoint remove or set packets */ 75153197fc4SJason Wessel static void gdb_cmd_break(struct kgdb_state *ks) 75253197fc4SJason Wessel { 75353197fc4SJason Wessel /* 75453197fc4SJason Wessel * Since GDB-5.3, it's been drafted that '0' is a software 75553197fc4SJason Wessel * breakpoint, '1' is a hardware breakpoint, so let's do that. 75653197fc4SJason Wessel */ 75753197fc4SJason Wessel char *bpt_type = &remcom_in_buffer[1]; 75853197fc4SJason Wessel char *ptr = &remcom_in_buffer[2]; 75953197fc4SJason Wessel unsigned long addr; 76053197fc4SJason Wessel unsigned long length; 76153197fc4SJason Wessel int error = 0; 76253197fc4SJason Wessel 76353197fc4SJason Wessel if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') { 76453197fc4SJason Wessel /* Unsupported */ 76553197fc4SJason Wessel if (*bpt_type > '4') 76653197fc4SJason Wessel return; 76753197fc4SJason Wessel } else { 76853197fc4SJason Wessel if (*bpt_type != '0' && *bpt_type != '1') 76953197fc4SJason Wessel /* Unsupported. */ 77053197fc4SJason Wessel return; 77153197fc4SJason Wessel } 77253197fc4SJason Wessel 77353197fc4SJason Wessel /* 77453197fc4SJason Wessel * Test if this is a hardware breakpoint, and 77553197fc4SJason Wessel * if we support it: 77653197fc4SJason Wessel */ 77753197fc4SJason Wessel if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)) 77853197fc4SJason Wessel /* Unsupported. */ 77953197fc4SJason Wessel return; 78053197fc4SJason Wessel 78153197fc4SJason Wessel if (*(ptr++) != ',') { 78253197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 78353197fc4SJason Wessel return; 78453197fc4SJason Wessel } 78553197fc4SJason Wessel if (!kgdb_hex2long(&ptr, &addr)) { 78653197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 78753197fc4SJason Wessel return; 78853197fc4SJason Wessel } 78953197fc4SJason Wessel if (*(ptr++) != ',' || 79053197fc4SJason Wessel !kgdb_hex2long(&ptr, &length)) { 79153197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 79253197fc4SJason Wessel return; 79353197fc4SJason Wessel } 79453197fc4SJason Wessel 79553197fc4SJason Wessel if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0') 79653197fc4SJason Wessel error = dbg_set_sw_break(addr); 79753197fc4SJason Wessel else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0') 79853197fc4SJason Wessel error = dbg_remove_sw_break(addr); 79953197fc4SJason Wessel else if (remcom_in_buffer[0] == 'Z') 80053197fc4SJason Wessel error = arch_kgdb_ops.set_hw_breakpoint(addr, 80153197fc4SJason Wessel (int)length, *bpt_type - '0'); 80253197fc4SJason Wessel else if (remcom_in_buffer[0] == 'z') 80353197fc4SJason Wessel error = arch_kgdb_ops.remove_hw_breakpoint(addr, 80453197fc4SJason Wessel (int) length, *bpt_type - '0'); 80553197fc4SJason Wessel 80653197fc4SJason Wessel if (error == 0) 80753197fc4SJason Wessel strcpy(remcom_out_buffer, "OK"); 80853197fc4SJason Wessel else 80953197fc4SJason Wessel error_packet(remcom_out_buffer, error); 81053197fc4SJason Wessel } 81153197fc4SJason Wessel 81253197fc4SJason Wessel /* Handle the 'C' signal / exception passing packets */ 81353197fc4SJason Wessel static int gdb_cmd_exception_pass(struct kgdb_state *ks) 81453197fc4SJason Wessel { 81553197fc4SJason Wessel /* C09 == pass exception 81653197fc4SJason Wessel * C15 == detach kgdb, pass exception 81753197fc4SJason Wessel */ 81853197fc4SJason Wessel if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') { 81953197fc4SJason Wessel 82053197fc4SJason Wessel ks->pass_exception = 1; 82153197fc4SJason Wessel remcom_in_buffer[0] = 'c'; 82253197fc4SJason Wessel 82353197fc4SJason Wessel } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') { 82453197fc4SJason Wessel 82553197fc4SJason Wessel ks->pass_exception = 1; 82653197fc4SJason Wessel remcom_in_buffer[0] = 'D'; 82753197fc4SJason Wessel dbg_remove_all_break(); 82853197fc4SJason Wessel kgdb_connected = 0; 82953197fc4SJason Wessel return 1; 83053197fc4SJason Wessel 83153197fc4SJason Wessel } else { 83253197fc4SJason Wessel gdbstub_msg_write("KGDB only knows signal 9 (pass)" 83353197fc4SJason Wessel " and 15 (pass and disconnect)\n" 83453197fc4SJason Wessel "Executing a continue without signal passing\n", 0); 83553197fc4SJason Wessel remcom_in_buffer[0] = 'c'; 83653197fc4SJason Wessel } 83753197fc4SJason Wessel 83853197fc4SJason Wessel /* Indicate fall through */ 83953197fc4SJason Wessel return -1; 84053197fc4SJason Wessel } 84153197fc4SJason Wessel 84253197fc4SJason Wessel /* 84353197fc4SJason Wessel * This function performs all gdbserial command procesing 84453197fc4SJason Wessel */ 84553197fc4SJason Wessel int gdb_serial_stub(struct kgdb_state *ks) 84653197fc4SJason Wessel { 84753197fc4SJason Wessel int error = 0; 84853197fc4SJason Wessel int tmp; 84953197fc4SJason Wessel 85053197fc4SJason Wessel /* Clear the out buffer. */ 85153197fc4SJason Wessel memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); 85253197fc4SJason Wessel 85353197fc4SJason Wessel if (kgdb_connected) { 85453197fc4SJason Wessel unsigned char thref[8]; 85553197fc4SJason Wessel char *ptr; 85653197fc4SJason Wessel 85753197fc4SJason Wessel /* Reply to host that an exception has occurred */ 85853197fc4SJason Wessel ptr = remcom_out_buffer; 85953197fc4SJason Wessel *ptr++ = 'T'; 86053197fc4SJason Wessel ptr = pack_hex_byte(ptr, ks->signo); 86153197fc4SJason Wessel ptr += strlen(strcpy(ptr, "thread:")); 86253197fc4SJason Wessel int_to_threadref(thref, shadow_pid(current->pid)); 86353197fc4SJason Wessel ptr = pack_threadid(ptr, thref); 86453197fc4SJason Wessel *ptr++ = ';'; 86553197fc4SJason Wessel put_packet(remcom_out_buffer); 86653197fc4SJason Wessel } 86753197fc4SJason Wessel 86853197fc4SJason Wessel kgdb_usethread = kgdb_info[ks->cpu].task; 86953197fc4SJason Wessel ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid); 87053197fc4SJason Wessel ks->pass_exception = 0; 87153197fc4SJason Wessel 87253197fc4SJason Wessel while (1) { 87353197fc4SJason Wessel error = 0; 87453197fc4SJason Wessel 87553197fc4SJason Wessel /* Clear the out buffer. */ 87653197fc4SJason Wessel memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); 87753197fc4SJason Wessel 87853197fc4SJason Wessel get_packet(remcom_in_buffer); 87953197fc4SJason Wessel 88053197fc4SJason Wessel switch (remcom_in_buffer[0]) { 88153197fc4SJason Wessel case '?': /* gdbserial status */ 88253197fc4SJason Wessel gdb_cmd_status(ks); 88353197fc4SJason Wessel break; 88453197fc4SJason Wessel case 'g': /* return the value of the CPU registers */ 88553197fc4SJason Wessel gdb_cmd_getregs(ks); 88653197fc4SJason Wessel break; 88753197fc4SJason Wessel case 'G': /* set the value of the CPU registers - return OK */ 88853197fc4SJason Wessel gdb_cmd_setregs(ks); 88953197fc4SJason Wessel break; 89053197fc4SJason Wessel case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ 89153197fc4SJason Wessel gdb_cmd_memread(ks); 89253197fc4SJason Wessel break; 89353197fc4SJason Wessel case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */ 89453197fc4SJason Wessel gdb_cmd_memwrite(ks); 89553197fc4SJason Wessel break; 89653197fc4SJason Wessel case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */ 89753197fc4SJason Wessel gdb_cmd_binwrite(ks); 89853197fc4SJason Wessel break; 89953197fc4SJason Wessel /* kill or detach. KGDB should treat this like a 90053197fc4SJason Wessel * continue. 90153197fc4SJason Wessel */ 90253197fc4SJason Wessel case 'D': /* Debugger detach */ 90353197fc4SJason Wessel case 'k': /* Debugger detach via kill */ 90453197fc4SJason Wessel gdb_cmd_detachkill(ks); 90553197fc4SJason Wessel goto default_handle; 90653197fc4SJason Wessel case 'R': /* Reboot */ 90753197fc4SJason Wessel if (gdb_cmd_reboot(ks)) 90853197fc4SJason Wessel goto default_handle; 90953197fc4SJason Wessel break; 91053197fc4SJason Wessel case 'q': /* query command */ 91153197fc4SJason Wessel gdb_cmd_query(ks); 91253197fc4SJason Wessel break; 91353197fc4SJason Wessel case 'H': /* task related */ 91453197fc4SJason Wessel gdb_cmd_task(ks); 91553197fc4SJason Wessel break; 91653197fc4SJason Wessel case 'T': /* Query thread status */ 91753197fc4SJason Wessel gdb_cmd_thread(ks); 91853197fc4SJason Wessel break; 91953197fc4SJason Wessel case 'z': /* Break point remove */ 92053197fc4SJason Wessel case 'Z': /* Break point set */ 92153197fc4SJason Wessel gdb_cmd_break(ks); 92253197fc4SJason Wessel break; 923dcc78711SJason Wessel #ifdef CONFIG_KGDB_KDB 924dcc78711SJason Wessel case '3': /* Escape into back into kdb */ 925dcc78711SJason Wessel if (remcom_in_buffer[1] == '\0') { 926dcc78711SJason Wessel gdb_cmd_detachkill(ks); 927dcc78711SJason Wessel return DBG_PASS_EVENT; 928dcc78711SJason Wessel } 929dcc78711SJason Wessel #endif 93053197fc4SJason Wessel case 'C': /* Exception passing */ 93153197fc4SJason Wessel tmp = gdb_cmd_exception_pass(ks); 93253197fc4SJason Wessel if (tmp > 0) 93353197fc4SJason Wessel goto default_handle; 93453197fc4SJason Wessel if (tmp == 0) 93553197fc4SJason Wessel break; 93653197fc4SJason Wessel /* Fall through on tmp < 0 */ 93753197fc4SJason Wessel case 'c': /* Continue packet */ 93853197fc4SJason Wessel case 's': /* Single step packet */ 93953197fc4SJason Wessel if (kgdb_contthread && kgdb_contthread != current) { 94053197fc4SJason Wessel /* Can't switch threads in kgdb */ 94153197fc4SJason Wessel error_packet(remcom_out_buffer, -EINVAL); 94253197fc4SJason Wessel break; 94353197fc4SJason Wessel } 94453197fc4SJason Wessel dbg_activate_sw_breakpoints(); 94553197fc4SJason Wessel /* Fall through to default processing */ 94653197fc4SJason Wessel default: 94753197fc4SJason Wessel default_handle: 94853197fc4SJason Wessel error = kgdb_arch_handle_exception(ks->ex_vector, 94953197fc4SJason Wessel ks->signo, 95053197fc4SJason Wessel ks->err_code, 95153197fc4SJason Wessel remcom_in_buffer, 95253197fc4SJason Wessel remcom_out_buffer, 95353197fc4SJason Wessel ks->linux_regs); 95453197fc4SJason Wessel /* 95553197fc4SJason Wessel * Leave cmd processing on error, detach, 95653197fc4SJason Wessel * kill, continue, or single step. 95753197fc4SJason Wessel */ 95853197fc4SJason Wessel if (error >= 0 || remcom_in_buffer[0] == 'D' || 95953197fc4SJason Wessel remcom_in_buffer[0] == 'k') { 96053197fc4SJason Wessel error = 0; 96153197fc4SJason Wessel goto kgdb_exit; 96253197fc4SJason Wessel } 96353197fc4SJason Wessel 96453197fc4SJason Wessel } 96553197fc4SJason Wessel 96653197fc4SJason Wessel /* reply to the request */ 96753197fc4SJason Wessel put_packet(remcom_out_buffer); 96853197fc4SJason Wessel } 96953197fc4SJason Wessel 97053197fc4SJason Wessel kgdb_exit: 97153197fc4SJason Wessel if (ks->pass_exception) 97253197fc4SJason Wessel error = 1; 97353197fc4SJason Wessel return error; 97453197fc4SJason Wessel } 975dcc78711SJason Wessel 976dcc78711SJason Wessel int gdbstub_state(struct kgdb_state *ks, char *cmd) 977dcc78711SJason Wessel { 978dcc78711SJason Wessel int error; 979dcc78711SJason Wessel 980dcc78711SJason Wessel switch (cmd[0]) { 981dcc78711SJason Wessel case 'e': 982dcc78711SJason Wessel error = kgdb_arch_handle_exception(ks->ex_vector, 983dcc78711SJason Wessel ks->signo, 984dcc78711SJason Wessel ks->err_code, 985dcc78711SJason Wessel remcom_in_buffer, 986dcc78711SJason Wessel remcom_out_buffer, 987dcc78711SJason Wessel ks->linux_regs); 988dcc78711SJason Wessel return error; 989dcc78711SJason Wessel case 's': 990dcc78711SJason Wessel case 'c': 991dcc78711SJason Wessel strcpy(remcom_in_buffer, cmd); 992dcc78711SJason Wessel return 0; 993dcc78711SJason Wessel case '?': 994dcc78711SJason Wessel gdb_cmd_status(ks); 995dcc78711SJason Wessel break; 996dcc78711SJason Wessel case '\0': 997dcc78711SJason Wessel strcpy(remcom_out_buffer, ""); 998dcc78711SJason Wessel break; 999dcc78711SJason Wessel } 1000dcc78711SJason Wessel dbg_io_ops->write_char('+'); 1001dcc78711SJason Wessel put_packet(remcom_out_buffer); 1002dcc78711SJason Wessel return 0; 1003dcc78711SJason Wessel } 1004