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
|
/**
* @file
* @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
* @copyright Simplified BSD
*
* @cond
* This program is free software: you can redistribute it and/or modify
* it under the terms of the FreeBSD license as published by the FreeBSD
* project.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the FreeBSD license along with this
* program. If not, see <http://www.opensource.org/licenses/bsd-license>.
* @endcond
*/
#include "uscxml/debug/DebugSession.h"
#include "uscxml/debug/Debugger.h"
#include "uscxml/util/Predicates.h"
#include "uscxml/interpreter/Logging.h"
namespace uscxml {
void DebugSession::checkBreakpoints(const std::list<Breakpoint> qualifiedBreakpoints) {
std::list<Breakpoint>::const_iterator qualifiedBreakpointIter = qualifiedBreakpoints.begin();
if (!_breakpointsEnabled)
return;
while(qualifiedBreakpointIter != qualifiedBreakpoints.end()) {
const Breakpoint& qualifiedBreakpoint = *qualifiedBreakpointIter++;
// check if one of the user-supplied breakpoints match
bool userBreakpointMatched = false;
Data replyData;
if (_skipTo) {
if (_skipTo.matches(_interpreter, qualifiedBreakpoint)) {
replyData.compound["breakpoint"] = _skipTo.toData();
replyData.compound["qualified"] = qualifiedBreakpoint.toData();
breakExecution(replyData);
_skipTo = Breakpoint();
}
continue;
}
std::set<Breakpoint>::const_iterator breakpointIter = _breakPoints.begin();
while(breakpointIter != _breakPoints.end()) {
const Breakpoint& breakpoint = *breakpointIter++;
if (!breakpoint.enabled)
continue;
if (breakpoint.matches(_interpreter, qualifiedBreakpoint)) {
// do we have a condition?
replyData.compound["breakpoint"] = breakpoint.toData();
replyData.compound["qualified"] = qualifiedBreakpoint.toData();
userBreakpointMatched = true;
breakExecution(replyData);
}
}
if (_isStepping && !userBreakpointMatched) {
replyData.compound["qualified"] = qualifiedBreakpoint.toData();
breakExecution(replyData);
}
}
}
void DebugSession::breakExecution(Data replyData) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
std::list<XERCESC_NS::DOMElement*> configuration = _interpreter.getConfiguration();
for (auto state : configuration) {
if (HAS_ATTR(state, kXMLCharId)) {
replyData.compound["activeStates"].array.push_back(Data(ATTR(state, kXMLCharId), Data::VERBATIM));
if (isAtomic(state)) {
replyData.compound["basicStates"].array.push_back(Data(ATTR(state, kXMLCharId), Data::VERBATIM));
}
}
}
replyData.compound["replyType"] = Data("breakpoint", Data::VERBATIM);
_debugger->pushData(shared_from_this(), replyData);
// wait for resume from the client
_resumeCond.wait(_mutex);
}
Data DebugSession::debugPrepare(const Data& data) {
Data replyData;
if (!data.hasKey("xml") && !data.hasKey("url")) {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
replyData.compound["reason"] = Data("No XML or URL given", Data::VERBATIM);
return replyData;
}
debugStop(data);
_isAttached = false;
try {
if (data.hasKey("xml")) {
_interpreter = Interpreter::fromXML(data.at("xml").atom, (data.hasKey("url") ? data.at("url").atom : ""));
} else if (data.hasKey("url")) {
_interpreter = Interpreter::fromURL(data.at("url").atom);
} else {
_interpreter = Interpreter();
}
} catch(ErrorEvent e) {
log(USCXML_ERROR, e);
} catch(...) {}
if (_interpreter) {
// register ourself as a monitor
_interpreter.addMonitor(_debugger);
_debugger->attachSession(_interpreter.getImpl().get(), shared_from_this());
replyData.compound["status"] = Data("success", Data::VERBATIM);
} else {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
}
return replyData;
}
Data DebugSession::debugAttach(const Data& data) {
Data replyData;
_isAttached = true;
if (!data.hasKey("attach")) {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
replyData.compound["reason"] = Data("No id to attach to given", Data::VERBATIM);
return replyData;
}
std::string interpreterId = data.at("attach").atom;
bool interpreterFound = false;
// find interpreter for sessionid
std::map<std::string, std::weak_ptr<InterpreterImpl> > instances = InterpreterImpl::getInstances();
for (auto weakInstance : instances) {
std::shared_ptr<InterpreterImpl> instance = weakInstance.second.lock();
if (instance && instance->getSessionId() == interpreterId) {
_interpreter = instance;
_debugger->attachSession(_interpreter.getImpl().get(), shared_from_this());
interpreterFound = true;
break;
}
}
if (!interpreterFound) {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
replyData.compound["reason"] = Data("No interpreter with given id found", Data::VERBATIM);
} else {
replyData.compound["xml"].node = _interpreter.getImpl()->getDocument();
replyData.compound["status"] = Data("success", Data::VERBATIM);
}
return replyData;
}
Data DebugSession::debugDetach(const Data& data) {
Data replyData;
_isAttached = false;
_debugger->detachSession(_interpreter.getImpl().get());
replyData.compound["status"] = Data("success", Data::VERBATIM);
return replyData;
}
Data DebugSession::debugStart(const Data& data) {
Data replyData;
if (_isAttached) {
replyData.compound["reason"] = Data("Already started when attached", Data::VERBATIM);
replyData.compound["status"] = Data("failure", Data::VERBATIM);
} else if (!_interpreter) {
replyData.compound["reason"] = Data("No interpreter attached or loaded", Data::VERBATIM);
replyData.compound["status"] = Data("failure", Data::VERBATIM);
} else {
_isRunning = true;
_interpreterThread = new std::thread(DebugSession::run, this);
replyData.compound["status"] = Data("success", Data::VERBATIM);
}
return replyData;
}
void DebugSession::run(void* instance) {
DebugSession* INSTANCE = (DebugSession*)instance;
#ifdef APPLE
std::string threadName;
threadName += "uscxml::";
threadName += (INSTANCE->_interpreter.getImpl()->_name.size() > 0 ? INSTANCE->_interpreter.getImpl()->_name : "anon");
threadName += ".debug";
pthread_setname_np(threadName.c_str());
#endif
InterpreterState state = USCXML_UNDEF;
while(state != USCXML_FINISHED && INSTANCE->_isRunning) {
state = INSTANCE->_interpreter.step();
// if (!INSTANCE->_isStarted) {
// // we have been cancelled
// INSTANCE->_isActive = false;
// return;
// }
}
LOG(INSTANCE->_interpreter.getLogger(), USCXML_DEBUG) << "done";
}
Data DebugSession::debugStop(const Data& data) {
Data replyData;
if (_interpreter) {
// detach from old intepreter
_debugger->detachSession(_interpreter.getImpl().get());
}
if (_isRunning && _interpreterThread != NULL) {
_isRunning = false;
// unblock
_resumeCond.notify_all();
_interpreterThread->join();
delete(_interpreterThread);
}
_skipTo = Breakpoint();
replyData.compound["status"] = Data("success", Data::VERBATIM);
// calls destructor
_interpreter = Interpreter();
return replyData;
}
Data DebugSession::debugStep(const Data& data) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
stepping(true);
_resumeCond.notify_one();
Data replyData;
if (_interpreter) {
// register ourself as a monitor
if (!_isRunning) {
_isRunning = true;
_interpreterThread = new std::thread(DebugSession::run, this);
}
replyData.compound["status"] = Data("success", Data::VERBATIM);
} else {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
}
return replyData;
}
Data DebugSession::debugResume(const Data& data) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
stepping(false);
Data replyData;
replyData.compound["status"] = Data("success", Data::VERBATIM);
_resumeCond.notify_one();
return replyData;
}
Data DebugSession::debugPause(const Data& data) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_skipTo = Breakpoint();
stepping(true);
Data replyData;
replyData.compound["status"] = Data("success", Data::VERBATIM);
return replyData;
}
Data DebugSession::skipToBreakPoint(const Data& data) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_skipTo = Breakpoint(data);
Data replyData;
if (_interpreter) {
// register ourself as a monitor
if (!_isRunning) {
_isRunning = true;
_interpreterThread = new std::thread(DebugSession::run, this);
}
replyData.compound["status"] = Data("success", Data::VERBATIM);
} else {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
}
_resumeCond.notify_one();
return replyData;
}
Data DebugSession::addBreakPoint(const Data& data) {
Breakpoint breakpoint(data);
Data replyData;
if (_breakPoints.find(breakpoint) == _breakPoints.end()) {
_breakPoints.insert(breakpoint);
replyData.compound["status"] = Data("success", Data::VERBATIM);
} else {
replyData.compound["reason"] = Data("Breakpoint already exists", Data::VERBATIM);
replyData.compound["status"] = Data("failure", Data::VERBATIM);
}
return replyData;
}
Data DebugSession::removeBreakPoint(const Data& data) {
Breakpoint breakpoint(data);
Data replyData;
if (_breakPoints.find(breakpoint) != _breakPoints.end()) {
_breakPoints.erase(breakpoint);
replyData.compound["status"] = Data("success", Data::VERBATIM);
} else {
replyData.compound["reason"] = Data("No such breakpoint", Data::VERBATIM);
replyData.compound["status"] = Data("failure", Data::VERBATIM);
}
return replyData;
}
Data DebugSession::enableBreakPoint(const Data& data) {
Breakpoint breakpoint(data);
Data replyData;
if (_breakPoints.find(breakpoint) != _breakPoints.end()) {
_breakPoints.find(breakpoint)->enabled = true;
replyData.compound["status"] = Data("success", Data::VERBATIM);
} else {
replyData.compound["reason"] = Data("No such breakpoint", Data::VERBATIM);
replyData.compound["status"] = Data("failure", Data::VERBATIM);
}
return replyData;
}
Data DebugSession::disableBreakPoint(const Data& data) {
Breakpoint breakpoint(data);
Data replyData;
if (_breakPoints.find(breakpoint) != _breakPoints.end()) {
_breakPoints.find(breakpoint)->enabled = false;
replyData.compound["status"] = Data("success", Data::VERBATIM);
} else {
replyData.compound["reason"] = Data("No such breakpoint", Data::VERBATIM);
replyData.compound["status"] = Data("failure", Data::VERBATIM);
}
return replyData;
}
Data DebugSession::enableAllBreakPoints() {
Data replyData;
_breakpointsEnabled = true;
replyData.compound["status"] = Data("success", Data::VERBATIM);
return replyData;
}
Data DebugSession::disableAllBreakPoints() {
Data replyData;
_breakpointsEnabled = false;
replyData.compound["status"] = Data("success", Data::VERBATIM);
return replyData;
}
Data DebugSession::getIssues() {
Data replyData;
std::list<InterpreterIssue> issues = _interpreter.validate();
replyData.compound["status"] = Data("success", Data::VERBATIM);
for (auto issue : issues) {
Data issueData;
issueData.compound["message"] = Data(issue.message, Data::VERBATIM);
issueData.compound["xPath"] = Data(issue.xPath, Data::VERBATIM);
issueData.compound["specRef"] = Data(issue.specRef, Data::VERBATIM);
switch (issue.severity) {
case InterpreterIssue::USCXML_ISSUE_FATAL:
issueData.compound["severity"] = Data("FATAL", Data::VERBATIM);
break;
case InterpreterIssue::USCXML_ISSUE_WARNING:
issueData.compound["severity"] = Data("WARN", Data::VERBATIM);
break;
case InterpreterIssue::USCXML_ISSUE_INFO:
issueData.compound["severity"] = Data("INFO", Data::VERBATIM);
break;
default:
break;
}
replyData.compound["issues"].array.push_back(issueData);
}
return replyData;
}
Data DebugSession::debugEval(const Data& data) {
Data replyData;
if (!data.hasKey("expression")) {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
replyData.compound["reason"] = Data("No expression given", Data::VERBATIM);
return replyData;
}
std::string expr = data.at("expression").atom;
if (!_interpreter) {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
replyData.compound["reason"] = Data("No interpreter running", Data::VERBATIM);
} else if (!_interpreter.getImpl()->_dataModel) {
replyData.compound["status"] = Data("failure", Data::VERBATIM);
replyData.compound["reason"] = Data("No datamodel available", Data::VERBATIM);
} else {
try {
replyData.compound["eval"] = _interpreter.getImpl()->getAsData(expr);
} catch (Event e) {
replyData.compound["eval"] = e.data;
replyData.compound["eval"].compound["error"] = Data(e.name, Data::VERBATIM);
}
replyData.compound["status"] = Data("success", Data::VERBATIM);
}
return replyData;
}
std::shared_ptr<LoggerImpl> DebugSession::create() {
return shared_from_this();
}
void DebugSession::log(LogSeverity severity, const Event& event) {
Data d;
d.compound["data"] = event.data;
d.compound["name"] = Data(event.name);
d.compound["origin"] = Data(event.origin);
d.compound["origintype"] = Data(event.origintype);
switch (event.eventType) {
case Event::Type::INTERNAL:
d.compound["eventType"] = Data("INTERNAL");
break;
case Event::Type::EXTERNAL:
d.compound["eventType"] = Data("EXTERNAL");
break;
case Event::Type::PLATFORM:
d.compound["eventType"] = Data("PLATFORM");
break;
default:
break;
}
if (!event.hideSendId)
d.compound["sendid"] = Data(event.sendid);
if (event.invokeid.size() > 0)
d.compound["invokeid"] = Data(event.invokeid);
// handle params
Data& params = d.compound["params"];
bool convertedToArray = false;
for (auto param : event.params) {
if (params.compound.find(param.first) != d.compound.end()) {
// no such key, add as literal data
d.compound[param.first] = param.second;
} else if (params.compound[param.first].array.size() > 0 && convertedToArray) {
// key is already an array
params.compound[param.first].array.push_back(param.second);
} else {
// key already given as literal data, move to array
Data& existingParam = params.compound[param.first];
params.compound[param.first].array.push_back(existingParam);
params.compound[param.first].array.push_back(param.second);
params.compound[param.first].compound.clear();
convertedToArray = true;
}
}
// handle namelist
Data& namelist = d.compound["namelist"];
for (auto name : event.namelist) {
namelist.compound[name.first] = name.second;
}
_debugger->pushData(shared_from_this(), d);
}
void DebugSession::log(LogSeverity severity, const Data& data) {
_debugger->pushData(shared_from_this(), data);
}
void DebugSession::log(LogSeverity severity, const std::string& message) {
_debugger->pushData(shared_from_this(), Data(message));
}
}
|