1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#define QT_NO_CAST_FROM_ASCII
#include "scriptengine.h"
#include "breakhandler.h"
#include "debuggerconstants.h"
#include "debuggerdialogs.h"
#include "debuggerstringutils.h"
#include "moduleshandler.h"
#include "registerhandler.h"
#include "stackhandler.h"
#include "watchhandler.h"
#include "watchutils.h"
#include <utils/qtcassert.h>
#include <texteditor/itexteditor.h>
#include <coreplugin/ifile.h>
#include <coreplugin/scriptmanager/scriptmanager.h>
#include <coreplugin/icore.h>
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QMessageBox>
#include <QtGui/QToolTip>
#include <QtScript/QScriptContext>
#include <QtScript/QScriptClassPropertyIterator>
#include <QtScript/QScriptContextInfo>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptEngineAgent>
#include <QtScript/QScriptValue>
#include <QtScript/QScriptValueIterator>
//#define DEBUG_SCRIPT 1
#if DEBUG_SCRIPT
# define SDEBUG(s) qDebug() << s
#else
# define SDEBUG(s)
#endif
# define XSDEBUG(s) qDebug() << s
namespace Debugger {
namespace Internal {
///////////////////////////////////////////////////////////////////////
//
// ScriptEngine
//
///////////////////////////////////////////////////////////////////////
class ScriptAgent : public QScriptEngineAgent
{
public:
ScriptAgent(ScriptEngine *debugger, QScriptEngine *script);
~ScriptAgent() {}
void contextPop();
void contextPush();
void exceptionCatch(qint64 scriptId, const QScriptValue &exception);
void exceptionThrow(qint64 scriptId, const QScriptValue & exception,
bool hasHandler);
void functionEntry(qint64 scriptId);
void functionExit(qint64 scriptId, const QScriptValue &returnValue);
void positionChange(qint64 scriptId, int lineNumber, int columnNumber);
void scriptLoad(qint64 id, const QString &program, const QString &fileName,
int baseLineNumber);
void scriptUnload(qint64 id);
void showMessage(const QString &msg);
private:
void maybeBreakNow(bool byFunction);
ScriptEngine *q;
int m_depth;
int m_contextDepth;
};
ScriptAgent::ScriptAgent(ScriptEngine *debugger, QScriptEngine *script)
: QScriptEngineAgent(script), q(debugger), m_depth(0), m_contextDepth(0)
{}
void ScriptAgent::showMessage(const QString &msg)
{
SDEBUG(msg);
q->showMessage(msg, LogMisc);
}
void ScriptAgent::contextPop()
{
//showMessage(_("ContextPop: %1").arg(m_contextDepth));
--m_contextDepth;
}
void ScriptAgent::contextPush()
{
++m_contextDepth;
//showMessage(_("ContextPush: %1 ").arg(m_contextDepth));
}
void ScriptAgent::exceptionCatch(qint64 scriptId, const QScriptValue & exception)
{
Q_UNUSED(scriptId)
Q_UNUSED(exception)
showMessage(_("An exception was caught on %1: '%2'").
arg(scriptId).arg(exception.toString()));
}
void ScriptAgent::exceptionThrow(qint64 scriptId, const QScriptValue &exception,
bool hasHandler)
{
Q_UNUSED(scriptId)
Q_UNUSED(exception)
Q_UNUSED(hasHandler)
showMessage(_("An exception occurred on %1: '%2'").
arg(scriptId).arg(exception.toString()));
}
void ScriptAgent::functionEntry(qint64 scriptId)
{
Q_UNUSED(scriptId)
++m_depth;
//showMessage(_("Function entry occurred on %1, depth: %2").arg(scriptId));
q->checkForBreakCondition(true);
}
void ScriptAgent::functionExit(qint64 scriptId, const QScriptValue &returnValue)
{
Q_UNUSED(scriptId)
Q_UNUSED(returnValue)
--m_depth;
//showMessage(_("Function exit occurred on %1: '%2'").
// arg(scriptId).arg(returnValue.toString()));
}
void ScriptAgent::positionChange(qint64 scriptId, int lineNumber, int columnNumber)
{
Q_UNUSED(scriptId)
Q_UNUSED(lineNumber)
Q_UNUSED(columnNumber)
//showMessage(_("Position: %1").arg(lineNumber));
q->checkForBreakCondition(false);
}
void ScriptAgent::scriptLoad(qint64 scriptId, const QString &program,
const QString &fileName, int baseLineNumber)
{
Q_UNUSED(scriptId)
Q_UNUSED(program)
Q_UNUSED(fileName)
Q_UNUSED(baseLineNumber)
showMessage(_("Loaded: %1 id: %2").arg(fileName).arg(scriptId));
}
void ScriptAgent::scriptUnload(qint64 scriptId)
{
Q_UNUSED(scriptId)
showMessage(_("Unload script id %1 ").arg(scriptId));
}
///////////////////////////////////////////////////////////////////////
//
// ScriptEngine
//
///////////////////////////////////////////////////////////////////////
ScriptEngine::ScriptEngine(const DebuggerStartParameters &startParameters)
: DebuggerEngine(startParameters)
{
}
ScriptEngine::~ScriptEngine()
{
}
void ScriptEngine::executeDebuggerCommand(const QString &command)
{
Q_UNUSED(command)
XSDEBUG("FIXME: ScriptEngine::executeDebuggerCommand()");
}
void ScriptEngine::shutdownInferior()
{
QTC_ASSERT(state() == InferiorShutdownRequested, qDebug() << state());
SDEBUG("ScriptEngine::shutdownInferior()");
m_scriptEngine->setAgent(0);
//m_scriptAgent.reset(0);
m_stopped = false;
m_stopOnNextLine = false;
if (m_scriptEngine->isEvaluating())
m_scriptEngine->abortEvaluation();
notifyInferiorShutdownOk();
}
void ScriptEngine::shutdownEngine()
{
QTC_ASSERT(state() == EngineShutdownRequested, qDebug() << state());
m_scriptEngine->setAgent(0);
notifyEngineShutdownOk();
}
void ScriptEngine::setupEngine()
{
QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
showMessage(_("STARTING SCRIPT DEBUGGER"), LogMisc);
if (m_scriptEngine.isNull())
m_scriptEngine = Core::ICore::instance()->scriptManager()->scriptEngine();
QTC_ASSERT(!m_scriptAgent, /**/);
m_scriptAgent.reset(new ScriptAgent(this, m_scriptEngine.data()));
m_scriptEngine->setAgent(m_scriptAgent.data());
//m_scriptEngine->setAgent(new ScriptAgent(this, m_scriptEngine.data()));
/* Keep the gui alive (have the engine call processEvents() while the script
* is run in the foreground). */
m_scriptEngine->setProcessEventsInterval(1 /*ms*/);
m_stopped = false;
m_stopOnNextLine = false;
m_scriptEngine->abortEvaluation();
notifyEngineSetupOk();
}
void ScriptEngine::setupInferior()
{
QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
m_scriptFileName = QFileInfo(startParameters().executable).absoluteFilePath();
showMessage(_("SCRIPT FILE: ") + m_scriptFileName);
QFile scriptFile(m_scriptFileName);
if (!scriptFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
showMessageBox(QMessageBox::Critical, tr("Error:"),
_("Cannot open script file %1:\n%2").
arg(m_scriptFileName, scriptFile.errorString()));
notifyInferiorSetupFailed();
return;
}
QTextStream stream(&scriptFile);
m_scriptContents = stream.readAll();
scriptFile.close();
attemptBreakpointSynchronization();
notifyInferiorSetupOk();
}
void ScriptEngine::continueInferior()
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
SDEBUG("ScriptEngine::continueInferior()");
m_stopped = false;
m_stopOnNextLine = false;
}
static const char *qtExtensionsC[] = {
"qt.core", "qt.gui", "qt.xml", "qt.svg", "qt.network",
"qt.sql", "qt.opengl", "qt.webkit", "qt.xmlpatterns", "qt.uitools"
};
void ScriptEngine::importExtensions()
{
SDEBUG("ScriptEngine::importExtensions()");
QStringList extensions;
const int extCount = sizeof(qtExtensionsC)/sizeof(const char *);
for (int e = 0; e < extCount; e++)
extensions.append(QLatin1String(qtExtensionsC[e]));
if (m_scriptEngine->importedExtensions().contains(extensions.front()))
return; // true;
QDir dir(QLatin1String("/home/apoenitz/dev/qtscriptgenerator"));
if (!dir.cd(QLatin1String("plugins"))) {
fprintf(stderr, "plugins folder does not exist -- did you build the bindings?\n");
return; // false;
}
QStringList paths = qApp->libraryPaths();
paths << dir.absolutePath();
qApp->setLibraryPaths(paths);
QStringList failExtensions;
foreach (const QString &ext, extensions) {
QScriptValue ret = m_scriptEngine->importExtension(ext);
if (ret.isError())
failExtensions.append(ext);
}
if (!failExtensions.isEmpty()) {
if (failExtensions.size() == extensions.size()) {
qWarning("Failed to import Qt bindings!\n"
"Plugins directory searched: %s/script\n"
"Make sure that the bindings have been built, "
"and that this executable and the plugins are "
"using compatible Qt libraries.", qPrintable(dir.absolutePath()));
} else {
qWarning("Failed to import some Qt bindings: %s\n"
"Plugins directory searched: %s/script\n"
"Make sure that the bindings have been built, "
"and that this executable and the plugins are "
"using compatible Qt libraries.",
qPrintable(failExtensions.join(QLatin1String(", "))), qPrintable(dir.absolutePath()));
}
}
return; // failExtensions.isEmpty();
}
void ScriptEngine::runEngine()
{
QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
SDEBUG("ScriptEngine::runEngine()");
notifyEngineRunAndInferiorRunOk();
showStatusMessage(tr("Running requested..."), 5000);
showMessage(QLatin1String("Running: ") + m_scriptFileName, LogMisc);
importExtensions();
const QScriptValue result = m_scriptEngine->evaluate(m_scriptContents, m_scriptFileName);
QString msg;
if (m_scriptEngine->hasUncaughtException()) {
msg = _("An exception occurred during execution at line: %1\n%2\n")
.arg(m_scriptEngine->uncaughtExceptionLineNumber())
.arg(m_scriptEngine->uncaughtException().toString());
msg += m_scriptEngine->uncaughtExceptionBacktrace()
.join(QString(QLatin1Char('\n')));
} else {
msg = _("Evaluation returns '%1'").arg(result.toString());
}
showMessage(msg, LogMisc);
showMessage(_("This was the outermost function."));
notifyInferiorExited();
}
void ScriptEngine::interruptInferior()
{
m_stopOnNextLine = true;
XSDEBUG("ScriptEngine::interruptInferior()");
}
void ScriptEngine::executeStep()
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
//SDEBUG("ScriptEngine::stepExec()");
m_stopped = false;
m_stopOnNextLine = true;
}
void ScriptEngine::executeStepI()
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
//SDEBUG("ScriptEngine::stepIExec()");
m_stopped = false;
m_stopOnNextLine = true;
}
void ScriptEngine::executeStepOut()
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
//SDEBUG("ScriptEngine::stepOutExec()");
m_stopped = false;
m_stopOnNextLine = true;
}
void ScriptEngine::executeNext()
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
//SDEBUG("ScriptEngine::nextExec()");
m_stopped = false;
m_stopOnNextLine = true;
}
void ScriptEngine::executeNextI()
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
//SDEBUG("ScriptEngine::nextIExec()");
m_stopped = false;
m_stopOnNextLine = true;
}
void ScriptEngine::executeRunToLine(const QString &fileName, int lineNumber)
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
Q_UNUSED(fileName)
Q_UNUSED(lineNumber)
SDEBUG("FIXME: ScriptEngine::runToLineExec()");
}
void ScriptEngine::executeRunToFunction(const QString &functionName)
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
Q_UNUSED(functionName)
XSDEBUG("FIXME: ScriptEngine::runToFunctionExec()");
}
void ScriptEngine::executeJumpToLine(const QString &fileName, int lineNumber)
{
QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
notifyInferiorRunRequested();
Q_UNUSED(fileName)
Q_UNUSED(lineNumber)
XSDEBUG("FIXME: ScriptEngine::jumpToLineExec()");
}
void ScriptEngine::activateFrame(int index)
{
Q_UNUSED(index)
}
void ScriptEngine::selectThread(int index)
{
Q_UNUSED(index)
}
void ScriptEngine::attemptBreakpointSynchronization()
{
BreakHandler *handler = breakHandler();
bool updateNeeded = false;
for (int index = 0; index != handler->size(); ++index) {
BreakpointData *data = handler->at(index);
if (data->pending) {
data->pending = false; // FIXME
updateNeeded = true;
}
if (data->bpNumber.isEmpty()) {
data->bpNumber = QByteArray::number(index + 1);
updateNeeded = true;
}
if (!data->fileName.isEmpty() && data->markerFileName().isEmpty()) {
data->setMarkerFileName(data->fileName);
data->setMarkerLineNumber(data->lineNumber.toInt());
updateNeeded = true;
}
}
if (updateNeeded)
handler->updateMarkers();
}
void ScriptEngine::loadSymbols(const QString &moduleName)
{
Q_UNUSED(moduleName)
}
void ScriptEngine::loadAllSymbols()
{
}
void ScriptEngine::reloadModules()
{
}
void ScriptEngine::requestModuleSymbols(const QString & /*moduleName*/)
{
}
//////////////////////////////////////////////////////////////////////
//
// Tooltip specific stuff
//
//////////////////////////////////////////////////////////////////////
static WatchData m_toolTip;
static QPoint m_toolTipPos;
static QHash<QString, WatchData> m_toolTipCache;
void ScriptEngine::setToolTipExpression(const QPoint &mousePos,
TextEditor::ITextEditor *editor, int cursorPos)
{
Q_UNUSED(mousePos)
Q_UNUSED(editor)
Q_UNUSED(cursorPos)
if (state() != InferiorStopOk) {
//SDEBUG("SUPPRESSING DEBUGGER TOOLTIP, INFERIOR NOT STOPPED");
return;
}
// Check mime type and get expression (borrowing some C++ - functions)
const QString javaScriptMimeType =
QLatin1String("application/javascript");
if (!editor->file() || editor->file()->mimeType() != javaScriptMimeType)
return;
int line;
int column;
QString exp = cppExpressionAt(editor, cursorPos, &line, &column);
/*
if (m_toolTipCache.contains(exp)) {
const WatchData & data = m_toolTipCache[exp];
q->watchHandler()->removeChildren(data.iname);
insertData(data);
return;
}
*/
QToolTip::hideText();
if (exp.isEmpty() || exp.startsWith(QLatin1Char('#'))) {
QToolTip::hideText();
return;
}
if (!hasLetterOrNumber(exp)) {
QToolTip::showText(m_toolTipPos, tr("'%1' contains no identifier").arg(exp));
return;
}
if (exp.startsWith(QLatin1Char('"')) && exp.endsWith(QLatin1Char('"'))) {
QToolTip::showText(m_toolTipPos, tr("String literal %1").arg(exp));
return;
}
if (exp.startsWith(QLatin1String("++")) || exp.startsWith(QLatin1String("--")))
exp.remove(0, 2);
if (exp.endsWith(QLatin1String("++")) || exp.endsWith(QLatin1String("--")))
exp.remove(0, 2);
if (exp.startsWith(QLatin1Char('<')) || exp.startsWith(QLatin1Char('[')))
return;
if (hasSideEffects(exp)) {
QToolTip::showText(m_toolTipPos,
tr("Cowardly refusing to evaluate expression '%1' "
"with potential side effects").arg(exp));
return;
}
#if 0
//if (m_manager->status() != InferiorStopOk)
// return;
// FIXME: 'exp' can contain illegal characters
m_toolTip = WatchData();
m_toolTip.exp = exp;
m_toolTip.name = exp;
m_toolTip.iname = tooltipIName;
insertData(m_toolTip);
#endif
}
//////////////////////////////////////////////////////////////////////
//
// Watch specific stuff
//
//////////////////////////////////////////////////////////////////////
void ScriptEngine::assignValueInDebugger(const QString &expression,
const QString &value)
{
SDEBUG("ASSIGNING: " << (expression + QLatin1Char('=') + value));
m_scriptEngine->evaluate(expression + QLatin1Char('=') + value);
updateLocals();
}
static BreakpointData *findBreakPointByFunction(BreakHandler *handler,
const QString &functionName)
{
const int count = handler->size();
for (int b = 0; b < count; b++) {
BreakpointData *data = handler->at(b);
if (data->funcName == functionName)
return data;
}
return 0;
}
static BreakpointData *findBreakPointByFileName(BreakHandler *handler,
int lineNumber,
const QString &fileName)
{
const int count = handler->size();
for (int b = 0; b < count; b++) {
BreakpointData *data = handler->at(b);
if (lineNumber == data->lineNumber.toInt() && fileName == data->fileName)
return data;
}
return 0;
}
bool ScriptEngine::checkForBreakCondition(bool byFunction)
{
// FIXME: Should that ever happen after setAgent(0) in shutdownInferior()?
// In practice, it does, so chicken out.
if (targetState() == DebuggerFinished)
return false;
const QScriptContext *context = m_scriptEngine->currentContext();
const QScriptContextInfo info(context);
// Update breakpoints
const QString functionName = info.functionName();
const QString fileName = info.fileName();
const int lineNumber = byFunction
? info.functionStartLineNumber() : info.lineNumber();
SDEBUG(Q_FUNC_INFO << byFunction << functionName
<< lineNumber << fileName);
if (m_stopOnNextLine) {
// Interrupt inferior
m_stopOnNextLine = false;
} else {
if (byFunction && functionName.isEmpty())
return false;
BreakpointData *data = byFunction ?
findBreakPointByFunction(breakHandler(), functionName) :
findBreakPointByFileName(breakHandler(), lineNumber, fileName);
if (!data)
return false;
// Skip disabled breakpoint.
if (!data->enabled)
return false;
// We just run into a breakpoint.
//SDEBUG("RESOLVING BREAKPOINT AT " << fileName << lineNumber);
data->bpLineNumber = QByteArray::number(lineNumber);
data->bpFileName = fileName;
data->bpFuncName = functionName;
data->setMarkerLineNumber(lineNumber);
data->setMarkerFileName(fileName);
data->pending = false;
data->updateMarker();
}
notifyInferiorSpontaneousStop();
SDEBUG("Stopped at " << lineNumber << fileName);
showStatusMessage(tr("Stopped at %1:%2.").arg(fileName).arg(lineNumber), 5000);
StackFrame frame;
frame.file = fileName;
frame.line = lineNumber;
gotoLocation(frame, true);
updateLocals();
return true;
}
void ScriptEngine::updateLocals()
{
QScriptContext *context = m_scriptEngine->currentContext();
watchHandler()->beginCycle();
SDEBUG(Q_FUNC_INFO);
//
// Build stack
//
QList<StackFrame> stackFrames;
int i = 0;
for (QScriptContext *c = context; c; c = c->parentContext(), ++i) {
const QScriptContextInfo info(c);
StackFrame frame;
frame.level = i;
frame.file = info.fileName();
frame.function = info.functionName();
frame.from = QString::number(info.functionStartLineNumber());
frame.to = QString::number(info.functionEndLineNumber());
frame.line = info.lineNumber();
if (frame.function.isEmpty())
frame.function = QLatin1String("<global scope>");
//frame.address = ...;
stackFrames.append(frame);
}
stackHandler()->setFrames(stackFrames);
//
// Build locals, deactivate agent meanwhile.
//
m_scriptEngine->setAgent(0);
WatchData data;
data.iname = "local";
data.name = _(data.iname);
data.scriptValue = context->activationObject();
watchHandler()->beginCycle();
updateSubItem(data);
watchHandler()->endCycle();
// FIXME: Use an extra thread. This here is evil.
m_stopped = true;
showStatusMessage(tr("Stopped."), 5000);
while (m_stopped) {
//SDEBUG("LOOPING");
QApplication::processEvents();
}
// Clear any exceptions occurred during locals evaluation.
m_scriptEngine->clearExceptions();
m_scriptEngine->setAgent(m_scriptAgent.data());
notifyInferiorRunOk();
}
void ScriptEngine::updateWatchData(const WatchData &data)
{
updateSubItem(data);
}
static inline QString msgDebugInsert(const WatchData &d0, const QList<WatchData>& children)
{
QString rc;
QTextStream str(&rc);
str << "INSERTING " << d0.toString() << '\n';
foreach(const WatchData &c, children)
str << " " << c.toString() << '\n';
return rc;
}
void ScriptEngine::updateSubItem(const WatchData &data0)
{
WatchData data = data0;
QList<WatchData> children;
//SDEBUG("\nUPDATE SUBITEM: " << data.toString() << data.scriptValue.toString());
QTC_ASSERT(data.isValid(), return);
if (data.isTypeNeeded() || data.isValueNeeded()) {
const QScriptValue &ob = data.scriptValue;
if (ob.isArray()) {
data.setType(QLatin1String("Array"), false);
data.setValue(QString(QLatin1Char(' ')));
} else if (ob.isBool()) {
data.setType(QLatin1String("Bool"), false);
data.setValue(ob.toBool() ? QLatin1String("true") : QLatin1String("false"));
data.setHasChildren(false);
} else if (ob.isDate()) {
data.setType(QLatin1String("Date"), false);
data.setValue(ob.toDateTime().toString());
data.setHasChildren(false);
} else if (ob.isError()) {
data.setType(QLatin1String("Error"), false);
data.setValue(QString(QLatin1Char(' ')));
} else if (ob.isFunction()) {
data.setType(QLatin1String("Function"), false);
data.setValue(QString(QLatin1Char(' ')));
} else if (ob.isNull()) {
const QString nullValue = QLatin1String("<null>");
data.setType(nullValue, false);
data.setValue(nullValue);
} else if (ob.isNumber()) {
data.setType(QLatin1String("Number"), false);
data.setValue(QString::number(ob.toNumber()));
data.setHasChildren(false);
} else if (ob.isObject()) {
data.setType(QLatin1String("Object"), false);
data.setValue(QString(QLatin1Char(' ')));
} else if (ob.isQMetaObject()) {
data.setType(QLatin1String("QMetaObject"), false);
data.setValue(QString(QLatin1Char(' ')));
} else if (ob.isQObject()) {
data.setType(QLatin1String("QObject"), false);
data.setValue(QString(QLatin1Char(' ')));
} else if (ob.isRegExp()) {
data.setType(QLatin1String("RegExp"), false);
data.setValue(ob.toRegExp().pattern());
} else if (ob.isString()) {
data.setType(QLatin1String("String"), false);
data.setValue(ob.toString());
} else if (ob.isVariant()) {
data.setType(QLatin1String("Variant"), false);
data.setValue(QString(QLatin1Char(' ')));
} else if (ob.isUndefined()) {
data.setType(QLatin1String("<undefined>"), false);
data.setValue(QLatin1String("<unknown>"));
data.setHasChildren(false);
} else {
const QString unknown = QLatin1String("<unknown>");
data.setType(unknown, false);
data.setValue(unknown);
data.setHasChildren(false);
}
}
if (data.isChildrenNeeded()) {
QScriptValueIterator it(data.scriptValue);
while (it.hasNext()) {
it.next();
WatchData data1;
data1.iname = data.iname + '.' + it.name().toLatin1();
data1.exp = it.name().toLatin1();
data1.name = it.name();
data1.scriptValue = it.value();
if (watchHandler()->isExpandedIName(data1.iname)) {
data1.setChildrenNeeded();
} else {
data1.setChildrenUnneeded();
}
children.push_back(data1);
}
data.setHasChildren(!children.isEmpty());
data.setChildrenUnneeded();
}
if (data.isHasChildrenNeeded()) {
QScriptValueIterator it(data.scriptValue);
data.setHasChildren(it.hasNext());
}
SDEBUG(msgDebugInsert(data, children));
watchHandler()->insertData(data);
if (!children.isEmpty())
watchHandler()->insertBulkData(children);
}
DebuggerEngine *createScriptEngine(const DebuggerStartParameters &sp)
{
return new ScriptEngine(sp);
}
} // namespace Internal
} // namespace Debugger
|