diff options
author | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2014-03-07 13:03:04 (GMT) |
---|---|---|
committer | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2014-03-07 13:03:04 (GMT) |
commit | ca46aa711fb5d08a8fd1cc6b91593c281189e8e3 (patch) | |
tree | c46ed5fcbf44ea1a32517f8ba3d6d9a066b6fed8 /src/uscxml/debug | |
parent | fce16e70dff8503bfab2e734bca5a52d9057a3ee (diff) | |
download | uscxml-ca46aa711fb5d08a8fd1cc6b91593c281189e8e3.zip uscxml-ca46aa711fb5d08a8fd1cc6b91593c281189e8e3.tar.gz uscxml-ca46aa711fb5d08a8fd1cc6b91593c281189e8e3.tar.bz2 |
Modified InterpreterMonitor for uscxml-debugger
Diffstat (limited to 'src/uscxml/debug')
-rw-r--r-- | src/uscxml/debug/Breakpoint.cpp | 231 | ||||
-rw-r--r-- | src/uscxml/debug/Breakpoint.h | 77 | ||||
-rw-r--r-- | src/uscxml/debug/Debugger.cpp | 247 | ||||
-rw-r--r-- | src/uscxml/debug/Debugger.h | 115 | ||||
-rw-r--r-- | src/uscxml/debug/DebuggerServlet.cpp | 321 | ||||
-rw-r--r-- | src/uscxml/debug/DebuggerServlet.h | 83 | ||||
-rw-r--r-- | src/uscxml/debug/SCXMLDotWriter.cpp | 16 | ||||
-rw-r--r-- | src/uscxml/debug/SCXMLDotWriter.h | 8 |
8 files changed, 1086 insertions, 12 deletions
diff --git a/src/uscxml/debug/Breakpoint.cpp b/src/uscxml/debug/Breakpoint.cpp new file mode 100644 index 0000000..f64efad --- /dev/null +++ b/src/uscxml/debug/Breakpoint.cpp @@ -0,0 +1,231 @@ +/** + * @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/Breakpoint.h" +#include "uscxml/Interpreter.h" + +namespace uscxml { + +Breakpoint::Breakpoint(const Data& data) { + subject = UNDEF_SUBJECT; + when = UNDEF_WHEN; + action = UNDEF_ACTION; + + if (data.hasKey("when")) { + if (false) { + } else if (data["when"].atom == "before") { + when = BEFORE; + } else if (data["when"].atom == "after") { + when = AFTER; + } else if (data["when"].atom == "on") { + when = ON; + } + } + + if (data.hasKey("action")) { + if (false) { + } else if (data["action"].atom == "enter") { + action = ENTER; + } else if (data["action"].atom == "exit") { + action = EXIT; + } else if (data["action"].atom == "invoke") { + action = INVOKE; + } else if (data["action"].atom == "cancel") { + action = UNINVOKE; + } + } + + if (data.hasKey("subject")) { + if (false) { + } else if (data["subject"].atom == "state") { + subject = STATE; + } else if (data["subject"].atom == "transition") { + subject = TRANSITION; + } else if (data["subject"].atom == "stable") { + subject = STABLE; + } else if (data["subject"].atom == "microstep") { + subject = MICROSTEP; + } else if (data["subject"].atom == "event") { + subject = EVENT; + } else if (data["subject"].atom == "invoker") { + subject = INVOKER; + } else if (data["subject"].atom == "exec") { + subject = EXECUTABLE; + } + } + + if (data.hasKey("condition")) + condition = data["condition"].atom; + + if (data.hasKey("invokeId")) + invokeId = data["invokeId"].atom; + + if (data.hasKey("invokeType")) + invokeType = data["invokeType"].atom; + + if (data.hasKey("eventName")) + eventName = data["eventName"].atom; + + if (data.hasKey("stateId")) + stateId = data["stateId"].atom; + + if (data.hasKey("transSource")) + transSource = data["transSource"].atom; + + if (data.hasKey("transTarget")) + transTarget = data["transTarget"].atom; + +} + +Data Breakpoint::toData() const { + Data data; + + switch (subject) { + case STATE: + data.compound["subject"] = Data("state", Data::VERBATIM); + break; + case TRANSITION: + data.compound["subject"] = Data("transition", Data::VERBATIM); + break; + case STABLE: + data.compound["subject"] = Data("stable", Data::VERBATIM); + break; + case MICROSTEP: + data.compound["subject"] = Data("microstep", Data::VERBATIM); + break; + case EVENT: + data.compound["subject"] = Data("event", Data::VERBATIM); + break; + case INVOKER: + data.compound["subject"] = Data("invoker", Data::VERBATIM); + break; + case EXECUTABLE: + data.compound["subject"] = Data("exec", Data::VERBATIM); + break; + default: + break; + } + + switch (when) { + case AFTER: + data.compound["when"] = Data("after", Data::VERBATIM); + break; + case BEFORE: + data.compound["when"] = Data("before", Data::VERBATIM); + break; + case ON: + data.compound["when"] = Data("on", Data::VERBATIM); + break; + default: + break; + } + + switch (action) { + case ENTER: + data.compound["action"] = Data("enter", Data::VERBATIM); + break; + case EXIT: + data.compound["action"] = Data("exit", Data::VERBATIM); + break; + case INVOKE: + data.compound["action"] = Data("invoke", Data::VERBATIM); + break; + case UNINVOKE: + data.compound["action"] = Data("cancel", Data::VERBATIM); + break; + default: + break; + } + + if (invokeId.length() > 0) + data.compound["invokeId"] = Data(invokeId, Data::VERBATIM); + + if (invokeType.length() > 0) + data.compound["invokeType"] = Data(invokeType, Data::VERBATIM); + + if (eventName.length() > 0) + data.compound["eventName"] = Data(eventName, Data::VERBATIM); + + if (stateId.length() > 0) + data.compound["stateId"] = Data(stateId, Data::VERBATIM); + + if (transSource.length() > 0) + data.compound["transSource"] = Data(transSource, Data::VERBATIM); + + if (transTarget.length() > 0) + data.compound["transTarget"] = Data(transTarget, Data::VERBATIM); + + if (condition.length() > 0) + data.compound["condition"] = Data(condition, Data::VERBATIM); + + return data; +} + +bool Breakpoint::matches(const Breakpoint& other) const { + // would we match the given breakpoint? + + if (subject != UNDEF_SUBJECT && + other.subject != UNDEF_SUBJECT && + other.subject != subject) + return false; // subject does not match + + if (when != UNDEF_WHEN && + other.when != UNDEF_WHEN && + other.when != when) + return false; // time does not match + + if (action != UNDEF_ACTION && + other.action != UNDEF_ACTION && + other.action != action) + return false; // action does not match + + // when we have a qualifier it has to match + if(invokeId.length() > 0 && !InterpreterImpl::nameMatch(invokeId, other.invokeId)) { + return false; + } + + if(invokeType.length() > 0 && !InterpreterImpl::nameMatch(invokeType, other.invokeType)) { + return false; + } + + if(stateId.length() > 0 && !InterpreterImpl::nameMatch(stateId, other.stateId)) { + return false; + } + + if(eventName.length() > 0 && !InterpreterImpl::nameMatch(eventName, other.eventName)) { + return false; + } + + if(transSource.length() > 0 && !InterpreterImpl::nameMatch(transSource, other.transSource)) { + return false; + } + + if(transTarget.length() > 0 && !InterpreterImpl::nameMatch(transTarget, other.transTarget)) { + return false; + } + + return true; +} + + +bool Breakpoint::isValid() { + return true; +} + +}
\ No newline at end of file diff --git a/src/uscxml/debug/Breakpoint.h b/src/uscxml/debug/Breakpoint.h new file mode 100644 index 0000000..b2861d8 --- /dev/null +++ b/src/uscxml/debug/Breakpoint.h @@ -0,0 +1,77 @@ +/** + * @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 + */ + +#ifndef BREAKPOINT_H_VR7K7T1X +#define BREAKPOINT_H_VR7K7T1X + +#include "uscxml/Message.h" + +namespace uscxml { + +class USCXML_API Breakpoint { +public: + + enum When { + UNDEF_WHEN, AFTER, BEFORE, ON + }; + + enum Subject { + UNDEF_SUBJECT, STATE, TRANSITION, STABLE, MICROSTEP, EVENT, INVOKER, EXECUTABLE + }; + + enum Action { + UNDEF_ACTION, ENTER, EXIT, INVOKE, UNINVOKE + }; + + Breakpoint() {} + Breakpoint(const Data& data); + + // would we match the given breakpoint as well? + bool matches(const Breakpoint& other) const; + + bool isValid(); + + Data toData() const; + + bool operator<(const Breakpoint& other) const { + return (origData < other.origData); + } + + When when; + Subject subject; + Action action; + + std::string invokeId; + std::string invokeType; + + std::string eventName; + + std::string stateId; + std::string transSource; + std::string transTarget; + + std::string condition; + Data origData; +}; + +} + + + +#endif /* end of include guard: BREAKPOINT_H_VR7K7T1X */ diff --git a/src/uscxml/debug/Debugger.cpp b/src/uscxml/debug/Debugger.cpp new file mode 100644 index 0000000..aa97a22 --- /dev/null +++ b/src/uscxml/debug/Debugger.cpp @@ -0,0 +1,247 @@ +/** +* @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/Debugger.h" +#include "uscxml/DOMUtils.h" + +namespace uscxml { + +void Debugger::afterCompletion(Interpreter interpreter) { + Data msg; + msg.compound["replyType"] = Data("finished", Data::VERBATIM); + pushData(msg); +} + +std::list<Breakpoint> getQualifiedStateBreakpoints(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state, Breakpoint breakpointTemplate) { + std::list<Breakpoint> breakpoints; + + Breakpoint bp = breakpointTemplate; // copy base as template + bp.stateId = ATTR(state, "id"); + bp.subject = Breakpoint::STATE; + breakpoints.push_back(bp); + + return breakpoints; +} + +std::list<Breakpoint> getQualifiedInvokeBreakpoints(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string invokeId, Breakpoint breakpointTemplate) { + std::list<Breakpoint> breakpoints; + + Breakpoint bp = breakpointTemplate; // copy base as template + bp.subject = Breakpoint::INVOKER; + bp.invokeId = invokeId; + + if (HAS_ATTR(invokeElem, "type")) { + bp.invokeType = ATTR(invokeElem, "type"); + } else if (HAS_ATTR(invokeElem, "typeexpr")) { + bp.invokeType = interpreter.getDataModel().evalAsString(ATTR(invokeElem, "typeexpr")); + } + + breakpoints.push_back(bp); + + return breakpoints; +} + +std::list<Breakpoint> getQualifiedTransBreakpoints(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition, Breakpoint breakpointTemplate) { + std::list<Breakpoint> breakpoints; + + Arabica::DOM::Element<std::string> source(interpreter.getSourceState(transition)); + Arabica::XPath::NodeSet<std::string> targets = interpreter.getTargetStates(transition); + + for (int j = 0; j < targets.size(); j++) { + Arabica::DOM::Element<std::string> target(targets[j]); + + Breakpoint bp = breakpointTemplate; // copy base as template + bp.transSource = ATTR(source, "id"); + bp.transTarget = ATTR(target, "id"); + bp.subject = Breakpoint::TRANSITION; + + breakpoints.push_back(bp); + } + + return breakpoints; +} + +void Debugger::beforeTakingTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition) { + handleTransition(interpreter, transition, Breakpoint::BEFORE); +} +void Debugger::afterTakingTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition) { + handleTransition(interpreter, transition, Breakpoint::AFTER); +} +void Debugger::beforeExitingState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state) { + handleState(interpreter, state, Breakpoint::BEFORE, Breakpoint::EXIT); +} +void Debugger::afterExitingState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state) { + handleState(interpreter, state, Breakpoint::AFTER, Breakpoint::EXIT); +} +void Debugger::beforeEnteringState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state) { + handleState(interpreter, state, Breakpoint::BEFORE, Breakpoint::ENTER); +} +void Debugger::afterEnteringState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state) { + handleState(interpreter, state, Breakpoint::AFTER, Breakpoint::ENTER); +} +void Debugger::beforeUninvoking(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeid) { + handleInvoke(interpreter, invokeElem, invokeid, Breakpoint::BEFORE, Breakpoint::UNINVOKE); +} +void Debugger::afterUninvoking(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeid) { + handleInvoke(interpreter, invokeElem, invokeid, Breakpoint::AFTER, Breakpoint::UNINVOKE); +} +void Debugger::beforeInvoking(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeid) { + handleInvoke(interpreter, invokeElem, invokeid, Breakpoint::BEFORE, Breakpoint::INVOKE); +} +void Debugger::afterInvoking(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeid) { + handleInvoke(interpreter, invokeElem, invokeid, Breakpoint::AFTER, Breakpoint::INVOKE); +} +void Debugger::onStableConfiguration(Interpreter interpreter) { + handleStable(interpreter, Breakpoint::ON); +} +void Debugger::beforeMicroStep(Interpreter interpreter) { + handleMicrostep(interpreter, Breakpoint::BEFORE); +} +void Debugger::afterMicroStep(Interpreter interpreter) { + handleMicrostep(interpreter, Breakpoint::AFTER); +} +void Debugger::beforeProcessingEvent(Interpreter interpreter, const Event& event) { + handleEvent(interpreter, event, Breakpoint::BEFORE); +} + +void Debugger::handleEvent(Interpreter interpreter, const Event& event, Breakpoint::When when) { + if (!interpreter.isRunning()) + return; + + std::list<Breakpoint> breakpoints; + + Breakpoint breakpoint; + breakpoint.when = when; + breakpoint.eventName = event.name; + breakpoint.subject = Breakpoint::EVENT; + breakpoints.push_back(breakpoint); + + checkBreakpoints(interpreter, breakpoints); + +} + +void Debugger::handleStable(Interpreter interpreter, Breakpoint::When when) { + if (!interpreter.isRunning()) + return; + + std::list<Breakpoint> breakpoints; + + Breakpoint breakpoint; + breakpoint.when = when; + breakpoint.subject = Breakpoint::STABLE; + breakpoints.push_back(breakpoint); + + checkBreakpoints(interpreter, breakpoints); +} + +void Debugger::handleMicrostep(Interpreter interpreter, Breakpoint::When when) { + if (!interpreter.isRunning()) + return; + + std::list<Breakpoint> breakpoints; + + Breakpoint breakpoint; + breakpoint.when = when; + breakpoint.subject = Breakpoint::MICROSTEP; + breakpoints.push_back(breakpoint); + + checkBreakpoints(interpreter, breakpoints); +} + +void Debugger::handleTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition, Breakpoint::When when) { + if (!interpreter.isRunning()) + return; + + Breakpoint breakpointTemplate; + breakpointTemplate.when = when; + std::list<Breakpoint> qualifiedBreakpoints = getQualifiedTransBreakpoints(interpreter, transition, breakpointTemplate); + checkBreakpoints(interpreter, qualifiedBreakpoints); +} + +void Debugger::handleState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state, Breakpoint::When when, Breakpoint::Action action) { + if (!interpreter.isRunning()) + return; + + Breakpoint breakpointTemplate; + breakpointTemplate.when = when; + breakpointTemplate.action = action; + std::list<Breakpoint> qualifiedBreakpoints = getQualifiedStateBreakpoints(interpreter, state, breakpointTemplate); + checkBreakpoints(interpreter, qualifiedBreakpoints); + +} + +void Debugger::handleInvoke(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeId, Breakpoint::When when, Breakpoint::Action action) { + if (!interpreter.isRunning()) + return; + + Breakpoint breakpointTemplate; + breakpointTemplate.when = when; + breakpointTemplate.action = action; + std::list<Breakpoint> qualifiedBreakpoints = getQualifiedInvokeBreakpoints(interpreter, invokeElem, invokeId, breakpointTemplate); + checkBreakpoints(interpreter, qualifiedBreakpoints); + +} + +void Debugger::checkBreakpoints(Interpreter interpreter, const std::list<Breakpoint> qualifiedBreakpoints) { + std::list<Breakpoint>::const_iterator qualifiedBreakpointIter = qualifiedBreakpoints.begin(); + while(qualifiedBreakpointIter != qualifiedBreakpoints.end()) { + const Breakpoint& qualifiedBreakpoint = *qualifiedBreakpointIter++; + + // check if one of the user-supplied breakpoints match + bool userBreakpointMatched = false; + std::set<Breakpoint>::const_iterator breakpointIter = _breakPoints.begin(); + while(breakpointIter != _breakPoints.end()) { + const Breakpoint& breakpoint = *breakpointIter++; + if (breakpoint.matches(qualifiedBreakpoint)) { + Data replyData; + replyData.compound["breakpoint"] = breakpoint.toData(); + replyData.compound["qualified"] = qualifiedBreakpoint.toData(); + + userBreakpointMatched = true; + hitBreakpoint(interpreter, replyData); + } + } + if (_isStepping && !userBreakpointMatched) { + Data replyData; + replyData.compound["qualified"] = qualifiedBreakpoint.toData(); + hitBreakpoint(interpreter, replyData); + } + } +} + +void Debugger::send(google::LogSeverity severity, const char* full_filename, + const char* base_filename, int line, + const struct ::tm* tm_time, + const char* message, size_t message_len) { + + // _sendQueue is thread-safe, not sure about ToString though + + LogMessage msg(severity, + full_filename, + base_filename, + line, + tm_time, + std::string(message, message_len), + ToString(severity, base_filename, line, tm_time, message, message_len)); + msg.compound["replyType"] = Data("log", Data::VERBATIM); + pushData(msg); +} + + +}
\ No newline at end of file diff --git a/src/uscxml/debug/Debugger.h b/src/uscxml/debug/Debugger.h new file mode 100644 index 0000000..dfc197d --- /dev/null +++ b/src/uscxml/debug/Debugger.h @@ -0,0 +1,115 @@ +/** + * @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 + */ + +#ifndef DEBUGGERMONITOR_H_Z050WPFH +#define DEBUGGERMONITOR_H_Z050WPFH + +#include "uscxml/Message.h" +#include "uscxml/Interpreter.h" +#include "uscxml/debug/Breakpoint.h" + +#include <glog/logging.h> + +namespace uscxml { + +class USCXML_API Debugger : public InterpreterMonitor, public google::LogSink { +public: + Debugger() { + _isStepping = false; + } + virtual ~Debugger() {} + + class LogMessage : public Data { + public: + LogMessage(google::LogSeverity severity, const char* full_filename, + const char* base_filename, int line, + const struct ::tm* tm_time, + std::string message, std::string formatted) { + + compound["severity"] = severity; + compound["fullFilename"] = Data(full_filename, Data::VERBATIM); + compound["baseFilename"] = Data(base_filename, Data::VERBATIM); + compound["line"] = line; + compound["message"] = Data(message, Data::VERBATIM); + compound["time"] = Data(mktime((struct ::tm*)tm_time), Data::INTERPRETED); + compound["formatted"] = Data(formatted, Data::VERBATIM); + } + }; + + virtual void pushData(Data pushData) = 0; + virtual void hitBreakpoint(const Interpreter& interpreter, Data data) = 0; + + void stepping(bool enable) { + _isStepping = enable; + } + + // InterpreterMonitor + virtual void beforeProcessingEvent(Interpreter interpreter, const Event& event); + virtual void beforeMicroStep(Interpreter interpreter); + virtual void beforeExitingState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state); + virtual void afterExitingState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state); + virtual void beforeUninvoking(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeid); + virtual void afterUninvoking(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeid); + virtual void beforeTakingTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition); + virtual void afterTakingTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition); + virtual void beforeEnteringState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state); + virtual void afterEnteringState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state); + virtual void beforeInvoking(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeid); + virtual void afterInvoking(Interpreter interpreter, const Arabica::DOM::Element<std::string>& invokeElem, const std::string& invokeid); + virtual void afterMicroStep(Interpreter interpreter); + virtual void beforeExecutingContent(Interpreter interpreter, const Arabica::DOM::Element<std::string>& element) {} + virtual void afterExecutingContent(Interpreter interpreter, const Arabica::DOM::Element<std::string>& element) {} + virtual void onStableConfiguration(Interpreter interpreter); + virtual void beforeCompletion(Interpreter interpreter) {} + virtual void afterCompletion(Interpreter interpreter); + + // Logsink + virtual void send(google::LogSeverity severity, const char* full_filename, + const char* base_filename, int line, + const struct ::tm* tm_time, + const char* message, size_t message_len); + +protected: + + void handleTransition(Interpreter interpreter, + const Arabica::DOM::Element<std::string>& transition, + Breakpoint::When when); + void handleState(Interpreter interpreter, + const Arabica::DOM::Element<std::string>& state, + Breakpoint::When when, + Breakpoint::Action action); + void handleInvoke(Interpreter interpreter, + const Arabica::DOM::Element<std::string>& invokeElem, + const std::string& invokeId, + Breakpoint::When when, + Breakpoint::Action action); + void handleStable(Interpreter interpreter, Breakpoint::When when); + void handleMicrostep(Interpreter interpreter, Breakpoint::When when); + void handleEvent(Interpreter interpreter, const Event& event, Breakpoint::When when); + void checkBreakpoints(Interpreter interpreter, const std::list<Breakpoint> qualifiedBreakpoints); + + bool _isStepping; + std::set<Breakpoint> _breakPoints; + +}; + +} + + +#endif /* end of include guard: DEBUGGERMONITOR_H_Z050WPFH */ diff --git a/src/uscxml/debug/DebuggerServlet.cpp b/src/uscxml/debug/DebuggerServlet.cpp new file mode 100644 index 0000000..e179b8c --- /dev/null +++ b/src/uscxml/debug/DebuggerServlet.cpp @@ -0,0 +1,321 @@ +/** + * @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/DebuggerServlet.h" +#include "uscxml/UUID.h" +#include <boost/algorithm/string.hpp> + +namespace uscxml { + +void DebuggerServlet::pushData(Data pushData) { + std::cout << "trying to push " << pushData["replyType"].atom << std::endl; + _sendQueue.push(pushData); + serverPushData(); +} + +void DebuggerServlet::serverPushData() { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + if (_sendQueue.isEmpty()) + return; + + if (!_clientConn) + return; + + Data reply = _sendQueue.pop(); + std::cout << "pushing " << reply["replyType"].atom << std::endl; + returnData(_clientConn, reply); + _clientConn = HTTPServer::Request(); +} + +void DebuggerServlet::returnData(const HTTPServer::Request& request, Data replyData) { + HTTPServer::Reply reply(request); + + if (!replyData.hasKey("status")) { + replyData.compound["status"] = Data("success", Data::VERBATIM); + } + + reply.content = Data::toJSON(replyData); + reply.headers["Access-Control-Allow-Origin"] = "*"; + reply.headers["Content-Type"] = "application/json"; + HTTPServer::reply(reply); +} + +void DebuggerServlet::hitBreakpoint(const Interpreter& interpreter, + Data data) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + data.compound["replyType"] = Data("breakpoint", Data::VERBATIM); + pushData(data); + + _resumeCond.wait(_mutex); + tthread::this_thread::sleep_for(tthread::chrono::milliseconds(200)); +} + +bool DebuggerServlet::isCORS(const HTTPServer::Request& request) { + return (request.data["type"].atom == "options" && + request.data["header"].hasKey("Origin") && + request.data["header"].hasKey("Access-Control-Request-Method")); +} + +void DebuggerServlet::handleCORS(const HTTPServer::Request& request) { + HTTPServer::Reply corsReply(request); + if (request.data["header"].hasKey("Origin")) { + corsReply.headers["Access-Control-Allow-Origin"] = request.data["header"]["Origin"].atom; + } else { + corsReply.headers["Access-Control-Allow-Origin"] = "*"; + } + if (request.data["header"].hasKey("Access-Control-Request-Method")) + corsReply.headers["Access-Control-Allow-Methods"] = request.data["header"]["Access-Control-Request-Method"].atom; + if (request.data["header"].hasKey("Access-Control-Request-Headers")) + corsReply.headers["Access-Control-Allow-Headers"] = request.data["header"]["Access-Control-Request-Headers"].atom; + + // std::cout << "CORS!" << std::endl << request << std::endl; + HTTPServer::reply(corsReply); +} + +bool DebuggerServlet::httpRecvRequest(const HTTPServer::Request& request) { + if (!request.data.hasKey("path")) + return false; // returnError(request); + + if (isCORS(request)) { + handleCORS(request); + return true; + } + + std::cout << request.data["path"] << ": " << request.data["content"] << std::endl; + + if (false) { + } else if (boost::starts_with(request.data["path"].atom, "/poll")) { + processPoll(request); + } else if (boost::starts_with(request.data["path"].atom, "/connect")) { + processConnect(request); + } else if (boost::starts_with(request.data["path"].atom, "/disconnect")) { + processDisconnect(request); + } else if (boost::starts_with(request.data["path"].atom, "/sessions")) { + processListSessions(request); + } else if (boost::starts_with(request.data["path"].atom, "/breakpoint/add")) { + processAddBreakPoint(request); + } else if (boost::starts_with(request.data["path"].atom, "/breakpoint/remove")) { + processRemoveBreakPoint(request); + } else if (boost::starts_with(request.data["path"].atom, "/debug/prepare")) { + processDebugPrepare(request); + } else if (boost::starts_with(request.data["path"].atom, "/debug/start")) { + processDebugStart(request); + } else if (boost::starts_with(request.data["path"].atom, "/debug/stop")) { + processDebugStop(request); + } else if (boost::starts_with(request.data["path"].atom, "/debug/step")) { + processDebugStep(request); + } else if (boost::starts_with(request.data["path"].atom, "/debug/pause")) { + processDebugPause(request); + } else if (boost::starts_with(request.data["path"].atom, "/debug/resume")) { + processDebugResume(request); + } else if (boost::starts_with(request.data["path"].atom, "/debug/eval")) { + processDebugEval(request); + } + + return true; +} + +void DebuggerServlet::processPoll(const HTTPServer::Request& request) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + _clientConn = request; + serverPushData(); +} + +void DebuggerServlet::processDebugPrepare(const HTTPServer::Request& request) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + +// std::cout << "clearing all pushes" << std::endl; +// _sendQueue.clear(); + + // this will call the destructor if _interpreter already is set + _resumeCond.notify_all(); + _interpreter = Interpreter::fromXML(request.data["content"].atom); + + Data replyData; + if (_interpreter) { + // register ourself as a monitor + _interpreter.addMonitor(this); + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + returnData(request, replyData); +} + +void DebuggerServlet::processDebugStart(const HTTPServer::Request& request) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + Data replyData; + if (_interpreter) { + // register ourself as a monitor + _interpreter.start(); + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + returnData(request, replyData); +} + +void DebuggerServlet::processDebugStop(const HTTPServer::Request& request) { +// tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + stepping(false); + + Data replyData; + if (_interpreter) { + _interpreter.stop(); + _resumeCond.notify_all(); // unblock breakpoints + _interpreter.join(); + _interpreter = Interpreter(); // empty interpreter, calls destructor + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("Interpreter already stopped", Data::VERBATIM); + } + returnData(request, replyData); +} + +void DebuggerServlet::processDebugEval(const HTTPServer::Request& request) { + Data replyData; + if (!_interpreter) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No interpreter running", Data::VERBATIM); + } else if (!_interpreter.getDataModel()) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No datamodel available", Data::VERBATIM); + } else if (!request.data["content"].hasKey("expression")) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No expression given", Data::VERBATIM); + } else { + std::string expr = request.data["content"]["expression"].atom; + try { + replyData.compound["eval"] = _interpreter.getDataModel().getStringAsData(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); + } + returnData(request, replyData); +} + +void DebuggerServlet::processDebugStep(const HTTPServer::Request& request) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + stepping(true); + _resumeCond.notify_one(); + + Data replyData; + if (_interpreter && !_interpreter.isRunning()) { + // register ourself as a monitor + _interpreter.start(); + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + returnData(request, replyData); + +} + +void DebuggerServlet::processDebugResume(const HTTPServer::Request& request) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + stepping(false); + + Data replyData; + replyData.compound["status"] = Data("success", Data::VERBATIM); + returnData(request, replyData); + + _resumeCond.notify_one(); +} + +void DebuggerServlet::processDebugPause(const HTTPServer::Request& request) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + Data replyData; + replyData.compound["status"] = Data("success", Data::VERBATIM); + returnData(request, replyData); +} + +void DebuggerServlet::processConnect(const HTTPServer::Request& request) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + _sessionId = UUID::getUUID(); + _breakPoints.clear(); +// _sendQueue.clear(); + + Data replyData; + replyData.compound["session"] = Data(_sessionId, Data::VERBATIM); + replyData.compound["status"] = Data("success", Data::VERBATIM); + returnData(request, replyData); +} + +void DebuggerServlet::processListSessions(const HTTPServer::Request& request) { + Data replyData; + + // TODO: return actual data + Data sessionData; + sessionData.compound["name"] = Data("Not actually a Session", Data::VERBATIM); + sessionData.compound["id"] = Data("23452523-wg23g2g2-234t2g-23g2g", Data::VERBATIM); + replyData.compound["sessions"].array.push_back(sessionData); + + sessionData.compound["name"] = Data("But returned from the server!", Data::VERBATIM); + sessionData.compound["id"] = Data("swfgsgfw-g232vqvq-234t2g-23g2g", Data::VERBATIM); + replyData.compound["sessions"].array.push_back(sessionData); + + replyData.compound["status"] = Data("success", Data::VERBATIM); + returnData(request, replyData); +} + +void DebuggerServlet::processDisconnect(const HTTPServer::Request& request) { + Data replyData; + replyData.compound["status"] = Data("success", Data::VERBATIM); + returnData(request, replyData); +} + +void DebuggerServlet::processAddBreakPoint(const HTTPServer::Request& request) { + Breakpoint breakPoint(request.data["content"]); + Data replyData; + if (breakPoint.isValid()) { + replyData.compound["status"] = Data("success", Data::VERBATIM); + + if (_breakPoints.find(breakPoint) == _breakPoints.end()) { + _breakPoints.insert(breakPoint); + } + } else { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + returnData(request, replyData); +} + +void DebuggerServlet::processRemoveBreakPoint(const HTTPServer::Request& request) { + Breakpoint breakPoint(request.data["content"]); + Data replyData; + if (_breakPoints.erase(breakPoint) > 0) { + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["message"] = Data("No such breakpoint", Data::VERBATIM); + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + returnData(request, replyData); +} + + +}
\ No newline at end of file diff --git a/src/uscxml/debug/DebuggerServlet.h b/src/uscxml/debug/DebuggerServlet.h new file mode 100644 index 0000000..5cd0be9 --- /dev/null +++ b/src/uscxml/debug/DebuggerServlet.h @@ -0,0 +1,83 @@ +/** + * @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 + */ + +#ifndef DEBUGGERSERVLET_H_ATUMDA3G +#define DEBUGGERSERVLET_H_ATUMDA3G + +#include "uscxml/Common.h" +#include "getopt.h" + +#include "uscxml/server/HTTPServer.h" +#include "uscxml/Interpreter.h" + +#include "uscxml/debug/Debugger.h" +#include "uscxml/concurrency/tinythread.h" + +namespace uscxml { + +class USCXML_API DebuggerServlet : public Debugger, public HTTPServlet { +public: + virtual ~DebuggerServlet() {} + + // from Debugger + virtual void addBreakpoint(const Breakpoint& breakpoint) {}; + + bool isCORS(const HTTPServer::Request& request); + void handleCORS(const HTTPServer::Request& request); + + bool httpRecvRequest(const HTTPServer::Request& request); + void setURL(const std::string& url) { + _url = url; + } + + void pushData(Data pushData); + void returnData(const HTTPServer::Request& request, Data replyData); + + void hitBreakpoint(const Interpreter& interpreter, + Data data); + + void processDebugEval(const HTTPServer::Request& request); + void processDebugPrepare(const HTTPServer::Request& request); + void processDebugStart(const HTTPServer::Request& request); + void processDebugStop(const HTTPServer::Request& request); + void processDebugStep(const HTTPServer::Request& request); + void processDebugResume(const HTTPServer::Request& request); + void processDebugPause(const HTTPServer::Request& request); + void processConnect(const HTTPServer::Request& request); + void processListSessions(const HTTPServer::Request& request); + void processDisconnect(const HTTPServer::Request& request); + void processAddBreakPoint(const HTTPServer::Request& request); + void processRemoveBreakPoint(const HTTPServer::Request& request); + void processPoll(const HTTPServer::Request& request); + +protected: + void serverPushData(); + + Interpreter _interpreter; + std::string _sessionId; + std::string _url; + HTTPServer::Request _clientConn; + tthread::condition_variable _resumeCond; + tthread::recursive_mutex _mutex; + concurrency::BlockingQueue<Data> _sendQueue; +}; + +} + +#endif /* end of include guard: DEBUGGERSERVLET_H_ATUMDA3G */ diff --git a/src/uscxml/debug/SCXMLDotWriter.cpp b/src/uscxml/debug/SCXMLDotWriter.cpp index 07e7b9a..2058d72 100644 --- a/src/uscxml/debug/SCXMLDotWriter.cpp +++ b/src/uscxml/debug/SCXMLDotWriter.cpp @@ -32,9 +32,9 @@ SCXMLDotWriter::SCXMLDotWriter() { _indentation = 0; } -SCXMLDotWriter::SCXMLDotWriter(Interpreter interpreter, const Arabica::XPath::NodeSet<std::string>& transitions) { +SCXMLDotWriter::SCXMLDotWriter(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition) { _interpreter = interpreter; - _transitions = transitions; + _transition = transition; _iteration = 0; _indentation = 0; } @@ -61,10 +61,10 @@ void SCXMLDotWriter::beforeMicroStep(Interpreter interpreter) { // toDot(fileSS.str(), interpreter); } -void SCXMLDotWriter::beforeTakingTransitions(Interpreter interpreter, const Arabica::XPath::NodeSet<std::string>& transitions) { +void SCXMLDotWriter::beforeTakingTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition) { std::ostringstream fileSS; fileSS << interpreter.getName() << "." << std::setw(6) << std::setfill('0') << _iteration++ << ".dot"; - toDot(fileSS.str(), interpreter, transitions); + toDot(fileSS.str(), interpreter, transition); } std::string SCXMLDotWriter::getPrefix() { @@ -74,10 +74,10 @@ std::string SCXMLDotWriter::getPrefix() { return prefix; } -void SCXMLDotWriter::toDot(const std::string& filename, Interpreter interpreter, const Arabica::XPath::NodeSet<std::string>& transitions) { +void SCXMLDotWriter::toDot(const std::string& filename, Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition) { std::ofstream outfile(filename.c_str()); NodeList<std::string > scxmlElems = interpreter.getDocument().getElementsByTagName("scxml"); - SCXMLDotWriter writer(interpreter, transitions); + SCXMLDotWriter writer(interpreter, transition); if (scxmlElems.getLength() > 0) { writer._indentation++; outfile << "digraph {" << std::endl; @@ -168,7 +168,7 @@ void SCXMLDotWriter::writeStateElement(std::ostream& os, const Arabica::DOM::Ele for (int i = 0; i < childElems.getLength(); i++) { if (childElems.item(i).getNodeType() == Node_base::ELEMENT_NODE && iequals(TAGNAME(childElems.item(i)), "transition")) { writeTransitionElement(os, (Arabica::DOM::Element<std::string>)childElems.item(i)); - bool active = Interpreter::isMember(childElems.item(i), _transitions); + bool active = (childElems.item(i) == _transition); os << getPrefix() << "\"" << elemId << "\" -> \"" << idForNode(childElems.item(i)) << "\" [arrowhead=none" << std::endl; if (active) { os << ", penwidth=3, color=red]" << std::endl; @@ -202,7 +202,7 @@ void SCXMLDotWriter::writeTransitionElement(std::ostream& os, const Arabica::DOM Arabica::XPath::NodeSet<std::string> targetStates = _interpreter.getTargetStates(elem); - bool active = Interpreter::isMember(elem, _transitions); + bool active = (elem == _transition); std::string label; os << getPrefix() << "\"" << elemId << "\"["; diff --git a/src/uscxml/debug/SCXMLDotWriter.h b/src/uscxml/debug/SCXMLDotWriter.h index 2d3625c..4e2d7a8 100644 --- a/src/uscxml/debug/SCXMLDotWriter.h +++ b/src/uscxml/debug/SCXMLDotWriter.h @@ -65,12 +65,12 @@ public: virtual void onStableConfiguration(Interpreter interpreter); virtual void afterCompletion(Interpreter interpreter); - virtual void beforeTakingTransitions(Interpreter interpreter, const Arabica::XPath::NodeSet<std::string>& transitions); + virtual void beforeTakingTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition); virtual void beforeMicroStep(Interpreter interpreter); static void toDot(const std::string& filename, Interpreter interpreter, - const Arabica::XPath::NodeSet<std::string>& transitions = Arabica::XPath::NodeSet<std::string>()); + const Arabica::DOM::Element<std::string>& transition = Arabica::DOM::Element<std::string>()); std::string getDetailedLabel(const Arabica::DOM::Element<std::string>& elem, int indentation = 0); std::string colorForIndent(int indent); @@ -84,7 +84,7 @@ public: protected: SCXMLDotWriter(Interpreter interpreter, - const Arabica::XPath::NodeSet<std::string>& transitions); + const Arabica::DOM::Element<std::string>& transition); void writeSCXMLElement(std::ostream& os, const Arabica::DOM::Element<std::string>& elem); void writeStateElement(std::ostream& os, const Arabica::DOM::Element<std::string>& elem); @@ -95,7 +95,7 @@ protected: int _indentation; // these are only set in ephemeral instances per monitor call - Arabica::XPath::NodeSet<std::string> _transitions; + Arabica::DOM::Element<std::string> _transition; Interpreter _interpreter; }; |