File indexing completed on 2024-04-21 05:26:40

0001 /*****************************************************************
0002  * drkonqi - The KDE Crash Handler
0003  *
0004  * SPDX-FileCopyrightText: 2000-2003 Hans Petter Bieker <bieker@kde.org>
0005  * SPDX-FileCopyrightText: 2021-2022 Harald Sitter <sitter@kde.org>
0006  *
0007  * SPDX-License-Identifier: BSD-2-Clause
0008  *****************************************************************/
0009 
0010 #ifndef BACKTRACEGENERATOR_H
0011 #define BACKTRACEGENERATOR_H
0012 
0013 #include <memory>
0014 
0015 #include <QProcess>
0016 #include <QTemporaryFile>
0017 
0018 #include "debugger.h"
0019 
0020 class KProcess;
0021 class BacktraceParser;
0022 class QTemporaryDir;
0023 
0024 class BacktraceGenerator : public QObject
0025 {
0026     Q_OBJECT
0027 
0028     Q_PROPERTY(State state READ state NOTIFY stateChanged)
0029     Q_PROPERTY(bool hasAnyFailure READ hasAnyFailure NOTIFY stateChanged) // derives from state
0030     Q_PROPERTY(bool supportsSymbolResolution MEMBER m_supportsSymbolResolution CONSTANT)
0031     Q_PROPERTY(bool symbolResolution MEMBER m_symbolResolution NOTIFY symbolResolutionChanged)
0032 public:
0033     enum State {
0034         NotLoaded,
0035         Loading,
0036         Loaded,
0037         Failed,
0038         FailedToStart,
0039     };
0040     Q_ENUM(State)
0041 
0042     BacktraceGenerator(const Debugger &debugger, QObject *parent);
0043     ~BacktraceGenerator() override;
0044 
0045     State state() const
0046     {
0047         return m_state;
0048     }
0049 
0050     Q_INVOKABLE BacktraceParser *parser() const
0051     {
0052         return m_parser;
0053     }
0054 
0055     Q_INVOKABLE QString backtrace() const
0056     {
0057         return m_parsedBacktrace;
0058     }
0059 
0060     // Called by manager when it is ready for us.
0061     void setBackendPrepared();
0062     // ... or not
0063     void setBackendFailed();
0064 
0065     Q_INVOKABLE bool debuggerIsGDB() const;
0066     Q_INVOKABLE QString debuggerName() const;
0067     QByteArray sentryPayload() const;
0068 
0069 public Q_SLOTS:
0070     void start();
0071     bool hasAnyFailure();
0072 
0073 Q_SIGNALS:
0074     void starting();
0075     void newLine(const QString &str); // emitted for every line
0076     void someError();
0077     void failedToStart();
0078     void done();
0079     void preparing();
0080     void stateChanged();
0081     void symbolResolutionChanged();
0082 
0083 private Q_SLOTS:
0084     void slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus);
0085     void slotReadInput();
0086     void slotOnErrorOccurred(QProcess::ProcessError error);
0087 
0088 private:
0089     const Debugger m_debugger;
0090     KProcess *m_proc = nullptr;
0091     QTemporaryFile *m_temp = nullptr;
0092     QByteArray m_output;
0093     State m_state = NotLoaded;
0094     BacktraceParser *m_parser = nullptr;
0095     QString m_parsedBacktrace;
0096     std::unique_ptr<QTemporaryDir> m_tempDirectory;
0097     const bool m_supportsSymbolResolution = false;
0098     bool m_symbolResolution = false;
0099     QByteArray m_sentryPayload;
0100 
0101 #ifdef BACKTRACE_PARSER_DEBUG
0102     BacktraceParser *m_debugParser = nullptr;
0103 #endif
0104 };
0105 
0106 #endif