qttypes.py 97 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
def qByteArrayData(d, value):
70
    private = value['d'].dereference()
71
72
73
74
75
    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.
76
77
78
        offset = int(private['offset'])
        addr = d.addressOf(private) + offset
        data = createPointerValue(value, addr, d.charType())
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
        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)
111

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

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


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


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

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

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

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

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

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

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

hjk's avatar
hjk committed
253

hjk's avatar
hjk committed
254
def qdump__QDate(d, value):
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    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
271
272


hjk's avatar
hjk committed
273
def qdump__QTime(d, value):
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    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)
291
292


hjk's avatar
hjk committed
293
def qdump__QDateTime(d, value):
294
295
    try:
        # Fails without debug info.
296
        p = value["d"]["d"].dereference()
297
    except:
hjk's avatar
hjk committed
298
        d.putPlainChildren(value)
299
        return
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    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
319
320
321


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


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


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

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

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


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

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


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

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

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

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

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

    hashDataType = d_ptr.type
    hashNodeType = e_ptr.type

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

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


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

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

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


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

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

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

613
614
    d.putItemCount(size)
    d.putNumChild(size)
hjk's avatar
hjk committed
615
    if d.isExpanded():
hjk's avatar
hjk committed
616
617
618
619
620
621
        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:
622
623
624
625
626
627
628
629
630
631
632
633
        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())
634
        else:
635
636
637
638
639
640
            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
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 = int(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"]
hjk's avatar
hjk committed
699
    n = int(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
    if d.isExpanded():
705
        innerType = d.templateArgument(value.type, 0)
hjk's avatar
hjk committed
706
        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
    n = int(d_ptr["size"])
hjk's avatar
hjk committed
766
    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

775
776
        keyType = d.templateArgument(value.type, 0)
        valueType = d.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
def qdumpHelper__Qt5_QMap(d, value, forceLong):
    d_ptr = value["d"].dereference()
hjk's avatar
hjk committed
813
    n = int(d_ptr["size"])
814
815
816
817
818
819
820
821
822
    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

823
824
        keyType = d.templateArgument(value.type, 0)
        valueType = d.templateArgument(value.type, 1)
825
        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():