gdbmacros.cpp 123 KB
Newer Older
1
/**************************************************************************
hjk's avatar
hjk committed
2
**
con's avatar
con committed
3 4
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
hjk's avatar
hjk committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
hjk's avatar
hjk committed
8
**
9
** Commercial Usage
con's avatar
con committed
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
con's avatar
con committed
15
**
16
** GNU Lesser General Public License Usage
con's avatar
con committed
17
**
18 19 20 21 22 23
** 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.
hjk's avatar
hjk committed
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
hjk's avatar
hjk committed
27
**
28
**************************************************************************/
con's avatar
con committed
29 30 31

#include <qglobal.h>

32 33 34 35 36
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
37
#include <QtCore/QHash>
38
#include <QtCore/QLinkedList>
39
#include <QtCore/QList>
40
#include <QtCore/QQueue>
41 42
#include <QtCore/QLocale>
#include <QtCore/QMap>
43
#include <QtCore/QMetaEnum>
44 45
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
46 47
#include <QtCore/QPoint>
#include <QtCore/QPointF>
48 49
#include <QtCore/QPointer>
#include <QtCore/QRect>
50
#include <QtCore/QRectF>
hjk's avatar
hjk committed
51
#include <QtCore/QStack>
52 53 54 55 56 57 58 59 60 61 62
#include <QtCore/QSize>
#include <QtCore/QSizeF>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QTextCodec>
#include <QtCore/QTextStream>
#include <QtCore/QVector>

#ifndef QT_BOOTSTRAPPED

#include <QtCore/QModelIndex>
63 64 65 66

#if QT_VERSION >= 0x040500
#include <QtCore/QSharedPointer>
#include <QtCore/QSharedDataPointer>
67
#include <QtCore/QSharedData>
68
#include <QtCore/QWeakPointer>
69
#endif
con's avatar
con committed
70

71 72 73 74 75 76 77
#ifndef USE_QT_GUI
#   ifdef QT_GUI_LIB
#        define USE_QT_GUI 1
#   endif
#endif

#if USE_QT_GUI
78
#   include <QtGui/QApplication>
79
#   include <QtGui/QImage>
80 81
#   include <QtGui/QPixmap>
#   include <QtGui/QWidget>
82 83 84 85
#   include <QtGui/QFont>
#   include <QtGui/QColor>
#   include <QtGui/QKeySequence>
#   include <QtGui/QSizePolicy>
86 87
#endif

88 89
#endif // QT_BOOTSTRAPPED

90 91 92 93
#ifdef Q_OS_WIN
#    include <windows.h>
#endif

94 95 96
#include <list>
#include <map>
#include <string>
97
#include <set>
98 99
#include <vector>

100 101 102
#ifdef QT_BOOTSTRAPPED

#   define NS ""
ck's avatar
ck committed
103 104
#   define NSX "'"
#   define NSY "'"
105 106 107 108 109 110 111 112 113 114

#else

#   include "gdbmacros_p.h"

#endif // QT_BOOTSTRAPPED


int qtGhVersion = QT_VERSION;

con's avatar
con committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
/*!
  \class QDumper
  \brief Helper class for producing "nice" output in Qt Creator's debugger.

  \internal

  The whole "custom dumper" implementation is currently far less modular
  than it could be. But as the code is still in a flux, making it nicer
  from a pure archtectural point of view seems still be a waste of resources.

  Some hints:

  New dumpers for non-templated classes should be mentioned in
  \c{qDumpObjectData440()} in the  \c{protocolVersion == 1} branch.

  Templated classes need extra support on the IDE level
  (see plugins/debugger/gdbengine.cpp) and should not be mentiond in
  \c{qDumpObjectData440()}.

134
  In any case, dumper processesing should end up in
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
135
  \c{handleProtocolVersion2and3()} and needs an entry in the big switch there.
con's avatar
con committed
136 137

  Next step is to create a suitable \c{static void qDumpFoo(QDumper &d)}
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
138
  function. At the bare minimum it should contain something like this:
con's avatar
con committed
139 140 141 142 143


  \c{
    const Foo &foo = *reinterpret_cast<const Foo *>(d.data);

144 145 146
    d.putItem("value", ...);
    d.putItem("type", "Foo");
    d.putItem("numchild", "0");
con's avatar
con committed
147 148 149
  }


150
  'd.putItem(name, value)' roughly expands to:
hjk's avatar
hjk committed
151
        d.put((name)).put("=\"").put(value).put("\"";
con's avatar
con committed
152 153 154 155 156 157 158 159 160

  Useful (i.e. understood by the IDE) names include:

  \list
    \o "name" shows up in the first column in the Locals&Watchers view.
    \o "value" shows up in the second column.
    \o "valueencoded" should be set to "1" if the value is base64 encoded.
        Always base64-encode values that might use unprintable or otherwise
        "confuse" the protocol (like spaces and quotes). [A-Za-z0-9] is "safe".
161
        A value of "3" is used for base64-encoded UCS4, "2" denotes
con's avatar
con committed
162 163 164 165 166 167
        base64-encoded UTF16.
    \o "numchild" return the number of children in the view. Effectively, only
        0 and != 0 will be used, so don't try too hard to get the number right.
  \endlist

  If the current item has children, it might be queried to produce information
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
168
  about these children. In this case the dumper should use something like this:
con's avatar
con committed
169 170 171

  \c{
    if (d.dumpChildren) {
hjk's avatar
hjk committed
172 173 174
        d.beginChildren();
            [...]
        d.endChildren();
con's avatar
con committed
175 176 177 178 179 180 181 182
   }

  */

#if defined(QT_BEGIN_NAMESPACE)
QT_BEGIN_NAMESPACE
#endif

183 184 185
const char *stdStringTypeC = "std::basic_string<char,std::char_traits<char>,std::allocator<char> >";
const char *stdWideStringTypeUShortC = "std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> >";

con's avatar
con committed
186 187 188 189 190
#if defined(QT_BEGIN_NAMESPACE)
QT_END_NAMESPACE
#endif


191 192 193
// This can be mangled typenames of nested templates, each char-by-char
// comma-separated integer list...
// The output buffer.
194 195 196 197 198 199 200 201 202 203 204
#ifdef MACROSDEBUG
    Q_DECL_EXPORT char xDumpInBuffer[10000];
    Q_DECL_EXPORT char xDumpOutBuffer[1000000];
    #define inBuffer xDumpInBuffer
    #define outBuffer xDumpOutBuffer
#else
    Q_DECL_EXPORT char qDumpInBuffer[10000];
    Q_DECL_EXPORT char qDumpOutBuffer[1000000];
    #define inBuffer qDumpInBuffer
    #define outBuffer qDumpOutBuffer
#endif
con's avatar
con committed
205 206 207

namespace {

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
208 209
static QByteArray strPtrConst = "* const";

con's avatar
con committed
210 211
static bool isPointerType(const QByteArray &type)
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
212
    return type.endsWith('*') || type.endsWith(strPtrConst);
con's avatar
con committed
213 214
}

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
215
static QByteArray stripPointerType(const QByteArray &_type)
con's avatar
con committed
216
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
217 218
    QByteArray type = _type;
    if (type.endsWith('*'))
con's avatar
con committed
219
        type.chop(1);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
220
    if (type.endsWith(strPtrConst))
con's avatar
con committed
221 222 223 224 225 226 227
        type.chop(7);
    if (type.endsWith(' '))
        type.chop(1);
    return type;
}

// This is used to abort evaluation of custom data dumpers in a "coordinated"
228
// way. Abortion will happen at the latest when we try to access a non-initialized
con's avatar
con committed
229
// non-trivial object, so there is no way to prevent this from occuring at all
230 231 232 233 234 235 236 237 238
// conceptionally.  Ideally, if there is API to check memory access, it should
// be used to terminate nicely, especially with CDB.
// 1) Gdb will catch SIGSEGV and return to the calling frame.
//    This is just fine provided we only _read_ memory in the custom handlers
//    below.
// 2) For MSVC/CDB, exceptions must be handled in the dumper, which is
//    achieved using __try/__except. The exception will be reported in the
//    debugger, which will then execute a 'gN' command, passing handling back
//    to the __except clause.
con's avatar
con committed
239 240 241 242 243 244 245 246 247 248

volatile int qProvokeSegFaultHelper;

static const void *addOffset(const void *p, int offset)
{
    return offset + reinterpret_cast<const char *>(p);
}

static const void *skipvtable(const void *p)
{
249
    return sizeof(void *) + reinterpret_cast<const char *>(p);
con's avatar
con committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

static const void *deref(const void *p)
{
    return *reinterpret_cast<const char* const*>(p);
}

static const void *dfunc(const void *p)
{
    return deref(skipvtable(p));
}

static bool isEqual(const char *s, const char *t)
{
    return qstrcmp(s, t) == 0;
}

static bool startsWith(const char *s, const char *t)
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
269 270 271 272
    while (char c = *t++)
        if (c != *s++)
            return false;
    return true;
con's avatar
con committed
273 274
}

hjk's avatar
hjk committed
275 276 277 278
static bool couldBePointer(const void *p)
{
    // we assume valid pointer to be 4-aligned at least.
    // So use this check only when this is guaranteed.
279
    // FIXME: this breaks e.g. in the QString dumper...
hjk's avatar
hjk committed
280
    const quintptr d = quintptr(p);
281 282 283
    //qDebug() << "CHECKING : " << p << ((d & 3) == 0 && (d > 1000 || d == 0));
    //return (d & 3) == 0 && (d > 1000 || d == 0);
    return d > 1000 || d == 0;
hjk's avatar
hjk committed
284 285
}

286 287 288 289
// Check memory for read access and provoke segfault if nothing else helps.
// On Windows, try to be less crash-prone by checking memory using WinAPI

#ifdef Q_OS_WIN
hjk's avatar
hjk committed
290 291 292 293 294 295 296 297 298 299 300 301

#    define qCheckAccess(d) do { \
        if (IsBadReadPtr(d, 1)) \
            return; \
         qProvokeSegFaultHelper = *(char*)d; \
    } while (0)
#    define qCheckPointer(d) do { \
        if (d && IsBadReadPtr(d, 1)) \
            return; \
        if (d) qProvokeSegFaultHelper = *(char*)d; \
    } while (0)

302
#else
hjk's avatar
hjk committed
303 304 305 306 307 308 309 310 311 312 313 314 315

#    define qCheckAccess(d) do { \
        if (!couldBePointer(d) && d != 0) \
            return; \
        qProvokeSegFaultHelper = *(char*)d; \
     } while (0)
#    define qCheckPointer(d) do { \
        if (!couldBePointer(d)) \
            return; \
        if (d) \
            qProvokeSegFaultHelper = *(char*)d; \
     } while (0)

316
#endif
con's avatar
con committed
317

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
318
#ifdef QT_NAMESPACE
con's avatar
con committed
319 320
const char *stripNamespace(const char *type)
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
321
    static const size_t nslen = strlen(NS);
con's avatar
con committed
322 323
    return startsWith(type, NS) ? type + nslen : type;
}
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
324 325 326 327 328 329
#else
inline const char *stripNamespace(const char *type)
{
    return type;
}
#endif
con's avatar
con committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

static bool isSimpleType(const char *type)
{
    switch (type[0]) {
        case 'c':
            return isEqual(type, "char");
        case 'd':
            return isEqual(type, "double");
        case 'f':
            return isEqual(type, "float");
        case 'i':
            return isEqual(type, "int");
        case 'l':
            return isEqual(type, "long") || startsWith(type, "long ");
        case 's':
345 346
            return isEqual(type, "short") || startsWith(type, "short ")
                || isEqual(type, "signed") || startsWith(type, "signed ");
con's avatar
con committed
347 348 349 350 351 352
        case 'u':
            return isEqual(type, "unsigned") || startsWith(type, "unsigned ");
    }
    return false;
}

353 354 355 356 357 358 359 360 361
static bool isStringType(const char *type)
{
    return isEqual(type, NS"QString")
        || isEqual(type, NS"QByteArray")
        || isEqual(type, "std::string")
        || isEqual(type, "std::wstring")
        || isEqual(type, "wstring");
}

con's avatar
con committed
362 363 364 365 366 367 368 369 370 371
static bool isMovableType(const char *type)
{
    if (isPointerType(type))
        return true;

    if (isSimpleType(type))
        return true;

    type = stripNamespace(type);

372
    switch (type[1]) {
con's avatar
con committed
373 374 375 376 377
        case 'B':
            return isEqual(type, "QBrush")
                || isEqual(type, "QBitArray")
                || isEqual(type, "QByteArray") ;
        case 'C':
378 379
            return isEqual(type, "QCustomTypeInfo")
                || isEqual(type, "QChar");
con's avatar
con committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
        case 'D':
            return isEqual(type, "QDate")
                || isEqual(type, "QDateTime");
        case 'F':
            return isEqual(type, "QFileInfo")
                || isEqual(type, "QFixed")
                || isEqual(type, "QFixedPoint")
                || isEqual(type, "QFixedSize");
        case 'H':
            return isEqual(type, "QHashDummyValue");
        case 'I':
            return isEqual(type, "QIcon")
                || isEqual(type, "QImage");
        case 'L':
            return isEqual(type, "QLine")
                || isEqual(type, "QLineF")
396
                || isEqual(type, "QLatin1Char")
con's avatar
con committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
                || isEqual(type, "QLocal");
        case 'M':
            return isEqual(type, "QMatrix")
                || isEqual(type, "QModelIndex");
        case 'P':
            return isEqual(type, "QPoint")
                || isEqual(type, "QPointF")
                || isEqual(type, "QPen")
                || isEqual(type, "QPersistentModelIndex");
        case 'R':
            return isEqual(type, "QResourceRoot")
                || isEqual(type, "QRect")
                || isEqual(type, "QRectF")
                || isEqual(type, "QRegExp");
        case 'S':
            return isEqual(type, "QSize")
                || isEqual(type, "QSizeF")
                || isEqual(type, "QString");
        case 'T':
            return isEqual(type, "QTime")
                || isEqual(type, "QTextBlock");
        case 'U':
            return isEqual(type, "QUrl");
        case 'V':
            return isEqual(type, "QVariant");
        case 'X':
            return isEqual(type, "QXmlStreamAttribute")
                || isEqual(type, "QXmlStreamNamespaceDeclaration")
                || isEqual(type, "QXmlStreamNotationDeclaration")
                || isEqual(type, "QXmlStreamEntityDeclaration");
    }
    return false;
}

struct QDumper
{
    explicit QDumper();
    ~QDumper();
435

436
    // direct write to the output
hjk's avatar
hjk committed
437 438 439 440 441 442 443 444
    QDumper &put(long c);
    QDumper &put(int i);
    QDumper &put(double d);
    QDumper &put(float d);
    QDumper &put(unsigned long c);
    QDumper &put(unsigned int i);
    QDumper &put(const void *p);
    QDumper &put(qulonglong c);
445
    QDumper &put(long long c);
hjk's avatar
hjk committed
446 447 448 449 450 451
    QDumper &put(const char *str);
    QDumper &put(const QByteArray &ba);
    QDumper &put(const QString &str);
    QDumper &put(char c);

    // convienience functions for writing key="value" pairs:
452
    template <class Value>
hjk's avatar
hjk committed
453 454 455 456 457 458
    void putItem(const char *name, const Value &value)
    {
        putCommaIfNeeded();
        put(name).put('=').put('"').put(value).put('"');
    }

459 460 461 462 463
    void putItem(const char *name, const char *value, const char *setvalue)
    {
        if (!isEqual(value, setvalue))
            putItem(name, value);
    }
hjk's avatar
hjk committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    // convienience functions for writing typical properties.
    // roughly equivalent to
    //   beginHash();
    //      putItem("name", name);
    //      putItem("value", value);
    //      putItem("type", NS"QString");
    //      putItem("numchild", "0");
    //      putItem("valueencoded", "2");
    //   endHash();
    void putHash(const char *name, const QString &value);
    void putHash(const char *name, const QByteArray &value);
    void putHash(const char *name, int value);
    void putHash(const char *name, long value);
    void putHash(const char *name, bool value);
    void putHash(const char *name, QChar value);
479 480
    void putHash(const char *name, float value);
    void putHash(const char *name, double value);
hjk's avatar
hjk committed
481 482 483 484

    void beginHash(); // start of data hash output
    void endHash(); // start of data hash output

485
    void beginChildren(const char *mainInnerType = 0); // start of children list
hjk's avatar
hjk committed
486 487 488 489
    void endChildren(); // end of children list

    void beginItem(const char *name); // start of named item, ready to accept value
    void endItem(); // end of named item, used after value output is complete
490 491 492

    // convienience for putting "<n items>"
    void putItemCount(const char *name, int count);
hjk's avatar
hjk committed
493 494
    void putCommaIfNeeded();
    // convienience function for writing the last item of an abbreviated list
495
    void putEllipsis();
con's avatar
con committed
496 497
    void disarm();

hjk's avatar
hjk committed
498 499
    void putBase64Encoded(const char *buf, int n);
    void checkFill();
con's avatar
con committed
500 501 502 503

    // the dumper arguments
    int protocolVersion;   // dumper protocol version
    int token;             // some token to show on success
504
    const char *outerType; // object type
con's avatar
con committed
505 506
    const char *iname;     // object name used for display
    const char *exp;       // object expression
507
    const char *innerType; // 'inner type' for class templates
con's avatar
con committed
508 509 510 511 512 513 514 515 516
    const void *data;      // pointer to raw data
    bool dumpChildren;     // do we want to see children?

    // handling of nested templates
    void setupTemplateParameters();
    enum { maxTemplateParameters = 10 };
    const char *templateParameters[maxTemplateParameters + 1];

    // internal state
hjk's avatar
hjk committed
517 518
    int extraInt[4];

con's avatar
con committed
519
    bool success;          // are we finished?
hjk's avatar
hjk committed
520
    bool full;
con's avatar
con committed
521
    int pos;
522 523 524

    const char *currentChildType;
    const char *currentChildNumChild;
con's avatar
con committed
525 526 527 528 529 530
};


QDumper::QDumper()
{
    success = false;
hjk's avatar
hjk committed
531
    full = false;
532
    outBuffer[0] = 'f'; // marks output as 'wrong'
hjk's avatar
hjk committed
533
    pos = 1;
534 535
    currentChildType = 0;
    currentChildNumChild = 0;
con's avatar
con committed
536 537 538 539
}

QDumper::~QDumper()
{
540
    outBuffer[pos++] = '\0';
hjk's avatar
hjk committed
541
    if (success)
542
        outBuffer[0] = (full ? '+' : 't');
con's avatar
con committed
543 544 545 546
}

void QDumper::setupTemplateParameters()
{
547
    char *s = const_cast<char *>(innerType);
con's avatar
con committed
548

hjk's avatar
hjk committed
549
    int templateParametersCount = 1;
con's avatar
con committed
550 551 552 553 554 555 556 557 558 559 560 561 562
    templateParameters[0] = s;
    for (int i = 1; i != maxTemplateParameters + 1; ++i)
        templateParameters[i] = 0;

    while (*s) {
        while (*s && *s != '@')
            ++s;
        if (*s) {
            *s = '\0';
            ++s;
            templateParameters[templateParametersCount++] = s;
        }
    }
hjk's avatar
hjk committed
563 564 565 566 567 568 569 570 571 572
    while (templateParametersCount < maxTemplateParameters)
        templateParameters[templateParametersCount++] = 0;
}

QDumper &QDumper::put(char c)
{
    checkFill();
    if (!full)
        outBuffer[pos++] = c;
    return *this;
con's avatar
con committed
573 574
}

hjk's avatar
hjk committed
575
QDumper &QDumper::put(unsigned long long c)
con's avatar
con committed
576 577
{
    checkFill();
578
    pos += sprintf(outBuffer + pos, "%llu", c);
con's avatar
con committed
579 580 581
    return *this;
}

582 583 584 585 586 587 588
QDumper &QDumper::put(long long c)
{
    checkFill();
    pos += sprintf(outBuffer + pos, "%lld", c);
    return *this;
}

hjk's avatar
hjk committed
589
QDumper &QDumper::put(unsigned long c)
con's avatar
con committed
590 591
{
    checkFill();
592
    pos += sprintf(outBuffer + pos, "%lu", c);
con's avatar
con committed
593 594 595
    return *this;
}

hjk's avatar
hjk committed
596
QDumper &QDumper::put(float d)
con's avatar
con committed
597 598
{
    checkFill();
599
    pos += sprintf(outBuffer + pos, "%f", d);
con's avatar
con committed
600 601 602
    return *this;
}

hjk's avatar
hjk committed
603
QDumper &QDumper::put(double d)
con's avatar
con committed
604 605
{
    checkFill();
606
    pos += sprintf(outBuffer + pos, "%f", d);
con's avatar
con committed
607 608 609
    return *this;
}

hjk's avatar
hjk committed
610
QDumper &QDumper::put(unsigned int i)
con's avatar
con committed
611 612
{
    checkFill();
613
    pos += sprintf(outBuffer + pos, "%u", i);
con's avatar
con committed
614 615 616
    return *this;
}

hjk's avatar
hjk committed
617
QDumper &QDumper::put(long c)
con's avatar
con committed
618 619
{
    checkFill();
620
    pos += sprintf(outBuffer + pos, "%ld", c);
con's avatar
con committed
621 622 623
    return *this;
}

hjk's avatar
hjk committed
624
QDumper &QDumper::put(int i)
con's avatar
con committed
625 626
{
    checkFill();
627
    pos += sprintf(outBuffer + pos, "%d", i);
con's avatar
con committed
628 629 630
    return *this;
}

hjk's avatar
hjk committed
631
QDumper &QDumper::put(const void *p)
con's avatar
con committed
632 633
{
    if (p) {
634
        // Pointer is 'long long' on WIN_64, only
635
        static const char *printFormat = sizeof(void *) == sizeof(long) ? "0x%lx" : "0x%llx";
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
636
        pos += sprintf(outBuffer + pos, printFormat, p);
con's avatar
con committed
637
    } else {
638
        pos += sprintf(outBuffer + pos, "<null>");
con's avatar
con committed
639 640 641 642
    }
    return *this;
}

hjk's avatar
hjk committed
643
QDumper &QDumper::put(const char *str)
con's avatar
con committed
644
{
hjk's avatar
hjk committed
645 646 647 648 649
    if (!str)
        return put("<null>");
    while (*str)
        put(*(str++));
    return *this;
con's avatar
con committed
650 651
}

hjk's avatar
hjk committed
652
QDumper &QDumper::put(const QByteArray &ba)
con's avatar
con committed
653
{
hjk's avatar
hjk committed
654 655
    putBase64Encoded(ba.constData(), ba.size());
    return *this;
con's avatar
con committed
656 657
}

hjk's avatar
hjk committed
658 659 660 661 662 663 664 665 666 667 668 669 670
QDumper &QDumper::put(const QString &str)
{
    putBase64Encoded((const char *)str.constData(), 2 * str.size());
    return *this;
}

void QDumper::checkFill()
{
    if (pos >= int(sizeof(outBuffer)) - 100)
        full = true;
}

void QDumper::putCommaIfNeeded()
con's avatar
con committed
671 672 673
{
    if (pos == 0)
        return;
674
    char c = outBuffer[pos - 1];
675
    if (c == '}' || c == '"' || c == ']')
con's avatar
con committed
676 677 678
        put(',');
}

679
void QDumper::putBase64Encoded(const char *buf, int n)
con's avatar
con committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
{
    const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
                            "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
    const char padchar = '=';
    int padlen = 0;

    //int tmpsize = ((n * 4) / 3) + 3;

    int i = 0;
    while (i < n) {
        int chunk = 0;
        chunk |= int(uchar(buf[i++])) << 16;
        if (i == n) {
            padlen = 2;
        } else {
            chunk |= int(uchar(buf[i++])) << 8;
            if (i == n)
                padlen = 1;
            else
                chunk |= int(uchar(buf[i++]));
        }

        int j = (chunk & 0x00fc0000) >> 18;
        int k = (chunk & 0x0003f000) >> 12;
        int l = (chunk & 0x00000fc0) >> 6;
        int m = (chunk & 0x0000003f);
        put(alphabet[j]);
        put(alphabet[k]);
        put(padlen > 1 ? padchar : alphabet[l]);
        put(padlen > 0 ? padchar : alphabet[m]);
    }
}

void QDumper::disarm()
{
    success = true;
}

void QDumper::beginHash()
{
hjk's avatar
hjk committed
720
    putCommaIfNeeded();
con's avatar
con committed
721 722 723 724 725 726 727 728
    put('{');
}

void QDumper::endHash()
{
    put('}');
}

729 730
void QDumper::putEllipsis()
{
hjk's avatar
hjk committed
731
    putCommaIfNeeded();
732
    put("{name=\"<incomplete>\",value=\"\",type=\"").put(innerType).put("\"}");
hjk's avatar
hjk committed
733 734 735 736 737 738
}

void QDumper::putItemCount(const char *name, int count)
{
    putCommaIfNeeded();
    put(name).put("=\"<").put(count).put(" items>\"");
739
}
con's avatar
con committed
740

hjk's avatar
hjk committed
741

con's avatar
con committed
742 743 744 745
//
// Some helpers to keep the dumper code short
//

746 747
void QDumper::beginItem(const char *name)
{
hjk's avatar
hjk committed
748 749
    putCommaIfNeeded();
    put(name).put('=').put('"');
750 751 752 753
}

void QDumper::endItem()
{
hjk's avatar
hjk committed
754
    put('"');
755 756
}

757
void QDumper::beginChildren(const char *mainInnerType)
758
{
759 760
    if (mainInnerType) {
        putItem("childtype", mainInnerType);
761 762
        currentChildType = mainInnerType;
        if (isSimpleType(mainInnerType) || isStringType(mainInnerType)) {
763
            putItem("childnumchild", "0");
764 765
            currentChildNumChild = "0";
        } else if (isPointerType(mainInnerType)) {
766
            putItem("childnumchild", "1");
767 768
            currentChildNumChild = "1";
        }
769 770
    }

hjk's avatar
hjk committed
771 772
    putCommaIfNeeded();
    put("children=[");
773 774
}

hjk's avatar
hjk committed
775 776 777
void QDumper::endChildren()
{
    put(']');
778 779
    currentChildType = 0;
    currentChildNumChild = 0;
hjk's avatar
hjk committed
780
}
con's avatar
con committed
781 782

// simple string property
hjk's avatar
hjk committed
783
void QDumper::putHash(const char *name, const QString &value)
784 785 786 787 788 789 790 791 792 793
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", NS"QString");
    putItem("numchild", "0");
    putItem("valueencoded", "2");
    endHash();
}

hjk's avatar
hjk committed
794
void QDumper::putHash(const char *name, const QByteArray &value)
795 796 797 798 799 800 801 802 803
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", NS"QByteArray");
    putItem("numchild", "0");
    putItem("valueencoded", "1");
    endHash();
}
con's avatar
con committed
804 805

// simple integer property
hjk's avatar
hjk committed
806
void QDumper::putHash(const char *name, int value)
807 808 809 810 811 812 813 814
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", "int");
    putItem("numchild", "0");
    endHash();
}
hjk's avatar
hjk committed
815 816

void QDumper::putHash(const char *name, long value)
817 818 819 820 821 822 823 824
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", "long");
    putItem("numchild", "0");
    endHash();
}
con's avatar
con committed
825

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
void QDumper::putHash(const char *name, float value)
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", "float");
    putItem("numchild", "0");
    endHash();
}

void QDumper::putHash(const char *name, double value)
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", "double");
    putItem("numchild", "0");
    endHash();
}

con's avatar
con committed
846
// simple boolean property
hjk's avatar
hjk committed
847
void QDumper::putHash(const char *name, bool value)
848 849 850 851 852 853 854 855
{
    beginHash();
    putItem("name", name);
    putItem("value", (value ? "true" : "false"));
    putItem("type", "bool");
    putItem("numchild", "0");
    endHash();
}
con's avatar
con committed
856 857

// a single QChar
hjk's avatar
hjk committed
858
void QDumper::putHash(const char *name, QChar value)
859 860 861 862 863 864 865 866 867 868
{
    beginHash();
    putItem("name", name);
    putItem("value", QString(QLatin1String("'%1' (%2, 0x%3)"))
        .arg(value).arg(value.unicode()).arg(value.unicode(), 0, 16));
    putItem("valueencoded", "2");
    putItem("type", NS"QChar");
    putItem("numchild", "0");
    endHash();
}
con's avatar
con committed
869

870
#define DUMPUNKNOWN_MESSAGE "<not in scope>"
871
static void qDumpUnknown(QDumper &d, const char *why = 0)
con's avatar
con committed
872
{
873 874
    if (!why)
        why = DUMPUNKNOWN_MESSAGE;
875
    d.putItem("value", why);
876 877
    d.putItem("valueeditable", "false");
    d.putItem("valueenabled", "false");
878
    d.putItem("numchild", "0", d.currentChildNumChild);
con's avatar
con committed
879 880 881
    d.disarm();
}

882
static void qDumpStdStringValue(QDumper &d, const std::string &str)
883 884 885 886 887 888
{
    d.beginItem("value");
    d.putBase64Encoded(str.c_str(), str.size());
    d.endItem();
    d.putItem("valueencoded", "1");
    d.putItem("type", "std::string");
889
    d.putItem("numchild", "0", d.currentChildNumChild);
890 891
}

892
static void qDumpStdWStringValue(QDumper &d, const std::wstring &str)
893 894 895 896 897
{
    d.beginItem("value");
    d.putBase64Encoded((const char *)str.c_str(), str.size() * sizeof(wchar_t));
    d.endItem();
    d.putItem("valueencoded", (sizeof(wchar_t) == 2 ? "2" : "3"));
898 899
    d.putItem("type", "std::wstring", d.currentChildType);
    d.putItem("numchild", "0", d.currentChildNumChild);
900 901
}

902
// Called by templates, so, not static.
hjk's avatar
hjk committed
903 904 905 906 907 908 909 910
static void qDumpInnerQCharValue(QDumper &d, QChar c, const char *field)
{
    char buf[30];
    sprintf(buf, "'?', ucs=%d", c.unicode());
    if (c.isPrint() && c.unicode() < 127)
        buf[1] = char(c.unicode());
    d.putCommaIfNeeded();
    d.putItem(field, buf);
911
    d.putItem("numchild", "0", d.currentChildNumChild);
hjk's avatar
hjk committed
912 913 914 915 916 917
}

static void qDumpInnerCharValue(QDumper &d, char c, const char *field)
{
    char buf[30];
    sprintf(buf, "'?', ascii=%d", c);
918
    if (QChar(QLatin1Char(c)).isPrint() && c < 127)
hjk's avatar
hjk committed
919 920 921
        buf[1] = c;
    d.putCommaIfNeeded();
    d.putItem(field, buf);
922
    d.putItem("numchild", "0", d.currentChildNumChild);
hjk's avatar
hjk committed
923 924
}

925
void qDumpInnerValueHelper(QDumper &d, const char *type, const void *addr,
926
    const char *field = "value")
con's avatar
con committed
927 928 929
{
    type = stripNamespace(type);
    switch (type[1]) {
hjk's avatar
hjk committed
930
        case 'h':
931
            if (isEqual(type, "char"))
hjk's avatar
hjk committed
932
                qDumpInnerCharValue(d, *(char *)addr, field);
933
            break;
con's avatar
con committed
934 935
        case 'l':
            if (isEqual(type, "float"))
936 937
                d.putItem(field, *(float*)addr);
            break;
con's avatar
con committed
938
        case 'n':
939
            if (isEqual(type, "int"))
940
                d.putItem(field, *(int*)addr);
941
            else if (isEqual(type, "unsigned") || isEqual(type, "unsigned int"))
942
                d.putItem(field, *(unsigned int*)addr);
943
            else if (isEqual(type, "unsigned char"))
hjk's avatar
hjk committed
944
                qDumpInnerCharValue(d, *(char *)addr, field);
945
            else if (isEqual(type, "unsigned long"))
946
                d.putItem(field, *(unsigned long*)addr);
947
            else if (isEqual(type, "unsigned long long"))
948
                d.putItem(field, *(qulonglong*)addr);
949
            break;
con's avatar
con committed
950
        case 'o':
951
            if (isEqual(type, "bool")) {
con's avatar
con committed
952
                switch (*(bool*)addr) {
953 954 955
                case 0: d.putItem(field, "false"); break;
                case 1: d.putItem(field, "true"); break;
                default: d.putItem(field, *(bool*)addr); break;
con's avatar
con committed
956
                }
957
            } else if (isEqual(type, "double"))
958
                d.putItem(field, *(double*)addr);
959
            else if (isEqual(type, "long"))
960
                d.putItem(field, *(long*)addr);
961
            else if (isEqual(type, "long long"))
962
                d.putItem(field, *(qulonglong*)addr);
963
            break;
con's avatar
con committed
964 965
        case 'B':
            if (isEqual(type, "QByteArray")) {
hjk's avatar
hjk committed
966 967
                d.putCommaIfNeeded();
                d.put(field).put("encoded=\"1\",");
968
                d.putItem(field, *(QByteArray*)addr);
con's avatar
con committed
969
            }
970
            break;
971
        case 'C':
972
            if (isEqual(type, "QChar"))
hjk's avatar
hjk committed
973
                qDumpInnerQCharValue(d, *(QChar*)addr, field);
974
            break;
con's avatar
con committed
975 976 977
        case 'L':
            if (startsWith(type, "QList<")) {
                const QListData *ldata = reinterpret_cast<const QListData*>(addr);
978
                d.putItemCount("value", ldata->size());
979
                d.putItem("valueeditable", "false");
980
                d.putItem("numchild", ldata->size());
con's avatar
con committed
981
            }
982
            break;
con's avatar
con committed
983
        case 'O':
984
            #ifndef QT_BOOTSTRAPPED
con's avatar
con committed
985 986 987
            if (isEqual(type, "QObject *")) {
                if (addr) {
                    const QObject *ob = reinterpret_cast<const QObject *>(addr);
988 989 990 991 992
                    d.putItem("addr", ob);
                    d.putItem("value", ob->objectName());
                    d.putItem("valueencoded", "2");
                    d.putItem("type", NS"QObject");
                    d.putItem("displayedtype", ob->metaObject()->className());
993
                    d.putItem("numchild", 1);
con's avatar
con committed
994
                } else {
995 996
                    d.putItem("value", "0x0");
                    d.putItem("type", NS"QObject *");
997
                    d.putItem("numchild", 0);
con's avatar
con committed
998 999
                }
            }
1000
            #endif
1001
            break;
con's avatar
con committed
1002 1003
        case 'S':
            if (isEqual(type, "QString")) {
hjk's avatar
hjk committed
1004
                d.putCommaIfNeeded();
1005
                d.putItem(field, *(QString*)addr);
hjk's avatar
hjk committed
1006
                d.put(',').put(field).put("encoded=\"2\"");
con's avatar
con committed
1007
            }
1008
            break;
1009 1010
        case 't':
            if (isEqual(type, "std::string")
1011
                || isEqual(type, stdStringTypeC)) {
1012
                d.putCommaIfNeeded();
1013 1014
                qDumpStdStringValue(d, *reinterpret_cast<const std::string*>(addr));
            } else if (isEqual(type, "std::wstring")
1015
                || isEqual(type, stdWideStringTypeUShortC)) {
1016
                qDumpStdWStringValue(d, *reinterpret_cast<const std::wstring*>(addr));
1017
            }
1018
            break;
con's avatar
con committed
1019
        default:
1020
            break;
con's avatar
con committed
1021 1022 1023
    }
}

1024
static void qDumpInnerValue(QDumper &d, const char *type, const void *addr)
con's avatar
con committed
1025
{
1026
    d.putItem("addr", addr);
1027
    d.putItem("type", type, d.currentChildType);
con's avatar
con committed
1028 1029

    if (!type[0])
1030
        return;
con's avatar
con committed
1031

1032
    return qDumpInnerValueHelper(d, type, addr);
con's avatar
con committed
1033 1034
}

1035
static void qDumpInnerValueOrPointer(QDumper &d,
1036 1037 1038 1039
    const char *type, const char *strippedtype, const void *addr)
{
    if (strippedtype) {
        if (deref(addr)) {
1040 1041
            d.putItem("addr", deref(addr));
            d.putItem("saddr", deref(addr));
1042
            d.putItem("type", strippedtype, d.currentChildType);
1043 1044 1045 1046 1047 1048
            qDumpInnerValueHelper(d, strippedtype, deref(addr));
        } else {
            d.putItem("addr", addr);
            d.putItem("type", strippedtype);
            d.putItem("value", "<null>");
            d.putItem("numchild", "0");
1049
        }
1050
    } else {
1051
        d.putItem("addr", addr);
1052
        d.putItem("type", type, d.currentChildType);
1053
        qDumpInnerValueHelper(d, type, addr);
1054 1055 1056
    }
}

con's avatar
con committed
1057 1058
//////////////////////////////////////////////////////////////////////////////

1059
#ifndef QT_BOOTSTRAPPED
1060 1061 1062 1063
struct ModelIndex { int r; int c; void *p; void *m; };

static void qDumpQAbstractItem(QDumper &d)
{
1064
    QModelIndex mi;
1065 1066
    {
       ModelIndex *mm = reinterpret_cast<ModelIndex *>(&mi);
1067
       mm->r = mm->c = 0;
1068
       mm->p = mm->m = 0;       
1069
       static const char *printFormat = sizeof(void *) == sizeof(long) ?
1070
                                        "%d,%d,0x%lx,0x%lx" : "%d,%d,0x%llx,0x%llx";
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
1071
       sscanf(d.templateParameters[0], printFormat, &mm->r, &mm->c, &mm->p, &mm->m);
1072
    }
1073 1074 1075 1076 1077 1078 1079
    const QAbstractItemModel *m = mi.model();
    const int rowCount = m->rowCount(mi);
    if (rowCount < 0)
        return;
    const int columnCount = m->columnCount(mi);
    if (columnCount < 0)
        return;
1080 1081
    d.putItem("type", NS"QAbstractItem");
    d.beginItem("addr");
1082 1083
        d.put('$').put(mi.row()).put(',').put(mi.column()).put(',')
            .put(mi.internalPointer()).put(',').put(mi.model());
1084
    d.endItem();
hjk's avatar
hjk committed
1085
    //d.putItem("value", "(").put(rowCount).put(",").put(columnCount).put(")");
1086 1087
    d.putItem("value", m->data(mi, Qt::DisplayRole).toString());
    d.putItem("valueencoded", "2");
1088
    d.putItem("numchild", rowCount * columnCount);
1089
    if (d.dumpChildren) {
hjk's avatar
hjk committed
1090
        d.beginChildren();
1091 1092 1093 1094
        for (int row = 0; row < rowCount; ++row) {
            for (int column = 0; column < columnCount; ++column) {
                QModelIndex child = m->index(row, column, mi);
                d.beginHash();