2022-05-10 10:06:48 +00:00
|
|
|
// 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
|
2017-02-01 19:32:13 +00:00
|
|
|
|
|
|
|
#include "lttng.h"
|
|
|
|
#include "provider.h"
|
|
|
|
#include "helpers.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "qtheaders.h"
|
|
|
|
|
|
|
|
#include <qfile.h>
|
|
|
|
#include <qfileinfo.h>
|
|
|
|
#include <qtextstream.h>
|
|
|
|
#include <qdebug.h>
|
|
|
|
|
2022-12-13 07:01:13 +00:00
|
|
|
static void writeCtfMacro(QTextStream &stream, const Provider &provider, const Tracepoint::Field &field)
|
2017-02-01 19:32:13 +00:00
|
|
|
{
|
|
|
|
const QString ¶mType = field.paramType;
|
|
|
|
const QString &name = field.name;
|
|
|
|
const QString &seqLen = field.seqLen;
|
|
|
|
const int arrayLen = field.arrayLen;
|
|
|
|
|
2022-12-14 07:32:59 +00:00
|
|
|
if (arrayLen > 0) {
|
2023-04-11 09:24:26 +00:00
|
|
|
if (paramType == QStringLiteral("double") || paramType == QStringLiteral("float")) {
|
|
|
|
const char *newline = nullptr;
|
|
|
|
for (int i = 0; i < arrayLen; i++) {
|
|
|
|
stream << newline;
|
|
|
|
stream << "ctf_float(" <<paramType << ", " << name << "_" << QString::number(i) << ", "
|
|
|
|
<< name << "[" << QString::number(i) << "]" << ")";
|
|
|
|
newline = "\n ";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
stream << "ctf_array(" <<paramType << ", "
|
|
|
|
<< name << ", " << name << ", " << arrayLen << ")";
|
|
|
|
}
|
2017-02-01 19:32:13 +00:00
|
|
|
return;
|
2022-12-14 07:32:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 06:22:19 +00:00
|
|
|
switch (field.backendType) {
|
2017-02-01 19:32:13 +00:00
|
|
|
case Tracepoint::Field::Sequence:
|
|
|
|
stream << "ctf_sequence(" << paramType
|
|
|
|
<< ", " << name << ", " << name
|
|
|
|
<< ", unsigned int, " << seqLen << ")";
|
|
|
|
return;
|
2022-12-13 07:01:13 +00:00
|
|
|
case Tracepoint::Field::Boolean:
|
2017-02-01 19:32:13 +00:00
|
|
|
case Tracepoint::Field::Integer:
|
|
|
|
stream << "ctf_integer(" << paramType << ", " << name << ", " << name << ")";
|
|
|
|
return;
|
2018-05-09 18:51:12 +00:00
|
|
|
case Tracepoint::Field::IntegerHex:
|
|
|
|
case Tracepoint::Field::Pointer:
|
|
|
|
stream << "ctf_integer_hex(" << paramType << ", " << name << ", " << name << ")";
|
|
|
|
return;
|
2017-02-01 19:32:13 +00:00
|
|
|
case Tracepoint::Field::Float:
|
|
|
|
stream << "ctf_float(" << paramType << ", " << name << ", " << name << ")";
|
|
|
|
return;
|
|
|
|
case Tracepoint::Field::String:
|
|
|
|
stream << "ctf_string(" << name << ", " << name << ")";
|
|
|
|
return;
|
|
|
|
case Tracepoint::Field::QtString:
|
2022-12-13 07:01:13 +00:00
|
|
|
stream << "ctf_string(" << name << ", " << name << ".toUtf8().constData())";
|
2017-02-01 19:32:13 +00:00
|
|
|
return;
|
|
|
|
case Tracepoint::Field::QtByteArray:
|
|
|
|
stream << "ctf_sequence(const char, " << name << ", "
|
|
|
|
<< name << ".constData(), unsigned int, " << name << ".size())";
|
|
|
|
return;
|
|
|
|
case Tracepoint::Field::QtUrl:
|
2022-12-13 07:01:13 +00:00
|
|
|
stream << "ctf_string(" << name << ", " << name << ".toString().toUtf8().constData())";
|
2017-02-01 19:32:13 +00:00
|
|
|
return;
|
|
|
|
case Tracepoint::Field::QtRect:
|
|
|
|
stream << "ctf_integer(int, x, " << name << ".x()) "
|
|
|
|
<< "ctf_integer(int, y, " << name << ".y()) "
|
|
|
|
<< "ctf_integer(int, width, " << name << ".width()) "
|
|
|
|
<< "ctf_integer(int, height, " << name << ".height()) ";
|
|
|
|
return;
|
2023-01-31 09:05:03 +00:00
|
|
|
case Tracepoint::Field::QtSizeF:
|
|
|
|
stream << "ctf_float(int, width, " << name << ".width()) "
|
|
|
|
<< "ctf_float(int, height, " << name << ".height()) ";
|
|
|
|
return;
|
|
|
|
case Tracepoint::Field::QtRectF:
|
|
|
|
stream << "ctf_float(int, x, " << name << ".x()) "
|
|
|
|
<< "ctf_float(int, y, " << name << ".y()) "
|
|
|
|
<< "ctf_float(int, width, " << name << ".width()) "
|
|
|
|
<< "ctf_float(int, height, " << name << ".height()) ";
|
|
|
|
return;
|
2022-07-27 23:41:21 +00:00
|
|
|
case Tracepoint::Field::QtSize:
|
|
|
|
stream << "ctf_integer(int, width, " << name << ".width()) "
|
|
|
|
<< "ctf_integer(int, height, " << name << ".height()) ";
|
|
|
|
return;
|
2022-12-13 07:01:13 +00:00
|
|
|
case Tracepoint::Field::EnumeratedType:
|
2023-01-17 06:22:19 +00:00
|
|
|
stream << "ctf_enum(" << provider.name << ", " << typeToTypeName(paramType) << ", int, " << name << ", " << name << ") ";
|
2022-12-13 07:01:13 +00:00
|
|
|
return;
|
|
|
|
case Tracepoint::Field::FlagType:
|
|
|
|
stream << "ctf_sequence(const char , " << name << ", "
|
|
|
|
<< name << ".constData(), unsigned int, " << name << ".size())";
|
|
|
|
return;
|
2017-02-01 19:32:13 +00:00
|
|
|
case Tracepoint::Field::Unknown:
|
2022-07-27 23:44:12 +00:00
|
|
|
panic("Cannot deduce CTF type for '%s %s", qPrintable(paramType), qPrintable(name));
|
2017-02-01 19:32:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 13:07:03 +00:00
|
|
|
static void writePrologue(QTextStream &stream, const QString &fileName, const Provider &provider)
|
2017-02-01 19:32:13 +00:00
|
|
|
{
|
2023-03-08 08:41:37 +00:00
|
|
|
writeCommonPrologue(stream);
|
2019-03-26 13:07:03 +00:00
|
|
|
const QString guard = includeGuard(fileName);
|
|
|
|
|
2017-02-01 19:32:13 +00:00
|
|
|
stream << "#undef TRACEPOINT_PROVIDER\n";
|
2019-03-26 13:07:03 +00:00
|
|
|
stream << "#define TRACEPOINT_PROVIDER " << provider.name << "\n";
|
|
|
|
stream << "\n";
|
2017-02-01 19:32:13 +00:00
|
|
|
|
2019-03-26 13:07:03 +00:00
|
|
|
// include prefix text or qt headers only once
|
|
|
|
stream << "#if !defined(" << guard << ")\n";
|
2017-02-01 19:32:13 +00:00
|
|
|
stream << qtHeaders();
|
|
|
|
stream << "\n";
|
2019-03-26 13:07:03 +00:00
|
|
|
if (!provider.prefixText.isEmpty())
|
2022-04-13 12:30:28 +00:00
|
|
|
stream << provider.prefixText.join(u'\n') << "\n\n";
|
2019-03-26 13:07:03 +00:00
|
|
|
stream << "#endif\n\n";
|
2017-02-01 19:32:13 +00:00
|
|
|
|
|
|
|
/* the first guard is the usual one, the second is required
|
|
|
|
* by LTTNG to force the re-evaluation of TRACEPOINT_* macros
|
|
|
|
*/
|
|
|
|
stream << "#if !defined(" << guard << ") || defined(TRACEPOINT_HEADER_MULTI_READ)\n";
|
|
|
|
|
|
|
|
stream << "#define " << guard << "\n\n"
|
|
|
|
<< "#undef TRACEPOINT_INCLUDE\n"
|
|
|
|
<< "#define TRACEPOINT_INCLUDE \"" << fileName << "\"\n\n";
|
|
|
|
|
|
|
|
stream << "#include <lttng/tracepoint.h>\n\n";
|
2021-10-18 16:12:39 +00:00
|
|
|
|
|
|
|
const QString namespaceGuard = guard + QStringLiteral("_USE_NAMESPACE");
|
|
|
|
stream << "#if !defined(" << namespaceGuard << ")\n"
|
|
|
|
<< "#define " << namespaceGuard << "\n"
|
|
|
|
<< "QT_USE_NAMESPACE\n"
|
|
|
|
<< "#endif // " << namespaceGuard << "\n\n";
|
2017-02-01 19:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void writeEpilogue(QTextStream &stream, const QString &fileName)
|
|
|
|
{
|
|
|
|
stream << "\n";
|
|
|
|
stream << "#endif // " << includeGuard(fileName) << "\n"
|
|
|
|
<< "#include <lttng/tracepoint-event.h>\n"
|
|
|
|
<< "#include <private/qtrace_p.h>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeWrapper(QTextStream &stream,
|
2022-12-13 07:01:13 +00:00
|
|
|
const Tracepoint &tracepoint, const Provider &provider)
|
2017-02-01 19:32:13 +00:00
|
|
|
{
|
|
|
|
const QString argList = formatFunctionSignature(tracepoint.args);
|
2022-12-13 07:01:13 +00:00
|
|
|
const QString paramList = formatParameterList(provider, tracepoint.args, tracepoint.fields, LTTNG);
|
2017-02-01 19:32:13 +00:00
|
|
|
const QString &name = tracepoint.name;
|
2022-12-13 07:01:13 +00:00
|
|
|
const QString includeGuard = QStringLiteral("TP_%1_%2").arg(provider.name).arg(name).toUpper();
|
2017-02-01 19:32:13 +00:00
|
|
|
|
|
|
|
/* prevents the redefinion of the inline wrapper functions
|
|
|
|
* once LTTNG recursively includes this header file
|
|
|
|
*/
|
|
|
|
stream << "\n"
|
|
|
|
<< "#ifndef " << includeGuard << "\n"
|
|
|
|
<< "#define " << includeGuard << "\n"
|
2021-10-18 16:12:39 +00:00
|
|
|
<< "QT_BEGIN_NAMESPACE\n"
|
2017-02-01 19:32:13 +00:00
|
|
|
<< "namespace QtPrivate {\n";
|
|
|
|
|
|
|
|
stream << "inline void trace_" << name << "(" << argList << ")\n"
|
|
|
|
<< "{\n"
|
2022-12-13 07:01:13 +00:00
|
|
|
<< " tracepoint(" << provider.name << ", " << name << paramList << ");\n"
|
2017-02-01 19:32:13 +00:00
|
|
|
<< "}\n";
|
|
|
|
|
|
|
|
stream << "inline void do_trace_" << name << "(" << argList << ")\n"
|
|
|
|
<< "{\n"
|
2022-12-13 07:01:13 +00:00
|
|
|
<< " do_tracepoint(" << provider.name << ", " << name << paramList << ");\n"
|
2017-02-01 19:32:13 +00:00
|
|
|
<< "}\n";
|
|
|
|
|
|
|
|
stream << "inline bool trace_" << name << "_enabled()\n"
|
|
|
|
<< "{\n"
|
2022-12-13 07:01:13 +00:00
|
|
|
<< " return tracepoint_enabled(" << provider.name << ", " << name << ");\n"
|
2017-02-01 19:32:13 +00:00
|
|
|
<< "}\n";
|
|
|
|
|
|
|
|
stream << "} // namespace QtPrivate\n"
|
2021-10-18 16:12:39 +00:00
|
|
|
<< "QT_END_NAMESPACE\n"
|
2017-02-01 19:32:13 +00:00
|
|
|
<< "#endif // " << includeGuard << "\n\n";
|
|
|
|
}
|
|
|
|
|
2022-12-13 07:01:13 +00:00
|
|
|
static void writeTracepoint(QTextStream &stream, const Provider &provider,
|
2017-02-01 19:32:13 +00:00
|
|
|
const Tracepoint &tracepoint, const QString &providerName)
|
|
|
|
{
|
|
|
|
stream << "TRACEPOINT_EVENT(\n"
|
|
|
|
<< " " << providerName << ",\n"
|
|
|
|
<< " " << tracepoint.name << ",\n"
|
|
|
|
<< " TP_ARGS(";
|
|
|
|
|
|
|
|
const char *comma = nullptr;
|
|
|
|
|
2022-12-13 07:01:13 +00:00
|
|
|
for (int i = 0; i < tracepoint.args.size(); i++) {
|
|
|
|
const auto &arg = tracepoint.args[i];
|
|
|
|
const auto &field = tracepoint.fields[i];
|
2023-01-17 06:22:19 +00:00
|
|
|
if (field.backendType == Tracepoint::Field::FlagType)
|
2022-12-13 07:01:13 +00:00
|
|
|
stream << comma << "QByteArray, " << arg.name;
|
|
|
|
else
|
|
|
|
stream << comma << arg.type << ", " << arg.name;
|
2017-02-01 19:32:13 +00:00
|
|
|
comma = ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
stream << "),\n"
|
|
|
|
<< " TP_FIELDS(";
|
|
|
|
|
|
|
|
const char *newline = nullptr;
|
|
|
|
|
|
|
|
for (const Tracepoint::Field &f : tracepoint.fields) {
|
|
|
|
stream << newline;
|
2022-12-13 07:01:13 +00:00
|
|
|
writeCtfMacro(stream, provider, f);
|
2017-02-01 19:32:13 +00:00
|
|
|
newline = "\n ";
|
|
|
|
}
|
|
|
|
|
|
|
|
stream << ")\n)\n\n";
|
|
|
|
}
|
|
|
|
|
2022-12-13 07:01:13 +00:00
|
|
|
static void writeEnums(QTextStream &stream, const Provider &provider)
|
|
|
|
{
|
|
|
|
for (const auto &e : provider.enumerations) {
|
|
|
|
stream << "TRACEPOINT_ENUM(\n"
|
|
|
|
<< " " << provider.name << ",\n"
|
2023-01-17 06:22:19 +00:00
|
|
|
<< " " << typeToTypeName(e.name) << ",\n"
|
2022-12-13 07:01:13 +00:00
|
|
|
<< " TP_ENUM_VALUES(\n";
|
2023-01-30 07:01:54 +00:00
|
|
|
QList<int> handledValues;
|
2022-12-13 07:01:13 +00:00
|
|
|
for (const auto &v : e.values) {
|
2023-01-30 07:01:54 +00:00
|
|
|
if (v.range > 0) {
|
2022-12-13 07:01:13 +00:00
|
|
|
stream << " ctf_enum_range(\"" << v.name << "\", " << v.value << ", " << v.range << ")\n";
|
2023-01-30 07:01:54 +00:00
|
|
|
} else if (!handledValues.contains(v.value)) {
|
|
|
|
stream << " ctf_enum_value(\"" << aggregateListValues(v.value, e.values) << "\", " << v.value << ")\n";
|
|
|
|
handledValues.append(v.value);
|
|
|
|
}
|
2022-12-13 07:01:13 +00:00
|
|
|
}
|
|
|
|
stream << " )\n)\n\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeFlags(QTextStream &stream, const Provider &provider)
|
|
|
|
{
|
|
|
|
for (const auto &f : provider.flags) {
|
|
|
|
stream << "TRACEPOINT_ENUM(\n"
|
|
|
|
<< " " << provider.name << ",\n"
|
2023-01-17 06:22:19 +00:00
|
|
|
<< " " << typeToTypeName(f.name) << ",\n"
|
2022-12-13 07:01:13 +00:00
|
|
|
<< " TP_ENUM_VALUES(\n";
|
2023-01-30 07:01:54 +00:00
|
|
|
QList<int> handledValues;
|
|
|
|
for (const auto &v : f.values) {
|
|
|
|
if (!handledValues.contains(v.value)) {
|
|
|
|
stream << " ctf_enum_value(\"" << aggregateListValues(v.value, f.values) << "\", " << v.value << ")\n";
|
|
|
|
handledValues.append(v.value);
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 07:01:13 +00:00
|
|
|
stream << " )\n)\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// converters
|
|
|
|
const QString includeGuard = QStringLiteral("TP_%1_CONVERTERS").arg(provider.name).toUpper();
|
|
|
|
stream << "\n"
|
|
|
|
<< "#ifndef " << includeGuard << "\n"
|
|
|
|
<< "#define " << includeGuard << "\n";
|
|
|
|
stream << "QT_BEGIN_NAMESPACE\n";
|
|
|
|
stream << "namespace QtPrivate {\n";
|
|
|
|
for (const auto &f : provider.flags) {
|
2023-01-17 06:22:19 +00:00
|
|
|
stream << "inline QByteArray trace_convert_" << typeToTypeName(f.name) << "(" << f.name << " val)\n";
|
2022-12-13 07:01:13 +00:00
|
|
|
stream << "{\n";
|
|
|
|
stream << " QByteArray ret;\n";
|
|
|
|
stream << " if (val == 0) { ret.append((char)0); return ret; }\n";
|
|
|
|
|
|
|
|
for (const auto &v : f.values) {
|
|
|
|
if (!v.value)
|
|
|
|
continue;
|
|
|
|
stream << " if (val & " << (1 << (v.value - 1)) << ") { ret.append((char)" << v.value << "); };\n";
|
|
|
|
}
|
|
|
|
stream << " return ret;\n";
|
|
|
|
stream << "}\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
stream << "} // namespace QtPrivate\n"
|
|
|
|
<< "QT_END_NAMESPACE\n\n"
|
|
|
|
<< "#endif // " << includeGuard << "\n\n";
|
|
|
|
}
|
|
|
|
|
2017-02-01 19:32:13 +00:00
|
|
|
static void writeTracepoints(QTextStream &stream, const Provider &provider)
|
|
|
|
{
|
|
|
|
for (const Tracepoint &t : provider.tracepoints) {
|
2022-12-13 07:01:13 +00:00
|
|
|
writeTracepoint(stream, provider, t, provider.name);
|
|
|
|
writeWrapper(stream, t, provider);
|
2017-02-01 19:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeLttng(QFile &file, const Provider &provider)
|
|
|
|
{
|
|
|
|
QTextStream stream(&file);
|
|
|
|
|
|
|
|
const QString fileName = QFileInfo(file.fileName()).fileName();
|
|
|
|
|
2019-03-26 13:07:03 +00:00
|
|
|
writePrologue(stream, fileName, provider);
|
2022-12-13 07:01:13 +00:00
|
|
|
writeEnums(stream, provider);
|
|
|
|
writeFlags(stream, provider);
|
2017-02-01 19:32:13 +00:00
|
|
|
writeTracepoints(stream, provider);
|
|
|
|
writeEpilogue(stream, fileName);
|
|
|
|
}
|
2022-12-13 07:01:13 +00:00
|
|
|
|