Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlnativedebugservice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6
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>
21
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>
29
30//#define TRACE_PROTOCOL(s) qDebug() << s
31#define TRACE_PROTOCOL(s)
32
34
35using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
36
38{
39public:
40 BreakPoint() : id(-1), lineNumber(-1), enabled(false), ignoreCount(0), hitCount(0) {}
41 bool isValid() const { return lineNumber >= 0 && !fileName.isEmpty(); }
42
43 int id;
46 bool enabled;
49
51};
52
53inline size_t qHash(const BreakPoint &b, size_t seed = 0) noexcept
54{
55 return qHash(b.fileName, seed) ^ b.lineNumber;
56}
57
58inline bool operator==(const BreakPoint &a, const BreakPoint &b)
59{
60 return a.lineNumber == b.lineNumber && a.fileName == b.fileName
61 && a.enabled == b.enabled && a.condition == b.condition
63}
64
65static void setError(QJsonObject *response, const QString &msg)
66{
67 response->insert(QStringLiteral("type"), QStringLiteral("error"));
68 response->insert(QStringLiteral("msg"), msg);
69}
70
71class NativeDebugger;
72
74{
75public:
76 Collector(QV4::ExecutionEngine *engine)
78 {}
79
80 void collect(QJsonArray *output, const QString &parentIName, const QString &name,
81 const QV4::Value &value);
82
83 bool isExpanded(const QString &iname) const { return m_expanded.contains(iname); }
84
85public:
89};
90
91// Encapsulate Breakpoint handling
92// Could be made per-NativeDebugger (i.e. per execution engine, if needed)
94{
95public:
97
98 void handleSetBreakpoint(QJsonObject *response, const QJsonObject &arguments);
99 void handleRemoveBreakpoint(QJsonObject *response, const QJsonObject &arguments);
100
101 void removeBreakPoint(int id);
102 void enableBreakPoint(int id, bool onoff);
103
104 void setBreakOnThrow(bool onoff);
109};
110
111void BreakPointHandler::handleSetBreakpoint(QJsonObject *response, const QJsonObject &arguments)
112{
113 TRACE_PROTOCOL("SET BREAKPOINT" << arguments);
114 QString type = arguments.value(QLatin1String("type")).toString();
115
116 QString fileName = arguments.value(QLatin1String("file")).toString();
117 if (fileName.isEmpty()) {
118 setError(response, QStringLiteral("breakpoint has no file name"));
119 return;
120 }
121
122 int line = arguments.value(QLatin1String("line")).toInt(-1);
123 if (line < 0) {
124 setError(response, QStringLiteral("breakpoint has an invalid line number"));
125 return;
126 }
127
128 BreakPoint bp;
129 bp.id = m_lastBreakpoint++;
130 bp.fileName = fileName.mid(fileName.lastIndexOf('/') + 1);
131 bp.lineNumber = line;
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);
136
137 m_haveBreakPoints = true;
138
139 response->insert(QStringLiteral("type"), type);
140 response->insert(QStringLiteral("breakpoint"), bp.id);
141}
142
143void BreakPointHandler::handleRemoveBreakpoint(QJsonObject *response, const QJsonObject &arguments)
144{
145 int id = arguments.value(QLatin1String("id")).toInt();
147 response->insert(QStringLiteral("id"), id);
148}
149
151{
152public:
153 NativeDebugger(QQmlNativeDebugServiceImpl *service, QV4::ExecutionEngine *engine);
154
155 void signalEmitted(const QString &signal);
156
157 QV4::ExecutionEngine *engine() const { return m_engine; }
158
160 return m_pauseRequested
161 || m_service->m_breakHandler->m_haveBreakPoints
162 || m_stepping >= StepOver;
163 }
164
167 void leavingFunction(const QV4::ReturnedValue &retVal) override;
170
171 void handleCommand(QJsonObject *response, const QString &cmd, const QJsonObject &arguments);
172
173private:
174 void handleBacktrace(QJsonObject *response, const QJsonObject &arguments);
175 void handleVariables(QJsonObject *response, const QJsonObject &arguments);
176 void handleExpressions(QJsonObject *response, const QJsonObject &arguments);
177
178 void handleDebuggerDeleted(QObject *debugger);
179
180 QV4::ReturnedValue evaluateExpression(const QString &expression);
181 bool checkCondition(const QString &expression);
182
183 QStringList breakOnSignals;
184
185 enum Speed {
186 NotStepping = 0,
187 StepOut,
188 StepOver,
189 StepIn,
190 };
191
192 void pauseAndWait();
193 void pause();
194 void handleContinue(QJsonObject *reponse, Speed speed);
195
196 QV4::Function *getFunction() const;
197
198 bool reallyHitTheBreakPoint(const QV4::Function *function, int lineNumber);
199
200 QV4::ExecutionEngine *m_engine;
202 QV4::CppStackFrame *m_currentFrame = nullptr;
203 Speed m_stepping;
204 bool m_pauseRequested;
205 bool m_runningJob;
206
207 QV4::PersistentValue m_returnedValue;
208};
209
210bool NativeDebugger::checkCondition(const QString &expression)
211{
212 QV4::Scope scope(m_engine);
213 QV4::ScopedValue r(scope, evaluateExpression(expression));
214 return r->booleanValue();
215}
216
217QV4::ReturnedValue NativeDebugger::evaluateExpression(const QString &expression)
218{
219 QV4::Scope scope(m_engine);
220 m_runningJob = true;
221
222 QV4::ExecutionContext *ctx = m_engine->currentStackFrame ? m_engine->currentContext()
223 : m_engine->scriptContext();
224
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());
229 }
230 // In order for property lookups in QML to work, we need to disable fast v4 lookups.
231 // That is a side-effect of inheritContext.
232 script.setInheritContext();
233 script.parse();
234 if (!m_engine->hasException) {
235 if (m_engine->currentStackFrame) {
236 QV4::ScopedValue thisObject(scope, m_engine->currentStackFrame->thisObject());
237 script.run(thisObject);
238 } else {
239 script.run();
240 }
241 }
242
243 m_runningJob = false;
244 return QV4::Encode::undefined();
245}
246
247NativeDebugger::NativeDebugger(QQmlNativeDebugServiceImpl *service, QV4::ExecutionEngine *engine)
249{
250 m_stepping = NotStepping;
251 m_pauseRequested = false;
252 m_runningJob = false;
253 m_service = service;
254 m_engine = engine;
255 TRACE_PROTOCOL("Creating native debugger");
256}
257
258void NativeDebugger::signalEmitted(const QString &signal)
259{
260 //This function is only called by QQmlBoundSignal
261 //only if there is a slot connected to the signal. Hence, there
262 //is no need for additional check.
263
264 //Parse just the name and remove the class info
265 //Normalize to Lower case.
266 QString signalName = signal.left(signal.indexOf(QLatin1Char('('))).toLower();
267
268 for (const QString &signal : std::as_const(breakOnSignals)) {
269 if (signal == signalName) {
270 // TODO: pause debugger
271 break;
272 }
273 }
274}
275
276void NativeDebugger::handleCommand(QJsonObject *response, const QString &cmd,
277 const QJsonObject &arguments)
278{
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);
293}
294
295static QString encodeFrame(QV4::CppStackFrame *f)
296{
297 QQmlDebugPacket ds;
298 ds << quintptr(f);
299 return QString::fromLatin1(ds.data().toHex());
300}
301
302static void decodeFrame(const QString &f, QV4::CppStackFrame **frame)
303{
304 quintptr rawFrame;
305 QQmlDebugPacket ds(QByteArray::fromHex(f.toLatin1()));
306 ds >> rawFrame;
307 *frame = reinterpret_cast<QV4::CppStackFrame *>(rawFrame);
308}
309
310void NativeDebugger::handleBacktrace(QJsonObject *response, const QJsonObject &arguments)
311{
312 int limit = arguments.value(QLatin1String("limit")).toInt(0);
313
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;
318
319 QJsonObject frame;
320 frame.insert(QStringLiteral("language"), QStringLiteral("js"));
321 frame.insert(QStringLiteral("context"), encodeFrame(f));
322
323 if (QV4::Heap::String *functionName = function->name())
324 frame.insert(QStringLiteral("function"), functionName->toQString());
325 frame.insert(QStringLiteral("file"), function->sourceFile());
326
327 int line = f->lineNumber();
328 frame.insert(QStringLiteral("line"), (line < 0 ? -line : line));
329
330 frameArray.push_back(frame);
331
332 f = f->parentFrame();
333 }
334
335 response->insert(QStringLiteral("frames"), frameArray);
336}
337
338void Collector::collect(QJsonArray *out, const QString &parentIName, const QString &name,
339 const QV4::Value &value)
340{
341 QJsonObject dict;
342 QV4::Scope scope(m_engine);
343
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);
348
349 QV4::ScopedValue typeString(scope, QV4::Runtime::TypeofValue::call(m_engine, value));
350 dict.insert(QStringLiteral("type"), typeString->toQStringNoThrow());
351
352 switch (value.type()) {
353 case QV4::Value::Empty_Type:
354 dict.insert(QStringLiteral("valueencoded"), QStringLiteral("empty"));
355 dict.insert(QStringLiteral("haschild"), false);
356 break;
357 case QV4::Value::Undefined_Type:
358 dict.insert(QStringLiteral("valueencoded"), QStringLiteral("undefined"));
359 dict.insert(QStringLiteral("haschild"), false);
360 break;
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);
365 break;
366 case QV4::Value::Boolean_Type:
367 dict.insert(QStringLiteral("value"), value.booleanValue());
368 dict.insert(QStringLiteral("haschild"), false);
369 break;
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>()) {
377 // The size of an array is number of its numerical properties.
378 // We don't consider free form object properties here.
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)) {
384 QJsonArray children;
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);
389 }
390 dict.insert(QStringLiteral("children"), children);
391 }
392 } else if (const QV4::Object *object = value.as<QV4::Object>()) {
393 QJsonArray children;
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);
399 while (true) {
400 QV4::PropertyAttributes attrs;
401 name = it.next(p, &attrs);
402 if (!name->isValid())
403 break;
404 if (name->isStringOrSymbol()) {
405 ++numProperties;
406 if (expanded) {
407 QV4::Value v = p.property->value;
408 collect(&children, iname, name->toQString(), v);
409 }
410 }
411 }
412 dict.insert(QStringLiteral("value"), numProperties);
413 dict.insert(QStringLiteral("valueencoded"), QStringLiteral("itemcount"));
414 dict.insert(QStringLiteral("haschild"), numProperties > 0);
415 if (expanded)
416 dict.insert(QStringLiteral("children"), children);
417 }
418 break;
419 case QV4::Value::Integer_Type:
420 dict.insert(QStringLiteral("value"), value.integerValue());
421 dict.insert(QStringLiteral("haschild"), false);
422 break;
423 default: // double
424 dict.insert(QStringLiteral("value"), value.doubleValue());
425 dict.insert(QStringLiteral("haschild"), false);
426 }
427
428 out->append(dict);
429}
430
431void NativeDebugger::handleVariables(QJsonObject *response, const QJsonObject &arguments)
432{
433 TRACE_PROTOCOL("Build variables");
434 QV4::CppStackFrame *frame = nullptr;
435 decodeFrame(arguments.value(QLatin1String("context")).toString(), &frame);
436 if (!frame) {
437 setError(response, QStringLiteral("No stack frame passed"));
438 return;
439 }
440 TRACE_PROTOCOL("Context: " << frame);
441
442 QV4::ExecutionEngine *engine = frame->v4Function->internalClass->engine;
443 if (!engine) {
444 setError(response, QStringLiteral("No execution engine passed"));
445 return;
446 }
447 TRACE_PROTOCOL("Engine: " << engine);
448
449 Collector collector(engine);
450 const QJsonArray expanded = arguments.value(QLatin1String("expanded")).toArray();
451 for (const QJsonValue ex : expanded)
452 collector.m_expanded.append(ex.toString());
453 TRACE_PROTOCOL("Expanded: " << collector.m_expanded);
454
455 QJsonArray output;
456 QV4::Scope scope(engine);
457
458 QV4::ScopedValue thisObject(scope, frame->thisObject());
459 collector.collect(&output, QString(), QStringLiteral("this"), thisObject);
460 QV4::Scoped<QV4::CallContext> callContext(scope, frame->callContext());
461 if (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);
469 }
470 }
471
472 response->insert(QStringLiteral("variables"), output);
473}
474
475void NativeDebugger::handleExpressions(QJsonObject *response, const QJsonObject &arguments)
476{
477 TRACE_PROTOCOL("Evaluate expressions");
478 QV4::CppStackFrame *frame = nullptr;
479 decodeFrame(arguments.value(QLatin1String("context")).toString(), &frame);
480 if (!frame) {
481 setError(response, QStringLiteral("No stack frame passed"));
482 return;
483 }
484 TRACE_PROTOCOL("Context: " << executionContext);
485
486 QV4::ExecutionEngine *engine = frame->v4Function->internalClass->engine;
487 if (!engine) {
488 setError(response, QStringLiteral("No execution engine passed"));
489 return;
490 }
491 TRACE_PROTOCOL("Engines: " << engine << m_engine);
492
493 Collector collector(engine);
494 const QJsonArray expanded = arguments.value(QLatin1String("expanded")).toArray();
495 for (const QJsonValue ex : expanded)
496 collector.m_expanded.append(ex.toString());
497 TRACE_PROTOCOL("Expanded: " << collector.m_expanded);
498
499 QJsonArray output;
500 QV4::Scope scope(engine);
501
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();
506 TRACE_PROTOCOL("Evaluate expression: " << expression);
507 m_runningJob = true;
508
509 QV4::ScopedValue result(scope, evaluateExpression(expression));
510
511 m_runningJob = false;
512 if (result->isUndefined()) {
513 QJsonObject dict;
514 dict.insert(QStringLiteral("name"), name);
515 dict.insert(QStringLiteral("valueencoded"), QStringLiteral("undefined"));
516 output.append(dict);
517 } else if (result.ptr && result.ptr->rawValue()) {
518 collector.collect(&output, QString(), name, *result);
519 } else {
520 QJsonObject dict;
521 dict.insert(QStringLiteral("name"), name);
522 dict.insert(QStringLiteral("valueencoded"), QStringLiteral("notaccessible"));
523 output.append(dict);
524 }
525 TRACE_PROTOCOL("EXCEPTION: " << engine->hasException);
526 engine->hasException = false;
527 }
528
529 response->insert(QStringLiteral("expressions"), output);
530}
531
533{
534 for (int i = 0; i != m_breakPoints.size(); ++i) {
535 if (m_breakPoints.at(i).id == id) {
536 m_breakPoints.remove(i);
537 m_haveBreakPoints = !m_breakPoints.isEmpty();
538 return;
539 }
540 }
541}
542
543void BreakPointHandler::enableBreakPoint(int id, bool enabled)
544{
545 m_breakPoints[id].enabled = enabled;
546}
547
548void NativeDebugger::pause()
549{
550 m_pauseRequested = true;
551}
552
553void NativeDebugger::handleContinue(QJsonObject *response, Speed speed)
554{
555 Q_UNUSED(response);
556
557 if (!m_returnedValue.isUndefined())
558 m_returnedValue.set(m_engine, QV4::Encode::undefined());
559
560 m_currentFrame = m_engine->currentStackFrame;
561 m_stepping = speed;
562}
563
565{
566 if (m_runningJob) // do not re-enter when we're doing a job for the debugger.
567 return;
568
569 if (m_stepping == StepOver) {
570 if (m_currentFrame == m_engine->currentStackFrame)
571 pauseAndWait();
572 return;
573 }
574
575 if (m_stepping == StepIn) {
576 pauseAndWait();
577 return;
578 }
579
580 if (m_pauseRequested) { // Serve debugging requests from the agent
581 m_pauseRequested = false;
582 pauseAndWait();
583 return;
584 }
585
586 if (m_service->m_breakHandler->m_haveBreakPoints) {
587 if (QV4::Function *function = getFunction()) {
588 // lineNumber will be negative for Ret instructions, so those won't match
589 const int lineNumber = m_engine->currentStackFrame->lineNumber();
590 if (reallyHitTheBreakPoint(function, lineNumber))
591 pauseAndWait();
592 }
593 }
594}
595
597{
598 if (m_runningJob)
599 return;
600
601 if (m_stepping == StepIn) {
602 m_currentFrame = m_engine->currentStackFrame;
603 }
604}
605
606void NativeDebugger::leavingFunction(const QV4::ReturnedValue &retVal)
607{
608 if (m_runningJob)
609 return;
610
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);
615 }
616}
617
619{
620 if (!m_service->m_breakHandler->m_breakOnThrow)
621 return;
622
623 if (m_runningJob) // do not re-enter when we're doing a job for the debugger.
624 return;
625
626 QJsonObject event;
627 // TODO: complete this!
628 event.insert(QStringLiteral("event"), QStringLiteral("exception"));
629 m_service->emitAsynchronousMessageToClient(event);
630}
631
633{
634 return &QV4::Moth::DebugInstructions::insert;
635}
636
637QV4::Function *NativeDebugger::getFunction() const
638{
639 if (m_engine->currentStackFrame)
640 return m_engine->currentStackFrame->v4Function;
641 else
642 return m_engine->globalCode;
643}
644
645void NativeDebugger::pauseAndWait()
646{
647 QJsonObject event;
648
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));
656 }
657
658 m_service->emitAsynchronousMessageToClient(event);
659}
660
661bool NativeDebugger::reallyHitTheBreakPoint(const QV4::Function *function, int lineNumber)
662{
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);
665 if (bp.lineNumber == lineNumber) {
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];
670 ++mbp.hitCount;
671 if (mbp.hitCount > mbp.ignoreCount)
672 return true;
673 }
674 }
675 }
676 }
677 return false;
678}
679
682{
683 m_breakHandler = new BreakPointHandler;
684}
685
687{
688 delete m_breakHandler;
689}
690
692{
693 TRACE_PROTOCOL("Adding engine" << engine);
694 if (engine) {
695 QV4::ExecutionEngine *ee = engine->handle();
696 TRACE_PROTOCOL("Adding execution engine" << ee);
697 if (ee) {
698 NativeDebugger *debugger = new NativeDebugger(this, ee);
699 if (state() == Enabled)
700 ee->setDebugger(debugger);
701 m_debuggers.append(QPointer<NativeDebugger>(debugger));
702 }
703 }
704 QQmlDebugService::engineAboutToBeAdded(engine);
705}
706
708{
709 TRACE_PROTOCOL("Removing engine" << engine);
710 if (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)
717 delete debugger;
718 }
719 }
720 }
721 QQmlDebugService::engineAboutToBeRemoved(engine);
722}
723
724void QQmlNativeDebugServiceImpl::stateAboutToBeChanged(QQmlDebugService::State state)
725{
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);
731 }
732 }
733 QQmlDebugService::stateAboutToBeChanged(state);
734}
735
736void QQmlNativeDebugServiceImpl::messageReceived(const QByteArray &message)
737{
738 TRACE_PROTOCOL("Native message received: " << message);
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();
743
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);
750 } else {
751 for (NativeDebugger *debugger : std::as_const(m_debuggers))
752 if (debugger)
753 debugger->handleCommand(&response, cmd, arguments);
754 }
755 QJsonDocument doc;
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);
760}
761
763{
764 QJsonDocument doc;
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);
769}
770
771QT_END_NAMESPACE
void setBreakOnThrow(bool onoff)
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)
void messageReceived(const QByteArray &message) 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)