gdbmacros.cpp 127 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
**
con's avatar
con committed
9
** No Commercial Usage
con's avatar
con committed
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
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
**
con's avatar
con committed
25
26
27
28
29
30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
hjk's avatar
hjk committed
31
**
32
**************************************************************************/
con's avatar
con committed
33
34
35

#include <qglobal.h>

36
37
38
39
40
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
41
#include <QtCore/QHash>
42
#include <QtCore/QLinkedList>
43
#include <QtCore/QList>
44
#include <QtCore/QQueue>
45
46
#include <QtCore/QLocale>
#include <QtCore/QMap>
47
#include <QtCore/QMetaEnum>
48
49
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
50
51
#include <QtCore/QPoint>
#include <QtCore/QPointF>
52
53
#include <QtCore/QPointer>
#include <QtCore/QRect>
54
#include <QtCore/QRectF>
hjk's avatar
hjk committed
55
#include <QtCore/QStack>
56
57
58
59
60
61
62
63
64
65
66
#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>
67
68
69
70

#if QT_VERSION >= 0x040500
#include <QtCore/QSharedPointer>
#include <QtCore/QSharedDataPointer>
71
#include <QtCore/QSharedData>
72
#include <QtCore/QWeakPointer>
73
#endif
con's avatar
con committed
74

75
76
77
78
79
80
81
#ifndef USE_QT_GUI
#   ifdef QT_GUI_LIB
#        define USE_QT_GUI 1
#   endif
#endif

#if USE_QT_GUI
82
#   include <QtGui/QApplication>
83
#   include <QtGui/QImage>
84
#   include <QtGui/QRegion>
85
86
#   include <QtGui/QPixmap>
#   include <QtGui/QWidget>
87
88
89
90
#   include <QtGui/QFont>
#   include <QtGui/QColor>
#   include <QtGui/QKeySequence>
#   include <QtGui/QSizePolicy>
91
92
#endif

93
94
#endif // QT_BOOTSTRAPPED

95
96
97
98
#ifdef Q_OS_WIN
#    include <windows.h>
#endif

99
100
101
#include <list>
#include <map>
#include <string>
102
#include <set>
103
104
#include <vector>

105
106
107
#ifdef QT_BOOTSTRAPPED

#   define NS ""
ck's avatar
ck committed
108
109
#   define NSX "'"
#   define NSY "'"
110
111
112
113
114
115
116
117
118
119

#else

#   include "gdbmacros_p.h"

#endif // QT_BOOTSTRAPPED


int qtGhVersion = QT_VERSION;

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

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

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


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

149
150
151
    d.putItem("value", ...);
    d.putItem("type", "Foo");
    d.putItem("numchild", "0");
con's avatar
con committed
152
153
154
  }


155
  'd.putItem(name, value)' roughly expands to:
hjk's avatar
hjk committed
156
        d.put((name)).put("=\"").put(value).put("\"";
con's avatar
con committed
157
158
159
160
161
162
163
164
165

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

  \c{
    if (d.dumpChildren) {
hjk's avatar
hjk committed
177
178
179
        d.beginChildren();
            [...]
        d.endChildren();
con's avatar
con committed
180
181
182
183
184
185
186
187
   }

  */

#if defined(QT_BEGIN_NAMESPACE)
QT_BEGIN_NAMESPACE
#endif

188
189
190
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
191
192
193
194
195
#if defined(QT_BEGIN_NAMESPACE)
QT_END_NAMESPACE
#endif


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

namespace {

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
213
214
static QByteArray strPtrConst = "* const";

con's avatar
con committed
215
216
static bool isPointerType(const QByteArray &type)
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
217
    return type.endsWith('*') || type.endsWith(strPtrConst);
con's avatar
con committed
218
219
}

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

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

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

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
274
275
276
277
    while (char c = *t++)
        if (c != *s++)
            return false;
    return true;
con's avatar
con committed
278
279
}

280
281
282
283
// 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
284
285
286
287
288
289
290
291
292
293
294
295

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

296
#else
hjk's avatar
hjk committed
297
298
299
300
301
302
303
304
305
306
307
308
309

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

310
311
312
313
314
315
316
317
318
319
320
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.
    // FIXME: this breaks e.g. in the QString dumper...
    const quintptr d = quintptr(p);
    //qDebug() << "CHECKING : " << p << ((d & 3) == 0 && (d > 1000 || d == 0));
    //return (d & 3) == 0 && (d > 1000 || d == 0);
    return d > 1000 || d == 0;
}

321
#endif
con's avatar
con committed
322

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

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

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

    if (isSimpleType(type))
        return true;

    type = stripNamespace(type);

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

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

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

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

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

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

hjk's avatar
hjk committed
504
505
    void putBase64Encoded(const char *buf, int n);
    void checkFill();
con's avatar
con committed
506
507
508
509

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

con's avatar
con committed
525
    bool success;          // are we finished?
hjk's avatar
hjk committed
526
    bool full;
con's avatar
con committed
527
    int pos;
528
529
530

    const char *currentChildType;
    const char *currentChildNumChild;
con's avatar
con committed
531
532
533
534
535
536
};


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

QDumper::~QDumper()
{
546
    outBuffer[pos++] = '\0';
hjk's avatar
hjk committed
547
    if (success)
548
        outBuffer[0] = (full ? '+' : 't');
con's avatar
con committed
549
550
551
552
}

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

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

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

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

588
589
590
591
592
593
594
QDumper &QDumper::put(long long c)
{
    checkFill();
    pos += sprintf(outBuffer + pos, "%lld", c);
    return *this;
}

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

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

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

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

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

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

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

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

hjk's avatar
hjk committed
658
QDumper &QDumper::put(const QByteArray &ba)
con's avatar
con committed
659
{
hjk's avatar
hjk committed
660
661
    putBase64Encoded(ba.constData(), ba.size());
    return *this;
con's avatar
con committed
662
663
}

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

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

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

con's avatar
con committed
732
733
734
735
736
737
738
void QDumper::disarm()
{
    success = true;
}

void QDumper::beginHash()
{
hjk's avatar
hjk committed
739
    putCommaIfNeeded();
con's avatar
con committed
740
741
742
743
744
745
746
747
    put('{');
}

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

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

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

hjk's avatar
hjk committed
760

con's avatar
con committed
761
762
763
764
//
// Some helpers to keep the dumper code short
//

765
766
void QDumper::beginItem(const char *name)
{
hjk's avatar
hjk committed
767
768
    putCommaIfNeeded();
    put(name).put('=').put('"');
769
770
771
772
}

void QDumper::endItem()
{
hjk's avatar
hjk committed
773
    put('"');
774
775
}

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

hjk's avatar
hjk committed
790
791
    putCommaIfNeeded();
    put("children=[");
792
793
}

hjk's avatar
hjk committed
794
795
796
void QDumper::endChildren()
{
    put(']');
797
798
    currentChildType = 0;
    currentChildNumChild = 0;
hjk's avatar
hjk committed
799
}
con's avatar
con committed
800
801

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

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

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

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

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

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

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

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

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

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

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

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

1041
static void qDumpInnerValue(QDumper &d, const char *type, const void *addr)
con's avatar
con committed
1042
{
1043
    d.putItem("addr", addr);
1044
    d.putItem("type", type, d.currentChildType);
con's avatar
con committed
1045
1046

    if (!type[0])
1047
        return;
con's avatar
con committed
1048

1049
    return qDumpInnerValueHelper(d, type, addr);
con's avatar
con committed
1050
1051
}

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

con's avatar
con committed
1073
1074
//////////////////////////////////////////////////////////////////////////////

1075
#ifndef QT_BOOTSTRAPPED
1076
1077
1078
1079
struct ModelIndex { int r; int c; void *p; void *m; };

static void qDumpQAbstractItem(QDumper &d)
{
1080
    QModelIndex mi;
1081
1082
    {
       ModelIndex *mm = reinterpret_cast<ModelIndex *>(&mi);
Kai Koehne's avatar
Kai Koehne committed
1083
       memset(&mi, 0, sizeof(mi));
1084
       static const char *printFormat = sizeof(void *) == sizeof(long) ?
1085
                                        "%d,%d,0x%lx,0x%lx" : "%d,%d,0x%llx,0x%llx";
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
1086
       sscanf(d.templateParameters[0], printFormat, &mm->r, &mm->c, &mm->p, &mm->m);
1087
    }
1088
1089
1090
1091
1092
1093
1094
    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;
1095
1096
    d.putItem("type", NS"QAbstractItem");
    d.beginItem("addr");
1097
1098
        d.put('$').put(mi.row()).put(',').put(mi.column()).put(',')
            .put(mi.internalPointer()).put(',').put(mi.model());
1099
    d.endItem();
hjk's avatar
hjk committed
1100
    //d.putItem("value", "(").put(rowCount).put(",").put(columnCount).put(")");
1101
1102
    d.putItem("value", m->data(mi, Qt::DisplayRole).toString());
    d.putItem("valueencoded", "2");
1103
    d.putItem("numchild", rowCount * columnCount);
1104
    if (d.dumpChildren) {
hjk's avatar
hjk committed
1105
        d.beginChildren();