OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / gdb / pythongdbengine.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "gdbengine.h"
34 #include "gdbmi.h"
35 #include "abstractgdbadapter.h"
36 #include "debuggeractions.h"
37 #include "debuggercore.h"
38 #include "debuggerstringutils.h"
39 #include "debuggertooltipmanager.h"
40
41 #include "breakhandler.h"
42 #include "watchhandler.h"
43 #include "stackhandler.h"
44
45 #include <utils/qtcassert.h>
46
47 #define PRECONDITION QTC_ASSERT(hasPython(), /**/)
48 #define CB(callback) &GdbEngine::callback, STRINGIFY(callback)
49
50
51 namespace Debugger {
52 namespace Internal {
53
54 void GdbEngine::updateLocalsPython(bool tryPartial, const QByteArray &varList)
55 {
56     PRECONDITION;
57     m_processedNames.clear();
58     watchHandler()->beginCycle(!tryPartial);
59     //m_toolTipExpression.clear();
60     WatchHandler *handler = watchHandler();
61
62     QByteArray expanded = "expanded:" + handler->expansionRequests() + ' ';
63     expanded += "typeformats:" + handler->typeFormatRequests() + ' ';
64     expanded += "formats:" + handler->individualFormatRequests();
65
66     QByteArray watchers;
67     const QString fileName = stackHandler()->currentFrame().file;
68     const QString function = stackHandler()->currentFrame().function;
69     if (!fileName.isEmpty()) {
70         QStringList expressions =
71             DebuggerToolTipManager::instance()->treeWidgetExpressions(fileName, objectName(), function);
72         const QString currentExpression = tooltipExpression();
73         if (!currentExpression.isEmpty() && !expressions.contains(currentExpression))
74             expressions.push_back(currentExpression);
75         foreach (const QString &te, expressions) {
76             if (!watchers.isEmpty())
77                 watchers += "##";
78             watchers += te.toLatin1();
79             watchers += '#';
80             watchers += tooltipIName(te);
81         }
82     }
83
84     QHash<QByteArray, int> watcherNames = handler->watcherNames();
85     QHashIterator<QByteArray, int> it(watcherNames);
86     while (it.hasNext()) {
87         it.next();
88         if (!watchers.isEmpty())
89             watchers += "##";
90         watchers += it.key() + "#watch." + QByteArray::number(it.value());
91     }
92
93     QByteArray options;
94     if (debuggerCore()->boolSetting(UseDebuggingHelpers))
95         options += "fancy,";
96     if (debuggerCore()->boolSetting(AutoDerefPointers))
97         options += "autoderef,";
98     if (!qgetenv("QTC_DEBUGGER_PYTHON_VERBOSE").isEmpty())
99         options += "pe,";
100     if (options.isEmpty())
101         options += "defaults,";
102     if (tryPartial)
103         options += "partial,";
104     options.chop(1);
105
106     QByteArray resultVar;
107     if (!m_resultVarName.isEmpty())
108         resultVar = "resultvarname:" + m_resultVarName + ' ';
109
110     postCommand("bb options:" + options + " vars:" + varList + ' '
111             + resultVar + expanded + " watchers:" + watchers.toHex(),
112         WatchUpdate, CB(handleStackFramePython), QVariant(tryPartial));
113 }
114
115 void GdbEngine::handleStackListLocalsPython(const GdbResponse &response)
116 {
117     PRECONDITION;
118     if (response.resultClass == GdbResultDone) {
119         // 44^done,data={locals=[name="model",name="backString",...]}
120         QByteArray varList = "vars"; // Dummy entry, will be stripped by dumper.
121         foreach (const GdbMi &child, response.data.findChild("locals").children()) {
122             varList.append(',');
123             varList.append(child.data());
124         }
125         updateLocalsPython(false, varList);
126     }
127 }
128
129 void GdbEngine::handleStackFramePython(const GdbResponse &response)
130 {
131     PRECONDITION;
132     if (response.resultClass == GdbResultDone) {
133         const bool partial = response.cookie.toBool();
134         //qDebug() << "READING " << (partial ? "PARTIAL" : "FULL");
135         QByteArray out = response.data.findChild("consolestreamoutput").data();
136         while (out.endsWith(' ') || out.endsWith('\n'))
137             out.chop(1);
138         //qDebug() << "SECOND CHUNK: " << out;
139         int pos = out.indexOf("data=");
140         if (pos != 0) {
141             showMessage(_("DISCARDING JUNK AT BEGIN OF RESPONSE: "
142                 + out.left(pos)));
143             out = out.mid(pos);
144         }
145         GdbMi all;
146         all.fromStringMultiple(out);
147         //qDebug() << "ALL: " << all.toString();
148
149         GdbMi data = all.findChild("data");
150         QList<WatchData> list;
151         foreach (const GdbMi &child, data.children()) {
152             WatchData dummy;
153             dummy.iname = child.findChild("iname").data();
154             GdbMi wname = child.findChild("wname");
155             if (wname.isValid()) {
156                 // Happens (only) for watched expressions. They are encoded as.
157                 // base64 encoded 8 bit data, without quotes
158                 dummy.name = decodeData(wname.data(), 5);
159                 dummy.exp = dummy.name.toUtf8();
160             } else {
161                 dummy.name = _(child.findChild("name").data());
162             }
163             //qDebug() << "CHILD: " << child.toString();
164             parseWatchData(watchHandler()->expandedINames(), dummy, child, &list);
165         }
166         watchHandler()->insertBulkData(list);
167         //for (int i = 0; i != list.size(); ++i)
168         //    qDebug() << "LOCAL: " << list.at(i).toString();
169
170 #if 0
171         data = all.findChild("bkpts");
172         if (data.isValid()) {
173             BreakHandler *handler = breakHandler();
174             foreach (const GdbMi &child, data.children()) {
175                 int bpNumber = child.findChild("number").data().toInt();
176                 int found = handler->findBreakpoint(bpNumber);
177                 if (found != -1) {
178                     BreakpointData *bp = handler->at(found);
179                     GdbMi addr = child.findChild("addr");
180                     if (addr.isValid()) {
181                         bp->bpAddress = child.findChild("addr").data();
182                         bp->pending = false;
183                     } else {
184                         bp->bpAddress = "<PENDING>";
185                         bp->pending = true;
186                     }
187                     bp->bpFuncName = child.findChild("func").data();
188                     bp->bpLineNumber = child.findChild("line").data();
189                     bp->bpFileName = child.findChild("file").data();
190                     bp->markerLineNumber = bp->bpLineNumber.toInt();
191                     bp->markerFileName = bp->bpFileName;
192                     // Happens with moved/symlinked sources.
193                     if (!bp->fileName.isEmpty()
194                             && !bp->bpFileName.isEmpty()
195                             && bp->fileName !=  bp->bpFileName)
196                         bp->markerFileName = bp->fileName;
197                 } else {
198                     QTC_ASSERT(false, qDebug() << child.toString() << bpNumber);
199                     //bp->bpNumber = "<unavailable>";
200                 }
201             }
202             handler->updateMarkers();
203         }
204 #endif
205
206         //PENDING_DEBUG("AFTER handleStackFrame()");
207         // FIXME: This should only be used when updateLocals() was
208         // triggered by expanding an item in the view.
209         if (m_pendingWatchRequests <= 0) {
210             //PENDING_DEBUG("\n\n ....  AND TRIGGERS MODEL UPDATE\n");
211             rebuildWatchModel();
212         }
213         if (!partial)
214             emit stackFrameCompleted();
215     } else {
216         showMessage(_("DUMPER FAILED: " + response.toString()));
217     }
218 }
219
220 // Called from CoreAdapter and AttachAdapter
221 void GdbEngine::updateAllPython()
222 {
223     PRECONDITION;
224     //PENDING_DEBUG("UPDATING ALL\n");
225     QTC_ASSERT(state() == InferiorUnrunnable || state() == InferiorStopOk, /**/);
226     reloadModulesInternal();
227     postCommand("-stack-list-frames", CB(handleStackListFrames),
228         QVariant::fromValue<StackCookie>(StackCookie(false, true)));
229     stackHandler()->setCurrentIndex(0);
230     if (m_gdbAdapter->isTrkAdapter())
231         m_gdbAdapter->trkReloadThreads();
232     else
233         postCommand("-thread-list-ids", CB(handleThreadListIds), 0);
234     reloadRegisters();
235     updateLocals();
236 }
237
238 } // namespace Internal
239 } // namespace Debugger