-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqdbgserver.cpp
674 lines (604 loc) · 13.9 KB
/
sqdbgserver.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <cassert>
#include "sqrdbg.h"
#include "sqdbgserver.h"
#include <sqstdblob.h>
#ifndef _UNICODE
#define scstrcpy strcpy
#else
#define scstrcpy wcscpy
#endif
struct XMLEscape{
const SQChar c;
const SQChar *esc;
};
#define SQDBG_DEBUG_HOOK _SC("_sqdbg_debug_hook_")
#define SQDBG_ERROR_HANDLER _SC("_sqdbg_error_handler_")
XMLEscape g_escapes[]={
{_SC('<'), _SC("<")},
{'>',_SC(">")},
{_SC('&'),_SC("&")},
{_SC('\''),_SC("'")},
{_SC('\"'),_SC(""")},
{_SC('\n'),_SC(""n")},
{_SC('\r'),_SC(""r")},
{NULL,NULL}
};
const SQChar *IntToString(SQInteger n)
{
static SQChar temp[256];
sprintf(temp,"%d",n);
return temp;
}
const SQChar *PtrToString(void *p)
{
static SQChar temp[256];
sprintf(temp,"%p",p);
return temp;
}
SQInteger debug_hook(HSQUIRRELVM v);
SQInteger error_handler(HSQUIRRELVM v);
SQInteger beginelement(HSQUIRRELVM v)
{
SQUserPointer up;
const SQChar *name;
sq_getuserpointer(v,-1,&up);
SQDbgServer *self = (SQDbgServer*)up;
sq_getuserpointer(v,-1,&up);
sq_getstring(v,2,&name);
self->BeginElement(name);
return 0;
}
SQInteger endelement(HSQUIRRELVM v)
{
SQUserPointer up;
const SQChar *name;
sq_getuserpointer(v,-1,&up);
SQDbgServer *self = (SQDbgServer*)up;
sq_getuserpointer(v,-1,&up);
sq_getstring(v,2,&name);
self->EndElement(name);
return 0;
}
SQInteger attribute(HSQUIRRELVM v)
{
SQUserPointer up;
const SQChar *name,*value;
sq_getuserpointer(v,-1,&up);
SQDbgServer *self = (SQDbgServer*)up;
sq_getuserpointer(v,-1,&up);
sq_getstring(v,2,&name);
sq_getstring(v,3,&value);
self->Attribute(name,value);
return 0;
}
SQDbgServer::SQDbgServer(HSQUIRRELVM v)
{
_ready = false;
//_nestedcalls = 0;
_autoupdate = false;
_v = v;
_state = eDBG_Running;
_accept = ~0;
_endpoint = ~0;
//_maxrecursion = 10;
sq_resetobject(&_debugroot);
}
SQDbgServer::~SQDbgServer()
{
VMStateMap::iterator itr = _vmstate.begin();
while(itr != _vmstate.end()) {
VMState *vs = itr->second;
delete vs;
++itr;
}
_vmstate.clear();
sq_pushobject(_v,_debugroot);
sq_clear(_v,-1);
sq_release(_v,&_debugroot);
if(_accept != ~0)
shutdown(_accept, SHUT_RDWR);
if(_endpoint != ~0)
shutdown(_endpoint, SHUT_RDWR);
}
bool SQDbgServer::Init()
{
//creates an environment table for the debugger
sq_newtable(_v);
sq_getstackobj(_v,-1,&_debugroot);
sq_addref(_v,&_debugroot);
//creates a emptyslot to store the watches
sq_pushstring(_v,_SC("watches"),-1);
sq_pushnull(_v);
sq_newslot(_v,-3, SQFalse);
sq_pushstring(_v,_SC("beginelement"),-1);
sq_pushuserpointer(_v,this);
sq_newclosure(_v,beginelement,1);
sq_setparamscheck(_v,2,_SC(".s"));
sq_newslot(_v,-3, SQFalse);
sq_pushstring(_v,_SC("endelement"),-1);
sq_pushuserpointer(_v,this);
sq_newclosure(_v,endelement,1);
sq_setparamscheck(_v,2,_SC(".s"));
sq_newslot(_v,-3, SQFalse);
sq_pushstring(_v,_SC("attribute"),-1);
sq_pushuserpointer(_v,this);
sq_newclosure(_v,attribute,1);
sq_setparamscheck(_v,3,_SC(".ss"));
sq_newslot(_v,-3, SQFalse);
sq_pop(_v,1);
//stores debug hook and error handler in the registry
sq_pushregistrytable(_v);
sq_pushstring(_v,SQDBG_DEBUG_HOOK,-1);
sq_pushuserpointer(_v,this);
sq_newclosure(_v,debug_hook,1);
sq_newslot(_v,-3, SQFalse);
sq_pushstring(_v,SQDBG_ERROR_HANDLER,-1);
sq_pushuserpointer(_v,this);
sq_newclosure(_v,error_handler,1);
sq_newslot(_v,-3, SQFalse);
sq_pop(_v,1);
//sets the error handlers
SetErrorHandlers(_v);
return true;
}
bool SQDbgServer::ReadMsg()
{
return false;
}
void SQDbgServer::BusyWait()
{
while( !ReadMsg() )
usleep(0);
}
void SQDbgServer::SendChunk(const SQChar *chunk)
{
char *buf=NULL;
int buf_len=0;
buf_len=(int)strlen(chunk);
buf=(char *)chunk;
send(_endpoint,
(const void*)buf,
(int)strlen((const char *)buf),
0
);
}
void SQDbgServer::Terminated()
{
BeginElement(_SC("terminated"));
EndElement(_SC("terminated"));
::usleep(200);
}
VMState *SQDbgServer::GetVMState(HSQUIRRELVM v)
{
VMState *ret = NULL;
VMStateMap::iterator itr = _vmstate.find(v);
if(itr == _vmstate.end()) {
ret = new VMState();
_vmstate.insert(VMStateMap::value_type(v,ret));
}
else {
ret = itr->second;
}
return ret;
}
void SQDbgServer::Hook(HSQUIRRELVM v,SQInteger type,SQInteger line,const SQChar *src,const SQChar *func)
{
VMState *vs = GetVMState(v);
switch(_state){
case eDBG_Running:
if(type==_SC('l') && _breakpoints.size()) {
BreakPointSetItor itr = _breakpoints.find(BreakPoint(line,src));
if(itr != _breakpoints.end()) {
Break(v,line,src,_SC("breakpoint"));
BreakExecution();
}
}
break;
case eDBG_Suspended:
vs->_nestedcalls=0;
case eDBG_StepOver:
switch(type){
case _SC('l'):
if(vs->_nestedcalls==0) {
Break(v,line,src,_SC("step"));
BreakExecution();
}
break;
case _SC('c'):
vs->_nestedcalls++;
break;
case _SC('r'):
if(vs->_nestedcalls==0){
vs->_nestedcalls=0;
}else{
vs->_nestedcalls--;
}
break;
}
break;
case eDBG_StepInto:
switch(type){
case _SC('l'):
vs->_nestedcalls=0;
Break(v,line,src,_SC("step"));
BreakExecution();
break;
}
break;
case eDBG_StepReturn:
switch(type){
case _SC('l'):
break;
case _SC('c'):
vs->_nestedcalls++;
break;
case _SC('r'):
if(vs->_nestedcalls==0){
vs->_nestedcalls=0;
_state=eDBG_StepOver;
}else{
vs->_nestedcalls--;
}
break;
}
break;
case eDBG_Disabled:
break;
}
}
#define MSG_ID(x,y) ((y<<8)|x)
//ab Add Breakpoint
//rb Remove Breakpoint
//sp Suspend
void SQDbgServer::ParseMsg(const char *msg)
{
switch(*((unsigned short *)msg)){
case MSG_ID('a','b'): {
BreakPoint bp;
if(ParseBreakpoint(msg+3,bp)){
AddBreakpoint(bp);
scprintf(_SC("added bp %d %s\n"),bp._line,bp._src.c_str());
}
else
scprintf(_SC("error parsing add breakpoint"));
}
break;
case MSG_ID('r','b'): {
BreakPoint bp;
if(ParseBreakpoint(msg+3,bp)){
RemoveBreakpoint(bp);
scprintf(_SC("removed bp %d %s\n"),bp._line,bp._src.c_str());
}else
scprintf(_SC("error parsing remove breakpoint"));
}
break;
case MSG_ID('g','o'):
if(_state!=eDBG_Running){
_state=eDBG_Running;
BeginDocument();
BeginElement(_SC("resumed"));
EndElement(_SC("resumed"));
EndDocument();
// Send(_SC("<resumed/>\r\n"));
scprintf(_SC("go (execution resumed)\n"));
}
break;
case MSG_ID('s','p'):
if(_state!=eDBG_Suspended){
_state=eDBG_Suspended;
scprintf(_SC("suspend\n"));
}
break;
case MSG_ID('s','o'):
if(_state==eDBG_Suspended){
_state=eDBG_StepOver;
}
break;
case MSG_ID('s','i'):
if(_state==eDBG_Suspended){
_state=eDBG_StepInto;
scprintf(_SC("step into\n"));
}
break;
case MSG_ID('s','r'):
if(_state==eDBG_Suspended){
_state=eDBG_StepReturn;
scprintf(_SC("step return\n"));
}
break;
case MSG_ID('d','i'):
if(_state!=eDBG_Disabled){
_state=eDBG_Disabled;
scprintf(_SC("disabled\n"));
}
break;
case MSG_ID('a','w'): {
Watch w;
if(ParseWatch(msg+3,w))
{
AddWatch(w);
scprintf(_SC("added watch %d %s\n"),w._id,w._exp.c_str());
/*if(_state == eDBG_Suspended) {
Break(_line,_src.c_str(),_break_type.c_str());
}*/
}
else
scprintf(_SC("error parsing add watch"));
}
break;
case MSG_ID('r','w'): {
SQInteger id;
if(ParseRemoveWatch(msg+3,id))
{
RemoveWatch(id);
scprintf(_SC("added watch %d\n"),id);
}
else
scprintf(_SC("error parsing remove watch"));
}
break;
case MSG_ID('t','r'):
scprintf(_SC("terminate from user\n"));
break;
case MSG_ID('r','d'):
scprintf(_SC("ready\n"));
_ready=true;
break;
default:
scprintf(_SC("unknown packet"));
}
}
bool SQDbgServer::ParseBreakpoint(const char *msg,BreakPoint &out)
{
static char stemp[MAX_BP_PATH];
static SQChar desttemp[MAX_BP_PATH];
char *ep=NULL;
out._line=strtoul(msg,&ep,16);
if(ep==msg || (*ep)!=':')return false;
char *dest=stemp;
ep++;
while((*ep)!='\n' && (*ep)!='\0')
{
*dest=tolower(*ep);
*dest++;*ep++;
}
*dest='\0';
*dest++;
*dest='\0';
#ifdef _UNICODE
int len=(int)strlen(stemp);
SQChar *p = desttemp;
size_t destlen = mbstowcs(p,stemp,len);
p[destlen]=_SC('\0');
out._src=p;
#else
out._src=stemp;
#endif
return true;
}
bool SQDbgServer::ParseWatch(const char *msg,Watch &out)
{
char *ep=NULL;
out._id=strtoul(msg,&ep,16);
if(ep==msg || (*ep)!=':')return false;
//char *dest=out._src;
ep++;
while((*ep)!='\n' && (*ep)!='\0')
{
out._exp.append(1,*ep);
*ep++;
}
return true;
}
bool SQDbgServer::ParseRemoveWatch(const char *msg,SQInteger &id)
{
char *ep=NULL;
id=strtoul(msg,&ep,16);
if(ep==msg)return false;
return true;
}
void SQDbgServer::BreakExecution()
{
_state=eDBG_Suspended;
while(_state==eDBG_Suspended){
if(SQ_FAILED(sq_rdbg_update(this)))
exit(0);
usleep(10);
}
}
//COMMANDS
void SQDbgServer::AddBreakpoint(BreakPoint &bp)
{
_breakpoints.insert(bp);
BeginDocument();
BeginElement(_SC("addbreakpoint"));
Attribute(_SC("line"),IntToString(bp._line));
Attribute(_SC("src"),bp._src.c_str());
EndElement(_SC("addbreakpoint"));
EndDocument();
}
void SQDbgServer::AddWatch(Watch &w)
{
_watches.insert(w);
}
void SQDbgServer::RemoveWatch(SQInteger id)
{
WatchSetItor itor=_watches.find(Watch(id,_SC("")));
if(itor==_watches.end()){
BeginDocument();
BeginElement(_SC("error"));
Attribute(_SC("desc"),_SC("the watch does not exists"));
EndElement(_SC("error"));
EndDocument();
}
else{
_watches.erase(itor);
scprintf(_SC("removed watch %d\n"),id);
}
}
void SQDbgServer::RemoveBreakpoint(BreakPoint &bp)
{
BreakPointSetItor itor=_breakpoints.find(bp);
if(itor==_breakpoints.end()){
BeginDocument();
BeginElement(_SC("break"));
Attribute(_SC("desc"),_SC("the breakpoint doesn't exists"));
EndElement(_SC("break"));
EndDocument();
}
else{
BeginDocument();
BeginElement(_SC("removebreakpoint"));
Attribute(_SC("line"),IntToString(bp._line));
Attribute(_SC("src"),bp._src.c_str());
EndElement(_SC("removebreakpoint"));
EndDocument();
_breakpoints.erase(itor);
}
}
void SQDbgServer::Break(HSQUIRRELVM v,SQInteger line,const SQChar *src,const SQChar *type,const SQChar *error)
{
_line = line;
_src = src;
_break_type = src;
if(!error){
BeginDocument();
BeginElement(_SC("break"));
Attribute(_SC("thread"),PtrToString(v));
Attribute(_SC("line"),IntToString(line));
Attribute(_SC("src"),src);
Attribute(_SC("type"),type);
SerializeState(v);
EndElement(_SC("break"));
EndDocument();
}else{
BeginDocument();
BeginElement(_SC("break"));
Attribute(_SC("thread"),PtrToString(v));
Attribute(_SC("line"),IntToString(line));
Attribute(_SC("src"),src);
Attribute(_SC("type"),type);
Attribute(_SC("error"),error);
SerializeState(v);
EndElement(_SC("break"));
EndDocument();
}
}
void SQDbgServer::SerializeState(HSQUIRRELVM v)
{
sq_pushnull(v);
sq_setdebughook(v);
sq_pushnull(v);
sq_seterrorhandler(v);
sq_pushobject(v,_serializefunc);
sq_pushobject(v,_debugroot);
sq_pushstring(v,_SC("watches"),-1);
sq_newtable(v);
for(WatchSetItor i=_watches.begin(); i!=_watches.end(); ++i)
{
sq_pushinteger(v,i->_id);
sq_pushstring(v,i->_exp.c_str(),(SQInteger)i->_exp.length());
sq_createslot(v,-3);
}
sq_rawset(v,-3);
if(SQ_SUCCEEDED(sq_call(v,1,SQFalse,SQFalse))){
// SQUserPointer sz;
// sqstd_getblob(v,-1,(SQUserPointer*)&sz);
// SendChunk(sz);
}
sq_pop(v,2);
SetErrorHandlers(v);
}
void SQDbgServer::SetErrorHandlers(HSQUIRRELVM v)
{
sq_pushregistrytable(v);
sq_pushstring(v,SQDBG_DEBUG_HOOK,-1);
sq_rawget(v,-2);
sq_setdebughook(v);
sq_pushstring(v,SQDBG_ERROR_HANDLER,-1);
sq_rawget(v,-2);
sq_seterrorhandler(v);
sq_pop(v,1);
}
void SQDbgServer::BeginDocument()
{
_xmlcurrentement = -1;
SendChunk(_SC("<?xml version=\"1.0\" encoding=\"utf-8\"?>"));
}
void SQDbgServer::BeginElement(const SQChar *name)
{
_xmlcurrentement++;
XMLElementState *self = &xmlstate[_xmlcurrentement];
scstrcpy(self->name,name);
self->haschildren = false;
if(_xmlcurrentement > 0) {
XMLElementState *parent = &xmlstate[_xmlcurrentement-1];
if(!parent->haschildren) {
SendChunk(_SC(">")); // closes the parent tag
parent->haschildren = true;
}
}
char elem[10+scstrlen(name)];
sprintf(elem, "<%s" ,name);
SendChunk(elem);
}
void SQDbgServer::Attribute(const SQChar *name,const SQChar *value)
{
XMLElementState *self = &xmlstate[_xmlcurrentement];
assert(!self->haschildren); //cannot have attributes if already has children
const SQChar *escval = escape_xml(value);
char attr[10+scstrlen(name)+scstrlen(escval)];
sprintf(attr, " %s=\"%s\"" , name, escval);
SendChunk(attr);
}
void SQDbgServer::EndElement(const SQChar *name)
{
XMLElementState *self = &xmlstate[_xmlcurrentement];
assert(scstrcmp(self->name,name) == 0);
if(self->haschildren) {
size_t size = 10 + scstrlen(name);
char elem[size];
sprintf(elem, "</%s>" , name);
SendChunk(elem);
}
else {
SendChunk(_SC("/>"));
}
_xmlcurrentement--;
}
void SQDbgServer::EndDocument()
{
SendChunk(_SC("\r\n"));
}
//this can be done much better/faster(do we need that?)
const SQChar *SQDbgServer::escape_xml(const SQChar *s)
{
SQChar *temp=sq_getscratchpad(_v,((SQInteger)scstrlen(s)*6) + sizeof(SQChar));
SQChar *dest=temp;
while(*s!=_SC('\0')){
const SQChar *escape = NULL;
switch(*s) {
case _SC('<'): escape = _SC("<"); break;
case _SC('>'): escape = _SC(">"); break;
case _SC('&'): escape = _SC("&"); break;
case _SC('\''): escape = _SC("'"); break;
case _SC('\"'): escape = _SC("""); break;
case _SC('\n'): escape = _SC("\\n"); break;
case _SC('\r'): escape = _SC("\\r"); break;
case _SC('\x1B'): escape = _SC("\\x1B"); break;
}
if(escape) {
scstrcpy(dest,escape);
dest += scstrlen(escape);
}
else {
*dest=*s;*dest++;
}
*s++;
}
*dest=_SC('\0');
return temp;
}