procinterrupt.cpp 6.58 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
#include "procinterrupt.h"
34
#include "debuggerconstants.h"
con's avatar
con committed
35

36
37
38
39
#include <QCoreApplication>
#include <QProcess> // makes kill visible on Windows.
#include <QFile>
#include <QDir>
con's avatar
con committed
40
41
using namespace Debugger::Internal;

42
43
44
45
46
static inline QString msgCannotInterrupt(int pid, const QString &why)
{
    return QString::fromLatin1("Cannot interrupt process %1: %2").arg(pid).arg(why);
}

47
#if defined(Q_OS_WIN)
con's avatar
con committed
48

49
50
#define _WIN32_WINNT 0x0501 /* WinXP, needed for DebugBreakProcess() */

51
#include <utils/winutils.h>
52
#include <windows.h>
con's avatar
con committed
53

54
55
56
57
#if !defined(PROCESS_SUSPEND_RESUME) // Check flag for MinGW
#    define PROCESS_SUSPEND_RESUME (0x0800)
#endif // PROCESS_SUSPEND_RESUME

58
59
60
61
static BOOL isWow64Process(HANDLE hproc)
{
    typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

62
63
64
65
66
67
68
69
70
71
    BOOL ret = false;

    static LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;
    if (!fnIsWow64Process) {
        if (HMODULE hModule = GetModuleHandle(L"kernel32.dll"))
            fnIsWow64Process = reinterpret_cast<LPFN_ISWOW64PROCESS>(GetProcAddress(hModule, "IsWow64Process"));
    }

    if (!fnIsWow64Process) {
        qWarning("Cannot retrieve symbol 'IsWow64Process'.");
72
        return false;
73
    }
74

75
76
77
    if (!fnIsWow64Process(hproc, &ret)) {
        qWarning("IsWow64Process() failed for %p: %s",
                 hproc, qPrintable(Utils::winErrorMessage(GetLastError())));
78
        return false;
79
    }
80
81
82
    return ret;
}

83
84
// Open the process and break into it
bool Debugger::Internal::interruptProcess(int pID, int engineType, QString *errorMessage)
hjk's avatar
hjk committed
85
{
86
    bool ok = false;
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    HANDLE inferior = NULL;
    do {
        const DWORD rights = PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION
                |PROCESS_VM_OPERATION|PROCESS_VM_WRITE|PROCESS_VM_READ
                |PROCESS_DUP_HANDLE|PROCESS_TERMINATE|PROCESS_CREATE_THREAD|PROCESS_SUSPEND_RESUME;
        inferior = OpenProcess(rights, FALSE, pID);
        if (inferior == NULL) {
            *errorMessage = QString::fromLatin1("Cannot open process %1: %2").
                    arg(pID).arg(Utils::winErrorMessage(GetLastError()));
            break;
        }
        // Try DebugBreakProcess if either Qt Creator is compiled 64 bit or
        // both Qt Creator and application are 32 bit.
#ifdef Q_OS_WIN64
101
102
103
104
105
106
107
        // Qt-Creator compiled 64 bit
        // Windows must be 64 bit
        // CDB 64 bit: use DebugBreakProcess for 32 an 64 bit processes.
        // CDB 32 bit: untested
        // GDB: not supported
        const bool useDebugBreakApi= true;

108
109
#else
        // Qt-Creator compiled 32 bit:
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

        bool useDebugBreakApi;
        if (isWow64Process(GetCurrentProcess())) {
            // Windows is 64 bit
            if (engineType == CdbEngineType) {
                // CDB 64 bit: If Qt-Creator is a WOW64 process (meaning a 32bit process
                //    running in emulation), always use win64interrupt.exe for native
                //    64 bit processes and WOW64 processes. While DebugBreakProcess()
                //    works in theory for other WOW64 processes, the break appears
                //    as a WOW64 breakpoint, which CDB is configured to ignore since
                //    it also triggers on module loading.
                // CDB 32 bit: untested
                useDebugBreakApi = false;
            } else {
                // GDB: Use win64interrupt for native 64bit processes only (it fails
                //    for WOW64 processes.
                useDebugBreakApi = isWow64Process(inferior);
            }
        } else {
            // Windows is 32 bit
            // All processes are 32 bit, so DebugBreakProcess can be used in all cases.
            useDebugBreakApi = true;
        }
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
#endif
        if (useDebugBreakApi) {
            ok = DebugBreakProcess(inferior);
            if (!ok)
                *errorMessage = QLatin1String("DebugBreakProcess failed: ") + Utils::winErrorMessage(GetLastError());
        } else {
            const QString executable = QCoreApplication::applicationDirPath() + QLatin1String("/win64interrupt.exe");
            switch (QProcess::execute(executable + QLatin1Char(' ') + QString::number(pID))) {
            case -2:
                *errorMessage = QString::fromLatin1("Cannot start %1. Check src\\tools\\win64interrupt\\win64interrupt.c for more information.").
                                arg(QDir::toNativeSeparators(executable));
                break;
            case 0:
                ok = true;
                break;
            default:
                *errorMessage = QDir::toNativeSeparators(executable)
                                + QLatin1String(" could not break the process.");
                break;
            }
            break;
        }
    } while (false);
    if (inferior != NULL)
        CloseHandle(inferior);
    if (!ok)
        *errorMessage = msgCannotInterrupt(pID, *errorMessage);
160
161
    return ok;
}
hjk's avatar
hjk committed
162

163
#else // Q_OS_WIN
con's avatar
con committed
164
165
166

#include <sys/types.h>
#include <signal.h>
167
168
#include <errno.h>
#include <string.h>
con's avatar
con committed
169

170
171
bool Debugger::Internal::interruptProcess(int pID, int /* engineType */,
                                          QString *errorMessage)
con's avatar
con committed
172
{
173
174
175
176
177
178
179
    if (pID <= 0) {
        *errorMessage = msgCannotInterrupt(pID, QString::fromLatin1("Invalid process id."));
        return false;
    }
    if (kill(pID, SIGINT)) {
        *errorMessage = msgCannotInterrupt(pID, QString::fromLocal8Bit(strerror(errno)));
        return false;
con's avatar
con committed
180
    }
181
    return true;
con's avatar
con committed
182
183
}

184
#endif // !Q_OS_WIN