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
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 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
#   include <QtGui/QRegion>
81 82
#   include <QtGui/QPixmap>
#   include <QtGui/QWidget>
83 84 85 86
#   include <QtGui/QFont>
#   include <QtGui/QColor>
#   include <QtGui/QKeySequence>
#   include <QtGui/QSizePolicy>
87 88
#endif

89 90
#endif // QT_BOOTSTRAPPED

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

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

101 102 103
#ifdef QT_BOOTSTRAPPED

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

#else

#   include "gdbmacros_p.h"

#endif // QT_BOOTSTRAPPED


int qtGhVersion = QT_VERSION;

con's avatar
con committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/*!
  \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()}.

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

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


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

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


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

  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".
162
        A value of "3" is used for base64-encoded UCS4, "2" denotes
con's avatar
con committed
163 164 165 166 167 168
        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
169
  about these children. In this case the dumper should use something like this:
con's avatar
con committed
170 171 172

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

  */

#if defined(QT_BEGIN_NAMESPACE)
QT_BEGIN_NAMESPACE
#endif

184 185 186
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
187 188 189 190 191
#if defined(QT_BEGIN_NAMESPACE)
QT_END_NAMESPACE
#endif


192 193 194
// This can be mangled typenames of nested templates, each char-by-char
// comma-separated integer list...
// The output buffer.
195 196 197 198 199 200 201 202 203 204 205
#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
206 207 208

namespace {

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

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

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

// This is used to abort evaluation of custom data dumpers in a "coordinated"
229
// way. Abortion will happen at the latest when we try to access a non-initialized
230
// non-trivial object, so there is no way to prevent this from occurring at all
231 232 233 234 235 236 237 238 239
// 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
240 241 242 243 244 245 246 247 248 249

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

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
270 271 272 273
    while (char c = *t++)
        if (c != *s++)
            return false;
    return true;
con's avatar
con committed
274 275
}

hjk's avatar
hjk committed
276 277 278 279
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.
280
    // FIXME: this breaks e.g. in the QString dumper...
hjk's avatar
hjk committed
281
    const quintptr d = quintptr(p);
282 283 284
    //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
285 286
}

287 288 289 290
// 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
291 292 293 294 295 296 297 298 299 300 301 302

#    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)

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

#    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)

317
#endif
con's avatar
con committed
318

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

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':
346 347
            return isEqual(type, "short") || startsWith(type, "short ")
                || isEqual(type, "signed") || startsWith(type, "signed ");
con's avatar
con committed
348 349 350 351 352 353
        case 'u':
            return isEqual(type, "unsigned") || startsWith(type, "unsigned ");
    }
    return false;
}

354 355 356 357 358 359 360 361 362
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
363 364 365 366 367 368 369 370 371 372
static bool isMovableType(const char *type)
{
    if (isPointerType(type))
        return true;

    if (isSimpleType(type))
        return true;

    type = stripNamespace(type);

373
    switch (type[1]) {
con's avatar
con committed
374 375 376 377 378
        case 'B':
            return isEqual(type, "QBrush")
                || isEqual(type, "QBitArray")
                || isEqual(type, "QByteArray") ;
        case 'C':
379 380
            return isEqual(type, "QCustomTypeInfo")
                || isEqual(type, "QChar");
con's avatar
con committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
        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")
397
                || isEqual(type, "QLatin1Char")
con's avatar
con committed
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 435
                || 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();
436

437
    // direct write to the output
hjk's avatar
hjk committed
438 439 440 441 442 443 444 445
    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);
446
    QDumper &put(long long c);
hjk's avatar
hjk committed
447 448 449 450 451 452
    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:
453
    template <class Value>
hjk's avatar
hjk committed
454 455 456 457 458 459
    void putItem(const char *name, const Value &value)
    {
        putCommaIfNeeded();
        put(name).put('=').put('"').put(value).put('"');
    }

460 461 462 463 464
    void putItem(const char *name, const char *value, const char *setvalue)
    {
        if (!isEqual(value, setvalue))
            putItem(name, value);
    }
hjk's avatar
hjk committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    // 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);
480 481
    void putHash(const char *name, float value);
    void putHash(const char *name, double value);
482
    void putStringValue(const QString &value);
hjk's avatar
hjk committed
483 484 485 486

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

487
    void beginChildren(const char *mainInnerType = 0); // start of children list
hjk's avatar
hjk committed
488 489 490 491
    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
492 493 494

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

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

    // the dumper arguments
    int protocolVersion;   // dumper protocol version
    int token;             // some token to show on success
506
    const char *outerType; // object type
con's avatar
con committed
507 508
    const char *iname;     // object name used for display
    const char *exp;       // object expression
509
    const char *innerType; // 'inner type' for class templates
con's avatar
con committed
510 511 512 513 514 515 516 517 518
    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
519 520
    int extraInt[4];

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

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


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

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

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

hjk's avatar
hjk committed
551
    int templateParametersCount = 1;
con's avatar
con committed
552 553 554 555 556 557 558 559 560 561 562 563 564
    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
565 566 567 568 569 570 571 572 573 574
    while (templateParametersCount < maxTemplateParameters)
        templateParameters[templateParametersCount++] = 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

hjk's avatar
hjk committed
660 661 662 663 664 665 666 667 668 669 670 671 672
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
673 674 675
{
    if (pos == 0)
        return;
676
    char c = outBuffer[pos - 1];
677
    if (c == '}' || c == '"' || c == ']')
con's avatar
con committed
678 679 680
        put(',');
}

681
void QDumper::putBase64Encoded(const char *buf, int n)
con's avatar
con committed
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
{
    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]);
    }
}

715 716 717
void QDumper::putStringValue(const QString &str)
{
    if (str.isNull()) {
718 719 720 721
        beginItem("value");
        putBase64Encoded("\"\" (null)", 9);
        endItem();
        putItem("valueencoded", "5");
722 723 724 725 726 727
    } else {
        putItem("value", str);
        putItem("valueencoded", "2");
    }
}

con's avatar
con committed
728 729 730 731 732 733 734
void QDumper::disarm()
{
    success = true;
}

void QDumper::beginHash()
{
hjk's avatar
hjk committed
735
    putCommaIfNeeded();
con's avatar
con committed
736 737 738 739 740 741 742 743
    put('{');
}

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

744 745
void QDumper::putEllipsis()
{
hjk's avatar
hjk committed
746
    putCommaIfNeeded();
747
    put("{name=\"<incomplete>\",value=\"\",type=\"").put(innerType).put("\"}");
hjk's avatar
hjk committed
748 749 750 751 752 753
}

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

hjk's avatar
hjk committed
756

con's avatar
con committed
757 758 759 760
//
// Some helpers to keep the dumper code short
//

761 762
void QDumper::beginItem(const char *name)
{
hjk's avatar
hjk committed
763 764
    putCommaIfNeeded();
    put(name).put('=').put('"');
765 766 767 768
}

void QDumper::endItem()
{
hjk's avatar
hjk committed
769
    put('"');
770 771
}

772
void QDumper::beginChildren(const char *mainInnerType)
773
{
774 775
    if (mainInnerType) {
        putItem("childtype", mainInnerType);
776 777
        currentChildType = mainInnerType;
        if (isSimpleType(mainInnerType) || isStringType(mainInnerType)) {
778
            putItem("childnumchild", "0");
779 780
            currentChildNumChild = "0";
        } else if (isPointerType(mainInnerType)) {
781
            putItem("childnumchild", "1");
782 783
            currentChildNumChild = "1";
        }
784 785
    }

hjk's avatar
hjk committed
786 787
    putCommaIfNeeded();
    put("children=[");
788 789
}

hjk's avatar
hjk committed
790 791 792
void QDumper::endChildren()
{
    put(']');
793 794
    currentChildType = 0;
    currentChildNumChild = 0;
hjk's avatar
hjk committed
795
}
con's avatar
con committed
796 797

// simple string property
hjk's avatar
hjk committed
798
void QDumper::putHash(const char *name, const QString &value)
799 800 801
{
    beginHash();
    putItem("name", name);
802
    putStringValue(value);
803 804 805 806 807
    putItem("type", NS"QString");
    putItem("numchild", "0");
    endHash();
}

hjk's avatar
hjk committed
808
void QDumper::putHash(const char *name, const QByteArray &value)
809 810 811 812 813 814 815 816 817
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", NS"QByteArray");
    putItem("numchild", "0");
    putItem("valueencoded", "1");
    endHash();
}
con's avatar
con committed
818 819

// simple integer property
hjk's avatar
hjk committed
820
void QDumper::putHash(const char *name, int value)
821 822 823 824 825 826 827 828
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", "int");
    putItem("numchild", "0");
    endHash();
}
hjk's avatar
hjk committed
829 830

void QDumper::putHash(const char *name, long value)
831 832 833 834 835 836 837 838
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", "long");
    putItem("numchild", "0");
    endHash();
}
con's avatar
con committed
839

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
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
860
// simple boolean property
hjk's avatar
hjk committed
861
void QDumper::putHash(const char *name, bool value)
862 863 864 865 866 867 868 869
{
    beginHash();
    putItem("name", name);
    putItem("value", (value ? "true" : "false"));
    putItem("type", "bool");
    putItem("numchild", "0");
    endHash();
}
con's avatar
con committed
870 871

// a single QChar
hjk's avatar
hjk committed
872
void QDumper::putHash(const char *name, QChar value)
873 874 875
{
    beginHash();
    putItem("name", name);
876
    putStringValue(QString(QLatin1String("'%1' (%2, 0x%3)"))
877 878 879 880 881
        .arg(value).arg(value.unicode()).arg(value.unicode(), 0, 16));
    putItem("type", NS"QChar");
    putItem("numchild", "0");
    endHash();
}
con's avatar
con committed
882

883
#define DUMPUNKNOWN_MESSAGE "<not in scope>"
884
static void qDumpUnknown(QDumper &d, const char *why = 0)
con's avatar
con committed
885
{
886 887
    if (!why)
        why = DUMPUNKNOWN_MESSAGE;
888
    d.putItem("value", why);
889 890
    d.putItem("valueeditable", "false");
    d.putItem("valueenabled", "false");
891
    d.putItem("numchild", "0", d.currentChildNumChild);
con's avatar
con committed
892 893 894
    d.disarm();
}

895
static void qDumpStdStringValue(QDumper &d, const std::string &str)
896 897 898 899 900 901
{
    d.beginItem("value");
    d.putBase64Encoded(str.c_str(), str.size());
    d.endItem();
    d.putItem("valueencoded", "1");
    d.putItem("type", "std::string");
902
    d.putItem("numchild", "0", d.currentChildNumChild);
903 904
}

905
static void qDumpStdWStringValue(QDumper &d, const std::wstring &str)
906 907 908 909 910
{
    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"));
911 912
    d.putItem("type", "std::wstring", d.currentChildType);
    d.putItem("numchild", "0", d.currentChildNumChild);
913 914
}

915
// Called by templates, so, not static.
hjk's avatar
hjk committed
916 917 918 919 920 921 922 923
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);
924
    d.putItem("numchild", "0", d.currentChildNumChild);
hjk's avatar
hjk committed
925 926 927 928 929 930
}

static void qDumpInnerCharValue(QDumper &d, char c, const char *field)
{
    char buf[30];
    sprintf(buf, "'?', ascii=%d", c);
931
    if (QChar(QLatin1Char(c)).isPrint() && c < 127)
hjk's avatar
hjk committed
932 933 934
        buf[1] = c;
    d.putCommaIfNeeded();
    d.putItem(field, buf);
935
    d.putItem("numchild", "0", d.currentChildNumChild);
hjk's avatar
hjk committed
936 937
}

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

1037
static void qDumpInnerValue(QDumper &d, const char *type, const void *addr)
con's avatar
con committed
1038
{
1039
    d.putItem("addr", addr);
1040
    d.putItem("type", type, d.currentChildType);
con's avatar
con committed
1041 1042

    if (!type[0])
1043
        return;
con's avatar
con committed
1044

1045
    return qDumpInnerValueHelper(d, type, addr);
con's avatar
con committed
1046 1047
}

1048
static void qDumpInnerValueOrPointer(QDumper &d,
1049 1050 1051 1052
    const char *type, const char *strippedtype, const void *addr)
{
    if (strippedtype) {
        if (deref(addr)) {
1053 1054
            d.putItem("addr", deref(addr));
            d.putItem("saddr", deref(addr));
1055
            d.putItem("type", strippedtype, d.currentChildType);
1056 1057 1058 1059 1060 1061
            qDumpInnerValueHelper(d, strippedtype, deref(addr));
        } else {
            d.putItem("addr", addr);
            d.putItem("type", strippedtype);
            d.putItem("value", "<null>");
            d.putItem("numchild", "0");
1062
        }
1063
    } else {
1064
        d.putItem("addr", addr);
1065
        d.putItem("type", type, d.currentChildType);
1066
        qDumpInnerValueHelper(d, type, addr);
1067 1068 1069
    }
}

con's avatar
con committed
1070 1071
//////////////////////////////////////////////////////////////////////////////

1072
#ifndef QT_BOOTSTRAPPED
1073 1074 1075 1076
struct ModelIndex { int r; int c; void *p; void *m; };

static void qDumpQAbstractItem(QDumper &d)
{
1077
    QModelIndex mi;
1078 1079
    {
       ModelIndex *mm = reinterpret_cast<ModelIndex *>(&mi);
1080
       mm->r = mm->c = 0;
1081
       mm->p = mm->m = 0;
1082
       static const char *printFormat = sizeof(void *) == sizeof(long) ?
1083
                                        "%d,%d,0x%lx,0x%lx" : "%d,%d,0x%llx,0x%llx";
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
1084
       sscanf(d.templateParameters[0], printFormat, &mm->r, &mm->c, &mm->p, &mm->m);
1085
    }
1086 1087 1088 1089 1090 1091 1092
    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;
1093 1094
    d.putItem("type", NS"QAbstractItem");
    d.beginItem("addr");
Oswald Buddenhagen's avatar