| 1 | /* |
| 2 | This file is part of the KDE Libraries |
| 3 | SPDX-FileCopyrightText: 2000 Timo Hummel <timo.hummel@sap.com> |
| 4 | SPDX-FileCopyrightText: 2000 Tom Braun <braunt@fh-konstanz.de> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef KCRASH_H |
| 10 | #define KCRASH_H |
| 11 | |
| 12 | #include <kcrash_export.h> |
| 13 | |
| 14 | #include <qglobal.h> |
| 15 | |
| 16 | class QString; |
| 17 | |
| 18 | /*! |
| 19 | * \namespace KCrash |
| 20 | * \inmodule KCrash |
| 21 | * |
| 22 | * \brief This namespace contains functions to handle crashes. |
| 23 | * |
| 24 | * It allows you to set a crash handler function that will be called |
| 25 | * when your application crashes and also provides a default crash |
| 26 | * handler that implements the following functionality: |
| 27 | * \list |
| 28 | * \li Launches the KDE crash display application (DrKonqi) to let |
| 29 | * the user report the bug and/or debug it. |
| 30 | * \li Calls an emergency save function that you can set with |
| 31 | * setEmergencySaveFunction() to attempt to save the application's data. |
| 32 | * \li Autorestarts your application. |
| 33 | * \endlist |
| 34 | * |
| 35 | * \note All the above features are optional and you need to enable them |
| 36 | * explicitly. By default, the defaultCrashHandler() will not do anything. |
| 37 | * However, if you are using KApplication, it will by default enable launching |
| 38 | * DrKonqi on crashes, unless the --nocrashhandler argument was passed on |
| 39 | * the command line or the environment variable KDE_DEBUG is set to any value. |
| 40 | */ |
| 41 | namespace KCrash |
| 42 | { |
| 43 | /*! |
| 44 | * Initialize KCrash. |
| 45 | * |
| 46 | * This does nothing if $KDE_DEBUG is set. |
| 47 | * |
| 48 | * Call this in your main() after setting up KAboutData to ensure that the crash handler is launched. |
| 49 | * \since 5.15 |
| 50 | */ |
| 51 | KCRASH_EXPORT void initialize(); |
| 52 | |
| 53 | /*! |
| 54 | * The default crash handler. |
| 55 | * Do not call this function directly. Instead, use |
| 56 | * setCrashHandler() to set it as your application's crash handler. |
| 57 | * |
| 58 | * \a signal the signal number |
| 59 | * |
| 60 | * \note If you implement your own crash handler, you will have to |
| 61 | * call this function from your implementation if you want to use the |
| 62 | * features of this namespace. |
| 63 | */ |
| 64 | KCRASH_EXPORT void defaultCrashHandler(int signal); |
| 65 | |
| 66 | /*! |
| 67 | * \typedef KCrash::HandlerType |
| 68 | * |
| 69 | * Typedef for a pointer to a crash handler function. |
| 70 | * The function's argument is the number of the signal. |
| 71 | */ |
| 72 | typedef void (*HandlerType)(int); |
| 73 | |
| 74 | /*! |
| 75 | * Install a function to be called when a crash occurs. |
| 76 | * A crash occurs when one of the following signals is |
| 77 | * caught: SIGSEGV, SIGBUS, SIGFPE, SIGILL, SIGABRT. |
| 78 | * |
| 79 | * \a handler this can be one of: |
| 80 | * \list |
| 81 | * \li null, in which case signal catching is disabled |
| 82 | * (by setting the signal handler for the crash signals to SIG_DFL) |
| 83 | * \li a user defined function in the form: |
| 84 | * static (if in a class) void myCrashHandler(int); |
| 85 | * \li if handler is omitted, the default crash handler is installed |
| 86 | * \endlist |
| 87 | * |
| 88 | * \note If you use setDrKonqiEnabled(true), setEmergencySaveFunction(myfunc) |
| 89 | * or setFlags(AutoRestart), you do not need to call this function |
| 90 | * explicitly. The default crash handler is automatically installed by |
| 91 | * those functions if needed. However, if you set a custom crash handler, |
| 92 | * those functions will not change it. |
| 93 | */ |
| 94 | KCRASH_EXPORT void setCrashHandler(HandlerType handler = defaultCrashHandler); |
| 95 | |
| 96 | /*! |
| 97 | * Returns the installed crash handler. |
| 98 | */ |
| 99 | KCRASH_EXPORT HandlerType crashHandler(); |
| 100 | |
| 101 | /*! |
| 102 | * Installs a function which should try to save the application's data. |
| 103 | * |
| 104 | * \note It is the crash handler's responsibility to call this function. |
| 105 | * Therefore, if no crash handler is set, the default crash handler |
| 106 | * is installed to ensure the save function will be called. |
| 107 | * |
| 108 | * \a saveFunction the handler to install |
| 109 | */ |
| 110 | KCRASH_EXPORT void setEmergencySaveFunction(HandlerType saveFunction = nullptr); |
| 111 | |
| 112 | /*! |
| 113 | * Returns the currently set emergency save function. |
| 114 | */ |
| 115 | KCRASH_EXPORT HandlerType emergencySaveFunction(); |
| 116 | |
| 117 | /*! |
| 118 | * Options to determine how the default crash handler should behave. |
| 119 | * |
| 120 | * \value KeepFDs Don't close all file descriptors immediately |
| 121 | * \value SaferDialog Start DrKonqi without arbitrary disk access |
| 122 | * \value AlwaysDirectly Never try to to start DrKonqi via kdeinit. Use fork() and exec() instead. This enum has been deprecated. This is now the default, and |
| 123 | * does not need to be set. |
| 124 | * \value [since 4.1] AutoRestart autorestart this application. Only sensible for KUniqueApplications |
| 125 | * |
| 126 | */ |
| 127 | enum CrashFlag { |
| 128 | KeepFDs = 1, |
| 129 | SaferDialog = 2, |
| 130 | AlwaysDirectly = 4, |
| 131 | AutoRestart = 8, |
| 132 | }; |
| 133 | Q_DECLARE_FLAGS(CrashFlags, CrashFlag) |
| 134 | Q_DECLARE_OPERATORS_FOR_FLAGS(CrashFlags) |
| 135 | |
| 136 | /*! |
| 137 | * Set options to determine how the default crash handler should behave. |
| 138 | * |
| 139 | * \a flags ORed together CrashFlags |
| 140 | */ |
| 141 | KCRASH_EXPORT void setFlags(KCrash::CrashFlags flags); |
| 142 | |
| 143 | /*! |
| 144 | * Enables or disables launching DrKonqi from the crash handler. |
| 145 | * By default, launching DrKonqi is enabled when QCoreApplication is created. |
| 146 | * To disable it: |
| 147 | * \code |
| 148 | * void disableDrKonqi() |
| 149 | * { |
| 150 | * KCrash::setDrKonqiEnabled(false); |
| 151 | * } |
| 152 | * Q_CONSTRUCTOR_FUNCTION(disableDrKonqi) |
| 153 | * \endcode |
| 154 | * \note It is the crash handler's responsibility to launch DrKonqi. |
| 155 | * Therefore, if no crash handler is set, this method also installs |
| 156 | * the default crash handler to ensure that DrKonqi will be launched. |
| 157 | * \since 4.5 |
| 158 | */ |
| 159 | KCRASH_EXPORT void setDrKonqiEnabled(bool enabled); |
| 160 | |
| 161 | /*! |
| 162 | * Returns true if DrKonqi is set to be launched from the crash handler or false otherwise. |
| 163 | * \since 4.5 |
| 164 | */ |
| 165 | KCRASH_EXPORT bool isDrKonqiEnabled(); |
| 166 | |
| 167 | /*! |
| 168 | * Allows providing information to be included in the bug report. Prefer setErrorExtraInformation as it is more flexible. |
| 169 | * |
| 170 | * \since 5.69 |
| 171 | */ |
| 172 | KCRASH_EXPORT void setErrorMessage(const QString &message); |
| 173 | |
| 174 | /*! |
| 175 | * Sets the error tags to be included in the crash report. These are rendered as tags in the crash reporting system. |
| 176 | * |
| 177 | * Note that server-side limits apply to the length of these so you should only put short, sortable data in here. |
| 178 | * \since 6.11 |
| 179 | */ |
| 180 | KCRASH_EXPORT void setErrorTags(const QHash<QString, QString> &details); |
| 181 | |
| 182 | /*! |
| 183 | * Sets the error details to be included in the crash report. These are rendered as extra blobs of data and can any form. |
| 184 | * |
| 185 | * Note that these are subject to event ingestion limits and should be kept at reasonable sizes to prevent event rejection. |
| 186 | * \since 6.11 |
| 187 | */ |
| 188 | KCRASH_EXPORT void (const QHash<QString, QString> &details); |
| 189 | |
| 190 | /*! |
| 191 | * Sets better GPU data. |
| 192 | * |
| 193 | * By default KCrash will try to determine the GPU name, this may however not be accurate data. In particular on |
| 194 | * multi-gpu systems it may not be possible to determine whether the integrated or dedicated GPU is in use. |
| 195 | * |
| 196 | * You should call this function once you know which GPU will be in use for application. This is a free form string. |
| 197 | * |
| 198 | * Server-side limits may apply; keep it as short as possible. |
| 199 | * |
| 200 | * At least 'name' should be set. Additional supported fields follow Sentry unless documented otherwise. |
| 201 | * |
| 202 | * Supported fields are listed at |
| 203 | * https://develop.sentry.dev/sdk/data-model/event-payloads/contexts/#gpu-context |
| 204 | * \since 6.11 |
| 205 | */ |
| 206 | KCRASH_EXPORT void setGPUData(const QVariantHash &data); |
| 207 | } |
| 208 | |
| 209 | #endif |
| 210 | |