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