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
16 // JPM Sept./2019 Support the unsigned/signed short type
20 // To think about unique format to handle variations from ELF, DWARF, etc.
27 #include "libelf/libelf.h"
28 #include "libelf/gelf.h"
30 #include "ELFManager.h"
31 #include "DwarfManager.h"
32 #include "DBGManager.h"
33 #include "HWLABELManager.h"
59 void DBGManager_SourceFileSearchPathsInit(void);
60 void DBGManager_SourceFileSearchPathsReset(void);
61 void DBGManager_SourceFileSearchPathsClose(void);
64 // Common debugger variables
67 size_t NbSFSearchPaths
;
68 char **SourceFileSearchPaths
;
71 // Init the source file search paths
72 void DBGManager_SourceFileSearchPathsInit(void)
75 SourceFileSearchPaths
= NULL
;
79 // Set the source file search paths
80 // Create individual path for each one provided in the list (separate with ';')
81 void DBGManager_SourceFileSearchPathsSet(char *ListPaths
)
83 // Check presence of a previous list
86 // Reset previous list
87 DBGManager_SourceFileSearchPathsReset();
90 // Check if there is a paths list
91 if (strlen(ListPaths
))
93 // Get number of paths
94 char *Ptr
= ListPaths
;
97 while (*Ptr
&& (*Ptr
++ != ';'));
103 // Isolate each search path
104 SourceFileSearchPaths
= (char **)calloc(NbSFSearchPaths
, sizeof(char *));
110 // Search the path separator (';')
112 while (*Ptr
&& (*Ptr
++ != ';'));
114 // Copy the inidividual search path
115 SourceFileSearchPaths
[i
] = (char *)calloc(1, (Ptr
- Ptr1
) + 1);
116 strncpy(SourceFileSearchPaths
[i
], Ptr1
, (Ptr
- Ptr1
));
117 if (SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] == ';')
119 SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] = 0;
125 DWARFManager_Set(NbSFSearchPaths
, SourceFileSearchPaths
);
129 // Reset the source file search paths
130 void DBGManager_SourceFileSearchPathsReset(void)
133 while (NbSFSearchPaths
)
135 free(SourceFileSearchPaths
[--NbSFSearchPaths
]);
138 // Free the pointers list
139 free(SourceFileSearchPaths
);
140 SourceFileSearchPaths
= NULL
;
144 // Close the source file search paths
145 void DBGManager_SourceFileSearchPathsClose(void)
147 DBGManager_SourceFileSearchPathsReset();
151 // Common debugger initialisation
152 void DBGManager_Init(void)
154 // DBG initialisations
155 DBGType
= DBG_NO_TYPE
;
156 DBGManager_SourceFileSearchPathsInit();
158 // ELF initialisation
160 // DWARF initialisation
165 // Common debugger reset
166 void DBGManager_Reset(void)
168 if ((DBGType
& DBG_DWARF
))
170 DWARFManager_Reset();
173 if ((DBGType
& DBG_ELF
))
178 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
179 DBGType
= DBG_NO_TYPE
;
183 // Common debugger close
184 void DBGManager_Close(void)
186 if ((DBGType
& DBG_DWARF
))
188 DWARFManager_Close();
191 if ((DBGType
& DBG_ELF
))
196 DBGManager_SourceFileSearchPathsClose();
197 DBGType
= DBG_NO_TYPE
;
201 // Common debugger set
202 void DBGManager_SetType(size_t DBGTypeSet
)
204 DBGType
|= DBGTypeSet
;
209 size_t DBGManager_GetType(void)
215 // Get source filename based on the memeory address
216 // return NULL if no source filename
217 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr
, bool *Error
)
219 if ((DBGType
& DBG_ELFDWARF
))
221 return DWARFManager_GetFullSourceFilenameFromAdr(Adr
, Error
);
230 // Get number of local variables
231 // Return 0 if none has been found
232 size_t DBGManager_GetNbLocalVariables(size_t Adr
)
234 if ((DBGType
& DBG_ELFDWARF
))
236 return DWARFManager_GetNbLocalVariables(Adr
);
245 // Get number of global variables
246 // Return 0 if none has been found
247 size_t DBGManager_GetNbGlobalVariables(void)
249 if ((DBGType
& DBG_ELFDWARF
))
251 return DWARFManager_GetNbGlobalVariables();
260 // Get address from symbol name
261 // Return found address
262 // Return NULL if no symbol has been found
263 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName
)
267 if ((DBGType
& DBG_ELF
))
269 return ELFManager_GetAdrFromSymbolName(SymbolName
);
277 // Get global variable's Address based on his Name
278 // Return found Address
279 // Return NULL if no Address has been found
280 size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName
)
282 if ((DBGType
& DBG_ELFDWARF
))
284 return DWARFManager_GetGlobalVariableAdrFromName(VariableName
);
293 // Get local variable's type encoding based on his address and Index
294 // Return the type encoding found
295 // Return 0 if no type encoding has been found
296 size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
298 if ((DBGType
& DBG_ELFDWARF
))
300 return DWARFManager_GetLocalVariableTypeEncoding(Adr
, Index
);
310 int DBGManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
312 if ((DBGType
& DBG_ELFDWARF
))
314 return DWARFManager_GetLocalVariableOffset(Adr
, Index
);
323 // Get local variable's type byte size based on his address and Index
324 // Return the type's byte size found
325 // Return 0 if no type's byte size has been found
326 size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr
, size_t Index
)
328 if ((DBGType
& DBG_ELFDWARF
))
330 return DWARFManager_GetLocalVariableTypeByteSize(Adr
, Index
);
340 size_t DBGManager_GetLocalVariableTypeTag(size_t Adr
, size_t Index
)
342 if ((DBGType
& DBG_ELFDWARF
))
344 return DWARFManager_GetLocalVariableTypeTag(Adr
, Index
);
354 size_t DBGManager_GetGlobalVariableTypeTag(size_t Index
)
356 if ((DBGType
& DBG_ELFDWARF
))
358 return DWARFManager_GetGlobalVariableTypeTag(Index
);
367 // Get global variable's type name based on his Index
368 // Return type name's text pointer found
369 // Return NULL if no type name has been found
370 char *DBGManager_GetGlobalVariableTypeName(size_t Index
)
372 if ((DBGType
& DBG_ELFDWARF
))
374 return DWARFManager_GetGlobalVariableTypeName(Index
);
383 // Get global variable's Address based on his Index
384 // Return the Address found
385 // Return 0 if no Address has been found
386 size_t DBGManager_GetGlobalVariableAdr(size_t Index
)
388 if ((DBGType
& DBG_ELFDWARF
))
390 return DWARFManager_GetGlobalVariableAdr(Index
);
399 // Get global variable's type byte size based on his Index
400 // Return the type's byte size found
401 // Return 0 if no type's byte size has been found
402 size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index
)
404 if ((DBGType
& DBG_ELFDWARF
))
406 return DWARFManager_GetGlobalVariableTypeByteSize(Index
);
415 // Get global variable's type encoding based on his Index
416 // Return the type encoding found
417 // Return 0 if no type encoding has been found
418 size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index
)
420 if ((DBGType
& DBG_ELFDWARF
))
422 return DWARFManager_GetGlobalVariableTypeEncoding(Index
);
431 // Get global variable value based on his Index
432 // Return value as a text pointer
433 // Note: Pointer may point on a 0 lenght text
434 char *DBGManager_GetGlobalVariableValue(size_t Index
)
437 size_t TypeEncoding
= DBG_NO_TYPEENCODING
;
438 size_t TypeByteSize
= 0;
440 if ((DBGType
& DBG_ELFDWARF
))
442 Adr
= DWARFManager_GetGlobalVariableAdr(Index
);
443 TypeEncoding
= DWARFManager_GetGlobalVariableTypeEncoding(Index
);
444 TypeByteSize
= DWARFManager_GetGlobalVariableTypeByteSize(Index
);
447 return DBGManager_GetVariableValueFromAdr(Adr
, TypeEncoding
, TypeByteSize
);
451 // Get variable value based on his Adresse, Encoding Type and Size
452 // Return value as a text pointer
453 // Note: Pointer may point on a 0 length text
454 char *DBGManager_GetVariableValueFromAdr(size_t Adr
, size_t TypeEncoding
, size_t TypeByteSize
)
457 char *Ptrvalue
= value
;
465 memset(&V
, 0, sizeof(Value
));
467 for (uint32_t i
= 0; i
< TypeByteSize
; i
++)
468 jaguarMainRAM
[Adr
+ i
] = 0;
469 //jaguarMainRAM[Adr + i] = rand();
470 jaguarMainRAM
[Adr
+ TypeByteSize
- 1] = 0x10;
472 for (size_t i
= 0, j
= TypeByteSize
; i
< TypeByteSize
; i
++, j
--)
474 V
.Ct
[i
] = jaguarMainRAM
[Adr
+ j
- 1];
477 switch (TypeEncoding
)
479 case DBG_ATE_address
:
482 case DBG_ATE_boolean
:
483 sprintf(value
, "%s", V
.B
? "true" : "false");
486 case DBG_ATE_complex_float
:
490 switch (TypeByteSize
)
493 sprintf(value
, "%F", V
.F
);
497 //V.D = (double)jaguarMainRAM[Adr];
498 //sprintf(value, "%10.10F", V.D);
499 sprintf(value
, "%F", V
.D
);
508 switch (TypeByteSize
)
511 sprintf(value
, "%i", V
.SS
);
515 sprintf(value
, "%i", V
.SI
);
519 sprintf(value
, "%i", V
.SL
);
527 case DBG_ATE_signed_char
:
530 case DBG_ATE_unsigned
:
531 switch (TypeByteSize
)
534 sprintf(value
, "%u", V
.US
);
538 sprintf(value
, "%u", V
.UI
);
542 sprintf(value
, "%u", V
.UL
);
550 case DBG_ATE_unsigned_char
:
551 sprintf(value
, "%u", (unsigned int(V
.C
)));
555 switch (TypeByteSize
)
558 sprintf(value
, "0x%06x", V
.UI
);
574 // Get local variable's type name based on his Index
575 // Return type name's text pointer found
576 // Return NULL if no type name has been found
577 char *DBGManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
579 if ((DBGType
& DBG_ELFDWARF
))
581 return DWARFManager_GetLocalVariableTypeName(Adr
, Index
);
590 // Get local variable Op based on his Index
591 // Return variable Op's found
592 // Return 0 if no variable Op has been found
593 size_t DBGManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
595 if ((DBGType
& DBG_ELFDWARF
))
597 return DWARFManager_GetLocalVariableOp(Adr
, Index
);
606 // Get local variable name based on his Index
607 // Return variable name's text pointer found
608 // Return NULL if no variable name has been found
609 char *DBGManager_GetLocalVariableName(size_t Adr
, size_t Index
)
611 if ((DBGType
& DBG_ELFDWARF
))
613 return DWARFManager_GetLocalVariableName(Adr
, Index
);
622 // Get global variable name based on his Index
623 // Return variable name's text pointer found
624 // Return NULL if no variable name has been found
625 char *DBGManager_GetGlobalVariableName(size_t Index
)
627 if ((DBGType
& DBG_ELFDWARF
))
629 return DWARFManager_GetGlobalVariableName(Index
);
638 // Get function name from address
639 // Return function name found
640 // Return NULL if no function name has been found
641 char *DBGManager_GetFunctionName(size_t Adr
)
643 char *Symbolname
= NULL
;
645 if ((DBGType
& DBG_ELFDWARF
))
647 Symbolname
= DWARFManager_GetFunctionName(Adr
);
650 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
652 Symbolname
= ELFManager_GetFunctionName(Adr
);
659 // Get line number from address and his tag
660 // Return line number on the symbol name found
661 // Return 0 if no symbol name has been found
662 size_t DBGManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
664 if ((DBGType
& DBG_ELFDWARF
))
666 return DWARFManager_GetNumLineFromAdr(Adr
, Tag
);
675 // Get symbol name from address
676 // Return text pointer on the symbol name found
677 // Return NULL if no symbol name has been found
678 char *DBGManager_GetSymbolNameFromAdr(size_t Adr
)
682 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
683 if (vjs
.displayHWlabels
)
685 Symbolname
= HWLABELManager_GetSymbolnameFromAdr(Adr
);
692 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
694 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
697 if (Symbolname
== NULL
)
699 if ((DBGType
& DBG_ELFDWARF
))
701 Symbolname
= DWARFManager_GetSymbolnameFromAdr(Adr
);
704 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
706 Symbolname
= ELFManager_GetSymbolnameFromAdr(Adr
);
714 // Get source line based on the Address and his Tag
715 // Return text pointer on the source line found
716 // Return NULL if no source line has been found
717 char *DBGManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
719 char *TextLine
= NULL
;
721 if ((DBGType
& DBG_ELFDWARF
))
723 TextLine
= DWARFManager_GetLineSrcFromAdr(Adr
, Tag
);
730 // Get text line from source based on address and num line (starting from 1)
731 // Return NULL if no text line has been found
732 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
734 char *TextLine
= NULL
;
736 if ((DBGType
& DBG_ELFDWARF
))
738 TextLine
= DWARFManager_GetLineSrcFromAdrNumLine(Adr
, NumLine
);
745 // Get text line from source based on address and num line (starting from 1)
746 // Return NULL if no text line has been found
747 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
749 char *TextLine
= NULL
;
751 if ((DBGType
& DBG_ELFDWARF
))
753 TextLine
= DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr
, NumLine
);
760 // Get number of source code filenames
761 size_t DBGManager_GetNbSources(void)
765 if ((DBGType
& DBG_ELFDWARF
))
767 Nbr
= DWARFManager_GetNbSources();
774 // Get source code filename based on index
775 char *DBGManager_GetNumSourceFilename(size_t Index
)
777 char *SourceFilename
= NULL
;
779 if ((DBGType
& DBG_ELFDWARF
))
781 SourceFilename
= DWARFManager_GetNumSourceFilename(Index
);
784 return SourceFilename
;
788 // Get source code filename based on index
789 char *DBGManager_GetNumFullSourceFilename(size_t Index
)
791 char *FullSourceFilename
= NULL
;
793 if ((DBGType
& DBG_ELFDWARF
))
795 FullSourceFilename
= DWARFManager_GetNumFullSourceFilename(Index
);
798 return FullSourceFilename
;
802 // Get number of lines of texts source list from source index
803 size_t DBGManager_GetSrcNbListPtrFromIndex(size_t Index
, bool Used
)
805 size_t NbListPtr
= 0;
807 if ((DBGType
& DBG_ELFDWARF
))
809 NbListPtr
= DWARFManager_GetSrcNbListPtrFromIndex(Index
, Used
);
816 // Get pointer to the lines number list from source index
817 size_t *DBGManager_GetSrcNumLinesPtrFromIndex(size_t Index
, bool Used
)
819 size_t *PtrNumLines
= NULL
;
821 if ((DBGType
& DBG_ELFDWARF
))
823 PtrNumLines
= DWARFManager_GetSrcNumLinesPtrFromIndex(Index
, Used
);
830 // Get text source list pointers from source index
831 char **DBGManager_GetSrcListPtrFromIndex(size_t Index
, bool Used
)
833 char **PtrSource
= NULL
;
835 if ((DBGType
& DBG_ELFDWARF
))
837 PtrSource
= DWARFManager_GetSrcListPtrFromIndex(Index
, Used
);
844 // Get source language
845 size_t DBGManager_GetSrcLanguageFromIndex(size_t Index
)
849 if ((DBGType
& DBG_ELFDWARF
))
851 Language
= DWARFManager_GetSrcLanguageFromIndex(Index
);