lldbbridge.py 63.6 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
96
97
98

def registerCommand(name, func):
    pass

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

99

100
# Data members
101
PointerCode = 102
102
103
104
105
106
107
108
109
110
111
112

# 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
113
114
115
116
117
BreakpointAtSysCall = 9
WatchpointAtAddress = 10
WatchpointAtExpression = 11
BreakpointOnQmlSignalEmit = 12
BreakpointAtJavaScriptThrow = 13
118

119
120
121
# See db.StateType
stateNames = ["invalid", "unloaded", "connected", "attaching", "launching", "stopped",
    "running", "stepping", "crashed", "detached", "exited", "suspended" ]
122

123
124
125
def loggingCallback(args):
    s = args.strip()
    s = s.replace('"', "'")
126
    sys.stdout.write('log="%s"@\n' % s)
127

hjk's avatar
hjk committed
128
129
130
131
def check(exp):
    if not exp:
        raise RuntimeError("Check failed")

132
133
Value = lldb.SBValue

134
135
def impl_SBValue__add__(self, offset):
    if self.GetType().IsPointerType():
136
137
138
139
        if isinstance(offset, int) or isinstance(offset, long):
            pass
        else:
            offset = offset.GetValueAsSigned()
140
        itemsize = self.GetType().GetPointeeType().GetByteSize()
141
        address = self.GetValueAsUnsigned() + offset * itemsize
142
        address = address & 0xFFFFFFFFFFFFFFFF  # Force unsigned
hjk's avatar
hjk committed
143
144
145
146
147
148

        # We don't have a dumper object
        #return createPointerValue(self, address, self.GetType().GetPointeeType())
        addr = int(address) & 0xFFFFFFFFFFFFFFFF
        return self.CreateValueFromAddress(None, addr, self.GetType().GetPointeeType()).AddressOf()

149
    raise RuntimeError("SBValue.__add__ not implemented: %s" % self.GetType())
150
151
    return NotImplemented

152
def impl_SBValue__sub__(self, other):
153
    if self.GetType().IsPointerType():
154
155
156
157
        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())
158
        if other.GetType().IsPointerType():
159
160
            itemsize = self.GetType().GetPointeeType().GetByteSize()
            return (self.GetValueAsUnsigned() - other.GetValueAsUnsigned()) / itemsize
161
    raise RuntimeError("SBValue.__sub__ not implemented: %s" % self.GetType())
162
163
    return NotImplemented

164
165
166
167
168
169
170
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):
171
    return self.GetValueAsSigned()
172
173
174
175
176
177
178
179
180
181

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
182

183
184
185
def impl_SBValue__long__(self):
    return int(self.GetValue(), 0)

186
187
188
189
190
191
192
193
194
195
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)
196

197
lldb.SBValue.__add__ = impl_SBValue__add__
198
lldb.SBValue.__sub__ = impl_SBValue__sub__
199
lldb.SBValue.__le__ = impl_SBValue__le__
200

201
202
lldb.SBValue.__getitem__ = impl_SBValue__getitem__
lldb.SBValue.__int__ = impl_SBValue__int__
203
lldb.SBValue.__float__ = impl_SBValue__float__
204
205
lldb.SBValue.__long__ = lambda self: long(self.GetValue(), 0)

206
lldb.SBValue.code = lambda self: self.GetTypeClass()
207
208
lldb.SBValue.cast = lambda self, typeObj: self.Cast(typeObj)
lldb.SBValue.dereference = lambda self: self.Dereference()
209
lldb.SBValue.address = property(lambda self: self.GetAddress())
210
211

lldb.SBType.pointer = lambda self: self.GetPointerType()
212
lldb.SBType.code = lambda self: self.GetTypeClass()
213
lldb.SBType.sizeof = property(lambda self: self.GetByteSize())
214
215


hjk's avatar
hjk committed
216
217
lldb.SBType.unqualified = \
    lambda self: self.GetUnqualifiedType() if hasattr(self, 'GetUnqualifiedType') else self
218
219
lldb.SBType.strip_typedefs = \
    lambda self: self.GetCanonicalType() if hasattr(self, 'GetCanonicalType') else self
220

hjk's avatar
hjk committed
221
222
223
lldb.SBType.__orig__str__ = lldb.SBType.__str__
lldb.SBType.__str__ = lldb.SBType.GetName

224
225
226
227
def simpleEncoding(typeobj):
    code = typeobj.GetTypeClass()
    size = typeobj.sizeof
    if code == lldb.eTypeClassBuiltin:
hjk's avatar
hjk committed
228
229
230
231
232
233
        name = str(typeobj)
        if name == "float":
            return Hex2EncodedFloat4
        if name == "double":
            return Hex2EncodedFloat8
        if name.find("unsigned") >= 0:
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
            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
252

253
class Dumper(DumperBase):
254
    def __init__(self):
255
256
        DumperBase.__init__(self)

257
        self.debugger = lldb.SBDebugger.Create()
258
        #self.debugger.SetLoggingCallback(loggingCallback)
259
260
261
262
263
264
        #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")
265
266
267
268
269
270
271
272
273
274

        # 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)

275
        self.isLldb = True
276
277
278
279
        self.process = None
        self.target = None
        self.eventState = lldb.eStateInvalid
        self.expandedINames = {}
280
        self.passExceptions = False
281
        self.useLldbDumpers = False
hjk's avatar
hjk committed
282
        self.autoDerefPointers = True
283
        self.useDynamicType = True
hjk's avatar
hjk committed
284
        self.useFancy = True
hjk's avatar
hjk committed
285
286
287
        self.formats = {}
        self.typeformats = {}
        self.currentAddress = None
288
289
290
291
292
293

        self.currentIName = None
        self.currentValuePriority = -100
        self.currentValueEncoding = None
        self.currentType = ""
        self.currentTypePriority = -100
294
        self.currentValue = None
295
296
297
298
299
        self.currentNumChild = None
        self.currentMaxNumChild = None
        self.currentPrintsAddress = None
        self.currentChildType = None
        self.currentChildNumChild = None
300
        self.currentWatchers = {}
301

hjk's avatar
hjk committed
302
        self.executable_ = None
303
304
305
306
        self.startMode_ = None
        self.processArgs_ = None
        self.attachPid_ = None

307
308
        self.charType_ = None
        self.intType_ = None
309
        self.int64Type_ = None
310
311
        self.sizetType_ = None
        self.charPtrType_ = None
hjk's avatar
hjk committed
312
        self.voidPtrType_ = None
313
        self.isShuttingDown_ = False
314
        self.isInterrupting_ = False
315
        self.dummyValue = None
316

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
    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()
        #warn("TYPECLASS: %s" % typeClass)
        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

401
402
403
404
405
    def parseAndEvaluate(self, expr):
        thread = self.currentThread()
        frame = thread.GetFrameAtIndex(0)
        return frame.EvaluateExpression(expr)

406
407
408
409
410
411
412
413
414
415
416
417
418
    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)

419
    def templateArgument(self, typeobj, index):
420
        type = typeobj.GetTemplateArgumentType(index)
hjk's avatar
hjk committed
421
        if type.IsValid():
422
423
424
425
426
427
428
            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)
429

hjk's avatar
hjk committed
430
431
432
433
434
435
    def isReferenceType(self, typeobj):
        return typeobj.IsReferenceType()

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

436
    def qtVersion(self):
437
        self.cachedQtVersion = 0x0
438
439
440
441
442
443
444
445
        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:
446
                    self.cachedQtVersion += v << shift
447
448
                    shift += 8
                break
449
450
451
452

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

hjk's avatar
hjk committed
454
455
456
    def intSize(self):
        return 4

457
458
    def intType(self):
        if self.intType_ is None:
hjk's avatar
hjk committed
459
             self.intType_ = self.target.FindFirstType('int')
460
461
        return self.intType_

462
463
464
465
466
    def int64Type(self):
        if self.int64Type_ is None:
             self.int64Type_ = self.target.FindFirstType('long long int')
        return self.int64Type_

467
468
    def charType(self):
        if self.charType_ is None:
hjk's avatar
hjk committed
469
             self.charType_ = self.target.FindFirstType('char')
470
471
472
473
474
475
476
477
        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
478
479
480
        if self.voidPtrType_ is None:
             self.voidPtrType_ = self.target.FindFirstType('void').GetPointerType()
        return self.voidPtrType_
481

hjk's avatar
hjk committed
482
    def ptrSize(self):
483
484
485
486
487
488
489
        return self.charPtrType().GetByteSize()

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

490
491
492
    def addressOf(self, value):
        return int(value.GetLoadAddress())

hjk's avatar
hjk committed
493
494
495
496
497
498
499
500
501
    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()))

502
503
504
    def extractInt64(self, address):
        return int(self.createValue(address, self.int64Type()))

505
506
507
    def extractByte(self, address):
        return int(self.createValue(address, self.charType())) & 0xFF

508
509
510
511
512
513
    def handleCommand(self, command):
        result = lldb.SBCommandReturnObject()
        self.debugger.GetCommandInterpreter().HandleCommand(command, result)
        success = result.Succeeded()
        if success:
            self.report('output="%s"' % result.GetOutput())
514
        else:
515
516
517
518
519
520
            self.report('error="%s"' % result.GetError())
        self.reportData()

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

hjk's avatar
hjk committed
521
522
523
    def putField(self, name, value):
        self.put('%s="%s",' % (name, value))

524
    def isMovableType(self, type):
525
        if type.GetTypeClass() in (lldb.eTypeClassBuiltin, lldb.eTypeClassPointer):
526
            return True
527
        return self.isKnownMovableType(self.stripNamespaceFromType(type.GetName()))
528

529
530
    def putNumChild(self, numchild):
        #warn("NUM CHILD: '%s' '%s'" % (numchild, self.currentChildNumChild))
hjk's avatar
hjk committed
531
532
        #if numchild != self.currentChildNumChild:
        self.put('numchild="%s",' % numchild)
533

hjk's avatar
hjk committed
534
535
536
537
538
539
    def putEmptyValue(self, priority = -10):
        if priority >= self.currentValuePriority:
            self.currentValue = ""
            self.currentValuePriority = priority
            self.currentValueEncoding = None

hjk's avatar
hjk committed
540
541
542
    def putSimpleValue(self, value, encoding = None, priority = 0):
        self.putValue(value.GetValue(), encoding, priority)

543
544
545
546
547
548
    def putValue(self, value, encoding = None, priority = 0):
        # Higher priority values override lower ones.
        if priority >= self.currentValuePriority:
            self.currentValue = value
            self.currentValuePriority = priority
            self.currentValueEncoding = encoding
hjk's avatar
hjk committed
549
        #self.put('value="%s",' % value)
550

hjk's avatar
hjk committed
551
552
553
    def putName(self, name):
        self.put('name="%s",' % name)

554
555
556
557
558
559
    def isExpanded(self):
        #warn("IS EXPANDED: %s in %s: %s" % (self.currentIName,
        #    self.expandedINames, self.currentIName in self.expandedINames))
        return self.currentIName in self.expandedINames

    def tryPutArrayContents(self, typeobj, base, n):
560
        if not self.isSimpleType(typeobj):
561
562
563
            return False
        size = n * typeobj.sizeof
        self.put('childtype="%s",' % typeobj)
564
565
        self.put('addrbase="0x%x",' % int(base))
        self.put('addrstep="%d",' % typeobj.sizeof)
566
567
        self.put('arrayencoding="%s",' % simpleEncoding(typeobj))
        self.put('arraydata="')
568
        self.put(self.readMemory(base, size))
569
570
571
572
        self.put('",')
        return True

    def putPlotData(self, type, base, n, plotFormat):
hjk's avatar
hjk committed
573
        #warn("PLOTDATA: %s %s" % (type, n))
574
575
        if self.isExpanded():
            self.putArrayData(type, base, n)
hjk's avatar
hjk committed
576
577
        self.putValue(self.currentValue)
        self.putField("plottable", "0")
578
579
580
581

    def putArrayData(self, type, base, n,
            childNumChild = None, maxNumChild = 10000):
        if not self.tryPutArrayContents(type, base, n):
582
            base = self.createPointerValue(base, type)
583
584
585
586
587
            with Children(self, n, type, childNumChild, maxNumChild,
                    base, type.GetByteSize()):
                for i in self.childRange():
                    self.putSubItem(i, (base + i).dereference())

588
589
590
    def parseAndEvalute(self, expr):
        return expr

591
592
593
594
595
596
597
598
    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)

599
    def putCallItem(self, name, value, func, *args):
600
        result = self.call2(value, func, args)
601
602
603
        with SubItem(self, name):
            self.putItem(result)

604
605
606
607
608
    def childRange(self):
        if self.currentMaxNumChild is None:
            return xrange(0, self.currentNumChild)
        return xrange(min(self.currentMaxNumChild, self.currentNumChild))

hjk's avatar
hjk committed
609
610
611
612
613
614
615
    def putPlainChildren(self, value):
        self.putEmptyValue(-99)
        self.putNumChild(1)
        if self.currentIName in self.expandedINames:
            with Children(self):
               self.putFields(value)

616
    def lookupType(self, name):
hjk's avatar
hjk committed
617
        #warn("LOOKUP TYPE NAME: %s" % name)
hjk's avatar
hjk committed
618
619
620
621
        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
622
623
        #warn("LOOKUP RESULT: %s" % type.name)
        #warn("LOOKUP VALID: %s" % type.IsValid())
hjk's avatar
hjk committed
624
        return type if type.IsValid() else None
625

626
    def setupInferior(self, args):
627
        error = lldb.SBError()
628
629

        self.executable_ = args['executable']
630
631
632
        self.startMode_ = args.get('startMode', 1)
        self.processArgs_ = args.get('processArgs', '')
        self.attachPid_ = args.get('attachPid', 0)
Fawzi Mohamed's avatar
Fawzi Mohamed committed
633
        self.sysRoot_ = args.get('sysRoot', '')
Fawzi Mohamed's avatar
Fawzi Mohamed committed
634
        self.remoteChannel_ = args.get('remoteChannel', '')
635
        self.platform_ = args.get('platform', '')
636

637
        if self.sysRoot_:
Fawzi Mohamed's avatar
Fawzi Mohamed committed
638
            self.debugger.SetCurrentPlatformSDKRoot(self.sysRoot_)
639
640
        if self.platform_:
            self.debugger.SetCurrentPlatform(self.platform_)
641
        self.target = self.debugger.CreateTarget(self.executable_, None, None, True, error)
642
643
        self.importDumpers()

644
645
        state = "inferiorsetupok" if self.target.IsValid() else "inferiorsetupfailed"
        self.report('state="%s",msg="%s",exe="%s"' % (state, error, self.executable_))
646

647
    def runEngine(self, _):
hjk's avatar
hjk committed
648
649
650
651
        s = threading.Thread(target=self.loop, args=[])
        s.start()

    def loop(self):
652
        error = lldb.SBError()
hjk's avatar
hjk committed
653
654
        listener = self.debugger.GetListener()

655
656
657
        if self.attachPid_ > 0:
            attachInfo = lldb.SBAttachInfo(self.attachPid_)
            self.process = self.target.Attach(attachInfo, error)
658
            if not error.Success():
Fawzi Mohamed's avatar
Fawzi Mohamed committed
659
660
661
662
663
664
665
666
                self.report('state="inferiorrunfailed"')
                return
            self.report('pid="%s"' % self.process.GetProcessID())
            self.report('state="enginerunandinferiorstopok"')
        elif len(self.remoteChannel_) > 0:
            self.process = self.target.ConnectRemote(
            self.debugger.GetListener(),
            self.remoteChannel_, None, error)
667
668
669
            if not error.Success():
                self.report('state="inferiorrunfailed"')
                return
Fawzi Mohamed's avatar
Fawzi Mohamed committed
670
            self.report('state="enginerunandinferiorstopok"')
671
672
673
        else:
            launchInfo = lldb.SBLaunchInfo(self.processArgs_.split(' '))
            launchInfo.SetWorkingDirectory(os.getcwd())
674
675
            environmentList = [key + "=" + value for key,value in os.environ.items()]
            launchInfo.SetEnvironmentEntries(environmentList, False)
676
            self.process = self.target.Launch(launchInfo, error)
677
            if not error.Success():
Fawzi Mohamed's avatar
Fawzi Mohamed committed
678
679
680
681
                self.report('state="inferiorrunfailed"')
                return
            self.report('pid="%s"' % self.process.GetProcessID())
            self.report('state="enginerunandinferiorrunok"')
682

hjk's avatar
hjk committed
683
684
685
686
687
688
        event = lldb.SBEvent()
        while True:
            if listener.WaitForEvent(10000000, event):
                self.handleEvent(event)
            else:
                warn('TIMEOUT')
689

690
    def describeError(self, error):
691
692
693
694
695
696
        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()
697
698
699
700
        return result

    def reportError(self, error):
        self.report(self.describeError(error))
701
702

    def currentThread(self):
703
        return self.process.GetSelectedThread()
704
705
706
707
708
709

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

    def reportLocation(self):
        thread = self.currentThread()
710
        frame = thread.GetSelectedFrame()
711
712
713
714
715
        file = fileName(frame.line_entry.file)
        line = frame.line_entry.line
        self.report('location={file="%s",line="%s",addr="%s"}' % (file, line, frame.pc))

    def reportThreads(self):
716
717
        reasons = ['None', 'Trace', 'Breakpoint', 'Watchpoint', 'Signal', 'Exception',
            'Exec', 'PlanComplete']
718
        result = 'threads={threads=['
719
        for i in xrange(0, self.process.GetNumThreads()):
720
            thread = self.process.GetThreadAtIndex(i)
721
            stopReason = thread.GetStopReason()
722
723
            result += '{id="%d"' % thread.GetThreadID()
            result += ',index="%s"' % i
724
            result += ',details="%s"' % thread.GetQueueName()
725
726
727
            result += ',stop-reason="%s"' % stopReason
            if stopReason >= 0 and stopReason < len(reasons):
                result += ',state="%s"' % reasons[stopReason]
728
            result += ',name="%s"' % thread.GetName()
729
730
731
732
733
            result += ',frame={'
            frame = thread.GetFrameAtIndex(0)
            result += 'pc="0x%x"' % frame.pc
            result += ',addr="0x%x"' % frame.pc
            result += ',fp="0x%x"' % frame.fp
734
            result += ',func="%s"' % frame.GetFunctionName()
735
736
737
738
739
740
741
742
            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)

hjk's avatar
hjk committed
743
744
    def firstUsableFrame(self):
        thread = self.currentThread()
hjk's avatar
hjk committed
745
        for i in xrange(10):
hjk's avatar
hjk committed
746
747
748
749
750
751
752
            frame = thread.GetFrameAtIndex(i)
            lineEntry = frame.GetLineEntry()
            line = lineEntry.GetLine()
            if line != 0:
                return i
        return None

hjk's avatar
hjk committed
753
    def reportStack(self, _ = None):
754
755
756
757
        if self.process is None:
            self.report('msg="No process"')
        else:
            thread = self.currentThread()
758
759
760
            result = 'stack={current-frame="%s"' % thread.GetSelectedFrame().GetFrameID()
            result += ',current-thread="%s"' % thread.GetThreadID()
            result += ',frames=['
761
762
            n = thread.GetNumFrames()
            for i in xrange(n):
763
764
                frame = thread.GetFrameAtIndex(i)
                lineEntry = frame.GetLineEntry()
765
766
                line = lineEntry.GetLine()
                usable = line != 0
767
                result += '{pc="0x%x"' % frame.GetPC()
768
                result += ',level="%d"' % frame.idx
769
                result += ',addr="0x%x"' % frame.GetPCAddress().GetLoadAddress(self.target)
770
                result += ',func="%s"' % frame.GetFunctionName()
771
                result += ',line="%d"' % line
772
                result += ',fullname="%s"' % fileName(lineEntry.file)
773
                result += ',usable="%d"' % usable
774
                result += ',file="%s"},' % fileName(lineEntry.file)
hjk's avatar
hjk committed
775
            result += '],hasmore="0"},'
776
777
            self.report(result)

778
779
780
781
782
    def putType(self, type, priority = 0):
        # Higher priority values override lower ones.
        if priority >= self.currentTypePriority:
            self.currentType = str(type)
            self.currentTypePriority = priority
hjk's avatar
hjk committed
783
        #warn("TYPE: %s PRIORITY: %s" % (type, priority))
784

785
786
787
788
789
    def putBetterType(self, type):
        try:
            self.currentType = type.GetName()
        except:
            self.currentType = str(type)
790
        self.currentTypePriority = self.currentTypePriority + 1
hjk's avatar
hjk committed
791
        #warn("BETTER TYPE: %s PRIORITY: %s" % (type, self.currentTypePriority))
hjk's avatar
hjk committed
792

793
    def readMemory(self, base, size):
hjk's avatar
hjk committed
794
795
        if size == 0:
            return ""
796
797
        base = int(base) & 0xFFFFFFFFFFFFFFFF
        size = int(size) & 0xFFFFFFFF
hjk's avatar
hjk committed
798
        error = lldb.SBError()
799
        contents = self.process.ReadMemory(base, size, error)
hjk's avatar
hjk committed
800
801
        return binascii.hexlify(contents)

hjk's avatar
hjk committed
802
803
804
805
806
807
808
809
810
811
812
    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

hjk's avatar
hjk committed
813
814
    def putValue(self, value, encoding = None, priority = 0):
        # Higher priority values override lower ones.
815
816
817
818
        if priority >= self.currentValuePriority:
            self.currentValue = value
            self.currentValuePriority = priority
            self.currentValueEncoding = encoding
hjk's avatar
hjk committed
819

820
821
822
823
    def qtNamespace(self):
        # FIXME
        return ""

824
825
826
    def stripNamespaceFromType(self, typeName):
        #type = stripClassTag(typeName)
        type = typeName
827
828
829
        #ns = qtNamespace()
        #if len(ns) > 0 and type.startswith(ns):
        #    type = type[len(ns):]
830
831
832
833
834
835
        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
836
837
838
839
        if type.startswith("const "):
            type = type[6:]
        if type.startswith("volatile "):
            type = type[9:]
840
841
842
        return type

    def putSubItem(self, component, value, tryDynamic=True):
843
        if not value.IsValid():
hjk's avatar
hjk committed
844
            warn("INVALID SUBITEM")
845
            return
846
847
848
        with SubItem(self, component):
            self.putItem(value, tryDynamic)

hjk's avatar
hjk committed
849
850
851
852
853
854
855
    def putAddress(self, addr):
        if self.currentPrintsAddress:
            try:
                self.currentAddress = 'addr="0x%s",' % int(addr)
            except:
                pass

856
    def putItem(self, value, tryDynamic=True):
hjk's avatar
hjk committed
857
858
        #value = value.GetDynamicValue(lldb.eDynamicCanRunTarget)
        typeName = value.GetTypeName()
859
        value.SetPreferDynamicValue(tryDynamic)
hjk's avatar
hjk committed
860
        typeClass = value.GetType().GetTypeClass()
861

hjk's avatar
hjk committed
862
863
864
        if tryDynamic:
            self.putAddress(value.address)

865
        # Handle build-in LLDB visualizers if wanted.
866
        if self.useLldbDumpers and value.GetTypeSynthetic().IsValid():
867
868
869
870
871
872
873
874
875
876
            # 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
877
            self.put('iname="%s",' % self.currentIName)
hjk's avatar
hjk committed
878
            self.putType(typeName)
hjk's avatar
hjk committed
879
880
            self.put('numchild="%s",' % numchild)
            self.put('addr="0x%x",' % value.GetLoadAddress())
881
            self.putItemCount(numchild)
hjk's avatar
hjk committed
882
883
            if self.currentIName in self.expandedINames:
                with Children(self):
884
885
886
887
888
889
                    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
890
891
        # Typedefs
        if typeClass == lldb.eTypeClassTypedef:
892
            if typeName in self.qqDumpers:
hjk's avatar
hjk committed
893
894
                self.putType(typeName)
                self.context = value
895
                self.qqDumpers[typeName](self, value)
hjk's avatar
hjk committed
896
                return
hjk's avatar
hjk committed
897
898
899
900
901
902
903
            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
904

905
        # Our turf now.
906
        value.SetPreferSyntheticValue(False)
907

908
        # Arrays
hjk's avatar
hjk committed
909
        if typeClass == lldb.eTypeClassArray:
910
911
912
            qdump____c_style_array__(self, value)
            return

913
914
915
916
917
        # Vectors like char __attribute__ ((vector_size (8)))
        if typeClass == lldb.eTypeClassVector:
            qdump____c_style_array__(self, value)
            return

918
919
        # References
        if value.GetType().IsReferenceType():
hjk's avatar
hjk committed
920
            origType = value.GetTypeName();
hjk's avatar
hjk committed
921
            type = value.GetType().GetDereferencedType().unqualified()
hjk's avatar
hjk committed
922
            addr = int(value) & 0xFFFFFFFFFFFFFFFF
hjk's avatar
hjk committed
923
            self.putItem(value.CreateValueFromAddress(None, addr, type))
hjk's avatar
hjk committed
924
            #self.putItem(value.CreateValueFromData(None, value.GetData(), type))
hjk's avatar
hjk committed
925
            self.putBetterType(origType)
926
927
            return

hjk's avatar
hjk committed
928
        # Pointers
hjk's avatar
hjk committed
929
        if value.GetType().IsPointerType():
930
            if self.isNull(value):
hjk's avatar
hjk committed
931
932
933
934
935
                self.putType(typeName)
                self.putValue("0x0")
                self.putNumChild(0)
                return

hjk's avatar
hjk committed
936
937
938
939
940
941
942
943
944
            try:
                value.dereference()
            except:
                # Failure to dereference a pointer should at least
                # show the value of a pointer.
                self.putValue(cleanAddress(value))
                self.putType(typeName)
                self.putNumChild(0)
                return
hjk's avatar
hjk committed
945

hjk's avatar
hjk committed
946
947
948
949
            type = value.GetType()
            innerType = value.GetType().GetPointeeType().unqualified()
            innerTypeName = str(innerType)

950
            format = self.currentItemFormat(type)
hjk's avatar
hjk committed
951
952
953
954
955
956
957
958
959
960

            if innerTypeName == "void":
                warn("VOID POINTER: %s" % format)
                self.putType(typeName)
                self.putValue(str(value))
                self.putNumChild(0)
                return

            if format == None and innerTypeName == "char":
                # Use Latin1 as default for char *.
hjk's avatar
hjk committed
961
                self.putType(typeName)
hjk's avatar
hjk committed
962
963
964
965
966
967
968
969
970
                self.putValue(self.encodeCharArray(value), Hex2EncodedLatin1)
                self.putNumChild(0)
                return

            if format == 0:
                # Explicitly requested bald pointer.
                self.putType(typeName)
                self.putPointerValue(value)
                self.putNumChild(1)
hjk's avatar
hjk committed
971
972
                if self.currentIName in self.expandedINames:
                    with Children(self):
hjk's avatar
hjk committed
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
                        with SubItem(self, '*'):
                            self.putItem(value.dereference())
                return

            if format == 1:
                # Explicitly requested Latin1 formatting.
                self.putType(typeName)
                self.putValue(self.encodeCharArray(value), Hex2EncodedLatin1)
                self.putNumChild(0)
                return

            if format == 2:
                # Explicitly requested UTF-8 formatting.
                self.putType(typeName)
                self.putValue(self.encodeCharArray(value), Hex2EncodedUtf8)
                self.putNumChild(0)
                return

            if format == 3:
                # Explicitly requested local 8 bit formatting.
                self.putType(typeName)
                self.putValue(self.encodeCharArray(value), Hex2EncodedLocal8Bit)
                self.putNumChild(0)
                return

            if format == 4:
                # Explicitly requested UTF-16 formatting.
                self.putType(typeName)
                self.putValue(self.encodeChar2Array(value), Hex4EncodedLittleEndian)
                self.putNumChild(0)
                return

            if format == 5:
                # Explicitly requested UCS-4 formatting.
                self.putType(typeName)
                self.putValue(self.encodeChar4Array(value), Hex8EncodedLittleEndian)
                self.putNumChild(0)
                return

            if format == 6:
                # Explicitly requested formatting as array of 10 items.
                self.putType(typeName)
                self.putItemCount(10)
                self.putNumChild(10)
                self.putArrayData(innerType, value, 10)
                return

            if format == 7:
                # Explicitly requested formatting as array of 1000 items.
                self.putType(typeName)
                self.putItemCount(1000)
                self.putNumChild(1000)
                self.putArrayData(innerType, value, 1000)
                return

1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
            if innerType.IsFunctionType():
                # A function pointer.
                val = str(value)
                pos = val.find(" = ")
                if pos > 0:
                    val = val[pos + 3:]
                self.putValue(val)
                self.putType(innerType)
                self.putNumChild(0)
                return
hjk's avatar
hjk committed
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100

            #warn("AUTODEREF: %s" % self.autoDerefPointers)
            #warn("INAME: %s" % self.currentIName)
            if self.autoDerefPointers or self.currentIName.endswith('.this'):
                ## Generic pointer type with format None
                #warn("GENERIC AUTODEREF POINTER: %s AT %s TO %s"
                #    % (type, value.address, innerTypeName))
                # Never dereference char types.
                if innerTypeName != "char" \
                        and innerTypeName != "signed char" \
                        and innerTypeName != "unsigned char"  \
                        and innerTypeName != "wchar_t":
                    self.putType(innerType)
                    savedCurrentChildType = self.currentChildType
                    self.currentChildType = stripClassTag(innerTypeName)
                    self.putItem(value.dereference())
                    self.currentChildType = savedCurrentChildType
                    #self.putPointerValue(value)
                    self.put('origaddr="%s",' % value.address)
                    return

            # Fall back to plain pointer printing.
            #warn("GENERIC PLAIN POINTER: %s" % value.type)
            #warn("ADDR PLAIN POINTER: %s" % value.address)
            #self.putType(typeName)
            #self.putField("aaa", "1")
            ##self.put('addr="0x%x",' % toInteger(value.address))
            ##self.putAddress(value.address)
            #self.putField("bbb", "1")
            ##self.putPointerValue(value)
            #self.putValue("0x%x" % value.cast(self.lookupType("unsigned long")))
            #self.putField("ccc", "1")
            #self.putNumChild(1)
            #if self.currentIName in self.expandedINames:
            #    with Children(self):
            #        with SubItem(self, "*"):
            #            self.putItem(value.dereference())
            #return

            #if self.autoDerefPointers:
            #    self.putType(innerType)
            #    savedCurrentChildType = self.currentChildType
            #    self.currentChildType = str(innerType)
            #    inner = value.Dereference()
            #    if inner.IsValid():
            #        self.putItem(inner)
            #        self.currentChildType = savedCurrentChildType
            #        self.put('origaddr="%s",' % value.address)
            #        return
#
#            else:

            numchild = value.GetNumChildren()
            self.put('iname="%s",' % self.currentIName)
            self.putType(typeName)
            self.putValue('0x%x' % value.GetValueAsUnsigned())
            self.put('numchild="1",')
            self.put('addr="0x%x",' % value.GetLoadAddress())
            if self.currentIName in self.expandedINames:
                with Children(self):
                    child = value.Dereference()
                    with SubItem(self, child):
                        self.putItem(child)
hjk's avatar
hjk committed
1101

hjk's avatar
hjk committed
1102

1103
        #warn("VALUE: %s" % value)
hjk's avatar
hjk committed
1104
1105
1106
1107
        #warn("FANCY: %s" % self.useFancy)
        if self.useFancy:
            stripped = self.stripNamespaceFromType(typeName).replace("::", "__")
            #warn("STRIPPED: %s" % stripped)
1108
1109
            #warn("DUMPABLE: %s" % (stripped in self.qqDumpers))
            if stripped in self.qqDumpers:
hjk's avatar
hjk committed
1110
                self.putType(typeName)
hjk's avatar
hjk committed
1111
                self.context = value
1112
                self.qqDumpers[stripped](self, value)
hjk's avatar
hjk committed
1113
                return
1114
1115
1116
1117

        # Normal value
        #numchild = 1 if value.MightHaveChildren() else 0
        numchild = value.GetNumChildren()
hjk's avatar
hjk committed
1118
        self.putType(typeName)
hjk's avatar
hjk committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
        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()

1133
1134
1135
1136
1137
        self.put('numchild="%s",' % numchild)
        self.put('addr="0x%x",' % value.GetLoadAddress())
        if self.currentIName in self.expandedINames:
            with Children(self):
                self.putFields(value)
1138
1139

    def putFields(self, value):
1140
1141
1142
1143
        # Suppress printing of 'name' field for arrays.
        if value.GetType().GetTypeClass() == lldb.eTypeClassArray:
            for i in xrange(value.GetNumChildren()):
                child = value.GetChildAtIndex(i)
1144
                with UnnamedSubItem(self, str(i)):
1145
1146
1147
                    self.putItem(child)
            return

1148
        n = value.GetNumChildren()
1149
        m = value.GetType().GetNumberOfDirectBaseClasses()
1150
1151
        if n > 10000:
            n = 10000
1152
1153
1154
        # seems to happen in the 'inheritance' autotest
        if m > n:
            m = n
1155
1156
1157
        for i in xrange(m):
            child = value.GetChildAtIndex(i)
            with UnnamedSubItem(self, "@%d" % (i + 1)):
1158
                self.put('iname="%s",' % self.currentIName)
1159
1160
1161
                self.put('name="[%s]",' % child.name)
                self.putItem(child)
        for i in xrange(m, n):
hjk's avatar
hjk committed
1162
            child = value.GetChildAtIndex(i)