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
15 // JPM Aug./2019 Added new functions mainly for source text lines
19 // To think about unique format to handle variations from ELF, DWARF, etc.
26 #include "libelf/libelf.h"
27 #include "libelf/gelf.h"
29 #include "ELFManager.h"
30 #include "DwarfManager.h"
31 #include "DBGManager.h"
32 #include "HWLABELManager.h"
56 void DBGManager_SourceFileSearchPathsInit(void);
57 void DBGManager_SourceFileSearchPathsReset(void);
58 void DBGManager_SourceFileSearchPathsClose(void);
61 // Common debugger variables
64 size_t NbSFSearchPaths
;
65 char **SourceFileSearchPaths
;
68 // Init the source file search paths
69 void DBGManager_SourceFileSearchPathsInit(void)
72 SourceFileSearchPaths
= NULL
;
76 // Set the source file search paths
77 // Create individual path for each one provided in the list (separate with ';')
78 void DBGManager_SourceFileSearchPathsSet(char *ListPaths
)
80 // Check presence of a previous list
83 // Reset previous list
84 DBGManager_SourceFileSearchPathsReset();
87 // Check if there is a paths list
88 if (strlen(ListPaths
))
90 // Get number of paths
91 char *Ptr
= ListPaths
;
94 while (*Ptr
&& (*Ptr
++ != ';'));
100 // Isolate each search path
101 SourceFileSearchPaths
= (char **)calloc(NbSFSearchPaths
, sizeof(char *));
107 // Search the path separator (';')
109 while (*Ptr
&& (*Ptr
++ != ';'));
111 // Copy the inidividual search path
112 SourceFileSearchPaths
[i
] = (char *)calloc(1, (Ptr
- Ptr1
) + 1);
113 strncpy(SourceFileSearchPaths
[i
], Ptr1
, (Ptr
- Ptr1
));
114 if (SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] == ';')
116 SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] = 0;
122 DWARFManager_Set(NbSFSearchPaths
, SourceFileSearchPaths
);
126 // Reset the source file search paths
127 void DBGManager_SourceFileSearchPathsReset(void)
130 while (NbSFSearchPaths
)
132 free(SourceFileSearchPaths
[--NbSFSearchPaths
]);
135 // Free the pointers list
136 free(SourceFileSearchPaths
);
137 SourceFileSearchPaths
= NULL
;
141 // Close the source file search paths
142 void DBGManager_SourceFileSearchPathsClose(void)
144 DBGManager_SourceFileSearchPathsReset();
148 // Common debugger initialisation
149 void DBGManager_Init(void)
151 // DBG initialisations
152 DBGType
= DBG_NO_TYPE
;
153 DBGManager_SourceFileSearchPathsInit();
155 // ELF initialisation
157 // DWARF initialisation
162 // Common debugger reset
163 void DBGManager_Reset(void)
165 if ((DBGType
& DBG_DWARF
))
167 DWARFManager_Reset();
170 if ((DBGType
& DBG_ELF
))
175 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
176 DBGType
= DBG_NO_TYPE
;
180 // Common debugger close
181 void DBGManager_Close(void)
183 if ((DBGType
& DBG_DWARF
))
185 DWARFManager_Close();
188 if ((DBGType
& DBG_ELF
))
193 DBGManager_SourceFileSearchPathsClose();
194 DBGType
= DBG_NO_TYPE
;
198 // Common debugger set
199 void DBGManager_SetType(size_t DBGTypeSet
)
201 DBGType
|= DBGTypeSet
;
206 size_t DBGManager_GetType(void)
212 // Get source filename based on the memeory address
213 // return NULL if no source filename
214 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr
, bool *Error
)
216 if ((DBGType
& DBG_ELFDWARF
))
218 return DWARFManager_GetFullSourceFilenameFromAdr(Adr
, Error
);
227 // Get number of local variables
228 // Return 0 if none has been found
229 size_t DBGManager_GetNbLocalVariables(size_t Adr
)
231 if ((DBGType
& DBG_ELFDWARF
))
233 return DWARFManager_GetNbLocalVariables(Adr
);
242 // Get number of global variables
243 // Return 0 if none has been found
244 size_t DBGManager_GetNbGlobalVariables(void)
246 if ((DBGType
& DBG_ELFDWARF
))
248 return DWARFManager_GetNbGlobalVariables();
257 // Get address from symbol name
258 // Return found address
259 // Return NULL if no symbol has been found
260 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName
)
264 if ((DBGType
& DBG_ELF
))
266 return ELFManager_GetAdrFromSymbolName(SymbolName
);
274 // Get global variable's Address based on his Name
275 // Return found Address
276 // Return NULL if no Address has been found
277 size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName
)
279 if ((DBGType
& DBG_ELFDWARF
))
281 return DWARFManager_GetGlobalVariableAdrFromName(VariableName
);
290 // Get local variable's type encoding based on his address and Index
291 // Return the type encoding found
292 // Return 0 if no type encoding has been found
293 size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
295 if ((DBGType
& DBG_ELFDWARF
))
297 return DWARFManager_GetLocalVariableTypeEncoding(Adr
, Index
);
307 int DBGManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
309 if ((DBGType
& DBG_ELFDWARF
))
311 return DWARFManager_GetLocalVariableOffset(Adr
, Index
);
320 // Get local variable's type byte size based on his address and Index
321 // Return the type's byte size found
322 // Return 0 if no type's byte size has been found
323 size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr
, size_t Index
)
325 if ((DBGType
& DBG_ELFDWARF
))
327 return DWARFManager_GetLocalVariableTypeByteSize(Adr
, Index
);
337 size_t DBGManager_GetLocalVariableTypeTag(size_t Adr
, size_t Index
)
339 if ((DBGType
& DBG_ELFDWARF
))
341 return DWARFManager_GetLocalVariableTypeTag(Adr
, Index
);
351 size_t DBGManager_GetGlobalVariableTypeTag(size_t Index
)
353 if ((DBGType
& DBG_ELFDWARF
))
355 return DWARFManager_GetGlobalVariableTypeTag(Index
);
364 // Get global variable's type name based on his Index
365 // Return type name's text pointer found
366 // Return NULL if no type name has been found
367 char *DBGManager_GetGlobalVariableTypeName(size_t Index
)
369 if ((DBGType
& DBG_ELFDWARF
))
371 return DWARFManager_GetGlobalVariableTypeName(Index
);
380 // Get global variable's Address based on his Index
381 // Return the Address found
382 // Return 0 if no Address has been found
383 size_t DBGManager_GetGlobalVariableAdr(size_t Index
)
385 if ((DBGType
& DBG_ELFDWARF
))
387 return DWARFManager_GetGlobalVariableAdr(Index
);
396 // Get global variable's type byte size based on his Index
397 // Return the type's byte size found
398 // Return 0 if no type's byte size has been found
399 size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index
)
401 if ((DBGType
& DBG_ELFDWARF
))
403 return DWARFManager_GetGlobalVariableTypeByteSize(Index
);
412 // Get global variable's type encoding based on his Index
413 // Return the type encoding found
414 // Return 0 if no type encoding has been found
415 size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index
)
417 if ((DBGType
& DBG_ELFDWARF
))
419 return DWARFManager_GetGlobalVariableTypeEncoding(Index
);
428 // Get global variable value based on his Index
429 // Return value as a text pointer
430 // Note: Pointer may point on a 0 lenght text
431 char *DBGManager_GetGlobalVariableValue(size_t Index
)
434 size_t TypeEncoding
= DBG_NO_TYPEENCODING
;
435 size_t TypeByteSize
= 0;
437 if ((DBGType
& DBG_ELFDWARF
))
439 Adr
= DWARFManager_GetGlobalVariableAdr(Index
);
440 TypeEncoding
= DWARFManager_GetGlobalVariableTypeEncoding(Index
);
441 TypeByteSize
= DWARFManager_GetGlobalVariableTypeByteSize(Index
);
444 return DBGManager_GetVariableValueFromAdr(Adr
, TypeEncoding
, TypeByteSize
);
448 // Get variable value based on his Adresse, Encoding Type and Size
449 // Return value as a text pointer
450 // Note: Pointer may point on a 0 length text
451 char *DBGManager_GetVariableValueFromAdr(size_t Adr
, size_t TypeEncoding
, size_t TypeByteSize
)
454 char *Ptrvalue
= value
;
462 memset(&V
, 0, sizeof(Value
));
464 for (uint32_t i
= 0; i
< TypeByteSize
; i
++)
465 jaguarMainRAM
[Adr
+ i
] = 0;
466 //jaguarMainRAM[Adr + i] = rand();
467 jaguarMainRAM
[Adr
+ TypeByteSize
- 1] = 0x10;
469 for (size_t i
= 0, j
= TypeByteSize
; i
< TypeByteSize
; i
++, j
--)
471 V
.Ct
[i
] = jaguarMainRAM
[Adr
+ j
- 1];
474 switch (TypeEncoding
)
476 case DBG_ATE_address
:
479 case DBG_ATE_boolean
:
480 sprintf(value
, "%s", V
.B
? "true" : "false");
483 case DBG_ATE_complex_float
:
487 switch (TypeByteSize
)
490 sprintf(value
, "%F", V
.F
);
494 //V.D = (double)jaguarMainRAM[Adr];
495 //sprintf(value, "%10.10F", V.D);
496 sprintf(value
, "%F", V
.D
);
505 switch (TypeByteSize
)
508 sprintf(value
, "%i", V
.SI
);
512 sprintf(value
, "%i", V
.SL
);
520 case DBG_ATE_signed_char
:
523 case DBG_ATE_unsigned
:
524 switch (TypeByteSize
)
527 sprintf(value
, "%u", V
.UI
);
531 sprintf(value
, "%u", V
.UL
);
539 case DBG_ATE_unsigned_char
:
540 sprintf(value
, "%u", (unsigned int(V
.C
)));
544 switch (TypeByteSize
)
547 sprintf(value
, "0x%06x", V
.UI
);
563 // Get local variable's type name based on his Index
564 // Return type name's text pointer found
565 // Return NULL if no type name has been found
566 char *DBGManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
568 if ((DBGType
& DBG_ELFDWARF
))
570 return DWARFManager_GetLocalVariableTypeName(Adr
, Index
);
579 // Get local variable Op based on his Index
580 // Return variable Op's found
581 // Return 0 if no variable Op has been found
582 size_t DBGManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
584 if ((DBGType
& DBG_ELFDWARF
))
586 return DWARFManager_GetLocalVariableOp(Adr
, Index
);
595 // Get local variable name based on his Index
596 // Return variable name's text pointer found
597 // Return NULL if no variable name has been found
598 char *DBGManager_GetLocalVariableName(size_t Adr
, size_t Index
)
600 if ((DBGType
& DBG_ELFDWARF
))
602 return DWARFManager_GetLocalVariableName(Adr
, Index
);
611 // Get global variable name based on his Index
612 // Return variable name's text pointer found
613 // Return NULL if no variable name has been found
614 char *DBGManager_GetGlobalVariableName(size_t Index
)
616 if ((DBGType
& DBG_ELFDWARF
))
618 return DWARFManager_GetGlobalVariableName(Index
);
627 // Get function name from address
628 // Return function name found
629 // Return NULL if no function name has been found
630 char *DBGManager_GetFunctionName(size_t Adr
)
632 char *Symbolname
= NULL
;
634 if ((DBGType
& DBG_ELFDWARF
))
636 Symbolname
= DWARFManager_GetFunctionName(Adr
);
639 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
641 Symbolname
= ELFManager_GetFunctionName(Adr
);
648 // Get line number from address and his tag
649 // Return line number on the symbol name found
650 // Return 0 if no symbol name has been found
651 size_t DBGManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
653 if ((DBGType
& DBG_ELFDWARF
))
655 return DWARFManager_GetNumLineFromAdr(Adr
, Tag
);
664 // Get symbol name from address
665 // Return text pointer on the symbol name found
666 // Return NULL if no symbol name has been found
667 char *DBGManager_GetSymbolNameFromAdr(size_t Adr
)
671 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
672 if (vjs
.displayHWlabels
)
674 Symbolname
= HWLABELManager_GetSymbolnameFromAdr(Adr
);
681 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
683 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
686 if (Symbolname
== NULL
)
688 if ((DBGType
& DBG_ELFDWARF
))
690 Symbolname
= DWARFManager_GetSymbolnameFromAdr(Adr
);
693 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
695 Symbolname
= ELFManager_GetSymbolnameFromAdr(Adr
);
703 // Get source line based on the Address and his Tag
704 // Return text pointer on the source line found
705 // Return NULL if no source line has been found
706 char *DBGManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
708 char *TextLine
= NULL
;
710 if ((DBGType
& DBG_ELFDWARF
))
712 TextLine
= DWARFManager_GetLineSrcFromAdr(Adr
, Tag
);
719 // Get text line from source based on address and num line (starting from 1)
720 // Return NULL if no text line has been found
721 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
723 char *TextLine
= NULL
;
725 if ((DBGType
& DBG_ELFDWARF
))
727 TextLine
= DWARFManager_GetLineSrcFromAdrNumLine(Adr
, NumLine
);
734 // Get text line from source based on address and num line (starting from 1)
735 // Return NULL if no text line has been found
736 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
738 char *TextLine
= NULL
;
740 if ((DBGType
& DBG_ELFDWARF
))
742 TextLine
= DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr
, NumLine
);
749 // Get number of source code filenames
750 size_t DBGManager_GetNbSources(void)
754 if ((DBGType
& DBG_ELFDWARF
))
756 Nbr
= DWARFManager_GetNbSources();
763 // Get source code filename based on index
764 char *DBGManager_GetNumSourceFilename(size_t Index
)
766 char *SourceFilename
= NULL
;
768 if ((DBGType
& DBG_ELFDWARF
))
770 SourceFilename
= DWARFManager_GetNumSourceFilename(Index
);
773 return SourceFilename
;
777 // Get source code filename based on index
778 char *DBGManager_GetNumFullSourceFilename(size_t Index
)
780 char *FullSourceFilename
= NULL
;
782 if ((DBGType
& DBG_ELFDWARF
))
784 FullSourceFilename
= DWARFManager_GetNumFullSourceFilename(Index
);
787 return FullSourceFilename
;
791 // Get number of lines of texts source list from source index
792 size_t DBGManager_GetSrcNbListPtrFromIndex(size_t Index
, bool Used
)
794 size_t NbListPtr
= 0;
796 if ((DBGType
& DBG_ELFDWARF
))
798 NbListPtr
= DWARFManager_GetSrcNbListPtrFromIndex(Index
, Used
);
805 // Get pointer to the lines number list from source index
806 size_t *DBGManager_GetSrcNumLinesPtrFromIndex(size_t Index
, bool Used
)
808 size_t *PtrNumLines
= NULL
;
810 if ((DBGType
& DBG_ELFDWARF
))
812 PtrNumLines
= DWARFManager_GetSrcNumLinesPtrFromIndex(Index
, Used
);
819 // Get text source list pointers from source index
820 char **DBGManager_GetSrcListPtrFromIndex(size_t Index
, bool Used
)
822 char **PtrSource
= NULL
;
824 if ((DBGType
& DBG_ELFDWARF
))
826 PtrSource
= DWARFManager_GetSrcListPtrFromIndex(Index
, Used
);
833 // Get source language
834 size_t DBGManager_GetSrcLanguageFromIndex(size_t Index
)
838 if ((DBGType
& DBG_ELFDWARF
))
840 Language
= DWARFManager_GetSrcLanguageFromIndex(Index
);