lldbbridge.py 55.8 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
############################################################################
#
# Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
# Contact: http://www.qt-project.org/legal
#
# This file is part of Qt Creator.
#
# Commercial License Usage
# Licensees holding valid commercial Qt licenses may use this file in
# accordance with the commercial license agreement provided with the
# Software or, alternatively, in accordance with the terms contained in
# a written agreement between you and Digia.  For licensing terms and
# conditions see http://qt.digia.com/licensing.  For further information
# use the contact form at http://qt.digia.com/contact-us.
#
# 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, Digia gives you certain additional
# rights.  These rights are described in the Digia Qt LGPL Exception
# version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
#
#############################################################################
29

30
import atexit
31
import binascii
32
import inspect
33
import json
34
import os
35
import re
36
37
import select
import sys
38
import subprocess
39
import threading
40

41
42
43
44
45
currentDir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
sys.path.insert(1, currentDir)

from dumper import *
from qttypes import *
hjk's avatar
hjk committed
46
47
48
from stdtypes import *
from misctypes import *
from boosttypes import *
49
from creatortypes import *
50

51
52
53
lldbCmd = 'lldb'
if len(sys.argv) > 1:
    lldbCmd = sys.argv[1]
54

55
proc = subprocess.Popen(args=[lldbCmd, '-P'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
56
(path, error) = proc.communicate()
57

58
if error.startswith('lldb: invalid option -- P'):
59
    sys.stdout.write('msg=\'Could not run "%s -P". Trying to find lldb.so from Xcode.\'@\n' % lldbCmd)
60
61
62
63
64
    proc = subprocess.Popen(args=['xcode-select', '--print-path'],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (path, error) = proc.communicate()
    if len(error):
        path = '/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/Resources/Python/'
65
66
        sys.stdout.write('msg=\'Could not run "xcode-select --print-path"@\n')
        sys.stdout.write('msg=\'Using hardcoded fallback at %s\'@\n' % path)
67
68
    else:
        path = path.strip() + '/../SharedFrameworks/LLDB.framework/Versions/A/Resources/Python/'
69
        sys.stdout.write('msg=\'Using fallback at %s\'@\n' % path)
70

71
sys.path.insert(1, path.strip())
72
73

import lldb
74
75
76
77
78
79
80

#######################################################################
#
# Helpers
#
#######################################################################

81
qqWatchpointOffset = 10000
82

83
84

def warn(message):
85
    print('\n\nWARNING="%s",\n' % message.encode("latin1").replace('"', "'"))
86
87
88

def showException(msg, exType, exValue, exTraceback):
    warn("**** CAUGHT EXCEPTION: %s ****" % msg)
89
90
91
    import traceback
    lines = [line for line in traceback.format_exception(exType, exValue, exTraceback)]
    warn('\n'.join(lines))
92
93
94
95

def fileName(file):
    return str(file) if file.IsValid() else ''

96

97
98
99
100
101
102
103
104
105
106
# Breakpoints. Keep synchronized with BreakpointType in breakpoint.h
UnknownType = 0
BreakpointByFileAndLine = 1
BreakpointByFunction = 2
BreakpointByAddress = 3
BreakpointAtThrow = 4
BreakpointAtCatch = 5
BreakpointAtMain = 6
BreakpointAtFork = 7
BreakpointAtExec = 8
107
108
109
110
111
BreakpointAtSysCall = 9
WatchpointAtAddress = 10
WatchpointAtExpression = 11
BreakpointOnQmlSignalEmit = 12
BreakpointAtJavaScriptThrow = 13
112

113
114
115
# See db.StateType
stateNames = ["invalid", "unloaded", "connected", "attaching", "launching", "stopped",
    "running", "stepping", "crashed", "detached", "exited", "suspended" ]
116

117
118
119
def loggingCallback(args):
    s = args.strip()
    s = s.replace('"', "'")
120
    sys.stdout.write('log="%s"@\n' % s)
121

hjk's avatar
hjk committed
122
123
124
125
def check(exp):
    if not exp:
        raise RuntimeError("Check failed")

126
127
Value = lldb.SBValue

128
129
def impl_SBValue__add__(self, offset):
    if self.GetType().IsPointerType():
130
131
132
133
        if isinstance(offset, int) or isinstance(offset, long):
            pass
        else:
            offset = offset.GetValueAsSigned()
134
        itemsize = self.GetType().GetPointeeType().GetByteSize()
135
        address = self.GetValueAsUnsigned() + offset * itemsize
136
        address = address & 0xFFFFFFFFFFFFFFFF  # Force unsigned
137
138
        return self.CreateValueFromAddress(None, address,
                self.GetType().GetPointeeType()).AddressOf()
hjk's avatar
hjk committed
139

140
    raise RuntimeError("SBValue.__add__ not implemented: %s" % self.GetType())
141
142
    return NotImplemented

143
def impl_SBValue__sub__(self, other):
144
    if self.GetType().IsPointerType():
145
146
147
148
        if isinstance(other, int) or isinstance(other, long):
            address = self.GetValueAsUnsigned() - offset.GetValueAsSigned()
            address = address & 0xFFFFFFFFFFFFFFFF  # Force unsigned
            return self.CreateValueFromAddress(None, address, self.GetType())
149
        if other.GetType().IsPointerType():
150
151
            itemsize = self.GetType().GetPointeeType().GetByteSize()
            return (self.GetValueAsUnsigned() - other.GetValueAsUnsigned()) / itemsize
152
    raise RuntimeError("SBValue.__sub__ not implemented: %s" % self.GetType())
153
154
    return NotImplemented

155
156
157
158
159
160
161
def impl_SBValue__le__(self, other):
    if self.GetType().IsPointerType() and other.GetType().IsPointerType():
        return int(self) <= int(other)
    raise RuntimeError("SBValue.__le__ not implemented")
    return NotImplemented

def impl_SBValue__int__(self):
162
    return self.GetValueAsSigned()
163
164
165
166
167
168
169
170
171
172

def impl_SBValue__float__(self):
    error = lldb.SBError()
    if self.GetType().GetByteSize() == 4:
        result = self.GetData().GetFloat(error, 0)
    else:
        result = self.GetData().GetDouble(error, 0)
    if error.Success():
        return result
    return NotImplemented
173

174
175
176
def impl_SBValue__long__(self):
    return int(self.GetValue(), 0)

177
178
179
180
181
182
183
184
185
186
def impl_SBValue__getitem__(value, index):
    if isinstance(index, int):
        type = value.GetType()
        if type.IsPointerType():
            innertype = value.Dereference().GetType()
            address = value.GetValueAsUnsigned() + index * innertype.GetByteSize()
            address = address & 0xFFFFFFFFFFFFFFFF  # Force unsigned
            return value.CreateValueFromAddress(None, address, innertype)
        return value.GetChildAtIndex(index)
    return value.GetChildMemberWithName(index)
187

188
lldb.SBValue.__add__ = impl_SBValue__add__
189
lldb.SBValue.__sub__ = impl_SBValue__sub__
190
lldb.SBValue.__le__ = impl_SBValue__le__
191

192
193
lldb.SBValue.__getitem__ = impl_SBValue__getitem__
lldb.SBValue.__int__ = impl_SBValue__int__
194
lldb.SBValue.__float__ = impl_SBValue__float__
195
196
lldb.SBValue.__long__ = lambda self: long(self.GetValue(), 0)

197
lldb.SBValue.code = lambda self: self.GetTypeClass()
198
199
lldb.SBValue.cast = lambda self, typeObj: self.Cast(typeObj)
lldb.SBValue.dereference = lambda self: self.Dereference()
200
lldb.SBValue.address = property(lambda self: self.GetAddress())
201
202

lldb.SBType.pointer = lambda self: self.GetPointerType()
hjk's avatar
hjk committed
203
lldb.SBType.target = lambda self: self.GetPointeeType()
204
lldb.SBType.code = lambda self: self.GetTypeClass()
205
lldb.SBType.sizeof = property(lambda self: self.GetByteSize())
206
207


hjk's avatar
hjk committed
208
209
lldb.SBType.unqualified = \
    lambda self: self.GetUnqualifiedType() if hasattr(self, 'GetUnqualifiedType') else self
210
211
lldb.SBType.strip_typedefs = \
    lambda self: self.GetCanonicalType() if hasattr(self, 'GetCanonicalType') else self
212

hjk's avatar
hjk committed
213
214
215
lldb.SBType.__orig__str__ = lldb.SBType.__str__
lldb.SBType.__str__ = lldb.SBType.GetName

216
217
218
219
def simpleEncoding(typeobj):
    code = typeobj.GetTypeClass()
    size = typeobj.sizeof
    if code == lldb.eTypeClassBuiltin:
hjk's avatar
hjk committed
220
221
222
223
224
225
        name = str(typeobj)
        if name == "float":
            return Hex2EncodedFloat4
        if name == "double":
            return Hex2EncodedFloat8
        if name.find("unsigned") >= 0:
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
            if size == 1:
                return Hex2EncodedUInt1
            if size == 2:
                return Hex2EncodedUInt2
            if size == 4:
                return Hex2EncodedUInt4
            if size == 8:
                return Hex2EncodedUInt8
        else:
            if size == 1:
                return Hex2EncodedInt1
            if size == 2:
                return Hex2EncodedInt2
            if size == 4:
                return Hex2EncodedInt4
            if size == 8:
                return Hex2EncodedInt8
    return None
hjk's avatar
hjk committed
244

245
class Dumper(DumperBase):
246
    def __init__(self):
247
248
        DumperBase.__init__(self)

249
        self.debugger = lldb.SBDebugger.Create()
250
        #self.debugger.SetLoggingCallback(loggingCallback)
251
252
253
254
255
256
        #Same as: self.debugger.HandleCommand("log enable lldb dyld step")
        #self.debugger.EnableLog("lldb", ["dyld", "step", "process", "state", "thread", "events",
        #    "communication", "unwind", "commands"])
        #self.debugger.EnableLog("lldb", ["all"])
        self.debugger.Initialize()
        self.debugger.HandleCommand("settings set auto-confirm on")
257
258
259
260
261
262
263
264
265
266

        # FIXME: warn("DISABLING DEFAULT FORMATTERS")
        # It doesn't work at all with 179.5 and we have some bad
        # interactonn in 3000
        # if not hasattr(lldb.SBType, 'GetCanonicalType'): # "Test" for 179.5
        self.debugger.HandleCommand('type category delete gnu-libstdc++')
        self.debugger.HandleCommand('type category delete libcxx')
        #for i in range(self.debugger.GetNumCategories()):
        #    self.debugger.GetCategoryAtIndex(i).SetEnabled(False)

267
        self.isLldb = True
268
269
270
271
        self.process = None
        self.target = None
        self.eventState = lldb.eStateInvalid
        self.expandedINames = {}
272
        self.passExceptions = False
273
        self.useLldbDumpers = False
hjk's avatar
hjk committed
274
        self.autoDerefPointers = True
275
        self.useDynamicType = True
hjk's avatar
hjk committed
276
        self.useFancy = True
hjk's avatar
hjk committed
277
278
279
        self.formats = {}
        self.typeformats = {}
        self.currentAddress = None
280
281
282
283
284
285

        self.currentIName = None
        self.currentValuePriority = -100
        self.currentValueEncoding = None
        self.currentType = ""
        self.currentTypePriority = -100
286
        self.currentValue = None
287
288
289
290
291
        self.currentNumChild = None
        self.currentMaxNumChild = None
        self.currentPrintsAddress = None
        self.currentChildType = None
        self.currentChildNumChild = None
292
        self.currentWatchers = {}
293

hjk's avatar
hjk committed
294
        self.executable_ = None
295
296
297
298
        self.startMode_ = None
        self.processArgs_ = None
        self.attachPid_ = None

299
300
        self.charType_ = None
        self.intType_ = None
301
        self.int64Type_ = None
302
303
        self.sizetType_ = None
        self.charPtrType_ = None
hjk's avatar
hjk committed
304
        self.voidPtrType_ = None
305
        self.isShuttingDown_ = False
306
        self.isInterrupting_ = False
307
        self.dummyValue = None
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
    def enterSubItem(self, item):
        if isinstance(item.name, lldb.SBValue):
            # Avoid $$__synth__ suffix on Mac.
            value = item.name
            value.SetPreferSyntheticValue(False)
            item.name = value.GetName()
            if item.name is None:
                self.anonNumber += 1
                item.name = "#%d" % self.anonNumber
        item.iname = "%s.%s" % (self.currentIName, item.name)
        self.put('{')
        #if not item.name is None:
        if isinstance(item.name, str):
            if item.name == '**&':
                item.name = '*'
            self.put('name="%s",' % item.name)
        item.savedIName = self.currentIName
        item.savedCurrentAddress = self.currentAddress
        item.savedValue = self.currentValue
        item.savedValuePriority = self.currentValuePriority
        item.savedValueEncoding = self.currentValueEncoding
        item.savedType = self.currentType
        item.savedTypePriority = self.currentTypePriority
        self.currentIName = item.iname
        self.currentValuePriority = -100
        self.currentValueEncoding = None
        self.currentType = ""
        self.currentTypePriority = -100

    def exitSubItem(self, item, exType, exValue, exTraceBack):
        if not exType is None:
            if self.passExceptions:
                showException("SUBITEM", exType, exValue, exTraceBack)
            self.putNumChild(0)
            self.putValue("<not accessible>")
        try:
            typeName = self.currentType
            if len(typeName) > 0 and typeName != self.currentChildType:
                self.put('type="%s",' % typeName) # str(type.unqualified()) ?
            if  self.currentValue is None:
                self.put('value="<not accessible>",numchild="0",')
            else:
                if not self.currentValueEncoding is None:
                    self.put('valueencoded="%d",' % self.currentValueEncoding)
                self.put('value="%s",' % self.currentValue)
        except:
            pass
        if not self.currentAddress is None:
            self.put(self.currentAddress)
        self.put('},')
        self.currentIName = item.savedIName
        self.currentValue = item.savedValue
        self.currentValuePriority = item.savedValuePriority
        self.currentValueEncoding = item.savedValueEncoding
        self.currentType = item.savedType
        self.currentTypePriority = item.savedTypePriority
        self.currentAddress = item.savedCurrentAddress
        return True

    def isSimpleType(self, typeobj):
        typeClass = typeobj.GetTypeClass()
        return typeClass == lldb.eTypeClassBuiltin

    def childAt(self, value, index):
        return value.GetChildAtIndex(index)

    def fieldAt(self, type, index):
        return type.GetFieldAtIndex(index)

    def pointerValue(self, value):
        return value.GetValueAsUnsigned()

    def call2(self, value, func, args):
        # args is a tuple.
        arg = ','.join(args)
        #warn("CALL: %s -> %s(%s)" % (value, func, arg))
        type = value.type.name
        exp = "((%s*)%s)->%s(%s)" % (type, value.address, func, arg)
        #warn("CALL: %s" % exp)
        result = value.CreateValueFromExpression('$tmp', exp)
        #warn("  -> %s" % result)
        return result

392
393
394
395
396
    def parseAndEvaluate(self, expr):
        thread = self.currentThread()
        frame = thread.GetFrameAtIndex(0)
        return frame.EvaluateExpression(expr)

397
398
399
400
401
402
403
404
405
406
407
408
409
    def call(self, value, func, *args):
        return self.call2(value, func, args)

    def checkPointer(self, p, align = 1):
        if not self.isNull(p):
            p.Dereference()

    def isNull(self, p):
        return p.GetValueAsUnsigned() == 0

    def directBaseClass(self, typeobj, index = 0):
        return typeobj.GetDirectBaseClassAtIndex(index)

410
    def templateArgument(self, typeobj, index):
411
        type = typeobj.GetTemplateArgumentType(index)
hjk's avatar
hjk committed
412
        if type.IsValid():
413
414
415
416
417
418
419
            return type
        inner = self.extractTemplateArgument(typeobj.GetName(), index)
        return self.lookupType(inner)

    def numericTemplateArgument(self, typeobj, index):
        inner = self.extractTemplateArgument(typeobj.GetName(), index)
        return int(inner)
420

hjk's avatar
hjk committed
421
422
423
424
425
426
    def isReferenceType(self, typeobj):
        return typeobj.IsReferenceType()

    def isStructType(self, typeobj):
        return typeobj.GetTypeClass() in (lldb.eTypeClassStruct, lldb.eTypeClassClass)

427
    def qtVersion(self):
428
        self.cachedQtVersion = 0x0
429
430
431
432
433
434
435
436
        coreExpression = re.compile(r"(lib)?Qt5?Core")
        for n in range(0, self.target.GetNumModules()):
            module = self.target.GetModuleAtIndex(n)
            if coreExpression.match(module.GetFileSpec().GetFilename()):
                reverseVersion = module.GetVersion()
                reverseVersion.reverse()
                shift = 0
                for v in reverseVersion:
437
                    self.cachedQtVersion += v << shift
438
439
                    shift += 8
                break
440
441
442
443

        # Memoize good results.
        self.qtVersion = lambda: self.cachedQtVersion
        return self.cachedQtVersion
444

hjk's avatar
hjk committed
445
446
447
    def intSize(self):
        return 4

448
449
    def intType(self):
        if self.intType_ is None:
hjk's avatar
hjk committed
450
             self.intType_ = self.target.FindFirstType('int')
451
452
        return self.intType_

453
454
455
456
457
    def int64Type(self):
        if self.int64Type_ is None:
             self.int64Type_ = self.target.FindFirstType('long long int')
        return self.int64Type_

458
459
    def charType(self):
        if self.charType_ is None:
hjk's avatar
hjk committed
460
             self.charType_ = self.target.FindFirstType('char')
461
462
463
464
465
466
467
468
        return self.charType_

    def charPtrType(self):
        if self.charPtrType_ is None:
             self.charPtrType_ = self.charType().GetPointerType()
        return self.charPtrType_

    def voidPtrType(self):
hjk's avatar
hjk committed
469
470
471
        if self.voidPtrType_ is None:
             self.voidPtrType_ = self.target.FindFirstType('void').GetPointerType()
        return self.voidPtrType_
472

hjk's avatar
hjk committed
473
    def ptrSize(self):
474
475
476
477
478
479
480
        return self.charPtrType().GetByteSize()

    def sizetType(self):
        if self.sizetType_ is None:
             self.sizetType_ = self.lookupType('size_t')
        return self.sizetType_

481
482
483
    def addressOf(self, value):
        return int(value.GetLoadAddress())

hjk's avatar
hjk committed
484
485
486
487
488
489
490
491
492
    def dereferenceValue(self, value):
        return long(value.Cast(self.voidPtrType()))

    def dereference(self, address):
        return long(self.createValue(address, self.voidPtrType()))

    def extractInt(self, address):
        return int(self.createValue(address, self.intType()))

493
494
495
    def extractInt64(self, address):
        return int(self.createValue(address, self.int64Type()))

496
497
498
    def extractByte(self, address):
        return int(self.createValue(address, self.charType())) & 0xFF

499
500
501
502
503
504
    def handleCommand(self, command):
        result = lldb.SBCommandReturnObject()
        self.debugger.GetCommandInterpreter().HandleCommand(command, result)
        success = result.Succeeded()
        if success:
            self.report('output="%s"' % result.GetOutput())
505
        else:
506
507
508
509
510
511
            self.report('error="%s"' % result.GetError())
        self.reportData()

    def put(self, stuff):
        sys.stdout.write(stuff)

512
    def isMovableType(self, type):
513
        if type.GetTypeClass() in (lldb.eTypeClassBuiltin, lldb.eTypeClassPointer):
514
            return True
515
        return self.isKnownMovableType(self.stripNamespaceFromType(type.GetName()))
516

517
518
    def putNumChild(self, numchild):
        #warn("NUM CHILD: '%s' '%s'" % (numchild, self.currentChildNumChild))
hjk's avatar
hjk committed
519
520
        #if numchild != self.currentChildNumChild:
        self.put('numchild="%s",' % numchild)
521

hjk's avatar
hjk committed
522
523
524
    def putSimpleValue(self, value, encoding = None, priority = 0):
        self.putValue(value.GetValue(), encoding, priority)

525
    def tryPutArrayContents(self, typeobj, base, n):
526
        if not self.isSimpleType(typeobj):
527
528
529
            return False
        size = n * typeobj.sizeof
        self.put('childtype="%s",' % typeobj)
530
531
        self.put('addrbase="0x%x",' % int(base))
        self.put('addrstep="%d",' % typeobj.sizeof)
532
533
        self.put('arrayencoding="%s",' % simpleEncoding(typeobj))
        self.put('arraydata="')
534
        self.put(self.readMemory(base, size))
535
536
537
538
        self.put('",')
        return True

    def putPlotData(self, type, base, n, plotFormat):
hjk's avatar
hjk committed
539
        #warn("PLOTDATA: %s %s" % (type, n))
540
541
        if self.isExpanded():
            self.putArrayData(type, base, n)
hjk's avatar
hjk committed
542
543
        self.putValue(self.currentValue)
        self.putField("plottable", "0")
544
545
546
547

    def putArrayData(self, type, base, n,
            childNumChild = None, maxNumChild = 10000):
        if not self.tryPutArrayContents(type, base, n):
548
            base = self.createPointerValue(base, type)
549
550
551
552
553
            with Children(self, n, type, childNumChild, maxNumChild,
                    base, type.GetByteSize()):
                for i in self.childRange():
                    self.putSubItem(i, (base + i).dereference())

554
555
556
    def parseAndEvalute(self, expr):
        return expr

557
558
559
560
561
562
563
564
    def createPointerValue(self, address, pointeeType):
        addr = int(address) & 0xFFFFFFFFFFFFFFFF
        return self.context.CreateValueFromAddress(None, addr, pointeeType).AddressOf()

    def createValue(self, address, referencedType):
        addr = int(address) & 0xFFFFFFFFFFFFFFFF
        return self.context.CreateValueFromAddress(None, addr, referencedType)

565
    def putCallItem(self, name, value, func, *args):
566
        result = self.call2(value, func, args)
567
568
569
        with SubItem(self, name):
            self.putItem(result)

570
571
572
573
574
575
    def childRange(self):
        if self.currentMaxNumChild is None:
            return xrange(0, self.currentNumChild)
        return xrange(min(self.currentMaxNumChild, self.currentNumChild))

    def lookupType(self, name):
hjk's avatar
hjk committed
576
        #warn("LOOKUP TYPE NAME: %s" % name)
hjk's avatar
hjk committed
577
578
579
580
        if name.endswith('*'):
            type = self.lookupType(name[:-1].strip())
            return type.GetPointerType() if type.IsValid() else None
        type = self.target.FindFirstType(name)
hjk's avatar
hjk committed
581
582
        #warn("LOOKUP RESULT: %s" % type.name)
        #warn("LOOKUP VALID: %s" % type.IsValid())
hjk's avatar
hjk committed
583
        return type if type.IsValid() else None
584

585
    def setupInferior(self, args):
586
        error = lldb.SBError()
587
588

        self.executable_ = args['executable']
589
590
591
        self.startMode_ = args.get('startMode', 1)
        self.processArgs_ = args.get('processArgs', '')
        self.attachPid_ = args.get('attachPid', 0)
Fawzi Mohamed's avatar
Fawzi Mohamed committed
592
        self.sysRoot_ = args.get('sysRoot', '')
Fawzi Mohamed's avatar
Fawzi Mohamed committed
593
        self.remoteChannel_ = args.get('remoteChannel', '')
594
        self.platform_ = args.get('platform', '')
595

596
        if self.sysRoot_:
Fawzi Mohamed's avatar
Fawzi Mohamed committed
597
            self.debugger.SetCurrentPlatformSDKRoot(self.sysRoot_)
598
599
        if self.platform_:
            self.debugger.SetCurrentPlatform(self.platform_)
600
        self.target = self.debugger.CreateTarget(self.executable_, None, None, True, error)
601
602
        self.importDumpers()

603
604
        state = "inferiorsetupok" if self.target.IsValid() else "inferiorsetupfailed"
        self.report('state="%s",msg="%s",exe="%s"' % (state, error, self.executable_))
605

606
    def runEngine(self, _):
hjk's avatar
hjk committed
607
608
609
610
        s = threading.Thread(target=self.loop, args=[])
        s.start()

    def loop(self):
611
        error = lldb.SBError()
hjk's avatar
hjk committed
612
613
        listener = self.debugger.GetListener()

614
615
616
        if self.attachPid_ > 0:
            attachInfo = lldb.SBAttachInfo(self.attachPid_)
            self.process = self.target.Attach(attachInfo, error)
617
            if not error.Success():
Fawzi Mohamed's avatar
Fawzi Mohamed committed
618
619
620
                self.report('state="inferiorrunfailed"')
                return
            self.report('pid="%s"' % self.process.GetProcessID())
621
622
623
624
            # even if it stops it seems that lldb assumes it is running and later detects that
            # it did stop after all, so it is be better to mirror that and wait for the spontaneous
            # stop
            self.report('state="enginerunandinferiorrunok"')
Fawzi Mohamed's avatar
Fawzi Mohamed committed
625
626
627
628
        elif len(self.remoteChannel_) > 0:
            self.process = self.target.ConnectRemote(
            self.debugger.GetListener(),
            self.remoteChannel_, None, error)
629
630
631
            if not error.Success():
                self.report('state="inferiorrunfailed"')
                return
632
633
634
635
            # even if it stops it seems that lldb assumes it is running and later detects that
            # it did stop after all, so it is be better to mirror that and wait for the spontaneous
            # stop
            self.report('state="enginerunandinferiorrunok"')
636
        else:
637
            launchInfo = lldb.SBLaunchInfo(self.processArgs_.split())
638
            launchInfo.SetWorkingDirectory(os.getcwd())
639
640
            environmentList = [key + "=" + value for key,value in os.environ.items()]
            launchInfo.SetEnvironmentEntries(environmentList, False)
641
            self.process = self.target.Launch(launchInfo, error)
642
            if not error.Success():
643
644
                self.reportError(error)
                self.report('state="enginerunfailed"')
Fawzi Mohamed's avatar
Fawzi Mohamed committed
645
646
647
                return
            self.report('pid="%s"' % self.process.GetProcessID())
            self.report('state="enginerunandinferiorrunok"')
648

hjk's avatar
hjk committed
649
650
651
652
653
654
        event = lldb.SBEvent()
        while True:
            if listener.WaitForEvent(10000000, event):
                self.handleEvent(event)
            else:
                warn('TIMEOUT')
655

656
    def describeError(self, error):
657
658
659
660
661
662
        desc = lldb.SBStream()
        error.GetDescription(desc)
        result = 'error={type="%s"' % error.GetType()
        result += ',code="%s"' % error.GetError()
        result += ',msg="%s"' % error.GetCString()
        result += ',desc="%s"}' % desc.GetData()
663
664
665
666
        return result

    def reportError(self, error):
        self.report(self.describeError(error))
667
668

    def currentThread(self):
669
        return self.process.GetSelectedThread()
670
671
672
673
674
675

    def currentFrame(self):
        return self.currentThread().GetSelectedFrame()

    def reportLocation(self):
        thread = self.currentThread()
676
        frame = thread.GetSelectedFrame()
677
678
679
680
        file = fileName(frame.line_entry.file)
        line = frame.line_entry.line
        self.report('location={file="%s",line="%s",addr="%s"}' % (file, line, frame.pc))

681
682
683
    def firstStoppedThread(self):
        for i in xrange(0, self.process.GetNumThreads()):
            thread = self.process.GetThreadAtIndex(i)
684
685
686
687
688
689
            reason = thread.GetStopReason()
            if (reason == lldb.eStopReasonBreakpoint or
                    reason == lldb.eStopReasonException or
                    reason == lldb.eStopReasonPlanComplete or
                    reason == lldb.eStopReasonSignal or
                    reason == lldb.eStopReasonWatchpoint):
690
691
692
                return thread
        return None

693
    def reportThreads(self):
694
695
        reasons = ['None', 'Trace', 'Breakpoint', 'Watchpoint', 'Signal', 'Exception',
            'Exec', 'PlanComplete']
696
        result = 'threads={threads=['
697
        for i in xrange(0, self.process.GetNumThreads()):
698
            thread = self.process.GetThreadAtIndex(i)
699
            stopReason = thread.GetStopReason()
700
701
            result += '{id="%d"' % thread.GetThreadID()
            result += ',index="%s"' % i
702
            result += ',details="%s"' % thread.GetQueueName()
703
704
705
            result += ',stop-reason="%s"' % stopReason
            if stopReason >= 0 and stopReason < len(reasons):
                result += ',state="%s"' % reasons[stopReason]
706
            result += ',name="%s"' % thread.GetName()
707
708
709
710
711
            result += ',frame={'
            frame = thread.GetFrameAtIndex(0)
            result += 'pc="0x%x"' % frame.pc
            result += ',addr="0x%x"' % frame.pc
            result += ',fp="0x%x"' % frame.fp
712
            result += ',func="%s"' % frame.GetFunctionName()
713
714
715
716
717
718
719
720
            result += ',line="%s"' % frame.line_entry.line
            result += ',fullname="%s"' % fileName(frame.line_entry.file)
            result += ',file="%s"' % fileName(frame.line_entry.file)
            result += '}},'

        result += '],current-thread-id="%s"},' % self.currentThread().id
        self.report(result)

721
    def firstUsableFrame(self, thread):
hjk's avatar
hjk committed
722
        for i in xrange(10):
hjk's avatar
hjk committed
723
724
725
726
727
728
729
            frame = thread.GetFrameAtIndex(i)
            lineEntry = frame.GetLineEntry()
            line = lineEntry.GetLine()
            if line != 0:
                return i
        return None

hjk's avatar
hjk committed
730
    def reportStack(self, _ = None):
731
        if not self.process:
732
            self.report('msg="No process"')
733
734
735
736
737
738
739
740
            return
        thread = self.currentThread()
        if not thread:
            self.report('msg="No thread"')
            return
        frame = thread.GetSelectedFrame()
        if frame:
            frameId = frame.GetFrameID()
741
        else:
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
            frameId = 0;
        result = 'stack={current-frame="%s"' % frameId
        result += ',current-thread="%s"' % thread.GetThreadID()
        result += ',frames=['
        n = thread.GetNumFrames()
        for i in xrange(n):
            frame = thread.GetFrameAtIndex(i)
            lineEntry = frame.GetLineEntry()
            line = lineEntry.GetLine()
            usable = line != 0
            result += '{pc="0x%x"' % frame.GetPC()
            result += ',level="%d"' % frame.idx
            result += ',addr="0x%x"' % frame.GetPCAddress().GetLoadAddress(self.target)
            result += ',func="%s"' % frame.GetFunctionName()
            result += ',line="%d"' % line
            result += ',fullname="%s"' % fileName(lineEntry.file)
            result += ',usable="%d"' % usable
            result += ',file="%s"},' % fileName(lineEntry.file)
        result += '],hasmore="0"},'
        self.report(result)
762

763
764
765
766
767
    def putBetterType(self, type):
        try:
            self.currentType = type.GetName()
        except:
            self.currentType = str(type)
768
        self.currentTypePriority = self.currentTypePriority + 1
hjk's avatar
hjk committed
769
        #warn("BETTER TYPE: %s PRIORITY: %s" % (type, self.currentTypePriority))
hjk's avatar
hjk committed
770

771
    def readMemory(self, base, size):
hjk's avatar
hjk committed
772
773
        if size == 0:
            return ""
774
775
        base = int(base) & 0xFFFFFFFFFFFFFFFF
        size = int(size) & 0xFFFFFFFF
hjk's avatar
hjk committed
776
        error = lldb.SBError()
777
        contents = self.process.ReadMemory(base, size, error)
hjk's avatar
hjk committed
778
779
        return binascii.hexlify(contents)

hjk's avatar
hjk committed
780
781
782
783
784
785
786
787
788
789
790
    def isQObject(self, value):
        try:
            vtable = value.Cast(self.voidPtrType().GetPointerType())
            metaObjectEntry = vtable.Dereference()
            addr = lldb.SBAddress(long(metaObjectEntry), self.target)
            symbol = addr.GetSymbol()
            name = symbol.GetMangledName()
            return name.find("10metaObjectEv") > 0
        except:
            return False

791
792
793
794
    def qtNamespace(self):
        # FIXME
        return ""

795
796
797
    def stripNamespaceFromType(self, typeName):
        #type = stripClassTag(typeName)
        type = typeName
798
799
800
        #ns = qtNamespace()
        #if len(ns) > 0 and type.startswith(ns):
        #    type = type[len(ns):]
801
802
803
804
805
806
        pos = type.find("<")
        # FIXME: make it recognize  foo<A>::bar<B>::iterator?
        while pos != -1:
            pos1 = type.rfind(">", pos)
            type = type[0:pos] + type[pos1+1:]
            pos = type.find("<")
hjk's avatar
hjk committed
807
808
809
810
        if type.startswith("const "):
            type = type[6:]
        if type.startswith("volatile "):
            type = type[9:]
811
812
813
        return type

    def putSubItem(self, component, value, tryDynamic=True):
814
        if not value.IsValid():
hjk's avatar
hjk committed
815
            warn("INVALID SUBITEM")
816
            return
817
818
819
        with SubItem(self, component):
            self.putItem(value, tryDynamic)

hjk's avatar
hjk committed
820
821
822
823
824
825
826
    def putAddress(self, addr):
        if self.currentPrintsAddress:
            try:
                self.currentAddress = 'addr="0x%s",' % int(addr)
            except:
                pass

hjk's avatar
hjk committed
827
828
829
    def isFunctionType(self, type):
        return type.IsFunctionType()

830
    def putItem(self, value, tryDynamic=True):
hjk's avatar
hjk committed
831
832
        #value = value.GetDynamicValue(lldb.eDynamicCanRunTarget)
        typeName = value.GetTypeName()
833
        value.SetPreferDynamicValue(tryDynamic)
hjk's avatar
hjk committed
834
        typeClass = value.GetType().GetTypeClass()
835

hjk's avatar
hjk committed
836
837
838
        if tryDynamic:
            self.putAddress(value.address)

839
        # Handle build-in LLDB visualizers if wanted.
840
        if self.useLldbDumpers and value.GetTypeSynthetic().IsValid():
841
842
843
844
845
846
847
848
849
850
            # FIXME: print "official" summary?
            summary = value.GetTypeSummary()
            if summary.IsValid():
                warn("DATA: %s" % summary.GetData())
            value.SetPreferSyntheticValue(False)
            provider = value.GetTypeSynthetic()
            data = provider.GetData()
            formatter = eval(data)(value, {})
            formatter.update()
            numchild = formatter.num_children()
hjk's avatar
hjk committed
851
            self.put('iname="%s",' % self.currentIName)
hjk's avatar
hjk committed
852
            self.putType(typeName)
hjk's avatar
hjk committed
853
854
            self.put('numchild="%s",' % numchild)
            self.put('addr="0x%x",' % value.GetLoadAddress())
855
            self.putItemCount(numchild)
hjk's avatar
hjk committed
856
857
            if self.currentIName in self.expandedINames:
                with Children(self):
858
859
860
861
862
863
                    for i in xrange(numchild):
                        child = formatter.get_child_at_index(i)
                        with SubItem(self, i):
                            self.putItem(child)
            return

hjk's avatar
hjk committed
864
865
        # Typedefs
        if typeClass == lldb.eTypeClassTypedef:
866
            if typeName in self.qqDumpers:
hjk's avatar
hjk committed
867
868
                self.putType(typeName)
                self.context = value
869
                self.qqDumpers[typeName](self, value)
hjk's avatar
hjk committed
870
                return
hjk's avatar
hjk committed
871
872
873
874
875
876
877
            realType = value.GetType()
            if hasattr(realType, 'GetCanonicalType'):
                realType = realType.GetCanonicalType()
                value = value.Cast(realType.unqualified())
                self.putItem(value)
                self.putBetterType(typeName)
                return
hjk's avatar
hjk committed
878

879
        # Our turf now.
880
        value.SetPreferSyntheticValue(False)
881

882
        # Arrays
hjk's avatar
hjk committed
883
        if typeClass == lldb.eTypeClassArray:
884
            self.putCStyleArray(value)
885
886
            return

887
888
        # Vectors like char __attribute__ ((vector_size (8)))
        if typeClass == lldb.eTypeClassVector:
889
            self.putCStyleArray(value)
890
891
            return

892
893
        # References
        if value.GetType().IsReferenceType():
hjk's avatar
hjk committed
894
            origType = value.GetTypeName();
hjk's avatar
hjk committed
895
            type = value.GetType().GetDereferencedType().unqualified()
hjk's avatar
hjk committed
896
            addr = int(value) & 0xFFFFFFFFFFFFFFFF
hjk's avatar
hjk committed
897
            self.putItem(value.CreateValueFromAddress(None, addr, type))
hjk's avatar
hjk committed
898
            #self.putItem(value.CreateValueFromData(None, value.GetData(), type))
hjk's avatar
hjk committed
899
            self.putBetterType(origType)
900
901
            return

hjk's avatar
hjk committed
902
        # Pointers
hjk's avatar
hjk committed
903
        if value.GetType().IsPointerType():
hjk's avatar
hjk committed
904
905
            self.putFormattedPointer(value)
            return
hjk's avatar
hjk committed
906

907
        #warn("VALUE: %s" % value)
hjk's avatar
hjk committed
908
909
910
911
        #warn("FANCY: %s" % self.useFancy)
        if self.useFancy:
            stripped = self.stripNamespaceFromType(typeName).replace("::", "__")
            #warn("STRIPPED: %s" % stripped)
912
913
            #warn("DUMPABLE: %s" % (stripped in self.qqDumpers))
            if stripped in self.qqDumpers:
hjk's avatar
hjk committed
914
                self.putType(typeName)
hjk's avatar
hjk committed
915
                self.context = value
916
                self.qqDumpers[stripped](self, value)
hjk's avatar
hjk committed
917
                return
918
919
920
921

        # Normal value
        #numchild = 1 if value.MightHaveChildren() else 0
        numchild = value.GetNumChildren()
hjk's avatar
hjk committed
922
        self.putType(typeName)
hjk's avatar
hjk committed
923
924
925
926
927
928
929
930
931
932
933
934
935
936
        if typeClass == lldb.eTypeClassStruct or typeClass == lldb.eTypeClassClass:
            if self.isQObject(value):
                self.context = value
                if not self.putQObjectNameValue(value):  # Is this too expensive?
                    self.putEmptyValue()
            else:
                self.putEmptyValue()
        else:
            v = value.GetValue()
            if v:
                self.putValue(v)
            else:
                self.putEmptyValue()

937
938
939
940
941
        self.put('numchild="%s",' % numchild)
        self.put('addr="0x%x",' % value.GetLoadAddress())
        if self.currentIName in self.expandedINames:
            with Children(self):
                self.putFields(value)
942
943

    def putFields(self, value):
944
945
946
947
        # Suppress printing of 'name' field for arrays.
        if value.GetType().GetTypeClass() == lldb.eTypeClassArray:
            for i in xrange(value.GetNumChildren()):
                child = value.GetChildAtIndex(i)
948
                with UnnamedSubItem(self, str(i)):
949
950
951
                    self.putItem(child)
            return

952
        n = value.GetNumChildren()
953
        m = value.GetType().GetNumberOfDirectBaseClasses()
954
955
        if n > 10000:
            n = 10000
956
957
958
        # seems to happen in the 'inheritance' autotest
        if m > n:
            m = n
959
960
961
        for i in xrange(m):
            child = value.GetChildAtIndex(i)
            with UnnamedSubItem(self, "@%d" % (i + 1)):
962
                self.put('iname="%s",' % self.currentIName)
963
964
965
                self.put('name="[%s]",' % child.name)
                self.putItem(child)
        for i in xrange(m, n):
hjk's avatar
hjk committed
966
            child = value.GetChildAtIndex(i)
hjk's avatar
hjk committed
967
968
969
            if child.IsValid():  # FIXME: Anon members?
                with SubItem(self, child):
                    self.putItem(child)
970

hjk's avatar
hjk committed
971
    def reportVariables(self, _ = None):
972
        frame = self.currentThread().GetSelectedFrame()
973
        self.currentIName = 'local'
974
        self.put('data=[')
975
        self.anonNumber = 0
976
977
978
979
        shadowed = {}
        values = [v for v in frame.GetVariables(True, True, False, False) if v.IsValid()]
        values.reverse() # To get shadowed vars numbered backwards.
        for value in values:
980
981
            if self.dummyValue is None:
                self.dummyValue = value
982
            name = value.GetName()
983
984
985
            if name is None:
                warn("NO NAME FOR VALUE: %s" % value)
                continue
986
            if name in shadowed:
987
988
989
990
991
992
993
994
                level = shadowed[name]
                shadowed[name] = level + 1
                name += "@%s" % level
            else:
                shadowed[name] = 1
            with SubItem(self, name):
                self.put('iname="%s",' % self.currentIName)
                self.putItem(value)
995
996
997
998
999

        # 'watchers':[{'id':'watch.0','exp':'23'},...]
        if not self.dummyValue is None:
            for watcher in self.currentWatchers:
                iname = watcher['iname']
1000
1001
                # could be 'watch.0' or 'tooltip.deadbead'
                (base, component) = iname.split('.')
1002
1003
1004
1005
1006
1007
                exp = binascii.unhexlify(watcher['exp'])
                if exp == "":
                    self.put('type="",value="",exp=""')
                    continue

                value = self.dummyValue.CreateValueFromExpression(iname, exp)
1008
1009
                self.currentIName = base
                with SubItem(self, component):
1010
1011
                    self.put('exp="%s",' % exp)
                    self.put('wname="%s",' % binascii.hexlify(exp))
1012
                    self.put('iname="%s",' % iname)
1013
1014
                    self.putItem(value)

1015
1016
1017
        self.put(']')
        self.report('')

hjk's avatar
hjk committed
1018
    def reportData(self, _ = None):
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
        if self.process is None:
            self.report('process="none"')
        else:
            state = self.process.GetState()
            if state == lldb.eStateStopped:
                self.reportStack()
                self.reportThreads()
                self.reportLocation()
                self.reportVariables()

1029
    def reportRegisters(self, _ = None):
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
        if self.process is None:
            self.report('process="none"')
        else:
            frame = self.currentFrame()
            result = 'registers=['
            for group in frame.GetRegisters():
                for reg in group:
                    result += '{name="%s"' % reg.GetName()
                    result += ',value="%s"' % reg.GetValue()
                    result += ',type="%s"},' % reg.GetType()
            result += ']'
            self.report(result)

    def report(self, stuff):
1044
        sys.stdout.write(stuff + "@\n")
1045

hjk's avatar
hjk committed
1046
    def interruptInferior(self, _ = None):
1047
1048
        if self.process is None:
            self.report('msg="No process"')
1049
            return
1050
        self.isInterrupting_ = True