File indexing completed on 2024-04-28 11:35:23

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2013 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QApplication>
0009 #include <QDebug>
0010 #include <QFile>
0011 #include <kcrash.h>
0012 #ifdef Q_OS_UNIX
0013 #include <cerrno>
0014 #include <sys/resource.h> // setrlimit
0015 #endif
0016 
0017 QFile output;
0018 
0019 void saveFunction(int)
0020 {
0021     output.write("saveFunction called\n");
0022     output.flush();
0023 }
0024 
0025 int main(int argc, char **argv)
0026 {
0027     QApplication app(argc, argv);
0028 
0029     const QStringList args = app.arguments();
0030     QByteArray flag = args.count() > 1 ? args.at(1).toLatin1() : QByteArray();
0031 
0032     if (flag == "AR") { // auto restart
0033         KCrash::setFlags(KCrash::AutoRestart);
0034     } else if (flag == "ARD") { // auto restart, always directly
0035         KCrash::setFlags(KCrash::AutoRestart | KCrash::AlwaysDirectly);
0036     } else if (flag == "ES") { // emergency save
0037         KCrash::setEmergencySaveFunction(saveFunction);
0038     }
0039 
0040 #ifdef Q_OS_UNIX
0041     // No core file
0042     struct rlimit rlp;
0043     rlp.rlim_cur = 0;
0044     rlp.rlim_max = 0;
0045     if (setrlimit(RLIMIT_CORE, &rlp) != 0) {
0046         qDebug() << strerror(errno);
0047     }
0048 #endif
0049 
0050     output.setFileName(QStringLiteral("kcrashtest_log"));
0051     if (!output.open(QIODevice::WriteOnly | QIODevice::Append)) {
0052         return 1;
0053     }
0054     if (qEnvironmentVariableIsEmpty("KCRASH_AUTO_RESTARTED")) {
0055         output.write("starting ");
0056         output.write(flag);
0057         output.write("\n");
0058         output.flush();
0059         // CRASH!
0060         delete (char *)0xdead;
0061     } else {
0062         output.write("autorestarted ");
0063         output.write(flag);
0064         output.write("\n");
0065         output.close();
0066     }
0067 
0068     return 0;
0069 }