From 127518deda5a05fda2ce24be98aaaeb7b975f163 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Wed, 27 Mar 2019 13:56:57 +0100 Subject: Introduce Q_TRACE_SCOPE to simplify tracing of a function entry/exit Additionally, we also add a Q_TRACE_EXIT which runs a trace point when the scope is exited, leveraging qScopeGuard behind the scenes. Q_TRACE_SCOPE uses Q_TRACE_EXIT internally - the difference is that the _SCOPE version enforces the naming scheme of _entry / _exit for the tracepoints, whereas Q_TRACE_EXIT can be used generically. Change-Id: I4a2f5ea09f451fcf664d07fd493b679f7527ac06 Reviewed-by: Thiago Macieira --- src/corelib/global/qtrace_p.h | 17 ++++++++++++++++- src/corelib/kernel/qcoreapplication.cpp | 4 +--- src/gui/kernel/qguiapplication.cpp | 8 ++------ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/corelib/global/qtrace_p.h b/src/corelib/global/qtrace_p.h index 3d04a7311d..20f2beac98 100644 --- a/src/corelib/global/qtrace_p.h +++ b/src/corelib/global/qtrace_p.h @@ -52,11 +52,18 @@ // /* - * The Qt tracepoints API consists of only three macros: + * The Qt tracepoints API consists of only five macros: * * - Q_TRACE(tracepoint, args...) * Fires 'tracepoint' if it is enabled. * + * - Q_TRACE_EXIT(tracepoint, args...) + * Fires 'tracepoint' if it is enabled when the current scope exists. + * + * - Q_TRACE_SCOPE(tracepoint, args...) + * Wrapper around Q_TRACE/_EXIT to trace entry and exit. First it traces + * `${tracepoint}_entry` and then `${tracepoint}_exit` on scope exit. + * * - Q_UNCONDITIONAL_TRACE(tracepoint, args...) * Fires 'tracepoint' unconditionally: no check is performed to query * whether 'tracepoint' is enabled. @@ -110,15 +117,23 @@ */ #include +#include QT_BEGIN_NAMESPACE #if defined(Q_TRACEPOINT) && !defined(QT_BOOTSTRAPPED) # define Q_TRACE(x, ...) QtPrivate::trace_ ## x(__VA_ARGS__) +# define Q_TRACE_EXIT(x, ...) \ + const auto qTraceExit_ ## x ## __COUNTER__ = qScopeGuard([&]() { Q_TRACE(x, __VA_ARGS__); }); +# define Q_TRACE_SCOPE(x, ...) \ + Q_TRACE(x ## _entry, __VA_ARGS__); \ + Q_TRACE_EXIT(x ## _exit, __VA_ARGS__); # define Q_UNCONDITIONAL_TRACE(x, ...) QtPrivate::do_trace_ ## x(__VA_ARGS__) # define Q_TRACE_ENABLED(x) QtPrivate::trace_ ## x ## _enabled() #else # define Q_TRACE(x, ...) +# define Q_TRACE_EXIT(x, ...) +# define Q_TRACE_SCOPE(x, ...) # define Q_UNCONDITIONAL_TRACE(x, ...) # define Q_TRACE_ENABLED(x) false #endif // defined(Q_TRACEPOINT) && !defined(QT_BOOTSTRAPPED) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 6d7985c91b..d173ec029b 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -778,7 +778,7 @@ QCoreApplication::QCoreApplication(int &argc, char **argv void QCoreApplicationPrivate::init() { - Q_TRACE(QCoreApplicationPrivate_init_entry); + Q_TRACE_SCOPE(QCoreApplicationPrivate_init); #if defined(Q_OS_MACOS) QMacAutoReleasePool pool; @@ -883,8 +883,6 @@ void QCoreApplicationPrivate::init() #ifndef QT_NO_QOBJECT is_app_running = true; // No longer starting up. #endif - - Q_TRACE(QCoreApplicationPrivate_init_exit); } /*! diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index fd01f8bb7b..424af20f26 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -1421,7 +1421,7 @@ void QGuiApplicationPrivate::eventDispatcherReady() void QGuiApplicationPrivate::init() { - Q_TRACE(QGuiApplicationPrivate_init_entry); + Q_TRACE_SCOPE(QGuiApplicationPrivate_init); #if defined(Q_OS_MACOS) QMacAutoReleasePool pool; @@ -1585,8 +1585,6 @@ void QGuiApplicationPrivate::init() if (!QGuiApplicationPrivate::displayName) QObject::connect(q, &QGuiApplication::applicationNameChanged, q, &QGuiApplication::applicationDisplayNameChanged); - - Q_TRACE(QGuiApplicationPrivate_init_exit); } extern void qt_cleanupFontDatabase(); @@ -1830,7 +1828,7 @@ bool QGuiApplicationPrivate::processNativeEvent(QWindow *window, const QByteArra void QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *e) { - Q_TRACE(QGuiApplicationPrivate_processWindowSystemEvent_entry, e->type); + Q_TRACE_SCOPE(QGuiApplicationPrivate_processWindowSystemEvent, e->type); switch(e->type) { case QWindowSystemInterfacePrivate::Mouse: @@ -1940,8 +1938,6 @@ void QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePriv qWarning() << "Unknown user input event type:" << e->type; break; } - - Q_TRACE(QGuiApplicationPrivate_processWindowSystemEvent_exit, e->type); } /*! \internal -- cgit v1.2.3