Keep the scrollbar position in accordance of the M68K PC pointer
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / DBGManager.cpp
1 //
2 // DBGManager.cpp: Debugger information manager
3 //
4 // by Jean-Paul Mari
5 //
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 //
8 // WHO WHEN WHAT
9 // --- ---------- ------------------------------------------------------------
10 // JPM 12/21/2016 Created this file
11 // JPM Various efforts to set the ELF format support
12 // JPM Various efforts to set the DWARF format support
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdint.h>
17 #include "libelf/libelf.h"
18 #include "libelf/gelf.h"
19 #include "log.h"
20 #include "ELFManager.h"
21 #include "DwarfManager.h"
22 #include "DBGManager.h"
23 #include "HWLABELManager.h"
24 #include "settings.h"
25 #include "memory.h"
26
27
28 //
29 char *DBGManager_GetVariableValueFromAdr(uint32_t Adr, uint32_t TypeEncoding, uint32_t TypeByteSize);
30
31
32 //
33 struct Value
34 {
35 union
36 {
37 char C[10];
38 double D;
39 float F;
40 int32_t SI;
41 int64_t SL;
42 uint32_t UI;
43 uint64_t UL;
44 };
45 }S_Value;
46
47
48 // Common debugger variables
49 size_t DBGType;
50 char value[1000];
51
52
53 // Common debugger initialisation
54 void DBGManager_Init(void)
55 {
56 DBGType = DBG_NO_TYPE;
57 ELFManager_Init();
58 DWARFManager_Init();
59 }
60
61
62 // Common debugger reset
63 void DBGManager_Reset(void)
64 {
65 if ((DBGType & DBG_DWARF))
66 {
67 DWARFManager_Reset();
68 }
69
70 if ((DBGType & DBG_ELF))
71 {
72 ELFManager_Reset();
73 }
74
75 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
76 DBGType = DBG_NO_TYPE;
77 }
78
79
80 // Common debugger set
81 void DBGManager_SetType(size_t DBGTypeSet)
82 {
83 DBGType |= DBGTypeSet;
84 }
85
86
87 // Common debugger close
88 void DBGManager_Close(void)
89 {
90 if ((DBGType & DBG_DWARF))
91 {
92 DWARFManager_Close();
93 }
94
95 if ((DBGType & DBG_ELF))
96 {
97 ELFManager_Close();
98 }
99 }
100
101
102 // Get source filename based on the memeory address
103 // return NULL if no source filename
104 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr, bool *Error)
105 {
106 if ((DBGType & DBG_ELFDWARF))
107 {
108 return DWARFManager_GetFullSourceFilenameFromAdr(Adr, Error);
109 }
110 else
111 {
112 return NULL;
113 }
114 }
115
116
117 // Get number of external variables
118 // Return 0 if none has been found
119 size_t DBGManager_GetNbExternalVariables(void)
120 {
121 if ((DBGType & DBG_ELFDWARF))
122 {
123 return DWARFManager_GetNbExternalVariables();
124 }
125 else
126 {
127 return 0;
128 }
129 }
130
131
132 //
133 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName)
134 {
135 if ((DBGType & DBG_ELF))
136 {
137 return ELFManager_GetAdrFromSymbolName(SymbolName);
138 }
139 else
140 {
141 return 0;
142 }
143 }
144
145
146 // Get external variable's Address based on his Name
147 // Return found Address
148 // Return NULL if no Address has been found
149 size_t DBGManager_GetExternalVariableAdrFromName(char *VariableName)
150 {
151 if ((DBGType & DBG_ELFDWARF))
152 {
153 return DWARFManager_GetExternalVariableAdrFromName(VariableName);
154 }
155 else
156 {
157 return 0;
158 }
159 }
160
161
162 //
163 size_t DBGManager_GetExternalVariableTypeTag(size_t Index)
164 {
165 if ((DBGType & DBG_ELFDWARF))
166 {
167 return DWARFManager_GetExternalVariableTypeTag(Index);
168 }
169 else
170 {
171 return 0;
172 }
173 }
174
175
176 // Get external variable's type name based on his Index
177 // Return type name's text pointer found
178 // Return NULL if no type name has been found
179 char *DBGManager_GetExternalVariableTypeName(size_t Index)
180 {
181 if ((DBGType & DBG_ELFDWARF))
182 {
183 return DWARFManager_GetExternalVariableTypeName(Index);
184 }
185 else
186 {
187 return NULL;
188 }
189 }
190
191
192 // Get external variable's Address based on his Index
193 // Return the Address found
194 // Return 0 if no Address has been found
195 size_t DBGManager_GetExternalVariableAdr(size_t Index)
196 {
197 if ((DBGType & DBG_ELFDWARF))
198 {
199 return DWARFManager_GetExternalVariableAdr(Index);
200 }
201 else
202 {
203 return 0;
204 }
205 }
206
207
208 // Get external variable's type byte size based on his Index
209 // Return the type's byte size found
210 // Return 0 if no type's byte size has been found
211 size_t DBGManager_GetExternalVariableTypeByteSize(size_t Index)
212 {
213 if ((DBGType & DBG_ELFDWARF))
214 {
215 return DWARFManager_GetExternalVariableTypeByteSize(Index);
216 }
217 else
218 {
219 return 0;
220 }
221 }
222
223
224 // Get external variable's type encoding based on his Index
225 // Return the type encoding found
226 // Return 0 if no type encoding has been found
227 size_t DBGManager_GetExternalVariableTypeEncoding(size_t Index)
228 {
229 if ((DBGType & DBG_ELFDWARF))
230 {
231 return DWARFManager_GetExternalVariableTypeEncoding(Index);
232 }
233 else
234 {
235 return 0;
236 }
237 }
238
239
240 // Get external variable value based on his Index
241 // Return value as a text pointer
242 // Note: Pointer may point on a 0 lenght text
243 char *DBGManager_GetExternalVariableValue(size_t Index)
244 {
245 uint32_t Adr = 0;
246 uint32_t TypeEncoding = DBG_NO_TYPEENCODING;
247 uint32_t TypeByteSize = 0;
248
249 if ((DBGType & DBG_ELFDWARF))
250 {
251 Adr = DWARFManager_GetExternalVariableAdr(Index);
252 TypeEncoding = DWARFManager_GetExternalVariableTypeEncoding(Index);
253 TypeByteSize = DWARFManager_GetExternalVariableTypeByteSize(Index);
254 }
255
256 return DBGManager_GetVariableValueFromAdr(Adr, TypeEncoding, TypeByteSize);
257 }
258
259
260 // Get variable value based on his Adresse, Encoding Type and Size
261 // Return value as a text pointer
262 // Note: Pointer may point on a 0 lenght text if Adress is NULL
263 char *DBGManager_GetVariableValueFromAdr(uint32_t Adr, uint32_t TypeEncoding, uint32_t TypeByteSize)
264 {
265 Value V;
266 char *Ptrvalue = value;
267
268 value[0] = 0;
269
270 if (Adr)
271 {
272 memset(&V, 0, sizeof(Value));
273 #if 0
274 for (uint32_t i = 0; i < TypeByteSize; i++)
275 jaguarMainRAM[Adr + i] = 0;
276 //jaguarMainRAM[Adr + i] = rand();
277 jaguarMainRAM[Adr + TypeByteSize - 1] = 0x10;
278 #endif
279 #if 1
280 for (uint32_t i = 0, j = TypeByteSize; i < TypeByteSize; i++, j--)
281 {
282 V.C[i] = jaguarMainRAM[Adr + j - 1];
283 }
284 #endif
285
286 switch (TypeEncoding)
287 {
288 case DBG_ATE_address:
289 break;
290
291 case DBG_ATE_boolean:
292 break;
293
294 case DBG_ATE_complex_float:
295 break;
296
297 case DBG_ATE_float:
298 switch (TypeByteSize)
299 {
300 case 4:
301 sprintf(value, "%F", V.F);
302 break;
303
304 case 8:
305 //V.D = (double)jaguarMainRAM[Adr];
306 //sprintf(value, "%10.10F", V.D);
307 sprintf(value, "%F", V.D);
308 break;
309
310 default:
311 break;
312 }
313 break;
314
315 case DBG_ATE_signed:
316 switch (TypeByteSize)
317 {
318 case 4:
319 sprintf(value, "%i", V.SI);
320 break;
321
322 case 8:
323 sprintf(value, "%i", V.SL);
324 break;
325
326 default:
327 break;
328 }
329 break;
330
331 case DBG_ATE_signed_char:
332 break;
333
334 case DBG_ATE_unsigned:
335 switch (TypeByteSize)
336 {
337 case 4:
338 sprintf(value, "%u", V.UI);
339 break;
340
341 case 8:
342 sprintf(value, "%u", V.UL);
343 break;
344
345 default:
346 break;
347 }
348 break;
349
350 case DBG_ATE_unsigned_char:
351 break;
352
353 case DBG_ATE_ptr:
354 switch (TypeByteSize)
355 {
356 case 4:
357 sprintf(value, "0x%06x", V.UI);
358 break;
359
360 default:
361 break;
362 }
363
364 default:
365 break;
366 }
367 }
368
369 return Ptrvalue;
370 }
371
372
373 // Get external variable name based on his Index
374 // Return variable name's text pointer found
375 // Return NULL if no variable name has been found
376 char *DBGManager_GetExternalVariableName(size_t Index)
377 {
378 if ((DBGType & DBG_ELFDWARF))
379 {
380 return DWARFManager_GetExternalVariableName(Index);
381 }
382 else
383 {
384 return NULL;
385 }
386 }
387
388
389 // Get line number from address and his tag
390 // Return line number on the symbol name found
391 // Return 0 if no symbol name has been found
392 size_t DBGManager_GetNumLineFromAdr(size_t Adr, size_t Tag)
393 {
394 if ((DBGType & DBG_ELFDWARF))
395 {
396 return DWARFManager_GetNumLineFromAdr(Adr, Tag);
397 }
398 else
399 {
400 return 0;
401 }
402 }
403
404
405 // Get symbol name from address
406 // Return text pointer on the symbol name found
407 // Return NULL if no symbol name has been found
408 char *DBGManager_GetSymbolnameFromAdr(size_t Adr)
409 {
410 char *Symbolname;
411
412 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
413 if (vjs.displayHWlabels)
414 {
415 Symbolname = HWLABELManager_GetSymbolnameFromAdr(Adr);
416 }
417 else
418 {
419 Symbolname = NULL;
420 }
421 #ifdef _MSC_VER
422 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
423 #else
424 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
425 #endif // _MSC_VER
426
427 if (Symbolname == NULL)
428 {
429 if ((DBGType & DBG_ELFDWARF))
430 {
431 Symbolname = DWARFManager_GetSymbolnameFromAdr(Adr);
432 }
433
434 if ((DBGType & DBG_ELF) && (Symbolname == NULL))
435 {
436 Symbolname = ELFManager_GetSymbolnameFromAdr(Adr);
437 }
438 }
439
440 return Symbolname;
441 }
442
443
444 // Get source line based on the Address and his Tag
445 // Return text pointer on the source line found
446 // Return NULL if no source line has been found
447 char *DBGManager_GetLineSrcFromAdr(size_t Adr, size_t Tag)
448 {
449 char *Symbolname = NULL;
450
451 if ((DBGType & DBG_ELFDWARF))
452 {
453 Symbolname = DWARFManager_GetLineSrcFromAdr(Adr, Tag);
454 }
455
456 return Symbolname;
457 }
458
459
460 // Get text line from source based on address and num line (starting by 1)
461 // Return NULL if no text line has been found
462 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr, size_t NumLine)
463 {
464 char *Symbolname = NULL;
465
466 if ((DBGType & DBG_ELFDWARF))
467 {
468 Symbolname = DWARFManager_GetLineSrcFromAdrNumLine(Adr, NumLine);
469 }
470
471 return Symbolname;
472 }
473
474
475 // Get text line from source based on address and num line (starting by 1)
476 // Return NULL if no text line has been found
477 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr, size_t NumLine)
478 {
479 char *Symbolname = NULL;
480
481 if ((DBGType & DBG_ELFDWARF))
482 {
483 Symbolname = DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr, NumLine);
484 }
485
486 return Symbolname;
487 }
488