File indexing completed on 2024-07-21 13:17:20

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Faure <david.faure@kdab.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "parser.h"
0008 
0009 #include "analyze/suppressions.h"
0010 
0011 #include <QCommandLineParser>
0012 #include <QCoreApplication>
0013 #include <QDebug>
0014 
0015 int main(int argc, char** argv)
0016 {
0017     QCoreApplication app(argc, argv);
0018 
0019     QCommandLineOption stopAfterOption(
0020         QStringLiteral("stop-after"),
0021         QStringLiteral("stop parsing after the given stage, possible values are: Summary, BottomUp, SizeHistogram, "
0022                        "TopDownAndCallerCallee, Finished"),
0023         QStringLiteral("stage"), QStringLiteral("Finished"));
0024     QCommandLineParser commandLineParser;
0025     commandLineParser.addOption(stopAfterOption);
0026     commandLineParser.addPositionalArgument(QStringLiteral("file"), QStringLiteral("heaptrack data files to parse"));
0027     commandLineParser.addHelpOption();
0028 
0029     commandLineParser.process(app);
0030 
0031     const auto files = commandLineParser.positionalArguments();
0032     if (files.isEmpty())
0033         return 1;
0034 
0035     qRegisterMetaType<CallerCalleeResults>();
0036     qRegisterMetaType<TreeData>();
0037 
0038     Parser parser;
0039     QObject::connect(&parser, &Parser::finished, &app, &QCoreApplication::quit);
0040     QObject::connect(&parser, &Parser::failedToOpen, &app, [&](const QString& path) {
0041         qWarning() << "failed to open" << path;
0042         app.exit(1);
0043     });
0044 
0045     const auto stopAfter = [&]() {
0046         const auto str = commandLineParser.value(stopAfterOption);
0047         if (str == QLatin1String("Summary")) {
0048             return Parser::StopAfter::Summary;
0049         } else if (str == QLatin1String("BottomUp")) {
0050             return Parser::StopAfter::BottomUp;
0051         } else if (str == QLatin1String("SizeHistogram")) {
0052             return Parser::StopAfter::SizeHistogram;
0053         } else if (str == QLatin1String("TopDownAndCallerCallee")) {
0054             return Parser::StopAfter::TopDownAndCallerCallee;
0055         } else if (str == QLatin1String("Finished")) {
0056             return Parser::StopAfter::Finished;
0057         }
0058 
0059         qWarning() << "unsupported stopAfter stage:" << str;
0060         exit(1);
0061     }();
0062 
0063     FilterParameters params;
0064     const auto suppressionsFile = files.value(2);
0065     if (!suppressionsFile.isEmpty()) {
0066         bool parsedOk = false;
0067         params.suppressions = parseSuppressions(suppressionsFile.toStdString(), &parsedOk);
0068         if (!parsedOk)
0069             return 1;
0070     }
0071 
0072     parser.parse(files.value(0), files.value(1), params, stopAfter);
0073 
0074     return app.exec();
0075 }