31 #include <linux/kernel.h>
34 #include <linux/reboot.h>
36 #include <asm/cacheflush.h>
37 #include <asm/unaligned.h>
40 #define KGDB_MAX_THREAD_QUERY 17
43 static char remcom_in_buffer[
BUFMAX];
44 static char remcom_out_buffer[
BUFMAX];
45 static int gdbstub_use_prev_in_buf;
46 static int gdbstub_prev_in_buf_pos;
50 sizeof(
unsigned long) - 1) /
51 sizeof(
unsigned long)];
57 #ifdef CONFIG_KGDB_KDB
58 static int gdbstub_read_wait(
void)
63 if (
unlikely(gdbstub_use_prev_in_buf)) {
64 if (gdbstub_prev_in_buf_pos < gdbstub_use_prev_in_buf)
65 return remcom_in_buffer[gdbstub_prev_in_buf_pos++];
67 gdbstub_use_prev_in_buf = 0;
80 static int gdbstub_read_wait(
void)
89 static void get_packet(
char *
buffer)
92 unsigned char xmitcsum;
101 while ((ch = (gdbstub_read_wait())) !=
'$')
113 while (count < (
BUFMAX - 1)) {
114 ch = gdbstub_read_wait();
117 checksum = checksum + ch;
123 xmitcsum =
hex_to_bin(gdbstub_read_wait()) << 4;
126 if (checksum != xmitcsum)
136 }
while (checksum != xmitcsum);
143 static void put_packet(
char *buffer)
157 while ((ch = buffer[count])) {
170 ch = gdbstub_read_wait();
173 ch = gdbstub_read_wait();
194 static char gdbmsgbuf[
BUFMAX + 1];
210 bufptr = gdbmsgbuf + 1;
213 if ((len << 1) > (
BUFMAX - 2))
214 wcount = (
BUFMAX - 2) >> 1;
219 for (i = 0; i < wcount; i++)
220 bufptr = hex_byte_pack(bufptr, s[i]);
228 put_packet(gdbmsgbuf);
252 buf = hex_byte_pack(buf, *tmp);
275 tmp_raw = buf + count * 2;
277 tmp_hex = tmp_raw - 1;
278 while (tmp_hex >= buf) {
308 *long_val = (*long_val << 4) | hex_val;
314 *long_val = -*long_val;
324 static int kgdb_ebin2mem(
char *
buf,
char *
mem,
int count)
329 while (count-- > 0) {
332 c[
size] = *buf++ ^ 0x20;
339 #if DBG_MAX_REG_NUM > 0
344 char *
ptr = (
char *)gdb_regs;
356 char *ptr = (
char *)gdb_regs;
366 static int write_mem_msg(
int binary)
368 char *ptr = &remcom_in_buffer[1];
376 err = kgdb_ebin2mem(ptr, (
char *)addr, length);
389 static void error_packet(
char *pkt,
int error)
393 pkt[1] =
hex_asc[(error / 10)];
394 pkt[2] =
hex_asc[(error % 10)];
404 #define BUF_THREAD_ID_SIZE 8
406 static char *pack_threadid(
char *pkt,
unsigned char *
id)
408 unsigned char *
limit;
413 if (!lzero || *
id != 0) {
414 pkt = hex_byte_pack(pkt, *
id);
421 pkt = hex_byte_pack(pkt, 0);
426 static void int_to_threadref(
unsigned char *
id,
int value)
436 if (tid == 0 || tid == -1)
438 if (tid < -1 && tid > -
NR_CPUS - 2) {
463 static inline int shadow_pid(
int realpid)
488 remcom_out_buffer[0] =
'S';
489 hex_byte_pack(&remcom_out_buffer[1], ks->
signo);
495 void *local_debuggerinfo;
503 local_debuggerinfo =
NULL;
512 local_debuggerinfo =
kgdb_info[
i].debuggerinfo;
521 if (local_debuggerinfo) {
536 static void gdb_cmd_getregs(
struct kgdb_state *ks)
538 gdb_get_regs_helper(ks);
543 static void gdb_cmd_setregs(
struct kgdb_state *ks)
548 error_packet(remcom_out_buffer, -
EINVAL);
551 strcpy(remcom_out_buffer,
"OK");
556 static void gdb_cmd_memread(
struct kgdb_state *ks)
558 char *ptr = &remcom_in_buffer[1];
565 err =
kgdb_mem2hex((
char *)addr, remcom_out_buffer, length);
567 error_packet(remcom_out_buffer, -
EINVAL);
569 error_packet(remcom_out_buffer, -
EINVAL);
574 static void gdb_cmd_memwrite(
struct kgdb_state *ks)
576 int err = write_mem_msg(0);
579 error_packet(remcom_out_buffer, err);
581 strcpy(remcom_out_buffer,
"OK");
584 #if DBG_MAX_REG_NUM > 0
585 static char *gdb_hex_reg_helper(
int regnum,
char *
out)
590 for (i = 0; i < regnum; i++)
597 static void gdb_cmd_reg_get(
struct kgdb_state *ks)
599 unsigned long regnum;
600 char *ptr = &remcom_in_buffer[1];
604 error_packet(remcom_out_buffer, -
EINVAL);
607 gdb_get_regs_helper(ks);
608 gdb_hex_reg_helper(regnum, remcom_out_buffer);
612 static void gdb_cmd_reg_set(
struct kgdb_state *ks)
614 unsigned long regnum;
615 char *ptr = &remcom_in_buffer[1];
622 error_packet(remcom_out_buffer, -
EINVAL);
625 memset(gdb_regs, 0,
sizeof(gdb_regs));
626 while (i <
sizeof(gdb_regs) * 2)
634 strcpy(remcom_out_buffer,
"OK");
639 static void gdb_cmd_binwrite(
struct kgdb_state *ks)
641 int err = write_mem_msg(1);
644 error_packet(remcom_out_buffer, err);
646 strcpy(remcom_out_buffer,
"OK");
650 static void gdb_cmd_detachkill(
struct kgdb_state *ks)
655 if (remcom_in_buffer[0] ==
'D') {
658 error_packet(remcom_out_buffer, error);
660 strcpy(remcom_out_buffer,
"OK");
663 put_packet(remcom_out_buffer);
675 static int gdb_cmd_reboot(
struct kgdb_state *ks)
678 if (
strcmp(remcom_in_buffer,
"R0") == 0) {
680 strcpy(remcom_out_buffer,
"OK");
681 put_packet(remcom_out_buffer);
696 static void gdb_cmd_query(
struct kgdb_state *ks)
706 switch (remcom_in_buffer[1]) {
709 if (
memcmp(remcom_in_buffer + 2,
"ThreadInfo", 10))
713 remcom_out_buffer[0] =
'm';
714 ptr = remcom_out_buffer + 1;
715 if (remcom_in_buffer[1] ==
'f') {
719 int_to_threadref(thref, -cpu - 2);
720 ptr = pack_threadid(ptr, thref);
728 int_to_threadref(thref, p->
pid);
729 ptr = pack_threadid(ptr, thref);
743 strcpy(remcom_out_buffer,
"QC");
745 int_to_threadref(thref, ks->
threadid);
746 pack_threadid(remcom_out_buffer + 2, thref);
749 if (
memcmp(remcom_in_buffer + 1,
"ThreadExtraInfo,", 16))
753 ptr = remcom_in_buffer + 17;
756 error_packet(remcom_out_buffer, -
EINVAL);
762 remcom_out_buffer, 16);
771 #ifdef CONFIG_KGDB_KDB
773 if (
strncmp(remcom_in_buffer,
"qRcmd,", 6) == 0) {
776 if ((len % 2) != 0) {
777 strcpy(remcom_out_buffer,
"E01");
781 remcom_out_buffer, len);
783 remcom_out_buffer[len++] = 0;
786 strcpy(remcom_out_buffer,
"OK");
794 static void gdb_cmd_task(
struct kgdb_state *ks)
799 switch (remcom_in_buffer[1]) {
801 ptr = &remcom_in_buffer[2];
805 error_packet(remcom_out_buffer, -
EINVAL);
810 strcpy(remcom_out_buffer,
"OK");
813 ptr = &remcom_in_buffer[2];
820 error_packet(remcom_out_buffer, -
EINVAL);
825 strcpy(remcom_out_buffer,
"OK");
831 static void gdb_cmd_thread(
struct kgdb_state *ks)
833 char *ptr = &remcom_in_buffer[1];
839 strcpy(remcom_out_buffer,
"OK");
841 error_packet(remcom_out_buffer, -
EINVAL);
845 static void gdb_cmd_break(
struct kgdb_state *ks)
851 char *bpt_type = &remcom_in_buffer[1];
852 char *ptr = &remcom_in_buffer[2];
862 if (*bpt_type !=
'0' && *bpt_type !=
'1')
871 if (*bpt_type ==
'1' && !(
arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
875 if (*(ptr++) !=
',') {
876 error_packet(remcom_out_buffer, -
EINVAL);
880 error_packet(remcom_out_buffer, -
EINVAL);
883 if (*(ptr++) !=
',' ||
885 error_packet(remcom_out_buffer, -
EINVAL);
889 if (remcom_in_buffer[0] ==
'Z' && *bpt_type ==
'0')
891 else if (remcom_in_buffer[0] ==
'z' && *bpt_type ==
'0')
893 else if (remcom_in_buffer[0] ==
'Z')
895 (
int)length, *bpt_type -
'0');
896 else if (remcom_in_buffer[0] ==
'z')
898 (
int) length, *bpt_type -
'0');
901 strcpy(remcom_out_buffer,
"OK");
903 error_packet(remcom_out_buffer, error);
907 static int gdb_cmd_exception_pass(
struct kgdb_state *ks)
912 if (remcom_in_buffer[1] ==
'0' && remcom_in_buffer[2] ==
'9') {
915 remcom_in_buffer[0] =
'c';
917 }
else if (remcom_in_buffer[1] ==
'1' && remcom_in_buffer[2] ==
'5') {
920 remcom_in_buffer[0] =
'D';
927 " and 15 (pass and disconnect)\n"
928 "Executing a continue without signal passing\n", 0);
929 remcom_in_buffer[0] =
'c';
945 memset(remcom_out_buffer, 0,
sizeof(remcom_out_buffer));
955 ptr = remcom_out_buffer;
957 ptr = hex_byte_pack(ptr, ks->
signo);
959 int_to_threadref(thref, shadow_pid(
current->pid));
960 ptr = pack_threadid(ptr, thref);
962 put_packet(remcom_out_buffer);
969 memset(remcom_out_buffer, 0,
sizeof(remcom_out_buffer));
971 get_packet(remcom_in_buffer);
973 switch (remcom_in_buffer[0]) {
987 gdb_cmd_memwrite(ks);
989 #if DBG_MAX_REG_NUM > 0
998 gdb_cmd_binwrite(ks);
1005 gdb_cmd_detachkill(ks);
1006 goto default_handle;
1008 if (gdb_cmd_reboot(ks))
1009 goto default_handle;
1024 #ifdef CONFIG_KGDB_KDB
1026 if (remcom_in_buffer[1] ==
'\0') {
1027 gdb_cmd_detachkill(ks);
1032 tmp = gdb_cmd_exception_pass(ks);
1034 goto default_handle;
1042 error_packet(remcom_out_buffer, -
EINVAL);
1059 if (error >= 0 || remcom_in_buffer[0] ==
'D' ||
1060 remcom_in_buffer[0] ==
'k') {
1068 put_packet(remcom_out_buffer);
1092 strcpy(remcom_in_buffer, cmd);
1095 strcpy(remcom_in_buffer, cmd);
1096 gdbstub_use_prev_in_buf =
strlen(remcom_in_buffer);
1097 gdbstub_prev_in_buf_pos = 0;
1101 put_packet(remcom_out_buffer);
1111 unsigned char checksum, ch, buffer[3];
1128 for (loop = 0; loop < 3; loop++) {