dumper.py 73 KB
Newer Older
1
2
############################################################################
#
Eike Ziller's avatar
Eike Ziller committed
3
4
# Copyright (C) 2015 The Qt Company Ltd.
# Contact: http://www.qt.io/licensing
5
6
7
8
9
10
11
#
# 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
Eike Ziller's avatar
Eike Ziller committed
12
13
# a written agreement between you and The Qt Company.  For licensing terms and
# conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
# use the contact form at http://www.qt.io/contact-us.
15
16
17
#
# GNU Lesser General Public License Usage
# Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
# General Public License version 2.1 or version 3 as published by the Free
# Software Foundation and appearing in the file LICENSE.LGPLv21 and
# LICENSE.LGPLv3 included in the packaging of this file.  Please review the
# following information to ensure the GNU Lesser General Public License
# requirements will be met: https://www.gnu.org/licenses/lgpl.html and
# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
#
Eike Ziller's avatar
Eike Ziller committed
25
26
# In addition, as a special exception, The Qt Company gives you certain additional
# rights.  These rights are described in The Qt Company LGPL Exception
27
28
29
30
31
# version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
#
############################################################################

import os
32
import struct
33
34
import sys
import base64
hjk's avatar
hjk committed
35
import re
36
import time
hjk's avatar
hjk committed
37
38
import importlib

39
40
41
42
43
44
try:
    import subprocess
    hasSubprocess = True
except:
    hasSubprocess = False
    hasPlot = False
45
46
47
48
49
50
51
52
53
54
55

if sys.version_info[0] >= 3:
    xrange = range
    toInteger = int
else:
    toInteger = long


verbosity = 0
verbosity = 1

56
57
58
59
60
61
62
63
64
65
# Debugger start modes. Keep in sync with DebuggerStartMode in debuggerconstants.h
NoStartMode, \
StartInternal, \
StartExternal,  \
AttachExternal,  \
AttachCrashedExternal,  \
AttachCore, \
AttachToRemoteServer, \
AttachToRemoteProcess, \
StartRemoteProcess, \
66
    = range(0, 9)
67
68


hjk's avatar
hjk committed
69
# Known special formats. Keep in sync with DisplayFormat in watchhandler.h
70
71
72
73
74
AutomaticFormat, \
RawFormat, \
SimpleFormat, \
EnhancedFormat, \
SeparateFormat, \
hjk's avatar
hjk committed
75
Latin1StringFormat, \
76
SeparateLatin1StringFormat, \
hjk's avatar
hjk committed
77
Utf8StringFormat, \
78
SeparateUtf8StringFormat, \
hjk's avatar
hjk committed
79
80
81
82
83
84
Local8BitStringFormat, \
Utf16StringFormat, \
Ucs4StringFormat, \
Array10Format, \
Array100Format, \
Array1000Format, \
85
Array10000Format, \
86
87
88
89
90
ArrayPlotFormat, \
CompactMapFormat, \
DirectQListStorageFormat, \
IndirectQListStorageFormat, \
    = range(0, 20)
hjk's avatar
hjk committed
91

hjk's avatar
hjk committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Breakpoints. Keep synchronized with BreakpointType in breakpoint.h
UnknownType, \
BreakpointByFileAndLine, \
BreakpointByFunction, \
BreakpointByAddress, \
BreakpointAtThrow, \
BreakpointAtCatch, \
BreakpointAtMain, \
BreakpointAtFork, \
BreakpointAtExec, \
BreakpointAtSysCall, \
WatchpointAtAddress, \
WatchpointAtExpression, \
BreakpointOnQmlSignalEmit, \
BreakpointAtJavaScriptThrow, \
    = range(0, 14)
108
109
110
#
# matplot based display for array-like structures.
#
111
try:
112
    import matplotlib
113
    hasPlot = True
114
except:
115
116
    hasPlot = False

117
118
119
if hasSubprocess and hasPlot:
    matplotFigure = {}
    matplotCount = 0
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    matplotProc = None
    devNull = None

    def matplotInit():
        global matplotProc
        global devNull

        if matplotProc is None:
            devNull = open(os.devnull)
            # FIXME: That might not be the one we want.
            pythonExecutable = sys.executable
            matplotProc = subprocess.Popen(args=[pythonExecutable, "-i"],
                        bufsize=0, stdin=subprocess.PIPE, stdout=devNull, stderr=devNull)

            matplotProc.stdin.write(b"import sys\n")
            matplotProc.stdin.write(b"sys.ps1=''\n")
            matplotProc.stdin.write(b"from matplotlib import pyplot\n")
            matplotProc.stdin.write(b"import time\n")
            matplotProc.stdin.write(b"pyplot.ion()\n")
            matplotProc.stdin.flush()
140

141
142
143
144
    def matplotSend(iname, show, data):
        global matplotFigure
        global matplotCount

145
146
        matplotInit()

147
148
149
150
151
152
153
154
155
156
157
        def s(line):
            matplotProc.stdin.write(line.encode("latin1"))
            matplotProc.stdin.write(b"\n")
            sys.stdout.flush()
            matplotProc.stdin.flush()

        if show:
            s("pyplot.ion()")
            if not iname in matplotFigure:
                matplotCount += 1
                matplotFigure[iname] = matplotCount
158
            s("pyplot.figure(%s)" % matplotFigure[iname])
159
160
            s("pyplot.suptitle('%s')" % iname)
            s("data = %s" % data)
161
            s("pyplot.plot([i for i in range(len(data))], data, 'b.-')")
162
163
164
165
166
167
168
169
            time.sleep(0.2)
            s("pyplot.draw()")
            matplotProc.stdin.flush()
        else:
            if iname in matplotFigure:
                s("pyplot.figure(%s)" % matplotFigure[iname])
                s("pyplot.close()")
                del matplotFigure[iname]
170

171
        matplotProc.stdin.flush()
172

173
    def matplotQuit():
174
175
176
177
178
        global matplotProc
        if not matplotProc is None:
            matplotProc.stdin.write(b"exit")
            matplotProc.kill()
            devNull.close()
179
180
181

def arrayForms():
    global hasPlot
182
    return [ArrayPlotFormat] if hasPlot else []
183
184

def mapForms():
185
    return [CompactMapFormat]
186
187


188
189
190
191
192
193
class ReportItem:
    """
    Helper structure to keep temporary "best" information about a value
    or a type scheduled to be reported. This might get overridden be
    subsequent better guesses during a putItem() run.
    """
hjk's avatar
hjk committed
194
195
196
197
198
199
200
201
202
    def __init__(self, value = None, encoding = None, priority = -100, elided = None):
        self.value = value
        self.priority = priority
        self.encoding = encoding
        self.elided = elided

    def __str__(self):
        return "Item(value: %s, encoding: %s, priority: %s, elided: %s)" \
            % (self.value, self.encoding, self.priority, self.elided)
203
204


hjk's avatar
hjk committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class Blob(object):
    """
    Helper structure to keep a blob of bytes, possibly
    in the inferior.
    """

    def __init__(self, data, isComplete = True):
        self.data = data
        self.size = len(data)
        self.isComplete = isComplete

    def size(self):
        return self.size

219
220
221
    def toBytes(self):
        """Retrieves "lazy" contents from memoryviews."""
        data = self.data
hjk's avatar
hjk committed
222
223
224
225
226
227

        major = sys.version_info[0]
        if major == 3 or (major == 2 and sys.version_info[1] >= 7):
            if isinstance(data, memoryview):
                data = data.tobytes()
        if major == 2 and isinstance(data, buffer):
228
229
230
            data = ''.join([c for c in data])
        return data

hjk's avatar
hjk committed
231
232
233
234
    def toString(self):
        data = self.toBytes()
        return data if sys.version_info[0] == 2 else data.decode("utf8")

235
236
237
238
239
240
241
242
243
244
245
246
247
248
    def extractByte(self, offset = 0):
        return struct.unpack_from("b", self.data, offset)[0]

    def extractShort(self, offset = 0):
        return struct.unpack_from("h", self.data, offset)[0]

    def extractUShort(self, offset = 0):
        return struct.unpack_from("H", self.data, offset)[0]

    def extractInt(self, offset = 0):
        return struct.unpack_from("i", self.data, offset)[0]

    def extractUInt(self, offset = 0):
        return struct.unpack_from("I", self.data, offset)[0]
249

250
251
    def extractLong(self, offset = 0):
        return struct.unpack_from("l", self.data, offset)[0]
252

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    # FIXME: Note these should take target architecture into account.
    def extractULong(self, offset = 0):
        return struct.unpack_from("L", self.data, offset)[0]

    def extractInt64(self, offset = 0):
        return struct.unpack_from("q", self.data, offset)[0]

    def extractUInt64(self, offset = 0):
        return struct.unpack_from("Q", self.data, offset)[0]

    def extractDouble(self, offset = 0):
        return struct.unpack_from("d", self.data, offset)[0]

    def extractFloat(self, offset = 0):
        return struct.unpack_from("f", self.data, offset)[0]

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
def warn(message):
    print("XXX: %s\n" % message.encode("latin1"))


def showException(msg, exType, exValue, exTraceback):
    warn("**** CAUGHT EXCEPTION: %s ****" % msg)
    try:
        import traceback
        for line in traceback.format_exception(exType, exValue, exTraceback):
            warn("%s" % line)
    except:
        pass


class Children:
    def __init__(self, d, numChild = 1, childType = None, childNumChild = None,
            maxNumChild = None, addrBase = None, addrStep = None):
        self.d = d
        self.numChild = numChild
        self.childNumChild = childNumChild
        self.maxNumChild = maxNumChild
        self.addrBase = addrBase
        self.addrStep = addrStep
        self.printsAddress = True
        if childType is None:
            self.childType = None
        else:
296
            self.childType = d.stripClassTag(str(childType))
297
298
            if not self.d.isCli:
                self.d.put('childtype="%s",' % self.childType)
299
300
301
302
303
304
305
306
307
308
309
            if childNumChild is None:
                pass
                #if self.d.isSimpleType(childType):
                #    self.d.put('childnumchild="0",')
                #    self.childNumChild = 0
                #elif childType.code == PointerCode:
                #    self.d.put('childnumchild="1",')
                #    self.childNumChild = 1
            else:
                self.d.put('childnumchild="%s",' % childNumChild)
                self.childNumChild = childNumChild
310
        self.printsAddress = not self.d.putAddressRange(addrBase, addrStep)
311
312
313
314
315
316
317
318
319
320
321
322

    def __enter__(self):
        self.savedChildType = self.d.currentChildType
        self.savedChildNumChild = self.d.currentChildNumChild
        self.savedNumChild = self.d.currentNumChild
        self.savedMaxNumChild = self.d.currentMaxNumChild
        self.savedPrintsAddress = self.d.currentPrintsAddress
        self.d.currentChildType = self.childType
        self.d.currentChildNumChild = self.childNumChild
        self.d.currentNumChild = self.numChild
        self.d.currentMaxNumChild = self.maxNumChild
        self.d.currentPrintsAddress = self.printsAddress
323
        self.d.put(self.d.childrenPrefix)
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

    def __exit__(self, exType, exValue, exTraceBack):
        if not exType is None:
            if self.d.passExceptions:
                showException("CHILDREN", exType, exValue, exTraceBack)
            self.d.putNumChild(0)
            self.d.putValue("<not accessible>")
        if not self.d.currentMaxNumChild is None:
            if self.d.currentMaxNumChild < self.d.currentNumChild:
                self.d.put('{name="<incomplete>",value="",type="",numchild="0"},')
        self.d.currentChildType = self.savedChildType
        self.d.currentChildNumChild = self.savedChildNumChild
        self.d.currentNumChild = self.savedNumChild
        self.d.currentMaxNumChild = self.savedMaxNumChild
        self.d.currentPrintsAddress = self.savedPrintsAddress
339
340
        self.d.putNewline()
        self.d.put(self.d.childrenSuffix)
341
342
        return True

343
class PairedChildrenData:
344
345
    def __init__(self, d, pairType, keyType, valueType, useKeyAndValue):
        self.useKeyAndValue = useKeyAndValue
346
        self.pairType = pairType
347
348
        self.keyType = keyType
        self.valueType = valueType
349
        self.isCompact = d.isMapCompact(self.keyType, self.valueType)
350
        self.childType = valueType if self.isCompact else pairType
351
        ns = d.qtNamespace()
352
353
354
355
356
        keyTypeName = d.stripClassTag(str(self.keyType))
        self.keyIsQString = keyTypeName == ns + "QString"
        self.keyIsQByteArray = keyTypeName == ns + "QByteArray"
        self.keyIsStdString = keyTypeName == "std::string" \
            or keyTypeName.startswith("std::basic_string<char")
357

hjk's avatar
hjk committed
358
class PairedChildren(Children):
359
360
    def __init__(self, d, numChild, useKeyAndValue = False,
            pairType = None, keyType = None, valueType = None, maxNumChild = None):
361
        self.d = d
362
363
364
365
        if keyType is None:
            keyType = d.templateArgument(pairType, 0).unqualified()
        if valueType is None:
            valueType = d.templateArgument(pairType, 1)
366
        d.pairData = PairedChildrenData(d, pairType, keyType, valueType, useKeyAndValue)
367

368
        Children.__init__(self, d, numChild,
369
370
371
            d.pairData.childType,
            maxNumChild = maxNumChild,
            addrBase = None, addrStep = None)
372
373
374
375
376
377
378
379
380

    def __enter__(self):
        self.savedPairData = self.d.pairData if hasattr(self.d, "pairData") else None
        Children.__enter__(self)

    def __exit__(self, exType, exValue, exTraceBack):
        Children.__exit__(self, exType, exValue, exTraceBack)
        self.d.pairData = self.savedPairData if self.savedPairData else None

381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421

class SubItem:
    def __init__(self, d, component):
        self.d = d
        self.name = component
        self.iname = None

    def __enter__(self):
        self.d.enterSubItem(self)

    def __exit__(self, exType, exValue, exTraceBack):
        return self.d.exitSubItem(self, exType, exValue, exTraceBack)

class NoAddress:
    def __init__(self, d):
        self.d = d

    def __enter__(self):
        self.savedPrintsAddress = self.d.currentPrintsAddress
        self.d.currentPrintsAddress = False

    def __exit__(self, exType, exValue, exTraceBack):
        self.d.currentPrintsAddress = self.savedPrintsAddress

class TopLevelItem(SubItem):
    def __init__(self, d, iname):
        self.d = d
        self.iname = iname
        self.name = None

class UnnamedSubItem(SubItem):
    def __init__(self, d, component):
        self.d = d
        self.iname = "%s.%s" % (self.d.currentIName, component)
        self.name = None

class DumperBase:
    def __init__(self):
        self.isCdb = False
        self.isGdb = False
        self.isLldb = False
422
        self.isCli = False
423

424
425
426
        # Later set, or not set:
        # cachedQtVersion
        self.stringCutOff = 10000
427
        self.displayStringLimit = 100
428
429
430
431
432
433
434
435
436
437
438
439
440

        # This is a cache mapping from 'type name' to 'display alternatives'.
        self.qqFormats = {}

        # This is a cache of all known dumpers.
        self.qqDumpers = {}

        # This is a cache of all dumpers that support writing.
        self.qqEditable = {}

        # This keeps canonical forms of the typenames, without array indices etc.
        self.cachedFormats = {}

441
442
443
        # Maps type names to static metaobjects. If a type is known
        # to not be QObject derived, it contains a 0 value.
        self.knownStaticMetaObjects = {}
hjk's avatar
hjk committed
444

445
446
447
        self.childrenPrefix = 'children=['
        self.childrenSuffix = '],'

448
449
450
451
452
453
454
455
456
457
        self.dumpermodules = [
            "qttypes",
            "stdtypes",
            "misctypes",
            "boosttypes",
            "creatortypes",
            "personaltypes",
        ]


458
459
    def putNewline(self):
        pass
460

461
462
463
464
465
466
467
468
469
470
471
    def stripClassTag(self, typeName):
        if typeName.startswith("class "):
            return typeName[6:]
        if typeName.startswith("struct "):
            return typeName[7:]
        if typeName.startswith("const "):
            return typeName[6:]
        if typeName.startswith("volatile "):
            return typeName[9:]
        return typeName

472
473
474
475
476
    def stripForFormat(self, typeName):
        if typeName in self.cachedFormats:
            return self.cachedFormats[typeName]
        stripped = ""
        inArray = 0
477
        for c in self.stripClassTag(typeName):
478
479
480
481
482
483
484
485
486
487
488
489
490
491
            if c == '<':
                break
            if c == ' ':
                continue
            if c == '[':
                inArray += 1
            elif c == ']':
                inArray -= 1
            if inArray and ord(c) >= 48 and ord(c) <= 57:
                continue
            stripped +=  c
        self.cachedFormats[typeName] = stripped
        return stripped

492
    # Hex decoding operating on str, return str.
hjk's avatar
hjk committed
493
494
495
496
497
    def hexdecode(self, s):
        if sys.version_info[0] == 2:
            return s.decode("hex")
        return bytes.fromhex(s).decode("utf8")

498
    # Hex decoding operating on str or bytes, return str.
hjk's avatar
hjk committed
499
500
501
    def hexencode(self, s):
        if sys.version_info[0] == 2:
            return s.encode("hex")
502
503
504
505
506
        if isinstance(s, str):
            s = s.encode("utf8")
        return base64.b16encode(s).decode("utf8")

    #def toBlob(self, value):
507
    #    """Abstract"""
hjk's avatar
hjk committed
508

509
510
511
    def is32bit(self):
        return self.ptrSize() == 4

hjk's avatar
hjk committed
512
513
514
    def is64bit(self):
        return self.ptrSize() == 8

515
516
517
518
    def isQt3Support(self):
        # assume no Qt 3 support by default
        return False

519
520
521
    def lookupQtType(self, typeName):
        return self.lookupType(self.qtNamespace() + typeName)

522
    # Clamps size to limit.
523
    def computeLimit(self, size, limit):
hjk's avatar
hjk committed
524
525
        if limit == 0:
            limit = self.displayStringLimit
526
527
528
        if limit is None or size <= limit:
            return 0, size
        return size, limit
529

530
531
532
533
534
535
536
537
538
539
540
    def vectorDataHelper(self, addr):
        if self.qtVersion() >= 0x050000:
            size = self.extractInt(addr + 4)
            alloc = self.extractInt(addr + 8) & 0x7ffffff
            data = addr + self.extractPointer(addr + 8 + self.ptrSize())
        else:
            alloc = self.extractInt(addr + 4)
            size = self.extractInt(addr + 8)
            data = addr + 16
        return data, size, alloc

541
542
543
544
545
546
547
548
549
    def byteArrayDataHelper(self, addr):
        if self.qtVersion() >= 0x050000:
            # QTypedArray:
            # - QtPrivate::RefCount ref
            # - int size
            # - uint alloc : 31, capacityReserved : 1
            # - qptrdiff offset
            size = self.extractInt(addr + 4)
            alloc = self.extractInt(addr + 8) & 0x7ffffff
550
            data = addr + self.extractPointer(addr + 8 + self.ptrSize())
551
552
553
554
            if self.ptrSize() == 4:
                data = data & 0xffffffff
            else:
                data = data & 0xffffffffffffffff
555
        elif self.qtVersion() >= 0x040000:
556
557
558
559
560
561
562
            # Data:
            # - QBasicAtomicInt ref;
            # - int alloc, size;
            # - [padding]
            # - char *data;
            alloc = self.extractInt(addr + 4)
            size = self.extractInt(addr + 8)
563
            data = self.extractPointer(addr + 8 + self.ptrSize())
564
565
566
567
568
569
570
571
572
        else:
            # Data:
            # - QShared count;
            # - QChar *unicode
            # - char *ascii
            # - uint len: 30
            size = self.extractInt(addr + 3 * self.ptrSize()) & 0x3ffffff
            alloc = size  # pretend.
            data = self.extractPointer(addr + self.ptrSize())
573
574
575
        return data, size, alloc

    # addr is the begin of a QByteArrayData structure
576
    def encodeStringHelper(self, addr, limit):
577
578
579
        # Should not happen, but we get it with LLDB as result
        # of inferior calls
        if addr == 0:
580
            return 0, ""
581
582
583
        data, size, alloc = self.byteArrayDataHelper(addr)
        if alloc != 0:
            self.check(0 <= size and size <= alloc and alloc <= 100*1000*1000)
584
585
        elided, shown = self.computeLimit(size, limit)
        return elided, self.readMemory(data, 2 * shown)
586

587
    def encodeByteArrayHelper(self, addr, limit):
588
589
590
        data, size, alloc = self.byteArrayDataHelper(addr)
        if alloc != 0:
            self.check(0 <= size and size <= alloc and alloc <= 100*1000*1000)
591
592
        elided, shown = self.computeLimit(size, limit)
        return elided, self.readMemory(data, shown)
593

594
    def putStdStringHelper(self, data, size, charSize, displayFormat = AutomaticFormat):
595
596
597
598
        bytelen = size * charSize
        elided, shown = self.computeLimit(bytelen, self.displayStringLimit)
        mem = self.readMemory(data, shown)
        if charSize == 1:
599
600
            if displayFormat == Latin1StringFormat \
                    or displayFormat == SeparateLatin1StringFormat:
601
602
603
                encodingType = Hex2EncodedLatin1
            else:
                encodingType = Hex2EncodedUtf8
604
605
606
607
608
609
610
611
612
613
614
            displayType = DisplayLatin1String
        elif charSize == 2:
            encodingType = Hex4EncodedLittleEndian
            displayType = DisplayUtf16String
        else:
            encodingType = Hex8EncodedLittleEndian
            displayType = DisplayUtf16String

        self.putNumChild(0)
        self.putValue(mem, encodingType, elided=elided)

615
616
        if displayFormat == Latin1StringFormat \
                or displayFormat == Utf8StringFormat:
617
            self.putDisplay(StopDisplay)
618
619
        elif displayFormat == SeparateLatin1StringFormat \
                or displayFormat == SeparateUtf8StringFormat:
620
621
622
623
            self.putField("editformat", displayType)
            elided, shown = self.computeLimit(bytelen, 100000)
            self.putField("editvalue", self.readMemory(data, shown))

hjk's avatar
hjk committed
624
    def readMemory(self, addr, size):
625
626
        data = self.extractBlob(addr, size).toBytes()
        return self.hexencode(data)
hjk's avatar
hjk committed
627

628
    def encodeByteArray(self, value, limit = 0):
629
630
        elided, data = self.encodeByteArrayHelper(self.extractPointer(value), limit)
        return data
631
632

    def byteArrayData(self, value):
633
        return self.byteArrayDataHelper(self.extractPointer(value))
634

hjk's avatar
hjk committed
635
636
637
    def putByteArrayValue(self, value):
        elided, data = self.encodeByteArrayHelper(self.extractPointer(value), self.displayStringLimit)
        self.putValue(data, Hex2EncodedLatin1, elided=elided)
638

639
    def encodeString(self, value, limit = 0):
640
641
        elided, data = self.encodeStringHelper(self.extractPointer(value), limit)
        return data
642

643
644
645
646
647
648
    def encodedUtf16ToUtf8(self, s):
        return ''.join([chr(int(s[i:i+2], 16)) for i in range(0, len(s), 4)])

    def encodeStringUtf8(self, value, limit = 0):
        return self.encodedUtf16ToUtf8(self.encodeString(value, limit))

649
    def stringData(self, value):
650
        return self.byteArrayDataHelper(self.extractPointer(value))
651

652
653
654
655
656
657
658
659
660
    def encodeStdString(self, value, limit = 0):
        data = value["_M_dataplus"]["_M_p"]
        sizePtr = data.cast(self.sizetType().pointer())
        size = int(sizePtr[-3])
        alloc = int(sizePtr[-2])
        self.check(0 <= size and size <= alloc and alloc <= 100*1000*1000)
        elided, shown = self.computeLimit(size, limit)
        return self.readMemory(data, shown)

661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
    def extractTemplateArgument(self, typename, position):
        level = 0
        skipSpace = False
        inner = ''
        for c in typename[typename.find('<') + 1 : -1]:
            if c == '<':
                inner += c
                level += 1
            elif c == '>':
                level -= 1
                inner += c
            elif c == ',':
                if level == 0:
                    if position == 0:
                        return inner.strip()
                    position -= 1
                    inner = ''
                else:
                    inner += c
                    skipSpace = True
            else:
                if skipSpace and c == ' ':
                    pass
                else:
                    inner += c
                    skipSpace = False
        return inner.strip()

hjk's avatar
hjk committed
689
690
691
692
    def putStringValueByAddress(self, addr):
        elided, data = self.encodeStringHelper(addr, self.displayStringLimit)
        self.putValue(data, Hex4EncodedLittleEndian, elided=elided)

693
    def putStringValue(self, value):
hjk's avatar
hjk committed
694
695
696
        elided, data = self.encodeStringHelper(
            self.extractPointer(value),
            self.displayStringLimit)
697
        self.putValue(data, Hex4EncodedLittleEndian, elided=elided)
698

699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
    def putAddressItem(self, name, value, type = ""):
        with SubItem(self, name):
            self.putValue("0x%x" % value)
            self.putType(type)
            self.putNumChild(0)

    def putIntItem(self, name, value):
        with SubItem(self, name):
            self.putValue(value)
            self.putType("int")
            self.putNumChild(0)

    def putBoolItem(self, name, value):
        with SubItem(self, name):
            self.putValue(value)
            self.putType("bool")
            self.putNumChild(0)

    def putGenericItem(self, name, type, value, encoding = None):
        with SubItem(self, name):
            self.putValue(value, encoding)
            self.putType(type)
            self.putNumChild(0)

723
724
725
726
727
728
729
730
731
732
733
734
    def putCallItem(self, name, value, func, *args):
        try:
            result = self.callHelper(value, func, args)
            with SubItem(self, name):
                self.putItem(result)
        except:
            with SubItem(self, name):
                self.putValue("<not callable>")
                self.putNumChild(0)

    def call(self, value, func, *args):
        return self.callHelper(value, func, args)
735

736
737
738
739
740
741
742
    def putAddressRange(self, base, step):
        try:
            if not addrBase is None and not step is None:
                self.put('addrbase="0x%x",' % toInteger(base))
                self.put('addrstep="0x%x",' % toInteger(step))
                return True
        except:
hjk's avatar
hjk committed
743
744
            #warn("ADDRBASE: %s" % base)
            #warn("ADDRSTEP: %s" % step)
745
            pass
746
747
748
        return False

        #warn("CHILDREN: %s %s %s" % (numChild, childType, childNumChild))
749
    def putMapName(self, value, index = None):
750
        ns = self.qtNamespace()
751
752
        typeName = self.stripClassTag(str(value.type))
        if typeName == ns + "QString":
753
754
            self.put('key="%s",' % self.encodeString(value))
            self.put('keyencoded="%s",' % Hex4EncodedLittleEndian)
755
        elif typeName == ns + "QByteArray":
756
757
            self.put('key="%s",' % self.encodeByteArray(value))
            self.put('keyencoded="%s",' % Hex2EncodedLatin1)
758
759
760
        elif typeName == "std::string":
            self.put('key="%s",' % self.encodeStdString(value))
            self.put('keyencoded="%s",' % Hex2EncodedLatin1)
761
        else:
762
            val = str(value.GetValue()) if self.isLldb else str(value)
763
764
            if index is None:
                key = '%s' % val
765
            else:
766
                key = '[%s] %s' % (index, val)
767
            self.put('key="%s",' % self.hexencode(key))
768
            self.put('keyencoded="%s",' % Hex2EncodedUtf8WithoutQuotes)
769

770
    def putPair(self, pair, index = None):
771
        if self.pairData.useKeyAndValue:
772
773
            key = pair["key"]
            value = pair["value"]
774
775
776
        else:
            key = pair["first"]
            value = pair["second"]
777
778
779
780
781
782
783
        if self.pairData.isCompact:
            if self.pairData.keyIsQString:
                self.put('key="%s",' % self.encodeString(key))
                self.put('keyencoded="%s",' % Hex4EncodedLittleEndian)
            elif self.pairData.keyIsQByteArray:
                self.put('key="%s",' % self.encodeByteArray(key))
                self.put('keyencoded="%s",' % Hex2EncodedLatin1)
784
785
786
            elif self.pairData.keyIsStdString:
                self.put('key="%s",' % self.encodeStdString(key))
                self.put('keyencoded="%s",' % Hex2EncodedLatin1)
787
788
789
790
791
            else:
                name = str(key.GetValue()) if self.isLldb else str(key)
                if index == -1:
                    self.put('name="%s",' % name)
                else:
792
                    self.put('key="[%s] %s",' % (index, name))
793
794
795
            self.putItem(value)
        else:
            self.putEmptyValue()
796
797
            self.putNumChild(2)
            self.putField("iname", self.currentIName)
798
            if self.isExpanded():
799
                with Children(self):
800
801
802
803
804
805
                    if self.pairData.useKeyAndValue:
                        self.putSubItem("key", key)
                        self.putSubItem("value", value)
                    else:
                        self.putSubItem("first", key)
                        self.putSubItem("second", value)
806

807
808
809
810
811
812
813
    def putPlainChildren(self, value, dumpBase = True):
        self.putEmptyValue(-99)
        self.putNumChild(1)
        if self.isExpanded():
            with Children(self):
                self.putFields(value, dumpBase)

814
    def isMapCompact(self, keyType, valueType):
815
816
        if self.currentItemFormat() == CompactMapFormat:
            return True
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
        return self.isSimpleType(keyType) and self.isSimpleType(valueType)


    def check(self, exp):
        if not exp:
            raise RuntimeError("Check failed")

    def checkRef(self, ref):
        try:
            count = int(ref["atomic"]["_q_value"]) # Qt 5.
            minimum = -1
        except:
            count = int(ref["_q_value"]) # Qt 4.
            minimum = 0
        # Assume there aren't a million references to any object.
        self.check(count >= minimum)
        self.check(count < 1000000)

835
836
837
838
    def readToFirstZero(self, p, tsize, maximum):
        code = (None, "b", "H", None, "I")[tsize]
        base = toInteger(p)
        blob = self.extractBlob(base, maximum).toBytes()
839
        for i in xrange(0, int(maximum / tsize)):
840
841
842
843
            t = struct.unpack_from(code, blob, i)[0]
            if t == 0:
                return 0, i, self.hexencode(blob[:i])

844
        # Real end is unknown.
845
        return -1, maximum, self.hexencode(blob[:maximum])
hjk's avatar
hjk committed
846

847
848
849
    def encodeCArray(self, p, tsize, limit):
        elided, shown, blob = self.readToFirstZero(p, tsize, limit)
        return elided, blob
hjk's avatar
hjk committed
850

851
852
853
854
855
856
    def putItemCount(self, count, maximum = 1000000000):
        # This needs to override the default value, so don't use 'put' directly.
        if count > maximum:
            self.putValue('<>%s items>' % maximum)
        else:
            self.putValue('<%s items>' % count)
857
        self.putNumChild(count)
858

859
860
861
862
863
    def putField(self, name, value):
        self.put('%s="%s",' % (name, value))

    def putType(self, type, priority = 0):
        # Higher priority values override lower ones.
864
865
866
        if priority >= self.currentType.priority:
            self.currentType.value = str(type)
            self.currentType.priority = priority
867

868
    def putValue(self, value, encoding = None, priority = 0, elided = None):
869
        # Higher priority values override lower ones.
870
871
872
        # elided = 0 indicates all data is available in value,
        # otherwise it's the true length.
        if priority >= self.currentValue.priority:
hjk's avatar
hjk committed
873
            self.currentValue = ReportItem(value, encoding, priority, elided)
874
875

    def putEmptyValue(self, priority = -10):
876
        if priority >= self.currentValue.priority:
hjk's avatar
hjk committed
877
            self.currentValue = ReportItem("", None, priority, None)
878
879
880
881

    def putName(self, name):
        self.put('name="%s",' % name)

882
    def putBetterType(self, type):
hjk's avatar
hjk committed
883
884
885
886
        if isinstance(type, ReportItem):
            self.currentType.value = str(type.value)
        else:
            self.currentType.value = str(type)
887
888
        self.currentType.priority += 1

889
890
891
892
893
894
895
896
    def putNoType(self):
        # FIXME: replace with something that does not need special handling
        # in SubItem.__exit__().
        self.putBetterType(" ")

    def putInaccessible(self):
        #self.putBetterType(" ")
        self.putNumChild(0)
897
        self.currentValue.value = None
898

899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
    def putNamedSubItem(self, component, value, name):
        with SubItem(self, component):
            self.putName(name)
            self.putItem(value)

    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 putPlainChildren(self, value):
        self.putEmptyValue(-99)
        self.putNumChild(1)
        if self.currentIName in self.expandedINames:
            with Children(self):
               self.putFields(value)

916
    def putCStyleArray(self, value):
917
        arrayType = value.type.unqualified()
918
        innerType = value[0].type
919
        ts = innerType.sizeof
920
        #self.putAddress(value.address)
921
922
923
924
        try:
            self.putValue("@0x%x" % self.addressOf(value), priority = -1)
        except:
            self.putEmptyValue()
925
        self.putType(arrayType)
926
        self.putNumChild(1)
927
928
929
930
931
932

        try:
            p = self.addressOf(value)
        except:
            p = None

933
        displayFormat = self.currentItemFormat()
934
935
        n = int(arrayType.sizeof / ts)

936
        if p and self.tryPutSimpleFormattedPointer(p, str(arrayType), displayFormat, arrayType.sizeof):
937
938
            self.putNumChild(n)
            pass
939
        elif displayFormat is None:
940
941
942
943
944
945
946
947
948
949
            innerTypeName = str(innerType.unqualified())
            blob = self.readMemory(self.addressOf(value), arrayType.sizeof)
            if innerTypeName == "char":
                # Use Latin1 as default for char [].
                self.putValue(blob, Hex2EncodedLatin1)
            elif innerTypeName == "wchar_t":
                if innerType.sizeof == 2:
                    self.putValue(blob, Hex4EncodedLittleEndian)
                else:
                    self.putValue(blob, Hex8EncodedLittleEndian)
950

hjk's avatar
hjk committed
951
        if self.isExpanded():
hjk's avatar
hjk committed
952
953
            try:
                # May fail on artificial items like xmm register data.
hjk's avatar
hjk committed
954
                #if not self.tryPutArrayContents(p, n, innerType):
955
                    with Children(self, childType=innerType, addrBase=p, addrStep=ts):
hjk's avatar
hjk committed
956
957
                        self.putFields(value)
            except:
958
                with Children(self, childType=innerType):
959
960
                    self.putFields(value)

hjk's avatar
hjk committed
961
962
963
964
965
966
967
968
969
970
971
972
        if hasPlot and self.isSimpleType(innerType):
            show = displayFormat == ArrayPlotFormat
            iname = self.currentIName
            data = []
            if show:
                base = self.createPointerValue(p, innerType)
                data = [str(base[i]) for i in range(0, n)]
            matplotSend(iname, show, data)
        else:
            #self.putValue(self.currentValue.value + " (not plottable)")
            self.putField("plottable", "0")

973
974
975
976
977
978
979
980
981
982
983
    def cleanAddress(self, addr):
        if addr is None:
            return "<no address>"
        # We cannot use str(addr) as it yields rubbish for char pointers
        # that might trigger Unicode encoding errors.
        #return addr.cast(lookupType("void").pointer())
        # We do not use "hex(...)" as it (sometimes?) adds a "L" suffix.
        try:
            return "0x%x" % toInteger(addr)
        except:
            warn("CANNOT CONVERT TYPE: %s" % type(addr))
984
985
986
987
988
989
990
991
            try:
                warn("ADDR: %s" % addr)
            except:
                pass
            try:
                warn("TYPE: %s" % addr.type)
            except:
                pass
992
993
            return str(addr)

994
995
996
997
998
999
1000
    def tryPutPrettyItem(self, typeName, value):
        if self.useFancy and self.currentItemFormat() != RawFormat:
            self.putType(typeName)

            nsStrippedType = self.stripNamespaceFromType(typeName)\
                .replace("::", "__")

For faster browsing, not all history is shown. View entire blame