6#include <private/qqmldebugconnector_p.h>
7#include <private/qqmldebugserviceinterfaces_p.h>
8#include <private/qv4debugging_p.h>
9#include <private/qv4debugging_p.h>
10#include <private/qv4engine_p.h>
11#include <private/qv4identifierhash_p.h>
12#include <private/qv4identifiertable_p.h>
13#include <private/qv4objectiterator_p.h>
14#include <private/qv4runtime_p.h>
15#include <private/qv4script_p.h>
16#include <private/qv4stackframe_p.h>
17#include <private/qv4string_p.h>
18#include <private/qversionedpacket_p.h>
20#include <QtQml/qjsengine.h>
21#include <QtCore/qjsonarray.h>
22#include <QtCore/qjsondocument.h>
23#include <QtCore/qjsonobject.h>
24#include <QtCore/qjsonvalue.h>
25#include <QtCore/qvector.h>
26#include <QtCore/qpointer.h>
29#define TRACE_PROTOCOL(s)
33using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
39 bool isValid()
const {
return lineNumber >= 0 && !fileName.isEmpty(); }
53 return qHash(b.fileName, seed) ^ b.lineNumber;
63static void setError(QJsonObject *response,
const QString &msg)
65 response->insert(QStringLiteral(
"type"), QStringLiteral(
"error"));
66 response->insert(QStringLiteral(
"msg"), msg);
78 void collect(QJsonArray *output,
const QString &parentIName,
const QString &name,
79 const QV4::Value &value);
81 bool isExpanded(
const QString &iname)
const {
return m_expanded.contains(iname); }
112 QString type = arguments.value(QLatin1String(
"type")).toString();
114 QString fileName = arguments.value(QLatin1String(
"file")).toString();
115 if (fileName.isEmpty()) {
116 setError(response, QStringLiteral(
"breakpoint has no file name"));
120 int line = arguments.value(QLatin1String(
"line")).toInt(-1);
122 setError(response, QStringLiteral(
"breakpoint has an invalid line number"));
128 bp.fileName = fileName.mid(fileName.lastIndexOf(
'/') + 1);
130 bp
.enabled = arguments.value(QLatin1String(
"enabled")).toBool(
true);
131 bp.condition = arguments.value(QLatin1String(
"condition")).toString();
132 bp
.ignoreCount = arguments.value(QLatin1String(
"ignorecount")).toInt();
133 m_breakPoints.append(bp);
137 response->insert(QStringLiteral(
"type"), type);
138 response->insert(QStringLiteral(
"breakpoint"), bp
.id);
143 int id = arguments.value(QLatin1String(
"id")).toInt();
145 response->insert(QStringLiteral(
"id"), id);
158 return m_pauseRequested
160 || m_stepping >= StepOver;
168 void handleCommand(QJsonObject *response,
const QString &cmd,
const QJsonObject &arguments);
171 void handleBacktrace(QJsonObject *response,
const QJsonObject &arguments);
172 void handleVariables(QJsonObject *response,
const QJsonObject &arguments);
173 void handleExpressions(QJsonObject *response,
const QJsonObject &arguments);
175 void handleDebuggerDeleted(QObject *debugger);
177 QV4::ReturnedValue evaluateExpression(
const QString &expression);
178 bool checkCondition(
const QString &expression);
180 QStringList breakOnSignals;
191 void handleContinue(QJsonObject *reponse, Speed speed);
193 QV4::Function *getFunction()
const;
195 bool reallyHitTheBreakPoint(
const QV4::Function *function,
int lineNumber);
197 QV4::ExecutionEngine *m_engine;
199 QV4::CppStackFrame *m_currentFrame =
nullptr;
201 bool m_pauseRequested;
204 QV4::PersistentValue m_returnedValue;
209 QV4::Scope scope(m_engine);
210 QV4::ScopedValue r(scope, evaluateExpression(expression));
211 return r->booleanValue();
214QV4::ReturnedValue
NativeDebugger::evaluateExpression(
const QString &expression)
216 QV4::Scope scope(m_engine);
219 QV4::ExecutionContext *ctx = m_engine->currentStackFrame ? m_engine->currentContext()
220 : m_engine->scriptContext();
222 QV4::Script script(ctx, QV4::Compiler::ContextType::Eval, expression);
223 if (
const QV4::Function *function = m_engine->currentStackFrame
224 ? m_engine->currentStackFrame->v4Function : m_engine->globalCode) {
225 script.setStrictMode(function->isStrict());
229 script.setInheritContext();
231 if (!m_engine->hasException) {
232 if (m_engine->currentStackFrame) {
233 QV4::ScopedValue thisObject(scope, m_engine->currentStackFrame->thisObject());
234 script.run(thisObject);
240 m_runningJob =
false;
241 return QV4::Encode::undefined();
247 m_stepping = NotStepping;
248 m_pauseRequested =
false;
249 m_runningJob =
false;
263 QString signalName = signal.left(signal.indexOf(QLatin1Char(
'('))).toLower();
265 for (
const QString &signal : std::as_const(breakOnSignals)) {
266 if (signal == signalName) {
274 const QJsonObject &arguments)
276 if (cmd == QLatin1String(
"backtrace"))
277 handleBacktrace(response, arguments);
278 else if (cmd == QLatin1String(
"variables"))
279 handleVariables(response, arguments);
280 else if (cmd == QLatin1String(
"expressions"))
281 handleExpressions(response, arguments);
282 else if (cmd == QLatin1String(
"stepin"))
283 handleContinue(response, StepIn);
284 else if (cmd == QLatin1String(
"stepout"))
285 handleContinue(response, StepOut);
286 else if (cmd == QLatin1String(
"stepover"))
287 handleContinue(response, StepOver);
288 else if (cmd == QLatin1String(
"continue"))
289 handleContinue(response, NotStepping);
296 return QString::fromLatin1(ds.data().toHex());
299static void decodeFrame(
const QString &f, QV4::CppStackFrame **frame)
302 QQmlDebugPacket ds(QByteArray::fromHex(f.toLatin1()));
304 *frame =
reinterpret_cast<QV4::CppStackFrame *>(rawFrame);
307void NativeDebugger::handleBacktrace(QJsonObject *response,
const QJsonObject &arguments)
309 int limit = arguments.value(QLatin1String(
"limit")).toInt(0);
311 QJsonArray frameArray;
312 QV4::CppStackFrame *f= m_engine->currentStackFrame;
313 for (
int i = 0; i < limit && f; ++i) {
314 QV4::Function *function = f->v4Function;
317 frame.insert(QStringLiteral(
"language"), QStringLiteral(
"js"));
318 frame.insert(QStringLiteral(
"context"), encodeFrame(f));
320 if (QV4::Heap::String *functionName = function->name())
321 frame.insert(QStringLiteral(
"function"), functionName->toQString());
322 frame.insert(QStringLiteral(
"file"), function->sourceFile());
324 int line = f->lineNumber();
325 frame.insert(QStringLiteral(
"line"), (line < 0 ? -line : line));
327 frameArray.push_back(frame);
329 f = f->parentFrame();
332 response->insert(QStringLiteral(
"frames"), frameArray);
336 const QV4::Value &value)
339 QV4::Scope scope(m_engine);
341 QString nonEmptyName = name.isEmpty() ? QString::fromLatin1(
"@%1").arg(m_anonCount++) : name;
342 QString iname = parentIName + QLatin1Char(
'.') + nonEmptyName;
343 dict.insert(QStringLiteral(
"iname"), iname);
344 dict.insert(QStringLiteral(
"name"), nonEmptyName);
346 QV4::ScopedValue typeString(scope, QV4::Runtime::TypeofValue::call(m_engine, value));
347 dict.insert(QStringLiteral(
"type"), typeString->toQStringNoThrow());
349 switch (value.type()) {
350 case QV4::Value::Empty_Type:
351 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"empty"));
352 dict.insert(QStringLiteral(
"haschild"),
false);
354 case QV4::Value::Undefined_Type:
355 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"undefined"));
356 dict.insert(QStringLiteral(
"haschild"),
false);
358 case QV4::Value::Null_Type:
359 dict.insert(QStringLiteral(
"type"), QStringLiteral(
"object"));
360 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"null"));
361 dict.insert(QStringLiteral(
"haschild"),
false);
363 case QV4::Value::Boolean_Type:
364 dict.insert(QStringLiteral(
"value"), value.booleanValue());
365 dict.insert(QStringLiteral(
"haschild"),
false);
367 case QV4::Value::Managed_Type:
368 if (
const QV4::String *string = value.as<QV4::String>()) {
369 dict.insert(QStringLiteral(
"value"), string->toQStringNoThrow());
370 dict.insert(QStringLiteral(
"haschild"),
false);
371 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"utf16"));
372 dict.insert(QStringLiteral(
"quoted"),
true);
373 }
else if (
const QV4::ArrayObject *array = value.as<QV4::ArrayObject>()) {
376 const uint n = array->getLength();
377 dict.insert(QStringLiteral(
"value"), qint64(n));
378 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"itemcount"));
379 dict.insert(QStringLiteral(
"haschild"), qint64(n));
380 if (isExpanded(iname)) {
382 for (uint i = 0; i < n; ++i) {
383 QV4::ReturnedValue v = array->get(i);
384 QV4::ScopedValue sval(scope, v);
385 collect(&children, iname, QString::number(i), *sval);
387 dict.insert(QStringLiteral(
"children"), children);
389 }
else if (
const QV4::Object *object = value.as<QV4::Object>()) {
391 bool expanded = isExpanded(iname);
392 qint64 numProperties = 0;
393 QV4::ObjectIterator it(scope, object, QV4::ObjectIterator::EnumerableOnly);
394 QV4::ScopedProperty p(scope);
395 QV4::ScopedPropertyKey name(scope);
397 QV4::PropertyAttributes attrs;
398 name = it.next(p, &attrs);
399 if (!name->isValid())
401 if (name->isStringOrSymbol()) {
404 QV4::Value v = p.property->value;
405 collect(&children, iname, name->toQString(), v);
409 dict.insert(QStringLiteral(
"value"), numProperties);
410 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"itemcount"));
411 dict.insert(QStringLiteral(
"haschild"), numProperties > 0);
413 dict.insert(QStringLiteral(
"children"), children);
416 case QV4::Value::Integer_Type:
417 dict.insert(QStringLiteral(
"value"), value.integerValue());
418 dict.insert(QStringLiteral(
"haschild"),
false);
421 dict.insert(QStringLiteral(
"value"), value.doubleValue());
422 dict.insert(QStringLiteral(
"haschild"),
false);
428void NativeDebugger::handleVariables(QJsonObject *response,
const QJsonObject &arguments)
431 QV4::CppStackFrame *frame =
nullptr;
432 decodeFrame(arguments.value(QLatin1String(
"context")).toString(), &frame);
434 setError(response, QStringLiteral(
"No stack frame passed"));
439 QV4::ExecutionEngine *engine = frame->v4Function->internalClass->engine;
441 setError(response, QStringLiteral(
"No execution engine passed"));
447 const QJsonArray expanded = arguments.value(QLatin1String(
"expanded")).toArray();
448 for (
const QJsonValue ex : expanded)
449 collector.m_expanded.append(ex.toString());
453 QV4::Scope scope(engine);
455 QV4::ScopedValue thisObject(scope, frame->thisObject());
456 collector.collect(&output, QString(), QStringLiteral(
"this"), thisObject);
457 QV4::Scoped<QV4::CallContext> callContext(scope, frame->callContext());
459 QV4::Heap::InternalClass *ic = callContext->internalClass();
460 QV4::ScopedValue v(scope);
461 for (uint i = 0; i < ic->size; ++i) {
462 QV4::ScopedValue stringOrSymbol(scope, ic->keyAt(i));
463 QV4::ScopedString propName(scope, stringOrSymbol->toString(scope.engine));
464 v = callContext->getProperty(propName);
465 collector.collect(&output, QString(), propName->toQString(), v);
469 response->insert(QStringLiteral(
"variables"), output);
472void NativeDebugger::handleExpressions(QJsonObject *response,
const QJsonObject &arguments)
475 QV4::CppStackFrame *frame =
nullptr;
476 decodeFrame(arguments.value(QLatin1String(
"context")).toString(), &frame);
478 setError(response, QStringLiteral(
"No stack frame passed"));
483 QV4::ExecutionEngine *engine = frame->v4Function->internalClass->engine;
485 setError(response, QStringLiteral(
"No execution engine passed"));
491 const QJsonArray expanded = arguments.value(QLatin1String(
"expanded")).toArray();
492 for (
const QJsonValue ex : expanded)
493 collector.m_expanded.append(ex.toString());
497 QV4::Scope scope(engine);
499 const QJsonArray expressions = arguments.value(QLatin1String(
"expressions")).toArray();
500 for (
const QJsonValue expr : expressions) {
501 QString expression = expr.toObject().value(QLatin1String(
"expression")).toString();
502 QString name = expr.toObject().value(QLatin1String(
"name")).toString();
506 QV4::ScopedValue result(scope, evaluateExpression(expression));
508 m_runningJob =
false;
509 if (result->isUndefined()) {
511 dict.insert(QStringLiteral(
"name"), name);
512 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"undefined"));
514 }
else if (result.ptr && result.ptr->rawValue()) {
515 collector.collect(&output, QString(), name, *result);
518 dict.insert(QStringLiteral(
"name"), name);
519 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"notaccessible"));
523 engine->hasException =
false;
526 response->insert(QStringLiteral(
"expressions"), output);
531 for (
int i = 0; i != m_breakPoints.size(); ++i) {
532 if (m_breakPoints.at(i).id == id) {
533 m_breakPoints.remove(i);
534 m_haveBreakPoints = !m_breakPoints.isEmpty();
542 m_breakPoints[id].enabled = enabled;
547 m_pauseRequested =
true;
550void NativeDebugger::handleContinue(QJsonObject *response, Speed speed)
554 if (!m_returnedValue.isUndefined())
555 m_returnedValue.set(m_engine, QV4::Encode::undefined());
557 m_currentFrame = m_engine->currentStackFrame;
566 if (m_stepping == StepOver) {
567 if (m_currentFrame == m_engine->currentStackFrame)
572 if (m_stepping == StepIn) {
577 if (m_pauseRequested) {
578 m_pauseRequested =
false;
584 if (QV4::Function *function = getFunction()) {
586 const int lineNumber = m_engine->currentStackFrame->lineNumber();
587 if (reallyHitTheBreakPoint(function, lineNumber))
598 if (m_stepping == StepIn) {
599 m_currentFrame = m_engine->currentStackFrame;
608 if (m_stepping != NotStepping && m_currentFrame == m_engine->currentStackFrame) {
609 m_currentFrame = m_currentFrame->parentFrame();
610 m_stepping = StepOver;
611 m_returnedValue.set(m_engine, retVal);
625 event.insert(QStringLiteral(
"event"), QStringLiteral(
"exception"));
626 m_service->emitAsynchronousMessageToClient(event);
631 if (m_engine->currentStackFrame)
632 return m_engine->currentStackFrame->v4Function;
634 return m_engine->globalCode;
641 event.insert(QStringLiteral(
"event"), QStringLiteral(
"break"));
642 event.insert(QStringLiteral(
"language"), QStringLiteral(
"js"));
643 if (QV4::CppStackFrame *frame = m_engine->currentStackFrame) {
644 QV4::Function *function = frame->v4Function;
645 event.insert(QStringLiteral(
"file"), function->sourceFile());
646 int line = frame->lineNumber();
647 event.insert(QStringLiteral(
"line"), (line < 0 ? -line : line));
650 m_service->emitAsynchronousMessageToClient(event);
653bool NativeDebugger::reallyHitTheBreakPoint(
const QV4::Function *function,
int lineNumber)
655 for (
int i = 0, n = m_service->m_breakHandler->m_breakPoints.size(); i != n; ++i) {
656 const BreakPoint &bp = m_service->m_breakHandler->m_breakPoints.at(i);
658 const QString base = QUrl(function->sourceFile()).fileName();
659 if (bp.fileName.endsWith(base)) {
660 if (bp.condition.isEmpty() || checkCondition(bp.condition)) {
661 BreakPoint &mbp = m_service->m_breakHandler->m_breakPoints[i];
680 delete m_breakHandler;
687 QV4::ExecutionEngine *ee = engine->handle();
691 if (state() == Enabled)
692 ee->setDebugger(debugger);
693 m_debuggers.append(QPointer<NativeDebugger>(debugger));
696 QQmlDebugService::engineAboutToBeAdded(engine);
703 QV4::ExecutionEngine *executionEngine = engine->handle();
704 const auto debuggersCopy = m_debuggers;
705 for (NativeDebugger *debugger : debuggersCopy) {
706 if (debugger->engine() == executionEngine)
707 m_debuggers.removeAll(debugger);
710 QQmlDebugService::engineAboutToBeRemoved(engine);
715 if (state == Enabled) {
716 for (NativeDebugger *debugger : std::as_const(m_debuggers)) {
717 QV4::ExecutionEngine *engine = debugger->engine();
718 if (!engine->debugger())
719 engine->setDebugger(debugger);
722 QQmlDebugService::stateAboutToBeChanged(state);
728 QJsonObject request = QJsonDocument::fromJson(message).object();
729 QJsonObject response;
730 QJsonObject arguments = request.value(QLatin1String(
"arguments")).toObject();
731 QString cmd = request.value(QLatin1String(
"command")).toString();
733 if (cmd == QLatin1String(
"setbreakpoint")) {
734 m_breakHandler->handleSetBreakpoint(&response, arguments);
735 }
else if (cmd == QLatin1String(
"removebreakpoint")) {
736 m_breakHandler->handleRemoveBreakpoint(&response, arguments);
737 }
else if (cmd == QLatin1String(
"echo")) {
738 response.insert(QStringLiteral(
"result"), arguments);
740 for (NativeDebugger *debugger : std::as_const(m_debuggers))
742 debugger->handleCommand(&response, cmd, arguments);
745 doc.setObject(response);
746 QByteArray ba = doc.toJson(QJsonDocument::Compact);
747 TRACE_PROTOCOL(
"Sending synchronous response:" << ba.constData() << endl);
748 emit messageToClient(s_key, ba);
754 doc.setObject(message);
755 QByteArray ba = doc.toJson(QJsonDocument::Compact);
756 TRACE_PROTOCOL(
"Sending asynchronous message:" << ba.constData() << endl);
757 emit messageToClient(s_key, ba);
void setBreakOnThrow(bool onoff)
void removeBreakPoint(int id)
void enableBreakPoint(int id, bool onoff)
QVector< BreakPoint > m_breakPoints
void handleRemoveBreakpoint(QJsonObject *response, const QJsonObject &arguments)
void handleSetBreakpoint(QJsonObject *response, const QJsonObject &arguments)
Collector(QV4::ExecutionEngine *engine)
void collect(QJsonArray *output, const QString &parentIName, const QString &name, const QV4::Value &value)
QV4::ExecutionEngine * m_engine
bool isExpanded(const QString &iname) const
void maybeBreakAtInstruction() override
void aboutToThrow() override
void handleCommand(QJsonObject *response, const QString &cmd, const QJsonObject &arguments)
QV4::ExecutionEngine * engine() const
NativeDebugger(QQmlNativeDebugServiceImpl *service, QV4::ExecutionEngine *engine)
void leavingFunction(const QV4::ReturnedValue &retVal) override
void enteringFunction() override
bool pauseAtNextOpportunity() const override
void signalEmitted(const QString &signal)
void stateAboutToBeChanged(State state) override
void engineAboutToBeRemoved(QJSEngine *engine) override
void engineAboutToBeAdded(QJSEngine *engine) override
void emitAsynchronousMessageToClient(const QJsonObject &message)
QQmlNativeDebugServiceImpl(QObject *parent)
void messageReceived(const QByteArray &message) override
~QQmlNativeDebugServiceImpl() override
static void setError(QJsonObject *response, const QString &msg)
static QString encodeFrame(QV4::CppStackFrame *f)
size_t qHash(const BreakPoint &b, size_t seed=0) noexcept
bool operator==(const BreakPoint &a, const BreakPoint &b)
static void decodeFrame(const QString &f, QV4::CppStackFrame **frame)
#define TRACE_PROTOCOL(x)