Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
callback.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "callback.h"
5#include "provider.h"
6#include "helpers.h"
7#include "qtheaders.h"
8
9#include <qfile.h>
10#include <qfileinfo.h>
11#include <qtextstream.h>
12
13// The "callback" backend turns every tracepoint into a call to a single
14// process-wide hook (qtTraceCallbackFunction), passing a pointer to a static
15// descriptor of the firing point, and a vararg list of arguments. This is
16// intended for in-process profilers that attribute time to whichever
17// tracepoint is currently executing (entry/exit pairs form a stack), not for
18// offline trace capture.
19
20static void writePrologue(QTextStream &stream, const QString &fileName, const Provider &provider)
21{
22 writeCommonPrologue(stream);
23 const QString guard = includeGuard(fileName);
24
25 stream << "\n#include <private/qtracecallback_p.h>\n\n";
26
27 stream << "#if !defined(" << guard << ")\n";
28 stream << qtHeaders();
29 stream << "\n";
30 if (!provider.prefixText.isEmpty())
31 stream << provider.prefixText.join(u'\n') << "\n\n";
32 stream << "#endif\n\n";
33
34 stream << "#if !defined(" << guard << ")\n";
35 stream << "#define " << guard << "\n\n";
36
37 const QString namespaceGuard = guard + QStringLiteral("_USE_NAMESPACE");
38 stream << "#if !defined(" << namespaceGuard << ")\n"
39 << "#define " << namespaceGuard << "\n"
40 << "QT_USE_NAMESPACE\n"
41 << "#endif // " << namespaceGuard << "\n\n";
42}
43
44static void writeEpilogue(QTextStream &stream, const QString &fileName)
45{
46 stream << "\n";
47 stream << "#endif // " << includeGuard(fileName) << "\n"
48 << "#include <private/qtrace_p.h>\n";
49}
50
51static void writeWrapper(QTextStream &stream, const Tracepoint &tracepoint,
52 const Provider &provider)
53{
54 const QString argList = formatFunctionSignature(tracepoint.args);
55 const QString paramList =
56 formatParameterList(provider, tracepoint.args, tracepoint.fields, CALLBACK);
57 const QString &name = tracepoint.name;
58 const QString includeGuard = QStringLiteral("TP_%1_%2").arg(provider.name, name).toUpper();
59
60 stream << "\n"
61 << "#ifndef " << includeGuard << "\n"
62 << "#define " << includeGuard << "\n"
63 << "QT_BEGIN_NAMESPACE\n"
64 << "namespace QtPrivate {\n";
65
66 const auto writeBody = [&](const char *enabledCheck) {
67 stream << "{\n";
68
69 // Constant-initialised aggregate: no thread-safe-init guard, and a unique stable
70 // address per tracepoint that the callback can key on. cookie starts at -1.
71 // The callback function has to perform any thread synchronization itself.
72 stream << " static QTraceCallbackTracepoint qtp { \"" << provider.name << "\", \""
73 << name << "\", -1 };\n"
74 << " " << enabledCheck << "\n"
75 << " qtTraceCallbackFunction(&qtp" << (paramList.isEmpty() ? "" : ", ")
76 << paramList << ");\n"
77 << "}\n";
78 };
79
80 stream << "inline void do_trace_" << name << "(" << argList << ")\n";
81 writeBody("if (qtTraceCallbackFunction)");
82
83 stream << "inline void trace_" << name << "(" << argList << ") { do_trace_" << name << "("
84 << paramList << "); }\n";
85
86 stream << "inline bool trace_" << name << "_enabled()\n"
87 << "{\n"
88 << " return qtTraceCallbackFunction != nullptr;\n"
89 << "}\n";
90
91 stream << "} // namespace QtPrivate\n"
92 << "QT_END_NAMESPACE\n"
93 << "#endif // " << includeGuard << "\n";
94}
95
96void writeCallback(QFile &file, const Provider &provider)
97{
98 QTextStream stream(&file);
99
100 const QString fileName = QFileInfo(file.fileName()).fileName();
101
102 writePrologue(stream, fileName, provider);
103 for (const Tracepoint &t : provider.tracepoints)
104 writeWrapper(stream, t, provider);
105 writeEpilogue(stream, fileName);
106}
static void writeEpilogue(QTextStream &stream, const QString &fileName)
Definition callback.cpp:44
void writeCallback(QFile &file, const Provider &provider)
Definition callback.cpp:96
static void writeWrapper(QTextStream &stream, const Tracepoint &tracepoint, const Provider &provider)
Definition callback.cpp:51
static void writePrologue(QTextStream &stream, const QString &fileName, const Provider &provider)
Definition callback.cpp:20
#define QStringLiteral(str)
Definition qstring.h:1847