qttypes.py 96.9 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
22
23
24
25
26
27
28
29
30
31
32
33
34
# Compatibility with earlier versions
Dumper.encodeByteArray = \
    lambda d, value, limit = None: qEncodeByteArray(d, value, limit)
Dumper.byteArrayData = \
    lambda d, value: qByteArrayData(d, value)
Dumper.putByteArrayValue = \
    lambda d, value: qPutByteArrayValue(d, value)
Dumper.encodeString = \
    lambda d, value, limit = None: qString(d, value, limit)
Dumper.stringData = \
    lambda d, value: qStringData(d, value)
Dumper.putStringValue = \
    lambda d, value: qPutStringValue(d, value)

35
36
37
def mapForms():
    return "Normal,Compact"

38
39
40
41
42
def arrayForms():
    if hasPlot():
        return "Normal,Plot"
    return "Normal"

43
44
45
46
47
48
def mapCompact(format, keyType, valueType):
    if format == 2:
        return True # Compact.
    return isSimpleType(keyType) and isSimpleType(valueType)


hjk's avatar
hjk committed
49
50
def qdump__QAtomicInt(d, value):
    d.putValue(value["_q_value"])
hjk's avatar
hjk committed
51
52
53
    d.putNumChild(0)


hjk's avatar
hjk committed
54
55
def qdump__QBasicAtomicInt(d, value):
    d.putValue(value["_q_value"])
56
57
58
    d.putNumChild(0)


hjk's avatar
hjk committed
59
60
61
def qdump__QBasicAtomicPointer(d, value):
    d.putType(value.type)
    p = cleanAddress(value["_q_value"])
62
    d.putValue(p)
hjk's avatar
hjk committed
63
    d.putPointerValue(value.address)
64
    d.putNumChild(p)
hjk's avatar
hjk committed
65
    if d.isExpanded():
66
        with Children(d):
hjk's avatar
hjk committed
67
           d.putItem(value["_q_value"])
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
def qByteArrayData(d, value):
    private = value['d']
    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.
        offset = private['offset']
        data = private.cast(d.charPtrType()) + offset
        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)
110

111
112
113
def qform__QByteArray():
    return "Inline,As Latin1 in Separate Window,As UTF-8 in Separate Window"

hjk's avatar
hjk committed
114
115
def qdump__QByteArray(d, value):
    d.putByteArrayValue(value)
116
    data, size, alloc = qByteArrayData(d, value)
117
    d.putNumChild(size)
118
119
120
121
    format = d.currentItemFormat()
    if format == 1:
        d.putDisplay(StopDisplay)
    elif format == 2:
122
        d.putField("editformat", DisplayLatin1String)
123
        d.putField("editvalue", qEncodeByteArray(d, value, None))
124
    elif format == 3:
125
        d.putField("editformat", DisplayUtf8String)
126
        d.putField("editvalue", qEncodeByteArray(d, value, None))
hjk's avatar
hjk committed
127
    if d.isExpanded():
128
        d.putArrayData(d.charType(), data, size)
hjk's avatar
hjk committed
129
130


131
132
133
134
135
136
137
138
139
140
141
142
143
# 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
144
145
def qdump__QChar(d, value):
    ucs = int(value["ucs"])
146
    d.putValue("'%c' (%d)" % (printableChar(ucs), ucs))
hjk's avatar
hjk committed
147
148
149
    d.putNumChild(0)


hjk's avatar
hjk committed
150
def qform__QAbstractItemModel():
151
    return "Normal,Enhanced"
152

hjk's avatar
hjk committed
153
def qdump__QAbstractItemModel(d, value):
154
155
156
157
158
    format = d.currentItemFormat()
    if format == 1:
        d.putPlainChildren(value)
        return
    #format == 2:
159
    # Create a default-constructed QModelIndex on the stack.
160
    try:
161
        ri = makeValue(d.ns + "QModelIndex", "-1, -1, 0, 0")
hjk's avatar
hjk committed
162
        this_ = makeExpression(value)
163
        ri_ = makeExpression(ri)
164
165
166
        rowCount = int(parseAndEvaluate("%s.rowCount(%s)" % (this_, ri_)))
        columnCount = int(parseAndEvaluate("%s.columnCount(%s)" % (this_, ri_)))
    except:
hjk's avatar
hjk committed
167
        d.putPlainChildren(value)
168
        return
169
170
    d.putValue("%d x %d" % (rowCount, columnCount))
    d.putNumChild(rowCount * columnCount)
hjk's avatar
hjk committed
171
172
    if d.isExpanded():
        with Children(d, numChild=rowCount * columnCount, childType=ri.type):
173
174
175
            i = 0
            for row in xrange(rowCount):
                for column in xrange(columnCount):
hjk's avatar
hjk committed
176
                    with SubItem(d, i):
177
178
179
180
181
182
                        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
183
                        d.putItem(mi)
184
185
186
187
188
189
190
191
                        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)")

192
193
194
def qform__QModelIndex():
    return "Normal,Enhanced"

hjk's avatar
hjk committed
195
def qdump__QModelIndex(d, value):
196
197
198
199
    format = d.currentItemFormat()
    if format == 1:
        d.putPlainChildren(value)
        return
hjk's avatar
hjk committed
200
201
    r = value["r"]
    c = value["c"]
202
203
204
205
    try:
        p = value["p"]
    except:
        p = value["i"]
hjk's avatar
hjk committed
206
    m = value["m"]
207
208
209
210
211
    if isNull(m) or r < 0 or c < 0:
        d.putValue("(invalid)")
        d.putPlainChildren(value)
        return

212
213
    mm = m.dereference()
    mm = mm.cast(mm.type.unqualified())
214
    try:
215
216
217
        mi = makeValue(d.ns + "QModelIndex", "%s,%s,%s,%s" % (r, c, p, m))
        mm_ = makeExpression(mm)
        mi_ = makeExpression(mi)
218
219
220
        rowCount = int(parseAndEvaluate("%s.rowCount(%s)" % (mm_, mi_)))
        columnCount = int(parseAndEvaluate("%s.columnCount(%s)" % (mm_, mi_)))
    except:
221
        d.putEmptyValue()
hjk's avatar
hjk committed
222
        d.putPlainChildren(value)
223
        return
224
225
226

    try:
        # Access DisplayRole as value
hjk's avatar
hjk committed
227
228
        val = parseAndEvaluate("%s.data(%s, 0)" % (mm_, mi_))
        v = val["d"]["data"]["ptr"]
229
230
231
232
        d.putStringValue(makeValue(d.ns + 'QString', v))
    except:
        d.putValue("(invalid)")

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
    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)
250
251
    #gdb.execute("call free($mi)")

hjk's avatar
hjk committed
252

hjk's avatar
hjk committed
253
def qdump__QDate(d, value):
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    jd = value["jd"]
    if int(jd):
        d.putValue(jd, JulianDate)
        d.putNumChild(1)
        if d.isExpanded():
            qt = d.ns + "Qt::"
            # 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
270
271


hjk's avatar
hjk committed
272
def qdump__QTime(d, value):
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    mds = value["mds"]
    if int(mds) >= 0:
        d.putValue(value["mds"], MillisecondsSinceMidnight)
        d.putNumChild(1)
        if d.isExpanded():
            qt = d.ns + "Qt::"
            # 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)
290
291


hjk's avatar
hjk committed
292
def qdump__QDateTime(d, value):
293
294
    try:
        # Fails without debug info.
295
        p = value["d"]["d"].dereference()
296
    except:
hjk's avatar
hjk committed
297
        d.putPlainChildren(value)
298
        return
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    mds = p["time"]["mds"]
    if int(mds) >= 0:
        d.putValue("%s/%s" % (p["date"]["jd"], mds),
            JulianDateAndMillisecondsSinceMidnight)
        d.putNumChild(1)
        if d.isExpanded():
            # FIXME: This improperly uses complex return values.
            with Children(d):
                qt = d.ns + "Qt::"
                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
318
319
320


def qdump__QDir(d, value):
hjk's avatar
hjk committed
321
    d.putNumChild(1)
322
323
324
325
326
327
328
    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
329
330
    if d.isExpanded():
        with Children(d):
331
            qdir = d.ns + "QDir::"
hjk's avatar
hjk committed
332
333
            d.putCallItem("absolutePath", value, "absolutePath")
            d.putCallItem("canonicalPath", value, "canonicalPath")
hjk's avatar
hjk committed
334
335
            d.putSubItem("entryList", data["files"])
            d.putSubItem("entryInfoList", data["fileInfos"])
hjk's avatar
hjk committed
336
337


hjk's avatar
hjk committed
338
def qdump__QFile(d, value):
hjk's avatar
hjk committed
339
    try:
340
        ptype = d.lookupType(d.ns + "QFilePrivate").pointer()
hjk's avatar
hjk committed
341
342
343
344
345
        d_ptr = value["d_ptr"]["d"]
        d.putStringValue(d_ptr.cast(ptype).dereference()["fileName"])
    except:
        d.putPlainChildren(value)
        return
346
    d.putNumChild(1)
hjk's avatar
hjk committed
347
348
    if d.isExpanded():
        with Children(d):
349
350
            base = value.type.fields()[0].type
            d.putSubItem("[%s]" % str(base), value.cast(base), False)
hjk's avatar
hjk committed
351
            d.putCallItem("exists", value, "exists")
hjk's avatar
hjk committed
352
353


hjk's avatar
hjk committed
354
def qdump__QFileInfo(d, value):
355
    try:
hjk's avatar
hjk committed
356
        d.putStringValue(value["d_ptr"]["d"].dereference()["fileNames"][3])
357
    except:
hjk's avatar
hjk committed
358
        d.putPlainChildren(value)
359
        return
hjk's avatar
hjk committed
360
    d.putNumChild(1)
hjk's avatar
hjk committed
361
    if d.isExpanded():
362
        with Children(d, childType=d.lookupType(d.ns + "QString")):
hjk's avatar
hjk committed
363
364
365
366
367
368
369
            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")
370
371
            if False:
                #ifdef Q_OS_MACX
hjk's avatar
hjk committed
372
373
374
375
                d.putCallItem("isBundle", value, "isBundle")
                d.putCallItem("bundleName", value, "bundleName")
            d.putCallItem("fileName", value, "fileName")
            d.putCallItem("filePath", value, "filePath")
376
            # Crashes gdb (archer-tromey-python, at dad6b53fe)
hjk's avatar
hjk committed
377
378
379
            #d.putCallItem("group", value, "group")
            #d.putCallItem("owner", value, "owner")
            d.putCallItem("path", value, "path")
380

hjk's avatar
hjk committed
381
382
            d.putCallItem("groupid", value, "groupId")
            d.putCallItem("ownerid", value, "ownerId")
383
384

            #QFile::Permissions permissions () const
hjk's avatar
hjk committed
385
            perms = call(value, "permissions")
386
387
388
            if perms is None:
                d.putValue("<not available>")
            else:
hjk's avatar
hjk committed
389
                with SubItem(d, "permissions"):
390
                    d.putEmptyValue()
391
392
                    d.putType(d.ns + "QFile::Permissions")
                    d.putNumChild(10)
hjk's avatar
hjk committed
393
                    if d.isExpanded():
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
                        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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
            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
430
431
432
433
    d.putValue("%s/64 = %s" % (v, v/64.0))
    d.putNumChild(0)


hjk's avatar
hjk committed
434
435
436
437
438
439
440
441
442
443
def qdump__QFiniteStack(d, value):
    alloc = value["_alloc"]
    size = value["_size"]
    check(0 <= size and size <= alloc and alloc <= 1000 * 1000 * 1000)
    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
        innerType = templateArgument(value.type, 0)
        d.putArrayData(innerType, value["_array"], size)

444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# 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
459
460
#    Traceback (most recent call last): File "<string>", line 1,
#      in <module> RuntimeError: No type named N::S::E.
461
462
463
464
465
#  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
466
467
def qdump__QFlags(d, value):
    i = value["i"]
468
    try:
hjk's avatar
hjk committed
469
        enumType = templateArgument(value.type.unqualified(), 0)
470
471
472
        d.putValue("%s (%s)" % (i.cast(enumType), i))
    except:
        d.putValue("%s" % i)
hjk's avatar
hjk committed
473
474
475
    d.putNumChild(0)


476
477
478
def qform__QHash():
    return mapForms()

hjk's avatar
hjk committed
479
def qdump__QHash(d, value):
hjk's avatar
hjk committed
480
481

    def hashDataFirstNode(value):
hjk's avatar
hjk committed
482
483
484
485
        val = value.cast(hashDataType)
        bucket = val["buckets"]
        e = val.cast(hashNodeType)
        for n in xrange(val["numBuckets"] - 1, -1, -1):
hjk's avatar
hjk committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
            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()
        numBuckets = d["numBuckets"]
        start = (node["h"] % numBuckets) + 1
        bucket = d["buckets"] + start
502
        for n in xrange(numBuckets - start):
hjk's avatar
hjk committed
503
504
505
506
507
            if bucket.dereference() != next:
                return bucket.dereference()
            bucket += 1
        return node

hjk's avatar
hjk committed
508
509
    keyType = templateArgument(value.type, 0)
    valueType = templateArgument(value.type, 1)
hjk's avatar
hjk committed
510

hjk's avatar
hjk committed
511
512
    d_ptr = value["d"]
    e_ptr = value["e"]
513
    size = d_ptr["size"]
hjk's avatar
hjk committed
514
515
516
517

    hashDataType = d_ptr.type
    hashNodeType = e_ptr.type

518
    check(0 <= size and size <= 100 * 1000 * 1000)
519
    checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
520

521
522
    d.putItemCount(size)
    d.putNumChild(size)
hjk's avatar
hjk committed
523
    if d.isExpanded():
524
        isCompact = mapCompact(d.currentItemFormat(), keyType, valueType)
hjk's avatar
hjk committed
525
        node = hashDataFirstNode(value)
hjk's avatar
hjk committed
526
        innerType = e_ptr.dereference().type
527
        childType = innerType
528
        if isCompact:
529
            childType = valueType
hjk's avatar
hjk committed
530
        with Children(d, size, maxNumChild=1000, childType=childType):
531
532
            for i in d.childRange():
                it = node.dereference().cast(innerType)
hjk's avatar
hjk committed
533
                with SubItem(d, i):
534
535
536
                    if isCompact:
                        d.putMapName(it["key"])
                        d.putItem(it["value"])
537
538
                        d.putType(valueType)
                    else:
hjk's avatar
hjk committed
539
                        d.putItem(it)
540
                node = hashDataNextNode(node)
hjk's avatar
hjk committed
541
542


hjk's avatar
hjk committed
543
544
545
546
547
def qdump__QHashNode(d, value):
    keyType = templateArgument(value.type, 0)
    valueType = templateArgument(value.type, 1)
    key = value["key"]
    val = value["value"]
hjk's avatar
hjk committed
548

549
550
551
552
    #if isSimpleType(keyType) and isSimpleType(valueType):
    #    d.putName(key)
    #    d.putValue(val)
    #else:
553
    d.putEmptyValue()
hjk's avatar
hjk committed
554

555
    d.putNumChild(2)
hjk's avatar
hjk committed
556
    if d.isExpanded():
557
        with Children(d):
hjk's avatar
hjk committed
558
559
            d.putSubItem("key", key)
            d.putSubItem("value", val)
hjk's avatar
hjk committed
560
561


562
563
def qHashIteratorHelper(d, value):
    typeName = str(value.type)
564
    hashType = d.lookupType(typeName[0:typeName.rfind("::")])
565
566
567
    keyType = templateArgument(hashType, 0)
    valueType = templateArgument(hashType, 1)
    d.putNumChild(1)
568
    d.putEmptyValue()
569
570
571
    if d.isExpanded():
        with Children(d):
            typeName = "%sQHash<%s,%s>::Node" % (d.ns, keyType, valueType)
572
            node = value["i"].cast(d.lookupType(typeName).pointer())
573
574
575
576
577
578
579
580
581
582
            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
583
584
def qdump__QHostAddress(d, value):
    data = value["d"]["d"].dereference()
hjk's avatar
hjk committed
585
586
587
    if int(data["ipString"]["d"]["size"]):
        d.putStringValue(data["ipString"])
    else:
588
589
590
591
592
593
        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
594
    d.putNumChild(1)
hjk's avatar
hjk committed
595
    if d.isExpanded():
hjk's avatar
hjk committed
596
        with Children(d):
hjk's avatar
hjk committed
597
           d.putFields(data)
hjk's avatar
hjk committed
598

hjk's avatar
hjk committed
599
def qdump__QList(d, value):
600
601
602
603
    d_ptr = childAt(value, 0)["d"].dereference()
    begin = int(d_ptr["begin"])
    end = int(d_ptr["end"])
    array = addressOf(d_ptr["array"])
hjk's avatar
hjk committed
604
    check(begin >= 0 and end >= 0 and end <= 1000 * 1000 * 1000)
605
606
    size = end - begin
    check(size >= 0)
607
    checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
608
609

    # Additional checks on pointer arrays.
hjk's avatar
hjk committed
610
    innerType = templateArgument(value.type, 0)
611
    innerTypeIsPointer = innerType.code == PointerCode \
hjk's avatar
hjk committed
612
613
        and str(innerType.target().unqualified()) != "char"
    if innerTypeIsPointer:
614
        p = array.cast(innerType.pointer()) + begin
hjk's avatar
hjk committed
615
        checkPointerRange(p, min(size, 100))
hjk's avatar
hjk committed
616

617
618
    d.putItemCount(size)
    d.putNumChild(size)
hjk's avatar
hjk committed
619
    if d.isExpanded():
hjk's avatar
hjk committed
620
621
622
623
624
625
626
        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:
        isInternal = innerSize <= d_ptr.type.sizeof and d.isMovableType(innerType)
627
        innerTypePointer = innerType.pointer()
628
        p = array.cast(d.charPtrType().pointer()) + begin
629
630
631
632
        if innerTypeIsPointer:
            inner = innerType.target()
        else:
            inner = innerType
633
        # about 0.5s / 1000 items
hjk's avatar
hjk committed
634
        with Children(d, size, maxNumChild=2000, childType=inner):
635
636
            for i in d.childRange():
                if isInternal:
hjk's avatar
hjk committed
637
                    d.putSubItem(i, p.cast(innerTypePointer).dereference())
638
                else:
hjk's avatar
hjk committed
639
                    d.putSubItem(i, p.cast(innerTypePointer.pointer()).dereference())
640
                p += 1
hjk's avatar
hjk committed
641

hjk's avatar
hjk committed
642
643
def qform__QImage():
    return "Normal,Displayed"
hjk's avatar
hjk committed
644

hjk's avatar
hjk committed
645
def qdump__QImage(d, value):
646
    try:
hjk's avatar
hjk committed
647
        painters = value["painters"]
648
    except:
hjk's avatar
hjk committed
649
        d.putPlainChildren(value)
650
        return
hjk's avatar
hjk committed
651
    check(0 <= painters and painters < 1000)
hjk's avatar
hjk committed
652
    d_ptr = value["d"]
hjk's avatar
hjk committed
653
    if isNull(d_ptr):
654
        d.putValue("(null)")
hjk's avatar
hjk committed
655
    else:
656
        checkSimpleRef(d_ptr["ref"])
657
        d.putValue("(%dx%d)" % (d_ptr["width"], d_ptr["height"]))
658
659
    bits = d_ptr["data"]
    nbytes = d_ptr["nbytes"]
660
    d.putNumChild(0)
hjk's avatar
hjk committed
661
    #d.putNumChild(1)
hjk's avatar
hjk committed
662
    if d.isExpanded():
663
        with Children(d):
hjk's avatar
hjk committed
664
            with SubItem(d, "data"):
665
                d.putNoType()
666
667
                d.putNumChild(0)
                d.putValue("size: %s bytes" % nbytes);
hjk's avatar
hjk committed
668
    format = d.currentItemFormat()
669
    if format == 1:
670
        d.putDisplay(StopDisplay)
671
    elif format == 2:
hjk's avatar
hjk committed
672
673
674
        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.
675
            d.putField("editformat", DisplayImageData)
676
            d.put('%s="' % name)
hjk's avatar
hjk committed
677
678
679
            d.put("%08x" % int(d_ptr["width"]))
            d.put("%08x" % int(d_ptr["height"]))
            d.put("%08x" % int(d_ptr["format"]))
680
            p = bits.cast(d.intType().pointer())
hjk's avatar
hjk committed
681
682
683
            for i in xrange(nbytes / 4):
                d.put("%08x" % int(p.dereference()))
                p += 1
684
            d.put('",')
hjk's avatar
hjk committed
685
686
687
        else:
            # Write to an external file. Much faster ;-(
            file = tempfile.mkstemp(prefix="gdbpy_")
688
            filename = file[1].replace("\\", "\\\\")
689
            p = bits.cast(d.charType().pointer())
hjk's avatar
hjk committed
690
691
            gdb.execute("dump binary memory %s %s %s" %
                (filename, cleanAddress(p), cleanAddress(p + nbytes)))
692
            d.putDisplay(DisplayImageFile, " %d %d %d %s"
693
                % (d_ptr["width"], d_ptr["height"], d_ptr["format"], filename))
hjk's avatar
hjk committed
694
695


hjk's avatar
hjk committed
696
697
698
def qdump__QLinkedList(d, value):
    d_ptr = value["d"]
    e_ptr = value["e"]
699
    n = d_ptr["size"]
hjk's avatar
hjk committed
700
    check(0 <= n and n <= 100*1000*1000)
701
    checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
702
    d.putItemCount(n)
703
    d.putNumChild(n)
hjk's avatar
hjk committed
704
705
706
    if d.isExpanded():
        innerType = templateArgument(value.type, 0)
        with Children(d, n, maxNumChild=1000, childType=innerType):
707
708
            p = e_ptr["n"]
            for i in d.childRange():
hjk's avatar
hjk committed
709
                d.putSubItem(i, p["t"])
710
                p = p["n"]
hjk's avatar
hjk committed
711

712
qqLocalesCount = None
hjk's avatar
hjk committed
713

hjk's avatar
hjk committed
714
def qdump__QLocale(d, value):
715
716
717
718
    # 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'.
719
720
721
    global qqLocalesCount
    if qqLocalesCount is None:
        try:
722
            qqLocalesCount = int(value(d.ns + 'locale_data_size'))
723
        except:
724
725
726
727
728
729
730
            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
731
    d.putStringValue(call(value, "name"))
732
733
734
    d.putNumChild(0)
    return
    # FIXME: Poke back for variants.
hjk's avatar
hjk committed
735
    if d.isExpanded():
736
        with Children(d, childType=d.lookupType(d.ns + "QChar"), childNumChild=0):
hjk's avatar
hjk committed
737
738
739
740
741
            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
742
                "timeFormat", d.ns + "QLocale::ShortFormat")
hjk's avatar
hjk committed
743
            d.putCallItem("timeFormat_(long)", value,
hjk's avatar
hjk committed
744
                "timeFormat", d.ns + "QLocale::LongFormat")
hjk's avatar
hjk committed
745
746
747
748
749
750
            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
751
752


hjk's avatar
hjk committed
753
def qdump__QMapNode(d, value):
754
    d.putEmptyValue()
755
    d.putNumChild(2)
hjk's avatar
hjk committed
756
757
758
759
760
761
    if d.isExpanded():
        with Children(d):
            d.putSubItem("key", value["key"])
            d.putSubItem("value", value["value"])


762
def qdumpHelper__Qt4_QMap(d, value, forceLong):
hjk's avatar
hjk committed
763
764
    d_ptr = value["d"].dereference()
    e_ptr = value["e"].dereference()
hjk's avatar
hjk committed
765
766
    n = d_ptr["size"]
    check(0 <= n and n <= 100*1000*1000)
767
    checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
768
769

    d.putItemCount(n)
770
    d.putNumChild(n)
hjk's avatar
hjk committed
771
    if d.isExpanded():
772
773
        if n > 10000:
            n = 10000
hjk's avatar
hjk committed
774

hjk's avatar
hjk committed
775
776
        keyType = templateArgument(value.type, 0)
        valueType = templateArgument(value.type, 1)
777
        isCompact = mapCompact(d.currentItemFormat(), keyType, valueType)
hjk's avatar
hjk committed
778
779
780
781
782
783

        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 *)
784
785
        nodeType = d.lookupType(d.ns + "QMapNode<%s, %s>" % (keyType, valueType))
        payloadSize = nodeType.sizeof - 2 * d.voidPtrSize
hjk's avatar
hjk committed
786

787
        if isCompact:
788
789
790
            innerType = valueType
        else:
            innerType = nodeType
hjk's avatar
hjk committed
791

hjk's avatar
hjk committed
792
        with Children(d, n, childType=innerType):
793
794
            for i in xrange(n):
                itd = it.dereference()
795
                base = it.cast(d.charPtr()) - payloadSize
796
                node = base.cast(nodeType.pointer()).dereference()
hjk's avatar
hjk committed
797
                with SubItem(d, i):
798
                    d.putField("iname", d.currentIName)
799
                    if isCompact:
800
                        #d.putType(valueType)
hjk's avatar
hjk committed
801
                        if forceLong:
802
                            d.putName("[%s] %s" % (i, node["key"]))
hjk's avatar
hjk committed
803
                        else:
804
805
                            d.putMapName(node["key"])
                        d.putItem(node["value"])
806
                    else:
hjk's avatar
hjk committed
807
                        d.putItem(node)
808
                it = it.dereference()["forward"].dereference()
hjk's avatar
hjk committed
809
810


811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
def qdumpHelper__Qt5_QMap(d, value, forceLong):
    d_ptr = value["d"].dereference()
    n = d_ptr["size"]
    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

        keyType = templateArgument(value.type, 0)
        valueType = templateArgument(value.type, 1)
        isCompact = mapCompact(d.currentItemFormat(), keyType, valueType)
826
        nodeType = d.lookupType(d.ns + "QMapNode<%s, %s>" % (keyType, valueType))
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
        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):
855
                    d.putField("iname", d.currentIName)
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
                    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):
    if value["d"].dereference().type.fields()[0].name == "backward":
        qdumpHelper__Qt4_QMap(d, value, forceLong)
    else:
        qdumpHelper__Qt5_QMap(d, value, forceLong)

872
873
874
def qform__QMap():
    return mapForms()

hjk's avatar
hjk committed
875
876
def qdump__QMap(d, value):
    qdumpHelper__QMap(d, value, False)
hjk's avatar
hjk committed
877

878
879
880
def qform__QMultiMap():
    return mapForms()

hjk's avatar
hjk committed
881
882
def qdump__QMultiMap(d, value):
    qdumpHelper__QMap(d, value, True)
hjk's avatar
hjk committed
883
884
885
886
887
888
889
890
891
892
893
894
895


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
896
897
def qdump__QObject(d, value):
    #warn("OBJECT: %s " % value)
hjk's avatar
hjk committed
898
899
    d.tryPutObjectNameValue(value)

900
    try:
hjk's avatar
hjk committed
901
        privateTypeName = self.ns + "QObjectPrivate"
902
        privateType = d.lookupType(privateTypeName)
hjk's avatar
hjk committed
903
        staticMetaObject = value["staticMetaObject"]
904
    except:
hjk's avatar
hjk committed
905
        d.putPlainChildren(value)
906
        return
hjk's avatar
hjk committed
907
    #warn("SMO: %s " % staticMetaObject)
908
909
910
911
912
913
914
    #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)

915
916
    if privateType is None:
        d.putNumChild(4)
hjk's avatar
hjk committed
917
        #d.putValue(cleanAddress(value.address))
918
        d.putPlainChildren(value)
hjk's avatar
hjk committed
919
        if d.isExpanded():
920
            with Children(d):
hjk's avatar
hjk committed
921
                d.putFields(value)
922
        return
hjk's avatar
hjk committed
923
    #warn("OBJECTNAME: %s " % objectName)
924
    #warn("D_PTR: %s " % d_ptr)
hjk's avatar
hjk committed
925
    mo = d_ptr["metaObject"]
926
927
928
    if not isAccessible(mo):
        d.putInaccessible()
        return
hjk's avatar
hjk committed
929
930
931
932
933
934
    if isNull(mo):
        mo = staticMetaObject
    #warn("MO: %s " % mo)
    #warn("MO.D: %s " % mo["d"])
    metaData = mo["d"]["data"]
    metaStringData = mo["d"]["stringdata"]
935
936
937
938
    # 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"]
939
        metaStringData = metaStringData.cast(d.charPtrType()) + int(offset)
940
941
942
    except:
        pass

943
    #extradata = mo["d"]["extradata"]   # Capitalization!
hjk's avatar
hjk committed
944
945
    #warn("METADATA: %s " % metaData)
    #warn("STRINGDATA: %s " % metaStringData)
hjk's avatar
hjk committed
946
947
    #warn("TYPE: %s " % value.type)
    #warn("INAME: %s " % d.currentIName())
948
    #d.putEmptyValue()
hjk's avatar
hjk committed
949
    #QSignalMapper::staticMetaObject
950
    #checkRef(d_ptr["ref"])
hjk's avatar
hjk committed
951
    d.putNumChild(4)
hjk's avatar
hjk committed
952
    if d.isExpanded():
953
      with Children(d):
954

hjk's avatar
hjk committed
955
        # Local data.
956
957
        if privateTypeName != d.ns + "QObjectPrivate":
            if not privateType is None:
hjk's avatar
hjk committed
958
              with SubItem(d, "data"):
959
                d.putEmptyValue()
960
                d.putNoType()
961
                d.putNumChild(1)
hjk's avatar
hjk committed
962
                if d.isExpanded():
963
                    with Children(d):
hjk's avatar
hjk committed
964
                        d.putFields(d_ptr, False)
965
966


hjk's avatar
hjk committed
967
        d.putFields(value)
968
        # Parent and children.
hjk's avatar
hjk committed
969
970
971
        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
972

973
        # Properties.
hjk's avatar
hjk committed
974
        with SubItem(d, "properties"):
975
            # Prolog
976
977
            extraData = d_ptr["extraData"]   # Capitalization!
            if isNull(extraData):
978
                dynamicPropertyCount = 0
979
            else:
980
                extraDataType = d.lookupType(
981
982
983
984
985
986
987
988
989
                    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"]
990
991
                dynamicPropertyCount = namesEnd - namesBegin

hjk's avatar
hjk committed
992
            #staticPropertyCount = call(mo, "propertyCount")
993
            staticPropertyCount = metaData[6]
994
995
996
            #warn("PROPERTY COUNT: %s" % staticPropertyCount)
            propertyCount = staticPropertyCount + dynamicPropertyCount

997
            d.putNoType()
998
999
1000
            d.putItemCount(propertyCount)
            d.putNumChild(propertyCount)

hjk's avatar
hjk committed
1001
            if d.isExpanded():
1002
1003
                # FIXME: Make this global. Don't leak.
                variant = "'%sQVariant'" % d.ns
1004
                # Avoid malloc symbol clash with QVector
hjk's avatar
hjk committed
1005
1006
                gdb.execute("set $d = (%s*)calloc(sizeof(%s), 1)"
                    % (variant, variant))
1007
                gdb.execute("set $d.d.is_shared = 0")
1008

hjk's avatar
hjk committed
1009
                with Children(d):
1010
                    # Dynamic properties.
1011
                    if dynamicPropertyCount != 0:
hjk's avatar