Merge branch 'develop'
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / callstackbrowser.cpp
CommitLineData
0718b3f2
JPM
1//
2// callstackbrowser.cpp - Call Stack
3//
4// by Jean-Paul Mari
5//
6// JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7//
e27f08e7
JPM
8// Who When (M/D/Y) What
9// --- ------------ -----------------------------------------------------------
10// JPM 08/31/2018 Created this file
11// JPM 09/12/2018 Added a status bar and better status report
12// JPM 10/20/2018 Added the return address information in the call stack
13// JPM 08/09/2019 Prevent crash in case of call stack is out of range
14// JPM 03/16/2020 Modified the layout window and added source filename from the called source line
b53b74f6 15// JPM April/2021 Added a #line information
0718b3f2
JPM
16
17// STILL TO DO:
2b91c435 18// To set the information display at the right
8b11a1b0
JPM
19// To use DWARF frame information?
20// To check if call stack pointer is used (DWARF information?)
0718b3f2
JPM
21//
22
23#include "debugger/callstackbrowser.h"
24#include "memory.h"
25#include "debugger/DBGManager.h"
26#include "m68000/m68kinterface.h"
8b11a1b0 27#include "settings.h"
0718b3f2
JPM
28
29
2b91c435 30//
0718b3f2 31CallStackBrowserWindow::CallStackBrowserWindow(QWidget * parent/*= 0*/) : QWidget(parent, Qt::Dialog),
2b91c435
JPM
32#ifdef CS_LAYOUTTEXTS
33text(new QTextBrowser),
34#else
35TableView(new QTableView),
36model(new QStandardItemModel),
37#endif
38statusbar(new QStatusBar),
39layout(new QVBoxLayout)
0718b3f2
JPM
40{
41 setWindowTitle(tr("Call Stack"));
42
2b91c435 43 // Set the font
0718b3f2
JPM
44 QFont fixedFont("Lucida Console", 8, QFont::Normal);
45 fixedFont.setStyleHint(QFont::TypeWriter);
0718b3f2 46
2b91c435
JPM
47#ifdef CS_LAYOUTTEXTS
48 // Set original layout
49 text->setFont(fixedFont);
0718b3f2 50 layout->addWidget(text);
2b91c435
JPM
51#else
52 // Set the new layout with proper identation and readibility
418240cd 53 model->setColumnCount(5);
e27f08e7 54 model->setHeaderData(0, Qt::Horizontal, QObject::tr("Function"));
418240cd
JPM
55 model->setHeaderData(1, Qt::Horizontal, QObject::tr("#Line"));
56 model->setHeaderData(2, Qt::Horizontal, QObject::tr("Line"));
57 model->setHeaderData(3, Qt::Horizontal, QObject::tr("Return address"));
58 model->setHeaderData(4, Qt::Horizontal, QObject::tr("Filename"));
2b91c435
JPM
59 // Information table
60 TableView->setModel(model);
61 TableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
62 TableView->setShowGrid(0);
63 TableView->setFont(fixedFont);
64 TableView->verticalHeader()->setDefaultSectionSize(TableView->verticalHeader()->minimumSectionSize());
65 TableView->verticalHeader()->setDefaultAlignment(Qt::AlignRight);
66 layout->addWidget(TableView);
67#endif
68
69 // Status bar
70 layout->addWidget(statusbar);
71 setLayout(layout);
0718b3f2
JPM
72}
73
74
2b91c435 75//
0718b3f2
JPM
76CallStackBrowserWindow::~CallStackBrowserWindow(void)
77{
78}
79
80
2b91c435 81//
0718b3f2
JPM
82void CallStackBrowserWindow::RefreshContents(void)
83{
2b91c435
JPM
84 char msg[1024];
85 size_t Error = CS_NOERROR;
009df4d7 86 DBGstatus FilenameStatus;
0718b3f2 87 unsigned int a6, Sa6, ret;
e27f08e7
JPM
88 char *Name;
89 size_t NumError = 0;
2b91c435 90#ifdef CS_LAYOUTTEXTS
0718b3f2
JPM
91 QString CallStack;
92 char string[1024];
2b91c435 93#else
8b11a1b0 94 int NbRaw = 0;
2b91c435
JPM
95 QString FunctionName;
96#endif
0718b3f2
JPM
97
98 if (isVisible())
99 {
2b91c435
JPM
100#ifndef CS_LAYOUTTEXTS
101 model->setRowCount(0);
102#endif
0718b3f2
JPM
103 if ((a6 = m68k_get_reg(NULL, M68K_REG_A6)) && DBGManager_GetType())
104 {
8b11a1b0 105 while ((Sa6 = a6) && !NumError)
0718b3f2 106 {
8b11a1b0
JPM
107 if ((Sa6 >= (m68k_get_reg(NULL, M68K_REG_SP) - 4)) && (Sa6 < vjs.DRAM_size))
108 {
109 a6 = GET32(jaguarMainRAM, Sa6);
110 ret = GET32(jaguarMainRAM, Sa6 + 4);
2b91c435 111#ifdef CS_LAYOUTTEXTS
e27f08e7 112 sprintf(string, "0x%06X | Ret: 0x%06X | From: %s - 0x%06X | Line: %s", Sa6, ret, (Name = DBGManager_GetFunctionName(ret)), (unsigned int)DBGManager_GetAdrFromSymbolName(Name), DBGManager_GetLineSrcFromAdr(ret, DBG_NO_TAG));
8b11a1b0
JPM
113 CallStack += QString(string);
114 if (a6)
115 {
116 CallStack += QString("<br>");
117 }
118#else
e27f08e7 119 // insert line
8b11a1b0 120 model->insertRow(NbRaw);
e27f08e7
JPM
121 // display the function name
122 model->setItem(NbRaw, 0, new QStandardItem(QString("%1").arg((Name = DBGManager_GetFunctionName(ret)) ? Name : "(N/A)")));
418240cd
JPM
123 // display the line number
124 sprintf(msg, "%zi", DBGManager_GetNumLineFromAdr(ret, DBG_NO_TAG));
125 model->setItem(NbRaw, 1, new QStandardItem(QString("%1").arg((msg[0] != '0') ? msg : "(N/A)")));
e27f08e7
JPM
126 // display the called line
127 FunctionName = QString(Name = DBGManager_GetLineSrcFromAdr(ret, DBG_NO_TAG));
aae93d86 128 //FunctionName.replace("&nbsp;", " ");
e27f08e7 129 FunctionName = FunctionName.trimmed();
418240cd 130 model->setItem(NbRaw, 2, new QStandardItem(QString("%1").arg(Name ? FunctionName : "(N/A)")));
e27f08e7 131 // display the return address
8b11a1b0 132 sprintf(msg, "0x%06X", ret);
418240cd 133 model->setItem(NbRaw, 3, new QStandardItem(QString("%1").arg(msg)));
e27f08e7 134 // display the source filename from called source line
418240cd 135 model->setItem(NbRaw++, 4, new QStandardItem(QString("%1").arg(((Name = DBGManager_GetFullSourceFilenameFromAdr(ret, &FilenameStatus)) && !FilenameStatus) ? Name : "(N/A)")));
e27f08e7 136#endif
8b11a1b0
JPM
137 }
138 else
0718b3f2 139 {
8b11a1b0 140 NumError = 0x1;
0718b3f2
JPM
141 }
142 }
2b91c435 143#ifdef CS_LAYOUTTEXTS
0718b3f2
JPM
144 text->clear();
145 text->setText(CallStack);
2b91c435 146#endif
8b11a1b0
JPM
147 switch (NumError)
148 {
149 case 0:
150 sprintf(msg, "Ready");
151 Error = CS_NOERROR;
152 break;
153
154 case 0x1:
155 sprintf(msg, "Call Stack out of range");
156 Error = CS_ERROR;
157 break;
158
159 default:
160 sprintf(msg, "Call Stack in limbo");
161 Error = CS_WARNING;
162 break;
163 }
0718b3f2
JPM
164 }
165 else
166 {
2b91c435
JPM
167 sprintf(msg, "Call Stack not available");
168 Error = CS_NOCALLSTACK;
169#ifdef CS_LAYOUTTEXTS
0718b3f2 170 text->clear();
2b91c435
JPM
171#endif
172 }
173
174 // Display status bar
175 if (Error)
176 {
177 if ((Error & CS_WARNING))
178 {
179 statusbar->setStyleSheet("background-color: lightyellow; font: bold");
180 }
181 else
182 {
183 statusbar->setStyleSheet("background-color: tomato; font: bold");
184 }
0718b3f2 185 }
2b91c435
JPM
186 else
187 {
188 statusbar->setStyleSheet("background-color: lightgreen; font: bold");
189 }
190 statusbar->showMessage(QString(msg));
0718b3f2
JPM
191 }
192}
193
194
2b91c435 195//
0718b3f2
JPM
196void CallStackBrowserWindow::keyPressEvent(QKeyEvent * e)
197{
198 if (e->key() == Qt::Key_Escape)
199 {
200 hide();
201 }
202}
203
204