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
36 #include <klibc/string.h>
37 #include <lunaix/peripheral/serial.h>
38 #include <sdbg/gdbstub.h>
40 /*****************************************************************************
42 ****************************************************************************/
44 #ifndef GDBSTUB_DONT_DEFINE_STDINT_TYPES
45 typedef unsigned char uint8_t;
46 typedef unsigned short uint16_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 reg registers[GDB_CPU_NUM_REGISTERS];
80 /*****************************************************************************
82 * GDB Remote Serial Protocol
84 ****************************************************************************/
86 /*****************************************************************************
88 ****************************************************************************/
90 #define GDB_PRINT(...)
92 #define COM_PORT SERIAL_COM1
97 #define NULL ((void*)0)
101 #define GDB_ASSERT(x) \
106 /*****************************************************************************
108 ****************************************************************************/
111 gdb_main(struct gdb_state* state);
113 /* System functions, supported by all stubs */
117 gdb_sys_getc(struct gdb_state* state);
119 gdb_sys_putchar(struct gdb_state* state, int ch);
121 gdb_sys_mem_readb(struct gdb_state* state, address addr, char* val);
123 gdb_sys_mem_writeb(struct gdb_state* state, address addr, char val);
125 gdb_sys_continue(struct gdb_state* state);
127 gdb_sys_step(struct gdb_state* state);
129 /*****************************************************************************
131 ****************************************************************************/
133 typedef int (*gdb_enc_func)(char* buf,
134 unsigned int buf_len,
136 unsigned int data_len);
137 typedef int (*gdb_dec_func)(const char* buf,
138 unsigned int buf_len,
140 unsigned int data_len);
142 /*****************************************************************************
144 ****************************************************************************/
146 static const char digits[] = "0123456789abcdef";
148 /*****************************************************************************
150 ****************************************************************************/
152 /* Communication functions */
154 gdb_write(struct gdb_state* state, const char* buf, unsigned int len);
156 gdb_read(struct gdb_state* state,
158 unsigned int buf_len,
161 /* String processing helper functions */
163 gdb_strlen(const char* ch);
165 gdb_get_digit(int val);
167 gdb_get_val(char digit, int base);
169 gdb_strtol(const char* str, unsigned int len, int base, const char** endptr);
171 /* Packet functions */
173 gdb_send_packet(struct gdb_state* state, const char* pkt, unsigned int pkt_len);
175 gdb_recv_packet(struct gdb_state* state,
177 unsigned int pkt_buf_len,
178 unsigned int* pkt_len);
180 gdb_checksum(const char* buf, unsigned int len);
182 gdb_recv_ack(struct gdb_state* state);
184 /* Data encoding/decoding */
186 gdb_enc_hex(char* buf,
187 unsigned int buf_len,
189 unsigned int data_len);
191 gdb_dec_hex(const char* buf,
192 unsigned int buf_len,
194 unsigned int data_len);
196 gdb_enc_bin(char* buf,
197 unsigned int buf_len,
199 unsigned int data_len);
201 gdb_dec_bin(const char* buf,
202 unsigned int buf_len,
204 unsigned int data_len);
206 /* Packet creation helpers */
208 gdb_send_ok_packet(struct gdb_state* state, char* buf, unsigned int buf_len);
210 gdb_send_conmsg_packet(struct gdb_state* state,
212 unsigned int buf_len,
215 gdb_send_signal_packet(struct gdb_state* state,
217 unsigned int buf_len,
220 gdb_send_error_packet(struct gdb_state* state,
222 unsigned int buf_len,
225 /* Command functions */
227 gdb_mem_read(struct gdb_state* state,
229 unsigned int buf_len,
234 gdb_mem_write(struct gdb_state* state,
236 unsigned int buf_len,
241 gdb_continue(struct gdb_state* state);
243 gdb_step(struct gdb_state* state);
245 /*****************************************************************************
246 * String Processing Helper Functions
247 ****************************************************************************/
250 * Get null-terminated string length.
253 gdb_strlen(const char* ch)
266 * Get integer value for a string representation.
268 * If the string starts with + or -, it will be signed accordingly.
270 * If base == 0, the base will be determined:
271 * base 16 if the string starts with 0x or 0X,
274 * If endptr is specified, it will point to the last non-digit in the
275 * string. If there are no digits in the string, it will be set to NULL.
278 gdb_strtol(const char* str, unsigned int len, int base, const char** endptr)
281 int sign, tmp, value, valid;
296 /* Detect negative numbers */
297 if (str[pos] == '-') {
300 } else if (str[pos] == '+') {
305 /* Detect '0x' hex prefix */
306 if ((pos + 2 < len) && (str[pos] == '0') &&
307 ((str[pos + 1] == 'x') || (str[pos + 1] == 'X'))) {
316 for (; (pos < len) && (str[pos] != '\x00'); pos++) {
317 tmp = gdb_get_val(str[pos], base);
318 if (tmp == GDB_EOF) {
322 value = value * base + tmp;
323 valid = 1; /* At least one digit is valid */
340 * Get the corresponding ASCII hex digit character for a value.
343 gdb_get_digit(int val)
345 if ((val >= 0) && (val <= 0xf)) {
353 * Get the corresponding value for a ASCII digit character.
355 * Supports bases 2-16.
358 gdb_get_val(char digit, int base)
362 if ((digit >= '0') && (digit <= '9')) {
364 } else if ((digit >= 'a') && (digit <= 'f')) {
365 value = digit - 'a' + 0xa;
366 } else if ((digit >= 'A') && (digit <= 'F')) {
367 value = digit - 'A' + 0xa;
372 return (value < base) ? value : GDB_EOF;
375 /*****************************************************************************
377 ****************************************************************************/
380 * Receive a packet acknowledgment
383 * 0 if an ACK (+) was received
384 * 1 if a NACK (-) was received
388 gdb_recv_ack(struct gdb_state* state)
392 /* Wait for packet ack */
393 switch (response = gdb_sys_getc(state)) {
395 /* Packet acknowledged */
398 /* Packet negative acknowledged */
402 GDB_PRINT("received bad packet response: 0x%2x\n", response);
408 * Calculate 8-bit checksum of a buffer.
414 gdb_checksum(const char* buf, unsigned int len)
428 * Transmits a packet of data.
429 * Packets are of the form: $<packet-data>#<checksum>
432 * 0 if the packet was transmitted and acknowledged
433 * 1 if the packet was transmitted but not acknowledged
437 gdb_send_packet(struct gdb_state* state,
438 const char* pkt_data,
439 unsigned int pkt_len)
444 /* Send packet start */
445 if (gdb_sys_putchar(state, '$') == GDB_EOF) {
449 /* Send packet data */
450 if (gdb_write(state, pkt_data, pkt_len) == GDB_EOF) {
454 /* Send the checksum */
456 csum = gdb_checksum(pkt_data, pkt_len);
457 if ((gdb_enc_hex(buf + 1, sizeof(buf) - 1, &csum, 1) == GDB_EOF) ||
458 (gdb_write(state, buf, sizeof(buf)) == GDB_EOF)) {
462 return gdb_recv_ack(state);
466 * Receives a packet of data, assuming a 7-bit clean connection.
469 * 0 if the packet was received
473 gdb_recv_packet(struct gdb_state* state,
475 unsigned int pkt_buf_len,
476 unsigned int* pkt_len)
479 char expected_csum, actual_csum;
482 /* Wait for packet start */
486 data = gdb_sys_getc(state);
487 if (data == GDB_EOF) {
489 } else if (data == '$') {
490 /* Detected start of packet. */
495 /* Read until checksum */
498 data = gdb_sys_getc(state);
500 if (data == GDB_EOF) {
501 /* Error receiving character */
503 } else if (data == '#') {
507 /* Check for space */
508 if (*pkt_len >= pkt_buf_len) {
509 GDB_PRINT("packet buffer overflow\n");
513 /* Store character and update checksum */
514 pkt_buf[(*pkt_len)++] = (char)data;
518 /* Receive the checksum */
519 if ((gdb_read(state, buf, sizeof(buf), 2) == GDB_EOF) ||
520 (gdb_dec_hex(buf, 2, &expected_csum, 1) == GDB_EOF)) {
524 /* Verify checksum */
525 actual_csum = gdb_checksum(pkt_buf, *pkt_len);
526 if (actual_csum != expected_csum) {
527 /* Send packet nack */
528 GDB_PRINT("received packet with bad checksum\n");
529 gdb_sys_putchar(state, '-');
533 /* Send packet ack */
534 gdb_sys_putchar(state, '+');
538 /*****************************************************************************
539 * Data Encoding/Decoding
540 ****************************************************************************/
543 * Encode data to its hex-value representation in a buffer.
546 * 0+ number of bytes written to buf
547 * GDB_EOF if the buffer is too small
550 gdb_enc_hex(char* buf,
551 unsigned int buf_len,
553 unsigned int data_len)
557 if (buf_len < data_len * 2) {
558 /* Buffer too small */
562 for (pos = 0; pos < data_len; pos++) {
563 *buf++ = gdb_get_digit((data[pos] >> 4) & 0xf);
564 *buf++ = gdb_get_digit((data[pos]) & 0xf);
571 * Decode data from its hex-value representation to a buffer.
575 * GDB_EOF if the buffer is too small
578 gdb_dec_hex(const char* buf,
579 unsigned int buf_len,
581 unsigned int data_len)
586 if (buf_len != data_len * 2) {
587 /* Buffer too small */
591 for (pos = 0; pos < data_len; pos++) {
592 /* Decode high nibble */
593 tmp = gdb_get_val(*buf++, 16);
594 if (tmp == GDB_EOF) {
595 /* Buffer contained junk. */
600 data[pos] = tmp << 4;
602 /* Decode low nibble */
603 tmp = gdb_get_val(*buf++, 16);
604 if (tmp == GDB_EOF) {
605 /* Buffer contained junk. */
616 * Encode data to its binary representation in a buffer.
619 * 0+ number of bytes written to buf
620 * GDB_EOF if the buffer is too small
623 gdb_enc_bin(char* buf,
624 unsigned int buf_len,
626 unsigned int data_len)
628 unsigned int buf_pos, data_pos;
630 for (buf_pos = 0, data_pos = 0; data_pos < data_len; data_pos++) {
631 if (data[data_pos] == '$' || data[data_pos] == '#' ||
632 data[data_pos] == '}' || data[data_pos] == '*') {
633 if (buf_pos + 1 >= buf_len) {
637 buf[buf_pos++] = '}';
638 buf[buf_pos++] = data[data_pos] ^ 0x20;
640 if (buf_pos >= buf_len) {
644 buf[buf_pos++] = data[data_pos];
652 * Decode data from its bin-value representation to a buffer.
655 * 0+ if successful, number of bytes decoded
656 * GDB_EOF if the buffer is too small
659 gdb_dec_bin(const char* buf,
660 unsigned int buf_len,
662 unsigned int data_len)
664 unsigned int buf_pos, data_pos;
666 for (buf_pos = 0, data_pos = 0; buf_pos < buf_len; buf_pos++) {
667 if (data_pos >= data_len) {
668 /* Output buffer overflow */
672 if (buf[buf_pos] == '}') {
673 /* The next byte is escaped! */
674 if (buf_pos + 1 >= buf_len) {
675 /* There's an escape character, but no escaped character
676 * following the escape character. */
681 data[data_pos++] = buf[buf_pos] ^ 0x20;
683 data[data_pos++] = buf[buf_pos];
690 /*****************************************************************************
692 ****************************************************************************/
695 * Read from memory and encode into buf.
698 * 0+ number of bytes written to buf
699 * GDB_EOF if the buffer is too small
702 gdb_mem_read(struct gdb_state* state,
704 unsigned int buf_len,
712 if (len > sizeof(data)) {
716 /* Read from system memory */
717 for (pos = 0; pos < len; pos++) {
718 if (gdb_sys_mem_readb(state, addr + pos, &data[pos])) {
725 return enc(buf, buf_len, data, len);
729 * Write to memory from encoded buf.
732 gdb_mem_write(struct gdb_state* state,
734 unsigned int buf_len,
742 if (len > sizeof(data)) {
747 if (dec(buf, buf_len, data, len) == GDB_EOF) {
751 /* Write to system memory */
752 for (pos = 0; pos < len; pos++) {
753 if (gdb_sys_mem_writeb(state, addr + pos, data[pos])) {
754 /* Failed to write */
763 * Continue program execution at PC.
766 gdb_continue(struct gdb_state* state)
768 gdb_sys_continue(state);
773 * Step one instruction.
776 gdb_step(struct gdb_state* state)
782 /*****************************************************************************
783 * Packet Creation Helpers
784 ****************************************************************************/
790 gdb_send_ok_packet(struct gdb_state* state, char* buf, unsigned int buf_len)
792 return gdb_send_packet(state, "OK", 2);
796 * Send a message to the debugging console (via O XX... packet)
799 gdb_send_conmsg_packet(struct gdb_state* state,
801 unsigned int buf_len,
808 /* Buffer too small */
813 status = gdb_enc_hex(&buf[1], buf_len - 1, msg, gdb_strlen(msg));
814 if (status == GDB_EOF) {
818 return gdb_send_packet(state, buf, size);
822 * Send a signal packet (S AA).
825 gdb_send_signal_packet(struct gdb_state* state,
827 unsigned int buf_len,
834 /* Buffer too small */
839 status = gdb_enc_hex(&buf[1], buf_len - 1, &signal, 1);
840 if (status == GDB_EOF) {
844 return gdb_send_packet(state, buf, size);
848 * Send a error packet (E AA).
851 gdb_send_error_packet(struct gdb_state* state,
853 unsigned int buf_len,
860 /* Buffer too small */
865 status = gdb_enc_hex(&buf[1], buf_len - 1, &error, 1);
866 if (status == GDB_EOF) {
870 return gdb_send_packet(state, buf, size);
873 /*****************************************************************************
874 * Communication Functions
875 ****************************************************************************/
878 * Write a sequence of bytes.
882 * GDB_EOF if failed to write all bytes
885 gdb_write(struct gdb_state* state, const char* buf, unsigned int len)
888 if (gdb_sys_putchar(state, *buf++) == GDB_EOF) {
897 * Read a sequence of bytes.
900 * 0 if successfully read len bytes
901 * GDB_EOF if failed to read all bytes
904 gdb_read(struct gdb_state* state,
906 unsigned int buf_len,
912 /* Buffer too small */
917 if ((c = gdb_sys_getc(state)) == GDB_EOF) {
926 /*****************************************************************************
928 ****************************************************************************/
931 * Main debug loop. Handles commands.
934 gdb_main(struct gdb_state* state)
940 unsigned int pkt_len;
941 const char* ptr_next;
943 gdb_send_signal_packet(state, pkt_buf, sizeof(pkt_buf), state->signum);
946 /* Receive the next packet */
947 status = gdb_recv_packet(state, pkt_buf, sizeof(pkt_buf), &pkt_len);
948 if (status == GDB_EOF) {
953 /* Received empty packet.. */
960 * Handle one letter commands
962 switch (pkt_buf[0]) {
964 /* Calculate remaining space in packet from ptr_next position. */
965 #define token_remaining_buf (pkt_len - (ptr_next - pkt_buf))
967 /* Expecting a seperator. If not present, go to error */
968 #define token_expect_seperator(c) \
970 if (!ptr_next || *ptr_next != c) { \
977 /* Expecting an integer argument. If not present, go to error */
978 #define token_expect_integer_arg(arg) \
980 arg = gdb_strtol(ptr_next, token_remaining_buf, 16, &ptr_next); \
991 /* Encode registers */
992 status = gdb_enc_hex(pkt_buf,
994 (char*)&(state->registers),
995 sizeof(state->registers));
996 if (status == GDB_EOF) {
1000 gdb_send_packet(state, pkt_buf, pkt_len);
1005 * Command Format: G XX...
1008 status = gdb_dec_hex(pkt_buf + 1,
1010 (char*)&(state->registers),
1011 sizeof(state->registers));
1012 if (status == GDB_EOF) {
1015 gdb_send_ok_packet(state, pkt_buf, sizeof(pkt_buf));
1020 * Command Format: p n
1024 token_expect_integer_arg(addr);
1026 if (addr >= GDB_CPU_NUM_REGISTERS) {
1031 status = gdb_enc_hex(pkt_buf,
1033 (char*)&(state->registers[addr]),
1034 sizeof(state->registers[addr]));
1035 if (status == GDB_EOF) {
1038 gdb_send_packet(state, pkt_buf, status);
1043 * Command Format: P n...=r...
1047 token_expect_integer_arg(addr);
1048 token_expect_seperator('=');
1050 if (addr < GDB_CPU_NUM_REGISTERS) {
1051 status = gdb_dec_hex(ptr_next,
1052 token_remaining_buf,
1053 (char*)&(state->registers[addr]),
1054 sizeof(state->registers[addr]));
1055 if (status == GDB_EOF) {
1059 gdb_send_ok_packet(state, pkt_buf, sizeof(pkt_buf));
1064 * Command Format: m addr,length
1068 token_expect_integer_arg(addr);
1069 token_expect_seperator(',');
1070 token_expect_integer_arg(length);
1073 status = gdb_mem_read(
1074 state, pkt_buf, sizeof(pkt_buf), addr, length, gdb_enc_hex);
1075 if (status == GDB_EOF) {
1078 gdb_send_packet(state, pkt_buf, status);
1083 * Command Format: M addr,length:XX..
1087 token_expect_integer_arg(addr);
1088 token_expect_seperator(',');
1089 token_expect_integer_arg(length);
1090 token_expect_seperator(':');
1093 status = gdb_mem_write(state,
1095 token_remaining_buf,
1099 if (status == GDB_EOF) {
1102 gdb_send_ok_packet(state, pkt_buf, sizeof(pkt_buf));
1106 * Write Memory (Binary)
1107 * Command Format: X addr,length:XX..
1111 token_expect_integer_arg(addr);
1112 token_expect_seperator(',');
1113 token_expect_integer_arg(length);
1114 token_expect_seperator(':');
1117 status = gdb_mem_write(state,
1119 token_remaining_buf,
1123 if (status == GDB_EOF) {
1126 gdb_send_ok_packet(state, pkt_buf, sizeof(pkt_buf));
1130 * Continue, Kill (also treated as continue!)
1131 * Command Format: c [addr]
1136 gdb_continue(state);
1141 * Command Format: s [addr]
1149 gdb_send_signal_packet(
1150 state, pkt_buf, sizeof(pkt_buf), state->signum);
1154 * Unsupported Command
1157 gdb_send_packet(state, 0, 0);
1163 gdb_send_error_packet(state, pkt_buf, sizeof(pkt_buf), 0x00);
1165 #undef token_remaining_buf
1166 #undef token_expect_seperator
1167 #undef token_expect_integer_arg
1173 /*****************************************************************************
1175 ****************************************************************************/
1181 } __attribute__((packed));
1185 uint16_t offset_low;
1188 uint16_t offset_high;
1189 } __attribute__((packed));
1191 /*****************************************************************************
1193 ****************************************************************************/
1194 #define gdb_x86_io_write_8(port, val) io_outb(port, val)
1195 #define gdb_x86_io_read_8(port) io_inb(port)
1197 #define gdb_x86_serial_getc() serial_rx_byte(COM_PORT)
1198 #define gdb_x86_serial_putchar(ch) serial_tx_byte(COM_PORT, ch)
1200 #ifdef __STRICT_ANSI__
1204 static struct gdb_state gdb_state;
1205 static volatile int start_debugging = 0;
1208 * Debug interrupt handler.
1211 gdbstub_loop(isr_param* param)
1213 /* Translate vector to signal */
1214 switch (param->execp->vector) {
1216 gdb_state.signum = 5;
1219 gdb_state.signum = 5;
1222 gdb_state.signum = 7;
1225 /* Load Registers */
1226 gdb_state.registers[GDB_CPU_I386_REG_EAX] = param->registers.eax;
1227 gdb_state.registers[GDB_CPU_I386_REG_ECX] = param->registers.ecx;
1228 gdb_state.registers[GDB_CPU_I386_REG_EDX] = param->registers.edx;
1229 gdb_state.registers[GDB_CPU_I386_REG_EBX] = param->registers.ebx;
1230 gdb_state.registers[GDB_CPU_I386_REG_ESP] = param->esp;
1231 gdb_state.registers[GDB_CPU_I386_REG_EBP] = param->registers.ebp;
1232 gdb_state.registers[GDB_CPU_I386_REG_ESI] = param->registers.esi;
1233 gdb_state.registers[GDB_CPU_I386_REG_EDI] = param->registers.edi;
1234 gdb_state.registers[GDB_CPU_I386_REG_PC] = param->execp->eip;
1235 gdb_state.registers[GDB_CPU_I386_REG_CS] = param->execp->cs;
1236 gdb_state.registers[GDB_CPU_I386_REG_PS] = param->execp->eflags;
1237 gdb_state.registers[GDB_CPU_I386_REG_SS] = param->execp->ss;
1238 gdb_state.registers[GDB_CPU_I386_REG_DS] = param->registers.ds;
1239 gdb_state.registers[GDB_CPU_I386_REG_ES] = param->registers.es;
1240 gdb_state.registers[GDB_CPU_I386_REG_FS] = param->registers.fs;
1241 gdb_state.registers[GDB_CPU_I386_REG_GS] = param->registers.gs;
1243 gdb_main(&gdb_state);
1245 /* Restore Registers */
1246 param->registers.eax = gdb_state.registers[GDB_CPU_I386_REG_EAX];
1247 param->registers.ecx = gdb_state.registers[GDB_CPU_I386_REG_ECX];
1248 param->registers.edx = gdb_state.registers[GDB_CPU_I386_REG_EDX];
1249 param->registers.ebx = gdb_state.registers[GDB_CPU_I386_REG_EBX];
1250 param->esp = gdb_state.registers[GDB_CPU_I386_REG_ESP];
1251 param->registers.ebp = gdb_state.registers[GDB_CPU_I386_REG_EBP];
1252 param->registers.esi = gdb_state.registers[GDB_CPU_I386_REG_ESI];
1253 param->registers.edi = gdb_state.registers[GDB_CPU_I386_REG_EDI];
1254 param->execp->eip = gdb_state.registers[GDB_CPU_I386_REG_PC];
1255 param->execp->cs = gdb_state.registers[GDB_CPU_I386_REG_CS];
1256 param->execp->eflags = gdb_state.registers[GDB_CPU_I386_REG_PS];
1257 param->execp->ss = gdb_state.registers[GDB_CPU_I386_REG_SS];
1258 param->registers.ds = gdb_state.registers[GDB_CPU_I386_REG_DS];
1259 param->registers.es = gdb_state.registers[GDB_CPU_I386_REG_ES];
1260 param->registers.fs = gdb_state.registers[GDB_CPU_I386_REG_FS];
1261 param->registers.gs = gdb_state.registers[GDB_CPU_I386_REG_GS];
1264 /*****************************************************************************
1265 * Debugging System Functions
1266 ****************************************************************************/
1269 * Write one character to the debugging stream.
1272 gdb_sys_putchar(struct gdb_state* state, int ch)
1274 gdb_x86_serial_putchar(ch);
1279 * Read one character from the debugging stream.
1282 gdb_sys_getc(struct gdb_state* state)
1284 return gdb_x86_serial_getc() & 0xff;
1288 * Read one byte from memory.
1291 gdb_sys_mem_readb(struct gdb_state* state, address addr, char* val)
1293 *val = *(volatile char*)addr;
1298 * Write one byte to memory.
1301 gdb_sys_mem_writeb(struct gdb_state* state, address addr, char val)
1303 *(volatile char*)addr = val;
1308 * Continue program execution.
1311 gdb_sys_continue(struct gdb_state* state)
1313 gdb_state.registers[GDB_CPU_I386_REG_PS] &= ~(1 << 8);
1318 * Single step the next instruction.
1321 gdb_sys_step(struct gdb_state* state)
1323 gdb_state.registers[GDB_CPU_I386_REG_PS] |= 1 << 8;