summaryrefslogtreecommitdiffstats
path: root/src/tools/tracegen/provider.cpp
blob: 11322e951ca6afa401de6ad4e1d82b95dfeb9200 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Rafael Roquetto <rafael.roquetto@kdab.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "provider.h"
#include "panic.h"

#include <qfile.h>
#include <qfileinfo.h>
#include <qtextstream.h>
#include <qregularexpression.h>
#include <qstring.h>

using namespace Qt::StringLiterals;

#ifdef TRACEGEN_DEBUG
#include <qdebug.h>

static void dumpTracepoint(const Tracepoint &t)
{
    qDebug() << "=== BEGIN TRACEPOINT ===";
    qDebug() << "name:" << t.name;
    qDebug() << "ARGS\n";

    int j = 0;

    for (auto i = t.args.constBegin(); i != t.args.constEnd(); ++i) {
        qDebug() << "ARG[" << j << "] type:" << i->type;
        qDebug() << "ARG[" << j << "] name:" << i->name;
        qDebug() << "ARG[" << j << "] arrayLen:" << i->arrayLen;
        ++j;
    }

    qDebug() << "\nFIELDS\n";

    j = 0;

    for (auto i = t.fields.constBegin(); i != t.fields.constEnd(); ++i) {
        qDebug() << "FIELD[" << j << "] backend_type" << static_cast<int>(i->backendType);
        qDebug() << "FIELD[" << j << "] param_type" << i->paramType;
        qDebug() << "FIELD[" << j << "] name" << i->name;
        qDebug() << "FIELD[" << j << "] arrayLen" << i->arrayLen;
        qDebug() << "FIELD[" << j << "] seqLen" << i->seqLen;
        ++j;
    }

    qDebug() << "=== END TRACEPOINT ===\n";

}
#endif

static inline int arrayLength(const QString &rawType)
{
    /* matches the length of an ordinary array type
     * Ex: foo[10] yields '10'
     */
    static const QRegularExpression rx(QStringLiteral("\\[([0-9]+)\\]"));

    auto match = rx.match(rawType);
    if (!match.hasMatch())
        return 0;

    return match.captured(1).toInt();
}

static inline QString sequenceLength(const QString &rawType)
{
    /* matches the identifier pointing to length of a CTF sequence type, which is
     * a dynamic sized array.
     * Ex: in qcoreapplication_foo(const char[len], some_string, unsigned int, * len)
     * it will match the 'len' part of 'const char[len]')
     */
    static const QRegularExpression rx(QStringLiteral("\\[([A-Za-z_][A-Za-z_0-9]*)\\]"));

    auto match = rx.match(rawType);
    if (!match.hasMatch())
        return QString();

    return match.captured(1);
}

static QString decayArrayToPointer(QString type)
{
    /* decays an array to a pointer, i.e., if 'type' holds int[10],
     * this function returns 'int *'
     */
    static QRegularExpression rx(QStringLiteral("\\[([^\\]]+)\\]"));

    return type.replace(rx, QStringLiteral("*"));
}

static QString removeBraces(QString type)
{
    static const QRegularExpression rx(QStringLiteral("\\[.*\\]"));

    return type.remove(rx);
}

static Tracepoint::Field::BackendType backendType(QString rawType)
{
    static const struct {
        const char *type;
        Tracepoint::Field::BackendType backendType;
    } typeTable[] = {
        { "bool",                   Tracepoint::Field::Integer },
        { "short_int",              Tracepoint::Field::Integer },
        { "signed_short",           Tracepoint::Field::Integer },
        { "signed_short_int",       Tracepoint::Field::Integer },
        { "unsigned_short",         Tracepoint::Field::Integer },
        { "unsigned_short_int",     Tracepoint::Field::Integer },
        { "int",                    Tracepoint::Field::Integer },
        { "signed",                 Tracepoint::Field::Integer },
        { "signed_int",             Tracepoint::Field::Integer },
        { "unsigned",               Tracepoint::Field::Integer },
        { "unsigned_int",           Tracepoint::Field::Integer },
        { "long",                   Tracepoint::Field::Integer },
        { "long_int",               Tracepoint::Field::Integer },
        { "signed_long",            Tracepoint::Field::Integer },
        { "signed_long_int",        Tracepoint::Field::Integer },
        { "unsigned_long",          Tracepoint::Field::Integer },
        { "unsigned_long_int",      Tracepoint::Field::Integer },
        { "long_long",              Tracepoint::Field::Integer },
        { "long_long_int",          Tracepoint::Field::Integer },
        { "signed_long_long",       Tracepoint::Field::Integer },
        { "signed_long_long_int",   Tracepoint::Field::Integer },
        { "unsigned_long_long",     Tracepoint::Field::Integer },
        { "char",                   Tracepoint::Field::Integer },
        { "intptr_t",               Tracepoint::Field::IntegerHex },
        { "uintptr_t",              Tracepoint::Field::IntegerHex },
        { "std::intptr_t",          Tracepoint::Field::IntegerHex },
        { "std::uintptr_t",         Tracepoint::Field::IntegerHex },
        { "float",                  Tracepoint::Field::Float },
        { "double",                 Tracepoint::Field::Float },
        { "long_double",            Tracepoint::Field::Float },
        { "QString",                Tracepoint::Field::QtString },
        { "QByteArray",             Tracepoint::Field::QtByteArray },
        { "QUrl",                   Tracepoint::Field::QtUrl },
        { "QRect",                  Tracepoint::Field::QtRect }
    };

    auto backendType = [](const QString &rawType) {
        static const size_t tableSize = sizeof (typeTable) / sizeof (typeTable[0]);

        for (size_t i = 0; i < tableSize; ++i) {
            if (rawType == QLatin1StringView(typeTable[i].type))
                return typeTable[i].backendType;
        }

        return Tracepoint::Field::Unknown;
    };

    if (arrayLength(rawType) > 0)
        return Tracepoint::Field::Array;

    if (!sequenceLength(rawType).isNull())
        return Tracepoint::Field::Sequence;

    static const QRegularExpression constMatch(QStringLiteral("\\bconst\\b"));
    rawType.remove(constMatch);
    rawType.remove(u'&');

    static const QRegularExpression ptrMatch(QStringLiteral("\\s*\\*\\s*"));
    rawType.replace(ptrMatch, QStringLiteral("_ptr"));
    rawType = rawType.trimmed();
    rawType.replace(QStringLiteral(" "), QStringLiteral("_"));

    if (rawType == "char_ptr"_L1)
        return Tracepoint::Field::String;

    if (rawType.endsWith("_ptr"_L1))
        return Tracepoint::Field::Pointer;

    return backendType(rawType);
}

static Tracepoint parseTracepoint(const QString &name, const QStringList &args,
        const QString &fileName, const int lineNumber)
{
    Tracepoint t;
    t.name = name;

    if (args.isEmpty())
        return t;

    auto i = args.constBegin();
    auto end = args.constEnd();
    int argc = 0;

    static const QRegularExpression rx(QStringLiteral("^(.*)\\b([A-Za-z_][A-Za-z0-9_]*)$"));

    while (i != end) {
        auto match = rx.match(*i);

        const QString type = match.captured(1).trimmed();

        if (type.isNull()) {
            panic("Missing parameter type for argument %d of %s (%s:%d)",
                    argc, qPrintable(name), qPrintable(fileName), lineNumber);
        }

        const QString name = match.captured(2).trimmed();

        if (name.isNull()) {
            panic("Missing parameter name for argument %d of %s (%s:%d)",
                    argc, qPrintable(name), qPrintable(fileName), lineNumber);
        }

        int arrayLen = arrayLength(type);

        Tracepoint::Argument a;
        a.arrayLen = arrayLen;
        a.name = name;
        a.type = decayArrayToPointer(type);

        t.args << std::move(a);

        Tracepoint::Field f;
        f.backendType = backendType(type);
        f.paramType = removeBraces(type);
        f.name = name;
        f.arrayLen = arrayLen;
        f.seqLen = sequenceLength(type);

        t.fields << std::move(f);

        ++i;
    }

    return t;
}

Provider parseProvider(const QString &filename)
{
    QFile f(filename);

    if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
        panic("Cannot open %s: %s", qPrintable(filename), qPrintable(f.errorString()));

    QTextStream s(&f);

    static const QRegularExpression tracedef(QStringLiteral("^([A-Za-z][A-Za-z0-9_]*)\\((.*)\\)$"));

    Provider provider;
    provider.name = QFileInfo(filename).baseName();

    bool parsingPrefixText = false;
    for (int lineNumber = 1; !s.atEnd(); ++lineNumber) {
        QString line = s.readLine().trimmed();

        if (line == "{"_L1) {
            parsingPrefixText = true;
            continue;
        } else if (parsingPrefixText && line == "}"_L1) {
            parsingPrefixText = false;
            continue;
        } else if (parsingPrefixText) {
            provider.prefixText.append(line);
            continue;
        }

        if (line.isEmpty() || line.startsWith(u'#'))
            continue;

        auto match = tracedef.match(line);
        if (match.hasMatch()) {
            const QString name = match.captured(1);
            const QString argsString = match.captured(2);
            const QStringList args = argsString.split(u',', Qt::SkipEmptyParts);

            provider.tracepoints << parseTracepoint(name, args, filename, lineNumber);
        } else {
            panic("Syntax error while processing '%s' line %d:\n"
                  "    '%s' does not look like a tracepoint definition",
                  qPrintable(filename), lineNumber,
                  qPrintable(line));
        }
    }

    if (parsingPrefixText) {
        panic("Syntax error while processing '%s': "
              "no closing brace found for prefix text block",
              qPrintable(filename));
    }

#ifdef TRACEGEN_DEBUG
    qDebug() << provider.prefixText;
    for (auto i = provider.tracepoints.constBegin(); i != provider.tracepoints.constEnd(); ++i)
        dumpTracepoint(*i);
#endif

    return provider;
}