qttypes.py 98.6 KB
Newer Older
hjk's avatar
hjk committed
1
2
3
4
5
6
7

#######################################################################
#
# Dumper Implementations
#
#######################################################################

8
9
from __future__ import with_statement

10
11
12
13
14
15
16
17
18
19
movableTypes = set([
    "QBrush", "QBitArray", "QByteArray", "QCustomTypeInfo", "QChar", "QDate",
    "QDateTime", "QFileInfo", "QFixed", "QFixedPoint", "QFixedSize",
    "QHashDummyValue", "QIcon", "QImage", "QLine", "QLineF", "QLatin1Char",
    "QLocale", "QMatrix", "QModelIndex", "QPoint", "QPointF", "QPen",
    "QPersistentModelIndex", "QResourceRoot", "QRect", "QRectF", "QRegExp",
    "QSize", "QSizeF", "QString", "QTime", "QTextBlock", "QUrl", "QVariant",
    "QXmlStreamAttribute", "QXmlStreamNamespaceDeclaration",
    "QXmlStreamNotationDeclaration", "QXmlStreamEntityDeclaration"
])
20

21
def qByteArrayData(d, value):
22
    private = value['d'].dereference()
23
24
25
26
27
    checkRef(private['ref'])
    size = int(private['size'])
    alloc = int(private['alloc'])
    try:
        # Qt 5. Will fail on Qt 4 due to the missing 'offset' member.
28
29
30
        offset = int(private['offset'])
        addr = d.addressOf(private) + offset
        data = createPointerValue(value, addr, d.charType())
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        return data, size, alloc
    except:
        # Qt 4:
        return private['data'], size, alloc

qStringData = qByteArrayData

def qEncodeByteArray(d, value, limit = None):
    data, size, alloc = qByteArrayData(d, value)
    if alloc != 0:
        check(0 <= size and size <= alloc and alloc <= 100*1000*1000)
    limit = d.computeLimit(size, limit)
    s = d.readRawMemory(data, limit)
    if limit < size:
        s += "2e2e2e"
    return s

def qEncodeString(d, value, limit = 0):
    data, size, alloc = qStringData(d, value)
    if alloc != 0:
        check(0 <= size and size <= alloc and alloc <= 100*1000*1000)
    limit = d.computeLimit(size, limit)
    s = d.readRawMemory(data, 2 * limit)
    if limit < size:
        s += "2e002e002e00"
    return s

def qPutByteArrayValue(d, value):
    d.putValue(qEncodeByteArray(d, value), Hex2EncodedLatin1)

def qPutStringValue(d, value):
    d.putValue(qEncodeString(d, value), Hex4EncodedLittleEndian)
63

hjk's avatar
hjk committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def mapForms():
    return "Normal,Compact"

def arrayForms():
    if hasPlot():
        return "Normal,Plot"
    return "Normal"

def mapCompact(format, keyType, valueType):
    if format == 2:
        return True # Compact.
    return isSimpleType(keyType) and isSimpleType(valueType)

def putMapName(d, value):
    if str(value.type) == d.ns + "QString":
        d.put('key="%s",' % d.encodeString(value))
        d.put('keyencoded="%s",' % Hex4EncodedLittleEndian)
    elif str(value.type) == d.ns + "QByteArray":
        d.put('key="%s",' % d.encodeByteArray(value))
        d.put('keyencoded="%s",' % Hex2EncodedLatin1)
    else:
        if lldbLoaded:
            d.put('name="%s",' % value.GetValue())
        else:
            d.put('name="%s",' % value)

# Compatibility with earlier versions
Dumper.encodeByteArray = qEncodeByteArray
Dumper.byteArrayData = qByteArrayData
Dumper.putByteArrayValue = qPutByteArrayValue
Dumper.encodeString = qEncodeString
Dumper.stringData = qStringData
Dumper.putStringValue = qPutStringValue

Dumper.putMapName = putMapName

Dumper.isMapCompact = \
    lambda d, keyType, valueType: mapCompact(d.currentItemFormat(), keyType, valueType)


def tryPutObjectNameValue(d, value):
    try:
        # Is this derived from QObject?
        dd = value["d_ptr"]["d"]
        privateType = d.lookupType(d.ns + "QObjectPrivate")
        staticMetaObject = value["staticMetaObject"]
        d_ptr = dd.cast(privateType.pointer()).dereference()
        objectName = None
        try:
            objectName = d_ptr["objectName"]
        except: # Qt 5
            p = d_ptr["extraData"]
            if not isNull(p):
                objectName = p.dereference()["objectName"]
        if not objectName is None:
            data, size, alloc = d.stringData(objectName)
            if size > 0:
                str = d.readRawMemory(data, 2 * size)
                d.putValue(str, Hex4EncodedLittleEndian, 1)
    except:
        pass

Dumper.tryPutObjectNameValue = tryPutObjectNameValue

############################################################################################


def qdump__QAtomicInt(d, value):
    d.putValue(value["_q_value"])
    d.putNumChild(0)


def qdump__QBasicAtomicInt(d, value):
    d.putValue(value["_q_value"])
    d.putNumChild(0)


def qdump__QBasicAtomicPointer(d, value):
    d.putType(value.type)
    p = cleanAddress(value["_q_value"])
    d.putValue(p)
    d.putPointerValue(value.address)
    d.putNumChild(p)
    if d.isExpanded():
        with Children(d):
           d.putItem(value["_q_value"])

151
152
153
def qform__QByteArray():
    return "Inline,As Latin1 in Separate Window,As UTF-8 in Separate Window"

hjk's avatar
hjk committed
154
155
def qdump__QByteArray(d, value):
    d.putByteArrayValue(value)
156
    data, size, alloc = qByteArrayData(d, value)
157
    d.putNumChild(size)
158
159
160
161
    format = d.currentItemFormat()
    if format == 1:
        d.putDisplay(StopDisplay)
    elif format == 2:
162
        d.putField("editformat", DisplayLatin1String)
163
        d.putField("editvalue", qEncodeByteArray(d, value, None))
164
    elif format == 3:
165
        d.putField("editformat", DisplayUtf8String)
166
        d.putField("editvalue", qEncodeByteArray(d, value, None))
hjk's avatar
hjk committed
167
    if d.isExpanded():
168
        d.putArrayData(d.charType(), data, size)
hjk's avatar
hjk committed
169
170


171
172
173
174
175
176
177
178
179
180
181
182
183
# Fails on Windows.
try:
    import curses.ascii
    def printableChar(ucs):
        if curses.ascii.isprint(ucs):
            return ucs
        return '?'
except:
    def printableChar(ucs):
        if ucs >= 32 and ucs <= 126:
            return ucs
        return '?'

hjk's avatar
hjk committed
184
185
def qdump__QChar(d, value):
    ucs = int(value["ucs"])
186
    d.putValue("'%c' (%d)" % (printableChar(ucs), ucs))
hjk's avatar
hjk committed
187
188
189
    d.putNumChild(0)


hjk's avatar
hjk committed
190
def qform__QAbstractItemModel():
191
    return "Normal,Enhanced"
192

hjk's avatar
hjk committed
193
def qdump__QAbstractItemModel(d, value):
194
195
196
197
198
    format = d.currentItemFormat()
    if format == 1:
        d.putPlainChildren(value)
        return
    #format == 2:
199
    # Create a default-constructed QModelIndex on the stack.
200
    try:
201
        ri = makeValue(d.ns + "QModelIndex", "-1, -1, 0, 0")
hjk's avatar
hjk committed
202
        this_ = makeExpression(value)
203
        ri_ = makeExpression(ri)
204
205
206
        rowCount = int(parseAndEvaluate("%s.rowCount(%s)" % (this_, ri_)))
        columnCount = int(parseAndEvaluate("%s.columnCount(%s)" % (this_, ri_)))
    except:
hjk's avatar
hjk committed
207
        d.putPlainChildren(value)
208
        return
209
210
    d.putValue("%d x %d" % (rowCount, columnCount))
    d.putNumChild(rowCount * columnCount)
hjk's avatar
hjk committed
211
212
    if d.isExpanded():
        with Children(d, numChild=rowCount * columnCount, childType=ri.type):
213
214
215
            i = 0
            for row in xrange(rowCount):
                for column in xrange(columnCount):
hjk's avatar
hjk committed
216
                    with SubItem(d, i):
217
218
219
220
221
222
                        d.putName("[%s, %s]" % (row, column))
                        mi = parseAndEvaluate("%s.index(%d,%d,%s)"
                            % (this_, row, column, ri_))
                        #warn("MI: %s " % mi)
                        #name = "[%d,%d]" % (row, column)
                        #d.putValue("%s" % mi)
hjk's avatar
hjk committed
223
                        d.putItem(mi)
224
225
226
227
228
229
230
231
                        i = i + 1
                        #warn("MI: %s " % mi)
                        #d.putName("[%d,%d]" % (row, column))
                        #d.putValue("%s" % mi)
                        #d.putNumChild(0)
                        #d.putType(mi.type)
    #gdb.execute("call free($ri)")

232
233
234
def qform__QModelIndex():
    return "Normal,Enhanced"

hjk's avatar
hjk committed
235
def qdump__QModelIndex(d, value):
236
237
238
239
    format = d.currentItemFormat()
    if format == 1:
        d.putPlainChildren(value)
        return
hjk's avatar
hjk committed
240
241
    r = value["r"]
    c = value["c"]
242
243
244
245
    try:
        p = value["p"]
    except:
        p = value["i"]
hjk's avatar
hjk committed
246
    m = value["m"]
247
248
249
250
251
    if isNull(m) or r < 0 or c < 0:
        d.putValue("(invalid)")
        d.putPlainChildren(value)
        return

252
253
    mm = m.dereference()
    mm = mm.cast(mm.type.unqualified())
254
    try:
255
256
257
        mi = makeValue(d.ns + "QModelIndex", "%s,%s,%s,%s" % (r, c, p, m))
        mm_ = makeExpression(mm)
        mi_ = makeExpression(mi)
258
259
260
        rowCount = int(parseAndEvaluate("%s.rowCount(%s)" % (mm_, mi_)))
        columnCount = int(parseAndEvaluate("%s.columnCount(%s)" % (mm_, mi_)))
    except:
261
        d.putEmptyValue()
hjk's avatar
hjk committed
262
        d.putPlainChildren(value)
263
        return
264
265
266

    try:
        # Access DisplayRole as value
hjk's avatar
hjk committed
267
268
        val = parseAndEvaluate("%s.data(%s, 0)" % (mm_, mi_))
        v = val["d"]["data"]["ptr"]
269
270
271
272
        d.putStringValue(makeValue(d.ns + 'QString', v))
    except:
        d.putValue("(invalid)")

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    d.putNumChild(rowCount * columnCount)
    if d.isExpanded():
        with Children(d):
            i = 0
            for row in xrange(rowCount):
                for column in xrange(columnCount):
                    with UnnamedSubItem(d, i):
                        d.putName("[%s, %s]" % (row, column))
                        mi2 = parseAndEvaluate("%s.index(%d,%d,%s)"
                            % (mm_, row, column, mi_))
                        d.putItem(mi2)
                        i = i + 1
            #d.putCallItem("parent", val, "parent")
            #with SubItem(d, "model"):
            #    d.putValue(m)
            #    d.putType(d.ns + "QAbstractItemModel*")
            #    d.putNumChild(1)
290
291
    #gdb.execute("call free($mi)")

hjk's avatar
hjk committed
292

hjk's avatar
hjk committed
293
def qdump__QDate(d, value):
294
    jd = int(value["jd"])
295
296
297
298
299
    if int(jd):
        d.putValue(jd, JulianDate)
        d.putNumChild(1)
        if d.isExpanded():
            qt = d.ns + "Qt::"
300
301
            if lldbLoaded:
                qt += "DateFormat::" # FIXME: Bug?...
302
303
304
305
306
307
308
309
310
311
            # FIXME: This improperly uses complex return values.
            with Children(d):
                d.putCallItem("toString", value, "toString", qt + "TextDate")
                d.putCallItem("(ISO)", value, "toString", qt + "ISODate")
                d.putCallItem("(SystemLocale)", value, "toString",
                    qt + "SystemLocaleDate")
                d.putCallItem("(Locale)", value, "toString", qt + "LocaleDate")
    else:
        d.putValue("(invalid)")
        d.putNumChild(0)
hjk's avatar
hjk committed
312
313


hjk's avatar
hjk committed
314
def qdump__QTime(d, value):
315
316
317
318
319
320
    mds = value["mds"]
    if int(mds) >= 0:
        d.putValue(value["mds"], MillisecondsSinceMidnight)
        d.putNumChild(1)
        if d.isExpanded():
            qt = d.ns + "Qt::"
321
322
            if lldbLoaded:
                qt += "DateFormat::" # FIXME: Bug?...
323
324
325
326
327
328
329
330
331
332
333
            # FIXME: This improperly uses complex return values.
            with Children(d):
                d.putCallItem("toString", value, "toString", qt + "TextDate")
                d.putCallItem("(ISO)", value, "toString", qt + "ISODate")
                d.putCallItem("(SystemLocale)", value, "toString",
                     qt + "SystemLocaleDate")
                d.putCallItem("(Locale)", value, "toString", qt + "LocaleDate")
                d.putCallItem("toUTC", value, "toTimeSpec", qt + "UTC")
    else:
        d.putValue("(invalid)")
        d.putNumChild(0)
334
335


hjk's avatar
hjk committed
336
def qdump__QDateTime(d, value):
337
338
    try:
        # Fails without debug info.
339
        p = value["d"]["d"].dereference()
340
    except:
hjk's avatar
hjk committed
341
        d.putPlainChildren(value)
342
        return
343
344
345
    mds = int(p["time"]["mds"])
    if mds >= 0:
        d.putValue("%s/%s" % (int(p["date"]["jd"]), mds),
346
347
348
349
350
351
            JulianDateAndMillisecondsSinceMidnight)
        d.putNumChild(1)
        if d.isExpanded():
            # FIXME: This improperly uses complex return values.
            with Children(d):
                qt = d.ns + "Qt::"
352
353
                if lldbLoaded:
                    qt += "DateFormat::" # FIXME: Bug?...
354
355
356
357
358
359
360
361
362
363
                d.putCallItem("toTime_t", value, "toTime_t")
                d.putCallItem("toString", value, "toString", qt + "TextDate")
                d.putCallItem("(ISO)", value, "toString", qt + "ISODate")
                d.putCallItem("(SystemLocale)", value, "toString", qt + "SystemLocaleDate")
                d.putCallItem("(Locale)", value, "toString", qt + "LocaleDate")
                d.putCallItem("toUTC", value, "toTimeSpec", qt + "UTC")
                d.putCallItem("toLocalTime", value, "toTimeSpec", qt + "LocalTime")
    else:
        d.putValue("(invalid)")
        d.putNumChild(0)
hjk's avatar
hjk committed
364
365
366


def qdump__QDir(d, value):
hjk's avatar
hjk committed
367
    d.putNumChild(1)
368
369
370
371
372
373
374
    data = value["d_ptr"]["d"].dereference()
    try:
        # Up to Qt 4.7
        d.putStringValue(data["path"])
    except:
        # Qt 4.8 and later.
        d.putStringValue(data["dirEntry"]["m_filePath"])
hjk's avatar
hjk committed
375
376
    if d.isExpanded():
        with Children(d):
377
            qdir = d.ns + "QDir::"
hjk's avatar
hjk committed
378
379
            d.putCallItem("absolutePath", value, "absolutePath")
            d.putCallItem("canonicalPath", value, "canonicalPath")
hjk's avatar
hjk committed
380
381
            d.putSubItem("entryList", data["files"])
            d.putSubItem("entryInfoList", data["fileInfos"])
hjk's avatar
hjk committed
382
383


hjk's avatar
hjk committed
384
def qdump__QFile(d, value):
hjk's avatar
hjk committed
385
    try:
386
        ptype = d.lookupType(d.ns + "QFilePrivate").pointer()
hjk's avatar
hjk committed
387
388
389
390
391
        d_ptr = value["d_ptr"]["d"]
        d.putStringValue(d_ptr.cast(ptype).dereference()["fileName"])
    except:
        d.putPlainChildren(value)
        return
392
    d.putNumChild(1)
hjk's avatar
hjk committed
393
394
    if d.isExpanded():
        with Children(d):
hjk's avatar
hjk committed
395
            base = fieldAt(value.type, 0).type
396
            d.putSubItem("[%s]" % str(base), value.cast(base), False)
hjk's avatar
hjk committed
397
            d.putCallItem("exists", value, "exists")
hjk's avatar
hjk committed
398
399


hjk's avatar
hjk committed
400
def qdump__QFileInfo(d, value):
401
    try:
hjk's avatar
hjk committed
402
        d.putStringValue(value["d_ptr"]["d"].dereference()["fileNames"][3])
403
    except:
hjk's avatar
hjk committed
404
        d.putPlainChildren(value)
405
        return
hjk's avatar
hjk committed
406
    d.putNumChild(1)
hjk's avatar
hjk committed
407
    if d.isExpanded():
408
        with Children(d, childType=d.lookupType(d.ns + "QString")):
hjk's avatar
hjk committed
409
410
411
412
413
414
415
            d.putCallItem("absolutePath", value, "absolutePath")
            d.putCallItem("absoluteFilePath", value, "absoluteFilePath")
            d.putCallItem("canonicalPath", value, "canonicalPath")
            d.putCallItem("canonicalFilePath", value, "canonicalFilePath")
            d.putCallItem("completeBaseName", value, "completeBaseName")
            d.putCallItem("completeSuffix", value, "completeSuffix")
            d.putCallItem("baseName", value, "baseName")
416
417
            if False:
                #ifdef Q_OS_MACX
hjk's avatar
hjk committed
418
419
420
421
                d.putCallItem("isBundle", value, "isBundle")
                d.putCallItem("bundleName", value, "bundleName")
            d.putCallItem("fileName", value, "fileName")
            d.putCallItem("filePath", value, "filePath")
422
            # Crashes gdb (archer-tromey-python, at dad6b53fe)
hjk's avatar
hjk committed
423
424
425
            #d.putCallItem("group", value, "group")
            #d.putCallItem("owner", value, "owner")
            d.putCallItem("path", value, "path")
426

hjk's avatar
hjk committed
427
428
            d.putCallItem("groupid", value, "groupId")
            d.putCallItem("ownerid", value, "ownerId")
429
430

            #QFile::Permissions permissions () const
hjk's avatar
hjk committed
431
            perms = call(value, "permissions")
432
433
434
            if perms is None:
                d.putValue("<not available>")
            else:
hjk's avatar
hjk committed
435
                with SubItem(d, "permissions"):
436
                    d.putEmptyValue()
437
438
                    d.putType(d.ns + "QFile::Permissions")
                    d.putNumChild(10)
hjk's avatar
hjk committed
439
                    if d.isExpanded():
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
                        with Children(d, 10):
                            perms = perms['i']
                            d.putBoolItem("ReadOwner",  perms & 0x4000)
                            d.putBoolItem("WriteOwner", perms & 0x2000)
                            d.putBoolItem("ExeOwner",   perms & 0x1000)
                            d.putBoolItem("ReadUser",   perms & 0x0400)
                            d.putBoolItem("WriteUser",  perms & 0x0200)
                            d.putBoolItem("ExeUser",    perms & 0x0100)
                            d.putBoolItem("ReadGroup",  perms & 0x0040)
                            d.putBoolItem("WriteGroup", perms & 0x0020)
                            d.putBoolItem("ExeGroup",   perms & 0x0010)
                            d.putBoolItem("ReadOther",  perms & 0x0004)
                            d.putBoolItem("WriteOther", perms & 0x0002)
                            d.putBoolItem("ExeOther",   perms & 0x0001)

            #QDir absoluteDir () const
            #QDir dir () const
hjk's avatar
hjk committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
            d.putCallItem("caching", value, "caching")
            d.putCallItem("exists", value, "exists")
            d.putCallItem("isAbsolute", value, "isAbsolute")
            d.putCallItem("isDir", value, "isDir")
            d.putCallItem("isExecutable", value, "isExecutable")
            d.putCallItem("isFile", value, "isFile")
            d.putCallItem("isHidden", value, "isHidden")
            d.putCallItem("isReadable", value, "isReadable")
            d.putCallItem("isRelative", value, "isRelative")
            d.putCallItem("isRoot", value, "isRoot")
            d.putCallItem("isSymLink", value, "isSymLink")
            d.putCallItem("isWritable", value, "isWritable")
            d.putCallItem("created", value, "created")
            d.putCallItem("lastModified", value, "lastModified")
            d.putCallItem("lastRead", value, "lastRead")


def qdump__QFixed(d, value):
    v = int(value["val"])
hjk's avatar
hjk committed
476
477
478
479
    d.putValue("%s/64 = %s" % (v, v/64.0))
    d.putNumChild(0)


hjk's avatar
hjk committed
480
def qdump__QFiniteStack(d, value):
hjk's avatar
hjk committed
481
482
    alloc = int(value["_alloc"])
    size = int(value["_size"])
hjk's avatar
hjk committed
483
484
485
486
    check(0 <= size and size <= alloc and alloc <= 1000 * 1000 * 1000)
    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
487
        innerType = d.templateArgument(value.type, 0)
hjk's avatar
hjk committed
488
489
        d.putArrayData(innerType, value["_array"], size)

490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# Stock gdb 7.2 seems to have a problem with types here:
#
#  echo -e "namespace N { struct S { enum E { zero, one, two }; }; }\n"\
#      "int main() { N::S::E x = N::S::one;\n return x; }" >> main.cpp
#  g++ -g main.cpp
#  gdb-7.2 -ex 'file a.out' -ex 'b main' -ex 'run' -ex 'step' \
#     -ex 'ptype N::S::E' -ex 'python print gdb.lookup_type("N::S::E")' -ex 'q'
#  gdb-7.1 -ex 'file a.out' -ex 'b main' -ex 'run' -ex 'step' \
#     -ex 'ptype N::S::E' -ex 'python print gdb.lookup_type("N::S::E")' -ex 'q'
#  gdb-cvs -ex 'file a.out' -ex 'b main' -ex 'run' -ex 'step' \
#     -ex 'ptype N::S::E' -ex 'python print gdb.lookup_type("N::S::E")' -ex 'q'
#
# gives as of 2010-11-02
#
#  type = enum N::S::E {N::S::zero, N::S::one, N::S::two} \n
hjk's avatar
hjk committed
505
506
#    Traceback (most recent call last): File "<string>", line 1,
#      in <module> RuntimeError: No type named N::S::E.
507
508
509
510
511
#  type = enum N::S::E {N::S::zero, N::S::one, N::S::two} \n  N::S::E
#  type = enum N::S::E {N::S::zero, N::S::one, N::S::two} \n  N::S::E
#
# i.e. there's something broken in stock 7.2 that is was ok in 7.1 and is ok later.

hjk's avatar
hjk committed
512
513
def qdump__QFlags(d, value):
    i = value["i"]
514
    try:
515
        enumType = d.templateArgument(value.type.unqualified(), 0)
516
517
518
        d.putValue("%s (%s)" % (i.cast(enumType), i))
    except:
        d.putValue("%s" % i)
hjk's avatar
hjk committed
519
520
521
    d.putNumChild(0)


522
523
524
def qform__QHash():
    return mapForms()

hjk's avatar
hjk committed
525
def qdump__QHash(d, value):
hjk's avatar
hjk committed
526
527

    def hashDataFirstNode(value):
hjk's avatar
hjk committed
528
529
530
        val = value.cast(hashDataType)
        bucket = val["buckets"]
        e = val.cast(hashNodeType)
hjk's avatar
hjk committed
531
        for n in xrange(int(val["numBuckets"]) - 1, -1, -1):
hjk's avatar
hjk committed
532
533
534
535
536
537
538
539
540
541
542
543
544
            n = n - 1
            if n < 0:
                break
            if bucket.dereference() != e:
                return bucket.dereference()
            bucket = bucket + 1
        return e;

    def hashDataNextNode(node):
        next = node["next"]
        if next["next"]:
            return next
        d = node.cast(hashDataType.pointer()).dereference()
hjk's avatar
hjk committed
545
546
        numBuckets = int(d["numBuckets"])
        start = (int(node["h"]) % numBuckets) + 1
hjk's avatar
hjk committed
547
        bucket = d["buckets"] + start
548
        for n in xrange(numBuckets - start):
hjk's avatar
hjk committed
549
550
551
552
553
            if bucket.dereference() != next:
                return bucket.dereference()
            bucket += 1
        return node

554
555
    keyType = d.templateArgument(value.type, 0)
    valueType = d.templateArgument(value.type, 1)
hjk's avatar
hjk committed
556

hjk's avatar
hjk committed
557
558
    d_ptr = value["d"]
    e_ptr = value["e"]
hjk's avatar
hjk committed
559
    size = int(d_ptr["size"])
hjk's avatar
hjk committed
560
561
562
563

    hashDataType = d_ptr.type
    hashNodeType = e_ptr.type

564
    check(0 <= size and size <= 100 * 1000 * 1000)
565
    checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
566

567
568
    d.putItemCount(size)
    d.putNumChild(size)
hjk's avatar
hjk committed
569
    if d.isExpanded():
hjk's avatar
hjk committed
570
        isCompact = d.isMapCompact(keyType, valueType)
hjk's avatar
hjk committed
571
        node = hashDataFirstNode(value)
hjk's avatar
hjk committed
572
        innerType = e_ptr.dereference().type
573
        childType = innerType
574
        if isCompact:
575
            childType = valueType
hjk's avatar
hjk committed
576
        with Children(d, size, maxNumChild=1000, childType=childType):
577
578
            for i in d.childRange():
                it = node.dereference().cast(innerType)
hjk's avatar
hjk committed
579
                with SubItem(d, i):
580
581
582
                    if isCompact:
                        d.putMapName(it["key"])
                        d.putItem(it["value"])
583
584
                        d.putType(valueType)
                    else:
hjk's avatar
hjk committed
585
                        d.putItem(it)
586
                node = hashDataNextNode(node)
hjk's avatar
hjk committed
587
588


hjk's avatar
hjk committed
589
def qdump__QHashNode(d, value):
590
591
    keyType = d.templateArgument(value.type, 0)
    valueType = d.templateArgument(value.type, 1)
hjk's avatar
hjk committed
592
593
    key = value["key"]
    val = value["value"]
hjk's avatar
hjk committed
594

595
596
597
598
    #if isSimpleType(keyType) and isSimpleType(valueType):
    #    d.putName(key)
    #    d.putValue(val)
    #else:
599
    d.putEmptyValue()
hjk's avatar
hjk committed
600

601
    d.putNumChild(2)
hjk's avatar
hjk committed
602
    if d.isExpanded():
603
        with Children(d):
hjk's avatar
hjk committed
604
605
            d.putSubItem("key", key)
            d.putSubItem("value", val)
hjk's avatar
hjk committed
606
607


608
609
def qHashIteratorHelper(d, value):
    typeName = str(value.type)
610
    hashType = d.lookupType(typeName[0:typeName.rfind("::")])
611
612
    keyType = d.templateArgument(hashType, 0)
    valueType = d.templateArgument(hashType, 1)
613
    d.putNumChild(1)
614
    d.putEmptyValue()
615
616
617
    if d.isExpanded():
        with Children(d):
            typeName = "%sQHash<%s,%s>::Node" % (d.ns, keyType, valueType)
618
            node = value["i"].cast(d.lookupType(typeName).pointer())
619
620
621
622
623
624
625
626
627
628
            d.putSubItem("key", node["key"])
            d.putSubItem("value", node["value"])

def qdump__QHash__const_iterator(d, value):
    qHashIteratorHelper(d, value)

def qdump__QHash__iterator(d, value):
    qHashIteratorHelper(d, value)


hjk's avatar
hjk committed
629
630
def qdump__QHostAddress(d, value):
    data = value["d"]["d"].dereference()
hjk's avatar
hjk committed
631
632
633
    if int(data["ipString"]["d"]["size"]):
        d.putStringValue(data["ipString"])
    else:
634
635
636
637
638
639
        a = long(data["a"])
        a, n4 = divmod(a, 256)
        a, n3 = divmod(a, 256)
        a, n2 = divmod(a, 256)
        a, n1 = divmod(a, 256)
        d.putValue("%d.%d.%d.%d" % (n1, n2, n3, n4));
hjk's avatar
hjk committed
640
    d.putNumChild(1)
hjk's avatar
hjk committed
641
    if d.isExpanded():
hjk's avatar
hjk committed
642
        with Children(d):
hjk's avatar
hjk committed
643
           d.putFields(data)
hjk's avatar
hjk committed
644

hjk's avatar
hjk committed
645
def qdump__QList(d, value):
646
647
648
649
650
    dptr = childAt(value, 0)["d"]
    private = dptr.dereference()
    begin = int(private["begin"])
    end = int(private["end"])
    array = private["array"]
hjk's avatar
hjk committed
651
    check(begin >= 0 and end >= 0 and end <= 1000 * 1000 * 1000)
652
653
    size = end - begin
    check(size >= 0)
654
    checkRef(private["ref"])
hjk's avatar
hjk committed
655

656
    innerType = d.templateArgument(value.type, 0)
hjk's avatar
hjk committed
657

658
659
    d.putItemCount(size)
    d.putNumChild(size)
hjk's avatar
hjk committed
660
    if d.isExpanded():
hjk's avatar
hjk committed
661
662
663
664
665
666
        innerSize = innerType.sizeof
        # The exact condition here is:
        #  QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic
        # but this data is available neither in the compiled binary nor
        # in the frontend.
        # So as first approximation only do the 'isLarge' check:
667
668
669
670
671
672
673
674
675
676
677
678
        stepSize = dptr.type.sizeof
        isInternal = innerSize <= stepSize and d.isMovableType(innerType)
        addr = d.addressOf(array) + begin * stepSize
        if isInternal:
            if innerSize == stepSize:
                p = createPointerValue(value, addr, innerType)
                d.putArrayData(innerType, p, size)
            else:
                with Children(d, size, childType=innerType):
                    for i in d.childRange():
                        p = createPointerValue(value, addr + i * stepSize, innerType)
                        d.putSubItem(i, p.dereference())
679
        else:
680
681
682
683
684
685
            p = createPointerValue(value, addr, innerType.pointer())
            # about 0.5s / 1000 items
            with Children(d, size, maxNumChild=2000, childType=innerType):
                for i in d.childRange():
                    d.putSubItem(i, p.dereference().dereference())
                    p += 1
hjk's avatar
hjk committed
686

hjk's avatar
hjk committed
687
688
def qform__QImage():
    return "Normal,Displayed"
hjk's avatar
hjk committed
689

hjk's avatar
hjk committed
690
def qdump__QImage(d, value):
691
    try:
hjk's avatar
hjk committed
692
        painters = int(value["painters"])
693
    except:
hjk's avatar
hjk committed
694
        d.putPlainChildren(value)
695
        return
hjk's avatar
hjk committed
696
    check(0 <= painters and painters < 1000)
hjk's avatar
hjk committed
697
    d_ptr = value["d"]
hjk's avatar
hjk committed
698
    if isNull(d_ptr):
699
        d.putValue("(null)")
hjk's avatar
hjk committed
700
    else:
701
        checkSimpleRef(d_ptr["ref"])
702
        d.putValue("(%dx%d)" % (d_ptr["width"], d_ptr["height"]))
703
704
    bits = d_ptr["data"]
    nbytes = d_ptr["nbytes"]
705
    d.putNumChild(0)
hjk's avatar
hjk committed
706
    #d.putNumChild(1)
hjk's avatar
hjk committed
707
    if d.isExpanded():
708
        with Children(d):
hjk's avatar
hjk committed
709
            with SubItem(d, "data"):
710
                d.putNoType()
711
712
                d.putNumChild(0)
                d.putValue("size: %s bytes" % nbytes);
hjk's avatar
hjk committed
713
    format = d.currentItemFormat()
714
    if format == 1:
715
        d.putDisplay(StopDisplay)
716
    elif format == 2:
hjk's avatar
hjk committed
717
718
719
        if False:
            # Take four bytes at a time, this is critical for performance.
            # In fact, even four at a time is too slow beyond 100x100 or so.
720
            d.putField("editformat", DisplayImageData)
721
            d.put('%s="' % name)
hjk's avatar
hjk committed
722
723
724
            d.put("%08x" % int(d_ptr["width"]))
            d.put("%08x" % int(d_ptr["height"]))
            d.put("%08x" % int(d_ptr["format"]))
725
            p = bits.cast(d.intType().pointer())
hjk's avatar
hjk committed
726
727
728
            for i in xrange(nbytes / 4):
                d.put("%08x" % int(p.dereference()))
                p += 1
729
            d.put('",')
hjk's avatar
hjk committed
730
731
732
        else:
            # Write to an external file. Much faster ;-(
            file = tempfile.mkstemp(prefix="gdbpy_")
733
            filename = file[1].replace("\\", "\\\\")
734
            p = bits.cast(d.charType().pointer())
hjk's avatar
hjk committed
735
736
            gdb.execute("dump binary memory %s %s %s" %
                (filename, cleanAddress(p), cleanAddress(p + nbytes)))
737
            d.putDisplay(DisplayImageFile, " %d %d %d %s"
738
                % (d_ptr["width"], d_ptr["height"], d_ptr["format"], filename))
hjk's avatar
hjk committed
739
740


hjk's avatar
hjk committed
741
742
743
def qdump__QLinkedList(d, value):
    d_ptr = value["d"]
    e_ptr = value["e"]
hjk's avatar
hjk committed
744
    n = int(d_ptr["size"])
hjk's avatar
hjk committed
745
    check(0 <= n and n <= 100*1000*1000)
746
    checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
747
    d.putItemCount(n)
748
    d.putNumChild(n)
hjk's avatar
hjk committed
749
    if d.isExpanded():
750
        innerType = d.templateArgument(value.type, 0)
hjk's avatar
hjk committed
751
        with Children(d, n, maxNumChild=1000, childType=innerType):
752
753
            p = e_ptr["n"]
            for i in d.childRange():
hjk's avatar
hjk committed
754
                d.putSubItem(i, p["t"])
755
                p = p["n"]
hjk's avatar
hjk committed
756

757
qqLocalesCount = None
hjk's avatar
hjk committed
758

hjk's avatar
hjk committed
759
def qdump__QLocale(d, value):
760
761
762
763
    # Check for uninitialized 'index' variable. Retrieve size of
    # QLocale data array from variable in qlocale.cpp.
    # Default is 368 in Qt 4.8, 438 in Qt 5.0.1, the last one
    # being 'System'.
764
765
766
    global qqLocalesCount
    if qqLocalesCount is None:
        try:
767
            qqLocalesCount = int(value(d.ns + 'locale_data_size'))
768
        except:
769
770
771
772
773
774
775
            qqLocalesCount = 438
    try:
        index = int(value["p"]["index"])
    except:
        index = int(value["d"]["d"]["m_index"])
    check(index >= 0)
    check(index <= qqLocalesCount)
hjk's avatar
hjk committed
776
    d.putStringValue(call(value, "name"))
777
778
779
    d.putNumChild(0)
    return
    # FIXME: Poke back for variants.
hjk's avatar
hjk committed
780
    if d.isExpanded():
781
        with Children(d, childType=d.lookupType(d.ns + "QChar"), childNumChild=0):
hjk's avatar
hjk committed
782
783
784
785
786
            d.putCallItem("country", value, "country")
            d.putCallItem("language", value, "language")
            d.putCallItem("measurementSystem", value, "measurementSystem")
            d.putCallItem("numberOptions", value, "numberOptions")
            d.putCallItem("timeFormat_(short)", value,
hjk's avatar
hjk committed
787
                "timeFormat", d.ns + "QLocale::ShortFormat")
hjk's avatar
hjk committed
788
            d.putCallItem("timeFormat_(long)", value,
hjk's avatar
hjk committed
789
                "timeFormat", d.ns + "QLocale::LongFormat")
hjk's avatar
hjk committed
790
791
792
793
794
795
            d.putCallItem("decimalPoint", value, "decimalPoint")
            d.putCallItem("exponential", value, "exponential")
            d.putCallItem("percent", value, "percent")
            d.putCallItem("zeroDigit", value, "zeroDigit")
            d.putCallItem("groupSeparator", value, "groupSeparator")
            d.putCallItem("negativeSign", value, "negativeSign")
hjk's avatar
hjk committed
796
797


hjk's avatar
hjk committed
798
def qdump__QMapNode(d, value):
799
    d.putEmptyValue()
800
    d.putNumChild(2)
hjk's avatar
hjk committed
801
802
803
804
805
806
    if d.isExpanded():
        with Children(d):
            d.putSubItem("key", value["key"])
            d.putSubItem("value", value["value"])


807
def qdumpHelper__Qt4_QMap(d, value, forceLong):
hjk's avatar
hjk committed
808
809
    d_ptr = value["d"].dereference()
    e_ptr = value["e"].dereference()
hjk's avatar
hjk committed
810
    n = int(d_ptr["size"])
hjk's avatar
hjk committed
811
    check(0 <= n and n <= 100*1000*1000)
812
    checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
813
814

    d.putItemCount(n)
815
    d.putNumChild(n)
hjk's avatar
hjk committed
816
    if d.isExpanded():
817
818
        if n > 10000:
            n = 10000
hjk's avatar
hjk committed
819

820
821
        keyType = d.templateArgument(value.type, 0)
        valueType = d.templateArgument(value.type, 1)
hjk's avatar
hjk committed
822
        isCompact = d.isMapCompact(keyType, valueType)
hjk's avatar
hjk committed
823
824
825
826
827
828

        it = e_ptr["forward"].dereference()

        # QMapPayloadNode is QMapNode except for the 'forward' member, so
        # its size is most likely the offset of the 'forward' member therein.
        # Or possibly 2 * sizeof(void *)
829
830
        nodeType = d.lookupType(d.ns + "QMapNode<%s, %s>" % (keyType, valueType))
        payloadSize = nodeType.sizeof - 2 * d.voidPtrSize
hjk's avatar
hjk committed
831

832
        if isCompact:
833
834
835
            innerType = valueType
        else:
            innerType = nodeType
hjk's avatar
hjk committed
836

hjk's avatar
hjk committed
837
        with Children(d, n, childType=innerType):
838
839
            for i in xrange(n):
                itd = it.dereference()
840
                base = it.cast(d.charPtr()) - payloadSize
841
                node = base.cast(nodeType.pointer()).dereference()
hjk's avatar
hjk committed
842
                with SubItem(d, i):
843
                    d.putField("iname", d.currentIName)
844
                    if isCompact:
845
                        #d.putType(valueType)
hjk's avatar
hjk committed
846
                        if forceLong:
847
                            d.putName("[%s] %s" % (i, node["key"]))
hjk's avatar
hjk committed
848
                        else:
849
850
                            d.putMapName(node["key"])
                        d.putItem(node["value"])
851
                    else:
hjk's avatar
hjk committed
852
                        d.putItem(node)
853
                it = it.dereference()["forward"].dereference()
hjk's avatar
hjk committed
854
855


856
857
def qdumpHelper__Qt5_QMap(d, value, forceLong):
    d_ptr = value["d"].dereference()
hjk's avatar
hjk committed
858
    n = int(d_ptr["size"])
859
860
861
862
863
864
865
866
867
    check(0 <= n and n <= 100*1000*1000)
    checkRef(d_ptr["ref"])

    d.putItemCount(n)
    d.putNumChild(n)
    if d.isExpanded():
        if n > 10000:
            n = 10000

868
869
        keyType = d.templateArgument(value.type, 0)
        valueType = d.templateArgument(value.type, 1)
hjk's avatar
hjk committed
870
        isCompact = d.isMapCompact(keyType, valueType)
871
        nodeType = d.lookupType(d.ns + "QMapNode<%s, %s>" % (keyType, valueType))
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
        if isCompact:
            innerType = valueType
        else:
            innerType = nodeType

        with Children(d, n, childType=innerType):
            toDo = []
            i = -1
            node = d_ptr["header"]
            left = node["left"]
            if not isNull(left):
                toDo.append(left.dereference())
            right = node["right"]
            if not isNull(right):
                toDo.append(right.dereference())

            while len(toDo):
                node = toDo[0].cast(nodeType)
                toDo = toDo[1:]
                left = node["left"]
                if not isNull(left):
                    toDo.append(left.dereference())
                right = node["right"]
                if not isNull(right):
                    toDo.append(right.dereference())
                i += 1

                with SubItem(d, i):
900
                    d.putField("iname", d.currentIName)
901
902
903
904
905
906
907
908
909
910
911
                    if isCompact:
                        if forceLong:
                            d.putName("[%s] %s" % (i, node["key"]))
                        else:
                            d.putMapName(node["key"])
                        d.putItem(node["value"])
                    else:
                        qdump__QMapNode(d, node)


def qdumpHelper__QMap(d, value, forceLong):
hjk's avatar
hjk committed
912
    if fieldAt(value["d"].dereference().type, 0).name == "backward":
913
914
915
916
        qdumpHelper__Qt4_QMap(d, value, forceLong)
    else:
        qdumpHelper__Qt5_QMap(d, value, forceLong)

917
918
919
def qform__QMap():
    return mapForms()

hjk's avatar
hjk committed
920
921
def qdump__QMap(d, value):
    qdumpHelper__QMap(d, value, False)
hjk's avatar
hjk committed
922

923
924
925
def qform__QMultiMap():
    return mapForms()

hjk's avatar
hjk committed
926
927
def qdump__QMultiMap(d, value):
    qdumpHelper__QMap(d, value, True)
hjk's avatar
hjk committed
928
929
930
931
932
933
934
935
936
937
938
939
940


def extractCString(table, offset):
    result = ""
    while True:
        d = table[offset]
        if d == 0:
            break
        result += "%c" % d
        offset += 1
    return result


hjk's avatar
hjk committed
941
942
def qdump__QObject(d, value):
    #warn("OBJECT: %s " % value)
hjk's avatar
hjk committed
943
944
    d.tryPutObjectNameValue(value)

945
    try:
hjk's avatar
hjk committed
946
        privateTypeName = d.ns + "QObjectPrivate"
947
        privateType = d.lookupType(privateTypeName)
hjk's avatar
hjk committed
948
        staticMetaObject = value["staticMetaObject"]
949
    except:
hjk's avatar
hjk committed
950
        d.putPlainChildren(value)
951
        return
hjk's avatar
hjk committed
952
    #warn("SMO: %s " % staticMetaObject)
953
954
955
956
957
958
959
    #warn("SMO DATA: %s " % staticMetaObject["d"]["stringdata"])
    superData = staticMetaObject["d"]["superdata"]
    #warn("SUPERDATA: %s" % superData)
    #while not isNull(superData):
    #    superData = superData.dereference()["d"]["superdata"]
    #    warn("SUPERDATA: %s" % superData)

960
961
    if privateType is None:
        d.putNumChild(4)
hjk's avatar
hjk committed
962
        #d.putValue(cleanAddress(value.address))
963
        d.putPlainChildren(value)
hjk's avatar
hjk committed
964
        if d.isExpanded():
965
            with Children(d):
hjk's avatar
hjk committed
966
                d.putFields(value)
967
        return
hjk's avatar
hjk committed
968
    #warn("OBJECTNAME: %s " % objectName)
969
    #warn("D_PTR: %s " % d_ptr)
hjk's avatar
hjk committed
970
    mo = d_ptr["metaObject"]
971
972
973
    if not isAccessible(mo):
        d.putInaccessible()
        return
hjk's avatar
hjk committed
974
975
976
977
978
979
    if isNull(mo):
        mo = staticMetaObject
    #warn("MO: %s " % mo)
    #warn("MO.D: %s " % mo["d"])
    metaData = mo["d"]["data"]
    metaStringData = mo["d"]["stringdata"]
980
981
982
983
    # This is char * in Qt 4 and ByteArrayData * in Qt 5.
    # Force it to the char * data in the Qt 5 case.
    try:
        offset = metaStringData["offset"]
984
        metaStringData = metaStringData.cast(d.charPtrType()) + int(offset)
985
986
987
    except:
        pass

988
    #extradata = mo["d"]["extradata"]   # Capitalization!
hjk's avatar
hjk committed
989
990
    #warn("METADATA: %s " % metaData)
    #warn("STRINGDATA: %s " % metaStringData)
hjk's avatar
hjk committed
991
992
    #warn("TYPE: %s " % value.type)
    #warn("INAME: %s " % d.currentIName())
993
    #d.putEmptyValue()
hjk's avatar
hjk committed
994
    #QSignalMapper::staticMetaObject
995
    #checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
996
    d.putNumChild(4)
hjk's avatar
hjk committed
997
    if d.isExpanded():
998
      with Children(d):
999

hjk's avatar
hjk committed
1000
        # Local data.
1001
1002
        if privateTypeName != d.ns + "QObjectPrivate":
            if not privateType is None:
hjk's avatar
hjk committed
1003
              with SubItem(d, "data"):
1004
                d.putEmptyValue()
1005
                d.putNoType()
1006
                d.putNumChild(1)
hjk's avatar
hjk committed
1007
                if d.isExpanded():
1008
                    with Children(d):
hjk's avatar
hjk committed
1009
                        d.putFields(d_ptr, False)
1010
1011


hjk's avatar
hjk committed
1012
        d.putFields(value)
1013
        # Parent and children.
hjk's avatar
hjk committed
1014
1015
1016
        if stripClassTag(str(value.type)) == d.ns + "QObject":
            d.putSubItem("parent", d_ptr["parent"])
            d.putSubItem("children", d_ptr["children"])
hjk's avatar
hjk committed
1017

1018
        # Properties.
hjk's avatar
hjk committed
1019
        with SubItem(d, "properties"):
1020
            # Prolog
1021
1022
            extraData = d_ptr["extraData"]   # Capitalization!
            if isNull(extraData):
1023
                dynamicPropertyCount = 0
1024
            else:
1025
                extraDataType = d.lookupType(
1026
1027
1028
1029
1030
1031
1032
1033
1034
                    d.ns + "QObjectPrivate::ExtraData").pointer()
                extraData = extraData.cast(extraDataType)
                ed = extraData.dereference()
                names = ed["propertyNames"]
                values = ed["propertyValues"]
                #userData = ed["userData"]
                namesBegin = names["d"]["begin"]
                namesEnd = names["d"]["end"]
                namesArray = names["d"]["array"]
1035
1036
                dynamicPropertyCount = namesEnd - namesBegin

hjk's avatar
hjk committed
1037
            #staticPropertyCount = call(mo, "propertyCount")