tst_dumpers.cpp 229 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Bill King's avatar
Bill King committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
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
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
Bill King's avatar
Bill King committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Bill King's avatar
Bill King committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
Bill King's avatar
Bill King committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Bill King's avatar
Bill King committed
30

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

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

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

46
#define MSKIP_SINGLE(x) do { disarm(); QSKIP(x); } while (0)
47

48
using namespace Debugger;
hjk's avatar
hjk committed
49
using namespace Internal;
50

51
#ifdef Q_CC_MSVC
52

53
// Copied from abstractmsvctoolchain.cpp to avoid plugin dependency.
54 55 56 57 58 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
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;
}

144 145 146 147 148

#ifndef CDBEXT_PATH
#define CDBEXT_PATH ""
#endif

149 150 151 152 153 154 155 156
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());
157
    const QByteArray cdbextPath = CDBEXT_PATH "\\qtcreatorcdbext64";
158
    QVERIFY(QFile::exists(QString::fromLatin1(cdbextPath + QByteArray("\\qtcreatorcdbext.dll"))));
159
    env.set(QLatin1String("_NT_DEBUGGER_EXTENSION_PATH"), QString::fromLatin1(cdbextPath));
Orgad Shaneh's avatar
Orgad Shaneh committed
160
    *makeBinary = env.searchInPath(QLatin1String("nmake.exe")).toString();
161 162 163 164 165 166 167 168 169 170
    *environment = env.toProcessEnvironment();
}

#else

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

#endif // Q_CC_MSVC


hjk's avatar
hjk committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
struct VersionBase
{
    // Minimum and maximum are inclusive.
    VersionBase(int minimum = 0, int maximum = INT_MAX)
    {
        isRestricted = minimum != 0 || maximum != INT_MAX;
        max = maximum;
        min = minimum;
    }

    bool covers(int what) const
    {
        return !isRestricted || (min <= what && what <= max);
    }

    bool isRestricted;
    int min;
    int max;
};

struct QtVersion : VersionBase
{
    QtVersion(int minimum = 0, int maximum = INT_MAX)
        : VersionBase(minimum, maximum)
    {}
};

struct GccVersion : VersionBase
{
    GccVersion(int minimum = 0, int maximum = INT_MAX)
        : VersionBase(minimum, maximum)
    {}
};

205 206 207 208 209 210 211
struct ClangVersion : VersionBase
{
    ClangVersion(int minimum = 0, int maximum = INT_MAX)
        : VersionBase(minimum, maximum)
    {}
};

hjk's avatar
hjk committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225
struct GdbVersion : VersionBase
{
    GdbVersion(int minimum = 0, int maximum = INT_MAX)
        : VersionBase(minimum, maximum)
    {}
};

struct LldbVersion : VersionBase
{
    LldbVersion(int minimum = 0, int maximum = INT_MAX)
        : VersionBase(minimum, maximum)
    {}
};

226 227 228 229 230 231 232
struct BoostVersion : VersionBase
{
    BoostVersion(int minimum = 0, int maximum = INT_MAX)
        : VersionBase(minimum, maximum)
    {}
};

233 234
static QByteArray noValue = "\001";

235 236 237 238 239 240 241 242 243 244
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
245 246
struct Context
{
247
    Context() : qtVersion(0), gccVersion(0), clangVersion(0) {}
hjk's avatar
hjk committed
248 249 250

    QByteArray nameSpace;
    int qtVersion;
hjk's avatar
hjk committed
251
    int gccVersion;
252
    int clangVersion;
hjk's avatar
hjk committed
253 254
};

hjk's avatar
hjk committed
255 256
struct Name
{
hjk's avatar
hjk committed
257 258 259 260 261
    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
262 263 264
    {
        QByteArray actualName = actualName0;
        QByteArray expectedName = name;
hjk's avatar
hjk committed
265
        expectedName.replace("@Q", context.nameSpace + 'Q');
hjk's avatar
hjk committed
266 267 268 269 270 271 272
        return actualName == expectedName;
    }

    QByteArray name;
};

static Name nameFromIName(const QByteArray &iname)
hjk's avatar
hjk committed
273 274
{
    int pos = iname.lastIndexOf('.');
hjk's avatar
hjk committed
275
    return Name(pos == -1 ? iname : iname.mid(pos + 1));
hjk's avatar
hjk committed
276 277 278 279 280 281 282 283
}

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

hjk's avatar
hjk committed
284
struct ValueBase
285
{
hjk's avatar
hjk committed
286 287 288 289
    ValueBase()
      : hasPtrSuffix(false), isFloatValue(false), substituteNamespace(true),
        qtVersion(0), minimalGccVersion(0)
    {}
hjk's avatar
hjk committed
290 291

    bool hasPtrSuffix;
292
    bool isFloatValue;
293
    bool substituteNamespace;
hjk's avatar
hjk committed
294 295
    int qtVersion;
    int minimalGccVersion;
hjk's avatar
hjk committed
296 297 298 299
};

struct Value : public ValueBase
{
300 301 302 303
    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) {}
304

hjk's avatar
hjk committed
305
    bool matches(const QString &actualValue0, const Context &context) const
306
    {
307
        if (value == QString::fromLatin1(noValue))
308
            return true;
hjk's avatar
hjk committed
309 310

        if (context.qtVersion) {
hjk's avatar
hjk committed
311
            if (qtVersion == 4) {
hjk's avatar
hjk committed
312 313 314 315
                if (context.qtVersion < 0x40000 || context.qtVersion >= 0x50000) {
                    //QWARN("Qt 4 specific case skipped");
                    return true;
                }
hjk's avatar
hjk committed
316
            } else if (qtVersion == 5) {
hjk's avatar
hjk committed
317 318 319 320 321 322
                if (context.qtVersion < 0x50000 || context.qtVersion >= 0x60000) {
                    //QWARN("Qt 5 specific case skipped");
                    return true;
                }
            }
        }
hjk's avatar
hjk committed
323 324 325 326 327 328
        if (minimalGccVersion && context.gccVersion) {
            if (minimalGccVersion >= context.gccVersion) {
                //QWARN("Current GCC is too old for this test.")
                return true;
            }
        }
329 330 331
        QString actualValue = actualValue0;
        if (actualValue == QLatin1String(" "))
            actualValue.clear(); // FIXME: Remove later.
332
        QString expectedValue = value;
333 334
        if (substituteNamespace)
            expectedValue.replace(QLatin1Char('@'), QString::fromLatin1(context.nameSpace));
hjk's avatar
hjk committed
335

336
        if (hasPtrSuffix)
337 338
            return actualValue.startsWith(expectedValue + QLatin1String(" @0x"))
                || actualValue.startsWith(expectedValue + QLatin1String("@0x"));
339 340 341 342 343 344 345 346 347 348 349 350

        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;
        }


351
        return actualValue == expectedValue;
352 353
    }

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

356
    QString value;
357 358 359 360 361 362 363 364
};

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

365 366 367 368 369 370
struct FloatValue : Value
{
    FloatValue() { isFloatValue = true; }
    FloatValue(const QByteArray &value) : Value(value) { isFloatValue = true; }
};

hjk's avatar
hjk committed
371 372
struct Value4 : Value
{
hjk's avatar
hjk committed
373
    Value4(const QByteArray &value) : Value(value) { qtVersion = 4; }
hjk's avatar
hjk committed
374 375 376 377
};

struct Value5 : Value
{
hjk's avatar
hjk committed
378
    Value5(const QByteArray &value) : Value(value) { qtVersion = 5; }
hjk's avatar
hjk committed
379 380
};

381 382 383 384 385
struct UnsubstitutedValue : Value
{
    UnsubstitutedValue(const QByteArray &value) : Value(value) { substituteNamespace = false; }
};

386 387
struct Optional {};

388 389
struct Type
{
390 391 392
    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
393

394
    bool matches(const QByteArray &actualType0, const Context &context, bool fullNamespaceMatch = true) const
395
    {
396
        if (context.qtVersion) {
hjk's avatar
hjk committed
397
            if (qtVersion == 4) {
398 399 400 401
                if (context.qtVersion < 0x40000 || context.qtVersion >= 0x50000) {
                    //QWARN("Qt 4 specific case skipped");
                    return true;
                }
hjk's avatar
hjk committed
402
            } else if (qtVersion == 5) {
403 404 405 406 407 408
                if (context.qtVersion < 0x50000 || context.qtVersion >= 0x60000) {
                    //QWARN("Qt 5 specific case skipped");
                    return true;
                }
            }
        }
409
        QByteArray actualType =
410
            simplifyType(QString::fromLatin1(actualType0)).toLatin1();
411
        actualType.replace(' ', "");
hjk's avatar
hjk committed
412
        actualType.replace("const", "");
413 414
        QByteArray expectedType = type;
        expectedType.replace(' ', "");
415
        expectedType.replace("const", "");
hjk's avatar
hjk committed
416
        expectedType.replace('@', context.nameSpace);
417 418 419 420 421
        if (fullNamespaceMatch)
            expectedType.replace('?', context.nameSpace);
        else
            expectedType.replace('?', "");

422 423 424 425 426
        if (isPattern) {
            QString actual = QString::fromLatin1(actualType);
            QString expected = QString::fromLatin1(expectedType);
            return QRegExp(expected).exactMatch(actual);
        }
427 428 429 430 431 432 433 434 435 436 437 438

        if (actualType == expectedType)
            return true;

        // LLDB 3.7 on Linux doesn't get the namespace right in QMapNode:
        // t = lldb.target.FindFirstType('Myns::QMapNode<int, CustomStruct>')
        // t.GetName() -> QMapNode<int, CustomStruct> (no Myns::)
        // So try again without namespace.
        if (fullNamespaceMatch)
            return matches(actualType0, context, false);

        return false;
439
    }
440

441
    QByteArray type;
hjk's avatar
hjk committed
442
    int qtVersion;
443
    bool isPattern;
444 445 446 447
};

struct Type4 : Type
{
hjk's avatar
hjk committed
448
    Type4(const QByteArray &ba) : Type(ba) { qtVersion = 4; }
449 450 451 452
};

struct Type5 : Type
{
hjk's avatar
hjk committed
453
    Type5(const QByteArray &ba) : Type(ba) { qtVersion = 5; }
454 455
};

456 457 458 459 460
struct Pattern : Type
{
    Pattern(const QByteArray &ba) : Type(ba) { isPattern = true; }
};

461 462
enum DebuggerEngine
{
hjk's avatar
hjk committed
463 464 465 466 467 468 469 470 471
    GdbEngine = 0x01,
    CdbEngine = 0x02,
    LldbEngine = 0x04,

    AllEngines = GdbEngine | CdbEngine | LldbEngine,

    NoCdbEngine = AllEngines & (~CdbEngine),
    NoLldbEngine = AllEngines & (~LldbEngine),
    NoGdbEngine = AllEngines & (~GdbEngine)
472 473 474 475
};

struct CheckBase
{
476
    CheckBase() : enginesForCheck(AllEngines), optionallyPresent(false) {}
477
    mutable int enginesForCheck;
hjk's avatar
hjk committed
478
    mutable VersionBase debuggerVersionForCheck;
479 480
    mutable VersionBase gccVersionForCheck;
    mutable VersionBase clangVersionForCheck;
hjk's avatar
hjk committed
481
    mutable QtVersion qtVersionForCheck;
482
    mutable bool optionallyPresent;
483 484 485
};

struct Check : CheckBase
486 487 488
{
    Check() {}

hjk's avatar
hjk committed
489
    Check(const QByteArray &iname, const Value &value, const Type &type)
hjk's avatar
hjk committed
490
        : iname(iname), expectedName(nameFromIName(iname)),
491
          expectedValue(value), expectedType(type)
hjk's avatar
hjk committed
492 493
    {}

hjk's avatar
hjk committed
494
    Check(const QByteArray &iname, const Name &name,
495
         const Value &value, const Type &type)
496
        : iname(iname), expectedName(name),
497
          expectedValue(value), expectedType(type)
498 499
    {}

500
    bool matches(DebuggerEngine engine, int debuggerVersion, const Context &context) const
501
    {
hjk's avatar
hjk committed
502 503
        return (engine & enginesForCheck)
            && debuggerVersionForCheck.covers(debuggerVersion)
504 505 506
            && gccVersionForCheck.covers(context.gccVersion)
            && clangVersionForCheck.covers(context.clangVersion)
            && qtVersionForCheck.covers(context.qtVersion);
507 508
    }

509 510 511 512 513 514
    const Check &operator%(Optional) const
    {
        optionallyPresent = true;
        return *this;
    }

hjk's avatar
hjk committed
515
    const Check &operator%(DebuggerEngine engine)
516
    {
hjk's avatar
hjk committed
517
        enginesForCheck = engine;
518
        return *this;
519
    }
520

hjk's avatar
hjk committed
521 522 523 524 525 526 527
    const Check &operator%(GdbVersion version)
    {
        enginesForCheck = GdbEngine;
        debuggerVersionForCheck = version;
        return *this;
    }

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    const Check &operator%(LldbVersion version)
    {
        enginesForCheck = LldbEngine;
        debuggerVersionForCheck = version;
        return *this;
    }

    const Check &operator%(GccVersion version)
    {
        enginesForCheck = GdbEngine;
        gccVersionForCheck = version;
        return *this;
    }

    const Check &operator%(ClangVersion version)
    {
        enginesForCheck = GdbEngine;
        clangVersionForCheck = version;
        return *this;
    }

549
    QByteArray iname;
hjk's avatar
hjk committed
550
    Name expectedName;
551
    Value expectedValue;
552
    Type expectedType;
553 554
};

hjk's avatar
hjk committed
555
struct CheckType : public Check
556
{
hjk's avatar
hjk committed
557
    CheckType(const QByteArray &iname, const Name &name,
558
         const Type &type)
559 560
        : Check(iname, name, noValue, type)
    {}
561

562
    CheckType(const QByteArray &iname, const Type &type)
hjk's avatar
hjk committed
563 564 565 566
        : Check(iname, noValue, type)
    {}
};

hjk's avatar
hjk committed
567 568 569
const QtVersion Qt4 = QtVersion(0, 0x4ffff);
const QtVersion Qt5 = QtVersion(0x50000);

570 571 572
struct Check4 : Check
{
    Check4(const QByteArray &iname, const Value &value, const Type &type)
hjk's avatar
hjk committed
573
        : Check(iname, value, type) { qtVersionForCheck = Qt4; }
574 575

    Check4(const QByteArray &iname, const Name &name, const Value &value, const Type &type)
hjk's avatar
hjk committed
576
        : Check(iname, name, value, type) { qtVersionForCheck = Qt4; }
577 578 579 580 581
};

struct Check5 : Check
{
    Check5(const QByteArray &iname, const Value &value, const Type &type)
hjk's avatar
hjk committed
582
        : Check(iname, value, type) { qtVersionForCheck = Qt5; }
583 584

    Check5(const QByteArray &iname, const Name &name, const Value &value, const Type &type)
hjk's avatar
hjk committed
585
        : Check(iname, name, value, type) { qtVersionForCheck = Qt5; }
586 587 588

};

hjk's avatar
hjk committed
589
struct Profile
hjk's avatar
hjk committed
590 591 592 593
{
    Profile(const QByteArray &contents) : contents(contents) {}

    QByteArray contents;
594 595
};

hjk's avatar
hjk committed
596

hjk's avatar
hjk committed
597 598
struct Cxx11Profile : public Profile
{
599 600 601 602 603 604
    Cxx11Profile()
      : Profile("greaterThan(QT_MAJOR_VERSION,4): CONFIG += c++11\n"
                "else: QMAKE_CXXFLAGS += -std=c++0x\n")
    {}
};

605 606 607 608 609 610 611
struct BoostProfile : public Profile
{
    BoostProfile()
      : Profile("macx:INCLUDEPATH += /usr/local/include")
    {}
};

612
struct MacLibCppProfile : public Profile
613
{
614
    MacLibCppProfile()
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
      : 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
630 631
};

632
struct ForceC {};
hjk's avatar
hjk committed
633
struct EigenProfile {};
634
struct UseDebugImage {};
635

hjk's avatar
hjk committed
636 637 638
struct CoreProfile {};
struct CorePrivateProfile {};
struct GuiProfile {};
hjk's avatar
hjk committed
639
struct NetworkProfile {};
hjk's avatar
hjk committed
640

641 642
struct BigArrayProfile {};

hjk's avatar
hjk committed
643 644
struct DataBase
{
645 646
    DataBase()
      : useQt(false), useQHash(false),
hjk's avatar
hjk committed
647
        forceC(false), engines(AllEngines),
648 649
        glibcxxDebug(false), useDebugImage(false),
        bigArray(false)
650
    {}
hjk's avatar
hjk committed
651 652

    mutable bool useQt;
653
    mutable bool useQHash;
hjk's avatar
hjk committed
654
    mutable bool forceC;
655
    mutable int engines;
656
    mutable bool glibcxxDebug;
657
    mutable bool useDebugImage;
658
    mutable bool bigArray;
659
    mutable GdbVersion neededGdbVersion;     // DEC. 70600
hjk's avatar
hjk committed
660
    mutable LldbVersion neededLldbVersion;
661
    mutable QtVersion neededQtVersion;       // HEX! 0x50300
662
    mutable GccVersion neededGccVersion;     // DEC. 40702  for 4.7.2
663
    mutable ClangVersion neededClangVersion; // DEC.
664
    mutable BoostVersion neededBoostVersion; // DEC. 105400 for 1.54.0
hjk's avatar
hjk committed
665 666 667
};

class Data : public DataBase
668
{
hjk's avatar
hjk committed
669 670
public:
    Data() {}
hjk's avatar
hjk committed
671
    Data(const QByteArray &code) : code(code) {}
672

673
    Data(const QByteArray &includes, const QByteArray &code)
hjk's avatar
hjk committed
674
        : includes(includes), code(code)
hjk's avatar
hjk committed
675
    {}
676

hjk's avatar
hjk committed
677
    const Data &operator+(const Check &check) const
678
    {
679
        checks.append(check);
680 681 682
        return *this;
    }

hjk's avatar
hjk committed
683
    const Data &operator+(const Profile &profile) const
hjk's avatar
hjk committed
684 685 686 687 688
    {
        profileExtra += profile.contents;
        return *this;
    }

hjk's avatar
hjk committed
689
    const Data &operator+(const QtVersion &qtVersion) const
690 691 692 693 694
    {
        neededQtVersion = qtVersion;
        return *this;
    }

hjk's avatar
hjk committed
695
    const Data &operator+(const GccVersion &gccVersion) const
696 697 698 699 700
    {
        neededGccVersion = gccVersion;
        return *this;
    }

701 702 703 704 705 706
    const Data &operator+(const ClangVersion &clangVersion) const
    {
        neededClangVersion = clangVersion;
        return *this;
    }

hjk's avatar
hjk committed
707
    const Data &operator+(const GdbVersion &gdbVersion) const
708 709 710 711 712
    {
        neededGdbVersion = gdbVersion;
        return *this;
    }

hjk's avatar
hjk committed
713
    const Data &operator+(const LldbVersion &lldbVersion) const
hjk's avatar
hjk committed
714 715 716 717 718
    {
        neededLldbVersion = lldbVersion;
        return *this;
    }

719 720 721 722 723 724
    const Data &operator+(const BoostVersion &boostVersion) const
    {
        neededBoostVersion = boostVersion;
        return *this;
    }

hjk's avatar
hjk committed
725
    const Data &operator+(const DebuggerEngine &enginesForTest) const
hjk's avatar
hjk committed
726
    {
727
        engines = enginesForTest;
hjk's avatar
hjk committed
728 729 730
        return *this;
    }

hjk's avatar
hjk committed
731
    const Data &operator+(const EigenProfile &) const
hjk's avatar
hjk committed
732 733
    {
        profileExtra +=
hjk's avatar
hjk committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
            "exists(/usr/include/eigen3/Eigen/Core) {\n"
            "    DEFINES += HAS_EIGEN3\n"
            "    INCLUDEPATH += /usr/include/eigen3\n"
            "}\n"
            "exists(/usr/local/include/eigen3/Eigen/Core) {\n"
            "    DEFINES += HAS_EIGEN3\n"
            "    INCLUDEPATH += /usr/local/include/eigen3\n"
            "}\n"
            "\n"
            "exists(/usr/include/eigen2/Eigen/Core) {\n"
            "    DEFINES += HAS_EIGEN2\n"
            "    INCLUDEPATH += /usr/include/eigen2\n"
            "}\n"
            "exists(/usr/local/include/eigen2/Eigen/Core) {\n"
            "    DEFINES += HAS_EIGEN2\n"
            "    INCLUDEPATH += /usr/local/include/eigen2\n"
            "}\n";
hjk's avatar
hjk committed
751 752 753 754

        return *this;
    }

hjk's avatar
hjk committed
755
    const Data &operator+(const UseDebugImage &) const
756 757 758 759 760
    {
        useDebugImage = true;
        return *this;
    }

hjk's avatar
hjk committed
761
    const Data &operator+(const CoreProfile &) const
hjk's avatar
hjk committed
762 763 764
    {
        profileExtra +=
            "CONFIG += QT\n"
765
            "QT += core\n";
hjk's avatar
hjk committed
766 767

        useQt = true;
768 769
        useQHash = true;

hjk's avatar
hjk committed
770 771 772
        return *this;
    }

hjk's avatar
hjk committed
773
    const Data &operator+(const NetworkProfile &) const
hjk's avatar
hjk committed
774 775 776 777 778 779 780 781 782 783 784
    {
        profileExtra +=
            "CONFIG += QT\n"
            "QT += core network\n";

        useQt = true;
        useQHash = true;

        return *this;
    }

785 786 787 788 789 790
    const Data &operator+(const BigArrayProfile &) const
    {
        this->bigArray = true;
        return *this;
    }

hjk's avatar
hjk committed
791
    const Data &operator+(const GuiProfile &) const
hjk's avatar
hjk committed
792
    {
hjk's avatar
hjk committed
793
        this->operator+(CoreProfile());
hjk's avatar
hjk committed
794 795 796 797 798 799 800
        profileExtra +=
            "QT += gui\n"
            "greaterThan(QT_MAJOR_VERSION, 4):QT *= widgets\n";

        return *this;
    }

hjk's avatar
hjk committed
801
    const Data &operator+(const CorePrivateProfile &) const
hjk's avatar
hjk committed
802
    {
hjk's avatar
hjk committed
803
        this->operator+(CoreProfile());
hjk's avatar
hjk committed
804 805
        profileExtra +=
            "greaterThan(QT_MAJOR_VERSION, 4) {\n"
806
            "  QT += core-private\n"
hjk's avatar
hjk committed
807 808 809 810 811 812
            "  CONFIG += no_private_qt_headers_warning\n"
            "}";

        return *this;
    }

hjk's avatar
hjk committed
813
    const Data &operator+(const ForceC &) const
814 815 816 817 818
    {
        forceC = true;
        return *this;
    }

hjk's avatar
hjk committed
819
public:
hjk's avatar
hjk committed
820
    mutable QByteArray profileExtra;
821 822
    mutable QByteArray includes;
    mutable QByteArray code;
823
    mutable QList<Check> checks;
hjk's avatar
hjk committed
824 825
};

826 827
struct TempStuff
{
828 829 830
    TempStuff(const char *tag) : buildTemp(QLatin1String("qt_tst_dumpers_")
                                           + QLatin1String(tag)
                                           + QLatin1Char('_'))
831
    {
832
        buildPath = QDir::currentPath() + QLatin1Char('/')  + buildTemp.path();
833 834 835 836 837 838 839 840 841
        buildTemp.setAutoRemove(false);
        QVERIFY(!buildPath.isEmpty());
    }

    QByteArray input;
    QTemporaryDir buildTemp;
    QString buildPath;
};

hjk's avatar
hjk committed
842
Q_DECLARE_METATYPE(Data)
hjk's avatar
hjk committed
843

844
class tst_Dumpers : public QObject
845 846 847 848
{
    Q_OBJECT

public:
849 850 851
    tst_Dumpers()
    {
        t = 0;
852
        m_keepTemp = true;
853
        m_forceKeepTemp = false;
hjk's avatar
hjk committed
854
        m_debuggerVersion = 0;
855
        m_gdbBuildVersion = 0;
856
        m_qtVersion = 0;
857
        m_gccVersion = 0;
858 859
        m_isMacGdb = false;
        m_isQnxGdb = false;
860
        m_useGLibCxxDebug = false;
861
    }
862 863

private slots:
hjk's avatar
hjk committed
864
    void initTestCase();
hjk's avatar
hjk committed
865 866
    void dumper();
    void dumper_data();
867 868
    void init();
    void cleanup();
hjk's avatar
hjk committed
869 870

private:
871
    void disarm() { t->buildTemp.setAutoRemove(!keepTemp()); }
872
    bool keepTemp() const { return m_keepTemp || m_forceKeepTemp; }
873
    TempStuff *t;
hjk's avatar
hjk committed
874 875
    QByteArray m_debuggerBinary;
    QByteArray m_qmakeBinary;
876
    QProcessEnvironment m_env;
877
    DebuggerEngine m_debuggerEngine;
878
    QString m_makeBinary;
hjk's avatar
hjk committed
879
    bool m_keepTemp;
880
    bool m_forceKeepTemp;
hjk's avatar
hjk committed
881
    int m_debuggerVersion; // GDB: 7.5.1 -> 70501
882
    int m_gdbBuildVersion;
883
    int m_qtVersion; // 5.2.0 -> 50200
884
    int m_gccVersion;
885 886
    bool m_isMacGdb;
    bool m_isQnxGdb;
887
    bool m_useGLibCxxDebug;
hjk's avatar
hjk committed
888 889 890 891
};

void tst_Dumpers::initTestCase()
{
892
    m_debuggerBinary = qgetenv("QTC_DEBUGGER_PATH_FOR_TEST");
893 894 895 896
    if (m_debuggerBinary.isEmpty()) {
#ifdef Q_OS_MAC
        m_debuggerBinary = "/Applications/Xcode.app/Contents/Developer/usr/bin/lldb";
#else
hjk's avatar
hjk committed
897
        m_debuggerBinary = "gdb";
898 899
#endif
    }
900
    qDebug() << "Debugger           : " << m_debuggerBinary.constData();
901

hjk's avatar
hjk committed
902
    m_debuggerEngine = GdbEngine;
903
    if (m_debuggerBinary.endsWith("cdb.exe"))
hjk's avatar
hjk committed
904
        m_debuggerEngine = CdbEngine;
905

906 907
    QString base = QFileInfo(QString::fromLatin1(m_debuggerBinary)).baseName();
    if (base.startsWith(QLatin1String("lldb")))
hjk's avatar
hjk committed
908
        m_debuggerEngine = LldbEngine;
hjk's avatar
hjk committed
909

hjk's avatar
hjk committed
910
    m_qmakeBinary = qgetenv("QTC_QMAKE_PATH_FOR_TEST");
hjk's avatar
hjk committed
911 912
    if (m_qmakeBinary.isEmpty())
        m_qmakeBinary = "qmake";
913 914 915 916 917 918 919
    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
920

hjk's avatar
hjk committed
921
    if (m_debuggerEngine == GdbEngine) {
922 923 924 925 926 927 928 929 930
        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;
931 932
        bool usePython = !output.contains("Python scripting is not supported in this copy of GDB");
        qDebug() << "Python             : " << (usePython ? "ok" : "*** not ok ***");
933
        qDebug() << "Dumper dir         : " << DUMPERDIR;
934
        QVERIFY(usePython);
935 936 937 938 939 940 941 942 943

        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);
hjk's avatar
hjk committed
944
        extractGdbVersion(version, &m_debuggerVersion,
945
            &m_gdbBuildVersion, &m_isMacGdb, &m_isQnxGdb);
946
        m_env = QProcessEnvironment::systemEnvironment();
947 948
        m_makeBinary = QString::fromLocal8Bit(qgetenv("QTC_MAKE_PATH_FOR_TEST"));
#ifdef Q_OS_WIN
Christian Stenger's avatar
Christian Stenger committed
949
        if (m_makeBinary.isEmpty())