summaryrefslogtreecommitdiff
path: root/src/citra_qt/debugger/wait_tree.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/citra_qt/debugger/wait_tree.cpp')
-rw-r--r--src/citra_qt/debugger/wait_tree.cpp417
1 files changed, 417 insertions, 0 deletions
diff --git a/src/citra_qt/debugger/wait_tree.cpp b/src/citra_qt/debugger/wait_tree.cpp
new file mode 100644
index 000000000..be5a51e52
--- /dev/null
+++ b/src/citra_qt/debugger/wait_tree.cpp
@@ -0,0 +1,417 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "citra_qt/debugger/wait_tree.h"
6#include "citra_qt/util/util.h"
7
8#include "core/hle/kernel/event.h"
9#include "core/hle/kernel/mutex.h"
10#include "core/hle/kernel/semaphore.h"
11#include "core/hle/kernel/session.h"
12#include "core/hle/kernel/thread.h"
13#include "core/hle/kernel/timer.h"
14
15WaitTreeItem::~WaitTreeItem() {}
16
17QColor WaitTreeItem::GetColor() const {
18 return QColor(Qt::GlobalColor::black);
19}
20
21std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeItem::GetChildren() const {
22 return {};
23}
24
25void WaitTreeItem::Expand() {
26 if (IsExpandable() && !expanded) {
27 children = GetChildren();
28 for (std::size_t i = 0; i < children.size(); ++i) {
29 children[i]->parent = this;
30 children[i]->row = i;
31 }
32 expanded = true;
33 }
34}
35
36WaitTreeItem* WaitTreeItem::Parent() const {
37 return parent;
38}
39
40const std::vector<std::unique_ptr<WaitTreeItem>>& WaitTreeItem::Children() const {
41 return children;
42}
43
44bool WaitTreeItem::IsExpandable() const {
45 return false;
46}
47
48std::size_t WaitTreeItem::Row() const {
49 return row;
50}
51
52std::vector<std::unique_ptr<WaitTreeThread>> WaitTreeItem::MakeThreadItemList() {
53 const auto& threads = Kernel::GetThreadList();
54 std::vector<std::unique_ptr<WaitTreeThread>> item_list;
55 item_list.reserve(threads.size());
56 for (std::size_t i = 0; i < threads.size(); ++i) {
57 item_list.push_back(std::make_unique<WaitTreeThread>(*threads[i]));
58 item_list.back()->row = i;
59 }
60 return item_list;
61}
62
63WaitTreeText::WaitTreeText(const QString& t) : text(t) {}
64
65QString WaitTreeText::GetText() const {
66 return text;
67}
68
69WaitTreeWaitObject::WaitTreeWaitObject(const Kernel::WaitObject& o) : object(o) {}
70
71bool WaitTreeExpandableItem::IsExpandable() const {
72 return true;
73}
74
75QString WaitTreeWaitObject::GetText() const {
76 return tr("[%1]%2 %3")
77 .arg(object.GetObjectId())
78 .arg(QString::fromStdString(object.GetTypeName()),
79 QString::fromStdString(object.GetName()));
80}
81
82std::unique_ptr<WaitTreeWaitObject> WaitTreeWaitObject::make(const Kernel::WaitObject& object) {
83 switch (object.GetHandleType()) {
84 case Kernel::HandleType::Event:
85 return std::make_unique<WaitTreeEvent>(static_cast<const Kernel::Event&>(object));
86 case Kernel::HandleType::Mutex:
87 return std::make_unique<WaitTreeMutex>(static_cast<const Kernel::Mutex&>(object));
88 case Kernel::HandleType::Semaphore:
89 return std::make_unique<WaitTreeSemaphore>(static_cast<const Kernel::Semaphore&>(object));
90 case Kernel::HandleType::Timer:
91 return std::make_unique<WaitTreeTimer>(static_cast<const Kernel::Timer&>(object));
92 case Kernel::HandleType::Thread:
93 return std::make_unique<WaitTreeThread>(static_cast<const Kernel::Thread&>(object));
94 default:
95 return std::make_unique<WaitTreeWaitObject>(object);
96 }
97}
98
99std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeWaitObject::GetChildren() const {
100 std::vector<std::unique_ptr<WaitTreeItem>> list;
101
102 const auto& threads = object.GetWaitingThreads();
103 if (threads.empty()) {
104 list.push_back(std::make_unique<WaitTreeText>(tr("waited by no thread")));
105 } else {
106 list.push_back(std::make_unique<WaitTreeThreadList>(threads));
107 }
108 return list;
109}
110
111QString WaitTreeWaitObject::GetResetTypeQString(Kernel::ResetType reset_type) {
112 switch (reset_type) {
113 case Kernel::ResetType::OneShot:
114 return tr("one shot");
115 case Kernel::ResetType::Sticky:
116 return tr("sticky");
117 case Kernel::ResetType::Pulse:
118 return tr("pulse");
119 }
120}
121
122WaitTreeObjectList::WaitTreeObjectList(
123 const std::vector<Kernel::SharedPtr<Kernel::WaitObject>>& list, bool w_all)
124 : object_list(list), wait_all(w_all) {}
125
126QString WaitTreeObjectList::GetText() const {
127 if (wait_all)
128 return tr("waiting for all objects");
129 return tr("waiting for one of the following objects");
130}
131
132std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeObjectList::GetChildren() const {
133 std::vector<std::unique_ptr<WaitTreeItem>> list(object_list.size());
134 std::transform(object_list.begin(), object_list.end(), list.begin(),
135 [](const auto& t) { return WaitTreeWaitObject::make(*t); });
136 return list;
137}
138
139WaitTreeThread::WaitTreeThread(const Kernel::Thread& thread) : WaitTreeWaitObject(thread) {}
140
141QString WaitTreeThread::GetText() const {
142 const auto& thread = static_cast<const Kernel::Thread&>(object);
143 QString status;
144 switch (thread.status) {
145 case THREADSTATUS_RUNNING:
146 status = tr("running");
147 break;
148 case THREADSTATUS_READY:
149 status = tr("ready");
150 break;
151 case THREADSTATUS_WAIT_ARB:
152 status = tr("waiting for address 0x%1").arg(thread.wait_address, 8, 16, QLatin1Char('0'));
153 break;
154 case THREADSTATUS_WAIT_SLEEP:
155 status = tr("sleeping");
156 break;
157 case THREADSTATUS_WAIT_SYNCH:
158 status = tr("waiting for objects");
159 break;
160 case THREADSTATUS_DORMANT:
161 status = tr("dormant");
162 break;
163 case THREADSTATUS_DEAD:
164 status = tr("dead");
165 break;
166 }
167 QString pc_info = tr(" PC = 0x%1 LR = 0x%2")
168 .arg(thread.context.pc, 8, 16, QLatin1Char('0'))
169 .arg(thread.context.lr, 8, 16, QLatin1Char('0'));
170 return WaitTreeWaitObject::GetText() + pc_info + " (" + status + ") ";
171}
172
173QColor WaitTreeThread::GetColor() const {
174 const auto& thread = static_cast<const Kernel::Thread&>(object);
175 switch (thread.status) {
176 case THREADSTATUS_RUNNING:
177 return QColor(Qt::GlobalColor::darkGreen);
178 case THREADSTATUS_READY:
179 return QColor(Qt::GlobalColor::darkBlue);
180 case THREADSTATUS_WAIT_ARB:
181 return QColor(Qt::GlobalColor::darkRed);
182 case THREADSTATUS_WAIT_SLEEP:
183 return QColor(Qt::GlobalColor::darkYellow);
184 case THREADSTATUS_WAIT_SYNCH:
185 return QColor(Qt::GlobalColor::red);
186 case THREADSTATUS_DORMANT:
187 return QColor(Qt::GlobalColor::darkCyan);
188 case THREADSTATUS_DEAD:
189 return QColor(Qt::GlobalColor::gray);
190 default:
191 return WaitTreeItem::GetColor();
192 }
193}
194
195std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeThread::GetChildren() const {
196 std::vector<std::unique_ptr<WaitTreeItem>> list(WaitTreeWaitObject::GetChildren());
197
198 const auto& thread = static_cast<const Kernel::Thread&>(object);
199
200 QString processor;
201 switch (thread.processor_id) {
202 case ThreadProcessorId::THREADPROCESSORID_DEFAULT:
203 processor = tr("default");
204 break;
205 case ThreadProcessorId::THREADPROCESSORID_ALL:
206 processor = tr("all");
207 break;
208 case ThreadProcessorId::THREADPROCESSORID_0:
209 processor = tr("AppCore");
210 break;
211 case ThreadProcessorId::THREADPROCESSORID_1:
212 processor = tr("SysCore");
213 break;
214 default:
215 processor = tr("Unknown processor %1").arg(thread.processor_id);
216 break;
217 }
218
219 list.push_back(std::make_unique<WaitTreeText>(tr("processor = %1").arg(processor)));
220 list.push_back(std::make_unique<WaitTreeText>(tr("thread id = %1").arg(thread.GetThreadId())));
221 list.push_back(std::make_unique<WaitTreeText>(tr("priority = %1(current) / %2(normal)")
222 .arg(thread.current_priority)
223 .arg(thread.nominal_priority)));
224 list.push_back(std::make_unique<WaitTreeText>(
225 tr("last running ticks = %1").arg(thread.last_running_ticks)));
226
227 if (thread.held_mutexes.empty()) {
228 list.push_back(std::make_unique<WaitTreeText>(tr("not holding mutex")));
229 } else {
230 list.push_back(std::make_unique<WaitTreeMutexList>(thread.held_mutexes));
231 }
232 if (thread.status == THREADSTATUS_WAIT_SYNCH) {
233 list.push_back(std::make_unique<WaitTreeObjectList>(thread.wait_objects, thread.wait_all));
234 }
235
236 return list;
237}
238
239WaitTreeEvent::WaitTreeEvent(const Kernel::Event& object) : WaitTreeWaitObject(object) {}
240
241std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeEvent::GetChildren() const {
242 std::vector<std::unique_ptr<WaitTreeItem>> list(WaitTreeWaitObject::GetChildren());
243
244 list.push_back(std::make_unique<WaitTreeText>(
245 tr("reset type = %1")
246 .arg(GetResetTypeQString(static_cast<const Kernel::Event&>(object).reset_type))));
247 return list;
248}
249
250WaitTreeMutex::WaitTreeMutex(const Kernel::Mutex& object) : WaitTreeWaitObject(object) {}
251
252std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeMutex::GetChildren() const {
253 std::vector<std::unique_ptr<WaitTreeItem>> list(WaitTreeWaitObject::GetChildren());
254
255 const auto& mutex = static_cast<const Kernel::Mutex&>(object);
256 if (mutex.lock_count) {
257 list.push_back(
258 std::make_unique<WaitTreeText>(tr("locked %1 times by thread:").arg(mutex.lock_count)));
259 list.push_back(std::make_unique<WaitTreeThread>(*mutex.holding_thread));
260 } else {
261 list.push_back(std::make_unique<WaitTreeText>(tr("free")));
262 }
263 return list;
264}
265
266WaitTreeSemaphore::WaitTreeSemaphore(const Kernel::Semaphore& object)
267 : WaitTreeWaitObject(object) {}
268
269std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeSemaphore::GetChildren() const {
270 std::vector<std::unique_ptr<WaitTreeItem>> list(WaitTreeWaitObject::GetChildren());
271
272 const auto& semaphore = static_cast<const Kernel::Semaphore&>(object);
273 list.push_back(
274 std::make_unique<WaitTreeText>(tr("available count = %1").arg(semaphore.available_count)));
275 list.push_back(std::make_unique<WaitTreeText>(tr("max count = %1").arg(semaphore.max_count)));
276 return list;
277}
278
279WaitTreeTimer::WaitTreeTimer(const Kernel::Timer& object) : WaitTreeWaitObject(object) {}
280
281std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeTimer::GetChildren() const {
282 std::vector<std::unique_ptr<WaitTreeItem>> list(WaitTreeWaitObject::GetChildren());
283
284 const auto& timer = static_cast<const Kernel::Timer&>(object);
285
286 list.push_back(std::make_unique<WaitTreeText>(
287 tr("reset type = %1").arg(GetResetTypeQString(timer.reset_type))));
288 list.push_back(
289 std::make_unique<WaitTreeText>(tr("initial delay = %1").arg(timer.initial_delay)));
290 list.push_back(
291 std::make_unique<WaitTreeText>(tr("interval delay = %1").arg(timer.interval_delay)));
292 return list;
293}
294
295WaitTreeMutexList::WaitTreeMutexList(
296 const boost::container::flat_set<Kernel::SharedPtr<Kernel::Mutex>>& list)
297 : mutex_list(list) {}
298
299QString WaitTreeMutexList::GetText() const {
300 return tr("holding mutexes");
301}
302
303std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeMutexList::GetChildren() const {
304 std::vector<std::unique_ptr<WaitTreeItem>> list(mutex_list.size());
305 std::transform(mutex_list.begin(), mutex_list.end(), list.begin(),
306 [](const auto& t) { return std::make_unique<WaitTreeMutex>(*t); });
307 return list;
308}
309
310WaitTreeThreadList::WaitTreeThreadList(const std::vector<Kernel::SharedPtr<Kernel::Thread>>& list)
311 : thread_list(list) {}
312
313QString WaitTreeThreadList::GetText() const {
314 return tr("waited by thread");
315}
316
317std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeThreadList::GetChildren() const {
318 std::vector<std::unique_ptr<WaitTreeItem>> list(thread_list.size());
319 std::transform(thread_list.begin(), thread_list.end(), list.begin(),
320 [](const auto& t) { return std::make_unique<WaitTreeThread>(*t); });
321 return list;
322}
323
324WaitTreeModel::WaitTreeModel(QObject* parent) : QAbstractItemModel(parent) {}
325
326QModelIndex WaitTreeModel::index(int row, int column, const QModelIndex& parent) const {
327 if (!hasIndex(row, column, parent))
328 return {};
329
330 if (parent.isValid()) {
331 WaitTreeItem* parent_item = static_cast<WaitTreeItem*>(parent.internalPointer());
332 parent_item->Expand();
333 return createIndex(row, column, parent_item->Children()[row].get());
334 }
335
336 return createIndex(row, column, thread_items[row].get());
337}
338
339QModelIndex WaitTreeModel::parent(const QModelIndex& index) const {
340 if (!index.isValid())
341 return {};
342
343 WaitTreeItem* parent_item = static_cast<WaitTreeItem*>(index.internalPointer())->Parent();
344 if (!parent_item) {
345 return QModelIndex();
346 }
347 return createIndex(static_cast<int>(parent_item->Row()), 0, parent_item);
348}
349
350int WaitTreeModel::rowCount(const QModelIndex& parent) const {
351 if (!parent.isValid())
352 return static_cast<int>(thread_items.size());
353
354 WaitTreeItem* parent_item = static_cast<WaitTreeItem*>(parent.internalPointer());
355 parent_item->Expand();
356 return static_cast<int>(parent_item->Children().size());
357}
358
359int WaitTreeModel::columnCount(const QModelIndex&) const {
360 return 1;
361}
362
363QVariant WaitTreeModel::data(const QModelIndex& index, int role) const {
364 if (!index.isValid())
365 return {};
366
367 switch (role) {
368 case Qt::DisplayRole:
369 return static_cast<WaitTreeItem*>(index.internalPointer())->GetText();
370 case Qt::ForegroundRole:
371 return static_cast<WaitTreeItem*>(index.internalPointer())->GetColor();
372 default:
373 return {};
374 }
375}
376
377void WaitTreeModel::ClearItems() {
378 thread_items.clear();
379}
380
381void WaitTreeModel::InitItems() {
382 thread_items = WaitTreeItem::MakeThreadItemList();
383}
384
385WaitTreeWidget::WaitTreeWidget(QWidget* parent) : QDockWidget(tr("Wait Tree"), parent) {
386 setObjectName("WaitTreeWidget");
387 view = new QTreeView(this);
388 view->setHeaderHidden(true);
389 setWidget(view);
390 setEnabled(false);
391}
392
393void WaitTreeWidget::OnDebugModeEntered() {
394 if (!Core::g_app_core)
395 return;
396 model->InitItems();
397 view->setModel(model);
398 setEnabled(true);
399}
400
401void WaitTreeWidget::OnDebugModeLeft() {
402 setEnabled(false);
403 view->setModel(nullptr);
404 model->ClearItems();
405}
406
407void WaitTreeWidget::OnEmulationStarting(EmuThread* emu_thread) {
408 model = new WaitTreeModel(this);
409 view->setModel(model);
410 setEnabled(false);
411}
412
413void WaitTreeWidget::OnEmulationStopping() {
414 view->setModel(nullptr);
415 delete model;
416 setEnabled(false);
417}