7#include <private/qqmldebugconnector_p.h>
8#include <private/qqmldebugserviceinterfaces_p.h>
9#include <private/qv4debugging_p.h>
10#include <private/qv4debugging_p.h>
11#include <private/qv4debuginstructions_p.h>
12#include <private/qv4engine_p.h>
13#include <private/qv4identifierhash_p.h>
14#include <private/qv4identifiertable_p.h>
15#include <private/qv4objectiterator_p.h>
16#include <private/qv4runtime_p.h>
17#include <private/qv4script_p.h>
18#include <private/qv4stackframe_p.h>
19#include <private/qv4string_p.h>
20#include <private/qversionedpacket_p.h>
22#include <QtQml/qjsengine.h>
23#include <QtCore/qjsonarray.h>
24#include <QtCore/qjsondocument.h>
25#include <QtCore/qjsonobject.h>
26#include <QtCore/qjsonvalue.h>
27#include <QtCore/qvector.h>
28#include <QtCore/qpointer.h>
31#define TRACE_PROTOCOL(s)
35using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
55 return qHash(b.fileName, seed) ^ b.lineNumber;
65static void setError(QJsonObject *response,
const QString &msg)
67 response->insert(QStringLiteral(
"type"), QStringLiteral(
"error"));
68 response->insert(QStringLiteral(
"msg"), msg);
80 void collect(QJsonArray *output,
const QString &parentIName,
const QString &name,
81 const QV4::Value &value);
83 bool isExpanded(
const QString &iname)
const {
return m_expanded.contains(iname); }
114 QString type = arguments.value(QLatin1String(
"type")).toString();
116 QString fileName = arguments.value(QLatin1String(
"file")).toString();
117 if (fileName.isEmpty()) {
118 setError(response, QStringLiteral(
"breakpoint has no file name"));
122 int line = arguments.value(QLatin1String(
"line")).toInt(-1);
124 setError(response, QStringLiteral(
"breakpoint has an invalid line number"));
130 bp.fileName = fileName.mid(fileName.lastIndexOf(
'/') + 1);
132 bp
.enabled = arguments.value(QLatin1String(
"enabled")).toBool(
true);
133 bp.condition = arguments.value(QLatin1String(
"condition")).toString();
134 bp
.ignoreCount = arguments.value(QLatin1String(
"ignorecount")).toInt();
135 m_breakPoints.append(bp);
139 response->insert(QStringLiteral(
"type"), type);
140 response->insert(QStringLiteral(
"breakpoint"), bp
.id);
145 int id = arguments.value(QLatin1String(
"id")).toInt();
147 response->insert(QStringLiteral(
"id"), id);
160 return m_pauseRequested
162 || m_stepping >= StepOver;
171 void handleCommand(QJsonObject *response,
const QString &cmd,
const QJsonObject &arguments);
174 void handleBacktrace(QJsonObject *response,
const QJsonObject &arguments);
175 void handleVariables(QJsonObject *response,
const QJsonObject &arguments);
176 void handleExpressions(QJsonObject *response,
const QJsonObject &arguments);
178 void handleDebuggerDeleted(QObject *debugger);
180 QV4::ReturnedValue evaluateExpression(
const QString &expression);
181 bool checkCondition(
const QString &expression);
183 QStringList breakOnSignals;
194 void handleContinue(QJsonObject *reponse, Speed speed);
196 QV4::Function *getFunction()
const;
198 bool reallyHitTheBreakPoint(
const QV4::Function *function,
int lineNumber);
200 QV4::ExecutionEngine *m_engine;
202 QV4::CppStackFrame *m_currentFrame =
nullptr;
204 bool m_pauseRequested;
207 QV4::PersistentValue m_returnedValue;
212 QV4::Scope scope(m_engine);
213 QV4::ScopedValue r(scope, evaluateExpression(expression));
214 return r->booleanValue();
217QV4::ReturnedValue
NativeDebugger::evaluateExpression(
const QString &expression)
219 QV4::Scope scope(m_engine);
222 QV4::ExecutionContext *ctx = m_engine->currentStackFrame ? m_engine->currentContext()
223 : m_engine->scriptContext();
225 QV4::Script script(ctx, QV4::Compiler::ContextType::Eval, expression);
226 if (
const QV4::Function *function = m_engine->currentStackFrame
227 ? m_engine->currentStackFrame->v4Function : m_engine->globalCode) {
228 script.setStrictMode(function->isStrict());
232 script.setInheritContext();
234 if (!m_engine->hasException) {
235 if (m_engine->currentStackFrame) {
236 QV4::ScopedValue thisObject(scope, m_engine->currentStackFrame->thisObject());
237 script.run(thisObject);
243 m_runningJob =
false;
244 return QV4::Encode::undefined();
250 m_stepping = NotStepping;
251 m_pauseRequested =
false;
252 m_runningJob =
false;
266 QString signalName = signal.left(signal.indexOf(QLatin1Char(
'('))).toLower();
268 for (
const QString &signal : std::as_const(breakOnSignals)) {
269 if (signal == signalName) {
277 const QJsonObject &arguments)
279 if (cmd == QLatin1String(
"backtrace"))
280 handleBacktrace(response, arguments);
281 else if (cmd == QLatin1String(
"variables"))
282 handleVariables(response, arguments);
283 else if (cmd == QLatin1String(
"expressions"))
284 handleExpressions(response, arguments);
285 else if (cmd == QLatin1String(
"stepin"))
286 handleContinue(response, StepIn);
287 else if (cmd == QLatin1String(
"stepout"))
288 handleContinue(response, StepOut);
289 else if (cmd == QLatin1String(
"stepover"))
290 handleContinue(response, StepOver);
291 else if (cmd == QLatin1String(
"continue"))
292 handleContinue(response, NotStepping);
299 return QString::fromLatin1(ds.data().toHex());
302static void decodeFrame(
const QString &f, QV4::CppStackFrame **frame)
305 QQmlDebugPacket ds(QByteArray::fromHex(f.toLatin1()));
307 *frame =
reinterpret_cast<QV4::CppStackFrame *>(rawFrame);
310void NativeDebugger::handleBacktrace(QJsonObject *response,
const QJsonObject &arguments)
312 int limit = arguments.value(QLatin1String(
"limit")).toInt(0);
314 QJsonArray frameArray;
315 QV4::CppStackFrame *f= m_engine->currentStackFrame;
316 for (
int i = 0; i < limit && f; ++i) {
317 QV4::Function *function = f->v4Function;
320 frame.insert(QStringLiteral(
"language"), QStringLiteral(
"js"));
321 frame.insert(QStringLiteral(
"context"), encodeFrame(f));
323 if (QV4::Heap::String *functionName = function->name())
324 frame.insert(QStringLiteral(
"function"), functionName->toQString());
325 frame.insert(QStringLiteral(
"file"), function->sourceFile());
327 int line = f->lineNumber();
328 frame.insert(QStringLiteral(
"line"), (line < 0 ? -line : line));
330 frameArray.push_back(frame);
332 f = f->parentFrame();
335 response->insert(QStringLiteral(
"frames"), frameArray);
339 const QV4::Value &value)
342 QV4::Scope scope(m_engine);
344 QString nonEmptyName = name.isEmpty() ? QString::fromLatin1(
"@%1").arg(m_anonCount++) : name;
345 QString iname = parentIName + QLatin1Char(
'.') + nonEmptyName;
346 dict.insert(QStringLiteral(
"iname"), iname);
347 dict.insert(QStringLiteral(
"name"), nonEmptyName);
349 QV4::ScopedValue typeString(scope, QV4::Runtime::TypeofValue::call(m_engine, value));
350 dict.insert(QStringLiteral(
"type"), typeString->toQStringNoThrow());
352 switch (value.type()) {
353 case QV4::Value::Empty_Type:
354 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"empty"));
355 dict.insert(QStringLiteral(
"haschild"),
false);
357 case QV4::Value::Undefined_Type:
358 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"undefined"));
359 dict.insert(QStringLiteral(
"haschild"),
false);
361 case QV4::Value::Null_Type:
362 dict.insert(QStringLiteral(
"type"), QStringLiteral(
"object"));
363 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"null"));
364 dict.insert(QStringLiteral(
"haschild"),
false);
366 case QV4::Value::Boolean_Type:
367 dict.insert(QStringLiteral(
"value"), value.booleanValue());
368 dict.insert(QStringLiteral(
"haschild"),
false);
370 case QV4::Value::Managed_Type:
371 if (
const QV4::String *string = value.as<QV4::String>()) {
372 dict.insert(QStringLiteral(
"value"), string->toQStringNoThrow());
373 dict.insert(QStringLiteral(
"haschild"),
false);
374 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"utf16"));
375 dict.insert(QStringLiteral(
"quoted"),
true);
376 }
else if (
const QV4::ArrayObject *array = value.as<QV4::ArrayObject>()) {
379 const uint n = array->getLength();
380 dict.insert(QStringLiteral(
"value"), qint64(n));
381 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"itemcount"));
382 dict.insert(QStringLiteral(
"haschild"), qint64(n));
383 if (isExpanded(iname)) {
385 for (uint i = 0; i < n; ++i) {
386 QV4::ReturnedValue v = array->get(i);
387 QV4::ScopedValue sval(scope, v);
388 collect(&children, iname, QString::number(i), *sval);
390 dict.insert(QStringLiteral(
"children"), children);
392 }
else if (
const QV4::Object *object = value.as<QV4::Object>()) {
394 bool expanded = isExpanded(iname);
395 qint64 numProperties = 0;
396 QV4::ObjectIterator it(scope, object, QV4::ObjectIterator::EnumerableOnly);
397 QV4::ScopedProperty p(scope);
398 QV4::ScopedPropertyKey name(scope);
400 QV4::PropertyAttributes attrs;
401 name = it.next(p, &attrs);
402 if (!name->isValid())
404 if (name->isStringOrSymbol()) {
407 QV4::Value v = p.property->value;
408 collect(&children, iname, name->toQString(), v);
412 dict.insert(QStringLiteral(
"value"), numProperties);
413 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"itemcount"));
414 dict.insert(QStringLiteral(
"haschild"), numProperties > 0);
416 dict.insert(QStringLiteral(
"children"), children);
419 case QV4::Value::Integer_Type:
420 dict.insert(QStringLiteral(
"value"), value.integerValue());
421 dict.insert(QStringLiteral(
"haschild"),
false);
424 dict.insert(QStringLiteral(
"value"), value.doubleValue());
425 dict.insert(QStringLiteral(
"haschild"),
false);
431void NativeDebugger::handleVariables(QJsonObject *response,
const QJsonObject &arguments)
434 QV4::CppStackFrame *frame =
nullptr;
435 decodeFrame(arguments.value(QLatin1String(
"context")).toString(), &frame);
437 setError(response, QStringLiteral(
"No stack frame passed"));
442 QV4::ExecutionEngine *engine = frame->v4Function->internalClass->engine;
444 setError(response, QStringLiteral(
"No execution engine passed"));
450 const QJsonArray expanded = arguments.value(QLatin1String(
"expanded")).toArray();
451 for (
const QJsonValue ex : expanded)
452 collector.m_expanded.append(ex.toString());
456 QV4::Scope scope(engine);
458 QV4::ScopedValue thisObject(scope, frame->thisObject());
459 collector.collect(&output, QString(), QStringLiteral(
"this"), thisObject);
460 QV4::Scoped<QV4::CallContext> callContext(scope, frame->callContext());
462 QV4::Heap::InternalClass *ic = callContext->internalClass();
463 QV4::ScopedValue v(scope);
464 for (uint i = 0; i < ic->size; ++i) {
465 QV4::ScopedValue stringOrSymbol(scope, ic->keyAt(i));
466 QV4::ScopedString propName(scope, stringOrSymbol->toString(scope.engine));
467 v = callContext->getProperty(propName);
468 collector.collect(&output, QString(), propName->toQString(), v);
472 response->insert(QStringLiteral(
"variables"), output);
475void NativeDebugger::handleExpressions(QJsonObject *response,
const QJsonObject &arguments)
478 QV4::CppStackFrame *frame =
nullptr;
479 decodeFrame(arguments.value(QLatin1String(
"context")).toString(), &frame);
481 setError(response, QStringLiteral(
"No stack frame passed"));
486 QV4::ExecutionEngine *engine = frame->v4Function->internalClass->engine;
488 setError(response, QStringLiteral(
"No execution engine passed"));
494 const QJsonArray expanded = arguments.value(QLatin1String(
"expanded")).toArray();
495 for (
const QJsonValue ex : expanded)
496 collector.m_expanded.append(ex.toString());
500 QV4::Scope scope(engine);
502 const QJsonArray expressions = arguments.value(QLatin1String(
"expressions")).toArray();
503 for (
const QJsonValue expr : expressions) {
504 QString expression = expr.toObject().value(QLatin1String(
"expression")).toString();
505 QString name = expr.toObject().value(QLatin1String(
"name")).toString();
509 QV4::ScopedValue result(scope, evaluateExpression(expression));
511 m_runningJob =
false;
512 if (result->isUndefined()) {
514 dict.insert(QStringLiteral(
"name"), name);
515 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"undefined"));
517 }
else if (result.ptr && result.ptr->rawValue()) {
518 collector.collect(&output, QString(), name, *result);
521 dict.insert(QStringLiteral(
"name"), name);
522 dict.insert(QStringLiteral(
"valueencoded"), QStringLiteral(
"notaccessible"));
526 engine->hasException =
false;
529 response->insert(QStringLiteral(
"expressions"), output);
534 for (
int i = 0; i != m_breakPoints.size(); ++i) {
535 if (m_breakPoints.at(i).id == id) {
536 m_breakPoints.remove(i);
545 m_breakPoints[id].enabled = enabled;
550 m_pauseRequested =
true;
553void NativeDebugger::handleContinue(QJsonObject *response, Speed speed)
557 if (!m_returnedValue.isUndefined())
558 m_returnedValue.set(m_engine, QV4::Encode::undefined());
560 m_currentFrame = m_engine->currentStackFrame;
569 if (m_stepping == StepOver) {
570 if (m_currentFrame == m_engine->currentStackFrame)
575 if (m_stepping == StepIn) {
580 if (m_pauseRequested) {
581 m_pauseRequested =
false;
587 if (QV4::Function *function = getFunction()) {
589 const int lineNumber = m_engine->currentStackFrame->lineNumber();
590 if (reallyHitTheBreakPoint(function, lineNumber))
601 if (m_stepping == StepIn) {
602 m_currentFrame = m_engine->currentStackFrame;
611 if (m_stepping != NotStepping && m_currentFrame == m_engine->currentStackFrame) {
612 m_currentFrame = m_currentFrame->parentFrame();
613 m_stepping = StepOver;
614 m_returnedValue.set(m_engine, retVal);
628 event.insert(QStringLiteral(
"event"), QStringLiteral(
"exception"));
629 m_service->emitAsynchronousMessageToClient(event);
634 return &QV4::Moth::DebugInstructions::insert;
639 if (m_engine->currentStackFrame)
640 return m_engine->currentStackFrame->v4Function;
642 return m_engine->globalCode;
649 event.insert(QStringLiteral(
"event"), QStringLiteral(
"break"));
650 event.insert(QStringLiteral(
"language"), QStringLiteral(
"js"));
651 if (QV4::CppStackFrame *frame = m_engine->currentStackFrame) {
652 QV4::Function *function = frame->v4Function;
653 event.insert(QStringLiteral(
"file"), function->sourceFile());
654 int line = frame->lineNumber();
655 event.insert(QStringLiteral(
"line"), (line < 0 ? -line : line));
658 m_service->emitAsynchronousMessageToClient(event);
661bool NativeDebugger::reallyHitTheBreakPoint(
const QV4::Function *function,
int lineNumber)
663 for (
int i = 0, n = m_service->m_breakHandler->m_breakPoints.size(); i != n; ++i) {
664 const BreakPoint &bp = m_service->m_breakHandler->m_breakPoints.at(i);
666 const QString base = QUrl(function->sourceFile()).fileName();
667 if (bp.fileName.endsWith(base)) {
668 if (bp.condition.isEmpty() || checkCondition(bp.condition)) {
669 BreakPoint &mbp = m_service->m_breakHandler->m_breakPoints[i];
688 delete m_breakHandler;
695 QV4::ExecutionEngine *ee = engine->handle();
699 if (state() == Enabled)
700 ee->setDebugger(debugger);
701 m_debuggers.append(QPointer<NativeDebugger>(debugger));
704 QQmlDebugService::engineAboutToBeAdded(engine);
711 QV4::ExecutionEngine *executionEngine = engine->handle();
712 const auto debuggersCopy = m_debuggers;
713 for (NativeDebugger *debugger : debuggersCopy) {
714 if (debugger->engine() == executionEngine) {
715 m_debuggers.removeAll(debugger);
716 if (executionEngine->debugger() != debugger)
721 QQmlDebugService::engineAboutToBeRemoved(engine);
726 if (state == Enabled) {
727 for (NativeDebugger *debugger : std::as_const(m_debuggers)) {
728 QV4::ExecutionEngine *engine = debugger->engine();
729 if (!engine->debugger())
730 engine->setDebugger(debugger);
733 QQmlDebugService::stateAboutToBeChanged(state);
739 QJsonObject request = QJsonDocument::fromJson(message).object();
740 QJsonObject response;
741 QJsonObject arguments = request.value(QLatin1String(
"arguments")).toObject();
742 QString cmd = request.value(QLatin1String(
"command")).toString();
744 if (cmd == QLatin1String(
"setbreakpoint")) {
745 m_breakHandler->handleSetBreakpoint(&response, arguments);
746 }
else if (cmd == QLatin1String(
"removebreakpoint")) {
747 m_breakHandler->handleRemoveBreakpoint(&response, arguments);
748 }
else if (cmd == QLatin1String(
"echo")) {
749 response.insert(QStringLiteral(
"result"), arguments);
751 for (NativeDebugger *debugger : std::as_const(m_debuggers))
753 debugger->handleCommand(&response, cmd, arguments);
756 doc.setObject(response);
757 QByteArray ba = doc.toJson(QJsonDocument::Compact);
758 TRACE_PROTOCOL(
"Sending synchronous response:" << ba.constData() << endl);
759 emit messageToClient(s_key, ba);
765 doc.setObject(message);
766 QByteArray ba = doc.toJson(QJsonDocument::Compact);
767 TRACE_PROTOCOL(
"Sending asynchronous message:" << ba.constData() << endl);
768 emit messageToClient(s_key, ba);
void setBreakOnThrow(bool onoff)
void removeBreakPoint(int id)
void enableBreakPoint(int id, bool onoff)
QList< 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
QV4::Moth::DebugInstructionPass debugInstructionPass() const 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
Combined button and popup list for selecting options.
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)