coregdbadapter.cpp 6.37 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
31
32
33
**
** 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 "coregdbadapter.h"

#include "debuggeractions.h"
#include "gdbengine.h"
34
#include "debuggerstringutils.h"
35
36
37

#include <utils/qtcassert.h>

38
#include <QtCore/QDir>
39
#include <QtCore/QFileInfo>
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
40
#include <QtGui/QMessageBox>
41
42
43
44
45
46
47
48

namespace Debugger {
namespace Internal {

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

49
50
51
52
#ifdef Q_OS_LINUX
# define EXE_FROM_CORE
#endif

53
54
55
56
57
58
///////////////////////////////////////////////////////////////////////
//
// CoreGdbAdapter
//
///////////////////////////////////////////////////////////////////////

59
static QByteArray coreName(const DebuggerStartParameters &sp)
60
{
61
62
    QFileInfo fi(sp.coreFile);
    return fi.absoluteFilePath().toLocal8Bit();
63
64
}

65
66
67
68
69
70
CoreGdbAdapter::CoreGdbAdapter(GdbEngine *engine, QObject *parent)
  : AbstractGdbAdapter(engine, parent),
    m_executable(startParameters().executable),
    m_coreName(coreName(startParameters()))
{}

71
72
void CoreGdbAdapter::startAdapter()
{
hjk's avatar
hjk committed
73
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
74
    showMessage(_("TRYING TO START ADAPTER"));
75

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
76
77
    if (!m_engine->startGdb())
        return;
78

79
80
#ifndef EXE_FROM_CORE
    if (m_executable.isEmpty()) {
Roberto Raggi's avatar
Roberto Raggi committed
81
        DebuggerEngine::showMessageBox(QMessageBox::Warning, tr("Error Loading Symbols"),
82
83
                       tr("No executable to load symbols from specified."));
    }
84
    m_engine->handleAdapterStarted();
85
86
87
88
89
90
91
92
93
#else
    // Extra round trip to get executable name from core file.
    // This is sometimes not the full name, so it can't be used
    // as the generic solution.

    // Quoting core name below fails in gdb 6.8-debian.
    m_engine->postCommand("target core " + m_coreName,
        CB(handleTemporaryTargetCore));
#endif
94
95
}

96
void CoreGdbAdapter::handleTemporaryTargetCore(const GdbResponse &response)
97
{
98
99
100
101
102
103
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
    if (response.resultClass != GdbResultDone) {
        showMessage(tr("Attach to core failed."), StatusBar);
        m_engine->notifyEngineSetupFailed();
        return;
    }
104

105
106
107
108
109
110
    GdbMi console = response.data.findChild("consolestreamoutput");
    int pos1 = console.data().indexOf('`');
    int pos2 = console.data().indexOf('\'');
    if (pos1 == -1 || pos2 == -1) {
        showMessage(tr("Attach to core failed."), StatusBar);
        m_engine->notifyEngineSetupFailed();
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
111
        return;
hjk's avatar
hjk committed
112
    }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

    m_executable = console.data().mid(pos1 + 1, pos2 - pos1 - 1);
    // Strip off command line arguments. FIXME: make robust.
    int idx = m_executable.indexOf(_c(' '));
    if (idx >= 0)
        m_executable.truncate(idx);
    if (m_executable.isEmpty()) {
        m_engine->postCommand("detach");
        m_engine->notifyEngineSetupFailed();
        return;
    }
    m_executable = QFileInfo(startParameters().coreFile).absoluteDir()
                   .absoluteFilePath(m_executable);
    showMessage(tr("Attached to core temporarily."), StatusBar);
    m_engine->postCommand("detach", CB(handleTemporaryDetach));
hjk's avatar
hjk committed
128
129
}

130
131
132
133
134
135
136
137
138
139
void CoreGdbAdapter::handleTemporaryDetach(const GdbResponse &response)
{
    QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
    if (response.resultClass == GdbResultDone)
        m_engine->notifyEngineSetupOk();
    else
        m_engine->notifyEngineSetupFailed();
}

void CoreGdbAdapter::setupInferior()
hjk's avatar
hjk committed
140
{
141
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
142
143
    // Do that first, otherwise no symbols are loaded.
    QFileInfo fi(m_executable);
144
145
146
    QByteArray path = fi.absoluteFilePath().toLocal8Bit();
    m_engine->postCommand("-file-exec-and-symbols \"" + path + '"',
         CB(handleFileExecAndSymbols));
147
148
}

149
150
void CoreGdbAdapter::handleFileExecAndSymbols(const GdbResponse &response)
{
hjk's avatar
hjk committed
151
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
152
    if (response.resultClass == GdbResultDone) {
153
        showMessage(tr("Symbols found."), StatusBar);
154
155
156
        m_engine->postCommand("target core " + m_coreName,
            CB(handleTargetCore));
        return;
157
    }
158
    m_engine->notifyInferiorSetupFailed(_("File or symbols not found"));
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
159
160
161
}

void CoreGdbAdapter::handleTargetCore(const GdbResponse &response)
hjk's avatar
hjk committed
162
{
hjk's avatar
hjk committed
163
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
hjk's avatar
hjk committed
164
    if (response.resultClass == GdbResultDone) {
165
166
        // HACK: The namespace is not accessible in the initial run.
        m_engine->loadPythonDumpers();
167
        showMessage(tr("Attached to core."), StatusBar);
hjk's avatar
hjk committed
168
        m_engine->handleInferiorPrepared();
169
        return;
hjk's avatar
hjk committed
170
    }
171
172
173
174
    QString msg = tr("Attach to core \"%1\" failed:\n")
        .arg(startParameters().coreFile)
        + QString::fromLocal8Bit(response.data.findChild("msg").data());
    m_engine->notifyInferiorSetupFailed(msg);
hjk's avatar
hjk committed
175
}
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
176

hjk's avatar
hjk committed
177
void CoreGdbAdapter::runEngine()
hjk's avatar
hjk committed
178
{
hjk's avatar
hjk committed
179
180
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
    m_engine->notifyInferiorUnrunnable();
hjk's avatar
hjk committed
181
182
183
    m_engine->updateAll();
}

184
185
void CoreGdbAdapter::interruptInferior()
{
hjk's avatar
hjk committed
186
    // A core never runs, so this cannot be called.
187
188
189
    QTC_ASSERT(false, /**/);
}

hjk's avatar
hjk committed
190
191
192
193
194
195
196
197
198
199
void CoreGdbAdapter::shutdownInferior()
{
    m_engine->notifyInferiorShutdownOk();
}

void CoreGdbAdapter::shutdownAdapter()
{
    m_engine->notifyAdapterShutdownOk();
}

200
201
} // namespace Internal
} // namespace Debugger