📄 gdb_interface.c
字号:
break; } case 'c': ret = SD_CONTINUE; goto out; case 'C': close(rfd); rfd = -1; ret = SD_CONTINUE; goto out; case 's': case 'S': cpuno = read_cpu(cmd_buf+1); if (cpuno < TOTAL_CPUS && CanSingleStep(simosCPUType)) { ret = (ch == 'S') ? SD_NEXTI_ANYCPU : (Simdebug_result) cpuno; } else { CPUWarning("Single-step request for bad cpu or SS not implemented, continuing...\n"); ret = SD_CONTINUE; } goto out; case 'k': exit(0); break; case 'p': pid = (int)getpid(); sprintf(cmd_buf, "%8u,%02x", pid, TOTAL_CPUS); break; case 'm': { unsigned int cno; Reg64 memaddr; unsigned int len; if (sscanf(cmd_buf+1, MEMORY_SCANF,&cno,&memaddr,&len) != 3) { write_enn (cmd_buf); } else { if (read_memory(cmd_buf, memaddr, len, cno)) { write_enn (cmd_buf); } } break; } case 'M': { unsigned int cno; Reg64 memaddr; unsigned int len; char* spkt = findchar(cmd_buf, ':'); if ((*spkt != ':') || (sscanf(cmd_buf+1, MEMORY_SCANF,&cno,&memaddr,&len) != 3) || write_memory(spkt+1, memaddr, len, cno)) { write_enn (cmd_buf); } else { write_ok (cmd_buf); } break; } default: write_enn (cmd_buf); break; } if (putpkt (rfd, cmd_buf) < 0) goto errout; }out: return ret;errout: /* the connection was terminated prematurely. Reset */ close(rfd); rfd = -1; CPUWarning("Remote debugger connection lost, continuing...\n"); return SD_CONTINUE;}static intread_cpu(char* buf){ return (fromhex (buf[0]) << 4) + fromhex (buf[1]);}#define MAX_XFER (8*1024)static intread_memory(char* buf, VA memaddr, unsigned int len, unsigned int cpuno){ char readout_buf[MAX_XFER]; ASSERT( cpuno < TOTAL_CPUS); if (len > MAX_XFER) { CPUError("simdebug.c::read_memory: too many bytes requested"); } if (CPUVec.GetMemory(cpuno, memaddr, len, readout_buf) == SUCCESS) { convert_int_to_ascii(readout_buf, buf, len); return 0; } return 1;}static intwrite_memory(char* buf, VA memaddr, unsigned int len, unsigned int cpuno){ char readout_buf[MAX_XFER]; ASSERT( cpuno < TOTAL_CPUS); if (len > MAX_XFER) { CPUError("simdebug.c::write_memory: too many bytes requested"); } convert_ascii_to_int(buf, readout_buf, len); if (CPUVec.PutMemory(cpuno, memaddr, len, readout_buf) == SUCCESS) { return 0; } return 1;}/* Convert hex digit A to a number. */static intfromhex (int a){ if (a >= '0' && a <= '9') return a - '0'; else if (a >= 'a' && a <= 'f') return a - 'a' + 10; else { CPUError ("Request contains invalid hex digit"); return 0; }}/* Convert number NIB to a hex digit. */static inttohex (int nib){ if (nib < 10) return '0' + nib; else return 'a' + nib - 10;}/* Send a packet to the remote machine, with error checking. The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */static intputpkt (int fd, char *buf){ int i; unsigned char csum = 0; char buf2[2000]; char buf3[1]; int cnt = strlen (buf); char *p; /* Copy the packet into buffer BUF2, encapsulating it and giving it a checksum. */ p = buf2; *p++ = '$'; for (i = 0; i < cnt; i++) { csum += buf[i]; *p++ = buf[i]; } *p++ = '#'; *p++ = tohex ((csum >> 4) & 0xf); *p++ = tohex (csum & 0xf); /* Send it over and over until we get a positive ack. */ do { int cc; if (remote_debug) CPUWarning( "PUTPKT: %s\n", buf); if (write (fd, buf2, p - buf2) != p - buf2) { CPUWarning("putpkt(write)"); goto errout; } cc = read (fd, buf3, 1); if (cc <= 0) { CPUWarning("putpkt(read)"); goto errout; } } while (buf3[0] != '+'); return 1; /* Success! */ errout: return -1;}/***************************************************************** * readchar * Returns next char from remote GDB. -1 if error. *****************************************************************/static intreadchar (int fd){ static char buf[BUFSIZ]; static int bufcnt = 0; static char *bufp; if (bufcnt-- > 0) return *bufp++ & 0x7f; bufcnt = read (fd, buf, BUFSIZ); if (bufcnt <= 0) { CPUWarning("Simdebug readchar"); return -1; } bufp = buf; bufcnt--; return *bufp++ & 0x7f;}/***************************************************************** * getpkt * * Read a packet from the remote machine, with error checking, * and store it in BUF. Returns length of packet, or negative if error. *****************************************************************/static intgetpkt (int fd, char *buf){ char *bp; unsigned char csum, c1, c2; int c; bp = buf; while (1) { csum = 0; while (1) { c = readchar (fd); if (c == '$') break; if (c < 0) goto errout; } bp = buf; while (1) { c = readchar (fd); if (c < 0) goto errout; if (c == '#') break; *bp++ = c; csum += c; } *bp = 0; c = readchar (fd); if (c < 0) goto errout; c1 = fromhex (c); c = readchar (fd); if (c < 0) goto errout; c2 = fromhex (c); if (csum == (c1 << 4) + c2) break; write (fd, "-", 1); } write (fd, "+", 1); return bp - buf; errout: return -1;}static voidwrite_ok (char *buf){ buf[0] = 'O'; buf[1] = 'k'; buf[2] = '\0';}static voidwrite_enn (char *buf){ buf[0] = 'E'; buf[1] = 'N'; buf[2] = 'N'; buf[3] = '\0';}static voidconvert_int_to_ascii (char *from, char *to, int n){ int nib; char ch; while (n--) { ch = *from++; nib = ((ch & 0xf0) >> 4) & 0x0f; *to++ = tohex (nib); nib = ch & 0x0f; *to++ = tohex (nib); } *to++ = 0;}static voidconvert_ascii_to_int (char *from, char *to, int n){ int nib1, nib2; while (n--) { nib1 = fromhex (*from++); nib2 = fromhex (*from++); *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f); }}static char*findchar(char* s, int c){ int c1; while (1) { c1 = *s; if ((c1 == c) || (! c1)) return s; s ++; } /* JeffG: This never gets executed (assuming s is NULL-terminated), but this is necessary to get it to compile without warnings */ return NULL; }static voidprepare_resume_reply (char *buf,unsigned int sig, unsigned int cpuno){ sprintf(buf, "S%02x%02x", sig, cpuno);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -