123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- #include <common.h>
- #include <kgdb.h>
- #include <command.h>
- #undef KGDB_DEBUG
- #define BUFMAX 1024
- static char remcomInBuffer[BUFMAX];
- static char remcomOutBuffer[BUFMAX];
- static char remcomRegBuffer[BUFMAX];
- static int initialized = 0;
- static int kgdb_active;
- static struct pt_regs entry_regs;
- static long error_jmp_buf[BUFMAX/2];
- static int longjmp_on_fault = 0;
- #ifdef KGDB_DEBUG
- static int kdebug = 1;
- #endif
- static const char hexchars[]="0123456789abcdef";
- static int
- hex(unsigned char ch)
- {
- if (ch >= 'a' && ch <= 'f')
- return ch-'a'+10;
- if (ch >= '0' && ch <= '9')
- return ch-'0';
- if (ch >= 'A' && ch <= 'F')
- return ch-'A'+10;
- return -1;
- }
- static unsigned char *
- mem2hex(char *mem, char *buf, int count)
- {
- char *tmp;
- unsigned char ch;
-
- tmp = buf + count;
- longjmp_on_fault = 1;
- memcpy(tmp, mem, count);
- while (count-- > 0) {
- ch = *tmp++;
- *buf++ = hexchars[ch >> 4];
- *buf++ = hexchars[ch & 0xf];
- }
- *buf = 0;
- longjmp_on_fault = 0;
- return (unsigned char *)buf;
- }
- static char *
- hex2mem(char *buf, char *mem, int count)
- {
- int hexValue;
- char *tmp_raw, *tmp_hex;
-
- tmp_raw = buf + count * 2;
- tmp_hex = tmp_raw - 1;
- longjmp_on_fault = 1;
- while (tmp_hex >= buf) {
- tmp_raw--;
- hexValue = hex(*tmp_hex--);
- if (hexValue < 0)
- kgdb_error(KGDBERR_NOTHEXDIG);
- *tmp_raw = hexValue;
- hexValue = hex(*tmp_hex--);
- if (hexValue < 0)
- kgdb_error(KGDBERR_NOTHEXDIG);
- *tmp_raw |= hexValue << 4;
- }
- memcpy(mem, tmp_raw, count);
- kgdb_flush_cache_range((void *)mem, (void *)(mem+count));
- longjmp_on_fault = 0;
- return buf;
- }
- static int
- hexToInt(char **ptr, int *intValue)
- {
- int numChars = 0;
- int hexValue;
- *intValue = 0;
- longjmp_on_fault = 1;
- while (**ptr) {
- hexValue = hex(**ptr);
- if (hexValue < 0)
- break;
- *intValue = (*intValue << 4) | hexValue;
- numChars ++;
- (*ptr)++;
- }
- longjmp_on_fault = 0;
- return (numChars);
- }
- static void
- getpacket(char *buffer)
- {
- unsigned char checksum;
- unsigned char xmitcsum;
- int i;
- int count;
- unsigned char ch;
- do {
-
- while ((ch = (getDebugChar() & 0x7f)) != '$') {
- #ifdef KGDB_DEBUG
- if (kdebug)
- putc(ch);
- #endif
- ;
- }
- checksum = 0;
- xmitcsum = -1;
- count = 0;
-
- while (count < BUFMAX) {
- ch = getDebugChar() & 0x7f;
- if (ch == '#')
- break;
- checksum = checksum + ch;
- buffer[count] = ch;
- count = count + 1;
- }
- if (count >= BUFMAX)
- continue;
- buffer[count] = 0;
- if (ch == '#') {
- xmitcsum = hex(getDebugChar() & 0x7f) << 4;
- xmitcsum |= hex(getDebugChar() & 0x7f);
- if (checksum != xmitcsum)
- putDebugChar('-');
- else {
- putDebugChar('+');
-
- if (buffer[2] == ':') {
- putDebugChar(buffer[0]);
- putDebugChar(buffer[1]);
-
- count = strlen(buffer);
- for (i=3; i <= count; i++)
- buffer[i-3] = buffer[i];
- }
- }
- }
- } while (checksum != xmitcsum);
- }
- static void
- putpacket(unsigned char *buffer)
- {
- unsigned char checksum;
- int count;
- unsigned char ch, recv;
-
- do {
- putDebugChar('$');
- checksum = 0;
- count = 0;
- while ((ch = buffer[count])) {
- putDebugChar(ch);
- checksum += ch;
- count += 1;
- }
- putDebugChar('#');
- putDebugChar(hexchars[checksum >> 4]);
- putDebugChar(hexchars[checksum & 0xf]);
- recv = getDebugChar();
- } while ((recv & 0x7f) != '+');
- }
- static int
- handle_exception (struct pt_regs *regs)
- {
- int addr;
- int length;
- char *ptr;
- kgdb_data kd;
- int i;
- if (!initialized) {
- printf("kgdb: exception before kgdb is initialized! huh?\n");
- return (0);
- }
-
- if (longjmp_on_fault) {
- longjmp_on_fault = 0;
- kgdb_longjmp(error_jmp_buf, KGDBERR_MEMFAULT);
- panic("kgdb longjump failed!\n");
- }
- if (kgdb_active) {
- printf("kgdb: unexpected exception from within kgdb\n");
- return (0);
- }
- kgdb_active = 1;
- kgdb_interruptible(0);
- printf("kgdb: handle_exception; trap [0x%x]\n", kgdb_trap(regs));
- if (kgdb_setjmp(error_jmp_buf) != 0)
- panic("kgdb: error or fault in entry init!\n");
- kgdb_enter(regs, &kd);
- entry_regs = *regs;
- ptr = remcomOutBuffer;
- *ptr++ = 'T';
- *ptr++ = hexchars[kd.sigval >> 4];
- *ptr++ = hexchars[kd.sigval & 0xf];
- for (i = 0; i < kd.nregs; i++) {
- kgdb_reg *rp = &kd.regs[i];
- *ptr++ = hexchars[rp->num >> 4];
- *ptr++ = hexchars[rp->num & 0xf];
- *ptr++ = ':';
- ptr = (char *)mem2hex((char *)&rp->val, ptr, 4);
- *ptr++ = ';';
- }
- *ptr = 0;
- #ifdef KGDB_DEBUG
- if (kdebug)
- printf("kgdb: remcomOutBuffer: %s\n", remcomOutBuffer);
- #endif
- putpacket((unsigned char *)&remcomOutBuffer);
- while (1) {
- volatile int errnum;
- remcomOutBuffer[0] = 0;
- getpacket(remcomInBuffer);
- ptr = &remcomInBuffer[1];
- #ifdef KGDB_DEBUG
- if (kdebug)
- printf("kgdb: remcomInBuffer: %s\n", remcomInBuffer);
- #endif
- errnum = kgdb_setjmp(error_jmp_buf);
- if (errnum == 0) switch (remcomInBuffer[0]) {
- case '?':
- remcomOutBuffer[0] = 'S';
- remcomOutBuffer[1] = hexchars[kd.sigval >> 4];
- remcomOutBuffer[2] = hexchars[kd.sigval & 0xf];
- remcomOutBuffer[3] = 0;
- break;
- #ifdef KGDB_DEBUG
- case 'd':
-
- kdebug ^= 1;
- break;
- #endif
- case 'g':
- length = kgdb_getregs(regs, remcomRegBuffer, BUFMAX);
- mem2hex(remcomRegBuffer, remcomOutBuffer, length);
- break;
- case 'G':
- length = strlen(ptr);
- if ((length & 1) != 0) kgdb_error(KGDBERR_BADPARAMS);
- hex2mem(ptr, remcomRegBuffer, length/2);
- kgdb_putregs(regs, remcomRegBuffer, length/2);
- strcpy(remcomOutBuffer,"OK");
- break;
- case 'm':
-
- if (hexToInt(&ptr, &addr)
- && *ptr++ == ','
- && hexToInt(&ptr, &length)) {
- mem2hex((char *)addr, remcomOutBuffer, length);
- } else {
- kgdb_error(KGDBERR_BADPARAMS);
- }
- break;
- case 'M':
-
- if (hexToInt(&ptr, &addr)
- && *ptr++ == ','
- && hexToInt(&ptr, &length)
- && *ptr++ == ':') {
- hex2mem(ptr, (char *)addr, length);
- strcpy(remcomOutBuffer, "OK");
- } else {
- kgdb_error(KGDBERR_BADPARAMS);
- }
- break;
- case 'k':
- kd.extype = KGDBEXIT_KILL;
- *regs = entry_regs;
- goto doexit;
- case 'C':
- *ptr = '\0';
-
- case 'c':
-
- kd.extype = KGDBEXIT_CONTINUE;
- if (hexToInt(&ptr, &addr)) {
- kd.exaddr = addr;
- kd.extype |= KGDBEXIT_WITHADDR;
- }
- goto doexit;
- case 'S':
- *ptr = '\0';
-
- case 's':
- kd.extype = KGDBEXIT_SINGLE;
- if (hexToInt(&ptr, &addr)) {
- kd.exaddr = addr;
- kd.extype |= KGDBEXIT_WITHADDR;
- }
- doexit:
- kgdb_flush_cache_all();
- kgdb_exit(regs, &kd);
- kgdb_active = 0;
- kgdb_interruptible(1);
- return (1);
- case 'r':
- panic("kgdb reset.");
- break;
- case 'P':
- if (hexToInt(&ptr, &addr)
- && *ptr++ == '='
- && ((length = strlen(ptr)) & 1) == 0) {
- hex2mem(ptr, remcomRegBuffer, length/2);
- kgdb_putreg(regs, addr,
- remcomRegBuffer, length/2);
- strcpy(remcomOutBuffer,"OK");
- } else {
- kgdb_error(KGDBERR_BADPARAMS);
- }
- break;
- }
- if (errnum != 0)
- sprintf(remcomOutBuffer, "E%02d", errnum);
- #ifdef KGDB_DEBUG
- if (kdebug)
- printf("kgdb: remcomOutBuffer: %s\n", remcomOutBuffer);
- #endif
-
- putpacket((unsigned char *)&remcomOutBuffer);
- }
- }
- void
- kgdb_init(void)
- {
- kgdb_serial_init();
- debugger_exception_handler = handle_exception;
- initialized = 1;
- putDebugStr("kgdb ready\n");
- puts("ready\n");
- }
- void
- kgdb_error(int errnum)
- {
- longjmp_on_fault = 0;
- kgdb_longjmp(error_jmp_buf, errnum);
- panic("kgdb_error: longjmp failed!\n");
- }
- int
- kgdb_output_string (const char* s, unsigned int count)
- {
- char buffer[512];
- count = (count <= (sizeof(buffer) / 2 - 2))
- ? count : (sizeof(buffer) / 2 - 2);
- buffer[0] = 'O';
- mem2hex ((char *)s, &buffer[1], count);
- putpacket((unsigned char *)&buffer);
- return 1;
- }
- void
- breakpoint(void)
- {
- if (!initialized) {
- printf("breakpoint() called b4 kgdb init\n");
- return;
- }
- kgdb_breakpoint(0, 0);
- }
- int
- do_kgdb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
- {
- printf("Entering KGDB mode via exception handler...\n\n");
- kgdb_breakpoint(argc - 1, argv + 1);
- printf("\nReturned from KGDB mode\n");
- return 0;
- }
- U_BOOT_CMD(
- kgdb, CONFIG_SYS_MAXARGS, 1, do_kgdb,
- "enter gdb remote debug mode",
- "[arg0 arg1 .. argN]\n"
- " - executes a breakpoint so that kgdb mode is\n"
- " entered via the exception handler. To return\n"
- " to the monitor, the remote gdb debugger must\n"
- " execute a \"continue\" or \"quit\" command.\n"
- "\n"
- " if a program is loaded by the remote gdb, any args\n"
- " passed to the kgdb command are given to the loaded\n"
- " program if it is executed (see the \"hello_world\"\n"
- " example program in the U-Boot examples directory)."
- );
|