2 // DBGManager.cpp: Debugger information manager
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 // RG = Richard Goedeken
10 // --- ---------- ------------------------------------------------------------
11 // JPM 12/21/2016 Created this file
12 // JPM Various efforts to set the ELF format support
13 // JPM Various efforts to set the DWARF format support
14 // JPM 09/15/2018 Support the unsigned char
15 // JPM Oct./2018 Cosmetic changes, added source file search paths, and ELF function name
16 // JPM Aug./2019 Added new functions mainly for source text lines
17 // JPM Sept./2019 Support the unsigned/signed short type
18 // RG Jan./2021 Linux build fixes
19 // JPM May/2021 Code refactoring for the variables
23 // To think about unique format to handle variations from ELF, DWARF, etc.
33 #include "ELFManager.h"
34 #include "DWARFManager.h"
35 #include "DBGManager.h"
36 #include "HWLABELManager.h"
62 void DBGManager_SourceFileSearchPathsInit(void);
63 void DBGManager_SourceFileSearchPathsReset(void);
64 void DBGManager_SourceFileSearchPathsClose(void);
67 // Common debugger variables
70 size_t NbSFSearchPaths
;
71 char **SourceFileSearchPaths
;
74 // Init the source file search paths
75 void DBGManager_SourceFileSearchPathsInit(void)
78 SourceFileSearchPaths
= NULL
;
82 // Set the source file search paths
83 // Create individual path for each one provided in the list (separate with ';')
84 void DBGManager_SourceFileSearchPathsSet(char *ListPaths
)
86 // Check presence of a previous list
89 // Reset previous list
90 DBGManager_SourceFileSearchPathsReset();
93 // Check if there is a paths list
94 if (strlen(ListPaths
))
96 // Get number of paths
97 char *Ptr
= ListPaths
;
100 while (*Ptr
&& (*Ptr
++ != ';'));
106 // Isolate each search path
107 SourceFileSearchPaths
= (char **)calloc(NbSFSearchPaths
, sizeof(char *));
113 // Search the path separator (';')
115 while (*Ptr
&& (*Ptr
++ != ';'));
117 // Copy the inidividual search path
118 SourceFileSearchPaths
[i
] = (char *)calloc(1, (Ptr
- Ptr1
) + 1);
119 strncpy(SourceFileSearchPaths
[i
], Ptr1
, (Ptr
- Ptr1
));
120 if (SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] == ';')
122 SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] = 0;
128 DWARFManager_Set(NbSFSearchPaths
, SourceFileSearchPaths
);
132 // Reset the source file search paths
133 void DBGManager_SourceFileSearchPathsReset(void)
136 while (NbSFSearchPaths
)
138 free(SourceFileSearchPaths
[--NbSFSearchPaths
]);
141 // Free the pointers list
142 free(SourceFileSearchPaths
);
143 SourceFileSearchPaths
= NULL
;
147 // Close the source file search paths
148 void DBGManager_SourceFileSearchPathsClose(void)
150 DBGManager_SourceFileSearchPathsReset();
154 // Common debugger initialisation
155 void DBGManager_Init(void)
157 // DBG initialisations
158 DBGType
= DBG_NO_TYPE
;
159 DBGManager_SourceFileSearchPathsInit();
161 // ELF initialisation
163 // DWARF initialisation
168 // Common debugger reset
169 void DBGManager_Reset(void)
171 if ((DBGType
& DBG_DWARF
))
173 DWARFManager_Reset();
176 if ((DBGType
& DBG_ELF
))
181 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
182 DBGType
= DBG_NO_TYPE
;
186 // Common debugger close
187 void DBGManager_Close(void)
189 if ((DBGType
& DBG_DWARF
))
191 DWARFManager_Close();
194 if ((DBGType
& DBG_ELF
))
199 DBGManager_SourceFileSearchPathsClose();
200 DBGType
= DBG_NO_TYPE
;
204 // Common debugger set
205 void DBGManager_SetType(size_t DBGTypeSet
)
207 DBGType
|= DBGTypeSet
;
212 size_t DBGManager_GetType(void)
218 // Get source filename based on the memeory address
219 // return NULL if no source filename
220 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr
, DBGstatus
*Status
)
222 if ((DBGType
& DBG_ELFDWARF
))
224 return DWARFManager_GetFullSourceFilenameFromAdr(Adr
, (DWARFstatus
*)Status
);
233 // Get number of variables
234 // A NULL address will return the numbre of global variables, otherwise it will return the number of local variables
235 size_t DBGManager_GetNbVariables(size_t Adr
)
237 if ((DBGType
& DBG_ELFDWARF
))
239 return DWARFManager_GetNbVariables(Adr
);
248 // Get variable's information
249 // A NULL address will return the pointer to the global variable structure, otherwise it will return the local's one
250 S_VariablesStruct
* DBGManager_GetInfosVariable(size_t Adr
, size_t Index
)
252 if ((DBGType
& DBG_ELFDWARF
))
254 return (S_VariablesStruct
*)DWARFManager_GetInfosVariable(Adr
, Index
);
263 // Get global variable's Address based on his Name
264 // Return found Address
265 // Return NULL if no Address has been found
266 size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName
)
268 if ((DBGType
& DBG_ELFDWARF
))
270 return DWARFManager_GetGlobalVariableAdrFromName(VariableName
);
280 // Get number of local variables
281 // Return 0 if none has been found
282 size_t DBGManager_GetNbLocalVariables(size_t Adr
)
284 if ((DBGType
& DBG_ELFDWARF
))
286 return DWARFManager_GetNbLocalVariables(Adr
);
295 // Get number of global variables
296 // Return 0 if none has been found
297 size_t DBGManager_GetNbGlobalVariables(void)
299 if ((DBGType
& DBG_ELFDWARF
))
301 return DWARFManager_GetNbGlobalVariables();
311 // Get address from symbol name
312 // Return found address
313 // Return NULL if no symbol has been found
314 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName
)
318 if ((DBGType
& DBG_ELF
))
320 return ELFManager_GetAdrFromSymbolName(SymbolName
);
329 // Get local variable's type encoding based on his address and Index
330 // Return the type encoding found
331 // Return 0 if no type encoding has been found
332 size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
334 if ((DBGType
& DBG_ELFDWARF
))
336 return DWARFManager_GetLocalVariableTypeEncoding(Adr
, Index
);
346 int DBGManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
348 if ((DBGType
& DBG_ELFDWARF
))
350 return DWARFManager_GetLocalVariableOffset(Adr
, Index
);
359 // Get local variable's type byte size based on his address and Index
360 // Return the type's byte size found
361 // Return 0 if no type's byte size has been found
362 size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr
, size_t Index
)
364 if ((DBGType
& DBG_ELFDWARF
))
366 return DWARFManager_GetLocalVariableTypeByteSize(Adr
, Index
);
376 size_t DBGManager_GetLocalVariableTypeTag(size_t Adr
, size_t Index
)
378 if ((DBGType
& DBG_ELFDWARF
))
380 return DWARFManager_GetLocalVariableTypeTag(Adr
, Index
);
390 size_t DBGManager_GetGlobalVariableTypeTag(size_t Index
)
392 if ((DBGType
& DBG_ELFDWARF
))
394 return DWARFManager_GetGlobalVariableTypeTag(Index
);
403 // Get global variable's type name based on his Index
404 // Return type name's text pointer found
405 // Return NULL if no type name has been found
406 char *DBGManager_GetGlobalVariableTypeName(size_t Index
)
408 if ((DBGType
& DBG_ELFDWARF
))
410 return DWARFManager_GetGlobalVariableTypeName(Index
);
419 // Get global variable's Address based on his Index
420 // Return the Address found
421 // Return 0 if no Address has been found
422 size_t DBGManager_GetGlobalVariableAdr(size_t Index
)
424 if ((DBGType
& DBG_ELFDWARF
))
426 return DWARFManager_GetGlobalVariableAdr(Index
);
435 // Get global variable's type byte size based on his Index
436 // Return the type's byte size found
437 // Return 0 if no type's byte size has been found
438 size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index
)
440 if ((DBGType
& DBG_ELFDWARF
))
442 return DWARFManager_GetGlobalVariableTypeByteSize(Index
);
451 // Get global variable's type encoding based on his Index
452 // Return the type encoding found
453 // Return 0 if no type encoding has been found
454 size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index
)
456 if ((DBGType
& DBG_ELFDWARF
))
458 return DWARFManager_GetGlobalVariableTypeEncoding(Index
);
467 // Get global variable value based on his Index
468 // Return value as a text pointer
469 // Note: Pointer may point on a 0 lenght text
470 char *DBGManager_GetGlobalVariableValue(size_t Index
)
473 size_t TypeEncoding
= DBG_NO_TYPEENCODING
;
474 size_t TypeByteSize
= 0;
476 if ((DBGType
& DBG_ELFDWARF
))
478 Adr
= DWARFManager_GetGlobalVariableAdr(Index
);
479 TypeEncoding
= DWARFManager_GetGlobalVariableTypeEncoding(Index
);
480 TypeByteSize
= DWARFManager_GetGlobalVariableTypeByteSize(Index
);
483 return DBGManager_GetVariableValueFromAdr(Adr
, TypeEncoding
, TypeByteSize
);
487 // Get local variable's type name based on his Index
488 // Return type name's text pointer found
489 // Return NULL if no type name has been found
490 char *DBGManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
492 if ((DBGType
& DBG_ELFDWARF
))
494 return DWARFManager_GetLocalVariableTypeName(Adr
, Index
);
503 // Get local variable Op based on his Index
504 // Return variable Op's found
505 // Return 0 if no variable Op has been found
506 size_t DBGManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
508 if ((DBGType
& DBG_ELFDWARF
))
510 return DWARFManager_GetLocalVariableOp(Adr
, Index
);
519 // Get local variable name based on his Index
520 // Return variable name's text pointer found
521 // Return NULL if no variable name has been found
522 char *DBGManager_GetLocalVariableName(size_t Adr
, size_t Index
)
524 if ((DBGType
& DBG_ELFDWARF
))
526 return DWARFManager_GetLocalVariableName(Adr
, Index
);
535 // Get global variable name based on his Index
536 // Return variable name's text pointer found
537 // Return NULL if no variable name has been found
538 char *DBGManager_GetGlobalVariableName(size_t Index
)
540 if ((DBGType
& DBG_ELFDWARF
))
542 return DWARFManager_GetGlobalVariableName(Index
);
552 // Get variable value based on his Adresse, Encoding Type and Size
553 // Return value as a text pointer
554 // Note: Pointer may point on a 0 length text
555 char *DBGManager_GetVariableValueFromAdr(size_t Adr
, size_t TypeEncoding
, size_t TypeByteSize
)
558 char *Ptrvalue
= value
;
566 memset(&V
, 0, sizeof(Value
));
568 for (uint32_t i
= 0; i
< TypeByteSize
; i
++)
569 jaguarMainRAM
[Adr
+ i
] = 0;
570 //jaguarMainRAM[Adr + i] = rand();
571 jaguarMainRAM
[Adr
+ TypeByteSize
- 1] = 0x10;
573 for (size_t i
= 0, j
= TypeByteSize
; i
< TypeByteSize
; i
++, j
--)
575 V
.Ct
[i
] = jaguarMainRAM
[Adr
+ j
- 1];
578 switch (TypeEncoding
)
580 case DBG_ATE_address
:
583 case DBG_ATE_boolean
:
584 sprintf(value
, "%s", V
.B
? "true" : "false");
587 case DBG_ATE_complex_float
:
591 switch (TypeByteSize
)
594 sprintf(value
, "%F", V
.F
);
598 //V.D = (double)jaguarMainRAM[Adr];
599 //sprintf(value, "%10.10F", V.D);
600 sprintf(value
, "%F", V
.D
);
609 switch (TypeByteSize
)
612 sprintf(value
, "%i", V
.SS
);
616 sprintf(value
, "%i", V
.SI
);
620 sprintf(value
, "%i", V
.SL
);
628 case DBG_ATE_signed_char
:
631 case DBG_ATE_unsigned
:
632 switch (TypeByteSize
)
635 sprintf(value
, "%u", V
.US
);
639 sprintf(value
, "%u", V
.UI
);
643 sprintf(value
, "%u", V
.UL
);
651 case DBG_ATE_unsigned_char
:
652 sprintf(value
, "%u", (unsigned int) V
.C
);
656 switch (TypeByteSize
)
659 sprintf(value
, "0x%06x", V
.UI
);
675 // Get function name from address
676 // Return function name found
677 // Return NULL if no function name has been found
678 char *DBGManager_GetFunctionName(size_t Adr
)
680 char *Symbolname
= NULL
;
682 if ((DBGType
& DBG_ELFDWARF
))
684 Symbolname
= DWARFManager_GetFunctionName(Adr
);
687 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
689 Symbolname
= ELFManager_GetFunctionName(Adr
);
696 // Get line number from address and his tag
697 // Return line number on the symbol name found
698 // Return 0 if no symbol name has been found
699 size_t DBGManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
701 if ((DBGType
& DBG_ELFDWARF
))
703 return DWARFManager_GetNumLineFromAdr(Adr
, Tag
);
712 // Get symbol name from address
713 // Return text pointer on the symbol name found
714 // Return NULL if no symbol name has been found
715 char *DBGManager_GetSymbolNameFromAdr(size_t Adr
)
719 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
720 if (vjs
.displayHWlabels
)
722 Symbolname
= HWLABELManager_GetSymbolnameFromAdr(Adr
);
729 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
731 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
734 if (Symbolname
== NULL
)
736 if ((DBGType
& DBG_ELFDWARF
))
738 Symbolname
= DWARFManager_GetSymbolnameFromAdr(Adr
);
741 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
743 Symbolname
= ELFManager_GetSymbolnameFromAdr(Adr
);
751 // Get source line based on the Address and his Tag
752 // Return text pointer on the source line found
753 // Return NULL if no source line has been found
754 char *DBGManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
756 char *TextLine
= NULL
;
758 if ((DBGType
& DBG_ELFDWARF
))
760 TextLine
= DWARFManager_GetLineSrcFromAdr(Adr
, Tag
);
767 // Get text line from source based on address and num line (starting from 1)
768 // Return NULL if no text line has been found
769 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
771 char *TextLine
= NULL
;
773 if ((DBGType
& DBG_ELFDWARF
))
775 TextLine
= DWARFManager_GetLineSrcFromAdrNumLine(Adr
, NumLine
);
782 // Get text line from source based on address and num line (starting from 1)
783 // Return NULL if no text line has been found
784 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
786 char *TextLine
= NULL
;
788 if ((DBGType
& DBG_ELFDWARF
))
790 TextLine
= DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr
, NumLine
);
797 // Get number of source code filenames
798 size_t DBGManager_GetNbSources(void)
802 if ((DBGType
& DBG_ELFDWARF
))
804 Nbr
= DWARFManager_GetNbSources();
811 // Get source code filename based on index
812 char *DBGManager_GetNumSourceFilename(size_t Index
)
814 char *SourceFilename
= NULL
;
816 if ((DBGType
& DBG_ELFDWARF
))
818 SourceFilename
= DWARFManager_GetNumSourceFilename(Index
);
821 return SourceFilename
;
825 // Get source code filename based on index
826 char *DBGManager_GetNumFullSourceFilename(size_t Index
)
828 char *FullSourceFilename
= NULL
;
830 if ((DBGType
& DBG_ELFDWARF
))
832 FullSourceFilename
= DWARFManager_GetNumFullSourceFilename(Index
);
835 return FullSourceFilename
;
839 // Get number of lines of texts source list from source index
840 size_t DBGManager_GetSrcNbListPtrFromIndex(size_t Index
, bool Used
)
842 size_t NbListPtr
= 0;
844 if ((DBGType
& DBG_ELFDWARF
))
846 NbListPtr
= DWARFManager_GetSrcNbListPtrFromIndex(Index
, Used
);
853 // Get pointer to the lines number list from source index
854 size_t *DBGManager_GetSrcNumLinesPtrFromIndex(size_t Index
, bool Used
)
856 size_t *PtrNumLines
= NULL
;
858 if ((DBGType
& DBG_ELFDWARF
))
860 PtrNumLines
= DWARFManager_GetSrcNumLinesPtrFromIndex(Index
, Used
);
867 // Get text source list pointers from source index
868 char **DBGManager_GetSrcListPtrFromIndex(size_t Index
, bool Used
)
870 char **PtrSource
= NULL
;
872 if ((DBGType
& DBG_ELFDWARF
))
874 PtrSource
= DWARFManager_GetSrcListPtrFromIndex(Index
, Used
);
881 // Get source language
882 size_t DBGManager_GetSrcLanguageFromIndex(size_t Index
)
886 if ((DBGType
& DBG_ELFDWARF
))
888 Language
= DWARFManager_GetSrcLanguageFromIndex(Index
);