Added a Local browser window for local variables
[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 struct Value
30 {
31 union
32 {
33 char C[10];
34 double D;
35 float F;
36 int32_t SI;
37 int64_t SL;
38 uint32_t UI;
39 uint64_t UL;
40 };
41 }S_Value;
42
43
44 // Common debugger variables
45 size_t DBGType;
46 char value[1000];
47
48
49 // Common debugger initialisation
50 void DBGManager_Init(void)
51 {
52 DBGType = DBG_NO_TYPE;
53 ELFManager_Init();
54 DWARFManager_Init();
55 }
56
57
58 // Common debugger reset
59 void DBGManager_Reset(void)
60 {
61 if ((DBGType & DBG_DWARF))
62 {
63 DWARFManager_Reset();
64 }
65
66 if ((DBGType & DBG_ELF))
67 {
68 ELFManager_Reset();
69 }
70
71 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
72 DBGType = DBG_NO_TYPE;
73 }
74
75
76 // Common debugger set
77 void DBGManager_SetType(size_t DBGTypeSet)
78 {
79 DBGType |= DBGTypeSet;
80 }
81
82
83 // Common debugger close
84 void DBGManager_Close(void)
85 {
86 if ((DBGType & DBG_DWARF))
87 {
88 DWARFManager_Close();
89 }
90
91 if ((DBGType & DBG_ELF))
92 {
93 ELFManager_Close();
94 }
95 }
96
97
98 // Get source filename based on the memeory address
99 // return NULL if no source filename
100 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr, bool *Error)
101 {
102 if ((DBGType & DBG_ELFDWARF))
103 {
104 return DWARFManager_GetFullSourceFilenameFromAdr(Adr, Error);
105 }
106 else
107 {
108 return NULL;
109 }
110 }
111
112
113 // Get number of local variables
114 // Return 0 if none has been found
115 size_t DBGManager_GetNbLocalVariables(size_t Adr)
116 {
117 if ((DBGType & DBG_ELFDWARF))
118 {
119 return DWARFManager_GetNbLocalVariables(Adr);
120 }
121 else
122 {
123 return 0;
124 }
125 }
126
127
128 // Get number of global variables
129 // Return 0 if none has been found
130 size_t DBGManager_GetNbGlobalVariables(void)
131 {
132 if ((DBGType & DBG_ELFDWARF))
133 {
134 return DWARFManager_GetNbGlobalVariables();
135 }
136 else
137 {
138 return 0;
139 }
140 }
141
142
143 //
144 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName)
145 {
146 if ((DBGType & DBG_ELF))
147 {
148 return ELFManager_GetAdrFromSymbolName(SymbolName);
149 }
150 else
151 {
152 return 0;
153 }
154 }
155
156
157 // Get global variable's Address based on his Name
158 // Return found Address
159 // Return NULL if no Address has been found
160 size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName)
161 {
162 if ((DBGType & DBG_ELFDWARF))
163 {
164 return DWARFManager_GetGlobalVariableAdrFromName(VariableName);
165 }
166 else
167 {
168 return 0;
169 }
170 }
171
172
173 // Get local variable's type encoding based on his address and Index
174 // Return the type encoding found
175 // Return 0 if no type encoding has been found
176 size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr, size_t Index)
177 {
178 if ((DBGType & DBG_ELFDWARF))
179 {
180 return DWARFManager_GetLocalVariableTypeEncoding(Adr, Index);
181 }
182 else
183 {
184 return 0;
185 }
186 }
187
188
189 //
190 int DBGManager_GetLocalVariableOffset(size_t Adr, size_t Index)
191 {
192 if ((DBGType & DBG_ELFDWARF))
193 {
194 return DWARFManager_GetLocalVariableOffset(Adr, Index);
195 }
196 else
197 {
198 return 0;
199 }
200 }
201
202
203 // Get local variable's type byte size based on his address and Index
204 // Return the type's byte size found
205 // Return 0 if no type's byte size has been found
206 size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr, size_t Index)
207 {
208 if ((DBGType & DBG_ELFDWARF))
209 {
210 return DWARFManager_GetLocalVariableTypeByteSize(Adr, Index);
211 }
212 else
213 {
214 return 0;
215 }
216 }
217
218
219 //
220 size_t DBGManager_GetLocalVariableTypeTag(size_t Adr, size_t Index)
221 {
222 if ((DBGType & DBG_ELFDWARF))
223 {
224 return DWARFManager_GetLocalVariableTypeTag(Adr, Index);
225 }
226 else
227 {
228 return 0;
229 }
230 }
231
232
233 //
234 size_t DBGManager_GetGlobalVariableTypeTag(size_t Index)
235 {
236 if ((DBGType & DBG_ELFDWARF))
237 {
238 return DWARFManager_GetGlobalVariableTypeTag(Index);
239 }
240 else
241 {
242 return 0;
243 }
244 }
245
246
247 // Get global variable's type name based on his Index
248 // Return type name's text pointer found
249 // Return NULL if no type name has been found
250 char *DBGManager_GetGlobalVariableTypeName(size_t Index)
251 {
252 if ((DBGType & DBG_ELFDWARF))
253 {
254 return DWARFManager_GetGlobalVariableTypeName(Index);
255 }
256 else
257 {
258 return NULL;
259 }
260 }
261
262
263 // Get global variable's Address based on his Index
264 // Return the Address found
265 // Return 0 if no Address has been found
266 size_t DBGManager_GetGlobalVariableAdr(size_t Index)
267 {
268 if ((DBGType & DBG_ELFDWARF))
269 {
270 return DWARFManager_GetGlobalVariableAdr(Index);
271 }
272 else
273 {
274 return 0;
275 }
276 }
277
278
279 // Get global variable's type byte size based on his Index
280 // Return the type's byte size found
281 // Return 0 if no type's byte size has been found
282 size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index)
283 {
284 if ((DBGType & DBG_ELFDWARF))
285 {
286 return DWARFManager_GetGlobalVariableTypeByteSize(Index);
287 }
288 else
289 {
290 return 0;
291 }
292 }
293
294
295 // Get global variable's type encoding based on his Index
296 // Return the type encoding found
297 // Return 0 if no type encoding has been found
298 size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index)
299 {
300 if ((DBGType & DBG_ELFDWARF))
301 {
302 return DWARFManager_GetGlobalVariableTypeEncoding(Index);
303 }
304 else
305 {
306 return 0;
307 }
308 }
309
310
311 // Get global variable value based on his Index
312 // Return value as a text pointer
313 // Note: Pointer may point on a 0 lenght text
314 char *DBGManager_GetGlobalVariableValue(size_t Index)
315 {
316 size_t Adr = 0;
317 size_t TypeEncoding = DBG_NO_TYPEENCODING;
318 size_t TypeByteSize = 0;
319
320 if ((DBGType & DBG_ELFDWARF))
321 {
322 Adr = DWARFManager_GetGlobalVariableAdr(Index);
323 TypeEncoding = DWARFManager_GetGlobalVariableTypeEncoding(Index);
324 TypeByteSize = DWARFManager_GetGlobalVariableTypeByteSize(Index);
325 }
326
327 return DBGManager_GetVariableValueFromAdr(Adr, TypeEncoding, TypeByteSize);
328 }
329
330
331 // Get variable value based on his Adresse, Encoding Type and Size
332 // Return value as a text pointer
333 // Note: Pointer may point on a 0 lenght text if Adress is NULL
334 char *DBGManager_GetVariableValueFromAdr(size_t Adr, size_t TypeEncoding, size_t TypeByteSize)
335 {
336 Value V;
337 char *Ptrvalue = value;
338
339 value[0] = 0;
340
341 if (Adr)
342 {
343 memset(&V, 0, sizeof(Value));
344 #if 0
345 for (uint32_t i = 0; i < TypeByteSize; i++)
346 jaguarMainRAM[Adr + i] = 0;
347 //jaguarMainRAM[Adr + i] = rand();
348 jaguarMainRAM[Adr + TypeByteSize - 1] = 0x10;
349 #endif
350 #if 1
351 for (size_t i = 0, j = TypeByteSize; i < TypeByteSize; i++, j--)
352 {
353 V.C[i] = jaguarMainRAM[Adr + j - 1];
354 }
355 #endif
356
357 switch (TypeEncoding)
358 {
359 case DBG_ATE_address:
360 break;
361
362 case DBG_ATE_boolean:
363 break;
364
365 case DBG_ATE_complex_float:
366 break;
367
368 case DBG_ATE_float:
369 switch (TypeByteSize)
370 {
371 case 4:
372 sprintf(value, "%F", V.F);
373 break;
374
375 case 8:
376 //V.D = (double)jaguarMainRAM[Adr];
377 //sprintf(value, "%10.10F", V.D);
378 sprintf(value, "%F", V.D);
379 break;
380
381 default:
382 break;
383 }
384 break;
385
386 case DBG_ATE_signed:
387 switch (TypeByteSize)
388 {
389 case 4:
390 sprintf(value, "%i", V.SI);
391 break;
392
393 case 8:
394 sprintf(value, "%i", V.SL);
395 break;
396
397 default:
398 break;
399 }
400 break;
401
402 case DBG_ATE_signed_char:
403 break;
404
405 case DBG_ATE_unsigned:
406 switch (TypeByteSize)
407 {
408 case 4:
409 sprintf(value, "%u", V.UI);
410 break;
411
412 case 8:
413 sprintf(value, "%u", V.UL);
414 break;
415
416 default:
417 break;
418 }
419 break;
420
421 case DBG_ATE_unsigned_char:
422 break;
423
424 case DBG_ATE_ptr:
425 switch (TypeByteSize)
426 {
427 case 4:
428 sprintf(value, "0x%06x", V.UI);
429 break;
430
431 default:
432 break;
433 }
434
435 default:
436 break;
437 }
438 }
439
440 return Ptrvalue;
441 }
442
443
444 // Get local variable's type name based on his Index
445 // Return type name's text pointer found
446 // Return NULL if no type name has been found
447 char *DBGManager_GetLocalVariableTypeName(size_t Adr, size_t Index)
448 {
449 if ((DBGType & DBG_ELFDWARF))
450 {
451 return DWARFManager_GetLocalVariableTypeName(Adr, Index);
452 }
453 else
454 {
455 return NULL;
456 }
457 }
458
459
460 // Get local variable Op based on his Index
461 // Return variable Op's found
462 // Return 0 if no variable Op has been found
463 size_t DBGManager_GetLocalVariableOp(size_t Adr, size_t Index)
464 {
465 if ((DBGType & DBG_ELFDWARF))
466 {
467 return DWARFManager_GetLocalVariableOp(Adr, Index);
468 }
469 else
470 {
471 return 0;
472 }
473 }
474
475
476 // Get local variable name based on his Index
477 // Return variable name's text pointer found
478 // Return NULL if no variable name has been found
479 char *DBGManager_GetLocalVariableName(size_t Adr, size_t Index)
480 {
481 if ((DBGType & DBG_ELFDWARF))
482 {
483 return DWARFManager_GetLocalVariableName(Adr, Index);
484 }
485 else
486 {
487 return NULL;
488 }
489 }
490
491
492 // Get global variable name based on his Index
493 // Return variable name's text pointer found
494 // Return NULL if no variable name has been found
495 char *DBGManager_GetGlobalVariableName(size_t Index)
496 {
497 if ((DBGType & DBG_ELFDWARF))
498 {
499 return DWARFManager_GetGlobalVariableName(Index);
500 }
501 else
502 {
503 return NULL;
504 }
505 }
506
507
508 // Get function name from address
509 // Return function name found
510 // Return NULL if no function name has been found
511 char *DBGManager_GetFunctionName(size_t Adr)
512 {
513 if ((DBGType & DBG_ELFDWARF))
514 {
515 return DWARFManager_GetFunctionName(Adr);
516 }
517 else
518 {
519 return NULL;
520 }
521 }
522
523
524 // Get line number from address and his tag
525 // Return line number on the symbol name found
526 // Return 0 if no symbol name has been found
527 size_t DBGManager_GetNumLineFromAdr(size_t Adr, size_t Tag)
528 {
529 if ((DBGType & DBG_ELFDWARF))
530 {
531 return DWARFManager_GetNumLineFromAdr(Adr, Tag);
532 }
533 else
534 {
535 return 0;
536 }
537 }
538
539
540 // Get symbol name from address
541 // Return text pointer on the symbol name found
542 // Return NULL if no symbol name has been found
543 char *DBGManager_GetSymbolNameFromAdr(size_t Adr)
544 {
545 char *Symbolname;
546
547 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
548 if (vjs.displayHWlabels)
549 {
550 Symbolname = HWLABELManager_GetSymbolnameFromAdr(Adr);
551 }
552 else
553 {
554 Symbolname = NULL;
555 }
556 #ifdef _MSC_VER
557 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
558 #else
559 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
560 #endif // _MSC_VER
561
562 if (Symbolname == NULL)
563 {
564 if ((DBGType & DBG_ELFDWARF))
565 {
566 Symbolname = DWARFManager_GetSymbolnameFromAdr(Adr);
567 }
568
569 if ((DBGType & DBG_ELF) && (Symbolname == NULL))
570 {
571 Symbolname = ELFManager_GetSymbolnameFromAdr(Adr);
572 }
573 }
574
575 return Symbolname;
576 }
577
578
579 // Get source line based on the Address and his Tag
580 // Return text pointer on the source line found
581 // Return NULL if no source line has been found
582 char *DBGManager_GetLineSrcFromAdr(size_t Adr, size_t Tag)
583 {
584 char *Symbolname = NULL;
585
586 if ((DBGType & DBG_ELFDWARF))
587 {
588 Symbolname = DWARFManager_GetLineSrcFromAdr(Adr, Tag);
589 }
590
591 return Symbolname;
592 }
593
594
595 // Get text line from source based on address and num line (starting by 1)
596 // Return NULL if no text line has been found
597 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr, size_t NumLine)
598 {
599 char *Symbolname = NULL;
600
601 if ((DBGType & DBG_ELFDWARF))
602 {
603 Symbolname = DWARFManager_GetLineSrcFromAdrNumLine(Adr, NumLine);
604 }
605
606 return Symbolname;
607 }
608
609
610 // Get text line from source based on address and num line (starting by 1)
611 // Return NULL if no text line has been found
612 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr, size_t NumLine)
613 {
614 char *Symbolname = NULL;
615
616 if ((DBGType & DBG_ELFDWARF))
617 {
618 Symbolname = DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr, NumLine);
619 }
620
621 return Symbolname;
622 }
623