~ubuntu-branches/ubuntu/saucy/kde-runtime/saucy-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************
* parsebugbacktraces.cpp
* Copyright 2011 Matthias Fuchs <mat69@gmx.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, see <http://www.gnu.org/licenses/>.
*
******************************************************************/

#include "parsebugbacktraces.h"

#include "parser/backtraceparser.h"

typedef QList<BacktraceLine>::const_iterator BacktraceConstIterator;

BacktraceConstIterator findCrashStackFrame(BacktraceConstIterator it, BacktraceConstIterator itEnd)
{
    BacktraceConstIterator result = itEnd;

    //find the beginning of the crash
    for ( ; it != itEnd; ++it) {
        if (it->type() == BacktraceLine::KCrash) {
            result = it;
            break;
        }
    }

    //find the beginning of the stack frame
    for (it = result; it != itEnd; ++it) {
        if (it->type() == BacktraceLine::StackFrame) {
            result = it;
            break;
        }
    }

    return result;
}

//TODO improve this stuff, it is just a HACK
ParseBugBacktraces::DuplicateRating rating(BacktraceConstIterator it, BacktraceConstIterator itEnd, BacktraceConstIterator it2, BacktraceConstIterator itEnd2)
{
    int matches = 0;
    int lines = 0;

    it = findCrashStackFrame(it, itEnd);
    it2 = findCrashStackFrame(it2, itEnd2);

    while (it != itEnd && it2 != itEnd2) {
        if (it->type() == BacktraceLine::StackFrame && it2->type() == BacktraceLine::StackFrame) {
            ++lines;
            if (it->frameNumber() == it2->frameNumber() && it->functionName() == it2->functionName()) {
                ++matches;
            }
            ++it;
            ++it2;
            continue;
        }

        //if iters do not point to emptylines or a stackframe increase them
        if (it->type() != BacktraceLine::StackFrame && it->type() != BacktraceLine::EmptyLine) {
            ++it;
            continue;
        }
        if (it2->type() != BacktraceLine::StackFrame && it2->type() != BacktraceLine::EmptyLine) {
            ++it2;
            continue;
        }

        //one bt is shorter than the other
        if (it->type() == BacktraceLine::StackFrame && it2->type() == BacktraceLine::EmptyLine) {
            ++lines;
            ++it;
            continue;
        }
        if (it2->type() == BacktraceLine::StackFrame && it->type() == BacktraceLine::EmptyLine) {
            ++lines;
            ++it2;
            continue;
        }

        if (it->type() == BacktraceLine::EmptyLine && it2->type() == BacktraceLine::EmptyLine) {
            //done
            break;
        }
    }

    if (!lines) {
        return ParseBugBacktraces::NoDuplicate;
    }

    const int rating = matches * 100 / lines;
    if (rating == 100) {
        return ParseBugBacktraces::PerfectDuplicate;
    } else if (rating >= 90) {
        return ParseBugBacktraces::MostLikelyDuplicate;
    } else if (rating >= 60) {
        return ParseBugBacktraces::MaybeDuplicate;
    } else {
        return ParseBugBacktraces::NoDuplicate;
    }

    return ParseBugBacktraces::NoDuplicate;
}

ParseBugBacktraces::ParseBugBacktraces(const BugReport &bug, QObject *parent)
  : QObject(parent),
    m_bug(bug)
{
    m_parser = BacktraceParser::newParser("gdb", this);
    m_parser->connectToGenerator(this);
}

void ParseBugBacktraces::parse()
{
    parse(m_bug.description());

    QStringList comments = m_bug.comments();
    foreach (const QString &comment, comments) {
        parse(comment);
    }
}

void ParseBugBacktraces::parse(const QString &comment)
{
    emit starting();

    int start = 0;
    int end = -1;
    do {
        start = end + 1;
        end = comment.indexOf('\n', start);
        emit newLine(comment.mid(start, (end != -1 ? end - start + 1 : end)));
    } while (end != -1);

    //accepts anything as backtrace, the start of the backtrace is searched later anyway
    m_backtraces << m_parser->parsedBacktraceLines();
}

ParseBugBacktraces::DuplicateRating ParseBugBacktraces::findDuplicate(const QList<BacktraceLine> &backtrace)
{
    if (m_backtraces.isEmpty() || backtrace.isEmpty()) {
        return NoDuplicate;
    }

    DuplicateRating bestRating = NoDuplicate;
    DuplicateRating currentRating = NoDuplicate;

    QList<QList<BacktraceLine> >::const_iterator itBts;
    QList<QList<BacktraceLine> >::const_iterator itEndBts = m_backtraces.constEnd();
    for (itBts = m_backtraces.constBegin(); itBts != itEndBts; ++itBts) {
        currentRating = rating(backtrace.constBegin(), backtrace.constEnd(), itBts->constBegin(), itBts->constEnd());
        if (currentRating < bestRating) {
            bestRating = currentRating;
        }

        if (bestRating == PerfectDuplicate) {
            return bestRating;
        }
    }

    return bestRating;
}

#include "parsebugbacktraces.moc"