aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/debugger/cdb/cdbexceptionutils.cpp
blob: 6404733308d30eedc90c16e92670cbb4d554025d (plain)
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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "cdbexceptionutils.h"
#include "cdbdebugengine_p.h"
#include "cdbdumperhelper.h"
#include "cdbstacktracecontext.h"

#include <QtCore/QString>
#include <QtCore/QTextStream>
#include <QtCore/QDebug>

enum { debugExc = 0 };

// Special exception codes.
enum { cppExceptionCode = 0xe06d7363, startupCompleteTrap = 0x406d1388,
       rpcServerUnavailableExceptionCode = 0x6ba,
       dllNotFoundExceptionCode = 0xc0000135 };

namespace Debugger {
namespace Internal {

static inline void formatDebugFilterExecFlags(ULONG f, const char *title, QTextStream &str)
{
    str.setIntegerBase(16);
    str << ' ' << title << "=0x" << f << " (";
    str.setIntegerBase(10);
    switch (f) {
    case DEBUG_FILTER_BREAK:
        str << "DEBUG_FILTER_BREAK";
        break;
    case DEBUG_FILTER_SECOND_CHANCE_BREAK:
        str << "DEBUG_FILTER_SECOND_CHANCE_BREAK";
        break;
    case DEBUG_FILTER_OUTPUT:
        str << "DEBUG_FILTER_OUTPUT";
        break;
    case DEBUG_FILTER_IGNORE:
        str << "DEBUG_FILTER_IGNORE";
        break;
    }
    str << ')';
}

static inline void formatDebugFilterContFlags(ULONG f, const char *title, QTextStream &str)
{
    str.setIntegerBase(16);
    str << ' ' << title << "=0x" << f << " (";
    str.setIntegerBase(10);
    switch (f) {
    case DEBUG_FILTER_GO_HANDLED:
        str << "DEBUG_FILTER_GO_HANDLED";
        break;
    case DEBUG_FILTER_GO_NOT_HANDLED:
        str << "DEBUG_FILTER_GO_NOT_HANDLED";
    }
    str << ')';
}

ExceptionBlocker::ExceptionBlocker(CIDebugControl *ctrl, ULONG code, Mode m) :
    m_ctrl(ctrl),
    m_code(code),
    m_state(StateError)
{
    // Retrieve current state
    memset(&m_oldParameters, 0, sizeof(DEBUG_EXCEPTION_FILTER_PARAMETERS));
    if (getExceptionParameters(ctrl, code, &m_oldParameters, &m_errorString)) {        
        // Are we in a nested instantiation?
        const ULONG desiredExOption = m == IgnoreException ? DEBUG_FILTER_IGNORE : DEBUG_FILTER_OUTPUT;
        const bool isAlreadyBlocked = m_oldParameters.ExecutionOption == desiredExOption
                                      && m_oldParameters.ContinueOption == DEBUG_FILTER_GO_NOT_HANDLED;
        if (isAlreadyBlocked) {
           m_state = StateNested;
        } else {
            // Nope, block it now.
            DEBUG_EXCEPTION_FILTER_PARAMETERS blockedState = m_oldParameters;
            blockedState.ExecutionOption = desiredExOption;
            blockedState.CommandSize = DEBUG_FILTER_GO_NOT_HANDLED;
            const bool ok = setExceptionParameters(m_ctrl, blockedState, &m_errorString);
            m_state = ok ? StateOk : StateError;
        } // not blocked
    } else {
        m_state = StateError;
    }
    if (debugExc)
        qDebug() << "ExceptionBlocker: state=" << m_state << format(m_oldParameters) << m_errorString;
}

ExceptionBlocker::~ExceptionBlocker()
{
    if (m_state == StateOk) {
        // Restore
        if (debugExc)
            qDebug() << "~ExceptionBlocker: unblocking " << m_oldParameters.ExceptionCode;
        if (!setExceptionParameters(m_ctrl, m_oldParameters, &m_errorString))
            qWarning("Unable to restore exception state for %lu: %s\n", m_oldParameters.ExceptionCode, qPrintable(m_errorString));
    }
}

bool ExceptionBlocker::getExceptionParameters(CIDebugControl *ctrl, ULONG exCode, DEBUG_EXCEPTION_FILTER_PARAMETERS *result, QString *errorMessage)
{
    const HRESULT ihr = ctrl->GetExceptionFilterParameters(1, &exCode, 0, result);
    if (FAILED(ihr)) {
        *errorMessage = msgComFailed("GetExceptionFilterParameters", ihr);
        return false;
    }
    return true;
}

bool ExceptionBlocker::setExceptionParameters(CIDebugControl *ctrl, const DEBUG_EXCEPTION_FILTER_PARAMETERS &p, QString *errorMessage)
{
    const HRESULT ihr = ctrl->SetExceptionFilterParameters(1, const_cast<DEBUG_EXCEPTION_FILTER_PARAMETERS*>(&p));
    if (FAILED(ihr)) {
        *errorMessage = msgComFailed("GetExceptionFilterParameters", ihr);
        return false;
    }
    return true;
}

QString ExceptionBlocker::format(const DEBUG_EXCEPTION_FILTER_PARAMETERS &p)
{
    QString rc;
    QTextStream str(&rc);
    str << "Code=" << p.ExceptionCode;
    formatDebugFilterExecFlags(p.ExecutionOption, "ExecutionOption", str);
    formatDebugFilterContFlags(p.ContinueOption, "ContinueOption", str);
    str << " TextSize=" << p.TextSize << " CommandSizes=" << p.CommandSize << ','
            << p.SecondCommandSize;
    return rc;
}

// ------------------ further exception utilities
// Simple exception formatting
void formatException(const EXCEPTION_RECORD64 *e, QTextStream &str)
{
    str.setIntegerBase(16);
    str << "\nException at 0x"  << e->ExceptionAddress
            <<  ", code: 0x" << e->ExceptionCode << ": ";
    switch (e->ExceptionCode) {
    case cppExceptionCode:
        str << "C++ exception";
        break;
    case startupCompleteTrap:
        str << "Startup complete";
        break;
    case dllNotFoundExceptionCode:
        str << "DLL not found";
        break;
    case EXCEPTION_ACCESS_VIOLATION: {
            const bool writeOperation = e->ExceptionInformation[0];
            str << (writeOperation ? "write" : "read")
                << " access violation at: 0x" << e->ExceptionInformation[1];
    }
        break;
    case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
        str << "arrary bounds exceeded";
        break;
    case EXCEPTION_BREAKPOINT:
        str << "breakpoint";
        break;
    case EXCEPTION_DATATYPE_MISALIGNMENT:
        str << "datatype misalignment";
        break;
    case EXCEPTION_FLT_DENORMAL_OPERAND:
        str << "floating point exception";
        break;
    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
        str << "division by zero";
        break;
    case EXCEPTION_FLT_INEXACT_RESULT:
        str << " floating-point operation cannot be represented exactly as a decimal fraction";
        break;
    case EXCEPTION_FLT_INVALID_OPERATION:
        str << "invalid floating-point operation";
        break;
    case EXCEPTION_FLT_OVERFLOW:
        str << "floating-point overflow";
        break;
    case EXCEPTION_FLT_STACK_CHECK:
        str << "floating-point operation stack over/underflow";
        break;
    case  EXCEPTION_FLT_UNDERFLOW:
        str << "floating-point UNDERFLOW";
        break;
    case  EXCEPTION_ILLEGAL_INSTRUCTION:
        str << "invalid instruction";
        break;
    case EXCEPTION_IN_PAGE_ERROR:
        str << "page in error";
        break;
    case EXCEPTION_INT_DIVIDE_BY_ZERO:
        str << "integer division by zero";
        break;
    case EXCEPTION_INT_OVERFLOW:
        str << "integer overflow";
        break;
    case EXCEPTION_INVALID_DISPOSITION:
        str << "invalid disposition to exception dispatcher";
        break;
    case EXCEPTION_NONCONTINUABLE_EXCEPTION:
        str << "attempt to continue execution after noncontinuable exception";
        break;
    case EXCEPTION_PRIV_INSTRUCTION:
        str << "privileged instruction";
        break;
    case EXCEPTION_SINGLE_STEP:
        str << "single step";
        break;
    case EXCEPTION_STACK_OVERFLOW:
        str << "stack_overflow";
        break;
    }
    str << ", flags=0x" << e->ExceptionFlags;
    if (e->ExceptionFlags == EXCEPTION_NONCONTINUABLE) {
        str << " (execution cannot be continued)";
    }
    str << "\n\n";
    str.setIntegerBase(10);
}

// Format exception with stacktrace in case of C++ exception
void formatException(const EXCEPTION_RECORD64 *e,
                     const QSharedPointer<CdbDumperHelper> &dumper,
                     QTextStream &str)
{
    formatException(e, str);
    if (e->ExceptionCode == cppExceptionCode) {
        QString errorMessage;
        ULONG currentThreadId = 0;
        dumper->comInterfaces()->debugSystemObjects->GetCurrentThreadId(&currentThreadId);
        if (CdbStackTraceContext *stc = CdbStackTraceContext::create(dumper, currentThreadId, &errorMessage)) {
            str << "at:\n";
            stc->format(str);
            str <<'\n';
            delete stc;
        }
    }
}

bool isFatalException(LONG code)
{
    switch (code) {
    case EXCEPTION_BREAKPOINT:
    case EXCEPTION_SINGLE_STEP:
    case startupCompleteTrap: // Mysterious exception at start of application
    case rpcServerUnavailableExceptionCode:
    case dllNotFoundExceptionCode:
    case cppExceptionCode:
        return false;
    default:
        break;
    }
    return true;
}

} // namespace Internal
} // namespace Debugger