stdtypes.py 28.2 KB
Newer Older
hjk's avatar
hjk committed
1 2
############################################################################
#
3
# Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# Contact: http://www.qt-project.org/legal
#
# This file is part of Qt Creator.
#
# Commercial License Usage
# Licensees holding valid commercial Qt licenses may use this file in
# accordance with the commercial license agreement provided with the
# Software or, alternatively, in accordance with the terms contained in
# a written agreement between you and Digia.  For licensing terms and
# conditions see http://qt.digia.com/licensing.  For further information
# use the contact form at http://qt.digia.com/contact-us.
#
# GNU Lesser General Public License Usage
# Alternatively, this file may be used under the terms of the GNU Lesser
# General Public License version 2.1 as published by the Free Software
# Foundation and appearing in the file LICENSE.LGPL included in the
# packaging of this file.  Please review the following information to
# ensure the GNU Lesser General Public License version 2.1 requirements
# will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
#
# In addition, as a special exception, Digia gives you certain additional
# rights.  These rights are described in the Digia Qt LGPL Exception
# version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
#
#############################################################################

from dumper import *

32 33 34
def qform__std__array():
    return arrayForms()

hjk's avatar
hjk committed
35 36 37 38 39
def qdump__std__array(d, value):
    size = d.numericTemplateArgument(value.type, 1)
    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
40
        d.putPlotData(d.addressOf(value), size, d.templateArgument(value.type, 0))
41

hjk's avatar
hjk committed
42

43 44
def qform__std____1__array():
    return arrayForms()
hjk's avatar
hjk committed
45 46 47 48 49 50 51

def qdump__std____1__array(d, value):
    qdump__std__array(d, value)


def qdump__std__complex(d, value):
    innerType = d.templateArgument(value.type, 0)
52 53
    real = value.cast(innerType)
    imag = d.createValue(d.addressOf(value) + innerType.sizeof, innerType)
hjk's avatar
hjk committed
54 55 56 57 58 59 60 61 62
    d.putValue("(%f, %f)" % (real, imag));
    d.putNumChild(2)
    if d.isExpanded():
        with Children(d, 2, childType=innerType):
            d.putSubItem("real", real)
            d.putSubItem("imag", imag)


def qdump__std__deque(d, value):
63 64 65 66
    if d.isQnxTarget():
        qdump__std__deque__QNX(d, value)
        return

hjk's avatar
hjk committed
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
    innerType = d.templateArgument(value.type, 0)
    innerSize = innerType.sizeof
    bufsize = 1
    if innerSize < 512:
        bufsize = int(512 / innerSize)

    impl = value["_M_impl"]
    start = impl["_M_start"]
    finish = impl["_M_finish"]
    size = bufsize * toInteger(finish["_M_node"] - start["_M_node"] - 1)
    size += toInteger(finish["_M_cur"] - finish["_M_first"])
    size += toInteger(start["_M_last"] - start["_M_cur"])

    d.check(0 <= size and size <= 1000 * 1000 * 1000)
    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
        with Children(d, size, maxNumChild=2000, childType=innerType):
            pcur = start["_M_cur"]
            pfirst = start["_M_first"]
            plast = start["_M_last"]
            pnode = start["_M_node"]
            for i in d.childRange():
                d.putSubItem(i, pcur.dereference())
                pcur += 1
92
                if toInteger(pcur) == toInteger(plast):
hjk's avatar
hjk committed
93 94 95 96 97 98
                    newnode = pnode + 1
                    pnode = newnode
                    pfirst = newnode.dereference()
                    plast = pfirst + bufsize
                    pcur = pfirst

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
def qdump__std__deque__QNX(d, value):
    innerType = d.templateArgument(value.type, 0)
    innerSize = innerType.sizeof
    if innerSize <= 1:
        bufsize = 16
    elif innerSize <= 2:
        bufsize = 8
    elif innerSize <= 4:
        bufsize = 4
    elif innerSize <= 8:
        bufsize = 2
    else:
        bufsize = 1

    myoff = value['_Myoff']
    mysize = value['_Mysize']
    mapsize = value['_Mapsize']

    d.check(0 <= mapsize and mapsize <= 1000 * 1000 * 1000)
    d.putItemCount(mysize)
    d.putNumChild(mysize)
    if d.isExpanded():
        with Children(d, mysize, maxNumChild=2000, childType=innerType):
            map = value['_Map']
            for i in d.childRange():
                block = myoff / bufsize
                offset = myoff - (block * bufsize)
                if mapsize <= block:
                    block -= mapsize
                d.putSubItem(i, map[block][offset])
                myoff += 1;

hjk's avatar
hjk committed
131 132 133 134 135
def qdump__std____debug__deque(d, value):
    qdump__std__deque(d, value)


def qdump__std__list(d, value):
136 137 138 139
    if d.isQnxTarget():
        qdump__std__list__QNX(d, value)
        return

hjk's avatar
hjk committed
140 141
    impl = value["_M_impl"]
    node = impl["_M_node"]
142
    head = d.addressOf(node)
hjk's avatar
hjk committed
143
    size = 0
144
    pp = d.extractPointer(head)
hjk's avatar
hjk committed
145 146
    while head != pp and size <= 1001:
        size += 1
147
        pp = d.extractPointer(pp)
hjk's avatar
hjk committed
148 149 150 151 152 153 154 155 156 157 158 159 160

    d.putItemCount(size, 1000)
    d.putNumChild(size)

    if d.isExpanded():
        p = node["_M_next"]
        innerType = d.templateArgument(value.type, 0)
        with Children(d, size, maxNumChild=1000, childType=innerType):
            for i in d.childRange():
                innerPointer = innerType.pointer()
                d.putSubItem(i, (p + 1).cast(innerPointer).dereference())
                p = p["_M_next"]

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
def qdump__std__list__QNX(d, value):
    node = value["_Myhead"]
    size = value["_Mysize"]

    d.putItemCount(size, 1000)
    d.putNumChild(size)

    if d.isExpanded():
        p = node["_Next"]
        innerType = d.templateArgument(value.type, 0)
        with Children(d, size, maxNumChild=1000, childType=innerType):
            for i in d.childRange():
                d.putSubItem(i, p['_Myval'])
                p = p["_Next"]

hjk's avatar
hjk committed
176 177 178 179 180 181 182
def qdump__std____debug__list(d, value):
    qdump__std__list(d, value)

def qform__std__map():
    return mapForms()

def qdump__std__map(d, value):
183 184 185 186
    if d.isQnxTarget():
        qdump__std__map__QNX(d, value)
        return

hjk's avatar
hjk committed
187 188 189 190 191 192 193
    impl = value["_M_t"]["_M_impl"]
    size = int(impl["_M_node_count"])
    d.check(0 <= size and size <= 100*1000*1000)
    d.putItemCount(size)
    d.putNumChild(size)

    if d.isExpanded():
194 195
        pairType = d.templateArgument(d.templateArgument(value.type, 3), 0)
        pairPointer = pairType.pointer()
196
        with PairedChildren(d, size, pairType=pairType, maxNumChild=1000):
197
            node = impl["_M_header"]["_M_left"]
hjk's avatar
hjk committed
198 199 200
            for i in d.childRange():
                with SubItem(d, i):
                    pair = (node + 1).cast(pairPointer).dereference()
201
                    d.putPair(pair)
hjk's avatar
hjk committed
202 203 204 205 206 207 208 209 210 211 212 213
                if d.isNull(node["_M_right"]):
                    parent = node["_M_parent"]
                    while node == parent["_M_right"]:
                        node = parent
                        parent = parent["_M_parent"]
                    if node["_M_right"] != parent:
                        node = parent
                else:
                    node = node["_M_right"]
                    while not d.isNull(node["_M_left"]):
                        node = node["_M_left"]

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
def qdump__std__map__QNX(d, value):
    size = value['_Mysize']
    d.check(0 <= size and size <= 100*1000*1000)
    d.putItemCount(size)
    d.putNumChild(size)

    if d.isExpanded():
        keyType = d.templateArgument(value.type, 0)
        valueType = d.templateArgument(value.type, 1)
        try:
            # Does not work on gcc 4.4, the allocator type (fourth template
            # argument) seems not to be available.
            pairType = d.templateArgument(d.templateArgument(value.type, 3), 0)
            pairPointer = pairType.pointer()
        except:
            # So use this as workaround:
            pairType = d.templateArgument(impl.type, 1)
            pairPointer = pairType.pointer()
        isCompact = d.isMapCompact(keyType, valueType)
        innerType = pairType
        if isCompact:
            innerType = valueType
        head = value['_Myhead']
        node = head['_Left']
        nodeType = head.type
        childType = innerType
        if size == 0:
            childType = pairType
        childNumChild = 2
        if isCompact:
            childNumChild = None
        with Children(d, size, maxNumChild=1000,
                childType=childType, childNumChild=childNumChild):
            for i in d.childRange():
                with SubItem(d, i):
                    pair = node.cast(nodeType).dereference()['_Myval']
                    if isCompact:
                        d.putMapName(pair["first"])
                        d.putItem(pair["second"])
                    else:
                        d.putEmptyValue()
                        if d.isExpanded():
                            with Children(d, 2):
                                d.putSubItem("first", pair["first"])
                                d.putSubItem("second", pair["second"])
                if not node['_Right']['_Isnil']:
                    node = node['_Right']
                    while not node['_Left']['_Isnil']:
                        node = node['_Left']
                else:
                    parent = node['_Parent']
                    while node == parent['_Right']['_Isnil']:
                        node = parent
                        parent = parent['_Parent']
                    if node['_Right'] != parent:
                        node = parent

hjk's avatar
hjk committed
271 272 273 274 275 276 277 278 279 280
def qdump__std____debug__map(d, value):
    qdump__std__map(d, value)

def qdump__std____debug__set(d, value):
    qdump__std__set(d, value)

def qdump__std____cxx1998__map(d, value):
    qdump__std__map(d, value)

def stdTreeIteratorHelper(d, value):
281
    node = value["_M_node"].dereference()
hjk's avatar
hjk committed
282 283 284
    d.putNumChild(1)
    d.putEmptyValue()
    if d.isExpanded():
285
        nodeTypeName = str(value.type).replace("_Rb_tree_iterator", "_Rb_tree_node", 1)
286
        nodeTypeName = nodeTypeName.replace("_Rb_tree_const_iterator", "_Rb_tree_node", 1)
287 288
        nodeType = d.lookupType(nodeTypeName)
        data = node.cast(nodeType)["_M_value_field"]
hjk's avatar
hjk committed
289
        with Children(d):
hjk's avatar
hjk committed
290 291 292
            first = d.childWithName(data, "first")
            if first:
                d.putSubItem("first", first)
hjk's avatar
hjk committed
293
                d.putSubItem("second", data["second"])
294
            else:
hjk's avatar
hjk committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
                d.putSubItem("value", data)
            with SubItem(d, "node"):
                d.putNumChild(1)
                d.putEmptyValue()
                d.putType(" ")
                if d.isExpanded():
                    with Children(d):
                        d.putSubItem("color", node["_M_color"])
                        d.putSubItem("left", node["_M_left"])
                        d.putSubItem("right", node["_M_right"])
                        d.putSubItem("parent", node["_M_parent"])


def qdump__std___Rb_tree_iterator(d, value):
    stdTreeIteratorHelper(d, value)

def qdump__std___Rb_tree_const_iterator(d, value):
    stdTreeIteratorHelper(d, value)

def qdump__std__map__iterator(d, value):
    stdTreeIteratorHelper(d, value)

def qdump____gnu_debug___Safe_iterator(d, value):
    d.putItem(value["_M_current"])

def qdump__std__map__const_iterator(d, value):
    stdTreeIteratorHelper(d, value)

def qdump__std__set__iterator(d, value):
    stdTreeIteratorHelper(d, value)

def qdump__std__set__const_iterator(d, value):
    stdTreeIteratorHelper(d, value)

def qdump__std____cxx1998__set(d, value):
    qdump__std__set(d, value)

def qdump__std__set(d, value):
333 334 335 336
    if d.isQnxTarget():
        qdump__std__set__QNX(d, value)
        return

hjk's avatar
hjk committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    impl = value["_M_t"]["_M_impl"]
    size = int(impl["_M_node_count"])
    d.check(0 <= size and size <= 100*1000*1000)
    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
        valueType = d.templateArgument(value.type, 0)
        node = impl["_M_header"]["_M_left"]
        with Children(d, size, maxNumChild=1000, childType=valueType):
            for i in d.childRange():
                d.putSubItem(i, (node + 1).cast(valueType.pointer()).dereference())
                if d.isNull(node["_M_right"]):
                    parent = node["_M_parent"]
                    while node == parent["_M_right"]:
                        node = parent
                        parent = parent["_M_parent"]
                    if node["_M_right"] != parent:
                        node = parent
                else:
                    node = node["_M_right"]
                    while not d.isNull(node["_M_left"]):
                        node = node["_M_left"]

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
def qdump__std__set__QNX(d, value):
    size = value['_Mysize']
    d.check(0 <= size and size <= 100*1000*1000)
    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
        valueType = d.templateArgument(value.type, 0)
        head = value['_Myhead']
        node = head['_Left']
        nodeType = head.type
        with Children(d, size, maxNumChild=1000, childType=valueType):
            for i in d.childRange():
                d.putSubItem(i, node.cast(nodeType).dereference()['_Myval'])
                if not node['_Right']['_Isnil']:
                    node = node['_Right']
                    while not node['_Left']['_Isnil']:
                        node = node['_Left']
                else:
                    parent = node['_Parent']
                    while node == parent['_Right']['_Isnil']:
                        node = parent
                        parent = parent['_Parent']
                    if node['_Right'] != parent:
                        node = parent
hjk's avatar
hjk committed
384 385

def qdump__std__stack(d, value):
386 387
    d.putItem(value["c"])
    d.putType(str(value.type))
hjk's avatar
hjk committed
388 389 390 391 392 393 394 395 396 397 398

def qdump__std____debug__stack(d, value):
    qdump__std__stack(d, value)

def qform__std__string():
    return "Inline,In Separate Window"

def qdump__std__string(d, value):
    qdump__std__stringHelper1(d, value, 1)

def qdump__std__stringHelper1(d, value, charSize):
399 400 401 402
    if d.isQnxTarget():
        qdump__std__stringHelper1__QNX(d, value, charSize)
        return

hjk's avatar
hjk committed
403 404 405 406 407 408 409
    data = value["_M_dataplus"]["_M_p"]
    # We can't lookup the std::string::_Rep type without crashing LLDB,
    # so hard-code assumption on member position
    # struct { size_type _M_length, size_type _M_capacity, int _M_refcount; }
    sizePtr = data.cast(d.sizetType().pointer())
    size = int(sizePtr[-3])
    alloc = int(sizePtr[-2])
410
    refcount = int(sizePtr[-1]) & 0xffffffff
hjk's avatar
hjk committed
411 412 413 414
    d.check(refcount >= -1) # Can be -1 accoring to docs.
    d.check(0 <= size and size <= alloc and alloc <= 100*1000*1000)
    qdump_stringHelper(d, sizePtr, size * charSize, charSize)

415 416 417 418 419 420 421 422 423 424 425 426 427 428
def qdump__std__stringHelper1__QNX(d, value, charSize):
    size = value['_Mysize']
    alloc = value['_Myres']
    _BUF_SIZE = 16 / charSize
    if _BUF_SIZE <= alloc: #(_BUF_SIZE <= _Myres ? _Bx._Ptr : _Bx._Buf);
        data = value['_Bx']['_Ptr']
    else:
        data = value['_Bx']['_Buf']
    sizePtr = data.cast(d.charType().pointer())
    refcount = int(sizePtr[-1])
    d.check(refcount >= -1) # Can be -1 accoring to docs.
    d.check(0 <= size and size <= alloc and alloc <= 100*1000*1000)
    qdump_stringHelper(d, sizePtr, size * charSize, charSize)

hjk's avatar
hjk committed
429
def qdump_stringHelper(d, data, size, charSize):
430
    cutoff = min(size, d.stringCutOff)
hjk's avatar
hjk committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    mem = d.readMemory(data, cutoff)
    if charSize == 1:
        encodingType = Hex2EncodedLatin1
        displayType = DisplayLatin1String
    elif charSize == 2:
        encodingType = Hex4EncodedLittleEndian
        displayType = DisplayUtf16String
    else:
        encodingType = Hex8EncodedLittleEndian
        displayType = DisplayUtf16String

    d.putNumChild(0)
    d.putValue(mem, encodingType)

    format = d.currentItemFormat()
    if format == 1:
        d.putDisplay(StopDisplay)
    elif format == 2:
        d.putField("editformat", displayType)
        d.putField("editvalue", d.readMemory(data, size))


def qdump__std____1__string(d, value):
hjk's avatar
hjk committed
454 455 456 457
    base = d.addressOf(value)
    firstByte = d.extractByte(base)
    if firstByte & 1:
        # Long/external.
458
        data = d.extractPointer(base + 2 * d.ptrSize())
hjk's avatar
hjk committed
459 460 461 462 463
        size = d.extractInt(base + d.ptrSize())
    else:
        # Short/internal.
        size = firstByte / 2
        data = base + 1
hjk's avatar
hjk committed
464 465 466 467 468
    qdump_stringHelper(d, data, size, 1)
    d.putType("std::string")


def qdump__std____1__wstring(d, value):
hjk's avatar
hjk committed
469 470 471 472
    base = d.addressOf(value)
    firstByte = d.extractByte(base)
    if firstByte & 1:
        # Long/external.
473
        data = d.extractPointer(base + 2 * d.ptrSize())
hjk's avatar
hjk committed
474 475 476 477 478 479 480
        size = d.extractInt(base + d.ptrSize())
    else:
        # Short/internal.
        size = firstByte / 2
        data = base + 4
    qdump_stringHelper(d, data, size * 4, 4)
    d.putType("std::xxwstring")
hjk's avatar
hjk committed
481 482 483 484 485 486 487 488 489 490


def qdump__std__shared_ptr(d, value):
    i = value["_M_ptr"]
    if d.isNull(i):
        d.putValue("(null)")
        d.putNumChild(0)
        return

    if d.isSimpleType(d.templateArgument(value.type, 0)):
491
        d.putValue("%s @0x%x" % (d.simpleValue(i.dereference()), d.pointerValue(i)))
hjk's avatar
hjk committed
492
    else:
493
        i = d.expensiveDowncast(i)
hjk's avatar
hjk committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
        d.putValue("@0x%x" % d.pointerValue(i))

    d.putNumChild(3)
    with Children(d, 3):
        d.putSubItem("data", i)
        refcount = value["_M_refcount"]["_M_pi"]
        d.putIntItem("usecount", refcount["_M_use_count"])
        d.putIntItem("weakcount", refcount["_M_weak_count"])

def qdump__std____1__shared_ptr(d, value):
    i = value["__ptr_"]
    if d.isNull(i):
        d.putValue("(null)")
        d.putNumChild(0)
        return

    if d.isSimpleType(d.templateArgument(value.type, 0)):
511
        d.putValue("%s @0x%x" % (d.simpleValue(i.dereference()), d.pointerValue(i)))
hjk's avatar
hjk committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    else:
        d.putValue("@0x%x" % d.pointerValue(i))

    d.putNumChild(3)
    with Children(d, 3):
        d.putSubItem("data", i.dereference())
        d.putFields(value["__cntrl_"].dereference())
        #d.putIntItem("usecount", refcount["_M_use_count"])
        #d.putIntItem("weakcount", refcount["_M_weak_count"])

def qdump__std__unique_ptr(d, value):
    i = value["_M_t"]["_M_head_impl"]
    if d.isNull(i):
        d.putValue("(null)")
        d.putNumChild(0)
        return

    if d.isSimpleType(d.templateArgument(value.type, 0)):
530
        d.putValue("%s @0x%x" % (d.simpleValue(i.dereference()), d.pointerValue(i)))
hjk's avatar
hjk committed
531
    else:
532
        i = d.expensiveDowncast(i)
hjk's avatar
hjk committed
533 534 535 536 537 538 539
        d.putValue("@0x%x" % d.pointerValue(i))

    d.putNumChild(1)
    with Children(d, 1):
        d.putSubItem("data", i)

def qdump__std____1__unique_ptr(d, value):
540 541
    #i = d.childAt(d.childAt(value["__ptr_"], 0), 0)
    i = d.childAt(value["__ptr_"], 0)["__first_"]
hjk's avatar
hjk committed
542 543 544 545 546 547
    if d.isNull(i):
        d.putValue("(null)")
        d.putNumChild(0)
        return

    if d.isSimpleType(d.templateArgument(value.type, 0)):
548
        d.putValue("%s @0x%x" % (d.simpleValue(i.dereference()), d.pointerValue(i)))
hjk's avatar
hjk committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    else:
        d.putValue("@0x%x" % d.pointerValue(i))

    d.putNumChild(1)
    with Children(d, 1):
        d.putSubItem("data", i.dereference())


def qform__std__unordered_map():
    return mapForms()

def qform__std____debug__unordered_map():
    return mapForms()

def qdump__std__unordered_map(d, value):
564 565 566 567 568
    keyType = d.templateArgument(value.type, 0)
    valueType = d.templateArgument(value.type, 1)
    allocatorType = d.templateArgument(value.type, 4)
    pairType = d.templateArgument(allocatorType, 0)
    ptrSize = d.ptrSize()
hjk's avatar
hjk committed
569
    try:
570
    # gcc >= 4.7
hjk's avatar
hjk committed
571 572
        size = value["_M_element_count"]
        start = value["_M_before_begin"]["_M_nxt"]
573
        offset = 0
hjk's avatar
hjk committed
574
    except:
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
            try:
                # libc++ (Mac)
                size = value["_M_h"]["_M_element_count"]
                start = value["_M_h"]["_M_bbegin"]["_M_node"]["_M_nxt"]
                offset = 0
            except:
                # gcc 4.6.2
                size = value["_M_element_count"]
                start = value["_M_buckets"].dereference()
                # FIXME: Pointer-aligned?
                offset = pairType.sizeof
                d.putItemCount(size)
                # We don't know where the data is
                d.putNumChild(0)
                return
hjk's avatar
hjk committed
590 591 592 593 594 595 596 597 598 599 600 601
    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
        p = d.pointerValue(start)
        if d.isMapCompact(keyType, valueType):
            with Children(d, size, childType=valueType):
                for i in d.childRange():
                    pair = d.createValue(p + ptrSize, pairType)
                    with SubItem(d, i):
                        d.putField("iname", d.currentIName)
                        d.putName("[%s] %s" % (i, pair["first"]))
                        d.putValue(pair["second"])
602
                    p = d.extractPointer(p)
hjk's avatar
hjk committed
603 604 605
        else:
            with Children(d, size, childType=pairType):
                for i in d.childRange():
606
                    d.putSubItem(i, d.createValue(p + ptrSize - offset, pairType))
607
                    p = d.extractPointer(p + offset)
hjk's avatar
hjk committed
608 609 610 611 612 613

def qdump__std____debug__unordered_map(d, value):
    qdump__std__unordered_map(d, value)

def qdump__std__unordered_set(d, value):
    try:
614
        # gcc >= 4.7
hjk's avatar
hjk committed
615 616
        size = value["_M_element_count"]
        start = value["_M_before_begin"]["_M_nxt"]
617
        offset = 0
hjk's avatar
hjk committed
618
    except:
619 620 621 622 623 624 625 626 627 628
            try:
                # libc++ (Mac)
                size = value["_M_h"]["_M_element_count"]
                start = value["_M_h"]["_M_bbegin"]["_M_node"]["_M_nxt"]
                offset = 0
            except:
                # gcc 4.6.2
                size = value["_M_element_count"]
                start = value["_M_buckets"].dereference()
                offset = d.ptrSize()
hjk's avatar
hjk committed
629 630 631 632 633 634 635 636
    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
        p = d.pointerValue(start)
        valueType = d.templateArgument(value.type, 0)
        with Children(d, size, childType=valueType):
            ptrSize = d.ptrSize()
            for i in d.childRange():
637
                d.putSubItem(i, d.createValue(p + ptrSize - offset, valueType))
638
                p = d.extractPointer(p + offset)
hjk's avatar
hjk committed
639

640 641 642 643
def qform__std____1__unordered_map():
    return mapForms()

def qdump__std____1__unordered_map(d, value):
644 645
    size = int(value["__table_"]["__p2_"]["__first_"])
    d.putItemCount(size)
646
    if d.isExpanded():
647
        node = value["__table_"]["__p1_"]["__first_"]["__next_"]
648
        with PairedChildren(d, size, pairType=node["__value_"].type, maxNumChild=1000):
hjk's avatar
hjk committed
649
            for i in d.childRange():
650
                with SubItem(d, i):
651
                    d.putPair(node["__value_"], i)
652
                node = node["__next_"]
653

654 655 656 657 658 659 660 661 662 663 664 665 666

def qdump__std____1__unordered_set(d, value):
    size = int(value["__table_"]["__p2_"]["__first_"])
    d.putItemCount(size)
    if d.isExpanded():
        node = value["__table_"]["__p1_"]["__first_"]["__next_"]
        valueType = d.templateArgument(value.type, 0)
        with Children(d, size, childType=valueType, maxNumChild=1000):
            for i in d.childRange():
                d.putSubItem(i, node["__value_"])
                node = node["__next_"]


hjk's avatar
hjk committed
667 668 669 670
def qdump__std____debug__unordered_set(d, value):
    qdump__std__unordered_set(d, value)


671 672 673
def qform__std__vector():
    return arrayForms()

674 675 676
def qedit__std__vector(d, value, data):
    import gdb
    values = data.split(',')
hjk's avatar
hjk committed
677
    n = len(values)
678
    innerType = d.templateArgument(value.type, 0)
hjk's avatar
hjk committed
679 680
    cmd = "set $d = (%s*)calloc(sizeof(%s)*%s,1)" % (innerType, innerType, n)
    gdb.execute(cmd)
681
    cmd = "set {void*[3]}%s = {$d, $d+%s, $d+%s}" % (value.address, n, n)
hjk's avatar
hjk committed
682
    gdb.execute(cmd)
683
    cmd = "set (%s[%d])*$d={%s}" % (innerType, n, data)
hjk's avatar
hjk committed
684 685 686
    gdb.execute(cmd)

def qdump__std__vector(d, value):
687 688 689 690
    if d.isQnxTarget():
        qdump__std__vector__QNX(d, value)
        return

hjk's avatar
hjk committed
691 692 693
    impl = value["_M_impl"]
    type = d.templateArgument(value.type, 0)
    alloc = impl["_M_end_of_storage"]
694 695 696 697
    # The allocator case below is bogus, but that's what Apple
    # LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
    # produces.
    isBool = str(type) == 'bool' or str(type) == 'std::allocator<bool>'
hjk's avatar
hjk committed
698 699 700 701
    if isBool:
        start = impl["_M_start"]["_M_p"]
        finish = impl["_M_finish"]["_M_p"]
        # FIXME: 8 is CHAR_BIT
hjk's avatar
hjk committed
702
        size = (d.pointerValue(finish) - d.pointerValue(start)) * 8
703 704
        size += int(impl["_M_finish"]["_M_offset"])
        size -= int(impl["_M_start"]["_M_offset"])
hjk's avatar
hjk committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
    else:
        start = impl["_M_start"]
        finish = impl["_M_finish"]
        size = finish - start

    d.check(0 <= size and size <= 1000 * 1000 * 1000)
    d.check(finish <= alloc)
    d.checkPointer(start)
    d.checkPointer(finish)
    d.checkPointer(alloc)

    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
        if isBool:
            with Children(d, size, maxNumChild=10000, childType=type):
hjk's avatar
hjk committed
721
                base = d.pointerValue(start)
hjk's avatar
hjk committed
722
                for i in d.childRange():
hjk's avatar
hjk committed
723
                    q = base + int(i / 8)
724
                    d.putBoolItem(str(i), (int(d.extractPointer(q)) >> (i % 8)) & 1)
hjk's avatar
hjk committed
725
        else:
726
            d.putPlotData(start, size, type)
hjk's avatar
hjk committed
727

728
def qdump__std__vector__QNX(d, value):
729
    innerType = d.templateArgument(value.type, 0)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    isBool = str(type) == 'bool'
    if isBool:
        impl = value['_Myvec']
        start = impl['_Myfirst']
        last = impl['_Mylast']
        end = impl['_Myend']
        size = value['_Mysize']
        storagesize = start.dereference().type.sizeof * 8
    else:
        start = value['_Myfirst']
        last = value['_Mylast']
        end = value['_Myend']
        size = int (last - start)
        alloc = int (end - start)

    d.check(0 <= size and size <= 1000 * 1000 * 1000)
    d.check(last <= end)
    d.checkPointer(start)
    d.checkPointer(last)
    d.checkPointer(end)

    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
        if isBool:
755
            with Children(d, size, maxNumChild=10000, childType=innerType):
756 757 758 759
                for i in d.childRange():
                    q = start + int(i / storagesize)
                    d.putBoolItem(str(i), (q.dereference() >> (i % storagesize)) & 1)
        else:
760
            d.putArrayData(start, size, innerType)
761

hjk's avatar
hjk committed
762 763 764 765
def qdump__std____1__vector(d, value):
    innerType = d.templateArgument(value.type, 0)
    if d.isLldb and d.childAt(value, 0).type == innerType:
        # That's old lldb automatically formatting
766
        begin = d.extractPointer(value)
hjk's avatar
hjk committed
767 768 769 770 771 772 773 774 775 776
        size = value.GetNumChildren()
    else:
        # Normal case
        begin = d.pointerValue(value['__begin_'])
        end = d.pointerValue(value['__end_'])
        size = (end - begin) / innerType.sizeof

    d.putItemCount(size)
    d.putNumChild(size)
    if d.isExpanded():
777
        d.putPlotData(begin, size, innerType)
778

hjk's avatar
hjk committed
779

780 781
def qform__std____debug__vector():
    return arrayForms()
hjk's avatar
hjk committed
782 783 784 785

def qdump__std____debug__vector(d, value):
    qdump__std__vector(d, value)

786

787 788
def qedit__std__string(d, value, data):
    d.call(value, "assign", '"%s"' % data.replace('"', '\\"'))
hjk's avatar
hjk committed
789

790 791
def qedit__string(d, expr, value):
    qedit__std__string(d, expr, value)
hjk's avatar
hjk committed
792 793 794 795 796 797 798 799 800 801 802 803

def qdump__string(d, value):
    qdump__std__string(d, value)

def qdump__std__wstring(d, value):
    charSize = d.lookupType('wchar_t').sizeof
    qdump__std__stringHelper1(d, value, charSize)

def qdump__std__basic_string(d, value):
    innerType = d.templateArgument(value.type, 0)
    qdump__std__stringHelper1(d, value, innerType.sizeof)

hjk's avatar
hjk committed
804 805 806 807 808 809 810 811 812
def qdump__std____1__basic_string(d, value):
    innerType = str(d.templateArgument(value.type, 0))
    if innerType == "char":
        qdump__std____1__string(d, value)
    elif innerType == "wchar_t":
        qdump__std____1__wstring(d, value)
    else:
        warn("UNKNOWN INNER TYPE %s" % innerType)

hjk's avatar
hjk committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
def qdump__wstring(d, value):
    qdump__std__wstring(d, value)


def qdump____gnu_cxx__hash_set(d, value):
    ht = value["_M_ht"]
    size = int(ht["_M_num_elements"])
    d.check(0 <= size and size <= 1000 * 1000 * 1000)
    d.putItemCount(size)
    d.putNumChild(size)
    type = d.templateArgument(value.type, 0)
    d.putType("__gnu__cxx::hash_set<%s>" % type)
    if d.isExpanded():
        with Children(d, size, maxNumChild=1000, childType=type):
            buckets = ht["_M_buckets"]["_M_impl"]
            bucketStart = buckets["_M_start"]
            bucketFinish = buckets["_M_finish"]
            p = bucketStart
            itemCount = 0
            for i in xrange(toInteger(bucketFinish - bucketStart)):
                if not d.isNull(p.dereference()):
                    cur = p.dereference()
                    while not d.isNull(cur):
                        with SubItem(d, itemCount):
                            d.putValue(cur["_M_val"])
                            cur = cur["_M_next"]
                            itemCount += 1
                p = p + 1


def qdump__uint8_t(d, value):
    d.putNumChild(0)
    d.putValue(int(value))

def qdump__int8_t(d, value):
    d.putNumChild(0)
    d.putValue(int(value))