OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / gdb / abstractplaingdbadapter.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "abstractplaingdbadapter.h"
35 #include "gdbmi.h"
36 #include "gdbengine.h"
37 #include "debuggerstartparameters.h"
38 #include "debuggeractions.h"
39 #include "debuggercore.h"
40 #include "debuggerstringutils.h"
41
42 #include <utils/qtcassert.h>
43
44 namespace Debugger {
45 namespace Internal {
46
47 #define CB(callback) \
48     static_cast<GdbEngine::AdapterCallback>(&AbstractPlainGdbAdapter::callback), \
49     STRINGIFY(callback)
50
51 AbstractPlainGdbAdapter::AbstractPlainGdbAdapter(GdbEngine *engine,
52                                                  QObject *parent)
53     : AbstractGdbAdapter(engine, parent)
54 {
55 }
56
57 void AbstractPlainGdbAdapter::setupInferior()
58 {
59     QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
60     if (!startParameters().processArgs.isEmpty()) {
61         QString args = startParameters().processArgs;
62         m_engine->postCommand("-exec-arguments " + toLocalEncoding(args));
63     }
64     m_engine->postCommand("-file-exec-and-symbols \"" + execFilePath() + '"',
65         CB(handleFileExecAndSymbols));
66 }
67
68 void AbstractPlainGdbAdapter::handleFileExecAndSymbols(const GdbResponse &response)
69 {
70     QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
71     if (response.resultClass == GdbResultDone) {
72         if (infoTargetNecessary()) {
73             // Old gdbs do not announce the PID for programs without pthreads.
74             // Note that successfully preloading the debugging helpers will
75             // automatically load pthreads, so this will be unnecessary.
76             if (m_engine->m_gdbVersion < 70000)
77                 m_engine->postCommand("info target", CB(handleInfoTarget));
78         }
79         m_engine->handleInferiorPrepared();
80     } else {
81         QByteArray ba = response.data.findChild("msg").data();
82         QString msg = fromLocalEncoding(ba);
83         // Extend the message a bit in unknown cases.
84         if (!ba.endsWith("File format not recognized"))
85             msg = tr("Starting executable failed:\n") + msg;
86         m_engine->notifyInferiorSetupFailed(msg);
87     }
88 }
89
90 void AbstractPlainGdbAdapter::runEngine()
91 {
92     QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
93     m_engine->postCommand("-exec-run", GdbEngine::RunRequest, CB(handleExecRun));
94 }
95
96 void AbstractPlainGdbAdapter::handleExecRun(const GdbResponse &response)
97 {
98     QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
99     if (response.resultClass == GdbResultRunning) {
100         m_engine->notifyEngineRunAndInferiorRunOk();
101         //showStatusMessage(tr("Running..."));
102         showMessage(_("INFERIOR STARTED"));
103         showMessage(msgInferiorSetupOk(), StatusBar);
104         // FIXME: That's the wrong place for it.
105         if (debuggerCore()->boolSetting(EnableReverseDebugging))
106             m_engine->postCommand("target record");
107     } else {
108         QString msg = fromLocalEncoding(response.data.findChild("msg").data());
109         //QTC_ASSERT(status() == InferiorRunOk, /**/);
110         //interruptInferior();
111         showMessage(msg);
112         m_engine->notifyEngineRunFailed();
113     }
114 }
115
116 void AbstractPlainGdbAdapter::handleInfoTarget(const GdbResponse &response)
117 {
118     if (response.resultClass == GdbResultDone) {
119         // [some leading stdout here]
120         // >&"        Entry point: 0x80831f0  0x08048134 - 0x08048147 is .interp\n"
121         // [some trailing stdout here]
122         QString msg = _(response.data.findChild("consolestreamoutput").data());
123         QRegExp needle(_("\\bEntry point: 0x([0-9a-f]+)\\b"));
124         if (needle.indexIn(msg) != -1) {
125             m_engine->m_entryPoint =
126                     "0x" + needle.cap(1).toLatin1().rightJustified(sizeof(void *) * 2, '0');
127             m_engine->postCommand("tbreak *0x" + needle.cap(1).toAscii());
128             // Do nothing here - inferiorPrepared handles the sequencing.
129         } else {
130             m_engine->notifyInferiorSetupFailed(_("Parsing start address failed"));
131         }
132     } else if (response.resultClass == GdbResultError) {
133         m_engine->notifyInferiorSetupFailed(_("Fetching start address failed"));
134     }
135 }
136
137 } // namespace Debugger
138 } // namespace Internal