Merge branch 'sched/core'
[linux-2.6/x86.git] / kernel / debug / gdbstub.c
bloba11db956dd62c4c5a5fa22699ccd3662a88f00bc
1 /*
2 * Kernel Debug Core
4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
6 * Copyright (C) 2000-2001 VERITAS Software Corporation.
7 * Copyright (C) 2002-2004 Timesys Corporation
8 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
9 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz>
10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
12 * Copyright (C) 2005-2009 Wind River Systems, Inc.
13 * Copyright (C) 2007 MontaVista Software, Inc.
14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
16 * Contributors at various stages not listed above:
17 * Jason Wessel ( jason.wessel@windriver.com )
18 * George Anzinger <george@mvista.com>
19 * Anurekh Saxena (anurekh.saxena@timesys.com)
20 * Lake Stevens Instrument Division (Glenn Engel)
21 * Jim Kingdon, Cygnus Support.
23 * Original KGDB stub: David Grothe <dave@gcom.com>,
24 * Tigran Aivazian <tigran@sco.com>
26 * This file is licensed under the terms of the GNU General Public License
27 * version 2. This program is licensed "as is" without any warranty of any
28 * kind, whether express or implied.
31 #include <linux/kernel.h>
32 #include <linux/kgdb.h>
33 #include <linux/kdb.h>
34 #include <linux/reboot.h>
35 #include <linux/uaccess.h>
36 #include <asm/cacheflush.h>
37 #include <asm/unaligned.h>
38 #include "debug_core.h"
40 #define KGDB_MAX_THREAD_QUERY 17
42 /* Our I/O buffers. */
43 static char remcom_in_buffer[BUFMAX];
44 static char remcom_out_buffer[BUFMAX];
46 /* Storage for the registers, in GDB format. */
47 static unsigned long gdb_regs[(NUMREGBYTES +
48 sizeof(unsigned long) - 1) /
49 sizeof(unsigned long)];
52 * GDB remote protocol parser:
55 #ifdef CONFIG_KGDB_KDB
56 static int gdbstub_read_wait(void)
58 int ret = -1;
59 int i;
61 /* poll any additional I/O interfaces that are defined */
62 while (ret < 0)
63 for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
64 ret = kdb_poll_funcs[i]();
65 if (ret > 0)
66 break;
68 return ret;
70 #else
71 static int gdbstub_read_wait(void)
73 int ret = dbg_io_ops->read_char();
74 while (ret == NO_POLL_CHAR)
75 ret = dbg_io_ops->read_char();
76 return ret;
78 #endif
79 /* scan for the sequence $<data>#<checksum> */
80 static void get_packet(char *buffer)
82 unsigned char checksum;
83 unsigned char xmitcsum;
84 int count;
85 char ch;
87 do {
89 * Spin and wait around for the start character, ignore all
90 * other characters:
92 while ((ch = (gdbstub_read_wait())) != '$')
93 /* nothing */;
95 kgdb_connected = 1;
96 checksum = 0;
97 xmitcsum = -1;
99 count = 0;
102 * now, read until a # or end of buffer is found:
104 while (count < (BUFMAX - 1)) {
105 ch = gdbstub_read_wait();
106 if (ch == '#')
107 break;
108 checksum = checksum + ch;
109 buffer[count] = ch;
110 count = count + 1;
112 buffer[count] = 0;
114 if (ch == '#') {
115 xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4;
116 xmitcsum += hex_to_bin(gdbstub_read_wait());
118 if (checksum != xmitcsum)
119 /* failed checksum */
120 dbg_io_ops->write_char('-');
121 else
122 /* successful transfer */
123 dbg_io_ops->write_char('+');
124 if (dbg_io_ops->flush)
125 dbg_io_ops->flush();
127 } while (checksum != xmitcsum);
131 * Send the packet in buffer.
132 * Check for gdb connection if asked for.
134 static void put_packet(char *buffer)
136 unsigned char checksum;
137 int count;
138 char ch;
141 * $<packet info>#<checksum>.
143 while (1) {
144 dbg_io_ops->write_char('$');
145 checksum = 0;
146 count = 0;
148 while ((ch = buffer[count])) {
149 dbg_io_ops->write_char(ch);
150 checksum += ch;
151 count++;
154 dbg_io_ops->write_char('#');
155 dbg_io_ops->write_char(hex_asc_hi(checksum));
156 dbg_io_ops->write_char(hex_asc_lo(checksum));
157 if (dbg_io_ops->flush)
158 dbg_io_ops->flush();
160 /* Now see what we get in reply. */
161 ch = gdbstub_read_wait();
163 if (ch == 3)
164 ch = gdbstub_read_wait();
166 /* If we get an ACK, we are done. */
167 if (ch == '+')
168 return;
171 * If we get the start of another packet, this means
172 * that GDB is attempting to reconnect. We will NAK
173 * the packet being sent, and stop trying to send this
174 * packet.
176 if (ch == '$') {
177 dbg_io_ops->write_char('-');
178 if (dbg_io_ops->flush)
179 dbg_io_ops->flush();
180 return;
185 static char gdbmsgbuf[BUFMAX + 1];
187 void gdbstub_msg_write(const char *s, int len)
189 char *bufptr;
190 int wcount;
191 int i;
193 if (len == 0)
194 len = strlen(s);
196 /* 'O'utput */
197 gdbmsgbuf[0] = 'O';
199 /* Fill and send buffers... */
200 while (len > 0) {
201 bufptr = gdbmsgbuf + 1;
203 /* Calculate how many this time */
204 if ((len << 1) > (BUFMAX - 2))
205 wcount = (BUFMAX - 2) >> 1;
206 else
207 wcount = len;
209 /* Pack in hex chars */
210 for (i = 0; i < wcount; i++)
211 bufptr = pack_hex_byte(bufptr, s[i]);
212 *bufptr = '\0';
214 /* Move up */
215 s += wcount;
216 len -= wcount;
218 /* Write packet */
219 put_packet(gdbmsgbuf);
224 * Convert the memory pointed to by mem into hex, placing result in
225 * buf. Return a pointer to the last char put in buf (null). May
226 * return an error.
228 char *kgdb_mem2hex(char *mem, char *buf, int count)
230 char *tmp;
231 int err;
234 * We use the upper half of buf as an intermediate buffer for the
235 * raw memory copy. Hex conversion will work against this one.
237 tmp = buf + count;
239 err = probe_kernel_read(tmp, mem, count);
240 if (err)
241 return NULL;
242 while (count > 0) {
243 buf = pack_hex_byte(buf, *tmp);
244 tmp++;
245 count--;
247 *buf = 0;
249 return buf;
253 * Convert the hex array pointed to by buf into binary to be placed in
254 * mem. Return a pointer to the character AFTER the last byte
255 * written. May return an error.
257 int kgdb_hex2mem(char *buf, char *mem, int count)
259 char *tmp_raw;
260 char *tmp_hex;
263 * We use the upper half of buf as an intermediate buffer for the
264 * raw memory that is converted from hex.
266 tmp_raw = buf + count * 2;
268 tmp_hex = tmp_raw - 1;
269 while (tmp_hex >= buf) {
270 tmp_raw--;
271 *tmp_raw = hex_to_bin(*tmp_hex--);
272 *tmp_raw |= hex_to_bin(*tmp_hex--) << 4;
275 return probe_kernel_write(mem, tmp_raw, count);
279 * While we find nice hex chars, build a long_val.
280 * Return number of chars processed.
282 int kgdb_hex2long(char **ptr, unsigned long *long_val)
284 int hex_val;
285 int num = 0;
286 int negate = 0;
288 *long_val = 0;
290 if (**ptr == '-') {
291 negate = 1;
292 (*ptr)++;
294 while (**ptr) {
295 hex_val = hex_to_bin(**ptr);
296 if (hex_val < 0)
297 break;
299 *long_val = (*long_val << 4) | hex_val;
300 num++;
301 (*ptr)++;
304 if (negate)
305 *long_val = -*long_val;
307 return num;
311 * Copy the binary array pointed to by buf into mem. Fix $, #, and
312 * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
313 * The input buf is overwitten with the result to write to mem.
315 static int kgdb_ebin2mem(char *buf, char *mem, int count)
317 int size = 0;
318 char *c = buf;
320 while (count-- > 0) {
321 c[size] = *buf++;
322 if (c[size] == 0x7d)
323 c[size] = *buf++ ^ 0x20;
324 size++;
327 return probe_kernel_write(mem, c, size);
330 #if DBG_MAX_REG_NUM > 0
331 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
333 int i;
334 int idx = 0;
335 char *ptr = (char *)gdb_regs;
337 for (i = 0; i < DBG_MAX_REG_NUM; i++) {
338 dbg_get_reg(i, ptr + idx, regs);
339 idx += dbg_reg_def[i].size;
343 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
345 int i;
346 int idx = 0;
347 char *ptr = (char *)gdb_regs;
349 for (i = 0; i < DBG_MAX_REG_NUM; i++) {
350 dbg_set_reg(i, ptr + idx, regs);
351 idx += dbg_reg_def[i].size;
354 #endif /* DBG_MAX_REG_NUM > 0 */
356 /* Write memory due to an 'M' or 'X' packet. */
357 static int write_mem_msg(int binary)
359 char *ptr = &remcom_in_buffer[1];
360 unsigned long addr;
361 unsigned long length;
362 int err;
364 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
365 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
366 if (binary)
367 err = kgdb_ebin2mem(ptr, (char *)addr, length);
368 else
369 err = kgdb_hex2mem(ptr, (char *)addr, length);
370 if (err)
371 return err;
372 if (CACHE_FLUSH_IS_SAFE)
373 flush_icache_range(addr, addr + length);
374 return 0;
377 return -EINVAL;
380 static void error_packet(char *pkt, int error)
382 error = -error;
383 pkt[0] = 'E';
384 pkt[1] = hex_asc[(error / 10)];
385 pkt[2] = hex_asc[(error % 10)];
386 pkt[3] = '\0';
390 * Thread ID accessors. We represent a flat TID space to GDB, where
391 * the per CPU idle threads (which under Linux all have PID 0) are
392 * remapped to negative TIDs.
395 #define BUF_THREAD_ID_SIZE 8
397 static char *pack_threadid(char *pkt, unsigned char *id)
399 unsigned char *limit;
400 int lzero = 1;
402 limit = id + (BUF_THREAD_ID_SIZE / 2);
403 while (id < limit) {
404 if (!lzero || *id != 0) {
405 pkt = pack_hex_byte(pkt, *id);
406 lzero = 0;
408 id++;
411 if (lzero)
412 pkt = pack_hex_byte(pkt, 0);
414 return pkt;
417 static void int_to_threadref(unsigned char *id, int value)
419 put_unaligned_be32(value, id);
422 static struct task_struct *getthread(struct pt_regs *regs, int tid)
425 * Non-positive TIDs are remapped to the cpu shadow information
427 if (tid == 0 || tid == -1)
428 tid = -atomic_read(&kgdb_active) - 2;
429 if (tid < -1 && tid > -NR_CPUS - 2) {
430 if (kgdb_info[-tid - 2].task)
431 return kgdb_info[-tid - 2].task;
432 else
433 return idle_task(-tid - 2);
435 if (tid <= 0) {
436 printk(KERN_ERR "KGDB: Internal thread select error\n");
437 dump_stack();
438 return NULL;
442 * find_task_by_pid_ns() does not take the tasklist lock anymore
443 * but is nicely RCU locked - hence is a pretty resilient
444 * thing to use:
446 return find_task_by_pid_ns(tid, &init_pid_ns);
451 * Remap normal tasks to their real PID,
452 * CPU shadow threads are mapped to -CPU - 2
454 static inline int shadow_pid(int realpid)
456 if (realpid)
457 return realpid;
459 return -raw_smp_processor_id() - 2;
463 * All the functions that start with gdb_cmd are the various
464 * operations to implement the handlers for the gdbserial protocol
465 * where KGDB is communicating with an external debugger
468 /* Handle the '?' status packets */
469 static void gdb_cmd_status(struct kgdb_state *ks)
472 * We know that this packet is only sent
473 * during initial connect. So to be safe,
474 * we clear out our breakpoints now in case
475 * GDB is reconnecting.
477 dbg_remove_all_break();
479 remcom_out_buffer[0] = 'S';
480 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
483 static void gdb_get_regs_helper(struct kgdb_state *ks)
485 struct task_struct *thread;
486 void *local_debuggerinfo;
487 int i;
489 thread = kgdb_usethread;
490 if (!thread) {
491 thread = kgdb_info[ks->cpu].task;
492 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
493 } else {
494 local_debuggerinfo = NULL;
495 for_each_online_cpu(i) {
497 * Try to find the task on some other
498 * or possibly this node if we do not
499 * find the matching task then we try
500 * to approximate the results.
502 if (thread == kgdb_info[i].task)
503 local_debuggerinfo = kgdb_info[i].debuggerinfo;
508 * All threads that don't have debuggerinfo should be
509 * in schedule() sleeping, since all other CPUs
510 * are in kgdb_wait, and thus have debuggerinfo.
512 if (local_debuggerinfo) {
513 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
514 } else {
516 * Pull stuff saved during switch_to; nothing
517 * else is accessible (or even particularly
518 * relevant).
520 * This should be enough for a stack trace.
522 sleeping_thread_to_gdb_regs(gdb_regs, thread);
526 /* Handle the 'g' get registers request */
527 static void gdb_cmd_getregs(struct kgdb_state *ks)
529 gdb_get_regs_helper(ks);
530 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
533 /* Handle the 'G' set registers request */
534 static void gdb_cmd_setregs(struct kgdb_state *ks)
536 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
538 if (kgdb_usethread && kgdb_usethread != current) {
539 error_packet(remcom_out_buffer, -EINVAL);
540 } else {
541 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
542 strcpy(remcom_out_buffer, "OK");
546 /* Handle the 'm' memory read bytes */
547 static void gdb_cmd_memread(struct kgdb_state *ks)
549 char *ptr = &remcom_in_buffer[1];
550 unsigned long length;
551 unsigned long addr;
552 char *err;
554 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
555 kgdb_hex2long(&ptr, &length) > 0) {
556 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
557 if (!err)
558 error_packet(remcom_out_buffer, -EINVAL);
559 } else {
560 error_packet(remcom_out_buffer, -EINVAL);
564 /* Handle the 'M' memory write bytes */
565 static void gdb_cmd_memwrite(struct kgdb_state *ks)
567 int err = write_mem_msg(0);
569 if (err)
570 error_packet(remcom_out_buffer, err);
571 else
572 strcpy(remcom_out_buffer, "OK");
575 #if DBG_MAX_REG_NUM > 0
576 static char *gdb_hex_reg_helper(int regnum, char *out)
578 int i;
579 int offset = 0;
581 for (i = 0; i < regnum; i++)
582 offset += dbg_reg_def[i].size;
583 return kgdb_mem2hex((char *)gdb_regs + offset, out,
584 dbg_reg_def[i].size);
587 /* Handle the 'p' individual regster get */
588 static void gdb_cmd_reg_get(struct kgdb_state *ks)
590 unsigned long regnum;
591 char *ptr = &remcom_in_buffer[1];
593 kgdb_hex2long(&ptr, &regnum);
594 if (regnum >= DBG_MAX_REG_NUM) {
595 error_packet(remcom_out_buffer, -EINVAL);
596 return;
598 gdb_get_regs_helper(ks);
599 gdb_hex_reg_helper(regnum, remcom_out_buffer);
602 /* Handle the 'P' individual regster set */
603 static void gdb_cmd_reg_set(struct kgdb_state *ks)
605 unsigned long regnum;
606 char *ptr = &remcom_in_buffer[1];
607 int i = 0;
609 kgdb_hex2long(&ptr, &regnum);
610 if (*ptr++ != '=' ||
611 !(!kgdb_usethread || kgdb_usethread == current) ||
612 !dbg_get_reg(regnum, gdb_regs, ks->linux_regs)) {
613 error_packet(remcom_out_buffer, -EINVAL);
614 return;
616 memset(gdb_regs, 0, sizeof(gdb_regs));
617 while (i < sizeof(gdb_regs) * 2)
618 if (hex_to_bin(ptr[i]) >= 0)
619 i++;
620 else
621 break;
622 i = i / 2;
623 kgdb_hex2mem(ptr, (char *)gdb_regs, i);
624 dbg_set_reg(regnum, gdb_regs, ks->linux_regs);
625 strcpy(remcom_out_buffer, "OK");
627 #endif /* DBG_MAX_REG_NUM > 0 */
629 /* Handle the 'X' memory binary write bytes */
630 static void gdb_cmd_binwrite(struct kgdb_state *ks)
632 int err = write_mem_msg(1);
634 if (err)
635 error_packet(remcom_out_buffer, err);
636 else
637 strcpy(remcom_out_buffer, "OK");
640 /* Handle the 'D' or 'k', detach or kill packets */
641 static void gdb_cmd_detachkill(struct kgdb_state *ks)
643 int error;
645 /* The detach case */
646 if (remcom_in_buffer[0] == 'D') {
647 error = dbg_remove_all_break();
648 if (error < 0) {
649 error_packet(remcom_out_buffer, error);
650 } else {
651 strcpy(remcom_out_buffer, "OK");
652 kgdb_connected = 0;
654 put_packet(remcom_out_buffer);
655 } else {
657 * Assume the kill case, with no exit code checking,
658 * trying to force detach the debugger:
660 dbg_remove_all_break();
661 kgdb_connected = 0;
665 /* Handle the 'R' reboot packets */
666 static int gdb_cmd_reboot(struct kgdb_state *ks)
668 /* For now, only honor R0 */
669 if (strcmp(remcom_in_buffer, "R0") == 0) {
670 printk(KERN_CRIT "Executing emergency reboot\n");
671 strcpy(remcom_out_buffer, "OK");
672 put_packet(remcom_out_buffer);
675 * Execution should not return from
676 * machine_emergency_restart()
678 machine_emergency_restart();
679 kgdb_connected = 0;
681 return 1;
683 return 0;
686 /* Handle the 'q' query packets */
687 static void gdb_cmd_query(struct kgdb_state *ks)
689 struct task_struct *g;
690 struct task_struct *p;
691 unsigned char thref[BUF_THREAD_ID_SIZE];
692 char *ptr;
693 int i;
694 int cpu;
695 int finished = 0;
697 switch (remcom_in_buffer[1]) {
698 case 's':
699 case 'f':
700 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10))
701 break;
703 i = 0;
704 remcom_out_buffer[0] = 'm';
705 ptr = remcom_out_buffer + 1;
706 if (remcom_in_buffer[1] == 'f') {
707 /* Each cpu is a shadow thread */
708 for_each_online_cpu(cpu) {
709 ks->thr_query = 0;
710 int_to_threadref(thref, -cpu - 2);
711 ptr = pack_threadid(ptr, thref);
712 *(ptr++) = ',';
713 i++;
717 do_each_thread(g, p) {
718 if (i >= ks->thr_query && !finished) {
719 int_to_threadref(thref, p->pid);
720 ptr = pack_threadid(ptr, thref);
721 *(ptr++) = ',';
722 ks->thr_query++;
723 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
724 finished = 1;
726 i++;
727 } while_each_thread(g, p);
729 *(--ptr) = '\0';
730 break;
732 case 'C':
733 /* Current thread id */
734 strcpy(remcom_out_buffer, "QC");
735 ks->threadid = shadow_pid(current->pid);
736 int_to_threadref(thref, ks->threadid);
737 pack_threadid(remcom_out_buffer + 2, thref);
738 break;
739 case 'T':
740 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16))
741 break;
743 ks->threadid = 0;
744 ptr = remcom_in_buffer + 17;
745 kgdb_hex2long(&ptr, &ks->threadid);
746 if (!getthread(ks->linux_regs, ks->threadid)) {
747 error_packet(remcom_out_buffer, -EINVAL);
748 break;
750 if ((int)ks->threadid > 0) {
751 kgdb_mem2hex(getthread(ks->linux_regs,
752 ks->threadid)->comm,
753 remcom_out_buffer, 16);
754 } else {
755 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
757 sprintf(tmpstr, "shadowCPU%d",
758 (int)(-ks->threadid - 2));
759 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
761 break;
762 #ifdef CONFIG_KGDB_KDB
763 case 'R':
764 if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
765 int len = strlen(remcom_in_buffer + 6);
767 if ((len % 2) != 0) {
768 strcpy(remcom_out_buffer, "E01");
769 break;
771 kgdb_hex2mem(remcom_in_buffer + 6,
772 remcom_out_buffer, len);
773 len = len / 2;
774 remcom_out_buffer[len++] = 0;
776 kdb_parse(remcom_out_buffer);
777 strcpy(remcom_out_buffer, "OK");
779 break;
780 #endif
784 /* Handle the 'H' task query packets */
785 static void gdb_cmd_task(struct kgdb_state *ks)
787 struct task_struct *thread;
788 char *ptr;
790 switch (remcom_in_buffer[1]) {
791 case 'g':
792 ptr = &remcom_in_buffer[2];
793 kgdb_hex2long(&ptr, &ks->threadid);
794 thread = getthread(ks->linux_regs, ks->threadid);
795 if (!thread && ks->threadid > 0) {
796 error_packet(remcom_out_buffer, -EINVAL);
797 break;
799 kgdb_usethread = thread;
800 ks->kgdb_usethreadid = ks->threadid;
801 strcpy(remcom_out_buffer, "OK");
802 break;
803 case 'c':
804 ptr = &remcom_in_buffer[2];
805 kgdb_hex2long(&ptr, &ks->threadid);
806 if (!ks->threadid) {
807 kgdb_contthread = NULL;
808 } else {
809 thread = getthread(ks->linux_regs, ks->threadid);
810 if (!thread && ks->threadid > 0) {
811 error_packet(remcom_out_buffer, -EINVAL);
812 break;
814 kgdb_contthread = thread;
816 strcpy(remcom_out_buffer, "OK");
817 break;
821 /* Handle the 'T' thread query packets */
822 static void gdb_cmd_thread(struct kgdb_state *ks)
824 char *ptr = &remcom_in_buffer[1];
825 struct task_struct *thread;
827 kgdb_hex2long(&ptr, &ks->threadid);
828 thread = getthread(ks->linux_regs, ks->threadid);
829 if (thread)
830 strcpy(remcom_out_buffer, "OK");
831 else
832 error_packet(remcom_out_buffer, -EINVAL);
835 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
836 static void gdb_cmd_break(struct kgdb_state *ks)
839 * Since GDB-5.3, it's been drafted that '0' is a software
840 * breakpoint, '1' is a hardware breakpoint, so let's do that.
842 char *bpt_type = &remcom_in_buffer[1];
843 char *ptr = &remcom_in_buffer[2];
844 unsigned long addr;
845 unsigned long length;
846 int error = 0;
848 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
849 /* Unsupported */
850 if (*bpt_type > '4')
851 return;
852 } else {
853 if (*bpt_type != '0' && *bpt_type != '1')
854 /* Unsupported. */
855 return;
859 * Test if this is a hardware breakpoint, and
860 * if we support it:
862 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
863 /* Unsupported. */
864 return;
866 if (*(ptr++) != ',') {
867 error_packet(remcom_out_buffer, -EINVAL);
868 return;
870 if (!kgdb_hex2long(&ptr, &addr)) {
871 error_packet(remcom_out_buffer, -EINVAL);
872 return;
874 if (*(ptr++) != ',' ||
875 !kgdb_hex2long(&ptr, &length)) {
876 error_packet(remcom_out_buffer, -EINVAL);
877 return;
880 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
881 error = dbg_set_sw_break(addr);
882 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
883 error = dbg_remove_sw_break(addr);
884 else if (remcom_in_buffer[0] == 'Z')
885 error = arch_kgdb_ops.set_hw_breakpoint(addr,
886 (int)length, *bpt_type - '0');
887 else if (remcom_in_buffer[0] == 'z')
888 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
889 (int) length, *bpt_type - '0');
891 if (error == 0)
892 strcpy(remcom_out_buffer, "OK");
893 else
894 error_packet(remcom_out_buffer, error);
897 /* Handle the 'C' signal / exception passing packets */
898 static int gdb_cmd_exception_pass(struct kgdb_state *ks)
900 /* C09 == pass exception
901 * C15 == detach kgdb, pass exception
903 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
905 ks->pass_exception = 1;
906 remcom_in_buffer[0] = 'c';
908 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
910 ks->pass_exception = 1;
911 remcom_in_buffer[0] = 'D';
912 dbg_remove_all_break();
913 kgdb_connected = 0;
914 return 1;
916 } else {
917 gdbstub_msg_write("KGDB only knows signal 9 (pass)"
918 " and 15 (pass and disconnect)\n"
919 "Executing a continue without signal passing\n", 0);
920 remcom_in_buffer[0] = 'c';
923 /* Indicate fall through */
924 return -1;
928 * This function performs all gdbserial command procesing
930 int gdb_serial_stub(struct kgdb_state *ks)
932 int error = 0;
933 int tmp;
935 /* Initialize comm buffer and globals. */
936 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
937 kgdb_usethread = kgdb_info[ks->cpu].task;
938 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
939 ks->pass_exception = 0;
941 if (kgdb_connected) {
942 unsigned char thref[BUF_THREAD_ID_SIZE];
943 char *ptr;
945 /* Reply to host that an exception has occurred */
946 ptr = remcom_out_buffer;
947 *ptr++ = 'T';
948 ptr = pack_hex_byte(ptr, ks->signo);
949 ptr += strlen(strcpy(ptr, "thread:"));
950 int_to_threadref(thref, shadow_pid(current->pid));
951 ptr = pack_threadid(ptr, thref);
952 *ptr++ = ';';
953 put_packet(remcom_out_buffer);
956 while (1) {
957 error = 0;
959 /* Clear the out buffer. */
960 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
962 get_packet(remcom_in_buffer);
964 switch (remcom_in_buffer[0]) {
965 case '?': /* gdbserial status */
966 gdb_cmd_status(ks);
967 break;
968 case 'g': /* return the value of the CPU registers */
969 gdb_cmd_getregs(ks);
970 break;
971 case 'G': /* set the value of the CPU registers - return OK */
972 gdb_cmd_setregs(ks);
973 break;
974 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
975 gdb_cmd_memread(ks);
976 break;
977 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
978 gdb_cmd_memwrite(ks);
979 break;
980 #if DBG_MAX_REG_NUM > 0
981 case 'p': /* pXX Return gdb register XX (in hex) */
982 gdb_cmd_reg_get(ks);
983 break;
984 case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */
985 gdb_cmd_reg_set(ks);
986 break;
987 #endif /* DBG_MAX_REG_NUM > 0 */
988 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
989 gdb_cmd_binwrite(ks);
990 break;
991 /* kill or detach. KGDB should treat this like a
992 * continue.
994 case 'D': /* Debugger detach */
995 case 'k': /* Debugger detach via kill */
996 gdb_cmd_detachkill(ks);
997 goto default_handle;
998 case 'R': /* Reboot */
999 if (gdb_cmd_reboot(ks))
1000 goto default_handle;
1001 break;
1002 case 'q': /* query command */
1003 gdb_cmd_query(ks);
1004 break;
1005 case 'H': /* task related */
1006 gdb_cmd_task(ks);
1007 break;
1008 case 'T': /* Query thread status */
1009 gdb_cmd_thread(ks);
1010 break;
1011 case 'z': /* Break point remove */
1012 case 'Z': /* Break point set */
1013 gdb_cmd_break(ks);
1014 break;
1015 #ifdef CONFIG_KGDB_KDB
1016 case '3': /* Escape into back into kdb */
1017 if (remcom_in_buffer[1] == '\0') {
1018 gdb_cmd_detachkill(ks);
1019 return DBG_PASS_EVENT;
1021 #endif
1022 case 'C': /* Exception passing */
1023 tmp = gdb_cmd_exception_pass(ks);
1024 if (tmp > 0)
1025 goto default_handle;
1026 if (tmp == 0)
1027 break;
1028 /* Fall through on tmp < 0 */
1029 case 'c': /* Continue packet */
1030 case 's': /* Single step packet */
1031 if (kgdb_contthread && kgdb_contthread != current) {
1032 /* Can't switch threads in kgdb */
1033 error_packet(remcom_out_buffer, -EINVAL);
1034 break;
1036 dbg_activate_sw_breakpoints();
1037 /* Fall through to default processing */
1038 default:
1039 default_handle:
1040 error = kgdb_arch_handle_exception(ks->ex_vector,
1041 ks->signo,
1042 ks->err_code,
1043 remcom_in_buffer,
1044 remcom_out_buffer,
1045 ks->linux_regs);
1047 * Leave cmd processing on error, detach,
1048 * kill, continue, or single step.
1050 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1051 remcom_in_buffer[0] == 'k') {
1052 error = 0;
1053 goto kgdb_exit;
1058 /* reply to the request */
1059 put_packet(remcom_out_buffer);
1062 kgdb_exit:
1063 if (ks->pass_exception)
1064 error = 1;
1065 return error;
1068 int gdbstub_state(struct kgdb_state *ks, char *cmd)
1070 int error;
1072 switch (cmd[0]) {
1073 case 'e':
1074 error = kgdb_arch_handle_exception(ks->ex_vector,
1075 ks->signo,
1076 ks->err_code,
1077 remcom_in_buffer,
1078 remcom_out_buffer,
1079 ks->linux_regs);
1080 return error;
1081 case 's':
1082 case 'c':
1083 strcpy(remcom_in_buffer, cmd);
1084 return 0;
1085 case '?':
1086 gdb_cmd_status(ks);
1087 break;
1088 case '\0':
1089 strcpy(remcom_out_buffer, "");
1090 break;
1092 dbg_io_ops->write_char('+');
1093 put_packet(remcom_out_buffer);
1094 return 0;
1098 * gdbstub_exit - Send an exit message to GDB
1099 * @status: The exit code to report.
1101 void gdbstub_exit(int status)
1103 unsigned char checksum, ch, buffer[3];
1104 int loop;
1106 buffer[0] = 'W';
1107 buffer[1] = hex_asc_hi(status);
1108 buffer[2] = hex_asc_lo(status);
1110 dbg_io_ops->write_char('$');
1111 checksum = 0;
1113 for (loop = 0; loop < 3; loop++) {
1114 ch = buffer[loop];
1115 checksum += ch;
1116 dbg_io_ops->write_char(ch);
1119 dbg_io_ops->write_char('#');
1120 dbg_io_ops->write_char(hex_asc_hi(checksum));
1121 dbg_io_ops->write_char(hex_asc_lo(checksum));
1123 /* make sure the output is flushed, lest the bootloader clobber it */
1124 dbg_io_ops->flush();