gdbmacros.cpp 97.3 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
26
** contact the sales department at http://www.qtsoftware.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
37
38
39
40
41
42
43
44
45
46
47
48
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QHash>
#include <QtCore/QLinkedList>
#include <QtCore/QLocale>
#include <QtCore/QMap>
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
#include <QtCore/QModelIndex>
#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtCore/QString>
#include <QtCore/QTextCodec>
#include <QtCore/QVector>
49
50
51
52

#if QT_VERSION >= 0x040500
#include <QtCore/QSharedPointer>
#include <QtCore/QSharedDataPointer>
53
#include <QtCore/QWeakPointer>
54
#endif
con's avatar
con committed
55

56
57
int qtGhVersion = QT_VERSION;

58
59
60
61
62
63
64
#ifndef USE_QT_GUI
#   ifdef QT_GUI_LIB
#        define USE_QT_GUI 1
#   endif
#endif

#if USE_QT_GUI
65
#   include <QtGui/QApplication>
66
#   include <QtGui/QImage>
67
68
#   include <QtGui/QPixmap>
#   include <QtGui/QWidget>
69
70
#endif

71
72
73
74
#ifdef Q_OS_WIN
#    include <windows.h>
#endif

75
76
77
#include <list>
#include <map>
#include <string>
78
#include <set>
79
80
#include <vector>

con's avatar
con committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*!
  \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()}.

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

  Next step is to create a suitable \c{static void qDumpFoo(QDumper &d)}
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
104
  function. At the bare minimum it should contain something like this:
con's avatar
con committed
105
106
107
108
109


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

110
111
112
    d.putItem("value", ...);
    d.putItem("type", "Foo");
    d.putItem("numchild", "0");
con's avatar
con committed
113
114
115
  }


116
  'd.putItem(name, value)' roughly expands to:
hjk's avatar
hjk committed
117
        d.put((name)).put("=\"").put(value).put("\"";
con's avatar
con committed
118
119
120
121
122
123
124
125
126

  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".
127
        A value of "3" is used for base64-encoded UCS4, "2" denotes
con's avatar
con committed
128
129
130
131
132
133
        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
134
  about these children. In this case the dumper should use something like this:
con's avatar
con committed
135
136
137

  \c{
    if (d.dumpChildren) {
hjk's avatar
hjk committed
138
139
140
        d.beginChildren();
            [...]
        d.endChildren();
con's avatar
con committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
   }

  */

#undef NS
#ifdef QT_NAMESPACE
#   define STRINGIFY0(s) #s
#   define STRINGIFY1(s) STRINGIFY0(s)
#   define NS STRINGIFY1(QT_NAMESPACE) "::"
#   define NSX "'" STRINGIFY1(QT_NAMESPACE) "::"
#   define NSY "'"
#else
#   define NS ""
#   define NSX ""
#   define NSY ""
#endif

#if defined(QT_BEGIN_NAMESPACE)
QT_BEGIN_NAMESPACE
#endif

162
struct Sender { QObject *sender; int signal; int ref; };
con's avatar
con committed
163

164
165
166
167
168
169
170
171
#if QT_VERSION < 0x040600
    struct Connection
    {
        QObject *receiver;
        int method;
        uint connectionType : 3; // 0 == auto, 1 == direct, 2 == queued, 4 == blocking
        QBasicAtomicPointer<int> argumentTypes;
    };
con's avatar
con committed
172

173
174
    typedef QList<Connection> ConnectionList;
    typedef QList<Sender> SenderList;
con's avatar
con committed
175

176
177
178
179
    const Connection &connectionAt(const ConnectionList &l, int i) { return l.at(i); }
    const QObject *senderAt(const SenderList &l, int i) { return l.at(i).sender; }
    int signalAt(const SenderList &l, int i) { return l.at(i).signal; }
#endif
con's avatar
con committed
180

181
182
#if QT_VERSION >= 0x040600
    struct Connection
con's avatar
con committed
183
184
    {
        QObject *sender;
185
186
187
188
        QObject *receiver;
        int method;
        uint connectionType : 3; // 0 == auto, 1 == direct, 2 == queued, 4 == blocking
        QBasicAtomicPointer<int> argumentTypes;
189
190
191
        //senders linked list
        //Connection *next;
        //Connection **prev;
con's avatar
con committed
192
193
    };

194
195
196
197
198
199
200
201
202
    typedef QList<Connection *> ConnectionList;
    typedef ConnectionList SenderList;

    const Connection &connectionAt(const ConnectionList &l, int i) { return *l.at(i); }
    const QObject *senderAt(const SenderList &l, int i) { return l.at(i)->sender; }
    // FIXME: 'method' is wrong
    int signalAt(const SenderList &l, int i) { return l.at(i)->method; }
#endif

203
class ObjectPrivate : public QObjectData
204
205
{
public:
206
207
    ObjectPrivate() {}
    virtual ~ObjectPrivate() {}
208
209
210
211
212

    QList<QObject *> pendingChildInsertedEvents;
    void *threadData;
    void *currentSender;
    void *currentChildBeingDeleted;
con's avatar
con committed
213
214
215

    QList<QPointer<QObject> > eventFilters;

216
    void *extraData;
con's avatar
con committed
217
218
219
220
    mutable quint32 connectedSignals;

    QString objectName;

221
222
    void *connectionLists;
    SenderList senders;
con's avatar
con committed
223
224
225
    int *deleteWatch;
};

226

con's avatar
con committed
227
228
229
230
231
#if defined(QT_BEGIN_NAMESPACE)
QT_END_NAMESPACE
#endif


232
233
234
// This can be mangled typenames of nested templates, each char-by-char
// comma-separated integer list...
// The output buffer.
235
236
237
238
239
240
241
242
243
244
245
#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
246
247
248

namespace {

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
249
250
static QByteArray strPtrConst = "* const";

con's avatar
con committed
251
252
static bool isPointerType(const QByteArray &type)
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
253
    return type.endsWith('*') || type.endsWith(strPtrConst);
con's avatar
con committed
254
255
}

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
256
static QByteArray stripPointerType(const QByteArray &_type)
con's avatar
con committed
257
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
258
259
    QByteArray type = _type;
    if (type.endsWith('*'))
con's avatar
con committed
260
        type.chop(1);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
261
    if (type.endsWith(strPtrConst))
con's avatar
con committed
262
263
264
265
266
267
268
        type.chop(7);
    if (type.endsWith(' '))
        type.chop(1);
    return type;
}

// This is used to abort evaluation of custom data dumpers in a "coordinated"
269
// way. Abortion will happen at the latest when we try to access a non-initialized
con's avatar
con committed
270
// non-trivial object, so there is no way to prevent this from occuring at all
271
272
273
274
275
276
277
278
279
// 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
280
281
282
283
284
285
286
287
288
289

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)
{
290
    return sizeof(void *) + reinterpret_cast<const char *>(p);
con's avatar
con committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
}

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
310
311
312
313
    while (char c = *t++)
        if (c != *s++)
            return false;
    return true;
con's avatar
con committed
314
315
}

316
317
318
319
// 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
320
321
#    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)
322
323
324
325
#else
#    define qCheckAccess(d) do { qProvokeSegFaultHelper = *(char*)d; } while (0)
#    define qCheckPointer(d) do { if (d) qProvokeSegFaultHelper = *(char*)d; } while (0)
#endif
con's avatar
con committed
326

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

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

362
#if 0
363
364
365
366
367
368
369
370
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");
}
371
#endif
372

con's avatar
con committed
373
374
375
376
377
378
379
380
381
382
static bool isMovableType(const char *type)
{
    if (isPointerType(type))
        return true;

    if (isSimpleType(type))
        return true;

    type = stripNamespace(type);

383
    switch (type[1]) {
con's avatar
con committed
384
385
386
387
388
        case 'B':
            return isEqual(type, "QBrush")
                || isEqual(type, "QBitArray")
                || isEqual(type, "QByteArray") ;
        case 'C':
389
390
            return isEqual(type, "QCustomTypeInfo")
                || isEqual(type, "QChar");
con's avatar
con committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
        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")
407
                || isEqual(type, "QLatin1Char")
con's avatar
con committed
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
440
441
442
443
444
445
                || 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();
446

hjk's avatar
hjk committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    // direct write to the output 
    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);
    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:
462
    template <class Value>
hjk's avatar
hjk committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
    void putItem(const char *name, const Value &value)
    {
        putCommaIfNeeded();
        put(name).put('=').put('"').put(value).put('"');
    }

    // 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);

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

    void beginChildren(); // start of children list
    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
493
494
495

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

hjk's avatar
hjk committed
501
502
    void putBase64Encoded(const char *buf, int n);
    void checkFill();
con's avatar
con committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

    // the dumper arguments
    int protocolVersion;   // dumper protocol version
    int token;             // some token to show on success
    const char *outertype; // object type
    const char *iname;     // object name used for display
    const char *exp;       // object expression
    const char *innertype; // 'inner type' for class templates
    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
520
521
    int extraInt[4];

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


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;
con's avatar
con committed
534
535
536
537
}

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

void QDumper::setupTemplateParameters()
{
    char *s = const_cast<char *>(innertype);

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

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

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

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

hjk's avatar
hjk committed
587
QDumper &QDumper::put(float d)
con's avatar
con committed
588
589
{
    checkFill();
590
    pos += sprintf(outBuffer + pos, "%f", d);
con's avatar
con committed
591
592
593
    return *this;
}

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

hjk's avatar
hjk committed
601
QDumper &QDumper::put(unsigned int i)
con's avatar
con committed
602
603
{
    checkFill();
604
    pos += sprintf(outBuffer + pos, "%u", i);
con's avatar
con committed
605
606
607
    return *this;
}

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

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

hjk's avatar
hjk committed
622
QDumper &QDumper::put(const void *p)
con's avatar
con committed
623
624
625
626
627
628
629
630
631
632
{
    static char buf[100];
    if (p) {
        sprintf(buf, "%p", p);
        // we get a '0x' prefix only on some implementations.
        // if it isn't there, write it out manually.
        if (buf[1] != 'x') {
            put('0');
            put('x');
        }
hjk's avatar
hjk committed
633
        put(buf);
con's avatar
con committed
634
    } else {
hjk's avatar
hjk committed
635
        put("<null>");
con's avatar
con committed
636
637
638
639
    }
    return *this;
}

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

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

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

676
void QDumper::putBase64Encoded(const char *buf, int n)
con's avatar
con committed
677
678
679
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
{
    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
717
    putCommaIfNeeded();
con's avatar
con committed
718
719
720
721
722
723
724
725
    put('{');
}

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

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

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

hjk's avatar
hjk committed
738

con's avatar
con committed
739
740
741
742
//
// Some helpers to keep the dumper code short
//

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

void QDumper::endItem()
{
hjk's avatar
hjk committed
751
    put('"');
752
753
}

hjk's avatar
hjk committed
754
void QDumper::beginChildren()
755
{
hjk's avatar
hjk committed
756
757
    putCommaIfNeeded();
    put("children=[");
758
759
}

hjk's avatar
hjk committed
760
761
762
763
void QDumper::endChildren()
{
    put(']');
}
con's avatar
con committed
764
765

// simple string property
hjk's avatar
hjk committed
766
void QDumper::putHash(const char *name, const QString &value)
767
768
769
770
771
772
773
774
775
776
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", NS"QString");
    putItem("numchild", "0");
    putItem("valueencoded", "2");
    endHash();
}

hjk's avatar
hjk committed
777
void QDumper::putHash(const char *name, const QByteArray &value)
778
779
780
781
782
783
784
785
786
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", NS"QByteArray");
    putItem("numchild", "0");
    putItem("valueencoded", "1");
    endHash();
}
con's avatar
con committed
787
788

// simple integer property
hjk's avatar
hjk committed
789
void QDumper::putHash(const char *name, int value)
790
791
792
793
794
795
796
797
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", "int");
    putItem("numchild", "0");
    endHash();
}
hjk's avatar
hjk committed
798
799

void QDumper::putHash(const char *name, long value)
800
801
802
803
804
805
806
807
{
    beginHash();
    putItem("name", name);
    putItem("value", value);
    putItem("type", "long");
    putItem("numchild", "0");
    endHash();
}
con's avatar
con committed
808
809

// simple boolean property
hjk's avatar
hjk committed
810
void QDumper::putHash(const char *name, bool value)
811
812
813
814
815
816
817
818
{
    beginHash();
    putItem("name", name);
    putItem("value", (value ? "true" : "false"));
    putItem("type", "bool");
    putItem("numchild", "0");
    endHash();
}
con's avatar
con committed
819
820

// a single QChar
hjk's avatar
hjk committed
821
void QDumper::putHash(const char *name, QChar value)
822
823
824
825
826
827
828
829
830
831
{
    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
832

833
834
#define DUMPUNKNOWN_MESSAGE "<internal error>"
static void qDumpUnknown(QDumper &d, const char *why = 0)
con's avatar
con committed
835
{
836
837
    //d.putItem("iname", d.iname);
    //d.putItem("addr", d.data);
838
839
    if (!why)
        why = DUMPUNKNOWN_MESSAGE;
840
841
842
    d.putItem("value", why);
    d.putItem("type", d.outertype);
    d.putItem("numchild", "0");
con's avatar
con committed
843
844
845
846
    d.disarm();
}

static void qDumpInnerValueHelper(QDumper &d, const char *type, const void *addr,
847
    const char *field = "value")
con's avatar
con committed
848
{
849
    char buf[30];
con's avatar
con committed
850
851
852
853
    type = stripNamespace(type);
    switch (type[1]) {
        case 'l':
            if (isEqual(type, "float"))
854
                d.putItem(field, *(float*)addr);
con's avatar
con committed
855
856
857
            return;
        case 'n':
            if (isEqual(type, "int"))
858
                d.putItem(field, *(int*)addr);
con's avatar
con committed
859
            else if (isEqual(type, "unsigned"))
860
                d.putItem(field, *(unsigned int*)addr);
con's avatar
con committed
861
            else if (isEqual(type, "unsigned int"))
862
                d.putItem(field, *(unsigned int*)addr);
con's avatar
con committed
863
            else if (isEqual(type, "unsigned long"))
864
                d.putItem(field, *(unsigned long*)addr);
con's avatar
con committed
865
            else if (isEqual(type, "unsigned long long"))
866
                d.putItem(field, *(qulonglong*)addr);
con's avatar
con committed
867
868
869
870
            return;
        case 'o':
            if (isEqual(type, "bool"))
                switch (*(bool*)addr) {
871
872
873
                    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
874
875
                }
            else if (isEqual(type, "double"))
876
                d.putItem(field, *(double*)addr);
con's avatar
con committed
877
            else if (isEqual(type, "long"))
878
                d.putItem(field, *(long*)addr);
con's avatar
con committed
879
            else if (isEqual(type, "long long"))
880
                d.putItem(field, *(qulonglong*)addr);
con's avatar
con committed
881
882
883
            return;
        case 'B':
            if (isEqual(type, "QByteArray")) {
hjk's avatar
hjk committed
884
885
                d.putCommaIfNeeded();
                d.put(field).put("encoded=\"1\",");
886
                d.putItem(field, *(QByteArray*)addr);
con's avatar
con committed
887
888
            }
            return;
889
890
        case 'C':
            if (isEqual(type, "QChar")) {
hjk's avatar
hjk committed
891
                d.putCommaIfNeeded();
892
                QChar c = *(QChar *)addr;
893
                sprintf(buf, "'?', ucs=%d", c.unicode());
894
                if (c.isPrint() && c.unicode() < 127)
895
896
897
                    buf[1] = char(c.unicode());
                d.putItem(field, buf);
                d.putItem("numchild", 0);
898
899
            }
            return;
con's avatar
con committed
900
901
902
        case 'L':
            if (startsWith(type, "QList<")) {
                const QListData *ldata = reinterpret_cast<const QListData*>(addr);
903
904
905
                d.putItemCount("value", ldata->size());
                d.putItem("valuedisabled", "true");
                d.putItem("numchild", ldata->size());
con's avatar
con committed
906
907
908
909
910
911
            }
            return;
        case 'O':
            if (isEqual(type, "QObject *")) {
                if (addr) {
                    const QObject *ob = reinterpret_cast<const QObject *>(addr);
912
913
914
915
916
                    d.putItem("addr", ob);
                    d.putItem("value", ob->objectName());
                    d.putItem("valueencoded", "2");
                    d.putItem("type", NS"QObject");
                    d.putItem("displayedtype", ob->metaObject()->className());
con's avatar
con committed
917
                } else {
918
919
                    d.putItem("value", "0x0");
                    d.putItem("type", NS"QObject *");
con's avatar
con committed
920
921
922
923
924
                }
            }
            return;
        case 'S':
            if (isEqual(type, "QString")) {
hjk's avatar
hjk committed
925
926
                d.putCommaIfNeeded();
                d.put(field).put("encoded=\"2\"");
927
                d.putItem(field, *(QString*)addr);
con's avatar
con committed
928
929
930
931
932
933
934
935
936
            }
            return;
        default:
            return;
    }
}

static void qDumpInnerValue(QDumper &d, const char *type, const void *addr)
{
937
938
    d.putItem("addr", addr);
    d.putItem("type", type);
con's avatar
con committed
939
940
941
942
943
944
945
946

    if (!type[0])
        return;

    qDumpInnerValueHelper(d, type, addr);
}


947
948
949
950
951
static void qDumpInnerValueOrPointer(QDumper &d,
    const char *type, const char *strippedtype, const void *addr)
{
    if (strippedtype) {
        if (deref(addr)) {
952
953
954
            d.putItem("addr", deref(addr));
            d.putItem("saddr", deref(addr));
            d.putItem("type", strippedtype);
955
956
            qDumpInnerValueHelper(d, strippedtype, deref(addr));
        } else {
957
958
959
960
            d.putItem("addr", addr);
            d.putItem("type", strippedtype);
            d.putItem("value", "<null>");
            d.putItem("numchild", "0");
961
962
        }
    } else {
963
964
        d.putItem("addr", addr);
        d.putItem("type", type);
965
966
967
968
        qDumpInnerValueHelper(d, type, addr);
    }
}

con's avatar
con committed
969
970
//////////////////////////////////////////////////////////////////////////////

971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
struct ModelIndex { int r; int c; void *p; void *m; };

static void qDumpQAbstractItem(QDumper &d)
{
    ModelIndex mm;
    mm.r = mm.c = 0;
    mm.p = mm.m = 0;
    sscanf(d.templateParameters[0], "%d,%d,%p,%p", &mm.r, &mm.c, &mm.p, &mm.m);
    const QModelIndex &mi(*reinterpret_cast<QModelIndex *>(&mm));
    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;
987
988
    d.putItem("type", NS"QAbstractItem");
    d.beginItem("addr");
hjk's avatar
hjk committed
989
        d.put('$').put(mm.r).put(',').put(mm.c).put(',').put(mm.p).put(',').put(mm.m);
990
    d.endItem();
hjk's avatar
hjk committed
991
    //d.putItem("value", "(").put(rowCount).put(",").put(columnCount).put(")");
992
993
994
    d.putItem("value", m->data(mi, Qt::DisplayRole).toString());
    d.putItem("valueencoded", "2");
    d.putItem("numchild", "1");
995
    if (d.dumpChildren) {
hjk's avatar
hjk committed
996
        d.beginChildren();
997
998
999
1000
        for (int row = 0; row < rowCount; ++row) {
            for (int column = 0; column < columnCount; ++column) {
                QModelIndex child = m->index(row, column, mi);
                d.beginHash();