backtraceparsergdb.cpp 12.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
    Copyright (C) 2009-2010  George Kiagiadakis <gkiagia@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "backtraceparsergdb.h"
#include "backtraceparser_p.h"
Laurent Montel's avatar
Laurent Montel committed
20
#include "drkonqi_parser_debug.h"
21

22
#include <QFileInfo>
23
#include <QRegularExpression>
24
25
26

//BEGIN BacktraceLineGdb

27
28
const QLatin1String BacktraceParserGdb::KCRASH_INFO_MESSAGE("KCRASH_INFO_MESSAGE: ");

29
BacktraceLineGdb::BacktraceLineGdb(const QString & lineStr)
30
    : BacktraceLine()
31
32
33
34
35
36
37
38
39
40
41
42
{
    d->m_line = lineStr;
    d->m_functionName = QLatin1String("??");
    parse();
    if (d->m_type == StackFrame) {
        rate();
    }
}

void BacktraceLineGdb::parse()
{

43
    if (d->m_line == QLatin1Char('\n')) {
44
45
        d->m_type = EmptyLine;
        return;
46
    } else if (d->m_line == QLatin1String("[KCrash Handler]\n")) {
47
48
        d->m_type = KCrash;
        return;
49
    } else if (d->m_line.contains(QLatin1String("<signal handler called>"))) {
50
51
52
53
        d->m_type = SignalHandlerStart;
        return;
    }

54
55
56
57
58
    static QRegularExpression regExp;
    // make dot "." char match new lines, to match e.g.:
    // "#5  0x00007f50e99f776f in QWidget::testAttribute_helper (this=0x6e6440,\n    attribute=Qt::WA_WState_Created) at kernel/qwidget.cpp:9081\n"
    // gdb breaks long stack frame lines into multiple ones for readability
    regExp.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
59
60
    regExp.setPattern(QRegularExpression::anchoredPattern(QStringLiteral(
                      "#([0-9]+)" //matches the stack frame number, ex. "#0"
61
62
                      "[\\s]+(?:0x[0-9a-f]+[\\s]+in[\\s]+)?" // matches " 0x0000dead in " (optionally)
                      "((?:\\(anonymous namespace\\)::)?[^\\(]+)?" //matches the function name
63
64
                      //(anything except left parenthesis, which is the start of the arguments section)
                      //and optionally the prefix "(anonymous namespace)::"
65
                      "(?:\\(.*\\))?" //matches the function arguments
66
                                    //(when the app doesn't have debugging symbols)
67
                      "[\\s]+(?:const[\\s]+)?" //matches a traling const, if it exists
68
69
70
71
72
                      "\\(.*\\)" //matches the arguments of the function with their values
                                 //(when the app has debugging symbols)
                      "([\\s]+" //beginning of optional file information
                      "(from|at)[\\s]+" //matches "from " or "at "
                      "(.+)" //matches the filename (source file or shared library file)
73
                      ")?\n"))); //matches trailing newline.
74
75
76
                    //the )? at the end closes the parenthesis before [\\s]+(from|at) and
                    //notes that the whole expression from there is optional.

77
78
    QRegularExpressionMatch match = regExp.match(d->m_line);
    if (match.hasMatch()) {
79
        d->m_type = StackFrame;
80
81
        d->m_stackFrameNumber = match.captured(1).toInt();
        d->m_functionName = match.captured(2).trimmed();
82

83
84
        if (!match.captured(3).isEmpty()) { //we have file information (stuff after from|at)
            bool file = match.captured(4) == QLatin1String("at"); //'at' means we have a source file (likely)
85
86
87
            // Gdb isn't entirely consistent here, when it uses 'from' it always refers to a library, but
            // sometimes the stack can resolve to a library even when it uses the 'at' key word.
            // This specifically seems to happen when a frame has no function name.
88
            const QString path = match.captured(5);
89
90
91
92
93
            const auto completeSuffix = QFileInfo(path).completeSuffix();
            file = file
                    && completeSuffix != QLatin1String("so") /* libf.so (so) */
                    && !completeSuffix.startsWith(QLatin1String("so.")) /* libf.so.1 (so.1) */
                    && !completeSuffix.contains(QLatin1String(".so") /* libf-1.0.so.1 (0.so.1)*/);
94
            if (file) {
95
                d->m_file = match.captured(5);
96
            } else { //'from' means we have a library
97
                d->m_library = match.captured(5);
98
99
100
            }
        }

Laurent Montel's avatar
Laurent Montel committed
101
        qCDebug(DRKONQI_PARSER_LOG) << d->m_stackFrameNumber << d->m_functionName << d->m_file << d->m_library;
102
103
104
        return;
    }

105
106
107
108
109
110
    if (d->m_line.contains(BacktraceParserGdb::KCRASH_INFO_MESSAGE)) {
        qCDebug(DRKONQI_PARSER_LOG) << "info:" << d->m_line;
        d->m_type = Info;
        return;
    }

111
112
113
114
115
116
117
118
    regExp.setPatternOptions(regExp.patternOptions() & ~QRegularExpression::DotMatchesEverythingOption);

    regExp.setPattern(QRegularExpression::anchoredPattern(
                        QStringLiteral(".*\\(no debugging symbols found\\).*|"
                                       ".*\\[Thread debugging using libthread_db enabled\\].*|"
                                       ".*\\[New .*|"
                                       "0x[0-9a-f]+.*|"
                                       "Current language:.*")));
119
    if (regExp.match(d->m_line).hasMatch()) {
Laurent Montel's avatar
Laurent Montel committed
120
        qCDebug(DRKONQI_PARSER_LOG) << "garbage detected:" << d->m_line;
121
122
123
124
        d->m_type = Crap;
        return;
    }

125
126
    regExp.setPattern(QRegularExpression::anchoredPattern(
                        QStringLiteral("Thread [0-9]+\\s+\\(Thread [0-9a-fx]+\\s+\\(.*\\)\\):\n")));
127
    if (regExp.match(d->m_line).hasMatch()) {
Laurent Montel's avatar
Laurent Montel committed
128
        qCDebug(DRKONQI_PARSER_LOG) << "thread start detected:" << d->m_line;
129
130
131
132
        d->m_type = ThreadStart;
        return;
    }

133
134
    regExp.setPattern(QRegularExpression::anchoredPattern(
                        QStringLiteral("\\[Current thread is [0-9]+ \\(.*\\)\\]\n")));
135
    if (regExp.match(d->m_line).hasMatch()) {
Laurent Montel's avatar
Laurent Montel committed
136
        qCDebug(DRKONQI_PARSER_LOG) << "thread indicator detected:" << d->m_line;
137
138
139
140
        d->m_type = ThreadIndicator;
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
141
    qCDebug(DRKONQI_PARSER_LOG) << "line" << d->m_line << "did not match";
142
143
144
145
146
147
148
149
150
151
}

void BacktraceLineGdb::rate()
{
    LineRating r;

    //for explanations, see the LineRating enum definition
    if (!fileName().isEmpty()) {
        r = Good;
    } else if (!libraryName().isEmpty()) {
152
        if (functionName() == QLatin1String("??") || functionName().isEmpty()) {
153
154
155
156
157
            r = MissingFunction;
        } else {
            r = MissingSourceFile;
        }
    } else {
158
        if (functionName() == QLatin1String("??") || functionName().isEmpty()) {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
            r = MissingEverything;
        } else {
            r = MissingLibrary;
        }
    }

    d->m_rating = r;
}

//END BacktraceLineGdb

//BEGIN BacktraceParserGdb

class BacktraceParserGdbPrivate : public BacktraceParserPrivate
{
public:
    BacktraceParserGdbPrivate()
        : BacktraceParserPrivate(),
          m_possibleKCrashStart(0), m_threadsCount(0),
          m_isBelowSignalHandler(false), m_frameZeroAppeared(false) {}

    QString m_lineInputBuffer;
    int m_possibleKCrashStart;
    int m_threadsCount;
    bool m_isBelowSignalHandler;
    bool m_frameZeroAppeared;
};

BacktraceParserGdb::BacktraceParserGdb(QObject *parent)
        : BacktraceParser(parent)
{
}

BacktraceParserPrivate* BacktraceParserGdb::constructPrivate() const
{
    return new BacktraceParserGdbPrivate;
}

void BacktraceParserGdb::newLine(const QString & lineStr)
{
    Q_D(BacktraceParserGdb);

    //when the line is too long, gdb splits it into two lines.
    //This breaks parsing and results in two Unknown lines instead of a StackFrame one.
    //Here we workaround this by joining the two lines when such a scenario is detected.
    if (d->m_lineInputBuffer.isEmpty()) {
        d->m_lineInputBuffer = lineStr;
    } else if (lineStr.startsWith(QLatin1Char(' ')) || lineStr.startsWith(QLatin1Char('\t'))) {
        //gdb always adds some whitespace at the beginning of the second line
        d->m_lineInputBuffer.append(lineStr);
    } else {
        parseLine(d->m_lineInputBuffer);
        d->m_lineInputBuffer = lineStr;
    }
}

void BacktraceParserGdb::parseLine(const QString & lineStr)
{
    Q_D(BacktraceParserGdb);

    BacktraceLineGdb line(lineStr);
    switch (line.type()) {
    case BacktraceLine::Crap:
        break; //we don't want crap in the backtrace ;)
223
224
225
    case BacktraceLine::Info:
        d->m_infoLines << line.toString().mid(KCRASH_INFO_MESSAGE.size());
        break;
226
227
228
229
230
231
232
233
234
235
236
237
    case BacktraceLine::ThreadStart:
        d->m_linesList.append(line);
        d->m_possibleKCrashStart = d->m_linesList.size();
        d->m_threadsCount++;
        //reset the state of the flags that need to be per-thread
        d->m_isBelowSignalHandler = false;
        d->m_frameZeroAppeared = false; // gdb bug workaround flag, see below
        break;
    case BacktraceLine::SignalHandlerStart:
        if (!d->m_isBelowSignalHandler) {
            //replace the stack frames of KCrash with a nice message
            d->m_linesList.erase(d->m_linesList.begin() + d->m_possibleKCrashStart, d->m_linesList.end());
238
            d->m_linesList.insert(d->m_possibleKCrashStart, BacktraceLineGdb(QStringLiteral("[KCrash Handler]\n")));
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
            d->m_isBelowSignalHandler = true; //next line is the first below the signal handler
        } else {
            //this is not the first time we see a crash handler frame on the same thread,
            //so we just add it to the list
            d->m_linesList.append(line);
        }
        break;
    case BacktraceLine::StackFrame:
        // gdb workaround - (v6.8 at least) - 'thread apply all bt' writes
        // the #0 stack frame again at the end.
        // Here we ignore this frame by using a flag that tells us whether
        // this is the first or the second time that the #0 frame appears in this thread.
        // The flag is cleared on each thread start.
        if (line.frameNumber() == 0) {
            if (d->m_frameZeroAppeared) {
                break; //break from the switch so that the frame is not added to the list.
            } else {
                d->m_frameZeroAppeared = true;
            }
        }

        //rate the stack frame if we are below the signal handler
        if (d->m_isBelowSignalHandler) {
            d->m_linesToRate.append(line);
        }
Laurent Montel's avatar
Laurent Montel committed
264
        Q_FALLTHROUGH();
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
        //fall through and append the line to the list
    default:
        d->m_linesList.append(line);
        break;
    }
}

QString BacktraceParserGdb::parsedBacktrace() const
{
    Q_D(const BacktraceParserGdb);

    QString result;
    if (d) {
        QList<BacktraceLine>::const_iterator i;
        for (i = d->m_linesList.constBegin(); i != d->m_linesList.constEnd(); ++i) {
            //if there is only one thread, we can omit the thread indicator,
            //the thread header and all the empty lines.
            if (d->m_threadsCount == 1 && ((*i).type() == BacktraceLine::ThreadIndicator
                                        || (*i).type() == BacktraceLine::ThreadStart
                                        || (*i).type() == BacktraceLine::EmptyLine))
            {
                continue;
            }
            result += i->toString();
        }
    }
    return result;
}

QList<BacktraceLine> BacktraceParserGdb::parsedBacktraceLines() const
{
    Q_D(const BacktraceParserGdb);

    QList<BacktraceLine> result;
    if (d) {
        QList<BacktraceLine>::const_iterator i;
        for (i = d->m_linesList.constBegin(); i != d->m_linesList.constEnd(); ++i) {
            //if there is only one thread, we can omit the thread indicator,
            //the thread header and all the empty lines.
            if (d->m_threadsCount == 1 && ((*i).type() == BacktraceLine::ThreadIndicator
                                        || (*i).type() == BacktraceLine::ThreadStart
                                        || (*i).type() == BacktraceLine::EmptyLine))
            {
                continue;
            }
            result.append(*i);
        }
    }
    return result;
}

//END BacktraceParserGdb

318