-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdbstub.cpp
399 lines (362 loc) · 11.9 KB
/
gdbstub.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include "Processor.h"
//http://www.cims.nyu.edu/cgi-systems/info2html?(gdb)Packets
using namespace gdb;
class QueryHandler: public Handler
{
public:
QueryHandler()
{
}
virtual
~QueryHandler()
{
}
virtual bool
onHandle(RSP* rsp, const string& cmd, const string& param)
{
const char* p = param.c_str();
string subcmd = "";
if(!RSP::getNextParamStr(p, subcmd)) {
return false;
}
if(cmd == "Q") {
if(subcmd == "StartNoAckMode") {
rsp->setNoAckMode(true);
rsp->sendPacket("OK");
return true;
}
if(subcmd == "PassSignals") {
rsp->sendPacket("OK");
return true;
}
return false;
}
if(subcmd == "Xfer") {
//$qXfer:features:read:target.xml:0,ffa#78, l = last, m = more
rsp->sendPacket("l<target version=\"1.0\"><architecture>i386</architecture></target>");
return true;
}
if(subcmd == "Supported") {
//$qSupported:xmlRegisters=i386;qRelocInsn+#25
rsp->sendPacketFormat("PacketSize=%x"
";qXfer:libraries:read+"
";qXfer:features:read+" //for registers
// ";qXfer:auxv:read+"
";QStartNoAckMode+"
";QPassSignals+"
, RSP_DEFAULT_BUFFER_SIZE - 1);
return true;
}
if(subcmd == "C") {
//current thread: hex encoded 16it process id
//$qC#b4
rsp->sendPacket("QC1234");
return true;
}
if(subcmd == "Attached") {
//$qAttached#8f
rsp->sendPacket("1");
return true;
}
if(subcmd == "TStatus") {
//$qTStatus#49
return false;
}
///////////////////////////////////////////////////////////////////////////////
//info threads
if(subcmd == "fThreadInfo") {
//all thread ids: first
//$qfThreadInfo#bb
rsp->sendPacket("m1234,1000,2000,3000");
return true;
}
if(subcmd == "sThreadInfo") {
//all thread ids: subsequent
//$qsThreadInfo#bb
rsp->sendPacket("l");
return true;
}
if(subcmd == "ThreadExtraInfo") {
//extra thread info
rsp->sendPacket(RSP::hexify("thread info la la la").c_str()); //$qThreadExtraInfo,1234#4f
return true;
}
if(subcmd == "L") { //replaced by qThreadExtraInfo
//query LIST or thread LIST (deprecated)
//$qL1200000000000000000#50
return false;
}
///////////////////////////////////////////////////////////////////////////////
if(subcmd == "CRC") {
//query CRC of memory block
//qCRC:addr,len
//Ccrc32
return false;
}
if(subcmd == "Offsets") {
//get section offsets
rsp->sendPacket("Text=0;Data=1;Bss=2");
return true;
}
if(subcmd == "P") {
//$qP0000001f0000000000001234#82
return false;
}
if(subcmd == "Symbol") {
string sym_name;
string sym_value;
if(!RSP::getNextParamStr(p, sym_name)) {
return false;
}
if(!RSP::getNextParamStr(p, sym_value)) {
return false;
}
if(sym_name == "" && sym_value == "") {
rsp->sendPacketFormat("qSymbol:%s", RSP::hexify("main").c_str());
} else {
rsp->sendPacket("OK");
}
}
return true;
}
};
class MemoryHandler: public Handler
{
public:
MemoryHandler()
{
}
virtual
~MemoryHandler()
{
}
virtual bool
onHandle(RSP* rsp, const string& cmd, const string& param)
{
if(cmd == "m") {
const char* p = param.c_str();
int addr = 0;
int len = 0;
if(RSP::getNextParamInt(p, addr, 16) && RSP::getNextParamInt(p, len, 16)) {
string result = "";
for(; len > 0; len--) {
result += "00";
}
rsp->sendPacket(result.c_str());
} else {
rsp->sendPacket("E00");
}
} else if(cmd == "M") {
rsp->sendPacket("OK");
}
return true;
}
};
class ContinueHandler: public Handler
{
public:
ContinueHandler()
{
}
virtual
~ContinueHandler()
{
}
virtual bool
onHandle(RSP* rsp, const string& cmd, const string& param)
{
if(cmd == "C") {
fprintf(stderr, "send signal %s\n", param.c_str());
rsp->sendPacketFormat("S%s", param.c_str());
return true;
}
if(cmd == "c") {
fprintf(stderr, "resume at %s...\n", param == ""? "current": param.c_str());
}
while(true) {
fprintf(stderr, "continue...\n");
if(rsp->isInterrupted()) {
rsp->sendPacketFormat("S%02x", SIGINT);
break;
}
sleep(1);
}
return true;
}
};
class StepHandler: public Handler
{
public:
StepHandler()
{
}
virtual
~StepHandler()
{
}
virtual bool
onHandle(RSP* rsp, const string& cmd, const string& param)
{
if(cmd == "S") {
fprintf(stderr, "send signal %s\n", param.c_str());
rsp->sendPacketFormat("S%s", param.c_str());
return true;
}
if(cmd == "s") {
fprintf(stderr, "stepping at %s...\n", param == ""? "current": param.c_str());
}
rsp->sendPacketFormat("T%02x05:01020304;04:02030405;08:03040506;thread:1234;", SIGTRAP);
//rsp->sendPacketFormat("T%02x", SIGTRAP);
//rsp->sendPacket("OK");
return true;
}
};
class RemoteCommandHandler: public Handler
{
public:
RemoteCommandHandler()
{
}
virtual
~RemoteCommandHandler()
{
}
virtual bool
onHandle(RSP* rsp, const string& cmd, const string& param)
{
fprintf(stderr, "remote command: %s ==> %s\n", param.c_str(), RSP::unhexify(param).c_str());
rsp->sendPacketFormat("O%s", RSP::hexify("how are you?\n").c_str());
rsp->sendPacket("OK");
return true;
}
};
int
main(int argc, char** argv)
{
const char* name = "tcp";
const char* params = "1234";
if(argc > 1) {
argc--, argv++;
for(; argc > 0; argv++, argc--) {
if(strncasecmp(*argv, "--tcp", 5) == 0) {
name = "tcp";
if(argc > 1) {
params = *++argv, argc--;
continue;
}
params = "1234";
continue;
}
if(strncasecmp(*argv, "--stdio", 7) == 0) {
name = "stdio";
params = "";
continue;
}
}
}
Processor* processor = new Processor();
///////////////////////////////////////////////////////////////////////////////////////////////////////
//a -- reserved
//Aarglen,argnum,arg,... -- set program arguments (reserved)
//b -- set baud (deprecated)
//Baddr,mode -- set breakpoint (deprecated); mode = {'S': set, 'C': clear}, replaced by 'Z'/'z'
///////////////////////////////////////////////////////////////////////////////////////////////////////
//caddr -- continue; if addr is omitted, resume at current addr.
ContinueHandler continue_handler;
processor->defineResponse("c", &continue_handler); //$c#63
//Csig;addr -- continue with signal in hex; if ';addr' is omitted, resume the same addr.
processor->defineResponse("C", &continue_handler); //$C01#a4
///////////////////////////////////////////////////////////////////////////////////////////////////////
//d -- toggle debug flag (deprecated)
//D -- detach
processor->defineResponse("D", "OK"); //$D#44 (no reply)
///////////////////////////////////////////////////////////////////////////////////////////////////////
//e -- reserved
//E -- reserved
//f -- reserved
//F -- reserved
///////////////////////////////////////////////////////////////////////////////////////////////////////
//g -- read registers: REGISTER_RAW_SIZE and REGISTER_NAME
processor->defineResponse("g", "xxxxxxxx0000000100000002"); //$g#67
//GXX... -- write registers
processor->defineResponse("G", "OK"); //$G#67
///////////////////////////////////////////////////////////////////////////////////////////////////////
//h -- reserved
//Hct -- set thread for subsequent op (m, M, g, G...), c for step continue, t = -1: for all threads
//Hgt -- set thread for subsequent op (m, M, g, G...), g for other operations, t = -1: for all threads
processor->defineResponse("Hc", "OK"); //$Hc-1#09 $Hc0#db
processor->defineResponse("Hg", "OK"); //$Hg0#df
///////////////////////////////////////////////////////////////////////////////////////////////////////
//iaddr,nnn -- cycle step (draft), step the remote target by a signel clock cycle. If ,nnn is present, cycle step nnn cycles. If addr is present, cycle step starting at that addr.
//I -- signal then cycle step (reserved)
//j -- reserved
//J -- reserved
//k -- kill request ???
//K -- reserved
//l -- reserved
//L -- reserved
///////////////////////////////////////////////////////////////////////////////////////////////////////
//maddr,len -- read memory (addr, len)
MemoryHandler memory_handler;
processor->defineResponse("m", &memory_handler); //$m0,1#fa $m0,8#01 $m0,7#00
//Maddr,len:XX... -- write memory
processor->defineResponse("M", &memory_handler);
///////////////////////////////////////////////////////////////////////////////////////////////////////
//n -- reserved
//N -- reserved
//o -- reserved
//O -- reserved
///////////////////////////////////////////////////////////////////////////////////////////////////////
//pn... -- read reg (reserved): hex encoded value in target byte order
//Pn...=r -- write reg n with value r containing two hex digits in target byte order
processor->defineResponse("p", "12345678"); //$p8#a8
processor->defineResponse("P", "OK"); //$P8#a8
///////////////////////////////////////////////////////////////////////////////////////////////////////
//qquery -- general query
QueryHandler query_handler;
processor->defineResponse("q", &query_handler); //$q...#xx
processor->defineResponse("Q", &query_handler); //$Q...#xx
//$qRcmd,xxxx....................xx#cc
RemoteCommandHandler remote_command_handler;
processor->defineResponse("qRcmd", &remote_command_handler);
///////////////////////////////////////////////////////////////////////////////////////////////////////
//r -- reset the entire system (deprecated)
//RXX -- remote restart. (extended mode); no reply
///////////////////////////////////////////////////////////////////////////////////////////////////////
//saddr -- step
StepHandler step_handler;
processor->defineResponse("s", &step_handler);
//Ssig;addr -- step with signal
//taddr:PP,MM -- search
//TXX -- thread alive
processor->defineResponse("T", "OK"); //OK or Enn $T1234#1e
///////////////////////////////////////////////////////////////////////////////////////////////////////
//u -- reserved
//U -- reserved
//v -- reserved
//$vCont?#49
//V -- reserved
//w -- reserved
//W -- reserved
///////////////////////////////////////////////////////////////////////////////////////////////////////
//x -- reserved
//Xaddr,len:XX... -- write mem with (XX) binary data (addr,len:XX), (escaped)
processor->defineResponse("X" , "OK");
///////////////////////////////////////////////////////////////////////////////////////////////////////
//y -- reserved
//Y -- reserved
///////////////////////////////////////////////////////////////////////////////////////////////////////
processor->defineResponse("z" , "OK");
processor->defineResponse("Z" , "OK");
//zt,addr,len -- remove break or watchpoint (draft)
//Zt,addr,len -- insert break or watchpoint (draft): 0: sw breakpoint, 1: hw breakpoint, 2: write watchpoint, 3: read watchpoint, 4: acess watchpoint
///////////////////////////////////////////////////////////////////////////////////////////////////////
//? -- current signal
processor->defineResponse("?" , "S%02x", SIGTRAP); //$?#3f
///////////////////////////////////////////////////////////////////////////////////////////////////////
processor->serve(name, params);
return 0;
}