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 "etw.h" |
41 | #include "provider.h" |
42 | #include "helpers.h" |
43 | #include "qtheaders.h" |
44 | |
45 | #include <qfile.h> |
46 | #include <qfileinfo.h> |
47 | #include <qtextstream.h> |
48 | #include <quuid.h> |
49 | |
50 | static inline QString providerVar(const QString &providerName) |
51 | { |
52 | return providerName + QLatin1String("_provider" ); |
53 | } |
54 | |
55 | static void writeEtwMacro(QTextStream &stream, const Tracepoint::Field &field) |
56 | { |
57 | const QString &name = field.name; |
58 | |
59 | switch (field.backendType) { |
60 | case Tracepoint::Field::QtString: |
61 | stream << "TraceLoggingCountedWideString(reinterpret_cast<LPCWSTR>(" |
62 | << name << ".utf16()), " << name << ".size(), \"" << name << "\")" ; |
63 | return; |
64 | case Tracepoint::Field::QtByteArray: |
65 | stream << "TraceLoggingBinary(" << name << ".constData(), " |
66 | << name << ".size(), \"" << name << "\")" ; |
67 | return; |
68 | case Tracepoint::Field::QtUrl: |
69 | stream << "TraceLoggingValue(" << name << ".toEncoded().constData(), \"" << name << "\")" ; |
70 | return; |
71 | case Tracepoint::Field::QtRect: |
72 | stream << "TraceLoggingValue(" << name << ".x(), \"x\"), " |
73 | << "TraceLoggingValue(" << name << ".y(), \"y\"), " |
74 | << "TraceLoggingValue(" << name << ".width(), \"width\"), " |
75 | << "TraceLoggingValue(" << name << ".height(), \"height\")" ; |
76 | return; |
77 | case Tracepoint::Field::Pointer: |
78 | stream << "TraceLoggingPointer(" << name << ", \"" << name << "\")" ; |
79 | return; |
80 | default: |
81 | break; |
82 | } |
83 | |
84 | stream << "TraceLoggingValue(" << name << ", \"" << name << "\")" ; |
85 | } |
86 | |
87 | static QString createGuid(const QUuid &uuid) |
88 | { |
89 | QString guid; |
90 | |
91 | QTextStream stream(&guid); |
92 | |
93 | Qt::hex(s&: stream); |
94 | |
95 | stream << "(" |
96 | << "0x" << uuid.data1 << ", " |
97 | << "0x" << uuid.data2 << ", " |
98 | << "0x" << uuid.data3 << ", " |
99 | << "0x" << uuid.data4[0] << ", " |
100 | << "0x" << uuid.data4[1] << ", " |
101 | << "0x" << uuid.data4[2] << ", " |
102 | << "0x" << uuid.data4[3] << ", " |
103 | << "0x" << uuid.data4[4] << ", " |
104 | << "0x" << uuid.data4[5] << ", " |
105 | << "0x" << uuid.data4[6] << ", " |
106 | << "0x" << uuid.data4[7] |
107 | << ")" ; |
108 | |
109 | return guid; |
110 | } |
111 | |
112 | static void writePrologue(QTextStream &stream, const QString &fileName, const Provider &provider) |
113 | { |
114 | QUuid uuid = QUuid::createUuidV5(ns: QUuid(), baseData: provider.name.toLocal8Bit()); |
115 | |
116 | const QString providerV = providerVar(providerName: provider.name); |
117 | const QString guard = includeGuard(filename: fileName); |
118 | const QString guid = createGuid(uuid); |
119 | const QString guidString = uuid.toString(); |
120 | |
121 | stream << "#ifndef " << guard << "\n" |
122 | << "#define " << guard << "\n" |
123 | << "\n" |
124 | << "#include <windows.h>\n" |
125 | << "#include <TraceLoggingProvider.h>\n" |
126 | << "\n" ; |
127 | |
128 | /* TraceLogging API macros cannot deal with UTF8 |
129 | * source files, so we work around it like this |
130 | */ |
131 | stream << "#undef _TlgPragmaUtf8Begin\n" |
132 | "#undef _TlgPragmaUtf8End\n" |
133 | "#define _TlgPragmaUtf8Begin\n" |
134 | "#define _TlgPragmaUtf8End\n" ; |
135 | |
136 | stream << "\n" ; |
137 | stream << qtHeaders(); |
138 | stream << "\n" ; |
139 | |
140 | if (!provider.prefixText.isEmpty()) |
141 | stream << provider.prefixText.join(sep: QLatin1Char('\n')) << "\n\n" ; |
142 | |
143 | stream << "#ifdef TRACEPOINT_DEFINE\n" |
144 | << "/* " << guidString << " */\n" |
145 | << "TRACELOGGING_DEFINE_PROVIDER(" << providerV << ", \"" |
146 | << provider.name <<"\", " << guid << ");\n\n" ; |
147 | |
148 | stream << "static inline void registerProvider()\n" |
149 | << "{\n" |
150 | << " TraceLoggingRegister(" << providerV << ");\n" |
151 | << "}\n\n" ; |
152 | |
153 | stream << "static inline void unregisterProvider()\n" |
154 | << "{\n" |
155 | << " TraceLoggingUnregister(" << providerV << ");\n" |
156 | << "}\n" ; |
157 | |
158 | stream << "Q_CONSTRUCTOR_FUNCTION(registerProvider)\n" |
159 | << "Q_DESTRUCTOR_FUNCTION(unregisterProvider)\n\n" ; |
160 | |
161 | stream << "#else\n" |
162 | << "TRACELOGGING_DECLARE_PROVIDER(" << providerV << ");\n" |
163 | << "#endif // TRACEPOINT_DEFINE\n\n" ; |
164 | } |
165 | |
166 | static void writeEpilogue(QTextStream &stream, const QString &fileName) |
167 | { |
168 | stream << "\n#endif // " << includeGuard(filename: fileName) << "\n" |
169 | << "#include <private/qtrace_p.h>\n" ; |
170 | } |
171 | |
172 | static void writeWrapper(QTextStream &stream, const Tracepoint &tracepoint, |
173 | const QString &providerName) |
174 | { |
175 | const QString argList = formatFunctionSignature(args: tracepoint.args); |
176 | const QString paramList = formatParameterList(args: tracepoint.args, type: ETW); |
177 | const QString &name = tracepoint.name; |
178 | const QString includeGuard = QStringLiteral("TP_%1_%2" ).arg(a: providerName).arg(a: name).toUpper(); |
179 | const QString provider = providerVar(providerName); |
180 | |
181 | stream << "\n" ; |
182 | |
183 | stream << "inline void trace_" << name << "(" << argList << ")\n" |
184 | << "{\n" |
185 | << " TraceLoggingWrite(" << provider << ", \"" << name << "\"" ; |
186 | |
187 | for (const Tracepoint::Field &field : tracepoint.fields) { |
188 | stream << ",\n" ; |
189 | stream << " " ; |
190 | writeEtwMacro(stream, field); |
191 | } |
192 | |
193 | stream << ");\n" |
194 | << "}\n\n" ; |
195 | |
196 | stream << "inline void do_trace_" << name << "(" << argList << ")\n" |
197 | << "{\n" |
198 | << " trace_" << name << "(" << paramList << ");\n" |
199 | << "}\n" ; |
200 | |
201 | stream << "inline bool trace_" << name << "_enabled()\n" |
202 | << "{\n" |
203 | << " return TraceLoggingProviderEnabled(" << provider << ", 0, 0);\n" |
204 | << "}\n" ; |
205 | } |
206 | |
207 | static void writeTracepoints(QTextStream &stream, const Provider &provider) |
208 | { |
209 | if (provider.tracepoints.isEmpty()) |
210 | return; |
211 | |
212 | const QString includeGuard = QStringLiteral("TP_%1_PROVIDER" ).arg(a: provider.name).toUpper(); |
213 | |
214 | stream << "#if !defined(" << includeGuard << ") && !defined(TRACEPOINT_DEFINE)\n" |
215 | << "#define " << includeGuard << "\n" |
216 | << "namespace QtPrivate {\n" ; |
217 | |
218 | for (const Tracepoint &t : provider.tracepoints) |
219 | writeWrapper(stream, tracepoint: t, providerName: provider.name); |
220 | |
221 | stream << "} // namespace QtPrivate\n" |
222 | << "#endif // " << includeGuard << "\n\n" ; |
223 | } |
224 | |
225 | void writeEtw(QFile &file, const Provider &provider) |
226 | { |
227 | QTextStream stream(&file); |
228 | |
229 | const QString fileName = QFileInfo(file.fileName()).fileName(); |
230 | |
231 | writePrologue(stream, fileName, provider); |
232 | writeTracepoints(stream, provider); |
233 | writeEpilogue(stream, fileName); |
234 | } |
235 | |
236 | |