1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "threadshandler.h"
35 #include "gdb/gdbmi.h"
37 #include "debuggerconstants.h"
39 #include <QtCore/QDebug>
40 #include <QtCore/QTextStream>
45 ////////////////////////////////////////////////////////////////////////
49 ///////////////////////////////////////////////////////////////////////
51 static QString threadToolTip(const ThreadData &thread)
53 const char start[] = "<tr><td>";
54 const char sep[] = "</td><td>";
55 const char end[] = "</td>";
58 str << "<html><head/><body><table>"
59 << start << ThreadsHandler::tr("Thread id:")
60 << sep << thread.id << end;
61 if (!thread.targetId.isEmpty())
62 str << start << ThreadsHandler::tr("Target 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;
74 str << start << ThreadsHandler::tr("Stopped 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);
83 str << "</table></body></html>";
87 ////////////////////////////////////////////////////////////////////////
91 ///////////////////////////////////////////////////////////////////////
94 \struct Debugger::Internal::ThreadData
95 \brief A structure containing information about a single thread
99 \class Debugger::Internal::ThreadsHandler
100 \brief A model to represent the running threads in a QTreeView or ComboBox
103 ThreadsHandler::ThreadsHandler()
105 m_positionIcon(QLatin1String(":/debugger/images/location_16.png")),
106 m_emptyIcon(QLatin1String(":/debugger/images/debugger_empty_14.png"))
110 int ThreadsHandler::rowCount(const QModelIndex &parent) const
112 // Since the stack is not a tree, row count is 0 for any valid parent.
113 return parent.isValid() ? 0 : m_threads.size();
116 int ThreadsHandler::columnCount(const QModelIndex &parent) const
118 return parent.isValid() ? 0 : int(ThreadData::ColumnCount);
121 QVariant ThreadsHandler::data(const QModelIndex &index, int role) const
123 if (!index.isValid())
125 const int row = index.row();
126 if (row >= m_threads.size())
128 const ThreadData &thread = m_threads.at(row);
131 case Qt::DisplayRole:
132 switch (index.column()) {
133 case ThreadData::IdColumn:
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)
146 case ThreadData::CoreColumn:
148 case ThreadData::StateColumn:
150 case ThreadData::NameColumn:
151 if (!thread.name.isEmpty())
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;
168 QVariant ThreadsHandler::headerData
169 (int section, Qt::Orientation orientation, int role) const
171 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
174 case ThreadData::IdColumn:
175 return tr("Thread ID");
176 case ThreadData::FunctionColumn:
177 return tr("Function");
178 case ThreadData::FileColumn:
180 case ThreadData::LineColumn:
182 case ThreadData::AddressColumn:
183 return tr("Address");
184 case ThreadData::CoreColumn:
186 case ThreadData::StateColumn:
188 case ThreadData::NameColumn:
194 int ThreadsHandler::currentThreadId() const
196 if (m_currentIndex < 0 || m_currentIndex >= m_threads.size())
198 return m_threads[m_currentIndex].id;
201 void ThreadsHandler::setCurrentThread(int index)
203 if (index == m_currentIndex)
206 // Emit changed for previous frame.
207 QModelIndex i = ThreadsHandler::index(m_currentIndex, 0);
208 emit dataChanged(i, i);
210 m_currentIndex = index;
212 // Emit changed for new frame
213 i = ThreadsHandler::index(m_currentIndex, 0);
214 emit dataChanged(i, i);
217 void ThreadsHandler::setCurrentThreadId(int id)
219 const int index = indexOf(id);
221 setCurrentThread(index);
223 qWarning("ThreadsHandler::setCurrentThreadId: No such thread %d.", id);
227 int ThreadsHandler::indexOf(quint64 threadId) const
229 const int count = m_threads.size();
230 for (int i = 0; i < count; ++i)
231 if (m_threads.at(i).id == threadId)
236 void ThreadsHandler::setThreads(const Threads &threads)
239 if (m_currentIndex >= m_threads.size())
244 Threads ThreadsHandler::threads() const
249 void ThreadsHandler::removeAll()
256 void ThreadsHandler::notifyRunning()
258 // Threads stopped (that is, address != 0 showing)?
259 if (m_threads.empty())
261 if (m_threads.front().address == 0)
263 const Threads::iterator end = m_threads.end();
264 for (Threads::iterator it = m_threads.begin(); it != end; ++it)
266 emit dataChanged(index(0, 1),
267 index(m_threads.size() - 1, ThreadData::ColumnCount - 1));
270 Threads ThreadsHandler::parseGdbmiThreads(const GdbMi &data, int *currentThread)
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();
280 for (int index = 0; index != n; ++index) {
282 const GdbMi item = items.at(index);
283 const GdbMi frame = item.findChild("frame");
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);
298 *currentThread = data.findChild("current-thread-id").data().toInt();
302 } // namespace Internal
303 } // namespace Debugger