9#include <private/qqmlcontext_p.h>
10#include <private/qqmlengine_p.h>
11#include <private/qv4compileddata_p.h>
12#include <private/qv4debuginstructions_p.h>
13#include <private/qv4function_p.h>
14#include <private/qv4scopedvalue_p.h>
15#include <private/qv4script_p.h>
16#include <private/qv4stackframe_p.h>
22QV4Debugger::BreakPoint::BreakPoint(
const QString &fileName,
int line)
23 : fileName(fileName), lineNumber(line)
26inline size_t qHash(
const QV4Debugger::BreakPoint &b, size_t seed = 0)
noexcept
28 return qHash(b.fileName, seed) ^ b.lineNumber;
31inline bool operator==(
const QV4Debugger::BreakPoint &a,
32 const QV4Debugger::BreakPoint &b)
34 return a.lineNumber == b.lineNumber && a.fileName == b.fileName;
41 , m_pauseRequested(
false)
42 , m_haveBreakPoints(
false)
43 , m_breakOnThrow(
false)
45 , m_gatherSources(
nullptr)
46 , m_runningJob(
nullptr)
49 static int debuggerId = qRegisterMetaType<
QV4Debugger*>();
52 Q_UNUSED(pauseReasonId);
54 this, &
QV4Debugger::runJobUnpaused, Qt::QueuedConnection);
74 QMutexLocker locker(&m_lock);
77 m_pauseRequested =
true;
82 QMutexLocker locker(&m_lock);
86 if (!m_returnedValue.isUndefined())
87 m_returnedValue.set(m_engine, QV4::Encode::undefined());
89 m_currentFrame = m_engine->currentStackFrame;
91 m_runningCondition.wakeAll();
101 QMutexLocker locker(&m_lock);
102 m_breakPoints.insert(BreakPoint(fileName.mid(fileName.lastIndexOf(
'/') + 1),
103 lineNumber), {fileName, condition});
104 m_haveBreakPoints =
true;
105 m_breakPointsChanged =
true;
110 QMutexLocker locker(&m_lock);
111 m_breakPoints.remove(BreakPoint(fileName.mid(fileName.lastIndexOf(
'/') + 1),
113 m_haveBreakPoints = !m_breakPoints.isEmpty();
114 m_breakPointsChanged =
true;
119 QMutexLocker locker(&m_lock);
121 m_breakOnThrow = onoff;
126 QMutexLocker locker(&m_lock);
127 m_pauseRequested =
false;
132 QMutexLocker locker(&m_lock);
133 return m_executableLines.size();
139 state.fileName = QUrl(getFunction()->sourceFile()).fileName();
140 state
.lineNumber = engine()->currentStackFrame->lineNumber();
148 return m_pauseRequested || m_haveBreakPoints || m_breakPointsChanged || m_gatherSources
154 return m_engine->stackTrace(frameLimit);
162 QMutexLocker locker(&m_lock);
164 if (m_gatherSources) {
166 delete m_gatherSources;
167 m_gatherSources =
nullptr;
170 if (m_breakPointsChanged) {
171 m_breakPointsChanged =
false;
172 pruneExecutableLines();
175 switch (m_stepping) {
177 if (m_currentFrame != m_engine->currentStackFrame)
188 if (m_pauseRequested) {
189 m_pauseRequested =
false;
191 }
else if (m_haveBreakPoints) {
192 if (QV4::Function *f = getFunction()) {
194 const int lineNumber = engine()->currentStackFrame->lineNumber();
195 if (reallyHitTheBreakPoint(f, f->sourceFile(), lineNumber))
205 QMutexLocker locker(&m_lock);
207 if (m_stepping == StepIn)
208 m_currentFrame = m_engine->currentStackFrame;
217 QMutexLocker locker(&m_lock);
219 if (m_stepping != NotStepping && m_currentFrame == m_engine->currentStackFrame) {
220 m_currentFrame = m_currentFrame->parentFrame();
222 m_returnedValue.set(m_engine, retVal);
234 QMutexLocker locker(&m_lock);
240 return &QV4::Moth::DebugInstructions::insert;
245 if (m_engine->currentStackFrame)
246 return m_engine->currentStackFrame->v4Function;
248 return m_engine->globalCode;
253 QMutexLocker locker(&m_lock);
256 m_jobIsRunning.wakeAll();
265 emit debuggerPaused(
this, reason);
268 m_runningCondition.wait(&m_lock);
271 m_jobIsRunning.wakeAll();
281
282
283
284
285
286
287
289 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit)
291 ExecutableLines result;
292 const QV4::CompiledData::Unit *data = unit ? unit->unitData() :
nullptr;
295 for (uint f = 0, fn = data->functionTableSize; f < fn; ++f) {
296 const QV4::CompiledData::Function *cf = data->functionAt(f);
297 const int start = cf->location.line();
298 result.functionStarts.append(start);
299 const QV4::CompiledData::CodeOffsetToLineAndStatement *table
300 = cf->lineAndStatementNumberTable();
301 for (uint i = 0, n = cf->nLineAndStatementNumbers; i < n; ++i) {
302 const int line = table[i].line;
304 result.lines.append({line, start});
307 std::sort(result.lines.begin(), result.lines.end());
308 result.lines.erase(
std::unique(result.lines.begin(), result.lines.end()),
310 std::sort(result.functionStarts.begin(), result.functionStarts.end());
314int QV4Debugger::firstLineAtOrAfter(
const ExecutableLines &executable,
int requestedLine)
318 const auto it =
std::lower_bound(executable.lines.cbegin(), executable.lines.cend(),
319 ExecutableLine{requestedLine, 0});
320 if (it == executable.lines.cend())
329 if (it->functionStart > requestedLine)
331 const QList<
int> &starts = executable.functionStarts;
332 const auto start =
std::upper_bound(starts.cbegin(), starts.cend(), requestedLine);
333 if (start != starts.cend() && *start < it->line)
343 m_executableLines.removeIf([
this](
const UnitLines &cached) {
344 const QString file = QUrl(cached.unit->fileName()).fileName();
345 for (
auto it = m_breakPoints.keyBegin(), end = m_breakPoints.keyEnd(); it != end; ++it) {
346 if (it->fileName == file)
354
355
356
357
358
363 constexpr qsizetype MaxCachedUnits = 8;
365 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit
366 = function->executableCompilationUnit()->baseCompilationUnit();
367 for (qsizetype i = 0, n = m_executableLines.size(); i < n; ++i) {
368 if (m_executableLines.at(i).unit == unit) {
370 m_executableLines.move(i, 0);
371 return m_executableLines.first().lines;
374 if (m_executableLines.size() == MaxCachedUnits)
375 m_executableLines.removeLast();
376 m_executableLines.prepend({unit, collectExecutableLines(unit)});
377 return m_executableLines.first().lines;
381
382
383
384
385
386
387
390 QMutexLocker locker(&m_lock);
391 QV4::Function *f = getFunction();
396 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit
397 = f->executableCompilationUnit()->baseCompilationUnit();
398 for (
const UnitLines &cached : std::as_const(m_executableLines)) {
399 if (cached.unit == unit)
400 return firstLineAtOrAfter(cached.lines, requestedLine);
402 return firstLineAtOrAfter(collectExecutableLines(unit), requestedLine);
405bool QV4Debugger::reallyHitTheBreakPoint(QV4::Function *function,
const QString &filename,
408 const QString file = QUrl(filename).fileName();
414 QStringList conditions;
415 for (
auto bp = m_breakPoints.constBegin(), end = m_breakPoints.constEnd(); bp != end; ++bp) {
416 if (bp.key().fileName != file || bp.key().lineNumber > linenr)
420 if (bp.key().lineNumber != linenr
421 && (!filename.endsWith(bp.value().target)
422 || firstLineAtOrAfter(executableLines(function), bp.key().lineNumber)
426 if (bp.value().condition.isEmpty())
428 conditions.append(bp.value().condition);
431 for (
const QString &condition : std::as_const(conditions)) {
432 Q_ASSERT(m_runningJob ==
nullptr);
433 EvalJob evilJob(m_engine, condition);
434 m_runningJob = &evilJob;
436 m_runningJob =
nullptr;
437 if (evilJob.resultAsBoolean())
445 QMutexLocker locker(&m_lock);
446 runInEngine_havingLock(job);
452 Q_ASSERT(m_runningJob ==
nullptr);
456 m_runningCondition.wakeAll();
459 m_jobIsRunning.wait(&m_lock);
460 m_runningJob =
nullptr;
465#include "moc_qv4debugger.cpp"
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
qsizetype executableLinesCacheSize()
void maybeBreakAtInstruction() override
QV4::Function * getFunction() const
void enteringFunction() override
void runInEngine(QV4DebugJob *job)
int resolvedBreakpointLine(int requestedLine)
QV4DataCollector * collector()
void removeBreakPoint(const QString &fileName, int lineNumber)
QV4::ExecutionEngine * engine() const
void setBreakOnThrow(bool onoff)
bool pauseAtNextOpportunity() const override
void leavingFunction(const QV4::ReturnedValue &retVal) override
ExecutionState currentExecutionState() const
QV4::Moth::DebugInstructionPass debugInstructionPass() const override
const QV4DataCollector * collector() const
QV4Debugger(QV4::ExecutionEngine *engine)
void aboutToThrow() override
void addBreakPoint(const QString &fileName, int lineNumber, const QString &condition=QString())
QList< QV4::StackFrame > stackTrace(int frameLimit=-1) const
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept