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
16class QString;
17
18/**
19 * This namespace contains functions to handle crashes.
20 * It allows you to set a crash handler function that will be called
21 * when your application crashes and also provides a default crash
22 * handler that implements the following functionality:
23 * @li Launches the KDE crash display application (DrKonqi) to let
24 * the user report the bug and/or debug it.
25 * @li Calls an emergency save function that you can set with
26 * setEmergencySaveFunction() to attempt to save the application's data.
27 * @li Autorestarts your application.
28 *
29 * @note All the above features are optional and you need to enable them
30 * explicitly. By default, the defaultCrashHandler() will not do anything.
31 * However, if you are using KApplication, it will by default enable launching
32 * DrKonqi on crashes, unless the --nocrashhandler argument was passed on
33 * the command line or the environment variable KDE_DEBUG is set to any value.
34 */
35namespace KCrash
36{
37/**
38 * Initialize KCrash.
39 *
40 * This does nothing if $KDE_DEBUG is set.
41 *
42 * Call this in your main() to ensure that the crash handler is always launched.
43 * @since 5.15
44 */
45KCRASH_EXPORT void initialize();
46
47/**
48 * The default crash handler.
49 * Do not call this function directly. Instead, use
50 * setCrashHandler() to set it as your application's crash handler.
51 * @param signal the signal number
52 * @note If you implement your own crash handler, you will have to
53 * call this function from your implementation if you want to use the
54 * features of this namespace.
55 */
56KCRASH_EXPORT void defaultCrashHandler(int signal);
57
58/**
59 * Typedef for a pointer to a crash handler function.
60 * The function's argument is the number of the signal.
61 */
62typedef void (*HandlerType)(int);
63
64/**
65 * Install a function to be called when a crash occurs.
66 * A crash occurs when one of the following signals is
67 * caught: SIGSEGV, SIGBUS, SIGFPE, SIGILL, SIGABRT.
68 * @param handler this can be one of:
69 * @li null, in which case signal catching is disabled
70 * (by setting the signal handler for the crash signals to SIG_DFL)
71 * @li a user defined function in the form:
72 * static (if in a class) void myCrashHandler(int);
73 * @li if handler is omitted, the default crash handler is installed
74 * @note If you use setDrKonqiEnabled(true), setEmergencySaveFunction(myfunc)
75 * or setFlags(AutoRestart), you do not need to call this function
76 * explicitly. The default crash handler is automatically installed by
77 * those functions if needed. However, if you set a custom crash handler,
78 * those functions will not change it.
79 */
80KCRASH_EXPORT void setCrashHandler(HandlerType handler = defaultCrashHandler);
81
82/**
83 * Returns the installed crash handler.
84 * @return the crash handler
85 */
86KCRASH_EXPORT HandlerType crashHandler();
87
88/**
89 * Installs a function which should try to save the application's data.
90 * @note It is the crash handler's responsibility to call this function.
91 * Therefore, if no crash handler is set, the default crash handler
92 * is installed to ensure the save function will be called.
93 * @param saveFunction the handler to install
94 */
95KCRASH_EXPORT void setEmergencySaveFunction(HandlerType saveFunction = nullptr);
96
97/**
98 * Returns the currently set emergency save function.
99 * @return the emergency save function
100 */
101KCRASH_EXPORT HandlerType emergencySaveFunction();
102
103/**
104 * Options to determine how the default crash handler should behave.
105 * @see CrashFlags
106 */
107enum CrashFlag {
108 KeepFDs = 1, ///< don't close all file descriptors immediately
109 SaferDialog = 2, ///< start DrKonqi without arbitrary disk access
110 AlwaysDirectly =
111 4, ///< never try to to start DrKonqi via kdeinit. Use fork() and exec() instead. @deprecated This is now the default, and does not need to be set.
112 AutoRestart = 8, ///< autorestart this application. Only sensible for KUniqueApplications. @since 4.1.
113};
114/**
115 * Stores a combination of #CrashFlag values.
116 */
117Q_DECLARE_FLAGS(CrashFlags, CrashFlag)
118Q_DECLARE_OPERATORS_FOR_FLAGS(CrashFlags)
119
120/**
121 * Set options to determine how the default crash handler should behave.
122 * @param flags ORed together CrashFlags
123 */
124KCRASH_EXPORT void setFlags(KCrash::CrashFlags flags);
125
126/**
127 * Enables or disables launching DrKonqi from the crash handler.
128 * By default, launching DrKonqi is enabled when QCoreApplication is created.
129 * To disable it:
130 * @code
131 void disableDrKonqi()
132 {
133 KCrash::setDrKonqiEnabled(false);
134 }
135 Q_CONSTRUCTOR_FUNCTION(disableDrKonqi)
136 * \endcode
137 * @note It is the crash handler's responsibility to launch DrKonqi.
138 * Therefore, if no crash handler is set, this method also installs
139 * the default crash handler to ensure that DrKonqi will be launched.
140 * @since 4.5
141 */
142KCRASH_EXPORT void setDrKonqiEnabled(bool enabled);
143
144/**
145 * Returns true if DrKonqi is set to be launched from the crash handler or false otherwise.
146 * @since 4.5
147 */
148KCRASH_EXPORT bool isDrKonqiEnabled();
149
150/**
151 * Allows providing information to be included in the bug report.
152 *
153 * @since 5.69
154 */
155KCRASH_EXPORT void setErrorMessage(const QString &message);
156
157}
158
159#endif
160

source code of kcrash/src/kcrash.h