Mercurial > repos > blastem
comparison gdb_remote.c @ 505:b7b7a1cab44a
The local clone on my laptop got messed up and some changes had not been pushed. This commit represents the status of the working copy from that clone. It unfortunately contains some changes that I did not intend to commit yet, but this seems like the best option at the moment.
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 06 Jan 2014 22:54:05 -0800 |
parents | 140af5509ce7 |
children | 1495179d6737 |
comparison
equal
deleted
inserted
replaced
504:7b0df1aaf384 | 505:b7b7a1cab44a |
---|---|
1 /* | 1 /* |
2 Copyright 2013 Michael Pavone | 2 Copyright 2013 Michael Pavone |
3 This file is part of BlastEm. | 3 This file is part of BlastEm. |
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text. | 4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text. |
5 */ | 5 */ |
6 #include "blastem.h" | 6 #include "blastem.h" |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
12 | 12 |
13 #define INITIAL_BUFFER_SIZE 4096 | 13 #define INITIAL_BUFFER_SIZE 4096 |
14 | 14 |
15 char * buf = NULL; | 15 char * buf = NULL; |
16 char * curbuf = NULL; | 16 char * curbuf = NULL; |
17 char * end = NULL; | |
17 size_t bufsize; | 18 size_t bufsize; |
18 int cont = 0; | 19 int cont = 0; |
19 int expect_break_response=0; | 20 int expect_break_response=0; |
20 uint32_t resume_pc; | 21 uint32_t resume_pc; |
21 | 22 |
22 void gdb_debug_enter(genesis_context * gen, uint32_t pc) | 23 void gdb_debug_enter(genesis_context * gen, uint32_t pc) |
23 { | 24 { |
25 fcntl(STDIN_FILENO, FD_SETFL, 0); | |
24 resume_pc = pc; | 26 resume_pc = pc; |
27 cont = 0; | |
28 uint8_t partial = 0; | |
25 while(!cont) | 29 while(!cont) |
26 { | 30 { |
31 if (!curbuf) { | |
32 int numread = read(STDIN_FILENO, buf, bufsize); | |
33 curbuf = buf; | |
34 end = buf + numread; | |
35 } else if (partial) { | |
36 if (curbuf != buf) { | |
37 memmove(curbuf, buf, end-curbuf); | |
38 end -= cufbuf - buf; | |
39 } | |
40 int numread = read(STDIN_FILENO, end, bufsize - (end-buf)); | |
41 end += numread; | |
42 curbuf = buf; | |
43 } | |
44 for (; curbuf < end; curbuf++) | |
45 { | |
46 if (*curbuf == '$') | |
47 { | |
48 curbuf++; | |
49 char * start = curbuf; | |
50 while (curbuf < end && *curbuf != '#') { | |
51 curbuf++; | |
52 } | |
53 if (*curbuf == '#') { | |
54 //check to make sure we've received the checksum bytes | |
55 if (end-curbuf >= 2) { | |
56 //TODO: verify checksum | |
57 //Null terminate payload | |
58 *curbuf = 0 | |
59 //send acknowledgement | |
60 write(FILENO_STDOUT, "+", 1); | |
61 gdb_run_command(genesis_context * gen, start); | |
62 curbuf += 2; | |
63 } | |
64 } else { | |
65 curbuf--; | |
66 partial = 1; | |
67 break; | |
68 } | |
69 } | |
70 } | |
27 } | 71 } |
28 cont = 0; | 72 fcntl(STDIN_FILENO, FD_SETFL, O_NONBLOCK); |
29 } | 73 } |
30 | 74 |
31 void gdb_run_command(genesis_context * gen, char * command) | 75 void gdb_run_command(genesis_context * gen, char * command) |
32 { | 76 { |
33 switch(*command) | 77 switch(*command) |
88 memmove(buf, buf + consumed, curbuf - buf - consumed); | 132 memmove(buf, buf + consumed, curbuf - buf - consumed); |
89 curbuf -= consumed; | 133 curbuf -= consumed; |
90 } | 134 } |
91 } | 135 } |
92 | 136 |
93 void gdb_command_poll(genesis_context * gen) | 137 int gdb_command_poll(genesis_context * gen) |
94 { | 138 { |
95 for(;;) | 139 for(;;) |
96 { | 140 { |
97 if (curbuf == buf + bufsize) { | 141 if (curbuf == buf + bufsize) { |
98 //buffer is full, expand it | 142 //buffer is full, expand it |
102 fprintf(stderr, "Failed to grow GDB command buffer to %d bytes\n", (int)bufsize); | 146 fprintf(stderr, "Failed to grow GDB command buffer to %d bytes\n", (int)bufsize); |
103 exit(1); | 147 exit(1); |
104 } | 148 } |
105 curbuf = buf + bufsize/2; | 149 curbuf = buf + bufsize/2; |
106 } | 150 } |
107 int numread = read(STDIN_FILENO, buf, bufsize - (curbuf-buf)); | 151 int numread = read(STDIN_FILENO, buf, bufsize); |
108 if (numread < 0) { | 152 if (numread < 0) { |
109 if (errno == EAGAIN || errno == EWOULDBLOCK) { | 153 if (errno == EAGAIN || errno == EWOULDBLOCK) { |
110 return; | 154 return 0; |
111 } else { | 155 } else { |
112 fprintf(stderr, "Error %d while reading GDB commands from stdin", errno); | 156 fprintf(stderr, "Error %d while reading GDB commands from stdin", errno); |
113 exit(1); | 157 exit(1); |
114 } | 158 } |
115 } else if (numread == 0) { | 159 } else if (numread == 0) { |
116 exit(0); | 160 exit(0); |
117 } | 161 } |
118 gdb_run_commands(genesis_context * gen); | 162 for (curbuf = buf, end = buf+numread; curbuf < end; curbuf++) |
163 { | |
164 if (*curbuf = 0x03) | |
165 { | |
166 curbuf++; | |
167 return 1; | |
168 } | |
169 } | |
119 } | 170 } |
171 return 0; | |
120 } | 172 } |
121 | 173 |
122 void gdb_remote_init() | 174 void gdb_remote_init() |
123 { | 175 { |
124 fcntl(STDIN_FILENO, FD_SETFL, O_NONBLOCK); | 176 fcntl(STDIN_FILENO, FD_SETFL, O_NONBLOCK); |