File indexing completed on 2024-04-28 07:42:25

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     KCrash::initialize();
0030 
0031     const QStringList args = app.arguments();
0032     QByteArray flag = args.count() > 1 ? args.at(1).toLatin1() : QByteArray();
0033 
0034     if (flag == "AR") { // auto restart
0035         KCrash::setFlags(KCrash::AutoRestart);
0036     } else if (flag == "ARD") { // auto restart, always directly
0037         KCrash::setFlags(KCrash::AutoRestart | KCrash::AlwaysDirectly);
0038     } else if (flag == "ES") { // emergency save
0039         KCrash::setEmergencySaveFunction(saveFunction);
0040     }
0041 
0042 #ifdef Q_OS_UNIX
0043     // No core file
0044     struct rlimit rlp;
0045     rlp.rlim_cur = 0;
0046     rlp.rlim_max = 0;
0047     if (setrlimit(RLIMIT_CORE, &rlp) != 0) {
0048         qDebug() << strerror(errno);
0049     }
0050 #endif
0051 
0052     output.setFileName(QStringLiteral("kcrashtest_log"));
0053     if (!output.open(QIODevice::WriteOnly | QIODevice::Append)) {
0054         return 1;
0055     }
0056     if (qEnvironmentVariableIsEmpty("KCRASH_AUTO_RESTARTED")) {
0057         output.write("starting ");
0058         output.write(flag);
0059         output.write("\n");
0060         output.flush();
0061         // CRASH!
0062         delete (char *)0xdead;
0063     } else {
0064         output.write("autorestarted ");
0065         output.write(flag);
0066         output.write("\n");
0067         output.close();
0068     }
0069 
0070     return 0;
0071 }