wait_tree.cpp 14 KB

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