Ver código fonte

[GDB] Add GDB stub for remote debugging

See http://etherboot.org/wiki/dev/gdbstub for documentation.
tags/v0.9.4
Stefan Hajnoczi 16 anos atrás
pai
commit
04bc50f025

+ 209
- 0
src/arch/i386/core/gdbidt.S Ver arquivo

@@ -0,0 +1,209 @@
1
+/*
2
+ * Interrupt Descriptor Table (IDT) setup and interrupt handlers for GDB stub.
3
+ */
4
+
5
+#include <virtaddr.h>
6
+
7
+#define SIZEOF_I386_REGS	32
8
+#define SIZEOF_I386_FLAGS	4
9
+
10
+/****************************************************************************
11
+ * Interrupt Descriptor Table
12
+ ****************************************************************************
13
+ */
14
+	.section ".data16"
15
+	.globl idtr
16
+idtr:
17
+idt_limit:
18
+	.word	idt_length - 1
19
+idt_base:
20
+	.long	0
21
+
22
+/* IDT entries have the following format:
23
+ * offset_lo, segment selector, flags, offset_hi
24
+ *
25
+ * Since it is not possible to specify relocations in arbitrary
26
+ * expressions like (int_overflow & 0xffff), we initialise the
27
+ * IDT with entries in an incorrect format.
28
+ *
29
+ * The entries are shuffled into the correct format in init_librm().
30
+ */
31
+#define IDT_ENTRY_EMPTY(name) .word 0, 0, 0, 0
32
+#define IDT_ENTRY_PRESENT(name) \
33
+	.long	int_##name; \
34
+	.word	0x8e00, VIRTUAL_CS
35
+
36
+.align 16
37
+idt:
38
+	IDT_ENTRY_PRESENT(divide_error)
39
+	IDT_ENTRY_PRESENT(debug_trap)
40
+	IDT_ENTRY_EMPTY(non_maskable_interrupt)
41
+	IDT_ENTRY_PRESENT(breakpoint)
42
+	IDT_ENTRY_PRESENT(overflow)
43
+	IDT_ENTRY_PRESENT(bound_range_exceeded)
44
+	IDT_ENTRY_PRESENT(invalid_opcode)
45
+	IDT_ENTRY_EMPTY(device_not_available)
46
+	IDT_ENTRY_PRESENT(double_fault)
47
+	IDT_ENTRY_EMPTY(coprocessor_segment_overrun)
48
+	IDT_ENTRY_PRESENT(invalid_tss)
49
+	IDT_ENTRY_PRESENT(segment_not_present)
50
+	IDT_ENTRY_PRESENT(stack_segment_fault)
51
+	IDT_ENTRY_PRESENT(general_protection)
52
+	IDT_ENTRY_PRESENT(page_fault)
53
+idt_end:
54
+	.equ	idt_length, idt_end - idt
55
+
56
+/* The IDT entries are fixed up (once) in init_librm() */
57
+idt_fixed:
58
+	.byte	0
59
+
60
+/****************************************************************************
61
+ * idt_init (real-mode near call, 16-bit real-mode near return address)
62
+ *
63
+ * Initialise the IDT, called from init_librm.
64
+ *
65
+ * Parameters:
66
+ *   %eax : IDT base address
67
+ *
68
+ * Destroys %ax, %bx, and %di.
69
+ ****************************************************************************
70
+ */
71
+	.section ".text16"
72
+	.code16
73
+	.globl idt_init
74
+idt_init:
75
+	movl	%eax, idt_base
76
+	addl	$idt, idt_base
77
+
78
+	/* IDT entries are only fixed up once */
79
+	movb	idt_fixed, %al
80
+	orb	%al, %al
81
+	jnz	2f
82
+	movb	$1, idt_fixed
83
+
84
+	/* Shuffle IDT entries into the correct format */
85
+	movb	$(idt_length / 8), %al
86
+	movw	$idt, %bx
87
+	or	%al, %al
88
+	jz	2f
89
+1:
90
+	movw	2(%bx), %di
91
+	xchg	%di, 6(%bx)
92
+	movw	%di, 2(%bx)
93
+	addw	$8, %bx
94
+	dec	%al
95
+	jnz	1b
96
+2:
97
+	ret
98
+
99
+/****************************************************************************
100
+ * Interrupt handlers
101
+ ****************************************************************************
102
+ */
103
+	.section ".text"
104
+	.code32
105
+
106
+/* POSIX signal numbers for reporting traps to GDB */
107
+#define SIGILL 4
108
+#define SIGTRAP 5
109
+#define SIGBUS 7
110
+#define SIGFPE 8
111
+#define SIGSEGV 11
112
+#define SIGSTKFLT 16
113
+
114
+int_divide_error:
115
+	pushl	$SIGFPE
116
+	jmp	do_interrupt
117
+
118
+int_debug_trap:
119
+int_breakpoint:
120
+	pushl	$SIGTRAP
121
+	jmp	do_interrupt
122
+
123
+int_overflow:
124
+int_bound_range_exceeded:
125
+	pushl	$SIGSTKFLT
126
+	jmp	do_interrupt
127
+
128
+int_invalid_opcode:
129
+	pushl	$SIGILL
130
+	jmp	do_interrupt
131
+
132
+int_double_fault:
133
+	movl	$SIGBUS, (%esp)
134
+	jmp	do_interrupt
135
+
136
+int_invalid_tss:
137
+int_segment_not_present:
138
+int_stack_segment_fault:
139
+int_general_protection:
140
+int_page_fault:
141
+	movl	$SIGSEGV, (%esp)
142
+	jmp	do_interrupt
143
+
144
+/* When invoked, the stack contains: eflags, cs, eip, signo. */
145
+#define IH_OFFSET_GDB_REGS ( 0 )
146
+#define IH_OFFSET_GDB_EIP ( IH_OFFSET_GDB_REGS + SIZEOF_I386_REGS )
147
+#define IH_OFFSET_GDB_EFLAGS ( IH_OFFSET_GDB_EIP + 4 )
148
+#define IH_OFFSET_GDB_SEG_REGS ( IH_OFFSET_GDB_EFLAGS + SIZEOF_I386_FLAGS )
149
+#define IH_OFFSET_GDB_END ( IH_OFFSET_GDB_SEG_REGS + 6 * 4 )
150
+#define IH_OFFSET_SIGNO ( IH_OFFSET_GDB_END )
151
+#define IH_OFFSET_OLD_EIP ( IH_OFFSET_SIGNO + 4 )
152
+#define IH_OFFSET_OLD_CS ( IH_OFFSET_OLD_EIP + 4 )
153
+#define IH_OFFSET_OLD_EFLAGS ( IH_OFFSET_OLD_CS + 4 )
154
+#define IH_OFFSET_END ( IH_OFFSET_OLD_EFLAGS + 4 )
155
+
156
+/* We also access the stack whilst still storing or restoring
157
+ * the register snapshot.  Since ESP is in flux, we need
158
+ * special offsets.
159
+ */
160
+#define IH_OFFSET_FLUX_OLD_CS ( IH_OFFSET_OLD_CS - 44 )
161
+#define IH_OFFSET_FLUX_OLD_EFLAGS ( IH_OFFSET_OLD_EFLAGS - 40 )
162
+#define IH_OFFSET_FLUX_OLD_EIP ( IH_OFFSET_OLD_EIP - 36 )
163
+#define IH_OFFSET_FLUX_END ( IH_OFFSET_END - 20 )
164
+do_interrupt:
165
+	/* Store CPU state in GDB register snapshot */
166
+	pushl	%gs
167
+	pushl	%fs
168
+	pushl	%es
169
+	pushl	%ds
170
+	pushl	%ss
171
+	pushl	IH_OFFSET_FLUX_OLD_CS(%esp)
172
+	pushl	IH_OFFSET_FLUX_OLD_EFLAGS(%esp)
173
+	pushl	IH_OFFSET_FLUX_OLD_EIP(%esp)
174
+	pushl	%edi
175
+	pushl	%esi
176
+	pushl	%ebp
177
+	leal	IH_OFFSET_FLUX_END(%esp), %edi
178
+	pushl	%edi /* old ESP */
179
+	pushl	%ebx
180
+	pushl	%edx
181
+	pushl	%ecx
182
+	pushl	%eax
183
+
184
+	/* Call GDB stub exception handler */
185
+	pushl	%esp
186
+	pushl	(IH_OFFSET_SIGNO + 4)(%esp)
187
+	call	gdbstub_handler
188
+	addl	$8, %esp
189
+
190
+	/* Restore CPU state from GDB register snapshot */
191
+	popl	%eax
192
+	popl	%ecx
193
+	popl	%edx
194
+	popl	%ebx
195
+	addl	$4, %esp /* Changing ESP currently not supported */
196
+	popl	%ebp
197
+	popl	%esi
198
+	popl	%edi
199
+	popl	IH_OFFSET_FLUX_OLD_EIP(%esp)
200
+	popl	IH_OFFSET_FLUX_OLD_EFLAGS(%esp)
201
+	popl	IH_OFFSET_FLUX_OLD_CS(%esp)
202
+	popl	%ss
203
+	popl	%ds
204
+	popl	%es
205
+	popl	%fs
206
+	popl	%gs
207
+
208
+	addl	$4, %esp /* drop signo */
209
+	iret

+ 51
- 0
src/arch/i386/include/gdbmach.h Ver arquivo

@@ -0,0 +1,51 @@
1
+#ifndef GDBMACH_H
2
+#define GDBMACH_H
3
+
4
+/** @file
5
+ *
6
+ * GDB architecture specifics
7
+ *
8
+ * This file declares functions for manipulating the machine state and
9
+ * debugging context.
10
+ *
11
+ */
12
+
13
+typedef uint32_t gdbreg_t;
14
+
15
+/* The register snapshot, this must be in sync with interrupt handler and the
16
+ * GDB protocol. */
17
+enum {
18
+	GDBMACH_EAX,
19
+	GDBMACH_ECX,
20
+	GDBMACH_EDX,
21
+	GDBMACH_EBX,
22
+	GDBMACH_ESP,
23
+	GDBMACH_EBP,
24
+	GDBMACH_ESI,
25
+	GDBMACH_EDI,
26
+	GDBMACH_EIP,
27
+	GDBMACH_EFLAGS,
28
+	GDBMACH_CS,
29
+	GDBMACH_SS,
30
+	GDBMACH_DS,
31
+	GDBMACH_ES,
32
+	GDBMACH_FS,
33
+	GDBMACH_GS,
34
+	GDBMACH_NREGS,
35
+	GDBMACH_SIZEOF_REGS = GDBMACH_NREGS * sizeof ( gdbreg_t )
36
+};
37
+
38
+static inline void gdbmach_set_pc ( gdbreg_t *regs, gdbreg_t pc ) {
39
+	regs [ GDBMACH_EIP ] = pc;
40
+}
41
+
42
+static inline void gdbmach_set_single_step ( gdbreg_t *regs, int step ) {
43
+	regs [ GDBMACH_EFLAGS ] &= ~( 1 << 8 ); /* Trace Flag (TF) */
44
+	regs [ GDBMACH_EFLAGS ] |= ( step << 8 );
45
+}
46
+
47
+static inline void gdbmach_breakpoint ( void ) {
48
+	__asm__ __volatile__ ( "int $3\n" );
49
+}
50
+
51
+#endif /* GDBMACH_H */

+ 32
- 11
src/arch/i386/transitions/librm.S Ver arquivo

@@ -50,6 +50,7 @@
50 50
 	.section ".data16"
51 51
 	.align 16
52 52
 gdt:
53
+gdtr:		/* The first GDT entry is unused, the GDTR can fit here. */
53 54
 gdt_limit:		.word gdt_length - 1
54 55
 gdt_base:		.long 0
55 56
 			.word 0 /* padding */
@@ -127,7 +128,7 @@ init_librm:
127 128
 	addr32 leal	(%eax, %edi), %ebx
128 129
 	movl	%ebx, _text16
129 130
 
130
-	/* Store rm_ds and _data16, set up real_ds segment and set GDT base */
131
+	/* Store rm_ds and _data16, set up real_ds segment */
131 132
 	xorl	%eax, %eax
132 133
 	movw	%ds, %ax
133 134
 	movw	%ax, %cs:rm_ds
@@ -136,9 +137,12 @@ init_librm:
136 137
 	call	set_seg_base
137 138
 	addr32 leal	(%eax, %edi), %ebx
138 139
 	movl	%ebx, _data16
139
-	addl	$gdt, %eax
140
+
141
+	/* Set GDT and IDT base */
140 142
 	movl	%eax, gdt_base
141
-		
143
+	addl	$gdt, gdt_base
144
+	call	idt_init
145
+
142 146
 	/* Restore registers */
143 147
 	negl	%edi
144 148
 	popl	%ebx
@@ -147,14 +151,16 @@ init_librm:
147 151
 
148 152
 	.section ".text16"
149 153
 	.code16
154
+	.weak idt_init
150 155
 set_seg_base:
151 156
 1:	movw	%ax, 2(%bx)
152 157
 	rorl	$16, %eax
153 158
 	movb	%al, 4(%bx)
154 159
 	movb	%ah, 7(%bx)
155 160
 	roll	$16, %eax
161
+idt_init: /* Reuse the return opcode here */
156 162
 	ret
157
-	
163
+
158 164
 /****************************************************************************
159 165
  * real_to_prot (real-mode near call, 32-bit virtual return address)
160 166
  *
@@ -197,7 +203,8 @@ real_to_prot:
197 203
 
198 204
 	/* Switch to protected mode */
199 205
 	cli
200
-	data32 lgdt	gdt
206
+	data32 lgdt	gdtr
207
+	data32 lidt	idtr
201 208
 	movl	%cr0, %eax
202 209
 	orb	$CR0_PE, %al
203 210
 	movl	%eax, %cr0
@@ -232,6 +239,14 @@ real_to_prot:
232 239
 	/* Return to virtual address */
233 240
 	ret
234 241
 
242
+	/* Default IDTR with no interrupts */
243
+	.section ".data16"
244
+	.weak idtr
245
+idtr:
246
+rm_idtr:
247
+	.word 0xffff /* limit */
248
+	.long 0 /* base */
249
+
235 250
 /****************************************************************************
236 251
  * prot_to_real (protected-mode near call, 32-bit real-mode return address)
237 252
  *
@@ -300,6 +315,9 @@ p2r_jump_target:
300 315
 	movw	%bp, %ss
301 316
 	movl	%edx, %esp
302 317
 
318
+	/* Reset IDTR to the real-mode defaults */
319
+	lidt	rm_idtr
320
+
303 321
 	/* Return to real-mode address */
304 322
 	data32 ret
305 323
 
@@ -318,7 +336,7 @@ rm_cs:	.word 0
318 336
 	.globl rm_ds
319 337
 	.section ".text16.data"
320 338
 rm_ds:	.word 0
321
-	
339
+
322 340
 /****************************************************************************
323 341
  * prot_call (real-mode far call, 16-bit real-mode far return address)
324 342
  *
@@ -354,7 +372,8 @@ rm_ds:	.word 0
354 372
  */
355 373
 
356 374
 #define PC_OFFSET_GDT ( 0 )
357
-#define PC_OFFSET_IX86 ( PC_OFFSET_GDT + 8 /* pad to 8 to keep alignment */ )
375
+#define PC_OFFSET_IDT ( PC_OFFSET_GDT + 8 /* pad to 8 to keep alignment */ )
376
+#define PC_OFFSET_IX86 ( PC_OFFSET_IDT + 8 /* pad to 8 to keep alignment */ )
358 377
 #define PC_OFFSET_RETADDR ( PC_OFFSET_IX86 + SIZEOF_I386_ALL_REGS )
359 378
 #define PC_OFFSET_FUNCTION ( PC_OFFSET_RETADDR + 4 )
360 379
 #define PC_OFFSET_END ( PC_OFFSET_FUNCTION + 4 )
@@ -372,8 +391,9 @@ prot_call:
372 391
 	pushw	%ds
373 392
 	pushw	%ss
374 393
 	pushw	%cs
375
-	subw	$8, %sp
394
+	subw	$16, %sp
376 395
 	movw	%sp, %bp
396
+	sidt	8(%bp)
377 397
 	sgdt	(%bp)
378 398
 
379 399
 	/* For sanity's sake, clear the direction flag as soon as possible */
@@ -402,10 +422,11 @@ prot_call:
402 422
 	.section ".text16"
403 423
 	.code16
404 424
 1:	
405
-	/* Reload GDT, restore registers and flags and return */
425
+	/* Reload GDT and IDT, restore registers and flags and return */
406 426
 	movw	%sp, %bp
407 427
 	lgdt	(%bp)
408
-	addw	$12, %sp /* also skip %cs and %ss */
428
+	lidt	8(%bp)
429
+	addw	$20, %sp /* also skip %cs and %ss */
409 430
 	popw	%ds
410 431
 	popw	%es
411 432
 	popw	%fs
@@ -495,7 +516,7 @@ real_call:
495 516
 	 */
496 517
 	.section ".data16"
497 518
 rc_function:	.word 0, 0
498
-	
519
+
499 520
 /****************************************************************************
500 521
  * Stored real-mode and protected-mode stack pointers
501 522
  *

+ 1
- 0
src/config.h Ver arquivo

@@ -164,6 +164,7 @@
164 164
 #undef	BUILD_ID		/* Include a custom build ID string,
165 165
 				 * e.g "test-foo" */
166 166
 #undef	NULL_TRAP		/* Attempt to catch NULL function calls */
167
+#undef	GDBSTUB			/* Remote GDB debugging */
167 168
 
168 169
 /* @END general.h */
169 170
 

+ 3
- 0
src/core/config.c Ver arquivo

@@ -198,3 +198,6 @@ REQUIRE_OBJECT ( sanboot_cmd );
198 198
 #ifdef NULL_TRAP
199 199
 REQUIRE_OBJECT ( nulltrap );
200 200
 #endif
201
+#ifdef GDBSTUB
202
+REQUIRE_OBJECT ( gdbidt );
203
+#endif

+ 330
- 0
src/core/gdbstub.c Ver arquivo

@@ -0,0 +1,330 @@
1
+/*
2
+ * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+/**
20
+ * @file
21
+ *
22
+ * GDB stub for remote debugging
23
+ *
24
+ */
25
+
26
+#include <stdlib.h>
27
+#include <stddef.h>
28
+#include <stdio.h>
29
+#include <ctype.h>
30
+#include <assert.h>
31
+#include <gpxe/process.h>
32
+#include <gpxe/serial.h>
33
+#include "gdbmach.h"
34
+
35
+enum {
36
+	POSIX_EINVAL = 0x1c /* used to report bad arguments to GDB */
37
+};
38
+
39
+struct gdbstub {
40
+	int signo;
41
+	gdbreg_t *regs;
42
+	int exit_handler; /* leave interrupt handler */
43
+
44
+	void ( * parse ) ( struct gdbstub *stub, char ch );
45
+	uint8_t cksum1;
46
+
47
+	/* Buffer for payload data when parsing a packet.  Once the
48
+	 * packet has been received, this buffer is used to hold
49
+	 * the reply payload. */
50
+	char payload [ 256 ];
51
+	int len;
52
+};
53
+
54
+/* Packet parser states */
55
+static void gdbstub_state_new ( struct gdbstub *stub, char ch );
56
+static void gdbstub_state_data ( struct gdbstub *stub, char ch );
57
+static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch );
58
+static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch );
59
+static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch );
60
+
61
+static uint8_t gdbstub_from_hex_digit ( char ch ) {
62
+	return ( isdigit ( ch ) ? ch - '0' : tolower ( ch ) - 'a' + 0xa ) & 0xf;
63
+}
64
+
65
+static uint8_t gdbstub_to_hex_digit ( uint8_t b ) {
66
+	b &= 0xf;
67
+	return ( b < 0xa ? '0' : 'a' - 0xa ) + b;
68
+}
69
+
70
+static void gdbstub_from_hex_buf ( char *dst, char *src, int len ) {
71
+	while ( len-- > 0 ) {
72
+		*dst = gdbstub_from_hex_digit ( *src++ );
73
+		if ( len-- > 0 ) {
74
+			*dst = (*dst << 4) | gdbstub_from_hex_digit ( *src++ );
75
+		}
76
+		dst++;
77
+	}
78
+}
79
+
80
+static void gdbstub_to_hex_buf ( char *dst, char *src, int len ) {
81
+	while ( len-- > 0 ) {
82
+		*dst++ = gdbstub_to_hex_digit ( *src >> 4 );
83
+		*dst++ = gdbstub_to_hex_digit ( *src++ );
84
+	}
85
+}
86
+
87
+static uint8_t gdbstub_cksum ( char *data, int len ) {
88
+	uint8_t cksum = 0;
89
+	while ( len-- > 0 ) {
90
+		cksum += ( uint8_t ) *data++;
91
+	}
92
+	return cksum;
93
+}
94
+
95
+static int gdbstub_getchar ( struct gdbstub *stub ) {
96
+	if ( stub->exit_handler ) {
97
+		return -1;
98
+	}
99
+	return serial_getc();
100
+}
101
+
102
+static void gdbstub_putchar ( struct gdbstub * stub __unused, char ch ) {
103
+	serial_putc ( ch );
104
+}
105
+
106
+static void gdbstub_tx_packet ( struct gdbstub *stub ) {
107
+	uint8_t cksum = gdbstub_cksum ( stub->payload, stub->len );
108
+	int i;
109
+
110
+	gdbstub_putchar ( stub, '$' );
111
+	for ( i = 0; i < stub->len; i++ ) {
112
+		gdbstub_putchar ( stub, stub->payload [ i ] );
113
+	}
114
+	gdbstub_putchar ( stub, '#' );
115
+	gdbstub_putchar ( stub, gdbstub_to_hex_digit ( cksum >> 4 ) );
116
+	gdbstub_putchar ( stub, gdbstub_to_hex_digit ( cksum ) );
117
+
118
+	stub->parse = gdbstub_state_wait_ack;
119
+}
120
+
121
+/* GDB commands */
122
+static void gdbstub_send_ok ( struct gdbstub *stub ) {
123
+	stub->payload [ 0 ] = 'O';
124
+	stub->payload [ 1 ] = 'K';
125
+	stub->len = 2;
126
+	gdbstub_tx_packet ( stub );
127
+}
128
+
129
+static void gdbstub_send_num_packet ( struct gdbstub *stub, char reply, int num ) {
130
+	stub->payload [ 0 ] = reply;
131
+	stub->payload [ 1 ] = gdbstub_to_hex_digit ( ( char ) num >> 4 );
132
+	stub->payload [ 2 ] = gdbstub_to_hex_digit ( ( char ) num );
133
+	stub->len = 3;
134
+	gdbstub_tx_packet ( stub );
135
+}
136
+
137
+/* Format is arg1,arg2,...,argn:data where argn are hex integers and data is not an argument */
138
+static int gdbstub_get_packet_args ( struct gdbstub *stub, unsigned long *args, int nargs, int *stop_idx ) {
139
+	int i;
140
+	char ch = 0;
141
+	int argc = 0;
142
+	unsigned long val = 0;
143
+	for ( i = 1; i < stub->len && argc < nargs; i++ ) {
144
+		ch = stub->payload [ i ];
145
+		if ( ch == ':' ) {
146
+			break;
147
+		} else if ( ch == ',' ) {
148
+			args [ argc++ ] = val;
149
+			val = 0;
150
+		} else {
151
+			val = ( val << 4 ) | gdbstub_from_hex_digit ( ch );
152
+		}
153
+	}
154
+	if ( stop_idx ) {
155
+		*stop_idx = i;
156
+	}
157
+	if ( argc < nargs ) {
158
+		args [ argc++ ] = val;
159
+	}
160
+	return ( ( i == stub->len || ch == ':' ) && argc == nargs );
161
+}
162
+
163
+static void gdbstub_send_errno ( struct gdbstub *stub, int errno ) {
164
+	gdbstub_send_num_packet ( stub, 'E', errno );
165
+}
166
+
167
+static void gdbstub_report_signal ( struct gdbstub *stub ) {
168
+	gdbstub_send_num_packet ( stub, 'S', stub->signo );
169
+}
170
+
171
+static void gdbstub_read_regs ( struct gdbstub *stub ) {
172
+	gdbstub_to_hex_buf ( stub->payload, ( char * ) stub->regs, GDBMACH_SIZEOF_REGS );
173
+	stub->len = GDBMACH_SIZEOF_REGS * 2;
174
+	gdbstub_tx_packet ( stub );
175
+}
176
+
177
+static void gdbstub_write_regs ( struct gdbstub *stub ) {
178
+	if ( stub->len != 1 + GDBMACH_SIZEOF_REGS * 2 ) {
179
+		gdbstub_send_errno ( stub, POSIX_EINVAL );
180
+		return;
181
+	}
182
+	gdbstub_from_hex_buf ( ( char * ) stub->regs, &stub->payload [ 1 ], stub->len );
183
+	gdbstub_send_ok ( stub );
184
+}
185
+
186
+static void gdbstub_read_mem ( struct gdbstub *stub ) {
187
+	unsigned long args [ 2 ];
188
+	if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
189
+		gdbstub_send_errno ( stub, POSIX_EINVAL );
190
+		return;
191
+	}
192
+	args [ 1 ] = ( args [ 1 ] < sizeof stub->payload / 2 ) ? args [ 1 ] : sizeof stub->payload / 2;
193
+	gdbstub_to_hex_buf ( stub->payload, ( char * ) args [ 0 ], args [ 1 ] );
194
+	stub->len = args [ 1 ] * 2;
195
+	gdbstub_tx_packet ( stub );
196
+}
197
+
198
+static void gdbstub_write_mem ( struct gdbstub *stub ) {
199
+	unsigned long args [ 2 ];
200
+	int colon;
201
+	if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], &colon ) ||
202
+			colon >= stub->len || stub->payload [ colon ] != ':' ||
203
+			( stub->len - colon - 1 ) % 2 != 0 ) {
204
+		gdbstub_send_errno ( stub, POSIX_EINVAL );
205
+		return;
206
+	}
207
+	gdbstub_from_hex_buf ( ( char * ) args [ 0 ], &stub->payload [ colon + 1 ], stub->len - colon - 1 );
208
+	gdbstub_send_ok ( stub );
209
+}
210
+
211
+static void gdbstub_continue ( struct gdbstub *stub, int single_step ) {
212
+	gdbreg_t pc;
213
+	if ( stub->len > 1 && gdbstub_get_packet_args ( stub, &pc, 1, NULL ) ) {
214
+		gdbmach_set_pc ( stub->regs, pc );
215
+	}
216
+	gdbmach_set_single_step ( stub->regs, single_step );
217
+	stub->exit_handler = 1;
218
+	/* Reply will be sent when we hit the next breakpoint or interrupt */
219
+}
220
+
221
+static void gdbstub_rx_packet ( struct gdbstub *stub ) {
222
+	switch ( stub->payload [ 0 ] ) {
223
+		case '?':
224
+			gdbstub_report_signal ( stub );
225
+			break;
226
+		case 'g':
227
+			gdbstub_read_regs ( stub );
228
+			break;
229
+		case 'G':
230
+			gdbstub_write_regs ( stub );
231
+			break;
232
+		case 'm':
233
+			gdbstub_read_mem ( stub );
234
+			break;
235
+		case 'M':
236
+			gdbstub_write_mem ( stub );
237
+			break;
238
+		case 'c':
239
+			gdbstub_continue ( stub, 0 );
240
+			break;
241
+		case 's':
242
+			gdbstub_continue ( stub, 1 );
243
+			break;
244
+		default:
245
+			stub->len = 0;
246
+			gdbstub_tx_packet ( stub );
247
+			break;
248
+	}
249
+}
250
+
251
+/* GDB packet parser */
252
+static void gdbstub_state_new ( struct gdbstub *stub, char ch ) {
253
+	if ( ch == '$' ) {
254
+		stub->len = 0;
255
+		stub->parse = gdbstub_state_data;
256
+	}
257
+}
258
+
259
+static void gdbstub_state_data ( struct gdbstub *stub, char ch ) {
260
+	if ( ch == '#' ) {
261
+		stub->parse = gdbstub_state_cksum1;
262
+	} else if ( ch == '$' ) {
263
+		stub->len = 0; /* retry new packet */
264
+	} else {
265
+		/* If the length exceeds our buffer, let the checksum fail */
266
+		if ( stub->len < ( int ) sizeof stub->payload ) {
267
+			stub->payload [ stub->len++ ] = ch;
268
+		}
269
+	}
270
+}
271
+
272
+static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch ) {
273
+	stub->cksum1 = gdbstub_from_hex_digit ( ch ) << 4;
274
+	stub->parse = gdbstub_state_cksum2;
275
+}
276
+
277
+static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch ) {
278
+	uint8_t their_cksum;
279
+	uint8_t our_cksum;
280
+
281
+	stub->parse = gdbstub_state_new;
282
+	their_cksum = stub->cksum1 + gdbstub_from_hex_digit ( ch );
283
+	our_cksum = gdbstub_cksum ( stub->payload, stub->len );
284
+	if ( their_cksum == our_cksum ) {
285
+		gdbstub_putchar ( stub, '+' );
286
+		if ( stub->len > 0 ) {
287
+			gdbstub_rx_packet ( stub );
288
+		}
289
+	} else {
290
+		gdbstub_putchar ( stub, '-' );
291
+	}
292
+}
293
+
294
+static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch ) {
295
+	if ( ch == '+' ) {
296
+		stub->parse = gdbstub_state_new;
297
+	} else if ( ch == '-' ) {
298
+		gdbstub_tx_packet ( stub ); /* retransmit */
299
+	}
300
+}
301
+
302
+static void gdbstub_parse ( struct gdbstub *stub, char ch ) {
303
+	stub->parse ( stub, ch );
304
+}
305
+
306
+static struct gdbstub stub = {
307
+	.parse = gdbstub_state_new
308
+};
309
+
310
+__cdecl void gdbstub_handler ( int signo, gdbreg_t *regs ) {
311
+	int ch;
312
+	stub.signo = signo;
313
+	stub.regs = regs;
314
+	stub.exit_handler = 0;
315
+	gdbstub_report_signal ( &stub );
316
+	while ( ( ch = gdbstub_getchar( &stub ) ) != -1 ) {
317
+		gdbstub_parse ( &stub, ch );
318
+	}
319
+}
320
+
321
+/* Activity monitor to detect packets from GDB when we are not active */
322
+static void gdbstub_activity_step ( struct process *process __unused ) {
323
+	if ( serial_ischar() ) {
324
+		gdbmach_breakpoint();
325
+	}
326
+}
327
+
328
+struct process gdbstub_activity_process __permanent_process = {
329
+	.step = gdbstub_activity_step,
330
+};

Carregando…
Cancelar
Salvar