lldbengineguest.cpp 25 KB
Newer Older
1
2
3
4
5
6
7
8
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** 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.
15
16
17
18
19
20
21
22
23
24
**
** 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.
**
con's avatar
con committed
25
26
27
28
29
30
** 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.
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
**
**************************************************************************/

#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
67
#define SYNC_INFERIOR_OR(x)  if (m_running) { x; }
68
69
70
71
72


namespace Debugger {
namespace Internal {

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

hjk's avatar
hjk committed
82
LldbEngineGuest::LldbEngineGuest()
83
84
    : IPCEngineGuest()
    , m_running (false)
hjk's avatar
hjk committed
85
    , m_worker  (new LldbEventListener)
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    , 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
105
LldbEngineGuest::~LldbEngineGuest()
106
107
108
109
110
111
112
{
    delete m_lldb;
    delete m_target;
    delete m_process;
    delete m_listener;
}

113
114
115
116
117
118

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

hjk's avatar
hjk committed
119
void LldbEngineGuest::setupEngine()
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{
    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
134
void LldbEngineGuest::setupInferior(const QString &executable,
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
        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
155
    const char **argp = new const char *[m_arguments.count() + 1];
156
157
158
159
160
    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
161
    const char **envp = new const char *[m_environment.count() + 1];
162
163
164
165
    envp[m_environment.count()] = 0;
    for (int i = 0; i < m_environment.count(); i++) {
        envp[i] = m_environment[i].data();
    }
166
    lldb::SBError err;
167
    *m_process = m_target->Launch(argp, envp, NULL, NULL, true, err);
168
169
170
171

    if (!err.Success()) {
        showMessage(QString::fromLocal8Bit(err.GetCString()));
        qDebug() << err.GetCString();
172
        notifyInferiorSetupFailed();
173
    }
174
175
176
177
178
179
180
181
182

    /*
     * 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();
183
    QTC_ASSERT(m_listener->IsValid(), qDebug() << false);
184
185
186
    m_listener->StartListeningForEvents(m_process->GetBroadcaster(), UINT32_MAX);
    QMetaObject::invokeMethod(m_worker, "listen", Qt::QueuedConnection,
            Q_ARG(lldb::SBListener *, m_listener));
187
188
189
190
191
192
193
    notifyInferiorSetupOk();
}

void LldbEngineGuest::runEngine()
{
    DEBUG_FUNC_ENTER;
    m_process->Continue();
194
195
}

hjk's avatar
hjk committed
196
void LldbEngineGuest::shutdownInferior()
197
198
199
200
201
{
    DEBUG_FUNC_ENTER;
    m_process->Kill();
}

hjk's avatar
hjk committed
202
void LldbEngineGuest::shutdownEngine()
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{
    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
220
void LldbEngineGuest::detachDebugger()
221
222
223
224
{
    DEBUG_FUNC_ENTER;
}

hjk's avatar
hjk committed
225
void LldbEngineGuest::executeStep()
226
227
228
229
230
231
232
233
{
    DEBUG_FUNC_ENTER;

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

hjk's avatar
hjk committed
234
void LldbEngineGuest::executeStepOut()
235
236
237
238
239
240
241
242
{
    DEBUG_FUNC_ENTER;

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

hjk's avatar
hjk committed
243
void LldbEngineGuest::executeNext()
244
245
246
247
248
249
250
251
{
    DEBUG_FUNC_ENTER;

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

hjk's avatar
hjk committed
252
void LldbEngineGuest::executeStepI()
253
254
255
256
257
258
259
260
{
    DEBUG_FUNC_ENTER;

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

hjk's avatar
hjk committed
261
void LldbEngineGuest::executeNextI()
262
263
264
265
266
267
268
269
{
    DEBUG_FUNC_ENTER;

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

hjk's avatar
hjk committed
270
void LldbEngineGuest::continueInferior()
271
272
273
274
275
276
277
{
    DEBUG_FUNC_ENTER;

    notifyInferiorRunRequested();
    m_process->Continue();
    showStatusMessage(QLatin1String("resuming inferior"));
}
hjk's avatar
hjk committed
278
void LldbEngineGuest::interruptInferior()
279
280
281
282
283
284
285
286
287
{
    DEBUG_FUNC_ENTER;

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

hjk's avatar
hjk committed
288
void LldbEngineGuest::executeRunToLine(const QString &fileName, int lineNumber)
289
290
291
292
293
294
295
296
{
    DEBUG_FUNC_ENTER;

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

hjk's avatar
hjk committed
297
void LldbEngineGuest::executeRunToFunction(const QString &functionName)
298
299
300
301
302
303
{
    DEBUG_FUNC_ENTER;

    // TODO
    Q_UNUSED(functionName);
}
hjk's avatar
hjk committed
304
void LldbEngineGuest::executeJumpToLine(const QString &fileName, int lineNumber)
305
306
307
308
309
310
311
312
{
    DEBUG_FUNC_ENTER;

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

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

    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
337
void LldbEngineGuest::requestUpdateWatchData(const Internal::WatchData &data,
338
339
340
341
342
343
344
345
346
347
348
349
350
351
        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
352
void LldbEngineGuest::getWatchDataR(lldb::SBValue v, int level,
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
        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
379
void LldbEngineGuest::disassemble(quint64 pc)
380
381
382
383
384
385
386
387
388
{
    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)) {
389
390
391
392
393
394
            QString linesStr = QString::fromLocal8Bit(fr.Disassemble());
            DisassemblerLines lines;
            foreach (const QString &lineStr, linesStr.split(QLatin1Char('\n'))) {
                lines.appendLine(DisassemblerLine(lineStr));
            }
            disassembled(pc, lines);
395
396
397
        }
    }
}
398
399
400
401
402
403
404
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()));
}
405

hjk's avatar
hjk committed
406
void LldbEngineGuest::addBreakpoint(BreakpointId id,
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
        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);
431
            showMessage(QLatin1String("[BB] ok."));
432
433
434
            notifyBreakpointAdjusted(id, bp);
        } else {
            m_breakpoints.take(id);
435
436
437
            showMessage(QLatin1String("[BB] failed. cant resolve yet"));
//            notifyAddBreakpointFailed(id);
//            notifyAddBreakpointOk(id);
438
439
440
441
442
443
444
        }
    } else {
        showMessage(QLatin1String("[BB] failed. dunno."));
        notifyAddBreakpointFailed(id);
    }
}

hjk's avatar
hjk committed
445
void LldbEngineGuest::removeBreakpoint(BreakpointId id)
446
447
448
449
450
451
452
453
454
{
    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
455
void LldbEngineGuest::changeBreakpoint(BreakpointId id,
456
457
458
459
460
461
462
463
464
        const Internal::BreakpointParameters &bp)
{
    DEBUG_FUNC_ENTER;

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

hjk's avatar
hjk committed
465
void LldbEngineGuest::selectThread(qint64 token)
466
467
468
469
{
    DEBUG_FUNC_ENTER;
    SYNC_INFERIOR_OR(return);

470
    m_frame_to_file.clear();
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
    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;
                    }
                }
533
534
                sourceFilePath = QFileInfo(sourceFilePath).canonicalFilePath();

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
                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;
551
                frame.usable = QFileInfo(frame.file).isReadable();
552
                frames.append(frame);
553
                m_frame_to_file.insert(j, frame.file);
554
555
556
557
558
559
560
561
562
            }
            currentThreadChanged(token);
            listFrames(frames);
            activateFrame(firstResolvableFrame > -1 ? firstResolvableFrame : 0);
            return;
        }
    }
}

hjk's avatar
hjk committed
563
void LldbEngineGuest::updateThreads()
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
{
    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
674
void LldbEngineGuest::lldbEvent(lldb::SBEvent *ev)
675
676
{
    qDebug() << "lldbevent" << ev->GetType() <<
677
         m_process->GetState() << (int)state();
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

    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