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