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
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "scriptdebugger.h"
#include "scriptbreakpointmanager.h"
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptEngineAgent>
#include <QtScript/QScriptContextInfo>
#include <QtScript/QScriptValueIterator>
#include <QtCore/QTextStream>
#include <QtCore/QStack>
static QString safeValueToString(const QScriptValue &value)
{
if (value.isObject())
return QLatin1String("[object Object]");
else
return value.toString();
}
class ScriptInfo;
class ScriptBreakpointManager;
class ScriptDebuggerPrivate
: public QScriptEngineAgent
{
Q_DECLARE_PUBLIC(ScriptDebugger)
public:
enum Mode {
Run,
StepInto,
StepOver
};
ScriptDebuggerPrivate(QScriptEngine *engine);
~ScriptDebuggerPrivate();
// QScriptEngineAgent interface
void scriptLoad(qint64 id, const QString &program,
const QString &fileName, int lineNumber);
void scriptUnload(qint64 id);
void positionChange(qint64 scriptId,
int lineNumber, int columnNumber);
void functionEntry(qint64 scriptId);
void functionExit(qint64 scriptId,
const QScriptValue &returnValue);
void exceptionThrow(qint64 scriptId,
const QScriptValue &exception, bool hasHandler);
void interactive();
bool executeCommand(const QString &command, const QStringList &args);
void setMode(Mode mode);
Mode mode() const;
int frameCount() const;
void setCurrentFrameIndex(int index);
int currentFrameIndex() const;
QScriptContext *frameContext(int index) const;
QScriptContext *currentFrameContext() const;
ScriptInfo *scriptInfo(QScriptContext *context) const;
int listLineNumber() const;
void setListLineNumber(int lineNumber);
QString readLine();
void output(const QString &text);
void message(const QString &text);
void errorMessage(const QString &text);
// attributes
QTextStream *m_defaultInputStream;
QTextStream *m_defaultOutputStream;
QTextStream *m_defaultErrorStream;
QTextStream *m_inputStream;
QTextStream *m_outputStream;
QTextStream *m_errorStream;
ScriptBreakpointManager *m_bpManager;
Mode m_mode;
QMap<qint64, ScriptInfo*> m_scripts;
QMap<QScriptContext*, QStack<qint64> > m_contextProgramIds;
QString m_lastInteractiveCommand;
QString m_commandPrefix;
int m_stepDepth;
int m_currentFrameIndex;
int m_listLineNumber;
ScriptDebugger *q_ptr;
};
class ScriptInfo
{
public:
ScriptInfo(const QString &code, const QString &fileName, int lineNumber)
: m_code(code), m_fileName(fileName), m_lineNumber(lineNumber)
{ }
inline QString code() const
{ return m_code; }
inline QString fileName() const
{ return m_fileName; }
inline int lineNumber() const
{ return m_lineNumber; }
QString lineText(int lineNumber);
QMap<int, int> m_lineOffsets;
private:
int lineOffset(int lineNumber);
QString m_code;
QString m_fileName;
int m_lineNumber;
};
int ScriptInfo::lineOffset(int lineNumber)
{
QMap<int, int>::const_iterator it = m_lineOffsets.constFind(lineNumber);
if (it != m_lineOffsets.constEnd())
return it.value();
int offset;
it = m_lineOffsets.constFind(lineNumber - 1);
if (it != m_lineOffsets.constEnd()) {
offset = it.value();
offset = m_code.indexOf(QLatin1Char('\n'), offset);
if (offset != -1)
++offset;
m_lineOffsets.insert(lineNumber, offset);
} else {
int index;
it = m_lineOffsets.lowerBound(lineNumber);
--it;
if (it != m_lineOffsets.constBegin()) {
index = it.key();
offset = it.value();
} else {
index = m_lineNumber;
offset = 0;
}
int j = index;
for ( ; j < lineNumber; ++j) {
m_lineOffsets.insert(j, offset);
offset = m_code.indexOf(QLatin1Char('\n'), offset);
if (offset == -1)
break;
++offset;
}
m_lineOffsets.insert(j, offset);
}
return offset;
}
QString ScriptInfo::lineText(int lineNumber)
{
int startOffset = lineOffset(lineNumber);
if (startOffset == -1)
return QString();
int endOffset = lineOffset(lineNumber + 1);
if (endOffset == -1)
return m_code.mid(startOffset);
else
return m_code.mid(startOffset, endOffset - startOffset - 1);
}
ScriptDebuggerPrivate::ScriptDebuggerPrivate(QScriptEngine *engine)
: QScriptEngineAgent(engine), m_mode(Run)
{
m_commandPrefix = QLatin1String(".");
m_bpManager = new ScriptBreakpointManager;
m_defaultInputStream = new QTextStream(stdin);
m_defaultOutputStream = new QTextStream(stdout);
m_defaultErrorStream = new QTextStream(stderr);
m_inputStream = m_defaultInputStream;
m_outputStream = m_defaultOutputStream;
m_errorStream = m_defaultErrorStream;
}
ScriptDebuggerPrivate::~ScriptDebuggerPrivate()
{
delete m_defaultInputStream;
delete m_defaultOutputStream;
delete m_defaultErrorStream;
delete m_bpManager;
qDeleteAll(m_scripts);
}
QString ScriptDebuggerPrivate::readLine()
{
return m_inputStream->readLine();
}
void ScriptDebuggerPrivate::output(const QString &text)
{
*m_outputStream << text;
}
void ScriptDebuggerPrivate::message(const QString &text)
{
*m_outputStream << text << endl;
m_outputStream->flush();
}
void ScriptDebuggerPrivate::errorMessage(const QString &text)
{
*m_errorStream << text << endl;
m_errorStream->flush();
}
void ScriptDebuggerPrivate::setMode(Mode mode)
{
m_mode = mode;
}
ScriptDebuggerPrivate::Mode ScriptDebuggerPrivate::mode() const
{
return m_mode;
}
QScriptContext *ScriptDebuggerPrivate::frameContext(int index) const
{
QScriptContext *ctx = engine()->currentContext();
for (int i = 0; i < index; ++i) {
ctx = ctx->parentContext();
if (!ctx)
break;
}
return ctx;
}
int ScriptDebuggerPrivate::currentFrameIndex() const
{
return m_currentFrameIndex;
}
void ScriptDebuggerPrivate::setCurrentFrameIndex(int index)
{
m_currentFrameIndex = index;
m_listLineNumber = -1;
}
int ScriptDebuggerPrivate::listLineNumber() const
{
return m_listLineNumber;
}
void ScriptDebuggerPrivate::setListLineNumber(int lineNumber)
{
m_listLineNumber = lineNumber;
}
QScriptContext *ScriptDebuggerPrivate::currentFrameContext() const
{
return frameContext(currentFrameIndex());
}
int ScriptDebuggerPrivate::frameCount() const
{
int count = 0;
QScriptContext *ctx = engine()->currentContext();
while (ctx) {
++count;
ctx = ctx->parentContext();
}
return count;
}
ScriptInfo *ScriptDebuggerPrivate::scriptInfo(QScriptContext *context) const
{
QStack<qint64> pids = m_contextProgramIds.value(context);
if (pids.isEmpty())
return 0;
return m_scripts.value(pids.top());
}
void ScriptDebuggerPrivate::interactive()
{
setCurrentFrameIndex(0);
QString qsdbgPrompt = QString::fromLatin1("(qsdbg) ");
QString dotPrompt = QString::fromLatin1(".... ");
QString prompt = qsdbgPrompt;
QString code;
forever {
*m_outputStream << prompt;
m_outputStream->flush();
QString line = readLine();
if (code.isEmpty() && (line.isEmpty() || line.startsWith(m_commandPrefix))) {
if (line.isEmpty())
line = m_lastInteractiveCommand;
else
m_lastInteractiveCommand = line;
QStringList parts = line.split(QLatin1Char(' '), QString::SkipEmptyParts);
if (!parts.isEmpty()) {
QString command = parts.takeFirst().mid(1);
if (executeCommand(command, parts))
break;
}
} else {
if (line.isEmpty())
continue;
code += line;
code += QLatin1Char('\n');
if (line.trimmed().isEmpty()) {
continue;
} else if (! engine()->canEvaluate(code)) {
prompt = dotPrompt;
} else {
setMode(Run);
QScriptValue result = engine()->evaluate(code, QLatin1String("typein"));
code.clear();
prompt = qsdbgPrompt;
if (! result.isUndefined()) {
errorMessage(result.toString());
engine()->clearExceptions();
}
}
}
}
}
bool ScriptDebuggerPrivate::executeCommand(const QString &command, const QStringList &args)
{
if (command == QLatin1String("c")
|| command == QLatin1String("continue")) {
setMode(Run);
return true;
} else if (command == QLatin1String("s")
|| command == QLatin1String("step")) {
setMode(StepInto);
return true;
} else if (command == QLatin1String("n")
|| command == QLatin1String("next")) {
setMode(StepOver);
m_stepDepth = 0;
return true;
} else if (command == QLatin1String("f")
|| command == QLatin1String("frame")) {
bool ok = false;
int index = args.value(0).toInt(&ok);
if (ok) {
if (index < 0 || index >= frameCount()) {
errorMessage("No such frame.");
} else {
setCurrentFrameIndex(index);
QScriptContext *ctx = currentFrameContext();
message(QString::fromLatin1("#%0 %1").arg(index).arg(ctx->toString()));
}
}
} else if (command == QLatin1String("bt")
|| command == QLatin1String("backtrace")) {
QScriptContext *ctx = engine()->currentContext();
int index = -1;
while (ctx) {
++index;
QString line = ctx->toString();
message(QString::fromLatin1("#%0 %1").arg(index).arg(line));
ctx = ctx->parentContext();
}
} else if (command == QLatin1String("up")) {
int index = currentFrameIndex() + 1;
if (index == frameCount()) {
errorMessage(QString::fromLatin1("Initial frame selected; you cannot go up."));
} else {
setCurrentFrameIndex(index);
QScriptContext *ctx = currentFrameContext();
message(QString::fromLatin1("#%0 %1").arg(index).arg(ctx->toString()));
}
} else if (command == QLatin1String("down")) {
int index = currentFrameIndex() - 1;
if (index < 0) {
errorMessage(QString::fromLatin1("Bottom (innermost) frame selected; you cannot go down."));
} else {
setCurrentFrameIndex(index);
QScriptContext *ctx = currentFrameContext();
message(QString::fromLatin1("#%0 %1").arg(index).arg(ctx->toString()));
}
} else if (command == QLatin1String("b")
|| command == QLatin1String("break")) {
QString str = args.value(0);
int colonIndex = str.indexOf(QLatin1Char(':'));
if (colonIndex != -1) {
// filename:line form
QString fileName = str.left(colonIndex);
int lineNumber = str.mid(colonIndex+1).toInt();
int id = m_bpManager->setBreakpoint(fileName, lineNumber);
message(QString::fromLatin1("Breakpoint %0 at %1, line %2.").arg(id+1).arg(fileName).arg(lineNumber));
} else {
// function
QScriptValue fun = engine()->globalObject().property(str);
if (fun.isFunction()) {
int id = m_bpManager->setBreakpoint(fun);
message(QString::fromLatin1("Breakpoint %0 at %1().").arg(id+1).arg(str));
}
}
} else if (command == QLatin1String("d")
|| command == QLatin1String("delete")) {
int id = args.value(0).toInt() - 1;
m_bpManager->removeBreakpoint(id);
} else if (command == QLatin1String("disable")) {
int id = args.value(0).toInt() - 1;
m_bpManager->setBreakpointEnabled(id, false);
} else if (command == QLatin1String("enable")) {
int id = args.value(0).toInt() - 1;
m_bpManager->setBreakpointEnabled(id, true);
} else if (command == QLatin1String("list")) {
QScriptContext *ctx = currentFrameContext();
ScriptInfo *progInfo = scriptInfo(ctx);
if (!progInfo) {
errorMessage("No source text available for this frame.");
} else {
QScriptContextInfo ctxInfo(ctx);
bool ok;
int line = args.value(0).toInt(&ok);
if (ok) {
line = qMax(1, line - 5);
} else {
line = listLineNumber();
if (line == -1)
line = qMax(progInfo->lineNumber(), ctxInfo.lineNumber() - 5);
}
for (int i = line; i < line + 10; ++i) {
message(QString::fromLatin1("%0\t%1").arg(i).arg(progInfo->lineText(i)));
}
setListLineNumber(line + 10);
}
} else if (command == QLatin1String("info")) {
if (args.size() < 1) {
} else {
QString what = args.value(0);
if (what == QLatin1String("locals")) {
QScriptValueIterator it(currentFrameContext()->activationObject());
while (it.hasNext()) {
it.next();
QString line;
line.append(it.name());
line.append(QLatin1String(" = "));
line.append(safeValueToString(it.value()));
message(line);
}
}
}
} else if (command == QLatin1String("help")) {
message("continue - continue execution\n"
"step - step into statement\n"
"next - step over statement\n"
"list - show where you are\n"
"\n"
"break - set breakpoint\n"
"delete - remove breakpoint\n"
"disable - disable breakpoint\n"
"enable - enable breakpoint\n"
"\n"
"backtrace - show backtrace\n"
"up - one frame up\n"
"down - one frame down\n"
"frame - set frame\n"
"\n"
"info locals - show local variables");
} else {
errorMessage(QString::fromLatin1("Undefined command \"%0\". Try \"help\".")
.arg(command));
}
return false;
}
// QScriptEngineAgent interface
void ScriptDebuggerPrivate::scriptLoad(qint64 id, const QString &program,
const QString &fileName, int lineNumber)
{
ScriptInfo *info = new ScriptInfo(program, fileName, lineNumber);
m_scripts.insert(id, info);
}
void ScriptDebuggerPrivate::scriptUnload(qint64 id)
{
ScriptInfo *info = m_scripts.take(id);
delete info;
}
void ScriptDebuggerPrivate::functionEntry(qint64 scriptId)
{
if (scriptId != -1) {
QScriptContext *ctx = engine()->currentContext();
QStack<qint64> ids = m_contextProgramIds.value(ctx);
ids.push(scriptId);
m_contextProgramIds.insert(ctx, ids);
}
if (mode() == StepOver)
++m_stepDepth;
}
void ScriptDebuggerPrivate::functionExit(qint64 scriptId,
const QScriptValue &/*returnValue*/)
{
if (scriptId != -1) {
QScriptContext *ctx = engine()->currentContext();
QStack<qint64> ids = m_contextProgramIds.value(ctx);
Q_ASSERT(!ids.isEmpty());
Q_ASSERT(ids.top() == scriptId);
ids.pop();
m_contextProgramIds.insert(ctx, ids);
}
if (mode() == StepOver)
--m_stepDepth;
}
void ScriptDebuggerPrivate::positionChange(qint64 scriptId,
int lineNumber, int /*columnNumber*/)
{
ScriptInfo *info = 0;
bool enterInteractiveMode = false;
if (m_bpManager->hasBreakpoints()) {
// check if we hit a breakpoint
info = m_scripts.value(scriptId);
QScriptContext *ctx = engine()->currentContext();
QScriptContextInfo ctxInfo(ctx);
QScriptValue callee = ctx->callee();
// try fileName:lineNumber
int bpid = m_bpManager->findBreakpoint(info->fileName(), lineNumber);
if ((bpid != -1) && m_bpManager->isBreakpointEnabled(bpid)) {
message(QString::fromLatin1("Breakpoint %0 at %1:%2")
.arg(bpid + 1).arg(info->fileName()).arg(lineNumber));
if (m_bpManager->isBreakpointSingleShot(bpid))
m_bpManager->removeBreakpoint(bpid);
}
if (bpid == -1) {
// try function
bpid = m_bpManager->findBreakpoint(callee);
if ((bpid != -1) && m_bpManager->isBreakpointEnabled(bpid)) {
message(QString::fromLatin1("Breakpoint %0, %1()")
.arg(bpid + 1).arg(ctxInfo.functionName()));
if (m_bpManager->isBreakpointSingleShot(bpid))
m_bpManager->removeBreakpoint(bpid);
}
}
if ((bpid == -1) && !ctxInfo.functionName().isEmpty()) {
// try functionName:fileName
bpid = m_bpManager->findBreakpoint(ctxInfo.functionName(), ctxInfo.fileName());
if ((bpid != -1) && m_bpManager->isBreakpointEnabled(bpid)) {
message(QString::fromLatin1("Breakpoint %0, %1():%2").arg(bpid + 1)
.arg(ctxInfo.functionName()).arg(ctxInfo.fileName()));
if (m_bpManager->isBreakpointSingleShot(bpid))
m_bpManager->removeBreakpoint(bpid);
}
}
enterInteractiveMode = (bpid != -1);
}
switch (mode()) {
case Run:
break;
case StepInto:
enterInteractiveMode = true;
break;
case StepOver:
enterInteractiveMode = enterInteractiveMode || (m_stepDepth <= 0);
break;
}
if (enterInteractiveMode) {
if (!info)
info = m_scripts.value(scriptId);
Q_ASSERT(info);
message(QString::fromLatin1("%0\t%1").arg(lineNumber).arg(info->lineText(lineNumber)));
interactive();
}
}
void ScriptDebuggerPrivate::exceptionThrow(qint64 /*scriptId*/,
const QScriptValue &exception,
bool hasHandler)
{
if (!hasHandler) {
errorMessage(QString::fromLatin1("uncaught exception: %0").arg(exception.toString()));
QScriptContext *ctx = engine()->currentContext();
int lineNumber = QScriptContextInfo(ctx).lineNumber();
ScriptInfo *info = scriptInfo(ctx);
QString lineText = info ? info->lineText(lineNumber) : QString("(no source text available)");
message(QString::fromLatin1("%0\t%1").arg(lineNumber).arg(lineText));
interactive();
}
}
ScriptDebugger::ScriptDebugger(QScriptEngine *engine)
: d_ptr(new ScriptDebuggerPrivate(engine))
{
d_ptr->q_ptr = this;
engine->setAgent(d_ptr);
}
ScriptDebugger::ScriptDebugger(QScriptEngine *engine, ScriptDebuggerPrivate &dd)
: d_ptr(&dd)
{
d_ptr->q_ptr = this;
engine->setAgent(d_ptr);
}
ScriptDebugger::~ScriptDebugger()
{
delete d_ptr;
d_ptr = 0;
}
void ScriptDebugger::breakAtNextStatement()
{
Q_D(ScriptDebugger);
d->setMode(ScriptDebuggerPrivate::StepInto);
}
void ScriptDebugger::setBreakpoint(const QString &fileName, int lineNumber)
{
Q_D(ScriptDebugger);
d->m_bpManager->setBreakpoint(fileName, lineNumber);
}
void ScriptDebugger::setBreakpoint(const QString &functionName, const QString &fileName)
{
Q_D(ScriptDebugger);
d->m_bpManager->setBreakpoint(functionName, fileName);
}
void ScriptDebugger::setBreakpoint(const QScriptValue &function)
{
Q_D(ScriptDebugger);
d->m_bpManager->setBreakpoint(function);
}
QTextStream *ScriptDebugger::inputStream() const
{
Q_D(const ScriptDebugger);
return d->m_inputStream;
}
void ScriptDebugger::setInputStream(QTextStream *inputStream)
{
Q_D(ScriptDebugger);
d->m_inputStream = inputStream;
}
QTextStream *ScriptDebugger::outputStream() const
{
Q_D(const ScriptDebugger);
return d->m_outputStream;
}
void ScriptDebugger::setOutputStream(QTextStream *outputStream)
{
Q_D(ScriptDebugger);
d->m_outputStream = outputStream;
}
QTextStream *ScriptDebugger::errorStream() const
{
Q_D(const ScriptDebugger);
return d->m_errorStream;
}
void ScriptDebugger::setErrorStream(QTextStream *errorStream)
{
Q_D(ScriptDebugger);
d->m_errorStream = errorStream;
}
|