coregdbadapter.cpp 7.87 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30

#include "coregdbadapter.h"
hjk's avatar
hjk committed
31

32 33 34
#include "debuggercore.h"
#include "debuggerprotocol.h"
#include "debuggerstartparameters.h"
35
#include "debuggerstringutils.h"
36

hjk's avatar
hjk committed
37
#include <utils/qtcassert.h>
38

39 40
#include <QDir>
#include <QMessageBox>
hjk's avatar
hjk committed
41
#include <QTemporaryFile>
42

hjk's avatar
hjk committed
43 44
using namespace Utils;

45 46 47 48
namespace Debugger {
namespace Internal {

#define CB(callback) \
49
    static_cast<GdbEngine::GdbCommandCallback>(&GdbCoreEngine::callback), \
50 51 52 53 54 55 56 57
    STRINGIFY(callback)

///////////////////////////////////////////////////////////////////////
//
// CoreGdbAdapter
//
///////////////////////////////////////////////////////////////////////

58 59
GdbCoreEngine::GdbCoreEngine(const DebuggerStartParameters &startParameters)
    : GdbEngine(startParameters)
60 61
{}

62
GdbCoreEngine::~GdbCoreEngine()
hjk's avatar
hjk committed
63 64 65 66 67 68 69
{
    if (false && !m_tempCoreName.isEmpty()) {
        QFile tmpFile(m_tempCoreName);
        tmpFile.remove();
    }
}

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

hjk's avatar
hjk committed
75 76 77 78 79 80 81 82
    const DebuggerStartParameters &sp = startParameters();
    m_executable = sp.executable;
    QFileInfo fi(sp.coreFile);
    m_coreName = fi.absoluteFilePath();

    unpackCoreIfNeeded();
}

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
QString GdbCoreEngine::readExecutableNameFromCore(bool *isCore)
{
#if 0
    ElfReader reader(coreFileName());
    return QString::fromLocal8Bit(reader.readCoreName(isCore));
#else
    const DebuggerStartParameters &sp = startParameters();
    QStringList args;
    args.append(QLatin1String("-nx"));
    args.append(QLatin1String("-batch"));
    args.append(QLatin1String("-c"));
    args.append(coreFileName());
    QProcess proc;
    proc.start(sp.debuggerCommand, args);
    if (proc.waitForFinished()) {
        QByteArray ba = proc.readAllStandardOutput();
        // Core was generated by `/data/dev/creator-2.6/bin/qtcreator'.
        // Program terminated with signal 11, Segmentation fault.
        int pos1 = ba.indexOf("Core was generated by");
        if (pos1 != -1) {
            pos1 += 23;
            int pos2 = ba.indexOf('\'', pos1);
            if (pos2 != -1) {
                *isCore = true;
                return QString::fromLocal8Bit(ba.mid(pos1, pos2 - pos1));
            }
        }
    }
    return QString();
#endif
}

115
void GdbCoreEngine::continueSetupEngine()
hjk's avatar
hjk committed
116
{
hjk's avatar
hjk committed
117 118
    if (m_executable.isEmpty()) {
        // Read executable from core.
119
        bool isCore = false;
120
        m_executable = readExecutableNameFromCore(&isCore);
121 122 123 124 125

        if (!isCore) {
            showMessageBox(QMessageBox::Warning,
                tr("Error Loading Core File"),
                tr("The specified file does not appear to be a core file."));
126
            notifyEngineSetupFailed();
127 128
            return;
        }
hjk's avatar
hjk committed
129 130 131 132 133 134 135 136

        // Strip off command line arguments. FIXME: make robust.
        int idx = m_executable.indexOf(QLatin1Char(' '));
        if (idx >= 0)
            m_executable.truncate(idx);
        if (m_executable.isEmpty()) {
            showMessageBox(QMessageBox::Warning,
                tr("Error Loading Symbols"),
137
                tr("No executable to load symbols from specified core."));
138
            notifyEngineSetupFailed();
hjk's avatar
hjk committed
139 140 141
            return;
        }
    }
142
    startGdb();
143
}
144

145
void GdbCoreEngine::setupInferior()
hjk's avatar
hjk committed
146
{
147
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
148 149
    // Do that first, otherwise no symbols are loaded.
    QFileInfo fi(m_executable);
150
    QByteArray path = fi.absoluteFilePath().toLocal8Bit();
151
    postCommand("-file-exec-and-symbols \"" + path + '"',
152
         CB(handleFileExecAndSymbols));
153 154
}

155
void GdbCoreEngine::handleFileExecAndSymbols(const GdbResponse &response)
156
{
hjk's avatar
hjk committed
157
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
hjk's avatar
hjk committed
158
    QString core = coreFileName();
159
    if (response.resultClass == GdbResultDone) {
160
        showMessage(tr("Symbols found."), StatusBar);
161
        postCommand("target core " + core.toLocal8Bit(),
162 163
            CB(handleTargetCore));
        return;
164
    }
165
    QString msg = tr("No symbols found in core file <i>%1</i>.")
hjk's avatar
hjk committed
166
        .arg(core);
167 168 169 170 171 172
    msg += _(" ");
    msg += tr("This can be caused by a path length limitation in the "
        "core file.");
    msg += _(" ");
    msg += tr("Try to specify the binary using the "
        "<i>Debug->Start Debugging->Attach to Core</i> dialog.");
173
    notifyInferiorSetupFailed(msg);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
174 175
}

176
void GdbCoreEngine::handleTargetCore(const GdbResponse &response)
hjk's avatar
hjk committed
177
{
hjk's avatar
hjk committed
178
    QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
hjk's avatar
hjk committed
179
    if (response.resultClass == GdbResultDone) {
180
        // HACK: The namespace is not accessible in the initial run.
181
        tryLoadPythonDumpers();
182
        showMessage(tr("Attached to core."), StatusBar);
183
        handleInferiorPrepared();
184 185
        // Due to the auto-solib-add off setting, we don't have any
        // symbols yet. Load them in order of importance.
186 187 188
        reloadStack(true);
        reloadModulesInternal();
        postCommand("p 5", CB(handleRoundTrip));
189
        return;
hjk's avatar
hjk committed
190
    }
191 192
    QString msg = tr("Attach to core \"%1\" failed:\n")
        .arg(startParameters().coreFile)
193
        + QString::fromLocal8Bit(response.data["msg"].data());
194
    notifyInferiorSetupFailed(msg);
hjk's avatar
hjk committed
195
}
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
196

197
void GdbCoreEngine::handleRoundTrip(const GdbResponse &response)
198
{
199
    Q_UNUSED(response);
200 201 202 203
    loadSymbolsForStack();
    QTimer::singleShot(1000, this, SLOT(loadAllSymbols()));
}

204
void GdbCoreEngine::runEngine()
hjk's avatar
hjk committed
205
{
hjk's avatar
hjk committed
206
    QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
207 208
    notifyInferiorUnrunnable();
    updateAll();
hjk's avatar
hjk committed
209 210
}

211
void GdbCoreEngine::interruptInferior()
212
{
hjk's avatar
hjk committed
213
    // A core never runs, so this cannot be called.
214
    QTC_CHECK(false);
215 216
}

217
void GdbCoreEngine::shutdownEngine()
hjk's avatar
hjk committed
218
{
219
    notifyAdapterShutdownOk();
hjk's avatar
hjk committed
220 221
}

222
void GdbCoreEngine::unpackCoreIfNeeded()
hjk's avatar
hjk committed
223
{
hjk's avatar
hjk committed
224
    if (!m_coreName.endsWith(QLatin1String(".lzo"))) {
225
        continueSetupEngine();
hjk's avatar
hjk committed
226 227 228 229 230 231 232 233 234 235 236 237
        return;
    }

    {
        QString pattern = QDir::tempPath() + QLatin1String("/tmpcore-XXXXXX");
        QTemporaryFile tmp(pattern, this);
        tmp.open();
        m_tempCoreName = tmp.fileName();
    }

    QProcess *process = new QProcess(this);
    process->setWorkingDirectory(QDir::tempPath());
238 239 240
    QStringList arguments;
    arguments << QLatin1String("-o") << m_tempCoreName << QLatin1String("-x") << m_coreName;
    process->start(QLatin1String("/usr/bin/lzop"), arguments);
241
    connect(process, SIGNAL(finished(int)), SLOT(continueSetupEngine()));
hjk's avatar
hjk committed
242 243
}

244
QString GdbCoreEngine::coreFileName() const
hjk's avatar
hjk committed
245 246
{
    return m_tempCoreName.isEmpty() ? m_coreName : m_tempCoreName;
hjk's avatar
hjk committed
247 248
}

249 250
} // namespace Internal
} // namespace Debugger