| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Rafael Roquetto <rafael.roquetto@kdab.com> |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the tools applications of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "lttng.h" |
| 41 | #include "provider.h" |
| 42 | #include "helpers.h" |
| 43 | #include "panic.h" |
| 44 | #include "qtheaders.h" |
| 45 | |
| 46 | #include <qfile.h> |
| 47 | #include <qfileinfo.h> |
| 48 | #include <qtextstream.h> |
| 49 | #include <qdebug.h> |
| 50 | |
| 51 | static void writeCtfMacro(QTextStream &stream, const Tracepoint::Field &field) |
| 52 | { |
| 53 | const QString ¶mType = field.paramType; |
| 54 | const QString &name = field.name; |
| 55 | const QString &seqLen = field.seqLen; |
| 56 | const int arrayLen = field.arrayLen; |
| 57 | |
| 58 | switch (field.backendType) { |
| 59 | case Tracepoint::Field::Array: |
| 60 | stream << "ctf_array(" <<paramType << ", " |
| 61 | << name << ", " << name << ", " << arrayLen << ")" ; |
| 62 | return; |
| 63 | case Tracepoint::Field::Sequence: |
| 64 | stream << "ctf_sequence(" << paramType |
| 65 | << ", " << name << ", " << name |
| 66 | << ", unsigned int, " << seqLen << ")" ; |
| 67 | return; |
| 68 | case Tracepoint::Field::Integer: |
| 69 | stream << "ctf_integer(" << paramType << ", " << name << ", " << name << ")" ; |
| 70 | return; |
| 71 | case Tracepoint::Field::IntegerHex: |
| 72 | case Tracepoint::Field::Pointer: |
| 73 | stream << "ctf_integer_hex(" << paramType << ", " << name << ", " << name << ")" ; |
| 74 | return; |
| 75 | case Tracepoint::Field::Float: |
| 76 | stream << "ctf_float(" << paramType << ", " << name << ", " << name << ")" ; |
| 77 | return; |
| 78 | case Tracepoint::Field::String: |
| 79 | stream << "ctf_string(" << name << ", " << name << ")" ; |
| 80 | return; |
| 81 | case Tracepoint::Field::QtString: |
| 82 | stream << "ctf_sequence(const ushort, " << name << ", " |
| 83 | << name << ".utf16(), unsigned int, " << name << ".size())" ; |
| 84 | return; |
| 85 | case Tracepoint::Field::QtByteArray: |
| 86 | stream << "ctf_sequence(const char, " << name << ", " |
| 87 | << name << ".constData(), unsigned int, " << name << ".size())" ; |
| 88 | return; |
| 89 | case Tracepoint::Field::QtUrl: |
| 90 | stream << "ctf_sequence(const char, " << name << ", " |
| 91 | << name << ".toEncoded().constData(), unsigned int, " |
| 92 | << name << ".toEncoded().size())" ; |
| 93 | return; |
| 94 | case Tracepoint::Field::QtRect: |
| 95 | stream << "ctf_integer(int, x, " << name << ".x()) " |
| 96 | << "ctf_integer(int, y, " << name << ".y()) " |
| 97 | << "ctf_integer(int, width, " << name << ".width()) " |
| 98 | << "ctf_integer(int, height, " << name << ".height()) " ; |
| 99 | return; |
| 100 | case Tracepoint::Field::Unknown: |
| 101 | justified_worry(fmt: "Cannot deduce CTF type for '%s %s'" , qPrintable(paramType), |
| 102 | qPrintable(name)); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void writePrologue(QTextStream &stream, const QString &fileName, const Provider &provider) |
| 108 | { |
| 109 | const QString guard = includeGuard(filename: fileName); |
| 110 | |
| 111 | stream << "#undef TRACEPOINT_PROVIDER\n" ; |
| 112 | stream << "#define TRACEPOINT_PROVIDER " << provider.name << "\n" ; |
| 113 | stream << "\n" ; |
| 114 | |
| 115 | // include prefix text or qt headers only once |
| 116 | stream << "#if !defined(" << guard << ")\n" ; |
| 117 | stream << qtHeaders(); |
| 118 | stream << "\n" ; |
| 119 | if (!provider.prefixText.isEmpty()) |
| 120 | stream << provider.prefixText.join(sep: QLatin1Char('\n')) << "\n\n" ; |
| 121 | stream << "#endif\n\n" ; |
| 122 | |
| 123 | /* the first guard is the usual one, the second is required |
| 124 | * by LTTNG to force the re-evaluation of TRACEPOINT_* macros |
| 125 | */ |
| 126 | stream << "#if !defined(" << guard << ") || defined(TRACEPOINT_HEADER_MULTI_READ)\n" ; |
| 127 | |
| 128 | stream << "#define " << guard << "\n\n" |
| 129 | << "#undef TRACEPOINT_INCLUDE\n" |
| 130 | << "#define TRACEPOINT_INCLUDE \"" << fileName << "\"\n\n" ; |
| 131 | |
| 132 | stream << "#include <lttng/tracepoint.h>\n\n" ; |
| 133 | } |
| 134 | |
| 135 | static void writeEpilogue(QTextStream &stream, const QString &fileName) |
| 136 | { |
| 137 | stream << "\n" ; |
| 138 | stream << "#endif // " << includeGuard(filename: fileName) << "\n" |
| 139 | << "#include <lttng/tracepoint-event.h>\n" |
| 140 | << "#include <private/qtrace_p.h>\n" ; |
| 141 | } |
| 142 | |
| 143 | static void writeWrapper(QTextStream &stream, |
| 144 | const Tracepoint &tracepoint, const QString &providerName) |
| 145 | { |
| 146 | const QString argList = formatFunctionSignature(args: tracepoint.args); |
| 147 | const QString paramList = formatParameterList(args: tracepoint.args, type: LTTNG); |
| 148 | const QString &name = tracepoint.name; |
| 149 | const QString includeGuard = QStringLiteral("TP_%1_%2" ).arg(a: providerName).arg(a: name).toUpper(); |
| 150 | |
| 151 | /* prevents the redefinion of the inline wrapper functions |
| 152 | * once LTTNG recursively includes this header file |
| 153 | */ |
| 154 | stream << "\n" |
| 155 | << "#ifndef " << includeGuard << "\n" |
| 156 | << "#define " << includeGuard << "\n" |
| 157 | << "namespace QtPrivate {\n" ; |
| 158 | |
| 159 | stream << "inline void trace_" << name << "(" << argList << ")\n" |
| 160 | << "{\n" |
| 161 | << " tracepoint(" << providerName << ", " << name << paramList << ");\n" |
| 162 | << "}\n" ; |
| 163 | |
| 164 | stream << "inline void do_trace_" << name << "(" << argList << ")\n" |
| 165 | << "{\n" |
| 166 | << " do_tracepoint(" << providerName << ", " << name << paramList << ");\n" |
| 167 | << "}\n" ; |
| 168 | |
| 169 | stream << "inline bool trace_" << name << "_enabled()\n" |
| 170 | << "{\n" |
| 171 | << " return tracepoint_enabled(" << providerName << ", " << name << ");\n" |
| 172 | << "}\n" ; |
| 173 | |
| 174 | stream << "} // namespace QtPrivate\n" |
| 175 | << "#endif // " << includeGuard << "\n\n" ; |
| 176 | } |
| 177 | |
| 178 | static void writeTracepoint(QTextStream &stream, |
| 179 | const Tracepoint &tracepoint, const QString &providerName) |
| 180 | { |
| 181 | stream << "TRACEPOINT_EVENT(\n" |
| 182 | << " " << providerName << ",\n" |
| 183 | << " " << tracepoint.name << ",\n" |
| 184 | << " TP_ARGS(" ; |
| 185 | |
| 186 | const char *comma = nullptr; |
| 187 | |
| 188 | for (const Tracepoint::Argument &arg : tracepoint.args) { |
| 189 | stream << comma << arg.type << ", " << arg.name; |
| 190 | comma = ", " ; |
| 191 | } |
| 192 | |
| 193 | stream << "),\n" |
| 194 | << " TP_FIELDS(" ; |
| 195 | |
| 196 | const char *newline = nullptr; |
| 197 | |
| 198 | for (const Tracepoint::Field &f : tracepoint.fields) { |
| 199 | stream << newline; |
| 200 | writeCtfMacro(stream, field: f); |
| 201 | newline = "\n " ; |
| 202 | } |
| 203 | |
| 204 | stream << ")\n)\n\n" ; |
| 205 | } |
| 206 | |
| 207 | static void writeTracepoints(QTextStream &stream, const Provider &provider) |
| 208 | { |
| 209 | for (const Tracepoint &t : provider.tracepoints) { |
| 210 | writeTracepoint(stream, tracepoint: t, providerName: provider.name); |
| 211 | writeWrapper(stream, tracepoint: t, providerName: provider.name); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void writeLttng(QFile &file, const Provider &provider) |
| 216 | { |
| 217 | QTextStream stream(&file); |
| 218 | |
| 219 | const QString fileName = QFileInfo(file.fileName()).fileName(); |
| 220 | |
| 221 | writePrologue(stream, fileName, provider); |
| 222 | writeTracepoints(stream, provider); |
| 223 | writeEpilogue(stream, fileName); |
| 224 | } |
| 225 | |