remotegdbadapter.cpp 8.43 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
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
**
** 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 "remotegdbadapter.h"
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
31

32
#include "debuggerstringutils.h"
33
34
35
#include "gdbengine.h"

#include <utils/qtcassert.h>
36
#include <utils/fancymainwindow.h>
37
#include <projectexplorer/toolchain.h>
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <QtCore/QFileInfo>
#include <QtGui/QMessageBox>

namespace Debugger {
namespace Internal {

#define CB(callback) \
    static_cast<GdbEngine::AdapterCallback>(&RemoteGdbAdapter::callback), \
    STRINGIFY(callback)

///////////////////////////////////////////////////////////////////////
//
// RemoteGdbAdapter
//
///////////////////////////////////////////////////////////////////////

55
56
57
RemoteGdbAdapter::RemoteGdbAdapter(GdbEngine *engine, int toolChainType, QObject *parent) :
    AbstractGdbAdapter(engine, parent),
    m_toolChainType(toolChainType)
58
{
59
60
61
62
63
64
    connect(&m_uploadProc, SIGNAL(error(QProcess::ProcessError)),
        this, SLOT(uploadProcError(QProcess::ProcessError)));
    connect(&m_uploadProc, SIGNAL(readyReadStandardOutput()),
        this, SLOT(readUploadStandardOutput()));
    connect(&m_uploadProc, SIGNAL(readyReadStandardError()),
        this, SLOT(readUploadStandardError()));
65
66
}

67
68
69
70
71
72
73
74
75
76
AbstractGdbAdapter::DumperHandling RemoteGdbAdapter::dumperHandling() const
{
    switch (m_toolChainType) {
    case ProjectExplorer::ToolChain::MinGW:
    case ProjectExplorer::ToolChain::MSVC:
    case ProjectExplorer::ToolChain::WINCE:
    case ProjectExplorer::ToolChain::WINSCW:
    case ProjectExplorer::ToolChain::GCCE:
    case ProjectExplorer::ToolChain::RVCT_ARMV5:
    case ProjectExplorer::ToolChain::RVCT_ARMV6:
77
    case ProjectExplorer::ToolChain::GCC_MAEMO:
78
79
80
81
82
83
84
        return DumperLoadedByGdb;
    default:
        break;
    }
    return DumperLoadedByGdbPreload;
}

85
void RemoteGdbAdapter::startAdapter()
86
{
hjk's avatar
hjk committed
87
    QTC_ASSERT(state() == EngineStarting, qDebug() << state());
88
89
90
    setState(AdapterStarting);
    debugMessage(_("TRYING TO START ADAPTER"));

91
    // FIXME: make asynchroneous
hjk's avatar
hjk committed
92
    // Start the remote server
93
    if (startParameters().serverStartScript.isEmpty()) {
94
        m_engine->showStatusMessage(_("No server start script given. "
hjk's avatar
hjk committed
95
96
            "Assuming server runs already."));
    } else {
97
        m_uploadProc.start(_("/bin/sh ") + startParameters().serverStartScript);
hjk's avatar
hjk committed
98
99
100
        m_uploadProc.waitForStarted();
    }

101
    if (!m_engine->startGdb(QStringList(), startParameters().debuggerCommand))
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
102
103
        // FIXME: cleanup missing
        return;
104
105
106
107

    emit adapterStarted();
}

108
109
110
111
112
void RemoteGdbAdapter::uploadProcError(QProcess::ProcessError error)
{
    QString msg;
    switch (error) {
        case QProcess::FailedToStart:
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
113
            msg = tr("The upload process failed to start. Shell missing?");
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
            break;
        case QProcess::Crashed:
            msg = tr("The upload process crashed some time after starting "
                "successfully.");
            break;
        case QProcess::Timedout:
            msg = tr("The last waitFor...() function timed out. "
                "The state of QProcess is unchanged, and you can try calling "
                "waitFor...() again.");
            break;
        case QProcess::WriteError:
            msg = tr("An error occurred when attempting to write "
                "to the upload process. For example, the process may not be running, "
                "or it may have closed its input channel.");
            break;
        case QProcess::ReadError:
            msg = tr("An error occurred when attempting to read from "
                "the upload process. For example, the process may not be running.");
            break;
        default:
            msg = tr("An unknown error in the upload process occurred. "
                "This is the default return value of error().");
    }

    m_engine->showStatusMessage(msg);
139
    showMessageBox(QMessageBox::Critical, tr("Error"), msg);
140
141
142
143
144
}

void RemoteGdbAdapter::readUploadStandardOutput()
{
    QByteArray ba = m_uploadProc.readAllStandardOutput();
145
    m_engine->showDebuggerOutput(LogOutput, QString::fromLocal8Bit(ba, ba.length()));
146
147
148
149
150
}

void RemoteGdbAdapter::readUploadStandardError()
{
    QByteArray ba = m_uploadProc.readAllStandardError();
151
    m_engine->showDebuggerOutput(LogError, QString::fromLocal8Bit(ba, ba.length()));
152
153
}

154
void RemoteGdbAdapter::startInferior()
155
{
156
    QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
157

158
159
160
161
162
163
164
165
166
167
168
    m_engine->postCommand("set architecture "
        + startParameters().remoteArchitecture.toLatin1());
    m_engine->postCommand("set sysroot "
        + startParameters().sysRoot.toLocal8Bit());
    m_engine->postCommand("set solib-search-path "
        + QFileInfo(startParameters().dumperLibrary).path().toLocal8Bit());

    if (!startParameters().processArgs.isEmpty()) {
        QString args = startParameters().processArgs.join(_(" "));
        m_engine->postCommand("-exec-arguments " + args.toLocal8Bit());
    }
169

170
    m_engine->postCommand("set target-async on", CB(handleSetTargetAsync));
171
    QString x = startParameters().executable;
hjk's avatar
hjk committed
172
173
    QFileInfo fi(startParameters().executable);
    QString fileName = fi.absoluteFilePath();
174
175
    m_engine->postCommand("-file-exec-and-symbols \""
        + fileName.toLocal8Bit() + '"',
hjk's avatar
hjk committed
176
        CB(handleFileExecAndSymbols));
177
178
179
180
}

void RemoteGdbAdapter::handleSetTargetAsync(const GdbResponse &response)
{
181
    QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
182
183
    if (response.resultClass == GdbResultError)
        qDebug() << "Adapter too old: does not support asynchronous mode.";
184
185
}

hjk's avatar
hjk committed
186
void RemoteGdbAdapter::handleFileExecAndSymbols(const GdbResponse &response)
187
{
188
    QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
189
190
    if (response.resultClass == GdbResultDone) {
        //m_breakHandler->clearBreakMarkers();
191
192
193
194
195
196

        // "target remote" does three things:
        //     (1) connects to the gdb server
        //     (2) starts the remote application
        //     (3) stops the remote application (early, e.g. in the dynamic linker)
        QString channel = startParameters().remoteChannel;
197
        m_engine->postCommand("target remote " + channel.toLatin1(),
198
            CB(handleTargetRemote));
199
    } else {
200
        QString msg = tr("Starting remote executable failed:\n");
201
        msg += QString::fromLocal8Bit(response.data.findChild("msg").data());
202
        emit inferiorStartFailed(msg);
203
204
205
    }
}

hjk's avatar
hjk committed
206
void RemoteGdbAdapter::handleTargetRemote(const GdbResponse &record)
207
{
208
    QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
209
    if (record.resultClass == GdbResultDone) {
210
        setState(InferiorStopped);
211
212
        // gdb server will stop the remote application itself.
        debugMessage(_("INFERIOR STARTED"));
Friedemann Kleint's avatar
Friedemann Kleint committed
213
        showStatusMessage(msgAttachedToStoppedInferior());
214
        emit inferiorPrepared();
215
    } else {
216
        // 16^error,msg="hd:5555: Connection timed out."
217
218
        QString msg = msgConnectRemoteServerFailed(
            QString::fromLocal8Bit(record.data.findChild("msg").data()));
hjk's avatar
hjk committed
219
        emit inferiorStartFailed(msg);
220
221
222
    }
}

223
void RemoteGdbAdapter::startInferiorPhase2()
224
{
225
    m_engine->continueInferiorInternal();
226
227
228
229
}

void RemoteGdbAdapter::interruptInferior()
{
230
231
    // FIXME: On some gdb versions like git 170ffa5d7dd this produces
    // >810^error,msg="mi_cmd_exec_interrupt: Inferior not executing."
232
    m_engine->postCommand("-exec-interrupt", GdbEngine::Immediate);
233
234
}

235
void RemoteGdbAdapter::shutdown()
236
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
237
    // FIXME: cleanup missing
238
239
240
241
}

} // namespace Internal
} // namespace Debugger