lldbengineguest.cpp 24.9 KB
Newer Older
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
/**************************************************************************
**
** 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 "lldbengineguest.h"

#include "debuggeractions.h"
#include "debuggerconstants.h"
#include "debuggerdialogs.h"
#include "debuggerplugin.h"
#include "debuggerstringutils.h"

#include "breakhandler.h"
#include "breakpoint.h"
#include "moduleshandler.h"
#include "registerhandler.h"
#include "stackhandler.h"
#include "watchhandler.h"
#include "watchutils.h"
#include "threadshandler.h"

#include <utils/qtcassert.h>
#include <QtCore/QDebug>
#include <QtCore/QProcess>
#include <QtCore/QFileInfo>
#include <QtCore/QThread>
#include <QtCore/QMutexLocker>

#include <lldb/API/LLDB.h>

#define DEBUG_FUNC_ENTER \
    showMessage(QString(QLatin1String("LLDB guest engine: %1 ")) \
    .arg(QLatin1String(Q_FUNC_INFO))); \
    qDebug("%s", Q_FUNC_INFO)

Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
63
#define SYNC_INFERIOR_OR(x)  if (m_running) { x; }
64
65
66
67
68


namespace Debugger {
namespace Internal {

hjk's avatar
hjk committed
69
void LldbEventListener::listen(lldb::SBListener *listener)
70
71
72
73
74
75
76
77
{
    while (true) {
        lldb::SBEvent event;
        if (listener->WaitForEvent(1000, event))
            emit lldbEvent(&event);
    }
}

hjk's avatar
hjk committed
78
LldbEngineGuest::LldbEngineGuest()
79
80
    : IPCEngineGuest()
    , m_running (false)
hjk's avatar
hjk committed
81
    , m_worker  (new LldbEventListener)
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    , m_lldb    (new lldb::SBDebugger)
    , m_target  (new lldb::SBTarget)
    , m_process (new lldb::SBProcess)
    , m_listener(new lldb::SBListener("bla"))
    , m_relistFrames (false)
#if defined(HAVE_LLDB_PRIVATE)
    , py (new PythonLLDBToGdbMiHack)
#endif
{
    qRegisterMetaType<lldb::SBListener *>("lldb::SBListener *");
    qRegisterMetaType<lldb::SBEvent *>("lldb::SBEvent *");

    m_worker->moveToThread(&m_wThread);
    connect(m_worker, SIGNAL(lldbEvent(lldb::SBEvent *)), this,
            SLOT(lldbEvent(lldb::SBEvent *)), Qt::BlockingQueuedConnection);
    m_wThread.start();
    setObjectName(QLatin1String("LLDBEngineGuest"));
}

hjk's avatar
hjk committed
101
LldbEngineGuest::~LldbEngineGuest()
102
103
104
105
106
107
108
{
    delete m_lldb;
    delete m_target;
    delete m_process;
    delete m_listener;
}

109
110
111
112
113
114

void LldbEngineGuest::nuke()
{
    ::exit(4);
}

hjk's avatar
hjk committed
115
void LldbEngineGuest::setupEngine()
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{
    DEBUG_FUNC_ENTER;

    lldb::SBDebugger::Initialize();

    *m_lldb = lldb::SBDebugger::Create();
    m_lldb->Initialize();
    if (m_lldb->IsValid())
        notifyEngineSetupOk();
    else
        notifyEngineSetupFailed();

}

hjk's avatar
hjk committed
130
void LldbEngineGuest::setupInferior(const QString &executable,
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
        const QStringList &args, const QStringList &env)
{
    DEBUG_FUNC_ENTER;

    foreach (const QString &s, args) {
        m_arguments.append(s.toLocal8Bit());
    }
    foreach (const QString &s, env) {
        m_environment.append(s.toLocal8Bit());
    }

    qDebug("creating target for %s", executable.toLocal8Bit().data());
    showStatusMessage(QLatin1String("starting ") + executable);
    *m_target = m_lldb->CreateTarget(executable.toLocal8Bit().data());
    if (!m_target->IsValid()) {
        notifyInferiorSetupFailed();
        return;
    }
    DEBUG_FUNC_ENTER;

hjk's avatar
hjk committed
151
    const char **argp = new const char *[m_arguments.count() + 1];
152
153
154
155
156
    argp[m_arguments.count()] = 0;
    for (int i = 0; i < m_arguments.count(); i++) {
        argp[i] = m_arguments[i].data();
    }

hjk's avatar
hjk committed
157
    const char **envp = new const char *[m_environment.count() + 1];
158
159
160
161
    envp[m_environment.count()] = 0;
    for (int i = 0; i < m_environment.count(); i++) {
        envp[i] = m_environment[i].data();
    }
162
    lldb::SBError err;
163
    *m_process = m_target->Launch(argp, envp, NULL, NULL, true, err);
164
165
166
167

    if (!err.Success()) {
        showMessage(QString::fromLocal8Bit(err.GetCString()));
        qDebug() << err.GetCString();
168
        notifyInferiorSetupFailed();
169
    }
170
171
172
173
174
175
176
177
178

    /*
     * note, the actual string ptrs are still valid. They are in m_environment.
     * They probably leak. Considered the marvelous API, there is not much we can do
     */
    delete [] envp;

    if (!m_process->IsValid())
        notifyEngineRunFailed();
179
    QTC_ASSERT(m_listener->IsValid(), qDebug() << false);
180
181
182
    m_listener->StartListeningForEvents(m_process->GetBroadcaster(), UINT32_MAX);
    QMetaObject::invokeMethod(m_worker, "listen", Qt::QueuedConnection,
            Q_ARG(lldb::SBListener *, m_listener));
183
184
185
186
187
188
189
    notifyInferiorSetupOk();
}

void LldbEngineGuest::runEngine()
{
    DEBUG_FUNC_ENTER;
    m_process->Continue();
190
191
}

hjk's avatar
hjk committed
192
void LldbEngineGuest::shutdownInferior()
193
194
195
196
197
{
    DEBUG_FUNC_ENTER;
    m_process->Kill();
}

hjk's avatar
hjk committed
198
void LldbEngineGuest::shutdownEngine()
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
{
    DEBUG_FUNC_ENTER;
    m_currentFrame = lldb::SBFrame();
    m_currentThread = lldb::SBThread();
    m_breakpoints.clear();
    m_localesCache.clear();

    /*
     * this leaks. However, Terminate is broken and lldb leaks anyway
     * We should kill the engine guest process
     */

    *m_lldb = lldb::SBDebugger();
    // leakd.Terminate();
    notifyEngineShutdownOk();
}

hjk's avatar
hjk committed
216
void LldbEngineGuest::detachDebugger()
217
218
219
220
{
    DEBUG_FUNC_ENTER;
}

hjk's avatar
hjk committed
221
void LldbEngineGuest::executeStep()
222
223
224
225
226
227
228
229
{
    DEBUG_FUNC_ENTER;

    if (!m_currentThread.IsValid())
        return;
    m_currentThread.StepInto();
}

hjk's avatar
hjk committed
230
void LldbEngineGuest::executeStepOut()
231
232
233
234
235
236
237
238
{
    DEBUG_FUNC_ENTER;

    if (!m_currentThread.IsValid())
        return;
    m_currentThread.StepOut();
}

hjk's avatar
hjk committed
239
void LldbEngineGuest::executeNext()
240
241
242
243
244
245
246
247
{
    DEBUG_FUNC_ENTER;

    if (!m_currentThread.IsValid())
        return;
    m_currentThread.StepOver();
}

hjk's avatar
hjk committed
248
void LldbEngineGuest::executeStepI()
249
250
251
252
253
254
255
256
{
    DEBUG_FUNC_ENTER;

    if (!m_currentThread.IsValid())
        return;
    m_currentThread.StepInstruction(false);
}

hjk's avatar
hjk committed
257
void LldbEngineGuest::executeNextI()
258
259
260
261
262
263
264
265
{
    DEBUG_FUNC_ENTER;

    if (!m_currentThread.IsValid())
        return;
    m_currentThread.StepInstruction(true);
}

hjk's avatar
hjk committed
266
void LldbEngineGuest::continueInferior()
267
268
269
270
271
272
273
{
    DEBUG_FUNC_ENTER;

    notifyInferiorRunRequested();
    m_process->Continue();
    showStatusMessage(QLatin1String("resuming inferior"));
}
hjk's avatar
hjk committed
274
void LldbEngineGuest::interruptInferior()
275
276
277
278
279
280
281
282
283
{
    DEBUG_FUNC_ENTER;

    m_process->Stop();
    notifyInferiorStopOk();
    m_relistFrames = true;
    updateThreads();
}

hjk's avatar
hjk committed
284
void LldbEngineGuest::executeRunToLine(const QString &fileName, int lineNumber)
285
286
287
288
289
290
291
292
{
    DEBUG_FUNC_ENTER;

    // TODO
    Q_UNUSED(fileName);
    Q_UNUSED(lineNumber);
}

hjk's avatar
hjk committed
293
void LldbEngineGuest::executeRunToFunction(const QString &functionName)
294
295
296
297
298
299
{
    DEBUG_FUNC_ENTER;

    // TODO
    Q_UNUSED(functionName);
}
hjk's avatar
hjk committed
300
void LldbEngineGuest::executeJumpToLine(const QString &fileName, int lineNumber)
301
302
303
304
305
306
307
308
{
    DEBUG_FUNC_ENTER;

    // TODO
    Q_UNUSED(fileName);
    Q_UNUSED(lineNumber);
}

hjk's avatar
hjk committed
309
void LldbEngineGuest::activateFrame(qint64 token)
310
311
{
    DEBUG_FUNC_ENTER;
Arvid Ephraim Picciani's avatar
Arvid Ephraim Picciani committed
312
313
    SYNC_INFERIOR_OR(showMessage(QLatin1String(
        "activateFrame called while inferior running")); return);
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

    currentFrameChanged(token);
    m_localesCache.clear();

    lldb::SBFrame fr = m_currentThread.GetFrameAtIndex(token);
    m_currentFrame = fr;
    lldb::SBSymbolContext context = fr.GetSymbolContext(lldb::eSymbolContextEverything);
    lldb::SBValueList values = fr.GetVariables(true, true, false, true);
    QList<WatchData> wd;
    QByteArray iname = "local";
    for (uint i = 0; i < values.GetSize(); i++) {
        lldb::SBValue v = values.GetValueAtIndex(i);
        if (!v.IsInScope(fr))
            continue;
        getWatchDataR(v, 1, iname, wd);
    }
    updateWatchData(true, wd);
}

hjk's avatar
hjk committed
333
void LldbEngineGuest::requestUpdateWatchData(const Internal::WatchData &data,
334
335
336
337
338
339
340
341
342
343
344
345
346
347
        const Internal::WatchUpdateFlags &)
{
    DEBUG_FUNC_ENTER;
    SYNC_INFERIOR_OR(return);

    lldb::SBValue v = m_localesCache.value(QString::fromUtf8(data.iname));
    QList<WatchData> wd;
    for (uint j = 0; j < v.GetNumChildren(); j++) {
        lldb::SBValue vv = v.GetChildAtIndex(j);
        getWatchDataR(vv, 1, data.iname, wd);
    }
    updateWatchData(false, wd);
}

hjk's avatar
hjk committed
348
void LldbEngineGuest::getWatchDataR(lldb::SBValue v, int level,
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
        const QByteArray &p_iname, QList<WatchData> &wd)
{
    QByteArray iname = p_iname + "." + QByteArray(v.GetName());
    m_localesCache.insert(QString::fromLocal8Bit(iname), v);

#if defined(HAVE_LLDB_PRIVATE)
    wd += py->expand(p_iname, v, m_currentFrame, *m_process);
#else
    WatchData d;
    d.name = QString::fromLocal8Bit(v.GetName());
    d.iname = iname;
    d.type = QByteArray(v.GetTypeName()).trimmed();
    d.value = (QString::fromLocal8Bit(v.GetValue(m_currentFrame)));
    d.hasChildren = v.GetNumChildren();
    d.state = WatchData::State(0);
    wd.append(d);
#endif

    if (--level > 0) {
        for (uint j = 0; j < v.GetNumChildren(); j++) {
            lldb::SBValue vv = v.GetChildAtIndex(j);
            getWatchDataR(vv, level, iname, wd);
        }
    }
}

hjk's avatar
hjk committed
375
void LldbEngineGuest::disassemble(quint64 pc)
376
377
378
379
380
381
382
383
384
{
    DEBUG_FUNC_ENTER;
    SYNC_INFERIOR_OR(return);

    if (!m_currentThread.IsValid())
        return;
    for (uint j = 0; j < m_currentThread.GetNumFrames(); j++) {
        lldb::SBFrame fr = m_currentThread.GetFrameAtIndex(j);
        if (pc == fr.GetPCAddress().GetLoadAddress(*m_target)) {
385
386
387
388
389
390
            QString linesStr = QString::fromLocal8Bit(fr.Disassemble());
            DisassemblerLines lines;
            foreach (const QString &lineStr, linesStr.split(QLatin1Char('\n'))) {
                lines.appendLine(DisassemblerLine(lineStr));
            }
            disassembled(pc, lines);
391
392
393
        }
    }
}
394
395
396
397
398
399
400
void LldbEngineGuest::fetchFrameSource(qint64 frame)
{
    QFile f(m_frame_to_file.value(frame));
    f.open(QFile::ReadOnly);
    frameSourceFetched(frame, QFileInfo(m_frame_to_file.value(frame)).fileName()
            , QString::fromLocal8Bit(f.readAll()));
}
401

hjk's avatar
hjk committed
402
void LldbEngineGuest::addBreakpoint(BreakpointId id,
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
        const Internal::BreakpointParameters &bp_)
{
    DEBUG_FUNC_ENTER;
    SYNC_INFERIOR_OR(notifyAddBreakpointFailed(id); return);

    Internal::BreakpointParameters bp(bp_);

    lldb::SBBreakpoint llbp = m_target->BreakpointCreateByLocation(
            bp.fileName.toLocal8Bit().constData(), bp.lineNumber);
    if (llbp.IsValid()) {
        m_breakpoints.insert(id, llbp);

        llbp.SetIgnoreCount(bp.ignoreCount);
        bp.ignoreCount = llbp.GetIgnoreCount();
        bp.enabled = llbp.IsEnabled();

        lldb::SBBreakpointLocation location = llbp.GetLocationAtIndex(0);
        if (location.IsValid()) {
            bp.address = location.GetLoadAddress();

            // FIXME get those from lldb
            bp.lineNumber = bp.lineNumber;
            bp.fileName = bp.fileName;
            notifyAddBreakpointOk(id);
427
            showMessage(QLatin1String("[BB] ok."));
428
429
430
            notifyBreakpointAdjusted(id, bp);
        } else {
            m_breakpoints.take(id);
431
432
433
            showMessage(QLatin1String("[BB] failed. cant resolve yet"));
//            notifyAddBreakpointFailed(id);
//            notifyAddBreakpointOk(id);
434
435
436
437
438
439
440
        }
    } else {
        showMessage(QLatin1String("[BB] failed. dunno."));
        notifyAddBreakpointFailed(id);
    }
}

hjk's avatar
hjk committed
441
void LldbEngineGuest::removeBreakpoint(BreakpointId id)
442
443
444
445
446
447
448
449
450
{
    DEBUG_FUNC_ENTER;
    SYNC_INFERIOR_OR(notifyRemoveBreakpointFailed(id); return);

    lldb::SBBreakpoint llbp = m_breakpoints.take(id);
    llbp.SetEnabled(false);
    notifyRemoveBreakpointOk(id);
}

hjk's avatar
hjk committed
451
void LldbEngineGuest::changeBreakpoint(BreakpointId id,
452
453
454
455
456
457
458
459
460
        const Internal::BreakpointParameters &bp)
{
    DEBUG_FUNC_ENTER;

    // TODO
    Q_UNUSED(id);
    Q_UNUSED(bp);
}

hjk's avatar
hjk committed
461
void LldbEngineGuest::selectThread(qint64 token)
462
463
464
465
{
    DEBUG_FUNC_ENTER;
    SYNC_INFERIOR_OR(return);

466
    m_frame_to_file.clear();
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
    for (uint i = 0; i < m_process->GetNumThreads(); i++) {
        lldb::SBThread t = m_process->GetThreadAtIndex(i);
        if (t.GetThreadID() == token) {
            m_currentThread = t;
            StackFrames frames;
            int firstResolvableFrame = -1;
            for (uint j = 0; j < t.GetNumFrames(); j++) {
                lldb::SBFrame fr = t.GetFrameAtIndex(j);
                if (!fr.IsValid()) {
                    qDebug("warning: frame %i is garbage", j);
                    continue;
                }
                lldb::SBSymbolContext context =
                    fr.GetSymbolContext(lldb::eSymbolContextEverything);
                lldb::SBSymbol sym = fr.GetSymbol();
                lldb::SBFunction func = fr.GetFunction();
                lldb::SBCompileUnit tu = fr.GetCompileUnit();
                lldb::SBModule module = fr.GetModule();
                lldb::SBBlock block = fr.GetBlock();
                lldb::SBBlock fblock = fr.GetFrameBlock();
                lldb::SBLineEntry le = fr.GetLineEntry();
                lldb::SBValueList values = fr.GetVariables(true, true, true, false);
#if 0
                qDebug()<<"\tframe "<<fr.GetFrameID();
                qDebug() << "\t\tPC:     " << ("0x" + QByteArray::number(
                            fr.GetPCAddress().GetLoadAddress(*m_target), 16)).data();
                qDebug() << "\t\tFP:     " << ("0x" + QByteArray::number(fr.GetFP(), 16)).data();
                qDebug() << "\t\tSP:     " << ("0x" + QByteArray::number(fr.GetSP(), 16)).data();
                qDebug() << "\t\tsymbol:  " << sym.IsValid() << sym.GetName() << sym.GetMangledName();
                qDebug() << "\t\tfunction:" << func.IsValid();
                qDebug() << "\t\ttu:      " << tu.IsValid();
                if (tu.IsValid()) {

                }
                qDebug() << "\t\tmodule:  " << module.IsValid() << module.GetFileSpec().IsValid()
                    << module.GetFileSpec().GetFilename();
                qDebug() << "\t\tblock:   " << block.IsValid() << block.GetInlinedName();
                qDebug() << "\t\tfblock:  " << block.IsValid() << block.GetInlinedName();
                qDebug() << "\t\tle:      " << le.IsValid() << le.GetLine()<<le.GetColumn();
                qDebug() << "\t\tvalues:  "<<values.IsValid() << values.GetSize();
                qDebug() << "\t\tcontext: " << context.IsValid();
                qDebug() << "\t\t\tmodule:   " << context.GetModule().IsValid();
                qDebug() << "\t\t\tfunction: " << context.GetFunction().IsValid();
                qDebug() << "\t\t\tblock:    " << context.GetBlock().IsValid();
                qDebug() << "\t\t\tle:       " << context.GetLineEntry().IsValid();
                qDebug() << "\t\t\tsymbol:   " << context.GetSymbol().IsValid();
//            qDebug() << "\t\tdisassemly -->\n" << fr.Disassemble() << "<--";
#endif

                QString sourceFile;
                QString sourceFilePath;
                int lineNumber = 0;
                if (le.IsValid()) {
                    lineNumber = le.GetLine();
                    if (le.GetFileSpec().IsValid()) {
                        sourceFile = QString::fromLocal8Bit(le.GetFileSpec().GetFilename());
                        sourceFilePath = QString::fromLocal8Bit(le.GetFileSpec().GetDirectory())
                            + QLatin1String("/") + sourceFile;
                        if (firstResolvableFrame < 0)
                            firstResolvableFrame = j;
                    }
                }
529
530
                sourceFilePath = QFileInfo(sourceFilePath).canonicalFilePath();

531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
                QString functionName;
                if (func.IsValid())
                    functionName = QString::fromLocal8Bit(func.GetName());
                else
                    functionName = QString::fromLocal8Bit(sym.GetName());

                StackFrame frame;
                frame.level = fr.GetFrameID();
                if (func.IsValid())
                    frame.function = QString::fromLocal8Bit(func.GetName());
                else
                    frame.function = QString::fromLocal8Bit(sym.GetName());
                frame.from = QString::fromLocal8Bit(module.GetFileSpec().GetFilename());
                frame.address = fr.GetPCAddress().GetLoadAddress(*m_target);
                frame.line = lineNumber;
                frame.file = sourceFilePath;
547
                frame.usable = QFileInfo(frame.file).isReadable();
548
                frames.append(frame);
549
                m_frame_to_file.insert(j, frame.file);
550
551
552
553
554
555
556
557
558
            }
            currentThreadChanged(token);
            listFrames(frames);
            activateFrame(firstResolvableFrame > -1 ? firstResolvableFrame : 0);
            return;
        }
    }
}

hjk's avatar
hjk committed
559
void LldbEngineGuest::updateThreads()
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
{
    DEBUG_FUNC_ENTER;
    SYNC_INFERIOR_OR(return);

   /* There is no  way to find the StopReason of a _process_
     * We try to emulate gdb here, by assuming there must be exactly one 'guilty' thread.
     * However, if there are no threads at all, it must be that the process
     * no longer exists. Let's tear down the whole session.
     */
    if (m_process->GetNumThreads() < 1) {
        notifyEngineSpontaneousShutdown();
        m_process->Kill();
        m_process->Destroy();
    }

    Threads threads;
    for (uint i = 0; i < m_process->GetNumThreads(); i++) {
        lldb::SBThread t = m_process->GetThreadAtIndex(i);
        if (!t.IsValid()) {
                qDebug("warning: thread %i is garbage", i);
                continue;
        }
        ThreadData thread;
        thread.id = t.GetThreadID();
        thread.targetId = QString::number(t.GetThreadID());
        thread.core = QString();
        thread.state = QString::number(t.GetStopReason());

        switch (t.GetStopReason()) {
            case lldb::eStopReasonInvalid:
            case lldb::eStopReasonNone:
            case lldb::eStopReasonTrace:
                thread.state = QLatin1String("running");
                break;
            case lldb::eStopReasonBreakpoint:
            case lldb::eStopReasonWatchpoint:
                showStatusMessage(QLatin1String("hit breakpoint"));
                thread.state = QLatin1String("hit breakpoint");
                if (m_currentThread.GetThreadID() != t.GetThreadID()) {
                    m_currentThread = t;
                    currentThreadChanged(t.GetThreadID());
                    m_relistFrames = true;
                }
                break;
            case lldb::eStopReasonSignal:
                showStatusMessage(QLatin1String("stopped"));
                thread.state = QLatin1String("stopped");
                if (m_currentThread.GetThreadID() != t.GetThreadID()) {
                    m_currentThread = t;
                    currentThreadChanged(t.GetThreadID());
                    m_relistFrames = true;
                }
                break;
            case lldb::eStopReasonException:
                showStatusMessage(QLatin1String("application crashed."));
                thread.state = QLatin1String("crashed");
                if (m_currentThread.GetThreadID() != t.GetThreadID()) {
                    m_currentThread = t;
                    currentThreadChanged(t.GetThreadID());
                    m_relistFrames = true;
                }
                break;
            case lldb::eStopReasonPlanComplete:
                thread.state = QLatin1String("crazy things happened");
                break;
        };

        thread.lineNumber = 0;
        thread.name = QString::fromLocal8Bit(t.GetName());

        lldb::SBFrame fr = t.GetFrameAtIndex(0);
        if (!fr.IsValid()) {
            qDebug("warning: frame 0 is garbage");
            continue;
        }
        lldb::SBSymbolContext context = fr.GetSymbolContext(lldb::eSymbolContextEverything);
        lldb::SBSymbol sym = fr.GetSymbol();
        lldb::SBFunction func = fr.GetFunction();
        lldb::SBLineEntry le = fr.GetLineEntry();
        QString sourceFile;
        QString sourceFilePath;
        int lineNumber = 0;
        if (le.IsValid()) {
            lineNumber = le.GetLine();
            if (le.GetFileSpec().IsValid()) {
                sourceFile = QString::fromLocal8Bit(le.GetFileSpec().GetFilename());
                sourceFilePath = QString::fromLocal8Bit(le.GetFileSpec().GetDirectory())
                    + QLatin1String("/") + sourceFile;
            }
        }
        QString functionName;
        if (func.IsValid())
            functionName = QString::fromLocal8Bit(func.GetName());
        else
            functionName = QString::fromLocal8Bit(sym.GetName());

        lldb::SBValueList values = fr.GetVariables(true, true, false, false);
        thread.fileName = sourceFile;
        thread.function = functionName;
        thread.address = fr.GetPCAddress().GetLoadAddress(*m_target);
        thread.lineNumber = lineNumber;
        threads.append(thread);
    }
    listThreads(threads);
    if (m_relistFrames) {
        selectThread(m_currentThread.GetThreadID());
        m_relistFrames = false;
    }
}

hjk's avatar
hjk committed
670
void LldbEngineGuest::lldbEvent(lldb::SBEvent *ev)
671
672
{
    qDebug() << "lldbevent" << ev->GetType() <<
673
         m_process->GetState() << (int)state();
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

    uint32_t etype = ev->GetType();
    switch (etype) {
        // ProcessEvent
        case 1:
            switch (m_process->GetState()) {
                case lldb::eStateRunning: // 5
                    if (!m_running) {
                        m_running = true;
                    }
                    notifyInferiorPid(m_process->GetProcessID());
                    switch (state()) {
                        case EngineRunRequested:
                            notifyEngineRunAndInferiorRunOk();
                            break;
                        case InferiorRunRequested:
                            notifyInferiorRunOk();
                            break;
                        case InferiorStopOk:
                            notifyInferiorRunRequested();
                            notifyInferiorRunOk();
                            break;
                        default:
                            break;
                    }
                    break;
                case lldb::eStateExited: // 9
                    if (m_running) {
                        m_running = false;
                    }
                    switch (state()) {
                        case InferiorShutdownRequested:
                            notifyInferiorShutdownOk();
                            break;
                        case InferiorRunOk:
                            m_relistFrames = true;
                            updateThreads();
                            notifyEngineSpontaneousShutdown();
                            m_process->Kill();
                            m_process->Destroy();
                            break;
                        default:
                            updateThreads();
                            break;
                    }
                    break;
                case lldb::eStateStopped: // 4
                    if (m_running) {
                        m_running = false;
                    }
                    switch (state()) {
                        case InferiorShutdownRequested:
                            notifyInferiorShutdownOk();
                            break;
                        case InferiorRunOk:
                            m_relistFrames = true;
                            updateThreads();
                            notifyInferiorSpontaneousStop();
                            // fall
                        default:
                            m_relistFrames = true;
                            updateThreads();
                            break;
                    }
                    break;
                case lldb::eStateCrashed: // 7
                    if (m_running) {
                        m_running = false;
                    }
                    switch (state()) {
                        case InferiorShutdownRequested:
                            notifyInferiorShutdownOk();
                            break;
                        case InferiorRunOk:
                            m_relistFrames = true;
                            updateThreads();
                            notifyInferiorSpontaneousStop();
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    qDebug("unexpected ProcessEvent");
                    break;
            }
            break;;
        default:
            break;;
    };
}


} // namespace Internal
} // namespace Debugger