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