-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebugThread.cpp
More file actions
623 lines (554 loc) · 14.7 KB
/
DebugThread.cpp
File metadata and controls
623 lines (554 loc) · 14.7 KB
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
/*
* The MIT License - see LICENSE file for details
*
* based on RemDebug 1.0 Beta
* Copyright Kepler Project 2005 (http://www.keplerproject.org/remdebug)
*/
#include <iostream>
#include <boost/regex.hpp>
#include <boost/format.hpp>
#include <wx/wx.h>
#include "DebugThread.h"
#include "StringUtils.h"
DebugThread::DebugThread(Poco::Net::StreamSocket client) :
panel(NULL), client(client)
{
// without timeout formated socket reading hangs
client.setReceiveTimeout(Poco::Timespan(50, 0));
client.setReceiveBufferSize(1024);
client.setSendTimeout(Poco::Timespan(50, 0));
client.setSendBufferSize(1024);
}
void DebugThread::assignDebugPanel(DebugPanel *panel)
{
assert(panel != NULL);
this->panel = panel;
}
void DebugThread::notifyFileAndLine(const std::string &file, unsigned int line)
{
assert(panel != NULL);
wxCommandEvent event(wxEVT_MY_EVENT_NOTIFY_FILE_AND_LINE);
event.SetString(wxString(file.c_str(), wxConvUTF8));
event.SetInt(line);
wxPostEvent(panel, event);
}
void DebugThread::print(const std::string &output)
{
assert(panel != NULL);
std::cout << output << std::endl;
wxCommandEvent event(wxEVT_MY_EVENT_PRINT_LINE);
event.SetString(wxString(output.c_str(), wxConvUTF8));
wxPostEvent(panel, event);
}
void DebugThread::stop()
{
running = false;
}
void DebugThread::send(const std::string &s)
{
// std::cout << "DEBUG SEND: '" << s << "'" << std::endl;
client << s << std::flush;
}
std::string DebugThread::receive()
{
std::string buffer;
getline(client, buffer);
// std::cout << "DEBUG RECV: '" << buffer << "'" << std::endl;
return buffer;
}
std::vector<unsigned int> DebugThread::listBreakpointOfFile(
const std::string &file)
{
Poco::Mutex::ScopedLock lock(mutex);
// TODO basedir compatible?
std::vector<unsigned int> lineNumbers;
for (std::map<std::string, bool>::iterator it = breakpoints[file].begin(); it
!= breakpoints[file].end(); ++it)
{
if (it->second)
{
lineNumbers.push_back(StringUtils::toUInt(it->first));
}
}
return lineNumbers;
}
bool DebugThread::hasBreakpointAt(const std::string &file, unsigned int line)
{
Poco::Mutex::ScopedLock lock(mutex);
// TODO basedir compatible?
return breakpoints[file][(boost::format("%1%") % line).str()];
}
std::string DebugThread::receive(unsigned int len)
{
std::string buffer;
const size_t BUFFER_SIZE = 512;
char chunk[BUFFER_SIZE];
while (buffer.size() < len)
{
int remaining = std::min(static_cast<size_t>(len - buffer.size()), BUFFER_SIZE);
client.read((char *) &chunk, remaining);
buffer += std::string((const char *) &chunk, (size_t) remaining);
}
// std::cout << "DEBUG RECV(" << len << "): '" << buffer << "'" << std::endl;
return buffer;
}
void DebugThread::postline(const std::string &line)
{
Poco::Mutex::ScopedLock lock(mutex);
lines.push(line);
}
/**
* blocks until someone posts a line
*/
std::string DebugThread::readline()
{
while (running)
{
{
Poco::Mutex::ScopedLock lock(mutex);
if (lines.size() > 0)
{
std::string line = lines.front();
lines.pop();
return line;
}
}
Poco::Thread::sleep(10);
}
return std::string();
}
void DebugThread::run()
{
try
{
running = true;
static const boost::regex pattern_spaces("^([[:blank:]]*)$");
static const boost::regex pattern_number("^([0-9]+)");
static const boost::regex pattern_line202(
"^202 Paused\\s+(.+)[[:blank:]]+([0-9]+)$");
static const boost::regex pattern_line203(
"^203 Paused\\s+(.+)[[:blank:]]+([0-9]+)[[:blank:]]+([0-9]+)$");
static const boost::regex pattern_line401(
"^401 Error in Execution ([0-9]+)$");
static const boost::regex pattern_command("^([a-z]+)");
static const boost::regex pattern_file_line(
"^[a-z]+[[:blank:]]+(.+)[[:blank:]]+([0-9]+)$");
static const boost::regex pattern_param("^[a-z]+[[:blank:]]+(.+)$");
static const boost::regex pattern_200ok("^200 OK ([0-9]+)$");
static const boost::regex pattern_exec_result(
"^([0-9]+)[a-zA-Z ]+([0-9]+)$");
// wait for panel to connect
while (panel == NULL)
{
Poco::Thread::sleep(10);
}
send("STEP\n");
receive();
std::string breakpoint = receive();
boost::match_results<std::string::const_iterator> what;
if (boost::regex_search(breakpoint, what, pattern_line202))
{
std::string file = what[1].str();
std::string line = what[2].str();
print("Paused at file " + file + " line " + line);
print("Type 'help' for commands");
notifyFileAndLine(file, StringUtils::toUInt(line));
}
else if (boost::regex_search(breakpoint, what, pattern_line401))
{
unsigned int size = StringUtils::toUInt(what[1].str());
print("Error in remote application: ");
print(receive(size));
running = false;
}
std::string basedir = "";
while (running)
{
// std::cout << "please enter command: ";
std::string line = readline();
if (boost::regex_search(line, what, pattern_command))
{
std::string command = what[1].str();
if ((command == "run") || (command == "step") || (command
== "over"))
{
send(StringUtils::toUpper(command) + "\n");
receive();
std::string breakpoint = receive();
if (breakpoint.size() == 0)
{
print("Program finished");
running = false;
}
if (boost::regex_search(breakpoint, what, pattern_number))
{
std::string status = what[1].str();
if (status == "202")
{
if (boost::regex_search(breakpoint, what,
pattern_line202))
{
std::string file = what[1].str();
std::string line = what[2].str();
print(
"Paused at file " + file + " line "
+ line);
notifyFileAndLine(file,
StringUtils::toUInt(line));
}
}
else if (status == "203")
{
if (boost::regex_search(breakpoint, what,
pattern_line203))
{
std::string file = what[1].str();
std::string line = what[2].str();
std::string watch_idx = what[3].str();
print(
"Paused at file " + file + " line "
+ line + " (watch expression "
+ watch_idx + ": ["
+ watches[watch_idx] + "])");
notifyFileAndLine(file,
StringUtils::toUInt(line));
}
}
else if (status == "401")
{
if (boost::regex_search(breakpoint, what,
pattern_line401))
{
unsigned int size = StringUtils::toUInt(
what[1].str());
print("Error in remote application: ");
print(receive(size));
running = false;
}
}
else
{
print("Unknown error");
running = false;
}
}
}
else if (command == "exit")
{
client.close();
running = false;
}
else if (command == "setb")
{
if (boost::regex_search(line, what, pattern_file_line))
{
std::string filename = basedir + what[1].str();
std::string line = what[2].str();
send("SETB " + filename + " " + line + "\n");
if (receive() == "200 OK")
{
Poco::Mutex::ScopedLock lock(mutex);
breakpoints[filename][line] = true;
}
else
{
print("Error: breakpoint not inserted");
}
}
else
{
print("Invalid command");
}
}
else if (command == "setw")
{
if (boost::regex_search(line, what, pattern_param))
{
std::string exp = what[1].str();
send("SETW " + exp + "\n");
std::string answer = receive();
if (boost::regex_search(answer, what, pattern_200ok))
{
std::string watch_idx = what[1].str();
watches[watch_idx] = exp;
print("Inserted watch exp no. " + watch_idx);
}
else
{
print("Error: Watch expression not inserted");
}
}
else
{
print("Invalid command");
}
}
else if (command == "delb")
{
if (boost::regex_search(line, what, pattern_file_line))
{
std::string filename = basedir + what[1].str();
std::string line = what[2].str();
send("DELB " + filename + " " + line + "\n");
if (receive() == "200 OK")
{
Poco::Mutex::ScopedLock lock(mutex);
breakpoints[filename].erase(line);
}
else
{
print("Error: breakpoint not inserted");
}
}
else
{
print("Invalid command");
}
}
else if (command == "delallb")
{
Poco::Mutex::ScopedLock lock(mutex);
for (std::map<std::string, std::map<std::string, bool> >::iterator
it_file = breakpoints.begin(); it_file
!= breakpoints.end(); ++it_file)
{
for (std::map<std::string, bool>::iterator it_line =
it_file->second.begin(); it_line
!= it_file->second.end(); ++it_line)
{
send(
"DELB " + it_file->first + " "
+ it_line->first + "\n");
if (receive() == "200 OK")
{
breakpoints[it_file->first].erase(it_line++);
}
else
{
print("Error: breakpoint not inserted");
}
}
if (it_file->second.size() == 0)
{
breakpoints.erase(it_file++);
}
}
}
else if (command == "delw")
{
if (boost::regex_search(line, what, pattern_param))
{
std::string index = what[1].str();
send("DELW " + index + "\n");
if (receive() == "200 OK")
{
watches.erase(index);
}
else
{
print("Error: watch expression not removed");
}
}
else
{
print("Invalid command");
}
}
else if (command == "delallw")
{
for (std::map<std::string, std::string>::iterator it =
watches.begin(); it != watches.end(); ++it)
{
send("DELW " + it->first + "\n");
if (receive() == "200 OK")
{
watches.erase(++it);
}
else
{
print(
"Error: watch expression at index "
+ it->first + " [" + it->second
+ "] not removed");
}
}
}
else if (command == "eval")
{
if (boost::regex_search(line, what, pattern_param))
{
std::string exp = what[1].str();
send("EXEC return (" + exp + ")\n");
std::string line = receive();
if (boost::regex_search(line, what, pattern_exec_result))
{
std::string status = what[1].str();
std::string len = what[2].str();
if (status == "200")
{
print( receive(StringUtils::toUInt( len)));
}
else if (status == "401")
{
print("Error in expression:");
print( receive(StringUtils::toUInt( len)));
}
}
else
{
print("Unknown error");
}
}
else
{
print("Invalid command");
}
}
else if (command == "exec")
{
if (boost::regex_search(line, what, pattern_param))
{
std::string exp = what[1].str();
send("EXEC " + exp + "\n");
std::string line = receive();
if (boost::regex_search(line, what, pattern_exec_result))
{
std::string status = what[1].str();
std::string len = what[2].str();
if (status == "200")
{
print( receive(StringUtils::toUInt( len)));
}
else if (status == "401")
{
print("Error in expression:");
print( receive(StringUtils::toUInt( len)));
}
}
else
{
print("Unknown error");
}
}
else
{
print("Invalid command");
}
}
else if (command == "file")
{
if (boost::regex_search(line, what, pattern_param))
{
std::string file = basedir + what[1].str();
send("FILE " + file + "\n");
std::string line = receive();
if (boost::regex_search(line, what, pattern_exec_result))
{
std::string status = what[1].str();
std::string len = what[2].str();
if (status == "200")
{
std::string src = receive(
StringUtils::toUInt(len));
print(src);
}
else if (status == "400")
{
print("Could not find file");
}
}
else
{
print("Unknown error");
}
}
else
{
print("Invalid command");
}
}
else if (command == "listb")
{
Poco::Mutex::ScopedLock lock(mutex);
for (std::map<std::string, std::map<std::string, bool> >::iterator
it_file = breakpoints.begin(); it_file
!= breakpoints.end(); ++it_file)
{
for (std::map<std::string, bool>::iterator it_line =
it_file->second.begin(); it_line
!= it_file->second.end(); ++it_line)
{
print(it_file->first + ":" + it_line->first);
}
}
}
else if (command == "listw")
{
for (std::map<std::string, std::string>::iterator it =
watches.begin(); it != watches.end(); ++it)
{
print("Watch exp. " + it->first + ": " + it->second);
}
}
else if (command == "basedir")
{
if (boost::regex_search(line, what, pattern_param))
{
std::string dir = what[1].str();
basedir = dir;
print("New base directory is: '" + basedir + "'");
}
else
{
print("Current basedir is: '" + basedir + "'");
}
}
else if (command == "help")
{
print("setb <file> <line> -- sets a breakpoint");
print("delb <file> <line> -- removes a breakpoint");
print("delallb -- removes all breakpoints");
print(
"setw <exp> -- adds a new watch expression");
print(
"delw <index> -- removes the watch expression at index");
print(
"delallw -- removes all watch expressions");
print("run -- run until next breakpoint");
print(
"step -- run until next line, stepping into function calls");
print(
"over -- run until next line, stepping over function calls");
print("listb -- lists breakpoints");
print("listw -- lists watch expressions");
print(
"eval <exp> -- evaluates expression on the current context and returns its value");
print(
"exec <stmt> -- executes statement on the current context");
print(
"basedir [<path>] -- sets the base path of the remote application, or shows the current one");
print("file <file> -- receives file content");
print("exit -- exits debugger");
}
// breakpoint update needed?
if (command == "delb" || command == "setb" || command
== "delallb")
{
wxCommandEvent event(
wxEVT_MY_EVENT_NOTIFY_BREAKPOINT_UPDATE);
wxPostEvent(panel, event);
}
}
else
{
if (!boost::regex_search(line, what, pattern_spaces))
{
print("Invalid command");
}
}
}
} catch (std::exception &e)
{
std::cout << "ERROR: " << e.what() << std::endl;
}
}