4 * @brief GDB Stub implementation (https://github.com/mborgerson/gdbstub).
9 * @copyright Copyright (c) 2022 Lunaixsky & (See license below)
14 * Copyright (c) 2016-2022 Matt Borgerson
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this software and associated documentation files (the "Software"), to deal
18 * in the Software without restriction, including without limitation the rights
19 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 * copies of the Software, and to permit persons to whom the Software is
21 * furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <hal/serial.h>
36 #include <klibc/string.h>
37 #include <sdbg/gdbstub.h>
38 #include <sys/port_io.h>
40 /*****************************************************************************
42 ****************************************************************************/
44 #ifndef GDBSTUB_DONT_DEFINE_STDINT_TYPES
45 typedef unsigned char u8_t;
46 typedef unsigned short u16_t;
47 typedef unsigned long uint32_t;
50 typedef unsigned int address;
51 typedef unsigned int reg;
55 GDB_CPU_I386_REG_EAX = 0,
56 GDB_CPU_I386_REG_ECX = 1,
57 GDB_CPU_I386_REG_EDX = 2,
58 GDB_CPU_I386_REG_EBX = 3,
59 GDB_CPU_I386_REG_ESP = 4,
60 GDB_CPU_I386_REG_EBP = 5,
61 GDB_CPU_I386_REG_ESI = 6,
62 GDB_CPU_I386_REG_EDI = 7,
63 GDB_CPU_I386_REG_PC = 8,
64 GDB_CPU_I386_REG_PS = 9,
65 GDB_CPU_I386_REG_CS = 10,
66 GDB_CPU_I386_REG_SS = 11,
67 GDB_CPU_I386_REG_DS = 12,
68 GDB_CPU_I386_REG_ES = 13,
69 GDB_CPU_I386_REG_FS = 14,
70 GDB_CPU_I386_REG_GS = 15,
71 GDB_CPU_NUM_REGISTERS = 16
77 struct serial_dev* sdev;
78 reg registers[GDB_CPU_NUM_REGISTERS];
81 /*****************************************************************************
83 * GDB Remote Serial Protocol
85 ****************************************************************************/
87 /*****************************************************************************
89 ****************************************************************************/
91 #define GDB_PRINT(...)
93 #define COM_PORT SERIAL_COM1
98 #define NULL ((void*)0)
102 #define GDB_ASSERT(x) \
107 /*****************************************************************************
109 ****************************************************************************/
112 gdb_main(struct gdb_state* state);
114 /* System functions, supported by all stubs */
118 gdb_sys_getc(struct gdb_state* state);
120 gdb_sys_putchar(struct gdb_state* state, int ch);
122 gdb_sys_mem_readb(struct gdb_state* state, address addr, char* val);
124 gdb_sys_mem_writeb(struct gdb_state* state, address addr, char val);
126 gdb_sys_continue(struct gdb_state* state);
128 gdb_sys_step(struct gdb_state* state);
130 /*****************************************************************************
132 ****************************************************************************/
134 typedef int (*gdb_enc_func)(char* buf,
135 unsigned int buf_len,
137 unsigned int data_len);
138 typedef int (*gdb_dec_func)(const char* buf,
139 unsigned int buf_len,
141 unsigned int data_len);
143 /*****************************************************************************
145 ****************************************************************************/
147 static const char digits[] = "0123456789abcdef";
149 /*****************************************************************************
151 ****************************************************************************/
153 /* Communication functions */
155 gdb_write(struct gdb_state* state, const char* buf, unsigned int len);
157 gdb_read(struct gdb_state* state,
159 unsigned int buf_len,
162 /* String processing helper functions */
164 gdb_strlen(const char* ch);
166 gdb_get_digit(int val);
168 gdb_get_val(char digit, int base);
170 gdb_strtol(const char* str, unsigned int len, int base, const char** endptr);
172 /* Packet functions */
174 gdb_send_packet(struct gdb_state* state, const char* pkt, unsigned int pkt_len);
176 gdb_recv_packet(struct gdb_state* state,
178 unsigned int pkt_buf_len,
179 unsigned int* pkt_len);
181 gdb_checksum(const char* buf, unsigned int len);
183 gdb_recv_ack(struct gdb_state* state);
185 /* Data encoding/decoding */
187 gdb_enc_hex(char* buf,
188 unsigned int buf_len,
190 unsigned int data_len);
192 gdb_dec_hex(const char* buf,
193 unsigned int buf_len,
195 unsigned int data_len);
197 gdb_enc_bin(char* buf,
198 unsigned int buf_len,
200 unsigned int data_len);
202 gdb_dec_bin(const char* buf,
203 unsigned int buf_len,
205 unsigned int data_len);
207 /* Packet creation helpers */
209 gdb_send_ok_packet(struct gdb_state* state, char* buf, unsigned int buf_len);
211 gdb_send_conmsg_packet(struct gdb_state* state,
213 unsigned int buf_len,
216 gdb_send_signal_packet(struct gdb_state* state,
218 unsigned int buf_len,
221 gdb_send_error_packet(struct gdb_state* state,
223 unsigned int buf_len,
226 /* Command functions */
228 gdb_mem_read(struct gdb_state* state,
230 unsigned int buf_len,
235 gdb_mem_write(struct gdb_state* state,
237 unsigned int buf_len,
242 gdb_continue(struct gdb_state* state);
244 gdb_step(struct gdb_state* state);
246 /*****************************************************************************
247 * String Processing Helper Functions
248 ****************************************************************************/
251 * Get null-terminated string length.
254 gdb_strlen(const char* ch)
267 * Get integer value for a string representation.
269 * If the string starts with + or -, it will be signed accordingly.
271 * If base == 0, the base will be determined:
272 * base 16 if the string starts with 0x or 0X,
275 * If endptr is specified, it will point to the last non-digit in the
276 * string. If there are no digits in the string, it will be set to NULL.
279 gdb_strtol(const char* str, unsigned int len, int base, const char** endptr)
282 int sign, tmp, value, valid;
297 /* Detect negative numbers */
298 if (str[pos] == '-') {
301 } else if (str[pos] == '+') {
306 /* Detect '0x' hex prefix */
307 if ((pos + 2 < len) && (str[pos] == '0') &&
308 ((str[pos + 1] == 'x') || (str[pos + 1] == 'X'))) {
317 for (; (pos < len) && (str[pos] != '\x00'); pos++) {
318 tmp = gdb_get_val(str[pos], base);
319 if (tmp == GDB_EOF) {
323 value = value * base + tmp;
324 valid = 1; /* At least one digit is valid */
341 * Get the corresponding ASCII hex digit character for a value.
344 gdb_get_digit(int val)
346 if ((val >= 0) && (val <= 0xf)) {
354 * Get the corresponding value for a ASCII digit character.
356 * Supports bases 2-16.
359 gdb_get_val(char digit, int base)
363 if ((digit >= '0') && (digit <= '9')) {
365 } else if ((digit >= 'a') && (digit <= 'f')) {
366 value = digit - 'a' + 0xa;
367 } else if ((digit >= 'A') && (digit <= 'F')) {
368 value = digit - 'A' + 0xa;
373 return (value < base) ? value : GDB_EOF;
376 /*****************************************************************************
378 ****************************************************************************/
381 * Receive a packet acknowledgment
384 * 0 if an ACK (+) was received
385 * 1 if a NACK (-) was received
389 gdb_recv_ack(struct gdb_state* state)
393 /* Wait for packet ack */
394 switch (response = gdb_sys_getc(state)) {
396 /* Packet acknowledged */
399 /* Packet negative acknowledged */
403 GDB_PRINT("received bad packet response: 0x%2x\n", response);
409 * Calculate 8-bit checksum of a buffer.
415 gdb_checksum(const char* buf, unsigned int len)
429 * Transmits a packet of data.
430 * Packets are of the form: $<packet-data>#<checksum>
433 * 0 if the packet was transmitted and acknowledged
434 * 1 if the packet was transmitted but not acknowledged
438 gdb_send_packet(struct gdb_state* state,
439 const char* pkt_data,
440 unsigned int pkt_len)
445 /* Send packet start */
446 if (gdb_sys_putchar(state, '$') == GDB_EOF) {
450 /* Send packet data */
451 if (gdb_write(state, pkt_data, pkt_len) == GDB_EOF) {
455 /* Send the checksum */
457 csum = gdb_checksum(pkt_data, pkt_len);
458 if ((gdb_enc_hex(buf + 1, sizeof(buf) - 1, &csum, 1) == GDB_EOF) ||
459 (gdb_write(state, buf, sizeof(buf)) == GDB_EOF)) {
463 return gdb_recv_ack(state);
467 * Receives a packet of data, assuming a 7-bit clean connection.
470 * 0 if the packet was received
474 gdb_recv_packet(struct gdb_state* state,
476 unsigned int pkt_buf_len,
477 unsigned int* pkt_len)
480 char expected_csum, actual_csum;
483 /* Wait for packet start */
487 data = gdb_sys_getc(state);
488 if (data == GDB_EOF) {
490 } else if (data == '$') {
491 /* Detected start of packet. */
496 /* Read until checksum */
499 data = gdb_sys_getc(state);
501 if (data == GDB_EOF) {
502 /* Error receiving character */
504 } else if (data == '#') {
508 /* Check for space */
509 if (*pkt_len >= pkt_buf_len) {
510 GDB_PRINT("packet buffer overflow\n");
514 /* Store character and update checksum */
515 pkt_buf[(*pkt_len)++] = (char)data;
519 /* Receive the checksum */
520 if ((gdb_read(state, buf, sizeof(buf), 2) == GDB_EOF) ||
521 (gdb_dec_hex(buf, 2, &expected_csum, 1) == GDB_EOF)) {
525 /* Verify checksum */
526 actual_csum = gdb_checksum(pkt_buf, *pkt_len);
527 if (actual_csum != expected_csum) {
528 /* Send packet nack */
529 GDB_PRINT("received packet with bad checksum\n");
530 gdb_sys_putchar(state, '-');
534 /* Send packet ack */
535 gdb_sys_putchar(state, '+');
539 /*****************************************************************************
540 * Data Encoding/Decoding
541 ****************************************************************************/
544 * Encode data to its hex-value representation in a buffer.
547 * 0+ number of bytes written to buf
548 * GDB_EOF if the buffer is too small
551 gdb_enc_hex(char* buf,
552 unsigned int buf_len,
554 unsigned int data_len)
558 if (buf_len < data_len * 2) {
559 /* Buffer too small */
563 for (pos = 0; pos < data_len; pos++) {
564 *buf++ = gdb_get_digit((data[pos] >> 4) & 0xf);
565 *buf++ = gdb_get_digit((data[pos]) & 0xf);
572 * Decode data from its hex-value representation to a buffer.
576 * GDB_EOF if the buffer is too small
579 gdb_dec_hex(const char* buf,
580 unsigned int buf_len,
582 unsigned int data_len)
587 if (buf_len != data_len * 2) {
588 /* Buffer too small */
592 for (pos = 0; pos < data_len; pos++) {
593 /* Decode high nibble */
594 tmp = gdb_get_val(*buf++, 16);
595 if (tmp == GDB_EOF) {
596 /* Buffer contained junk. */
601 data[pos] = tmp << 4;
603 /* Decode low nibble */
604 tmp = gdb_get_val(*buf++, 16);
605 if (tmp == GDB_EOF) {
606 /* Buffer contained junk. */
617 * Encode data to its binary representation in a buffer.
620 * 0+ number of bytes written to buf
621 * GDB_EOF if the buffer is too small
624 gdb_enc_bin(char* buf,
625 unsigned int buf_len,
627 unsigned int data_len)
629 unsigned int buf_pos, data_pos;
631 for (buf_pos = 0, data_pos = 0; data_pos < data_len; data_pos++) {
632 if (data[data_pos] == '$' || data[data_pos] == '#' ||
633 data[data_pos] == '}' || data[data_pos] == '*') {
634 if (buf_pos + 1 >= buf_len) {
638 buf[buf_pos++] = '}';
639 buf[buf_pos++] = data[data_pos] ^ 0x20;
641 if (buf_pos >= buf_len) {
645 buf[buf_pos++] = data[data_pos];
653 * Decode data from its bin-value representation to a buffer.
656 * 0+ if successful, number of bytes decoded
657 * GDB_EOF if the buffer is too small
660 gdb_dec_bin(const char* buf,
661 unsigned int buf_len,
663 unsigned int data_len)
665 unsigned int buf_pos, data_pos;
667 for (buf_pos = 0, data_pos = 0; buf_pos < buf_len; buf_pos++) {
668 if (data_pos >= data_len) {
669 /* Output buffer overflow */
673 if (buf[buf_pos] == '}') {
674 /* The next byte is escaped! */
675 if (buf_pos + 1 >= buf_len) {
676 /* There's an escape character, but no escaped character
677 * following the escape character. */
682 data[data_pos++] = buf[buf_pos] ^ 0x20;
684 data[data_pos++] = buf[buf_pos];
691 /*****************************************************************************
693 ****************************************************************************/
696 * Read from memory and encode into buf.
699 * 0+ number of bytes written to buf
700 * GDB_EOF if the buffer is too small
703 gdb_mem_read(struct gdb_state* state,
705 unsigned int buf_len,
713 if (len > sizeof(data)) {
717 /* Read from system memory */
718 for (pos = 0; pos < len; pos++) {
719 if (gdb_sys_mem_readb(state, addr + pos, &data[pos])) {
726 return enc(buf, buf_len, data, len);
730 * Write to memory from encoded buf.
733 gdb_mem_write(struct gdb_state* state,
735 unsigned int buf_len,
743 if (len > sizeof(data)) {
748 if (dec(buf, buf_len, data, len) == GDB_EOF) {
752 /* Write to system memory */
753 for (pos = 0; pos < len; pos++) {
754 if (gdb_sys_mem_writeb(state, addr + pos, data[pos])) {
755 /* Failed to write */
764 * Continue program execution at PC.
767 gdb_continue(struct gdb_state* state)
769 gdb_sys_continue(state);
774 * Step one instruction.
777 gdb_step(struct gdb_state* state)
783 /*****************************************************************************
784 * Packet Creation Helpers
785 ****************************************************************************/
791 gdb_send_ok_packet(struct gdb_state* state, char* buf, unsigned int buf_len)
793 return gdb_send_packet(state, "OK", 2);
797 * Send a message to the debugging console (via O XX... packet)
800 gdb_send_conmsg_packet(struct gdb_state* state,
802 unsigned int buf_len,
809 /* Buffer too small */
814 status = gdb_enc_hex(&buf[1], buf_len - 1, msg, gdb_strlen(msg));
815 if (status == GDB_EOF) {
819 return gdb_send_packet(state, buf, size);
823 * Send a signal packet (S AA).
826 gdb_send_signal_packet(struct gdb_state* state,
828 unsigned int buf_len,
835 /* Buffer too small */
840 status = gdb_enc_hex(&buf[1], buf_len - 1, &signal, 1);
841 if (status == GDB_EOF) {
845 return gdb_send_packet(state, buf, size);
849 * Send a error packet (E AA).
852 gdb_send_error_packet(struct gdb_state* state,
854 unsigned int buf_len,
861 /* Buffer too small */
866 status = gdb_enc_hex(&buf[1], buf_len - 1, &error, 1);
867 if (status == GDB_EOF) {
871 return gdb_send_packet(state, buf, size);
874 /*****************************************************************************
875 * Communication Functions
876 ****************************************************************************/
879 * Write a sequence of bytes.
883 * GDB_EOF if failed to write all bytes
886 gdb_write(struct gdb_state* state, const char* buf, unsigned int len)
888 int err = serial_rwbuf_sync(state->sdev, buf, len, SERIAL_RW_TX);
890 return err < 0 ? GDB_EOF : 0;
894 * Read a sequence of bytes.
897 * 0 if successfully read len bytes
898 * GDB_EOF if failed to read all bytes
901 gdb_read(struct gdb_state* state,
903 unsigned int buf_len,
909 /* Buffer too small */
913 int err = serial_rwbuf_sync(state->sdev, buf, buf_len, SERIAL_RW_RX);
915 return err < 0 ? GDB_EOF : 0;
918 /*****************************************************************************
920 ****************************************************************************/
923 * Main debug loop. Handles commands.
926 gdb_main(struct gdb_state* state)
932 unsigned int pkt_len;
933 const char* ptr_next;
935 gdb_send_signal_packet(state, pkt_buf, sizeof(pkt_buf), state->signum);
938 /* Receive the next packet */
939 status = gdb_recv_packet(state, pkt_buf, sizeof(pkt_buf), &pkt_len);
940 if (status == GDB_EOF) {
945 /* Received empty packet.. */
952 * Handle one letter commands
954 switch (pkt_buf[0]) {
956 /* Calculate remaining space in packet from ptr_next position. */
957 #define token_remaining_buf (pkt_len - (ptr_next - pkt_buf))
959 /* Expecting a seperator. If not present, go to error */
960 #define token_expect_seperator(c) \
962 if (!ptr_next || *ptr_next != c) { \
969 /* Expecting an integer argument. If not present, go to error */
970 #define token_expect_integer_arg(arg) \
972 arg = gdb_strtol(ptr_next, token_remaining_buf, 16, &ptr_next); \
983 /* Encode registers */
984 status = gdb_enc_hex(pkt_buf,
986 (char*)&(state->registers),
987 sizeof(state->registers));
988 if (status == GDB_EOF) {
992 gdb_send_packet(state, pkt_buf, pkt_len);
997 * Command Format: G XX...
1000 status = gdb_dec_hex(pkt_buf + 1,
1002 (char*)&(state->registers),
1003 sizeof(state->registers));
1004 if (status == GDB_EOF) {
1007 gdb_send_ok_packet(state, pkt_buf, sizeof(pkt_buf));
1012 * Command Format: p n
1016 token_expect_integer_arg(addr);
1018 if (addr >= GDB_CPU_NUM_REGISTERS) {
1023 status = gdb_enc_hex(pkt_buf,
1025 (char*)&(state->registers[addr]),
1026 sizeof(state->registers[addr]));
1027 if (status == GDB_EOF) {
1030 gdb_send_packet(state, pkt_buf, status);
1035 * Command Format: P n...=r...
1039 token_expect_integer_arg(addr);
1040 token_expect_seperator('=');
1042 if (addr < GDB_CPU_NUM_REGISTERS) {
1043 status = gdb_dec_hex(ptr_next,
1044 token_remaining_buf,
1045 (char*)&(state->registers[addr]),
1046 sizeof(state->registers[addr]));
1047 if (status == GDB_EOF) {
1051 gdb_send_ok_packet(state, pkt_buf, sizeof(pkt_buf));
1056 * Command Format: m addr,length
1060 token_expect_integer_arg(addr);
1061 token_expect_seperator(',');
1062 token_expect_integer_arg(length);
1065 status = gdb_mem_read(
1066 state, pkt_buf, sizeof(pkt_buf), addr, length, gdb_enc_hex);
1067 if (status == GDB_EOF) {
1070 gdb_send_packet(state, pkt_buf, status);
1075 * Command Format: M addr,length:XX..
1079 token_expect_integer_arg(addr);
1080 token_expect_seperator(',');
1081 token_expect_integer_arg(length);
1082 token_expect_seperator(':');
1085 status = gdb_mem_write(state,
1087 token_remaining_buf,
1091 if (status == GDB_EOF) {
1094 gdb_send_ok_packet(state, pkt_buf, sizeof(pkt_buf));
1098 * Write Memory (Binary)
1099 * Command Format: X addr,length:XX..
1103 token_expect_integer_arg(addr);
1104 token_expect_seperator(',');
1105 token_expect_integer_arg(length);
1106 token_expect_seperator(':');
1109 status = gdb_mem_write(state,
1111 token_remaining_buf,
1115 if (status == GDB_EOF) {
1118 gdb_send_ok_packet(state, pkt_buf, sizeof(pkt_buf));
1122 * Continue, Kill (also treated as continue!)
1123 * Command Format: c [addr]
1128 gdb_continue(state);
1133 * Command Format: s [addr]
1141 gdb_send_signal_packet(
1142 state, pkt_buf, sizeof(pkt_buf), state->signum);
1146 * Unsupported Command
1149 gdb_send_packet(state, 0, 0);
1155 gdb_send_error_packet(state, pkt_buf, sizeof(pkt_buf), 0x00);
1157 #undef token_remaining_buf
1158 #undef token_expect_seperator
1159 #undef token_expect_integer_arg
1165 /*****************************************************************************
1167 ****************************************************************************/
1173 } __attribute__((packed));
1181 } __attribute__((packed));
1183 /*****************************************************************************
1185 ****************************************************************************/
1186 #define gdb_x86_io_write_8(port, val) port_wrbyte(port, val)
1187 #define gdb_x86_io_read_8(port) port_rdbyte(port)
1189 #define gdb_x86_serial_getc() serial_rx_byte(COM_PORT)
1190 #define gdb_x86_serial_putchar(ch) serial_tx_byte(COM_PORT, ch)
1192 #ifdef __STRICT_ANSI__
1196 static struct gdb_state gdb_state;
1197 static volatile int start_debugging = 0;
1200 * Debug interrupt handler.
1203 gdbstub_loop(isr_param* param)
1205 /* Translate vector to signal */
1206 switch (param->execp->vector) {
1208 gdb_state.signum = 5;
1211 gdb_state.signum = 5;
1214 gdb_state.signum = 7;
1217 /* Load Registers */
1218 gdb_state.registers[GDB_CPU_I386_REG_EAX] = param->registers.eax;
1219 gdb_state.registers[GDB_CPU_I386_REG_ECX] = param->registers.ecx;
1220 gdb_state.registers[GDB_CPU_I386_REG_EDX] = param->registers.edx;
1221 gdb_state.registers[GDB_CPU_I386_REG_EBX] = param->registers.ebx;
1222 gdb_state.registers[GDB_CPU_I386_REG_ESP] = param->esp;
1223 gdb_state.registers[GDB_CPU_I386_REG_EBP] = param->registers.ebp;
1224 gdb_state.registers[GDB_CPU_I386_REG_ESI] = param->registers.esi;
1225 gdb_state.registers[GDB_CPU_I386_REG_EDI] = param->registers.edi;
1226 gdb_state.registers[GDB_CPU_I386_REG_PC] = param->execp->eip;
1227 gdb_state.registers[GDB_CPU_I386_REG_CS] = param->execp->cs;
1228 gdb_state.registers[GDB_CPU_I386_REG_PS] = param->execp->eflags;
1229 gdb_state.registers[GDB_CPU_I386_REG_SS] = param->execp->ss;
1230 gdb_state.registers[GDB_CPU_I386_REG_DS] = param->registers.ds;
1231 gdb_state.registers[GDB_CPU_I386_REG_ES] = param->registers.es;
1232 gdb_state.registers[GDB_CPU_I386_REG_FS] = param->registers.fs;
1233 gdb_state.registers[GDB_CPU_I386_REG_GS] = param->registers.gs;
1235 gdb_main(&gdb_state);
1237 /* Restore Registers */
1238 param->registers.eax = gdb_state.registers[GDB_CPU_I386_REG_EAX];
1239 param->registers.ecx = gdb_state.registers[GDB_CPU_I386_REG_ECX];
1240 param->registers.edx = gdb_state.registers[GDB_CPU_I386_REG_EDX];
1241 param->registers.ebx = gdb_state.registers[GDB_CPU_I386_REG_EBX];
1242 param->esp = gdb_state.registers[GDB_CPU_I386_REG_ESP];
1243 param->registers.ebp = gdb_state.registers[GDB_CPU_I386_REG_EBP];
1244 param->registers.esi = gdb_state.registers[GDB_CPU_I386_REG_ESI];
1245 param->registers.edi = gdb_state.registers[GDB_CPU_I386_REG_EDI];
1246 param->execp->eip = gdb_state.registers[GDB_CPU_I386_REG_PC];
1247 param->execp->cs = gdb_state.registers[GDB_CPU_I386_REG_CS];
1248 param->execp->eflags = gdb_state.registers[GDB_CPU_I386_REG_PS];
1249 param->execp->ss = gdb_state.registers[GDB_CPU_I386_REG_SS];
1250 param->registers.ds = gdb_state.registers[GDB_CPU_I386_REG_DS];
1251 param->registers.es = gdb_state.registers[GDB_CPU_I386_REG_ES];
1252 param->registers.fs = gdb_state.registers[GDB_CPU_I386_REG_FS];
1253 param->registers.gs = gdb_state.registers[GDB_CPU_I386_REG_GS];
1256 /*****************************************************************************
1257 * Debugging System Functions
1258 ****************************************************************************/
1261 * Write one character to the debugging stream.
1264 gdb_sys_putchar(struct gdb_state* state, int ch)
1266 serial_rwbuf_sync(state->sdev, &ch, 1, SERIAL_RW_TX);
1271 * Read one character from the debugging stream.
1274 gdb_sys_getc(struct gdb_state* state)
1277 serial_rwbuf_sync(state->sdev, &ch, 1, SERIAL_RW_RX);
1282 * Read one byte from memory.
1285 gdb_sys_mem_readb(struct gdb_state* state, address addr, char* val)
1287 *val = *(volatile char*)addr;
1292 * Write one byte to memory.
1295 gdb_sys_mem_writeb(struct gdb_state* state, address addr, char val)
1297 *(volatile char*)addr = val;
1302 * Continue program execution.
1305 gdb_sys_continue(struct gdb_state* state)
1307 gdb_state.registers[GDB_CPU_I386_REG_PS] &= ~(1 << 8);
1312 * Single step the next instruction.
1315 gdb_sys_step(struct gdb_state* state)
1317 gdb_state.registers[GDB_CPU_I386_REG_PS] |= 1 << 8;