Debugger source code clean-up
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / localbrowser.cpp
1 //
2 // localbrowser.cpp - Local variables
3 //
4 // by Jean-Paul Mari
5 //
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 //
8 // Who When What
9 // --- ---------- -----------------------------------------------------------
10 // JPM 11/03/2017 Created this file
11 // JPM Sept./2018 Added a status bar and better status report, and set information values in a tab
12 //
13
14 // STILL TO DO:
15 // Feature to list the pointer(s) in the code using the allocation
16 // To set the information display at the right
17 // To support the array
18 // To support the static variables
19 // To add a filter
20 //
21
22
23 #include "debugger/localbrowser.h"
24 #include "memory.h"
25 #include "debugger/DBGManager.h"
26 #include "settings.h"
27 #include "m68000/m68kinterface.h"
28
29
30 //
31 LocalBrowserWindow::LocalBrowserWindow(QWidget * parent/*= 0*/) : QWidget(parent, Qt::Dialog),
32 layout(new QVBoxLayout),
33 #ifdef LOCAL_LAYOUTTEXTS
34 text(new QTextBrowser),
35 #else
36 TableView(new QTableView),
37 model(new QStandardItemModel),
38 #endif
39 NbLocal(0),
40 FuncName((char *)calloc(1, 1)),
41 LocalInfo(NULL),
42 statusbar(new QStatusBar)
43 {
44 setWindowTitle(tr("Locals"));
45
46 // Set the font
47 QFont fixedFont("Lucida Console", 8, QFont::Normal);
48 fixedFont.setStyleHint(QFont::TypeWriter);
49
50 #ifdef LOCAL_LAYOUTTEXTS
51 // Set original layout
52 text->setFont(fixedFont);
53 layout->addWidget(text);
54 #else
55 // Set the new layout with proper identation and readibility
56 model->setColumnCount(3);
57 model->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
58 model->setHeaderData(1, Qt::Horizontal, QObject::tr("Value"));
59 model->setHeaderData(2, Qt::Horizontal, QObject::tr("Type"));
60 // Information table
61 TableView->setModel(model);
62 TableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
63 TableView->setShowGrid(0);
64 TableView->setFont(fixedFont);
65 TableView->verticalHeader()->setDefaultSectionSize(TableView->verticalHeader()->minimumSectionSize());
66 TableView->verticalHeader()->setDefaultAlignment(Qt::AlignRight);
67 layout->addWidget(TableView);
68 #endif
69
70 // Status bar
71 layout->addWidget(statusbar);
72 setLayout(layout);
73 }
74
75
76 //
77 LocalBrowserWindow::~LocalBrowserWindow(void)
78 {
79 free(LocalInfo);
80 free(FuncName);
81 }
82
83
84 //
85 bool LocalBrowserWindow::UpdateInfos(void)
86 {
87 size_t Adr;
88 char *Ptr;
89
90 if (NbLocal = DBGManager_GetNbLocalVariables(Adr = m68k_get_reg(NULL, M68K_REG_PC)))
91 {
92 if (Ptr = DBGManager_GetFunctionName(Adr))
93 {
94 if (strcmp(FuncName, Ptr))
95 {
96 if (FuncName = (char *)realloc(FuncName, strlen(Ptr) + 1))
97 {
98 strcpy(FuncName, Ptr);
99
100 if (LocalInfo = (WatchInfo *)realloc(LocalInfo, (sizeof(WatchInfo) * NbLocal)))
101 {
102 for (size_t i = 0; i < NbLocal; i++)
103 {
104 // Get local variable name and his information
105 if (LocalInfo[i].PtrVariableName = DBGManager_GetLocalVariableName(Adr, i + 1))
106 {
107 LocalInfo[i].Op = DBGManager_GetLocalVariableOp(Adr, i + 1);
108 LocalInfo[i].Adr = NULL;
109 LocalInfo[i].PtrCPURegisterName = NULL;
110 LocalInfo[i].TypeTag = DBGManager_GetLocalVariableTypeTag(Adr, i + 1);
111 LocalInfo[i].PtrVariableBaseTypeName = DBGManager_GetLocalVariableTypeName(Adr, i + 1);
112 LocalInfo[i].TypeEncoding = DBGManager_GetLocalVariableTypeEncoding(Adr, i + 1);
113 LocalInfo[i].TypeByteSize = DBGManager_GetLocalVariableTypeByteSize(Adr, i + 1);
114 LocalInfo[i].Offset = DBGManager_GetLocalVariableOffset(Adr, i + 1);
115 }
116 }
117 }
118 }
119 }
120
121 return true;
122 }
123 }
124
125 *FuncName = 0;
126
127 return false;
128 }
129
130
131 //
132 void LocalBrowserWindow::RefreshContents(void)
133 {
134 #ifdef LOCAL_LAYOUTTEXTS
135 char string[1024];
136 #endif
137 size_t Error = LOCAL_NOERROR;
138 QString Local;
139 QString MSG;
140 char Value1[100];
141 #ifdef LOCAL_SUPPORTARRAY
142 char Value[100];
143 #endif
144 char *PtrValue;
145
146 const char *CPURegName[] = { "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7" };
147
148 if (isVisible())
149 {
150 #ifndef LOCAL_LAYOUTTEXTS
151 model->setRowCount(0);
152 #endif
153 if (UpdateInfos())
154 {
155 for (size_t i = 0; i < NbLocal; i++)
156 {
157 if (LocalInfo[i].PtrVariableName)
158 {
159 memset(Value1, 0, sizeof(Value1));
160 #ifdef LOCAL_LAYOUTTEXTS
161 if (i)
162 {
163 Local += QString("<br>");
164 }
165 #else
166 model->insertRow(i);
167 #endif
168 // Local or parameters variables
169 if (((LocalInfo[i].Op >= DBG_OP_breg0) && (LocalInfo[i].Op <= DBG_OP_breg31)) || (LocalInfo[i].Op == DBG_OP_fbreg))
170 {
171 LocalInfo[i].Adr = m68k_get_reg(NULL, M68K_REG_A6) + LocalInfo[i].Offset;
172
173 if ((LocalInfo[i].Op == DBG_OP_fbreg))
174 {
175 LocalInfo[i].Adr += 8;
176 }
177
178 if ((LocalInfo[i].Adr >= 0) && (LocalInfo[i].Adr < vjs.DRAM_size))
179 {
180 if ((LocalInfo[i].TypeTag & (DBG_TAG_TYPE_array | DBG_TAG_TYPE_structure)))
181 {
182 #if defined(LOCAL_SUPPORTARRAY) || defined(LOCAL_SUPPORTSTRUCTURE)
183 //memcpy(Value1, &jaguarMainRAM[LocalInfo[i].Adr], 20);
184 #ifdef LOCAL_LAYOUTTEXTS
185 //sprintf(Value, "\"%s\"", Value1);
186 #else
187 //sprintf(Value, "0x%06X, \"%s\"", LocalInfo[i].Adr, Value1);
188 #endif
189 //PtrValue = Value;
190 PtrValue = NULL;
191 #else
192 PtrValue = NULL;
193 #endif
194 }
195 else
196 {
197 PtrValue = DBGManager_GetVariableValueFromAdr(LocalInfo[i].Adr, LocalInfo[i].TypeEncoding, LocalInfo[i].TypeByteSize);
198 }
199 }
200 else
201 {
202 PtrValue = NULL;
203 }
204 }
205 else
206 {
207 // Value from CPU register
208 if ((LocalInfo[i].Op >= DBG_OP_reg0) && (LocalInfo[i].Op <= DBG_OP_reg31))
209 {
210 LocalInfo[i].PtrCPURegisterName = (char *)CPURegName[(LocalInfo[i].Op - DBG_OP_reg0)];
211 PtrValue = itoa(m68k_get_reg(NULL, (m68k_register_t)((size_t)M68K_REG_D0 + (LocalInfo[i].Op - DBG_OP_reg0))), Value1, 10);
212 }
213 else
214 {
215 PtrValue = NULL;
216 }
217 }
218
219 #ifndef LOCAL_LAYOUTTEXTS
220 model->setItem(i, 0, new QStandardItem(QString("%1").arg(LocalInfo[i].PtrVariableName)));
221 #endif
222 // Check if the local variable is use by the code
223 if (!LocalInfo[i].Op)
224 {
225 #ifdef LOCAL_LAYOUTTEXTS
226 sprintf(string, "<font color='#A52A2A'>%i : %s | %s | [Not used]</font>", (i + 1), (LocalInfo[i].PtrVariableBaseTypeName ? LocalInfo[i].PtrVariableBaseTypeName : (char *)"<font color='#ff0000'>N/A</font>"), LocalInfo[i].PtrVariableName);
227 #else
228 #endif
229 }
230 else
231 {
232 #ifndef LOCAL_LAYOUTTEXTS
233 model->setItem(i, 1, new QStandardItem(QString("%1").arg(PtrValue)));
234 #else
235 sprintf(string, "%i : %s | %s | ", (i + 1), (LocalInfo[i].PtrVariableBaseTypeName ? LocalInfo[i].PtrVariableBaseTypeName : (char *)"<font color='#ff0000'>N/A</font>"), LocalInfo[i].PtrVariableName);
236 Local += QString(string);
237
238 if ((unsigned int)LocalInfo[i].Adr)
239 {
240 sprintf(string, "0x%06X", (unsigned int)LocalInfo[i].Adr);
241 }
242 else
243 {
244 if (LocalInfo[i].PtrCPURegisterName)
245 {
246 sprintf(string, "<font color='#0000FF'>%s</font>", LocalInfo[i].PtrCPURegisterName);
247 }
248 else
249 {
250 sprintf(string, "%s", (char *)"<font color='#ff0000'>N/A</font>");
251 }
252 }
253
254 Local += QString(string);
255 sprintf(string, " | %s", (!PtrValue ? (char *)"<font color='#ff0000'>N/A</font>" : PtrValue));
256 #endif
257 }
258 #ifndef LOCAL_LAYOUTTEXTS
259 model->setItem(i, 2, new QStandardItem(QString("%1").arg((LocalInfo[i].PtrVariableBaseTypeName ? LocalInfo[i].PtrVariableBaseTypeName : (char *)"<font color='#ff0000'>N/A</font>"))));
260 #else
261 Local += QString(string);
262 #endif
263 }
264 }
265
266 MSG += QString("Ready");
267 #ifdef LOCAL_LAYOUTTEXTS
268 text->clear();
269 text->setText(Local);
270 #endif
271 }
272 else
273 {
274 // No locals
275 MSG += QString("No locals");
276 Error = LOCAL_NOLOCALS;
277 #ifdef LOCAL_LAYOUTTEXTS
278 text->clear();
279 #endif
280 }
281
282 // Display status bar
283 if (Error)
284 {
285 if ((Error & LOCAL_WARNING))
286 {
287 statusbar->setStyleSheet("background-color: lightyellow; font: bold");
288 }
289 else
290 {
291 statusbar->setStyleSheet("background-color: tomato; font: bold");
292 }
293 }
294 else
295 {
296 statusbar->setStyleSheet("background-color: lightgreen; font: bold");
297 }
298 statusbar->showMessage(MSG);
299 }
300 }
301
302
303 //
304 void LocalBrowserWindow::keyPressEvent(QKeyEvent * e)
305 {
306 if (e->key() == Qt::Key_Escape)
307 {
308 hide();
309 }
310 }