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
22 // To think about unique format to handle variations from ELF, DWARF, etc.
32 #include "ELFManager.h"
33 #include "DWARFManager.h"
34 #include "DBGManager.h"
35 #include "HWLABELManager.h"
61 void DBGManager_SourceFileSearchPathsInit(void);
62 void DBGManager_SourceFileSearchPathsReset(void);
63 void DBGManager_SourceFileSearchPathsClose(void);
66 // Common debugger variables
69 size_t NbSFSearchPaths
;
70 char **SourceFileSearchPaths
;
73 // Init the source file search paths
74 void DBGManager_SourceFileSearchPathsInit(void)
77 SourceFileSearchPaths
= NULL
;
81 // Set the source file search paths
82 // Create individual path for each one provided in the list (separate with ';')
83 void DBGManager_SourceFileSearchPathsSet(char *ListPaths
)
85 // Check presence of a previous list
88 // Reset previous list
89 DBGManager_SourceFileSearchPathsReset();
92 // Check if there is a paths list
93 if (strlen(ListPaths
))
95 // Get number of paths
96 char *Ptr
= ListPaths
;
99 while (*Ptr
&& (*Ptr
++ != ';'));
105 // Isolate each search path
106 SourceFileSearchPaths
= (char **)calloc(NbSFSearchPaths
, sizeof(char *));
112 // Search the path separator (';')
114 while (*Ptr
&& (*Ptr
++ != ';'));
116 // Copy the inidividual search path
117 SourceFileSearchPaths
[i
] = (char *)calloc(1, (Ptr
- Ptr1
) + 1);
118 strncpy(SourceFileSearchPaths
[i
], Ptr1
, (Ptr
- Ptr1
));
119 if (SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] == ';')
121 SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] = 0;
127 DWARFManager_Set(NbSFSearchPaths
, SourceFileSearchPaths
);
131 // Reset the source file search paths
132 void DBGManager_SourceFileSearchPathsReset(void)
135 while (NbSFSearchPaths
)
137 free(SourceFileSearchPaths
[--NbSFSearchPaths
]);
140 // Free the pointers list
141 free(SourceFileSearchPaths
);
142 SourceFileSearchPaths
= NULL
;
146 // Close the source file search paths
147 void DBGManager_SourceFileSearchPathsClose(void)
149 DBGManager_SourceFileSearchPathsReset();
153 // Common debugger initialisation
154 void DBGManager_Init(void)
156 // DBG initialisations
157 DBGType
= DBG_NO_TYPE
;
158 DBGManager_SourceFileSearchPathsInit();
160 // ELF initialisation
162 // DWARF initialisation
167 // Common debugger reset
168 void DBGManager_Reset(void)
170 if ((DBGType
& DBG_DWARF
))
172 DWARFManager_Reset();
175 if ((DBGType
& DBG_ELF
))
180 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
181 DBGType
= DBG_NO_TYPE
;
185 // Common debugger close
186 void DBGManager_Close(void)
188 if ((DBGType
& DBG_DWARF
))
190 DWARFManager_Close();
193 if ((DBGType
& DBG_ELF
))
198 DBGManager_SourceFileSearchPathsClose();
199 DBGType
= DBG_NO_TYPE
;
203 // Common debugger set
204 void DBGManager_SetType(size_t DBGTypeSet
)
206 DBGType
|= DBGTypeSet
;
211 size_t DBGManager_GetType(void)
217 // Get source filename based on the memeory address
218 // return NULL if no source filename
219 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr
, DBGstatus
*Status
)
221 if ((DBGType
& DBG_ELFDWARF
))
223 return DWARFManager_GetFullSourceFilenameFromAdr(Adr
, (DWARFstatus
*)Status
);
232 // Get number of local variables
233 // Return 0 if none has been found
234 size_t DBGManager_GetNbLocalVariables(size_t Adr
)
236 if ((DBGType
& DBG_ELFDWARF
))
238 return DWARFManager_GetNbLocalVariables(Adr
);
247 // Get number of global variables
248 // Return 0 if none has been found
249 size_t DBGManager_GetNbGlobalVariables(void)
251 if ((DBGType
& DBG_ELFDWARF
))
253 return DWARFManager_GetNbGlobalVariables();
262 // Get address from symbol name
263 // Return found address
264 // Return NULL if no symbol has been found
265 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName
)
269 if ((DBGType
& DBG_ELF
))
271 return ELFManager_GetAdrFromSymbolName(SymbolName
);
279 // Get global variable's Address based on his Name
280 // Return found Address
281 // Return NULL if no Address has been found
282 size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName
)
284 if ((DBGType
& DBG_ELFDWARF
))
286 return DWARFManager_GetGlobalVariableAdrFromName(VariableName
);
295 // Get local variable's type encoding based on his address and Index
296 // Return the type encoding found
297 // Return 0 if no type encoding has been found
298 size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
300 if ((DBGType
& DBG_ELFDWARF
))
302 return DWARFManager_GetLocalVariableTypeEncoding(Adr
, Index
);
312 int DBGManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
314 if ((DBGType
& DBG_ELFDWARF
))
316 return DWARFManager_GetLocalVariableOffset(Adr
, Index
);
325 // Get local variable's type byte size based on his address and Index
326 // Return the type's byte size found
327 // Return 0 if no type's byte size has been found
328 size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr
, size_t Index
)
330 if ((DBGType
& DBG_ELFDWARF
))
332 return DWARFManager_GetLocalVariableTypeByteSize(Adr
, Index
);
342 size_t DBGManager_GetLocalVariableTypeTag(size_t Adr
, size_t Index
)
344 if ((DBGType
& DBG_ELFDWARF
))
346 return DWARFManager_GetLocalVariableTypeTag(Adr
, Index
);
356 size_t DBGManager_GetGlobalVariableTypeTag(size_t Index
)
358 if ((DBGType
& DBG_ELFDWARF
))
360 return DWARFManager_GetGlobalVariableTypeTag(Index
);
369 // Get global variable's type name based on his Index
370 // Return type name's text pointer found
371 // Return NULL if no type name has been found
372 char *DBGManager_GetGlobalVariableTypeName(size_t Index
)
374 if ((DBGType
& DBG_ELFDWARF
))
376 return DWARFManager_GetGlobalVariableTypeName(Index
);
385 // Get global variable's Address based on his Index
386 // Return the Address found
387 // Return 0 if no Address has been found
388 size_t DBGManager_GetGlobalVariableAdr(size_t Index
)
390 if ((DBGType
& DBG_ELFDWARF
))
392 return DWARFManager_GetGlobalVariableAdr(Index
);
401 // Get global variable's type byte size based on his Index
402 // Return the type's byte size found
403 // Return 0 if no type's byte size has been found
404 size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index
)
406 if ((DBGType
& DBG_ELFDWARF
))
408 return DWARFManager_GetGlobalVariableTypeByteSize(Index
);
417 // Get global variable's type encoding based on his Index
418 // Return the type encoding found
419 // Return 0 if no type encoding has been found
420 size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index
)
422 if ((DBGType
& DBG_ELFDWARF
))
424 return DWARFManager_GetGlobalVariableTypeEncoding(Index
);
433 // Get global variable value based on his Index
434 // Return value as a text pointer
435 // Note: Pointer may point on a 0 lenght text
436 char *DBGManager_GetGlobalVariableValue(size_t Index
)
439 size_t TypeEncoding
= DBG_NO_TYPEENCODING
;
440 size_t TypeByteSize
= 0;
442 if ((DBGType
& DBG_ELFDWARF
))
444 Adr
= DWARFManager_GetGlobalVariableAdr(Index
);
445 TypeEncoding
= DWARFManager_GetGlobalVariableTypeEncoding(Index
);
446 TypeByteSize
= DWARFManager_GetGlobalVariableTypeByteSize(Index
);
449 return DBGManager_GetVariableValueFromAdr(Adr
, TypeEncoding
, TypeByteSize
);
453 // Get variable value based on his Adresse, Encoding Type and Size
454 // Return value as a text pointer
455 // Note: Pointer may point on a 0 length text
456 char *DBGManager_GetVariableValueFromAdr(size_t Adr
, size_t TypeEncoding
, size_t TypeByteSize
)
459 char *Ptrvalue
= value
;
467 memset(&V
, 0, sizeof(Value
));
469 for (uint32_t i
= 0; i
< TypeByteSize
; i
++)
470 jaguarMainRAM
[Adr
+ i
] = 0;
471 //jaguarMainRAM[Adr + i] = rand();
472 jaguarMainRAM
[Adr
+ TypeByteSize
- 1] = 0x10;
474 for (size_t i
= 0, j
= TypeByteSize
; i
< TypeByteSize
; i
++, j
--)
476 V
.Ct
[i
] = jaguarMainRAM
[Adr
+ j
- 1];
479 switch (TypeEncoding
)
481 case DBG_ATE_address
:
484 case DBG_ATE_boolean
:
485 sprintf(value
, "%s", V
.B
? "true" : "false");
488 case DBG_ATE_complex_float
:
492 switch (TypeByteSize
)
495 sprintf(value
, "%F", V
.F
);
499 //V.D = (double)jaguarMainRAM[Adr];
500 //sprintf(value, "%10.10F", V.D);
501 sprintf(value
, "%F", V
.D
);
510 switch (TypeByteSize
)
513 sprintf(value
, "%i", V
.SS
);
517 sprintf(value
, "%i", V
.SI
);
521 sprintf(value
, "%i", V
.SL
);
529 case DBG_ATE_signed_char
:
532 case DBG_ATE_unsigned
:
533 switch (TypeByteSize
)
536 sprintf(value
, "%u", V
.US
);
540 sprintf(value
, "%u", V
.UI
);
544 sprintf(value
, "%u", V
.UL
);
552 case DBG_ATE_unsigned_char
:
553 sprintf(value
, "%u", (unsigned int) V
.C
);
557 switch (TypeByteSize
)
560 sprintf(value
, "0x%06x", V
.UI
);
576 // Get local variable's type name based on his Index
577 // Return type name's text pointer found
578 // Return NULL if no type name has been found
579 char *DBGManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
581 if ((DBGType
& DBG_ELFDWARF
))
583 return DWARFManager_GetLocalVariableTypeName(Adr
, Index
);
592 // Get local variable Op based on his Index
593 // Return variable Op's found
594 // Return 0 if no variable Op has been found
595 size_t DBGManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
597 if ((DBGType
& DBG_ELFDWARF
))
599 return DWARFManager_GetLocalVariableOp(Adr
, Index
);
608 // Get local variable name based on his Index
609 // Return variable name's text pointer found
610 // Return NULL if no variable name has been found
611 char *DBGManager_GetLocalVariableName(size_t Adr
, size_t Index
)
613 if ((DBGType
& DBG_ELFDWARF
))
615 return DWARFManager_GetLocalVariableName(Adr
, Index
);
624 // Get global variable name based on his Index
625 // Return variable name's text pointer found
626 // Return NULL if no variable name has been found
627 char *DBGManager_GetGlobalVariableName(size_t Index
)
629 if ((DBGType
& DBG_ELFDWARF
))
631 return DWARFManager_GetGlobalVariableName(Index
);
640 // Get function name from address
641 // Return function name found
642 // Return NULL if no function name has been found
643 char *DBGManager_GetFunctionName(size_t Adr
)
645 char *Symbolname
= NULL
;
647 if ((DBGType
& DBG_ELFDWARF
))
649 Symbolname
= DWARFManager_GetFunctionName(Adr
);
652 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
654 Symbolname
= ELFManager_GetFunctionName(Adr
);
661 // Get line number from address and his tag
662 // Return line number on the symbol name found
663 // Return 0 if no symbol name has been found
664 size_t DBGManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
666 if ((DBGType
& DBG_ELFDWARF
))
668 return DWARFManager_GetNumLineFromAdr(Adr
, Tag
);
677 // Get symbol name from address
678 // Return text pointer on the symbol name found
679 // Return NULL if no symbol name has been found
680 char *DBGManager_GetSymbolNameFromAdr(size_t Adr
)
684 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
685 if (vjs
.displayHWlabels
)
687 Symbolname
= HWLABELManager_GetSymbolnameFromAdr(Adr
);
694 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
696 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
699 if (Symbolname
== NULL
)
701 if ((DBGType
& DBG_ELFDWARF
))
703 Symbolname
= DWARFManager_GetSymbolnameFromAdr(Adr
);
706 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
708 Symbolname
= ELFManager_GetSymbolnameFromAdr(Adr
);
716 // Get source line based on the Address and his Tag
717 // Return text pointer on the source line found
718 // Return NULL if no source line has been found
719 char *DBGManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
721 char *TextLine
= NULL
;
723 if ((DBGType
& DBG_ELFDWARF
))
725 TextLine
= DWARFManager_GetLineSrcFromAdr(Adr
, Tag
);
732 // Get text line from source based on address and num line (starting from 1)
733 // Return NULL if no text line has been found
734 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
736 char *TextLine
= NULL
;
738 if ((DBGType
& DBG_ELFDWARF
))
740 TextLine
= DWARFManager_GetLineSrcFromAdrNumLine(Adr
, NumLine
);
747 // Get text line from source based on address and num line (starting from 1)
748 // Return NULL if no text line has been found
749 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
751 char *TextLine
= NULL
;
753 if ((DBGType
& DBG_ELFDWARF
))
755 TextLine
= DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr
, NumLine
);
762 // Get number of source code filenames
763 size_t DBGManager_GetNbSources(void)
767 if ((DBGType
& DBG_ELFDWARF
))
769 Nbr
= DWARFManager_GetNbSources();
776 // Get source code filename based on index
777 char *DBGManager_GetNumSourceFilename(size_t Index
)
779 char *SourceFilename
= NULL
;
781 if ((DBGType
& DBG_ELFDWARF
))
783 SourceFilename
= DWARFManager_GetNumSourceFilename(Index
);
786 return SourceFilename
;
790 // Get source code filename based on index
791 char *DBGManager_GetNumFullSourceFilename(size_t Index
)
793 char *FullSourceFilename
= NULL
;
795 if ((DBGType
& DBG_ELFDWARF
))
797 FullSourceFilename
= DWARFManager_GetNumFullSourceFilename(Index
);
800 return FullSourceFilename
;
804 // Get number of lines of texts source list from source index
805 size_t DBGManager_GetSrcNbListPtrFromIndex(size_t Index
, bool Used
)
807 size_t NbListPtr
= 0;
809 if ((DBGType
& DBG_ELFDWARF
))
811 NbListPtr
= DWARFManager_GetSrcNbListPtrFromIndex(Index
, Used
);
818 // Get pointer to the lines number list from source index
819 size_t *DBGManager_GetSrcNumLinesPtrFromIndex(size_t Index
, bool Used
)
821 size_t *PtrNumLines
= NULL
;
823 if ((DBGType
& DBG_ELFDWARF
))
825 PtrNumLines
= DWARFManager_GetSrcNumLinesPtrFromIndex(Index
, Used
);
832 // Get text source list pointers from source index
833 char **DBGManager_GetSrcListPtrFromIndex(size_t Index
, bool Used
)
835 char **PtrSource
= NULL
;
837 if ((DBGType
& DBG_ELFDWARF
))
839 PtrSource
= DWARFManager_GetSrcListPtrFromIndex(Index
, Used
);
846 // Get source language
847 size_t DBGManager_GetSrcLanguageFromIndex(size_t Index
)
851 if ((DBGType
& DBG_ELFDWARF
))
853 Language
= DWARFManager_GetSrcLanguageFromIndex(Index
);