tst_dumpers.cpp 214 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Bill King's avatar
Bill King committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Bill King's avatar
Bill King committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Bill King's avatar
Bill King committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Bill King's avatar
Bill King committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** 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.
Bill King's avatar
Bill King committed
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Bill King's avatar
Bill King committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Bill King's avatar
Bill King committed
29

hjk's avatar
hjk committed
30
#include "debuggerprotocol.h"
31
#include "simplifytype.h"
32
33
#include "watchdata.h"
#include "watchutils.h"
34

35
#ifdef Q_CC_MSVC
36
#include <utils/environment.h>
37
38
39
#include <utils/qtcprocess.h>
#include <utils/fileutils.h>
#include <utils/synchronousprocess.h>
40
#endif // Q_CC_MSVC
41

42
#include "temporarydir.h"
43

44
#include <QtTest>
45
#include <math.h>
46

47
#if  QT_VERSION >= 0x050000
48
#define MSKIP_SINGLE(x) do { disarm(); QSKIP(x); } while (0)
49
#else
50
#define MSKIP_SINGLE(x) do { disarm(); QSKIP(x, SkipSingle); } while (0)
51
52
#endif

53
using namespace Debugger;
hjk's avatar
hjk committed
54
using namespace Internal;
55

56
#ifdef Q_CC_MSVC
57

58
// Copied from abstractmsvctoolchain.cpp to avoid plugin dependency.
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
static bool generateEnvironmentSettings(Utils::Environment &env,
                                        const QString &batchFile,
                                        const QString &batchArgs,
                                        QMap<QString, QString> &envPairs)
{
    // Create a temporary file name for the output. Use a temporary file here
    // as I don't know another way to do this in Qt...
    // Note, can't just use a QTemporaryFile all the way through as it remains open
    // internally so it can't be streamed to later.
    QString tempOutFile;
    QTemporaryFile* pVarsTempFile = new QTemporaryFile(QDir::tempPath() + QLatin1String("/XXXXXX.txt"));
    pVarsTempFile->setAutoRemove(false);
    pVarsTempFile->open();
    pVarsTempFile->close();
    tempOutFile = pVarsTempFile->fileName();
    delete pVarsTempFile;

    // Create a batch file to create and save the env settings
    Utils::TempFileSaver saver(QDir::tempPath() + QLatin1String("/XXXXXX.bat"));

    QByteArray call = "call ";
    call += Utils::QtcProcess::quoteArg(batchFile).toLocal8Bit();
    if (!batchArgs.isEmpty()) {
        call += ' ';
        call += batchArgs.toLocal8Bit();
    }
    saver.write(call + "\r\n");

    const QByteArray redirect = "set > " + Utils::QtcProcess::quoteArg(
                                    QDir::toNativeSeparators(tempOutFile)).toLocal8Bit() + "\r\n";
    saver.write(redirect);
    if (!saver.finalize()) {
        qWarning("%s: %s", Q_FUNC_INFO, qPrintable(saver.errorString()));
        return false;
    }

    Utils::QtcProcess run;
    // As of WinSDK 7.1, there is logic preventing the path from being set
    // correctly if "ORIGINALPATH" is already set. That can cause problems
    // if Creator is launched within a session set up by setenv.cmd.
    env.unset(QLatin1String("ORIGINALPATH"));
    run.setEnvironment(env);
    const QString cmdPath = QString::fromLocal8Bit(qgetenv("COMSPEC"));
    // Windows SDK setup scripts require command line switches for environment expansion.
    QString cmdArguments = QLatin1String(" /E:ON /V:ON /c \"");
    cmdArguments += QDir::toNativeSeparators(saver.fileName());
    cmdArguments += QLatin1Char('"');
    run.setCommand(cmdPath, cmdArguments);
    run.start();

    if (!run.waitForStarted()) {
        qWarning("%s: Unable to run '%s': %s", Q_FUNC_INFO, qPrintable(batchFile),
                 qPrintable(run.errorString()));
        return false;
    }
    if (!run.waitForFinished()) {
        qWarning("%s: Timeout running '%s'", Q_FUNC_INFO, qPrintable(batchFile));
        Utils::SynchronousProcess::stopProcess(run);
        return false;
    }
    // The SDK/MSVC scripts do not return exit codes != 0. Check on stdout.
    const QByteArray stdOut = run.readAllStandardOutput();
    if (!stdOut.isEmpty() && (stdOut.contains("Unknown") || stdOut.contains("Error")))
        qWarning("%s: '%s' reports:\n%s", Q_FUNC_INFO, call.constData(), stdOut.constData());

    //
    // Now parse the file to get the environment settings
    QFile varsFile(tempOutFile);
    if (!varsFile.open(QIODevice::ReadOnly))
        return false;

    QRegExp regexp(QLatin1String("(\\w*)=(.*)"));
    while (!varsFile.atEnd()) {
        const QString line = QString::fromLocal8Bit(varsFile.readLine()).trimmed();
        if (regexp.exactMatch(line)) {
            const QString varName = regexp.cap(1);
            const QString varValue = regexp.cap(2);

            if (!varValue.isEmpty())
                envPairs.insert(varName, varValue);
        }
    }

    // Tidy up and remove the file
    varsFile.close();
    varsFile.remove();

    return true;
}

149
150
151
152
153

#ifndef CDBEXT_PATH
#define CDBEXT_PATH ""
#endif

154
155
156
157
158
159
160
161
static void setupCdb(QString *makeBinary, QProcessEnvironment *environment)
{
    QByteArray envBat = qgetenv("QTC_MSVC_ENV_BAT");
    QMap <QString, QString> envPairs;
    Utils::Environment env = Utils::Environment::systemEnvironment();
    QVERIFY(generateEnvironmentSettings(env, QString::fromLatin1(envBat), QString(), envPairs));
    for (QMap<QString,QString>::const_iterator envIt = envPairs.begin(); envIt != envPairs.end(); ++envIt)
            env.set(envIt.key(), envIt.value());
162
    const QByteArray cdbextPath = CDBEXT_PATH "\\qtcreatorcdbext64";
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    QVERIFY(QFile::exists(QString::fromLatin1(cdbextPath + QByteArray("\\qtcreatorcdbext.dll"))));
    env.appendOrSet(QLatin1String("_NT_DEBUGGER_EXTENSION_PATH"),
                         QString::fromLatin1(cdbextPath),
                         QLatin1String(";"));
    *makeBinary = env.searchInPath(QLatin1String("nmake.exe"));
    *environment = env.toProcessEnvironment();
}

#else

static void setupCdb(QString *, QProcessEnvironment *) {}

#endif // Q_CC_MSVC


178
179
static QByteArray noValue = "\001";

180
181
182
183
184
185
186
187
188
189
static QString toHex(const QString &str)
{
    QString encoded;
    foreach (const QChar c, str) {
        encoded += QString::fromLatin1("%1")
            .arg(c.unicode(), 2, 16, QLatin1Char('0'));
    }
    return encoded;
}

hjk's avatar
hjk committed
190
191
struct Context
{
hjk's avatar
hjk committed
192
    Context() : qtVersion(0), gccVersion(0) {}
hjk's avatar
hjk committed
193
194
195

    QByteArray nameSpace;
    int qtVersion;
hjk's avatar
hjk committed
196
    int gccVersion;
hjk's avatar
hjk committed
197
198
};

hjk's avatar
hjk committed
199
200
struct Name
{
hjk's avatar
hjk committed
201
202
203
204
205
    Name() : name(noValue) {}
    Name(const char *str) : name(str) {}
    Name(const QByteArray &ba) : name(ba) {}

    bool matches(const QByteArray &actualName0, const Context &context) const
hjk's avatar
hjk committed
206
207
208
    {
        QByteArray actualName = actualName0;
        QByteArray expectedName = name;
hjk's avatar
hjk committed
209
        expectedName.replace("@Q", context.nameSpace + 'Q');
hjk's avatar
hjk committed
210
211
212
213
214
215
216
        return actualName == expectedName;
    }

    QByteArray name;
};

static Name nameFromIName(const QByteArray &iname)
hjk's avatar
hjk committed
217
218
{
    int pos = iname.lastIndexOf('.');
hjk's avatar
hjk committed
219
    return Name(pos == -1 ? iname : iname.mid(pos + 1));
hjk's avatar
hjk committed
220
221
222
223
224
225
226
227
}

static QByteArray parentIName(const QByteArray &iname)
{
    int pos = iname.lastIndexOf('.');
    return pos == -1 ? QByteArray() : iname.left(pos);
}

hjk's avatar
hjk committed
228
struct ValueBase
229
{
hjk's avatar
hjk committed
230
231
232
233
    ValueBase()
      : hasPtrSuffix(false), isFloatValue(false), substituteNamespace(true),
        qtVersion(0), minimalGccVersion(0)
    {}
hjk's avatar
hjk committed
234
235

    bool hasPtrSuffix;
236
    bool isFloatValue;
237
    bool substituteNamespace;
hjk's avatar
hjk committed
238
239
    int qtVersion;
    int minimalGccVersion;
hjk's avatar
hjk committed
240
241
242
243
};

struct Value : public ValueBase
{
244
245
246
247
    Value() : value(QString::fromLatin1(noValue)) {}
    Value(const char *str) : value(QLatin1String(str)) {}
    Value(const QByteArray &ba) : value(QString::fromLatin1(ba.data(), ba.size())) {}
    Value(const QString &str) : value(str) {}
248

hjk's avatar
hjk committed
249
    bool matches(const QString &actualValue0, const Context &context) const
250
    {
251
        if (value == QString::fromLatin1(noValue))
252
            return true;
hjk's avatar
hjk committed
253
254

        if (context.qtVersion) {
hjk's avatar
hjk committed
255
            if (qtVersion == 4) {
hjk's avatar
hjk committed
256
257
258
259
                if (context.qtVersion < 0x40000 || context.qtVersion >= 0x50000) {
                    //QWARN("Qt 4 specific case skipped");
                    return true;
                }
hjk's avatar
hjk committed
260
            } else if (qtVersion == 5) {
hjk's avatar
hjk committed
261
262
263
264
265
266
                if (context.qtVersion < 0x50000 || context.qtVersion >= 0x60000) {
                    //QWARN("Qt 5 specific case skipped");
                    return true;
                }
            }
        }
hjk's avatar
hjk committed
267
268
269
270
271
272
        if (minimalGccVersion && context.gccVersion) {
            if (minimalGccVersion >= context.gccVersion) {
                //QWARN("Current GCC is too old for this test.")
                return true;
            }
        }
273
274
275
        QString actualValue = actualValue0;
        if (actualValue == QLatin1String(" "))
            actualValue.clear(); // FIXME: Remove later.
276
        QString expectedValue = value;
277
278
        if (substituteNamespace)
            expectedValue.replace(QLatin1Char('@'), QString::fromLatin1(context.nameSpace));
hjk's avatar
hjk committed
279

280
        if (hasPtrSuffix)
281
282
            return actualValue.startsWith(expectedValue + QLatin1String(" @0x"))
                || actualValue.startsWith(expectedValue + QLatin1String("@0x"));
283
284
285
286
287
288
289
290
291
292
293
294

        if (isFloatValue) {
            double f1 = fabs(expectedValue.toDouble());
            double f2 = fabs(actualValue.toDouble());
            //qDebug() << "expected float: " << qPrintable(expectedValue) << f1;
            //qDebug() << "actual   float: " << qPrintable(actualValue) << f2;
            if (f1 < f2)
                std::swap(f1, f2);
            return f1 - f2 <= 0.01 * f2;
        }


295
        return actualValue == expectedValue;
296
297
    }

hjk's avatar
hjk committed
298
299
    void setMinimalGccVersion(int version) { minimalGccVersion = version; }

300
    QString value;
301
302
303
304
305
306
307
308
};

struct Pointer : Value
{
    Pointer() { hasPtrSuffix = true; }
    Pointer(const QByteArray &value) : Value(value) { hasPtrSuffix = true; }
};

309
310
311
312
313
314
struct FloatValue : Value
{
    FloatValue() { isFloatValue = true; }
    FloatValue(const QByteArray &value) : Value(value) { isFloatValue = true; }
};

hjk's avatar
hjk committed
315
316
struct Value4 : Value
{
hjk's avatar
hjk committed
317
    Value4(const QByteArray &value) : Value(value) { qtVersion = 4; }
hjk's avatar
hjk committed
318
319
320
321
};

struct Value5 : Value
{
hjk's avatar
hjk committed
322
    Value5(const QByteArray &value) : Value(value) { qtVersion = 5; }
hjk's avatar
hjk committed
323
324
};

325
326
327
328
329
struct UnsubstitutedValue : Value
{
    UnsubstitutedValue(const QByteArray &value) : Value(value) { substituteNamespace = false; }
};

330
331
struct Type
{
332
333
334
    Type() : qtVersion(0), isPattern(false) {}
    Type(const char *str) : type(str), qtVersion(0), isPattern(false) {}
    Type(const QByteArray &ba) : type(ba), qtVersion(0), isPattern(false) {}
hjk's avatar
hjk committed
335
336

    bool matches(const QByteArray &actualType0, const Context &context) const
337
    {
338
        if (context.qtVersion) {
hjk's avatar
hjk committed
339
            if (qtVersion == 4) {
340
341
342
343
                if (context.qtVersion < 0x40000 || context.qtVersion >= 0x50000) {
                    //QWARN("Qt 4 specific case skipped");
                    return true;
                }
hjk's avatar
hjk committed
344
            } else if (qtVersion == 5) {
345
346
347
348
349
350
                if (context.qtVersion < 0x50000 || context.qtVersion >= 0x60000) {
                    //QWARN("Qt 5 specific case skipped");
                    return true;
                }
            }
        }
351
        QByteArray actualType =
352
            simplifyType(QString::fromLatin1(actualType0)).toLatin1();
353
        actualType.replace(' ', "");
hjk's avatar
hjk committed
354
        actualType.replace("const", "");
355
356
        QByteArray expectedType = type;
        expectedType.replace(' ', "");
357
        expectedType.replace("const", "");
hjk's avatar
hjk committed
358
        expectedType.replace('@', context.nameSpace);
359
360
361
362
363
        if (isPattern) {
            QString actual = QString::fromLatin1(actualType);
            QString expected = QString::fromLatin1(expectedType);
            return QRegExp(expected).exactMatch(actual);
        }
364
365
366
        return actualType == expectedType;
    }
    QByteArray type;
hjk's avatar
hjk committed
367
    int qtVersion;
368
    bool isPattern;
369
370
371
372
};

struct Type4 : Type
{
hjk's avatar
hjk committed
373
    Type4(const QByteArray &ba) : Type(ba) { qtVersion = 4; }
374
375
376
377
};

struct Type5 : Type
{
hjk's avatar
hjk committed
378
    Type5(const QByteArray &ba) : Type(ba) { qtVersion = 5; }
379
380
};

381
382
383
384
385
struct Pattern : Type
{
    Pattern(const QByteArray &ba) : Type(ba) { isPattern = true; }
};

386
387
enum DebuggerEngine
{
hjk's avatar
hjk committed
388
389
390
391
392
393
394
395
396
    GdbEngine = 0x01,
    CdbEngine = 0x02,
    LldbEngine = 0x04,

    AllEngines = GdbEngine | CdbEngine | LldbEngine,

    NoCdbEngine = AllEngines & (~CdbEngine),
    NoLldbEngine = AllEngines & (~LldbEngine),
    NoGdbEngine = AllEngines & (~GdbEngine)
397
398
399
400
};

struct CheckBase
{
hjk's avatar
hjk committed
401
    CheckBase() : enginesForCheck(AllEngines) {}
402
    mutable int enginesForCheck;
403
404
405
};

struct Check : CheckBase
406
407
408
{
    Check() {}

hjk's avatar
hjk committed
409
    Check(const QByteArray &iname, const Value &value, const Type &type)
hjk's avatar
hjk committed
410
411
412
413
        : iname(iname), expectedName(nameFromIName(iname)),
          expectedValue(value), expectedType(type)
    {}

hjk's avatar
hjk committed
414
    Check(const QByteArray &iname, const Name &name,
415
         const Value &value, const Type &type)
416
417
418
419
        : iname(iname), expectedName(name),
          expectedValue(value), expectedType(type)
    {}

420
421
    bool matchesEngine(DebuggerEngine engine) const
    {
422
423
424
425
426
427
428
        return (engine & enginesForCheck);
    }

    const Check &setEngines(int debuggerEngine)
    {
        enginesForCheck = debuggerEngine;
        return *this;
429
430
431
432
    }

    const Check &setForLldbOnly() const
    {
hjk's avatar
hjk committed
433
        enginesForCheck = LldbEngine;
434
435
436
437
438
        return *this;
    }

    const Check &setForGdbOnly() const
    {
hjk's avatar
hjk committed
439
        enginesForCheck = GdbEngine;
440
441
442
        return *this;
    }

443
    const Check &setForCdbOnly()
444
    {
hjk's avatar
hjk committed
445
        enginesForCheck = CdbEngine;
446
        return *this;
447
    }
448

449
    QByteArray iname;
hjk's avatar
hjk committed
450
    Name expectedName;
451
    Value expectedValue;
452
    Type expectedType;
453
454
};

hjk's avatar
hjk committed
455
struct CheckType : public Check
456
{
hjk's avatar
hjk committed
457
    CheckType(const QByteArray &iname, const Name &name,
458
         const Type &type)
459
460
        : Check(iname, name, noValue, type)
    {}
461

462
    CheckType(const QByteArray &iname, const Type &type)
hjk's avatar
hjk committed
463
464
465
466
        : Check(iname, noValue, type)
    {}
};

hjk's avatar
hjk committed
467
struct Profile
hjk's avatar
hjk committed
468
469
470
471
{
    Profile(const QByteArray &contents) : contents(contents) {}

    QByteArray contents;
472
473
};

hjk's avatar
hjk committed
474

hjk's avatar
hjk committed
475
476
struct Cxx11Profile : public Profile
{
477
478
479
480
481
482
    Cxx11Profile()
      : Profile("greaterThan(QT_MAJOR_VERSION,4): CONFIG += c++11\n"
                "else: QMAKE_CXXFLAGS += -std=c++0x\n")
    {}
};

483
484
485
486
487
488
489
struct BoostProfile : public Profile
{
    BoostProfile()
      : Profile("macx:INCLUDEPATH += /usr/local/include")
    {}
};

490
struct MacLibCppProfile : public Profile
491
{
492
    MacLibCppProfile()
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
      : Profile("macx {\n"
                "QMAKE_CXXFLAGS += -stdlib=libc++\n"
                "LIBS += -stdlib=libc++\n"
                "QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7\n"
                "QMAKE_IOS_DEPLOYMENT_TARGET = 10.7\n"
                "QMAKE_CFLAGS -= -mmacosx-version-min=10.6\n"
                "QMAKE_CFLAGS += -mmacosx-version-min=10.7\n"
                "QMAKE_CXXFLAGS -= -mmacosx-version-min=10.6\n"
                "QMAKE_CXXFLAGS += -mmacosx-version-min=10.7\n"
                "QMAKE_OBJECTIVE_CFLAGS -= -mmacosx-version-min=10.6\n"
                "QMAKE_OBJECTIVE_CFLAGS += -mmacosx-version-min=10.7\n"
                "QMAKE_LFLAGS -= -mmacosx-version-min=10.6\n"
                "QMAKE_LFLAGS += -mmacosx-version-min=10.7\n"
                "}")
    {}
hjk's avatar
hjk committed
508
509
};

510
struct VersionBase
511
512
{
    // Minimum and maximum are inclusive.
513
    VersionBase(int minimum = 0, int maximum = INT_MAX)
514
    {
515
        isRestricted = minimum != 0 || maximum != INT_MAX;
516
517
518
        max = maximum;
        min = minimum;
    }
519
520

    bool isRestricted;
521
522
523
524
    int min;
    int max;
};

525
struct QtVersion : VersionBase
hjk's avatar
hjk committed
526
{
527
    QtVersion(int minimum = 0, int maximum = INT_MAX)
528
529
530
        : VersionBase(minimum, maximum)
    {}
};
hjk's avatar
hjk committed
531

532
533
struct GccVersion : VersionBase
{
534
    GccVersion(int minimum = 0, int maximum = INT_MAX)
535
536
537
538
        : VersionBase(minimum, maximum)
    {}
};

539
540
struct GdbVersion : VersionBase
{
541
    GdbVersion(int minimum = 0, int maximum = INT_MAX)
542
543
544
545
546
547
        : VersionBase(minimum, maximum)
    {}
};

struct LldbVersion : VersionBase
{
548
    LldbVersion(int minimum = 0, int maximum = INT_MAX)
549
550
        : VersionBase(minimum, maximum)
    {}
hjk's avatar
hjk committed
551
552
};

553
struct ForceC {};
hjk's avatar
hjk committed
554
struct EigenProfile {};
555
struct UseDebugImage {};
556

hjk's avatar
hjk committed
557
558
559
struct CoreProfile {};
struct CorePrivateProfile {};
struct GuiProfile {};
hjk's avatar
hjk committed
560
struct NetworkProfile {};
hjk's avatar
hjk committed
561
562
563

struct DataBase
{
564
565
    DataBase()
      : useQt(false), useQHash(false),
hjk's avatar
hjk committed
566
        forceC(false), engines(GdbEngine | CdbEngine | LldbEngine),
567
        glibcxxDebug(false), useDebugImage(false)
568
    {}
hjk's avatar
hjk committed
569
570

    mutable bool useQt;
571
    mutable bool useQHash;
hjk's avatar
hjk committed
572
    mutable bool forceC;
573
    mutable int engines;
574
    mutable bool glibcxxDebug;
575
    mutable bool useDebugImage;
576
    mutable GdbVersion neededGdbVersion;
hjk's avatar
hjk committed
577
    mutable LldbVersion neededLldbVersion;
578
    mutable QtVersion neededQtVersion;
579
    mutable GccVersion neededGccVersion;
hjk's avatar
hjk committed
580
581
582
};

class Data : public DataBase
583
{
hjk's avatar
hjk committed
584
585
public:
    Data() {}
hjk's avatar
hjk committed
586
    Data(const QByteArray &code) : code(code) {}
587

588
    Data(const QByteArray &includes, const QByteArray &code)
hjk's avatar
hjk committed
589
        : includes(includes), code(code)
hjk's avatar
hjk committed
590
    {}
591

592
593
    const Data &operator%(const Check &check) const
    {
594
        checks.append(check);
595
596
597
        return *this;
    }

hjk's avatar
hjk committed
598
599
600
601
602
603
    const Data &operator%(const Profile &profile) const
    {
        profileExtra += profile.contents;
        return *this;
    }

604
605
606
607
608
609
    const Data &operator%(const QtVersion &qtVersion) const
    {
        neededQtVersion = qtVersion;
        return *this;
    }

610
611
612
613
614
615
    const Data &operator%(const GccVersion &gccVersion) const
    {
        neededGccVersion = gccVersion;
        return *this;
    }

616
617
618
619
620
621
    const Data &operator%(const GdbVersion &gdbVersion) const
    {
        neededGdbVersion = gdbVersion;
        return *this;
    }

hjk's avatar
hjk committed
622
623
624
625
626
627
    const Data &operator%(const LldbVersion &lldbVersion) const
    {
        neededLldbVersion = lldbVersion;
        return *this;
    }

628
    const Data &operator%(const DebuggerEngine &enginesForTest) const
hjk's avatar
hjk committed
629
    {
630
        engines = enginesForTest;
hjk's avatar
hjk committed
631
632
633
        return *this;
    }

hjk's avatar
hjk committed
634
635
636
637
638
639
640
641
642
    const Data &operator%(const EigenProfile &) const
    {
        profileExtra +=
            "INCLUDEPATH += /usr/include/eigen2\n"
            "INCLUDEPATH += /usr/include/eigen3\n";

        return *this;
    }

643
644
645
646
647
648
    const Data &operator%(const UseDebugImage &) const
    {
        useDebugImage = true;
        return *this;
    }

hjk's avatar
hjk committed
649
650
651
652
    const Data &operator%(const CoreProfile &) const
    {
        profileExtra +=
            "CONFIG += QT\n"
653
            "QT += core\n";
hjk's avatar
hjk committed
654
655

        useQt = true;
656
657
        useQHash = true;

hjk's avatar
hjk committed
658
659
660
        return *this;
    }

hjk's avatar
hjk committed
661
662
663
664
665
666
667
668
669
670
671
672
    const Data &operator%(const NetworkProfile &) const
    {
        profileExtra +=
            "CONFIG += QT\n"
            "QT += core network\n";

        useQt = true;
        useQHash = true;

        return *this;
    }

hjk's avatar
hjk committed
673
674
    const Data &operator%(const GuiProfile &) const
    {
675
        this->operator%(CoreProfile());
hjk's avatar
hjk committed
676
677
678
679
680
681
682
683
684
        profileExtra +=
            "QT += gui\n"
            "greaterThan(QT_MAJOR_VERSION, 4):QT *= widgets\n";

        return *this;
    }

    const Data &operator%(const CorePrivateProfile &) const
    {
685
        this->operator%(CoreProfile());
hjk's avatar
hjk committed
686
687
        profileExtra +=
            "greaterThan(QT_MAJOR_VERSION, 4) {\n"
688
            "  QT += core-private\n"
hjk's avatar
hjk committed
689
690
691
692
693
694
            "  CONFIG += no_private_qt_headers_warning\n"
            "}";

        return *this;
    }

695
696
697
698
699
700
    const Data &operator%(const ForceC &) const
    {
        forceC = true;
        return *this;
    }

hjk's avatar
hjk committed
701
public:
hjk's avatar
hjk committed
702
    mutable QByteArray profileExtra;
703
704
    mutable QByteArray includes;
    mutable QByteArray code;
705
    mutable QList<Check> checks;
hjk's avatar
hjk committed
706
707
};

708
709
710
711
struct TempStuff
{
    TempStuff() : buildTemp(QLatin1String("qt_tst_dumpers_"))
    {
712
        buildPath = QDir::currentPath() + QLatin1Char('/')  + buildTemp.path();
713
714
715
716
717
718
719
720
721
        buildTemp.setAutoRemove(false);
        QVERIFY(!buildPath.isEmpty());
    }

    QByteArray input;
    QTemporaryDir buildTemp;
    QString buildPath;
};

hjk's avatar
hjk committed
722
Q_DECLARE_METATYPE(Data)
hjk's avatar
hjk committed
723

724
class tst_Dumpers : public QObject
725
726
727
728
{
    Q_OBJECT

public:
729
730
731
    tst_Dumpers()
    {
        t = 0;
732
        m_keepTemp = true;
733
        m_forceKeepTemp = false;
734
735
736
        m_gdbVersion = 0;
        m_gdbBuildVersion = 0;
        m_lldbVersion = 0;
737
        m_qtVersion = 0;
738
        m_gccVersion = 0;
739
740
        m_isMacGdb = false;
        m_isQnxGdb = false;
741
        m_useGLibCxxDebug = false;
742
    }
743
744

private slots:
hjk's avatar
hjk committed
745
    void initTestCase();
hjk's avatar
hjk committed
746
747
    void dumper();
    void dumper_data();
748
749
    void init();
    void cleanup();
hjk's avatar
hjk committed
750
751

private:
752
    void disarm() { t->buildTemp.setAutoRemove(!keepTemp()); }
753
    bool keepTemp() const { return m_keepTemp || m_forceKeepTemp; }
754
    TempStuff *t;
hjk's avatar
hjk committed
755
756
    QByteArray m_debuggerBinary;
    QByteArray m_qmakeBinary;
757
    QProcessEnvironment m_env;
758
    DebuggerEngine m_debuggerEngine;
759
    QString m_makeBinary;
hjk's avatar
hjk committed
760
    bool m_keepTemp;
761
    bool m_forceKeepTemp;
762
763
    int m_gdbVersion; // 7.5.1 -> 70501
    int m_gdbBuildVersion;
764
    int m_lldbVersion;
765
    int m_qtVersion; // 5.2.0 -> 50200
766
    int m_gccVersion;
767
768
    bool m_isMacGdb;
    bool m_isQnxGdb;
769
    bool m_useGLibCxxDebug;
hjk's avatar
hjk committed
770
771
772
773
};

void tst_Dumpers::initTestCase()
{
774
    m_debuggerBinary = qgetenv("QTC_DEBUGGER_PATH_FOR_TEST");
hjk's avatar
hjk committed
775
776
    if (m_debuggerBinary.isEmpty())
        m_debuggerBinary = "gdb";
777
    qDebug() << "Debugger           : " << m_debuggerBinary.constData();
778

hjk's avatar
hjk committed
779
    m_debuggerEngine = GdbEngine;
780
    if (m_debuggerBinary.endsWith("cdb.exe"))
hjk's avatar
hjk committed
781
        m_debuggerEngine = CdbEngine;
782

783
784
    QString base = QFileInfo(QString::fromLatin1(m_debuggerBinary)).baseName();
    if (base.startsWith(QLatin1String("lldb")))
hjk's avatar
hjk committed
785
        m_debuggerEngine = LldbEngine;
hjk's avatar
hjk committed
786

hjk's avatar
hjk committed
787
    m_qmakeBinary = qgetenv("QTC_QMAKE_PATH_FOR_TEST");
hjk's avatar
hjk committed
788
789
    if (m_qmakeBinary.isEmpty())
        m_qmakeBinary = "qmake";
790
791
792
793
794
795
796
    qDebug() << "QMake              : " << m_qmakeBinary.constData();

    m_useGLibCxxDebug = qgetenv("QTC_USE_GLIBCXXDEBUG_FOR_TEST").toInt();
    qDebug() << "Use _GLIBCXX_DEBUG : " << m_useGLibCxxDebug;

    m_forceKeepTemp = qgetenv("QTC_KEEP_TEMP_FOR_TEST").toInt();
    qDebug() << "Force keep temp    : " << m_forceKeepTemp;
hjk's avatar
hjk committed
797

hjk's avatar
hjk committed
798
    if (m_debuggerEngine == GdbEngine) {
799
800
801
802
803
804
805
806
807
        QProcess debugger;
        debugger.start(QString::fromLatin1(m_debuggerBinary)
                       + QLatin1String(" -i mi -quiet -nx"));
        bool ok = debugger.waitForStarted();
        debugger.write("set confirm off\npython print 43\nshow version\nquit\n");
        ok = debugger.waitForFinished();
        QVERIFY(ok);
        QByteArray output = debugger.readAllStandardOutput();
        //qDebug() << "stdout: " << output;
808
809
        bool usePython = !output.contains("Python scripting is not supported in this copy of GDB");
        qDebug() << "Python             : " << (usePython ? "ok" : "*** not ok ***");
810
        qDebug() << "Dumper dir         : " << DUMPERDIR;
811
        QVERIFY(usePython);
812
813
814
815
816
817
818
819
820

        QString version = QString::fromLocal8Bit(output);
        int pos1 = version.indexOf(QLatin1String("&\"show version\\n"));
        QVERIFY(pos1 != -1);
        pos1 += 20;
        int pos2 = version.indexOf(QLatin1String("~\"Copyright (C) "), pos1);
        QVERIFY(pos2 != -1);
        pos2 -= 4;
        version = version.mid(pos1, pos2 - pos1);
821
        extractGdbVersion(version, &m_gdbVersion,
822
            &m_gdbBuildVersion, &m_isMacGdb, &m_isQnxGdb);
823
824
        m_env = QProcessEnvironment::systemEnvironment();
        m_makeBinary = QLatin1String("make");
825
        qDebug() << "Gdb version        : " << m_gdbVersion;
hjk's avatar
hjk committed
826
    } else if (m_debuggerEngine == CdbEngine) {
827
        setupCdb(&m_makeBinary, &m_env);
hjk's avatar
hjk committed
828
    } else if (m_debuggerEngine == LldbEngine) {
829
        QProcess debugger;
830
831
        QString cmd = QString::fromUtf8(m_debuggerBinary + " -v");
        debugger.start(cmd);
832
833
834
835
836
        bool ok = debugger.waitForFinished(2000);
        QVERIFY(ok);
        QByteArray output = debugger.readAllStandardOutput();
        output += debugger.readAllStandardError();
        output = output.trimmed();
837
838
        // Should be something like "LLDB-178" (Mac OS X 10.8)
        // or "lldb version 3.5 ( revision )" (Ubuntu 13.10)
839
840
841
842
843
        QByteArray ba = output.mid(output.indexOf('-') + 1);
        int pos = ba.indexOf('.');
        if (pos >= 0)
            ba = ba.left(pos);
        m_lldbVersion = ba.toInt();
844
845
846
847
848
849
850
851
852
853
        if (!m_lldbVersion) {
            if (output.startsWith("lldb version")) {
                int pos1 = output.indexOf('.', 13);
                int major = output.mid(13, pos1++ - 13).toInt();
                int pos2 = output.indexOf(' ', pos1);
                int minor = output.mid(pos1, pos2++ - pos1).toInt();
                m_lldbVersion = 100 * major + 10 * minor;
            }
        }

854
        qDebug() << "Lldb version       :" << output << ba << m_lldbVersion;
855
        QVERIFY(m_lldbVersion);
856
857
858

        m_env = QProcessEnvironment::systemEnvironment();
        m_makeBinary = QLatin1String("make");
859
    }
hjk's avatar
hjk committed
860
861
}

862
void tst_Dumpers::init()
hjk's avatar
hjk committed
863
{
864
    t = new TempStuff();
hjk's avatar
hjk committed
865
866
}

867
void tst_Dumpers::cleanup()
hjk's avatar
hjk committed
868
{
869
870
    if (!t->buildTemp.autoRemove()) {
        QFile logger(t->buildPath + QLatin1String("/input.txt"));
hjk's avatar
hjk committed
871
        logger.open(QIODevice::ReadWrite);
872
        logger.write(t->input);
hjk's avatar
hjk committed
873
    }
874
875
    delete t;
}
876

hjk's avatar
hjk committed
877
878
879
880
void tst_Dumpers::dumper()
{
    QFETCH(Data, data);

881
882
883
    if (!(data.engines & m_debuggerEngine))
        MSKIP_SINGLE("The test is excluded for this debugger engine.");

hjk's avatar
hjk committed
884
    if (data.neededGdbVersion.isRestricted && m_debuggerEngine == GdbEngine) {
885
        if (data.neededGdbVersion.min > m_gdbVersion)
hjk's avatar
hjk committed
886
887
            MSKIP_SINGLE("Need minimum GDB version "
                + QByteArray::number(data.neededGdbVersion.min));
888
        if (data.neededGdbVersion.max < m_gdbVersion)
hjk's avatar
hjk committed
889
890
891
892
            MSKIP_SINGLE("Need maximum GDB version "
                + QByteArray::number(data.neededGdbVersion.max));
    }

hjk's avatar
hjk committed
893
    if (data.neededLldbVersion.isRestricted && m_debuggerEngine == LldbEngine) {
894
        if (data.neededLldbVersion.min > m_lldbVersion)
hjk's avatar
hjk committed
895
896
            MSKIP_SINGLE("Need minimum LLDB version "
                + QByteArray::number(data.neededLldbVersion.min));
897
        if (data.neededLldbVersion.max < m_lldbVersion)
hjk's avatar
hjk committed
898
899
            MSKIP_SINGLE("Need maximum LLDB version "
                + QByteArray::number(data.neededLldbVersion.max));
900
    }
901

hjk's avatar
hjk committed
902
903
904
905
    QString cmd;
    QByteArray output;
    QByteArray error;

906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
    if (data.neededQtVersion.isRestricted) {
        QProcess qmake;
        qmake.setWorkingDirectory(t->buildPath);
        cmd = QString::fromLatin1(m_qmakeBinary);
        qmake.start(cmd, QStringList(QLatin1String("--version")));
        QVERIFY(qmake.waitForFinished());
        output = qmake.readAllStandardOutput();
        error = qmake.readAllStandardError();
        int pos0 = output.indexOf("Qt version");
        if (pos0 == -1) {
            qDebug() << "Output: " << output;
            qDebug() << "Error: " << error;
            QVERIFY(false);
        }
        pos0 += 11;
        int pos1 = output.indexOf('.', pos0 + 1);
        int major = output.mid(pos0, pos1++ - pos0).toInt();
        int pos2 = output.indexOf('.', pos1 + 1);
        int minor = output.mid(pos1, pos2++ - pos1).toInt();
        int pos3 = output.indexOf(' ', pos2 + 1);
        int patch = output.mid(pos2, pos3++ - pos2).toInt();
        m_qtVersion = 10000 * major + 100 * minor + patch;

        if (data.neededQtVersion.min > m_qtVersion)
            MSKIP_SINGLE("Need minimum Qt version "
                + QByteArray::number(data.neededQtVersion.min));
        if (data.neededQtVersion.max < m_qtVersion)
            MSKIP_SINGLE("Need maximum Qt version "
                + QByteArray::number(data.neededQtVersion.max));
    }

937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
    if (data.neededGccVersion.isRestricted) {
        QProcess gcc;
        gcc.setWorkingDirectory(t->buildPath);
        cmd = QLatin1String("gcc");
        gcc.start(cmd, QStringList(QLatin1String("--version")));
        QVERIFY(gcc.waitForFinished());
        output = gcc.readAllStandardOutput();
        error = gcc.readAllStandardError();
        int pos = output.indexOf('\n');
        if (pos != -1)
            output = output.left(pos);
        qDebug() << "Extracting GCC version from: " << output;
        pos = output.lastIndexOf(' ');
        output = output.mid(pos + 1);
        int pos1 = output.indexOf('.');
        int major = output.left(pos1++).toInt();
        int pos2 = output.indexOf('.', pos1 + 1);
        int minor = output.mid(pos1, pos2++ - pos1).toInt();
        int patch = output.mid(pos2).toInt();
        m_gccVersion = 10000 * major + 100 * minor + patch;
        qDebug() << "GCC version: " << m_gccVersion;

        if (data.neededGccVersion.min > m_gccVersion)
            MSKIP_SINGLE("Need minimum GCC version "
                + QByteArray::number(data.neededGccVersion.min));
        if (data.neededGccVersion.max < m_gccVersion)
            MSKIP_SINGLE("Need maximum GCC version "
                + QByteArray::number(data.neededGccVersion.max));
    }

967
968
    const char *mainFile = data.forceC ? "main.c" : "main.cpp";

969
970
    QFile proFile(t->buildPath + QLatin1String("/doit.pro"));
    QVERIFY(proFile.open(QIODevice::ReadWrite));
971
972
973
    proFile.write("SOURCES = ");
    proFile.write(mainFile);
    proFile.write("\nTARGET = doit\n");
974
    proFile.write("\nCONFIG -= app_bundle\n");
975
976
    proFile.write("\nCONFIG -= release\n");
    proFile.write("\nCONFIG += debug\n");
hjk's avatar
hjk committed
977
978
979
980
    if (data.useQt)
        proFile.write("QT -= widgets gui\n");
    else
        proFile.write("CONFIG -= QT\n");
981
982
    if (m_useGLibCxxDebug)
        proFile.write("DEFINES += _GLIBCXX_DEBUG\n");
983
984
    if (m_gdbVersion < 70500)
        proFile.write("QMAKE_CXXFLAGS += -gdwarf-3\n");
hjk's avatar
hjk committed
985
    proFile.write(data.profileExtra);
hjk's avatar
hjk committed
986
987
    proFile.close();

988
989
    QFile source(t->buildPath + QLatin1Char('/') + QLatin1String(mainFile));
    QVERIFY(source.open(QIODevice::ReadWrite));
990
    QByteArray fullCode = QByteArray() +
hjk's avatar
hjk committed
991
            "\n\nvoid unused(const void *first,...) { (void) first; }"
992
993
994
            "\n\n#if defined(_MSC_VER)" + (data.useQt ?
                "\n#include <qt_windows.h>" :
                "\n#include <Windows.h>") +
hjk's avatar
hjk committed
995
            "\n#define BREAK do { DebugBreak(); } while (0)"
996
            "\n#else"
hjk's avatar
hjk committed
997
            "\n#define BREAK do { asm(\"int $3\"); } while (0)"
998
999
            "\n#endif"
            "\n"
hjk's avatar
hjk committed
1000
            "\n\n" + data.includes +
1001
1002
1003
1004
1005
1006
1007
            "\n\n" + (data.useQHash ?
                "\n#include <QByteArray>"
                "\n#if QT_VERSION >= 0x050000"
                "\nQT_BEGIN_NAMESPACE"
                "\nQ_CORE_EXPORT extern QBasicAtomicInt qt_qhash_seed; // from qhash.cpp"
                "\nQT_END_NAMESPACE"
                "\n#endif" : "") +
hjk's avatar
hjk committed
1008
            "\n\nint main(int argc, char *argv[])"
hjk's avatar
hjk committed
1009
            "\n{"
1010
            "\n    int qtversion = " + (data.useQt ? "QT_VERSION" : "0") + ";"
hjk's avatar
hjk committed
1011
1012
1013
1014
1015
            "\n#ifdef __GNUC__"
            "\n    int gccversion = 0x10000 * __GNUC__ + 0x100 * __GNUC_MINOR__;"
            "\n#else"
            "\n    int gccversion = 0;"
            "\n#endif"
1016
1017
            "\n" + (data.useQHash ?
                "\n#if QT_VERSION >= 0x050000"
1018
                "\nqt_qhash_seed.store(0);"
1019
                "\n#endif\n" : "") +
hjk's avatar
hjk committed
1020
            "\n    unused(&argc, &argv, &qtversion, &gccversion);\n"
hjk's avatar
hjk committed
1021
            "\n" + data.code +
1022
            "\n    BREAK;"
1023
            "\n    return 0;"
hjk's avatar
hjk committed
1024
1025
            "\n}\n";
    source.write(fullCode);
hjk's avatar
hjk committed
1026
1027
1028
    source.close();

    QProcess qmake;
1029
    qmake.setWorkingDirectory(t->buildPath);
hjk's avatar
hjk committed
1030
    cmd = QString::fromLatin1(m_qmakeBinary);
hjk's avatar
hjk committed
1031
1032
    //qDebug() << "Starting qmake: " << cmd;
    qmake.start(cmd);
1033
    QVERIFY(qmake.waitForFinished());
hjk's avatar
hjk committed
1034
1035
1036
    output = qmake.readAllStandardOutput();
    error = qmake.readAllStandardError();
    //qDebug() << "stdout: " << output;
1037
    if (!error.isEmpty()) { qDebug() << error; QVERIFY(false); }
hjk's avatar
hjk committed
1038
1039

    QProcess make;
1040
    make.setWorkingDirectory(t->buildPath);
1041
    make.setProcessEnvironment(m_env);
1042

1043
    make.start(m_makeBinary, QStringList());
David Schulz's avatar