1d024e34399fa765ad97d51852ee420ea361c26d
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 Sept./2019 Support the unsigned/signed short type
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"
58 void DBGManager_SourceFileSearchPathsInit(void);
59 void DBGManager_SourceFileSearchPathsReset(void);
60 void DBGManager_SourceFileSearchPathsClose(void);
63 // Common debugger variables
66 size_t NbSFSearchPaths
;
67 char **SourceFileSearchPaths
;
70 // Init the source file search paths
71 void DBGManager_SourceFileSearchPathsInit(void)
74 SourceFileSearchPaths
= NULL
;
78 // Set the source file search paths
79 // Create individual path for each one provided in the list (separate with ';')
80 void DBGManager_SourceFileSearchPathsSet(char *ListPaths
)
82 // Check presence of a previous list
85 // Reset previous list
86 DBGManager_SourceFileSearchPathsReset();
89 // Check if there is a paths list
90 if (strlen(ListPaths
))
92 // Get number of paths
93 char *Ptr
= ListPaths
;
96 while (*Ptr
&& (*Ptr
++ != ';'));
102 // Isolate each search path
103 SourceFileSearchPaths
= (char **)calloc(NbSFSearchPaths
, sizeof(char *));
109 // Search the path separator (';')
111 while (*Ptr
&& (*Ptr
++ != ';'));
113 // Copy the inidividual search path
114 SourceFileSearchPaths
[i
] = (char *)calloc(1, (Ptr
- Ptr1
) + 1);
115 strncpy(SourceFileSearchPaths
[i
], Ptr1
, (Ptr
- Ptr1
));
116 if (SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] == ';')
118 SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] = 0;
124 DWARFManager_Set(NbSFSearchPaths
, SourceFileSearchPaths
);
128 // Reset the source file search paths
129 void DBGManager_SourceFileSearchPathsReset(void)
132 while (NbSFSearchPaths
)
134 free(SourceFileSearchPaths
[--NbSFSearchPaths
]);
137 // Free the pointers list
138 free(SourceFileSearchPaths
);
139 SourceFileSearchPaths
= NULL
;
143 // Close the source file search paths
144 void DBGManager_SourceFileSearchPathsClose(void)
146 DBGManager_SourceFileSearchPathsReset();
150 // Common debugger initialisation
151 void DBGManager_Init(void)
153 // DBG initialisations
154 DBGType
= DBG_NO_TYPE
;
155 DBGManager_SourceFileSearchPathsInit();
157 // ELF initialisation
159 // DWARF initialisation
164 // Common debugger reset
165 void DBGManager_Reset(void)
167 if ((DBGType
& DBG_DWARF
))
169 DWARFManager_Reset();
172 if ((DBGType
& DBG_ELF
))
177 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
178 DBGType
= DBG_NO_TYPE
;
182 // Common debugger close
183 void DBGManager_Close(void)
185 if ((DBGType
& DBG_DWARF
))
187 DWARFManager_Close();
190 if ((DBGType
& DBG_ELF
))
195 DBGManager_SourceFileSearchPathsClose();
196 DBGType
= DBG_NO_TYPE
;
200 // Common debugger set
201 void DBGManager_SetType(size_t DBGTypeSet
)
203 DBGType
|= DBGTypeSet
;
208 size_t DBGManager_GetType(void)
214 // Get source filename based on the memeory address
215 // return NULL if no source filename
216 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr
, bool *Error
)
218 if ((DBGType
& DBG_ELFDWARF
))
220 return DWARFManager_GetFullSourceFilenameFromAdr(Adr
, Error
);
229 // Get number of local variables
230 // Return 0 if none has been found
231 size_t DBGManager_GetNbLocalVariables(size_t Adr
)
233 if ((DBGType
& DBG_ELFDWARF
))
235 return DWARFManager_GetNbLocalVariables(Adr
);
244 // Get number of global variables
245 // Return 0 if none has been found
246 size_t DBGManager_GetNbGlobalVariables(void)
248 if ((DBGType
& DBG_ELFDWARF
))
250 return DWARFManager_GetNbGlobalVariables();
259 // Get address from symbol name
260 // Return found address
261 // Return NULL if no symbol has been found
262 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName
)
266 if ((DBGType
& DBG_ELF
))
268 return ELFManager_GetAdrFromSymbolName(SymbolName
);
276 // Get global variable's Address based on his Name
277 // Return found Address
278 // Return NULL if no Address has been found
279 size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName
)
281 if ((DBGType
& DBG_ELFDWARF
))
283 return DWARFManager_GetGlobalVariableAdrFromName(VariableName
);
292 // Get local variable's type encoding based on his address and Index
293 // Return the type encoding found
294 // Return 0 if no type encoding has been found
295 size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
297 if ((DBGType
& DBG_ELFDWARF
))
299 return DWARFManager_GetLocalVariableTypeEncoding(Adr
, Index
);
309 int DBGManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
311 if ((DBGType
& DBG_ELFDWARF
))
313 return DWARFManager_GetLocalVariableOffset(Adr
, Index
);
322 // Get local variable's type byte size based on his address and Index
323 // Return the type's byte size found
324 // Return 0 if no type's byte size has been found
325 size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr
, size_t Index
)
327 if ((DBGType
& DBG_ELFDWARF
))
329 return DWARFManager_GetLocalVariableTypeByteSize(Adr
, Index
);
339 size_t DBGManager_GetLocalVariableTypeTag(size_t Adr
, size_t Index
)
341 if ((DBGType
& DBG_ELFDWARF
))
343 return DWARFManager_GetLocalVariableTypeTag(Adr
, Index
);
353 size_t DBGManager_GetGlobalVariableTypeTag(size_t Index
)
355 if ((DBGType
& DBG_ELFDWARF
))
357 return DWARFManager_GetGlobalVariableTypeTag(Index
);
366 // Get global variable's type name based on his Index
367 // Return type name's text pointer found
368 // Return NULL if no type name has been found
369 char *DBGManager_GetGlobalVariableTypeName(size_t Index
)
371 if ((DBGType
& DBG_ELFDWARF
))
373 return DWARFManager_GetGlobalVariableTypeName(Index
);
382 // Get global variable's Address based on his Index
383 // Return the Address found
384 // Return 0 if no Address has been found
385 size_t DBGManager_GetGlobalVariableAdr(size_t Index
)
387 if ((DBGType
& DBG_ELFDWARF
))
389 return DWARFManager_GetGlobalVariableAdr(Index
);
398 // Get global variable's type byte size based on his Index
399 // Return the type's byte size found
400 // Return 0 if no type's byte size has been found
401 size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index
)
403 if ((DBGType
& DBG_ELFDWARF
))
405 return DWARFManager_GetGlobalVariableTypeByteSize(Index
);
414 // Get global variable's type encoding based on his Index
415 // Return the type encoding found
416 // Return 0 if no type encoding has been found
417 size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index
)
419 if ((DBGType
& DBG_ELFDWARF
))
421 return DWARFManager_GetGlobalVariableTypeEncoding(Index
);
430 // Get global variable value based on his Index
431 // Return value as a text pointer
432 // Note: Pointer may point on a 0 lenght text
433 char *DBGManager_GetGlobalVariableValue(size_t Index
)
436 size_t TypeEncoding
= DBG_NO_TYPEENCODING
;
437 size_t TypeByteSize
= 0;
439 if ((DBGType
& DBG_ELFDWARF
))
441 Adr
= DWARFManager_GetGlobalVariableAdr(Index
);
442 TypeEncoding
= DWARFManager_GetGlobalVariableTypeEncoding(Index
);
443 TypeByteSize
= DWARFManager_GetGlobalVariableTypeByteSize(Index
);
446 return DBGManager_GetVariableValueFromAdr(Adr
, TypeEncoding
, TypeByteSize
);
450 // Get variable value based on his Adresse, Encoding Type and Size
451 // Return value as a text pointer
452 // Note: Pointer may point on a 0 length text
453 char *DBGManager_GetVariableValueFromAdr(size_t Adr
, size_t TypeEncoding
, size_t TypeByteSize
)
456 char *Ptrvalue
= value
;
464 memset(&V
, 0, sizeof(Value
));
466 for (uint32_t i
= 0; i
< TypeByteSize
; i
++)
467 jaguarMainRAM
[Adr
+ i
] = 0;
468 //jaguarMainRAM[Adr + i] = rand();
469 jaguarMainRAM
[Adr
+ TypeByteSize
- 1] = 0x10;
471 for (size_t i
= 0, j
= TypeByteSize
; i
< TypeByteSize
; i
++, j
--)
473 V
.Ct
[i
] = jaguarMainRAM
[Adr
+ j
- 1];
476 switch (TypeEncoding
)
478 case DBG_ATE_address
:
481 case DBG_ATE_boolean
:
482 sprintf(value
, "%s", V
.B
? "true" : "false");
485 case DBG_ATE_complex_float
:
489 switch (TypeByteSize
)
492 sprintf(value
, "%F", V
.F
);
496 //V.D = (double)jaguarMainRAM[Adr];
497 //sprintf(value, "%10.10F", V.D);
498 sprintf(value
, "%F", V
.D
);
507 switch (TypeByteSize
)
510 sprintf(value
, "%i", V
.SS
);
514 sprintf(value
, "%i", V
.SI
);
518 sprintf(value
, "%i", V
.SL
);
526 case DBG_ATE_signed_char
:
529 case DBG_ATE_unsigned
:
530 switch (TypeByteSize
)
533 sprintf(value
, "%u", V
.US
);
537 sprintf(value
, "%u", V
.UI
);
541 sprintf(value
, "%u", V
.UL
);
549 case DBG_ATE_unsigned_char
:
550 sprintf(value
, "%u", (unsigned int(V
.C
)));
554 switch (TypeByteSize
)
557 sprintf(value
, "0x%06x", V
.UI
);
573 // Get local variable's type name based on his Index
574 // Return type name's text pointer found
575 // Return NULL if no type name has been found
576 char *DBGManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
578 if ((DBGType
& DBG_ELFDWARF
))
580 return DWARFManager_GetLocalVariableTypeName(Adr
, Index
);
589 // Get local variable Op based on his Index
590 // Return variable Op's found
591 // Return 0 if no variable Op has been found
592 size_t DBGManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
594 if ((DBGType
& DBG_ELFDWARF
))
596 return DWARFManager_GetLocalVariableOp(Adr
, Index
);
605 // Get local variable name based on his Index
606 // Return variable name's text pointer found
607 // Return NULL if no variable name has been found
608 char *DBGManager_GetLocalVariableName(size_t Adr
, size_t Index
)
610 if ((DBGType
& DBG_ELFDWARF
))
612 return DWARFManager_GetLocalVariableName(Adr
, Index
);
621 // Get global variable name based on his Index
622 // Return variable name's text pointer found
623 // Return NULL if no variable name has been found
624 char *DBGManager_GetGlobalVariableName(size_t Index
)
626 if ((DBGType
& DBG_ELFDWARF
))
628 return DWARFManager_GetGlobalVariableName(Index
);
637 // Get function name from address
638 // Return function name found
639 // Return NULL if no function name has been found
640 char *DBGManager_GetFunctionName(size_t Adr
)
642 char *Symbolname
= NULL
;
644 if ((DBGType
& DBG_ELFDWARF
))
646 Symbolname
= DWARFManager_GetFunctionName(Adr
);
649 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
651 Symbolname
= ELFManager_GetFunctionName(Adr
);
658 // Get line number from address and his tag
659 // Return line number on the symbol name found
660 // Return 0 if no symbol name has been found
661 size_t DBGManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
663 if ((DBGType
& DBG_ELFDWARF
))
665 return DWARFManager_GetNumLineFromAdr(Adr
, Tag
);
674 // Get symbol name from address
675 // Return text pointer on the symbol name found
676 // Return NULL if no symbol name has been found
677 char *DBGManager_GetSymbolNameFromAdr(size_t Adr
)
681 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
682 if (vjs
.displayHWlabels
)
684 Symbolname
= HWLABELManager_GetSymbolnameFromAdr(Adr
);
691 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
693 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
696 if (Symbolname
== NULL
)
698 if ((DBGType
& DBG_ELFDWARF
))
700 Symbolname
= DWARFManager_GetSymbolnameFromAdr(Adr
);
703 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
705 Symbolname
= ELFManager_GetSymbolnameFromAdr(Adr
);
713 // Get source line based on the Address and his Tag
714 // Return text pointer on the source line found
715 // Return NULL if no source line has been found
716 char *DBGManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
718 char *TextLine
= NULL
;
720 if ((DBGType
& DBG_ELFDWARF
))
722 TextLine
= DWARFManager_GetLineSrcFromAdr(Adr
, Tag
);
729 // Get text line from source based on address and num line (starting from 1)
730 // Return NULL if no text line has been found
731 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
733 char *TextLine
= NULL
;
735 if ((DBGType
& DBG_ELFDWARF
))
737 TextLine
= DWARFManager_GetLineSrcFromAdrNumLine(Adr
, NumLine
);
744 // Get text line from source based on address and num line (starting from 1)
745 // Return NULL if no text line has been found
746 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
748 char *TextLine
= NULL
;
750 if ((DBGType
& DBG_ELFDWARF
))
752 TextLine
= DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr
, NumLine
);
759 // Get number of source code filenames
760 size_t DBGManager_GetNbFullSourceFilename(void)
764 if ((DBGType
& DBG_ELFDWARF
))
766 Nbr
= DWARFManager_GetNbFullSourceFilename();
773 // Get source code filename based on index
774 char *DBGManager_GetNumFullSourceFilename(size_t Index
)
776 char *FullSourceFilename
= NULL
;
778 if ((DBGType
& DBG_ELFDWARF
))
780 FullSourceFilename
= DWARFManager_GetNumFullSourceFilename(Index
);
783 return FullSourceFilename
;