Fix crash at exit when tracing

The crash is caused by the cleanup sending trace messages when the
plugin has already been destroyed. Add shutdown callback to the plugin
to indicate this has happened. We can't use signals since that also
generetes trace event.

Pick-to: 6.5
Change-Id: I2e490fc51c2aaa97c240c1496a528a6ff6077bd0
Reviewed-by: Hatem ElKharashy <hatem.elkharashy@qt.io>
Reviewed-by: Janne Koskinen <janne.p.koskinen@qt.io>
This commit is contained in:
Antti Määttä 2023-03-20 08:17:52 +02:00 committed by Janne Koskinen
parent 257b3161c5
commit bb8aada627
5 changed files with 15 additions and 8 deletions

View File

@ -15,6 +15,7 @@ QT_BEGIN_NAMESPACE
static bool s_initialized = false;
static bool s_triedLoading = false;
static bool s_prevent_recursion = false;
static bool s_shutdown = false;
static QCtfLib* s_plugin = nullptr;
#if defined(Q_OS_ANDROID)
@ -59,18 +60,13 @@ static bool loadPlugin(bool &retry)
s_plugin = qobject_cast<QCtfLib *>(loader.instance());
if (!s_plugin)
return false;
QObject *obj = loader.instance();
if (obj) {
QObject::connect(obj, &QObject::destroyed, []() {
s_plugin = nullptr;
});
}
s_plugin->shutdown(&s_shutdown);
return true;
}
static bool initialize()
{
if (s_prevent_recursion)
if (s_shutdown || s_prevent_recursion)
return false;
if (s_initialized || s_triedLoading)
return s_initialized;

View File

@ -232,6 +232,7 @@ public:
virtual void doTracepoint(const QCtfTracePointEvent &point, const QByteArray &arr) = 0;
virtual bool sessionEnabled() = 0;
virtual QCtfTracePointPrivate *initializeTracepoint(const QCtfTracePointEvent &point) = 0;
virtual void shutdown(bool *shutdown) = 0;
};
QT_END_NAMESPACE

View File

@ -50,6 +50,7 @@ void QCtfLibImpl::cleanup()
{
if (s_instance)
delete s_instance;
s_instance = nullptr;
}
QCtfLibImpl::QCtfLibImpl()

View File

@ -78,6 +78,10 @@ public:
QCtfTracePointPrivate *initializeTracepoint(const QCtfTracePointEvent &point) override;
void registerMetadata(const QCtfTraceMetadata &metadata);
int eventId();
void shutdown(bool *) override
{
}
static QCtfLib *instance();
static void cleanup();

View File

@ -22,9 +22,13 @@ public:
~QCtfTracePlugin()
{
m_cleanup = true;
*m_shutdown = true;
QCtfLibImpl::cleanup();
}
void shutdown(bool *shutdown) override
{
m_shutdown = shutdown;
}
bool tracepointEnabled(const QCtfTracePointEvent &point) override
{
if (m_cleanup)
@ -51,6 +55,7 @@ public:
}
private:
bool m_cleanup = false;
bool *m_shutdown = nullptr;
};
#include "qctfplugin.moc"