OSDN Git Service

42d01f8ab0da832efcccc9a76d3f029d92620e9c
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / threadshandler.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 "threadshandler.h"
35 #include "gdb/gdbmi.h"
36
37 #include "debuggerconstants.h"
38
39 #include <QtCore/QDebug>
40 #include <QtCore/QTextStream>
41
42 namespace Debugger {
43 namespace Internal {
44
45 ////////////////////////////////////////////////////////////////////////
46 //
47 // ThreadsHandler
48 //
49 ///////////////////////////////////////////////////////////////////////
50
51 static QString threadToolTip(const ThreadData &thread)
52 {
53     const char start[] = "<tr><td>";
54     const char sep[] = "</td><td>";
55     const char end[] = "</td>";
56     QString rc;
57     QTextStream str(&rc);
58     str << "<html><head/><body><table>"
59         << start << ThreadsHandler::tr("Thread&nbsp;id:")
60         << sep << thread.id << end;
61     if (!thread.targetId.isEmpty())
62         str << start << ThreadsHandler::tr("Target&nbsp;id:")
63             << sep << thread.targetId << end;
64     if (!thread.name.isEmpty())
65         str << start << ThreadsHandler::tr("Name:")
66             << sep << thread.name << end;
67     if (!thread.state.isEmpty())
68         str << start << ThreadsHandler::tr("State:")
69             << sep << thread.state << end;
70     if (!thread.core.isEmpty())
71         str << start << ThreadsHandler::tr("Core:")
72             << sep << thread.core << end;
73     if (thread.address) {
74         str << start << ThreadsHandler::tr("Stopped&nbsp;at:") << sep;
75         if (!thread.function.isEmpty())
76             str << thread.function << "<br>";
77         if (!thread.fileName.isEmpty())
78             str << thread.fileName << ':' << thread.lineNumber << "<br>";
79         str.setIntegerBase(16);
80         str << "0x" << thread.address;
81         str.setIntegerBase(10);
82     }
83     str << "</table></body></html>";
84     return rc;
85 }
86
87 ////////////////////////////////////////////////////////////////////////
88 //
89 // ThreadsHandler
90 //
91 ///////////////////////////////////////////////////////////////////////
92
93 /*!
94     \struct Debugger::Internal::ThreadData
95     \brief A structure containing information about a single thread
96 */
97
98 /*!
99     \class Debugger::Internal::ThreadsHandler
100     \brief A model to represent the running threads in a QTreeView or ComboBox
101 */
102
103 ThreadsHandler::ThreadsHandler()
104   : m_currentIndex(0),
105     m_positionIcon(QLatin1String(":/debugger/images/location_16.png")),
106     m_emptyIcon(QLatin1String(":/debugger/images/debugger_empty_14.png"))
107 {
108 }
109
110 int ThreadsHandler::rowCount(const QModelIndex &parent) const
111 {
112     // Since the stack is not a tree, row count is 0 for any valid parent.
113     return parent.isValid() ? 0 : m_threads.size();
114 }
115
116 int ThreadsHandler::columnCount(const QModelIndex &parent) const
117 {
118     return parent.isValid() ? 0 : int(ThreadData::ColumnCount);
119 }
120
121 QVariant ThreadsHandler::data(const QModelIndex &index, int role) const
122 {
123     if (!index.isValid())
124         return QVariant();
125     const int row = index.row();
126     if (row  >= m_threads.size())
127         return QVariant();
128     const ThreadData &thread = m_threads.at(row);
129
130     switch (role) {
131     case Qt::DisplayRole:
132         switch (index.column()) {
133         case ThreadData::IdColumn:
134             return thread.id;
135         case ThreadData::FunctionColumn:
136             return thread.function;
137         case ThreadData::FileColumn:
138             return thread.fileName;
139         case ThreadData::LineColumn:
140             return thread.lineNumber >= 0
141                 ? QString::number(thread.lineNumber) : QString();
142         case ThreadData::AddressColumn:
143             return thread.address > 0
144                 ? QLatin1String("0x") + QString::number(thread.address, 16)
145                 : QString();
146         case ThreadData::CoreColumn:
147             return thread.core;
148         case ThreadData::StateColumn:
149             return thread.state;
150         case ThreadData::NameColumn:
151             if (!thread.name.isEmpty())
152                 return thread.name;
153             return thread.id;
154         }
155     case Qt::ToolTipRole:
156         return threadToolTip(thread);
157     case Qt::DecorationRole:
158         // Return icon that indicates whether this is the active stack frame.
159         if (index.column() == 0)
160             return (index.row() == m_currentIndex) ? m_positionIcon : m_emptyIcon;
161         break;
162     default:
163         break;
164     }
165     return QVariant();
166 }
167
168 QVariant ThreadsHandler::headerData
169     (int section, Qt::Orientation orientation, int role) const
170 {
171     if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
172         return QVariant();
173     switch (section) {
174     case ThreadData::IdColumn:
175         return tr("Thread ID");
176     case ThreadData::FunctionColumn:
177         return tr("Function");
178     case ThreadData::FileColumn:
179         return tr("File");
180     case ThreadData::LineColumn:
181         return tr("Line");
182     case ThreadData::AddressColumn:
183         return tr("Address");
184     case ThreadData::CoreColumn:
185         return tr("Core");
186     case ThreadData::StateColumn:
187         return tr("State");
188     case ThreadData::NameColumn:
189         return tr("Name");
190     }
191     return QVariant();
192 }
193
194 int ThreadsHandler::currentThreadId() const
195 {
196     if (m_currentIndex < 0 || m_currentIndex >= m_threads.size())
197         return -1;
198     return m_threads[m_currentIndex].id;
199 }
200
201 void ThreadsHandler::setCurrentThread(int index)
202 {
203     if (index == m_currentIndex)
204         return;
205
206     // Emit changed for previous frame.
207     QModelIndex i = ThreadsHandler::index(m_currentIndex, 0);
208     emit dataChanged(i, i);
209
210     m_currentIndex = index;
211
212     // Emit changed for new frame
213     i = ThreadsHandler::index(m_currentIndex, 0);
214     emit dataChanged(i, i);
215 }
216
217 void ThreadsHandler::setCurrentThreadId(int id)
218 {
219     const int index = indexOf(id);
220     if (index != -1) {
221         setCurrentThread(index);
222     } else {
223         qWarning("ThreadsHandler::setCurrentThreadId: No such thread %d.", id);
224     }
225 }
226
227 int ThreadsHandler::indexOf(quint64 threadId) const
228 {
229     const int count = m_threads.size();
230     for (int i = 0; i < count; ++i)
231         if (m_threads.at(i).id == threadId)
232             return i;
233     return -1;
234 }
235
236 void ThreadsHandler::setThreads(const Threads &threads)
237 {
238     m_threads = threads;
239     if (m_currentIndex >= m_threads.size())
240         m_currentIndex = -1;
241     layoutChanged();
242 }
243
244 Threads ThreadsHandler::threads() const
245 {
246     return m_threads;
247 }
248
249 void ThreadsHandler::removeAll()
250 {
251     m_threads.clear();
252     m_currentIndex = 0;
253     layoutChanged();
254 }
255
256 void ThreadsHandler::notifyRunning()
257 {
258     // Threads stopped (that is, address != 0 showing)?
259     if (m_threads.empty())
260         return;
261     if (m_threads.front().address == 0)
262         return;
263     const Threads::iterator end = m_threads.end();
264     for (Threads::iterator it = m_threads.begin(); it != end; ++it)
265         it->notifyRunning();
266     emit dataChanged(index(0, 1),
267         index(m_threads.size() - 1, ThreadData::ColumnCount - 1));
268 }
269
270 Threads ThreadsHandler::parseGdbmiThreads(const GdbMi &data, int *currentThread)
271 {
272     // ^done,threads=[{id="1",target-id="Thread 0xb7fdc710 (LWP 4264)",
273     // frame={level="0",addr="0x080530bf",func="testQString",args=[],
274     // file="/.../app.cpp",fullname="/../app.cpp",line="1175"},
275     // state="stopped",core="0"}],current-thread-id="1"
276     const QList<GdbMi> items = data.findChild("threads").children();
277     const int n = items.size();
278     Threads threads;
279     threads.reserve(n);
280     for (int index = 0; index != n; ++index) {
281         bool ok = false;
282         const GdbMi item = items.at(index);
283         const GdbMi frame = item.findChild("frame");
284         ThreadData thread;
285         thread.id = item.findChild("id").data().toInt();
286         thread.targetId = QString::fromAscii(item.findChild("target-id").data());
287         thread.core = QString::fromLatin1(item.findChild("core").data());
288         thread.state = QString::fromLatin1(item.findChild("state").data());
289         thread.address = frame.findChild("addr").data().toULongLong(&ok, 0);
290         thread.function = QString::fromLatin1(frame.findChild("func").data());
291         thread.fileName = QString::fromLatin1(frame.findChild("fullname").data());
292         thread.lineNumber = frame.findChild("line").data().toInt();
293         // Non-GDB (Cdb2) output name here.
294         thread.name = QString::fromLatin1(frame.findChild("name").data());
295         threads.append(thread);
296     }
297     if (currentThread)
298         *currentThread = data.findChild("current-thread-id").data().toInt();
299     return threads;
300 }
301
302 } // namespace Internal
303 } // namespace Debugger