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, and added source file search paths
24 #include "libelf/libelf.h"
25 #include "libelf/gelf.h"
27 #include "ELFManager.h"
28 #include "DwarfManager.h"
29 #include "DBGManager.h"
30 #include "HWLABELManager.h"
54 void DBGManager_SourceFileSearchPathsInit(void);
55 void DBGManager_SourceFileSearchPathsReset(void);
56 void DBGManager_SourceFileSearchPathsClose(void);
59 // Common debugger variables
62 size_t NbSFSearchPaths
;
63 char **SourceFileSearchPaths
;
66 // Init the source file search paths
67 void DBGManager_SourceFileSearchPathsInit(void)
70 SourceFileSearchPaths
= NULL
;
74 // Set the source file search paths
75 // Create individual path for each one provided in the list (separate with ';')
76 void DBGManager_SourceFileSearchPathsSet(char *ListPaths
)
78 // Check presence of a previous list
81 // Reset previous list
82 DBGManager_SourceFileSearchPathsReset();
85 // Check if there is a paths list
86 if (strlen(ListPaths
))
88 // Get number of paths
89 char *Ptr
= ListPaths
;
92 while (*Ptr
&& (*Ptr
++ != ';'));
98 // Isolate each search path
99 SourceFileSearchPaths
= (char **)calloc(NbSFSearchPaths
, sizeof(char *));
105 // Search the path separator (';')
107 while (*Ptr
&& (*Ptr
++ != ';'));
109 // Copy the inidividual search path
110 SourceFileSearchPaths
[i
] = (char *)calloc(1, (Ptr
- Ptr1
) + 1);
111 strncpy(SourceFileSearchPaths
[i
], Ptr1
, (Ptr
- Ptr1
));
112 if (SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] == ';')
114 SourceFileSearchPaths
[i
][strlen(SourceFileSearchPaths
[i
]) - 1] = 0;
120 DWARFManager_Set(NbSFSearchPaths
, SourceFileSearchPaths
);
124 // Reset the source file search paths
125 void DBGManager_SourceFileSearchPathsReset(void)
128 while (NbSFSearchPaths
)
130 free(SourceFileSearchPaths
[--NbSFSearchPaths
]);
133 // Free the pointers list
134 free(SourceFileSearchPaths
);
135 SourceFileSearchPaths
= NULL
;
139 // Close the source file search paths
140 void DBGManager_SourceFileSearchPathsClose(void)
142 DBGManager_SourceFileSearchPathsReset();
146 // Common debugger initialisation
147 void DBGManager_Init(void)
149 // DBG initialisations
150 DBGType
= DBG_NO_TYPE
;
151 DBGManager_SourceFileSearchPathsInit();
153 // ELF initialisation
155 // DWARF initialisation
160 // Common debugger reset
161 void DBGManager_Reset(void)
163 if ((DBGType
& DBG_DWARF
))
165 DWARFManager_Reset();
168 if ((DBGType
& DBG_ELF
))
173 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;
174 DBGType
= DBG_NO_TYPE
;
178 // Common debugger close
179 void DBGManager_Close(void)
181 if ((DBGType
& DBG_DWARF
))
183 DWARFManager_Close();
186 if ((DBGType
& DBG_ELF
))
191 DBGManager_SourceFileSearchPathsClose();
192 DBGType
= DBG_NO_TYPE
;
196 // Common debugger set
197 void DBGManager_SetType(size_t DBGTypeSet
)
199 DBGType
|= DBGTypeSet
;
204 size_t DBGManager_GetType(void)
210 // Get source filename based on the memeory address
211 // return NULL if no source filename
212 char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr
, bool *Error
)
214 if ((DBGType
& DBG_ELFDWARF
))
216 return DWARFManager_GetFullSourceFilenameFromAdr(Adr
, Error
);
225 // Get number of local variables
226 // Return 0 if none has been found
227 size_t DBGManager_GetNbLocalVariables(size_t Adr
)
229 if ((DBGType
& DBG_ELFDWARF
))
231 return DWARFManager_GetNbLocalVariables(Adr
);
240 // Get number of global variables
241 // Return 0 if none has been found
242 size_t DBGManager_GetNbGlobalVariables(void)
244 if ((DBGType
& DBG_ELFDWARF
))
246 return DWARFManager_GetNbGlobalVariables();
255 // Get address from symbol name
256 // Return found address
257 // Return NULL if no symbol has been found
258 size_t DBGManager_GetAdrFromSymbolName(char *SymbolName
)
262 if ((DBGType
& DBG_ELF
))
264 return ELFManager_GetAdrFromSymbolName(SymbolName
);
272 // Get global variable's Address based on his Name
273 // Return found Address
274 // Return NULL if no Address has been found
275 size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName
)
277 if ((DBGType
& DBG_ELFDWARF
))
279 return DWARFManager_GetGlobalVariableAdrFromName(VariableName
);
288 // Get local variable's type encoding based on his address and Index
289 // Return the type encoding found
290 // Return 0 if no type encoding has been found
291 size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
293 if ((DBGType
& DBG_ELFDWARF
))
295 return DWARFManager_GetLocalVariableTypeEncoding(Adr
, Index
);
305 int DBGManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
307 if ((DBGType
& DBG_ELFDWARF
))
309 return DWARFManager_GetLocalVariableOffset(Adr
, Index
);
318 // Get local variable's type byte size based on his address and Index
319 // Return the type's byte size found
320 // Return 0 if no type's byte size has been found
321 size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr
, size_t Index
)
323 if ((DBGType
& DBG_ELFDWARF
))
325 return DWARFManager_GetLocalVariableTypeByteSize(Adr
, Index
);
335 size_t DBGManager_GetLocalVariableTypeTag(size_t Adr
, size_t Index
)
337 if ((DBGType
& DBG_ELFDWARF
))
339 return DWARFManager_GetLocalVariableTypeTag(Adr
, Index
);
349 size_t DBGManager_GetGlobalVariableTypeTag(size_t Index
)
351 if ((DBGType
& DBG_ELFDWARF
))
353 return DWARFManager_GetGlobalVariableTypeTag(Index
);
362 // Get global variable's type name based on his Index
363 // Return type name's text pointer found
364 // Return NULL if no type name has been found
365 char *DBGManager_GetGlobalVariableTypeName(size_t Index
)
367 if ((DBGType
& DBG_ELFDWARF
))
369 return DWARFManager_GetGlobalVariableTypeName(Index
);
378 // Get global variable's Address based on his Index
379 // Return the Address found
380 // Return 0 if no Address has been found
381 size_t DBGManager_GetGlobalVariableAdr(size_t Index
)
383 if ((DBGType
& DBG_ELFDWARF
))
385 return DWARFManager_GetGlobalVariableAdr(Index
);
394 // Get global variable's type byte size based on his Index
395 // Return the type's byte size found
396 // Return 0 if no type's byte size has been found
397 size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index
)
399 if ((DBGType
& DBG_ELFDWARF
))
401 return DWARFManager_GetGlobalVariableTypeByteSize(Index
);
410 // Get global variable's type encoding based on his Index
411 // Return the type encoding found
412 // Return 0 if no type encoding has been found
413 size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index
)
415 if ((DBGType
& DBG_ELFDWARF
))
417 return DWARFManager_GetGlobalVariableTypeEncoding(Index
);
426 // Get global variable value based on his Index
427 // Return value as a text pointer
428 // Note: Pointer may point on a 0 lenght text
429 char *DBGManager_GetGlobalVariableValue(size_t Index
)
432 size_t TypeEncoding
= DBG_NO_TYPEENCODING
;
433 size_t TypeByteSize
= 0;
435 if ((DBGType
& DBG_ELFDWARF
))
437 Adr
= DWARFManager_GetGlobalVariableAdr(Index
);
438 TypeEncoding
= DWARFManager_GetGlobalVariableTypeEncoding(Index
);
439 TypeByteSize
= DWARFManager_GetGlobalVariableTypeByteSize(Index
);
442 return DBGManager_GetVariableValueFromAdr(Adr
, TypeEncoding
, TypeByteSize
);
446 // Get variable value based on his Adresse, Encoding Type and Size
447 // Return value as a text pointer
448 // Note: Pointer may point on a 0 length text
449 char *DBGManager_GetVariableValueFromAdr(size_t Adr
, size_t TypeEncoding
, size_t TypeByteSize
)
452 char *Ptrvalue
= value
;
460 memset(&V
, 0, sizeof(Value
));
462 for (uint32_t i
= 0; i
< TypeByteSize
; i
++)
463 jaguarMainRAM
[Adr
+ i
] = 0;
464 //jaguarMainRAM[Adr + i] = rand();
465 jaguarMainRAM
[Adr
+ TypeByteSize
- 1] = 0x10;
467 for (size_t i
= 0, j
= TypeByteSize
; i
< TypeByteSize
; i
++, j
--)
469 V
.Ct
[i
] = jaguarMainRAM
[Adr
+ j
- 1];
472 switch (TypeEncoding
)
474 case DBG_ATE_address
:
477 case DBG_ATE_boolean
:
478 sprintf(value
, "%s", V
.B
? "true" : "false");
481 case DBG_ATE_complex_float
:
485 switch (TypeByteSize
)
488 sprintf(value
, "%F", V
.F
);
492 //V.D = (double)jaguarMainRAM[Adr];
493 //sprintf(value, "%10.10F", V.D);
494 sprintf(value
, "%F", V
.D
);
503 switch (TypeByteSize
)
506 sprintf(value
, "%i", V
.SI
);
510 sprintf(value
, "%i", V
.SL
);
518 case DBG_ATE_signed_char
:
521 case DBG_ATE_unsigned
:
522 switch (TypeByteSize
)
525 sprintf(value
, "%u", V
.UI
);
529 sprintf(value
, "%u", V
.UL
);
537 case DBG_ATE_unsigned_char
:
538 sprintf(value
, "%u", (unsigned int(V
.C
)));
542 switch (TypeByteSize
)
545 sprintf(value
, "0x%06x", V
.UI
);
561 // Get local variable's type name based on his Index
562 // Return type name's text pointer found
563 // Return NULL if no type name has been found
564 char *DBGManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
566 if ((DBGType
& DBG_ELFDWARF
))
568 return DWARFManager_GetLocalVariableTypeName(Adr
, Index
);
577 // Get local variable Op based on his Index
578 // Return variable Op's found
579 // Return 0 if no variable Op has been found
580 size_t DBGManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
582 if ((DBGType
& DBG_ELFDWARF
))
584 return DWARFManager_GetLocalVariableOp(Adr
, Index
);
593 // Get local variable name based on his Index
594 // Return variable name's text pointer found
595 // Return NULL if no variable name has been found
596 char *DBGManager_GetLocalVariableName(size_t Adr
, size_t Index
)
598 if ((DBGType
& DBG_ELFDWARF
))
600 return DWARFManager_GetLocalVariableName(Adr
, Index
);
609 // Get global variable name based on his Index
610 // Return variable name's text pointer found
611 // Return NULL if no variable name has been found
612 char *DBGManager_GetGlobalVariableName(size_t Index
)
614 if ((DBGType
& DBG_ELFDWARF
))
616 return DWARFManager_GetGlobalVariableName(Index
);
625 // Get function name from address
626 // Return function name found
627 // Return NULL if no function name has been found
628 char *DBGManager_GetFunctionName(size_t Adr
)
630 if ((DBGType
& DBG_ELFDWARF
))
632 return DWARFManager_GetFunctionName(Adr
);
641 // Get line number from address and his tag
642 // Return line number on the symbol name found
643 // Return 0 if no symbol name has been found
644 size_t DBGManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
646 if ((DBGType
& DBG_ELFDWARF
))
648 return DWARFManager_GetNumLineFromAdr(Adr
, Tag
);
657 // Get symbol name from address
658 // Return text pointer on the symbol name found
659 // Return NULL if no symbol name has been found
660 char *DBGManager_GetSymbolNameFromAdr(size_t Adr
)
664 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)
665 if (vjs
.displayHWlabels
)
667 Symbolname
= HWLABELManager_GetSymbolnameFromAdr(Adr
);
674 #pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")
676 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"
679 if (Symbolname
== NULL
)
681 if ((DBGType
& DBG_ELFDWARF
))
683 Symbolname
= DWARFManager_GetSymbolnameFromAdr(Adr
);
686 if ((DBGType
& DBG_ELF
) && (Symbolname
== NULL
))
688 Symbolname
= ELFManager_GetSymbolnameFromAdr(Adr
);
696 // Get source line based on the Address and his Tag
697 // Return text pointer on the source line found
698 // Return NULL if no source line has been found
699 char *DBGManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
701 char *TextLine
= NULL
;
703 if ((DBGType
& DBG_ELFDWARF
))
705 TextLine
= DWARFManager_GetLineSrcFromAdr(Adr
, Tag
);
712 // Get text line from source based on address and num line (starting from 1)
713 // Return NULL if no text line has been found
714 char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
716 char *TextLine
= NULL
;
718 if ((DBGType
& DBG_ELFDWARF
))
720 TextLine
= DWARFManager_GetLineSrcFromAdrNumLine(Adr
, NumLine
);
727 // Get text line from source based on address and num line (starting from 1)
728 // Return NULL if no text line has been found
729 char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
731 char *TextLine
= NULL
;
733 if ((DBGType
& DBG_ELFDWARF
))
735 TextLine
= DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr
, NumLine
);
742 // Get number of source code filenames
743 size_t DBGManager_GetNbFullSourceFilename(void)
747 if ((DBGType
& DBG_ELFDWARF
))
749 Nbr
= DWARFManager_GetNbFullSourceFilename();
756 // Get source code filename based on index
757 char *DBGManager_GetNumFullSourceFilename(size_t Index
)
759 char *FullSourceFilename
= NULL
;
761 if ((DBGType
& DBG_ELFDWARF
))
763 FullSourceFilename
= DWARFManager_GetNumFullSourceFilename(Index
);
766 return FullSourceFilename
;