2 // DBGManager.cpp: Debugger information manager
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
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 Oct./2018 Cosmetic changes, added source file search paths, and ELF function name
18 // To think about unique format to handle variations from ELF, DWARF, etc.
25 #include "libelf/libelf.h"
26 #include "libelf/gelf.h"
28 #include "ELFManager.h"
29 #include "DwarfManager.h"
30 #include "DBGManager.h"
31 #include "HWLABELManager.h"
55 void DBGManager_SourceFileSearchPathsInit(void);
56 void DBGManager_SourceFileSearchPathsReset(void);
57 void DBGManager_SourceFileSearchPathsClose(void);
60 // Common debugger variables
63 size_t NbSFSearchPaths
;
64 char **SourceFileSearchPaths
;
67 // Init the source file search paths
68 void DBGManager_SourceFileSearchPathsInit(void)
71 SourceFileSearchPaths
= NULL
;
75 // Set the source file search paths
76 // Create individual path for each one provided in the list (separate with ';')
77 void DBGManager_SourceFileSearchPathsSet(char *ListPaths
)
79 // Check presence of a previous list
82 // Reset previous list
83 DBGManager_SourceFileSearchPathsReset();
86 // Check if there is a paths list
87 if (strlen(ListPaths
))
89 // Get number of paths
90 char *Ptr
= ListPaths
;
93 while (*Ptr
&& (*Ptr
++ != ';'));
99 // Isolate each search path
100 SourceFileSearchPaths
= (char **)calloc(NbSFSearchPaths
, sizeof(char *));
106 // Search the path separator (';')
108 while (*Ptr
&& (*Ptr
++ != ';'));
110 // Copy the inidividual search path
111 SourceFileSearchPaths
[i
] = (char *)calloc(1, (Ptr
- Ptr1
) + 1);
112 strncpy(SourceFileSearchPaths
[i
], Ptr1
, (Ptr
- Ptr1
));
113 if (SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] == ';')
115 SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] = 0;
121 DWARFManager_Set(NbSFSearchPaths
, SourceFileSearchPaths
);
125 // Reset the source file search paths
126 void DBGManager_SourceFileSearchPathsReset(void)
129 while (NbSFSearchPaths
)
131 free(SourceFileSearchPaths
[--NbSFSearchPaths
]);
134 // Free the pointers list
135 free(SourceFileSearchPaths
);
136 SourceFileSearchPaths
= NULL
;
140 // Close the source file search paths
141 void DBGManager_SourceFileSearchPathsClose(void)
143 DBGManager_SourceFileSearchPathsReset();
147 // Common debugger initialisation
148 void DBGManager_Init(void)
150 // DBG initialisations
151 DBGType
= DBG_NO_TYPE
;
152 DBGManager_SourceFileSearchPathsInit();
154 // ELF initialisation
156 // DWARF initialisation
161 // Common debugger reset
162 void DBGManager_Reset(void)
164 if ((DBGType
& DBG_DWARF
))
166 DWARFManager_Reset();
169 if ((DBGType
& DBG_ELF
))
174 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
175 DBGType
= DBG_NO_TYPE
;
179 // Common debugger close
180 void DBGManager_Close(void)
182 if ((DBGType
& DBG_DWARF
))
184 DWARFManager_Close();
187 if ((DBGType
& DBG_ELF
))
192 DBGManager_SourceFileSearchPathsClose();
193 DBGType
= DBG_NO_TYPE
;
197 // Common debugger set
198 void DBGManager_SetType(size_t DBGTypeSet
)
200 DBGType
|= DBGTypeSet
;
205 size_t DBGManager_GetType(void)
211 // Get source filename based on the memeory address
212 // return NULL if no source filename
213 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr
, bool *Error
)
215 if ((DBGType
& DBG_ELFDWARF
))
217 return DWARFManager_GetFullSourceFilenameFromAdr(Adr
, Error
);
226 // Get number of local variables
227 // Return 0 if none has been found
228 size_t DBGManager_GetNbLocalVariables(size_t Adr
)
230 if ((DBGType
& DBG_ELFDWARF
))
232 return DWARFManager_GetNbLocalVariables(Adr
);
241 // Get number of global variables
242 // Return 0 if none has been found
243 size_t DBGManager_GetNbGlobalVariables(void)
245 if ((DBGType
& DBG_ELFDWARF
))
247 return DWARFManager_GetNbGlobalVariables();
256 // Get address from symbol name
257 // Return found address
258 // Return NULL if no symbol has been found
259 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName
)
263 if ((DBGType
& DBG_ELF
))
265 return ELFManager_GetAdrFromSymbolName(SymbolName
);
273 // Get global variable's Address based on his Name
274 // Return found Address
275 // Return NULL if no Address has been found
276 size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName
)
278 if ((DBGType
& DBG_ELFDWARF
))
280 return DWARFManager_GetGlobalVariableAdrFromName(VariableName
);
289 // Get local variable's type encoding based on his address and Index
290 // Return the type encoding found
291 // Return 0 if no type encoding has been found
292 size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
294 if ((DBGType
& DBG_ELFDWARF
))
296 return DWARFManager_GetLocalVariableTypeEncoding(Adr
, Index
);
306 int DBGManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
308 if ((DBGType
& DBG_ELFDWARF
))
310 return DWARFManager_GetLocalVariableOffset(Adr
, Index
);
319 // Get local variable's type byte size based on his address and Index
320 // Return the type's byte size found
321 // Return 0 if no type's byte size has been found
322 size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr
, size_t Index
)
324 if ((DBGType
& DBG_ELFDWARF
))
326 return DWARFManager_GetLocalVariableTypeByteSize(Adr
, Index
);
336 size_t DBGManager_GetLocalVariableTypeTag(size_t Adr
, size_t Index
)
338 if ((DBGType
& DBG_ELFDWARF
))
340 return DWARFManager_GetLocalVariableTypeTag(Adr
, Index
);
350 size_t DBGManager_GetGlobalVariableTypeTag(size_t Index
)
352 if ((DBGType
& DBG_ELFDWARF
))
354 return DWARFManager_GetGlobalVariableTypeTag(Index
);
363 // Get global variable's type name based on his Index
364 // Return type name's text pointer found
365 // Return NULL if no type name has been found
366 char *DBGManager_GetGlobalVariableTypeName(size_t Index
)
368 if ((DBGType
& DBG_ELFDWARF
))
370 return DWARFManager_GetGlobalVariableTypeName(Index
);
379 // Get global variable's Address based on his Index
380 // Return the Address found
381 // Return 0 if no Address has been found
382 size_t DBGManager_GetGlobalVariableAdr(size_t Index
)
384 if ((DBGType
& DBG_ELFDWARF
))
386 return DWARFManager_GetGlobalVariableAdr(Index
);
395 // Get global variable's type byte size based on his Index
396 // Return the type's byte size found
397 // Return 0 if no type's byte size has been found
398 size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index
)
400 if ((DBGType
& DBG_ELFDWARF
))
402 return DWARFManager_GetGlobalVariableTypeByteSize(Index
);
411 // Get global variable's type encoding based on his Index
412 // Return the type encoding found
413 // Return 0 if no type encoding has been found
414 size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index
)
416 if ((DBGType
& DBG_ELFDWARF
))
418 return DWARFManager_GetGlobalVariableTypeEncoding(Index
);
427 // Get global variable value based on his Index
428 // Return value as a text pointer
429 // Note: Pointer may point on a 0 lenght text
430 char *DBGManager_GetGlobalVariableValue(size_t Index
)
433 size_t TypeEncoding
= DBG_NO_TYPEENCODING
;
434 size_t TypeByteSize
= 0;
436 if ((DBGType
& DBG_ELFDWARF
))
438 Adr
= DWARFManager_GetGlobalVariableAdr(Index
);
439 TypeEncoding
= DWARFManager_GetGlobalVariableTypeEncoding(Index
);
440 TypeByteSize
= DWARFManager_GetGlobalVariableTypeByteSize(Index
);
443 return DBGManager_GetVariableValueFromAdr(Adr
, TypeEncoding
, TypeByteSize
);
447 // Get variable value based on his Adresse, Encoding Type and Size
448 // Return value as a text pointer
449 // Note: Pointer may point on a 0 length text
450 char *DBGManager_GetVariableValueFromAdr(size_t Adr
, size_t TypeEncoding
, size_t TypeByteSize
)
453 char *Ptrvalue
= value
;
461 memset(&V
, 0, sizeof(Value
));
463 for (uint32_t i
= 0; i
< TypeByteSize
; i
++)
464 jaguarMainRAM
[Adr
+ i
] = 0;
465 //jaguarMainRAM[Adr + i] = rand();
466 jaguarMainRAM
[Adr
+ TypeByteSize
- 1] = 0x10;
468 for (size_t i
= 0, j
= TypeByteSize
; i
< TypeByteSize
; i
++, j
--)
470 V
.Ct
[i
] = jaguarMainRAM
[Adr
+ j
- 1];
473 switch (TypeEncoding
)
475 case DBG_ATE_address
:
478 case DBG_ATE_boolean
:
479 sprintf(value
, "%s", V
.B
? "true" : "false");
482 case DBG_ATE_complex_float
:
486 switch (TypeByteSize
)
489 sprintf(value
, "%F", V
.F
);
493 //V.D = (double)jaguarMainRAM[Adr];
494 //sprintf(value, "%10.10F", V.D);
495 sprintf(value
, "%F", V
.D
);
504 switch (TypeByteSize
)
507 sprintf(value
, "%i", V
.SI
);
511 sprintf(value
, "%i", V
.SL
);
519 case DBG_ATE_signed_char
:
522 case DBG_ATE_unsigned
:
523 switch (TypeByteSize
)
526 sprintf(value
, "%u", V
.UI
);
530 sprintf(value
, "%u", V
.UL
);
538 case DBG_ATE_unsigned_char
:
539 sprintf(value
, "%u", (unsigned int(V
.C
)));
543 switch (TypeByteSize
)
546 sprintf(value
, "0x%06x", V
.UI
);
562 // Get local variable's type name based on his Index
563 // Return type name's text pointer found
564 // Return NULL if no type name has been found
565 char *DBGManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
567 if ((DBGType
& DBG_ELFDWARF
))
569 return DWARFManager_GetLocalVariableTypeName(Adr
, Index
);
578 // Get local variable Op based on his Index
579 // Return variable Op's found
580 // Return 0 if no variable Op has been found
581 size_t DBGManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
583 if ((DBGType
& DBG_ELFDWARF
))
585 return DWARFManager_GetLocalVariableOp(Adr
, Index
);
594 // Get local variable name based on his Index
595 // Return variable name's text pointer found
596 // Return NULL if no variable name has been found
597 char *DBGManager_GetLocalVariableName(size_t Adr
, size_t Index
)
599 if ((DBGType
& DBG_ELFDWARF
))
601 return DWARFManager_GetLocalVariableName(Adr
, Index
);
610 // Get global variable name based on his Index
611 // Return variable name's text pointer found
612 // Return NULL if no variable name has been found
613 char *DBGManager_GetGlobalVariableName(size_t Index
)
615 if ((DBGType
& DBG_ELFDWARF
))
617 return DWARFManager_GetGlobalVariableName(Index
);
626 // Get function name from address
627 // Return function name found
628 // Return NULL if no function name has been found
629 char *DBGManager_GetFunctionName(size_t Adr
)
631 char *Symbolname
= NULL
;
633 if ((DBGType
& DBG_ELFDWARF
))
635 Symbolname
= DWARFManager_GetFunctionName(Adr
);
638 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
640 Symbolname
= ELFManager_GetFunctionName(Adr
);
647 // Get line number from address and his tag
648 // Return line number on the symbol name found
649 // Return 0 if no symbol name has been found
650 size_t DBGManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
652 if ((DBGType
& DBG_ELFDWARF
))
654 return DWARFManager_GetNumLineFromAdr(Adr
, Tag
);
663 // Get symbol name from address
664 // Return text pointer on the symbol name found
665 // Return NULL if no symbol name has been found
666 char *DBGManager_GetSymbolNameFromAdr(size_t Adr
)
670 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
671 if (vjs
.displayHWlabels
)
673 Symbolname
= HWLABELManager_GetSymbolnameFromAdr(Adr
);
680 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
682 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
685 if (Symbolname
== NULL
)
687 if ((DBGType
& DBG_ELFDWARF
))
689 Symbolname
= DWARFManager_GetSymbolnameFromAdr(Adr
);
692 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
694 Symbolname
= ELFManager_GetSymbolnameFromAdr(Adr
);
702 // Get source line based on the Address and his Tag
703 // Return text pointer on the source line found
704 // Return NULL if no source line has been found
705 char *DBGManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
707 char *TextLine
= NULL
;
709 if ((DBGType
& DBG_ELFDWARF
))
711 TextLine
= DWARFManager_GetLineSrcFromAdr(Adr
, Tag
);
718 // Get text line from source based on address and num line (starting from 1)
719 // Return NULL if no text line has been found
720 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
722 char *TextLine
= NULL
;
724 if ((DBGType
& DBG_ELFDWARF
))
726 TextLine
= DWARFManager_GetLineSrcFromAdrNumLine(Adr
, NumLine
);
733 // Get text line from source based on address and num line (starting from 1)
734 // Return NULL if no text line has been found
735 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
737 char *TextLine
= NULL
;
739 if ((DBGType
& DBG_ELFDWARF
))
741 TextLine
= DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr
, NumLine
);
748 // Get number of source code filenames
749 size_t DBGManager_GetNbFullSourceFilename(void)
753 if ((DBGType
& DBG_ELFDWARF
))
755 Nbr
= DWARFManager_GetNbFullSourceFilename();
762 // Get source code filename based on index
763 char *DBGManager_GetNumFullSourceFilename(size_t Index
)
765 char *FullSourceFilename
= NULL
;
767 if ((DBGType
& DBG_ELFDWARF
))
769 FullSourceFilename
= DWARFManager_GetNumFullSourceFilename(Index
);
772 return FullSourceFilename
;