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
qctf.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#define BUILD_LIBRARY
5
6#include <qthread.h>
7#include <qpluginloader.h>
8#include <qfileinfo.h>
9#include <qdir.h>
10#include <qtenvironmentvariables.h>
11#include <qjsonarray.h>
12
13#include "qctf_p.h"
14
16
17using namespace Qt::StringLiterals;
18
19static bool s_initialized = false;
20static bool s_triedLoading = false;
21static bool s_prevent_recursion = false;
22static bool s_shutdown = false;
23static QCtfLib* s_plugin = nullptr;
24
25#if QT_CONFIG(library) && defined(QT_SHARED)
26
27#if defined(Q_OS_ANDROID)
28static QString findPlugin(QLatin1StringView plugin)
29{
30 const QString pluginPath = qEnvironmentVariable("QT_PLUGIN_PATH");
31 for (const auto &entry : QDirListing(pluginPath, QDirListing::IteratorFlag::FilesOnly)) {
32 if (entry.fileName().contains(plugin))
33 return entry.absoluteFilePath();
34 }
35 return {};
36}
37#endif
38
39static bool loadPlugin(bool &retry)
40{
41 retry = false;
42#ifdef Q_OS_WIN
43#ifdef QT_DEBUG
44 QPluginLoader loader(QStringLiteral("tracing/QCtfTracePlugind.dll"));
45#else
46 QPluginLoader loader(QStringLiteral("tracing/QCtfTracePlugin.dll"));
47#endif
48#elif defined(Q_OS_ANDROID)
49
50 const QString plugin = findPlugin("QCtfTracePlugin"_L1);
51 if (plugin.isEmpty()) {
52 retry = true;
53 return false;
54 }
55 QPluginLoader loader(plugin);
56#else
57 QPluginLoader loader(QStringLiteral("tracing/libQCtfTracePlugin.so"));
58#endif
59
60 if (!loader.isLoaded()) {
61 if (!loader.load())
62 return false;
63 }
64 s_plugin = qobject_cast<QCtfLib *>(loader.instance());
65 if (!s_plugin)
66 return false;
67 s_plugin->shutdown(&s_shutdown);
68 return true;
69}
70
71#else
72
73#define QCtfPluginIID QStringLiteral("org.qt-project.Qt.QCtfLib")
74
75static bool loadPlugin(bool &retry)
76{
77 retry = false;
78 const auto &plugins = QPluginLoader::staticPlugins();
79 for (const auto &plugin : plugins) {
80 const auto json = plugin.metaData();
81 const auto IID = json[QStringLiteral("IID")];
82 if (IID.toString() == QCtfPluginIID) {
83 s_plugin = qobject_cast<QCtfLib *>(plugin.instance());
84 if (!s_plugin)
85 return false;
86 s_plugin->shutdown(&s_shutdown);
87 return true;
88 }
89 }
90 return false;
91}
92
93#endif
94
95static bool initialize()
96{
98 return false;
100 return s_initialized;
101 s_prevent_recursion = true;
102 bool retry = false;
103 if (!loadPlugin(retry)) {
104 if (!retry) {
105 s_triedLoading = true;
106 s_initialized = false;
107 }
108 } else {
109 bool enabled = s_plugin->sessionEnabled();
110 if (!enabled) {
111 s_triedLoading = true;
112 s_initialized = false;
113 } else {
114 s_initialized = true;
115 }
116 }
117 s_prevent_recursion = false;
118 return s_initialized;
119}
120
121bool _tracepoint_enabled(const QCtfTracePointEvent &point)
122{
123 if (!initialize())
124 return false;
125 return s_plugin ? s_plugin->tracepointEnabled(point) : false;
126}
127
128void _do_tracepoint(const QCtfTracePointEvent &point, const QByteArray &arr)
129{
130 if (!initialize())
131 return;
132 if (s_plugin)
133 s_plugin->doTracepoint(point, arr);
134}
135
136QCtfTracePointPrivate *_initialize_tracepoint(const QCtfTracePointEvent &point)
137{
138 if (!initialize())
139 return nullptr;
140 return s_plugin ? s_plugin->initializeTracepoint(point) : nullptr;
141}
142
143QT_END_NAMESPACE
144
145#include "moc_qctf_p.cpp"
Combined button and popup list for selecting options.
static bool s_prevent_recursion
Definition qctf.cpp:21
bool _tracepoint_enabled(const QCtfTracePointEvent &point)
Definition qctf.cpp:121
static QCtfLib * s_plugin
Definition qctf.cpp:23
void _do_tracepoint(const QCtfTracePointEvent &point, const QByteArray &arr)
Definition qctf.cpp:128
static bool s_shutdown
Definition qctf.cpp:22
static bool loadPlugin(bool &retry)
Definition qctf.cpp:75
static bool s_initialized
Definition qctf.cpp:19
QCtfTracePointPrivate * _initialize_tracepoint(const QCtfTracePointEvent &point)
Definition qctf.cpp:136
static bool s_triedLoading
Definition qctf.cpp:20
#define QCtfPluginIID
Definition qctf.cpp:73
static bool initialize()
Definition qctf.cpp:95