2 // DWARFManager.cpp: DWARF format manager
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
9 // --- ---------- ------------------------------------------------------------
10 // JPM Dec./2016 Created this file, and added the DWARF format support
11 // JPM Sept./2018 Added LEB128 decoding features, and improve the DWARF parsing information
12 // JPM Oct./2018 Improve the DWARF parsing information, and the source file text reading; support the used source lines from DWARF structure, and the search paths for the files
13 // JPM Aug./2019 Added new functions to handle DWARF information, full filename fix
14 // JPM Mar./2020 Fix a random crash when reading the source lines information
18 // To use pointers instead of arrays usage
31 // Definitions for debugging
32 //#define DEBUG_NumCU 0x3 // CU number to debug or undefine it
33 //#define DEBUG_VariableName "sound_death" // Variable name to look for or undefine it
34 //#define DEBUG_TypeName "Cbuf_Execute" // Type name to look for or undefine it
35 //#define DEBUG_TypeDef DW_TAG_typedef // Type def to look for or undefine it (not supported)
36 //#define DEBUG_Filename "crt0" // Filename to look for or undefine it
38 // Definitions for handling data
39 //#define CONVERT_QT_HML // Text will be converted as HTML
41 // Definitions for the variables's typetag
42 #define TypeTag_structure 0x01 // structure
43 #define TypeTag_pointer 0x02 // pointer
44 #define TypeTag_subrange 0x04 // (subrange_type?)
45 #define TypeTag_arraytype 0x08 // array type
46 #define TypeTag_consttype 0x10 // const type
47 #define TypeTag_typedef 0x20 // typedef
48 #define TypeTag_enumeration_type 0x40 // enumeration
49 #define TypeTag_subroutine_type 0x80 // subroutine
52 // Source line CU structure
53 typedef struct CUStruct_LineSrc
60 // Source line internal structure
61 typedef struct DMIStruct_LineSrc
69 // Enumeration structure
70 typedef struct EnumerationStruct
72 char *PtrName
; // Enumeration's name
73 size_t value
; // Enumeration's value
76 // Structure members structure
77 //typedef struct StructureMembersStruct
79 //}S_StructureMembersStruct;
81 // Base type internal structure
82 typedef struct BaseTypeStruct
84 size_t Tag
; // Type's Tag
85 size_t Offset
; // Type's offset
86 size_t TypeOffset
; // Type's offset on another type
87 size_t ByteSize
; // Type's Byte Size
88 size_t Encoding
; // Type's encoding
89 char *PtrName
; // Type's name
90 size_t NbEnumeration
; // Type's enumeration numbers
91 EnumerationStruct
*PtrEnumeration
; // Type's enumeration
92 // StructureMembersStruct *PtrStructureMembers; // Type's structure members
95 // Variables internal structure
96 typedef struct VariablesStruct
98 size_t Op
; // Variable's DW_OP
101 size_t Addr
; // Variable memory address
102 int Offset
; // Variable stack offset (signed)
104 char *PtrName
; // Variable's name
105 size_t TypeOffset
; // Offset pointing on the Variable's Type
106 size_t TypeByteSize
; // Variable's Type byte size
107 size_t TypeTag
; // Variable's Type Tag
108 size_t TypeEncoding
; // Variable's Type encoding
109 char *PtrTypeName
; // Variable's Type name
112 // Sub program internal structure
113 typedef struct SubProgStruct
118 size_t LowPC
, HighPC
;
121 char *PtrSubprogramName
; // Sub program name
122 size_t NbLinesSrc
; // Number of lines source used by the sub program
123 DMIStruct_LineSrc
*PtrLinesSrc
; // Pointer of the lines source for the sub program
124 size_t NbVariables
; // Variables number
125 VariablesStruct
*PtrVariables
; // Pointer to the local variables list information structure
128 // Compilation Unit internal structure
129 typedef struct CUStruct
132 size_t Language
; // Language (C, etc.) used by the source code
133 size_t LowPC
, HighPC
; // Memory range for the code
134 char *PtrProducer
; // Pointer to the "Producer" text information (mostly compiler and compilation options used)
135 char *PtrSourceFilename
; // Source file name
136 char *PtrSourceFileDirectory
; // Directory of the source file
137 char *PtrFullFilename
; // Pointer to full namefile (directory & filename)
138 size_t SizeLoadSrc
; // Source code text size
139 char *PtrLoadSrc
; // Pointer to the source code text
140 size_t NbLinesLoadSrc
; // Total number of lines in the source code text
141 char **PtrLinesLoadSrc
; // Pointer lists to each source line put in QT html/text conformity
142 size_t NbSubProgs
; // Number of sub programs / routines
143 SubProgStruct
*PtrSubProgs
; // Pointer to the sub programs / routines structure
144 size_t NbTypes
; // Number of types
145 BaseTypeStruct
*PtrTypes
; // Pointer to types
146 size_t NbVariables
; // Variables number
147 VariablesStruct
*PtrVariables
; // Pointer to the global variables list structure
148 size_t NbFrames
; // Frames number
149 size_t NbUsedLinesSrc
; // Number of used source lines
150 size_t LastNumUsedLinesSrc
; // Last number line used
151 CUStruct_LineSrc
*PtrUsedLinesSrc
; // Pointer to the used source lines list structure
152 char **PtrUsedLinesLoadSrc
; // Pointer lists to each used source line referenced by the CUStruct_LineSrc structure
153 size_t *PtrUsedNumLines
; // List of the number lines used
164 char **ListSearchPaths
;
165 size_t NbSearchPaths
;
169 Dwarf_Handler
DWARFManager_ErrorHandler(Dwarf_Ptr perrarg
);
170 void DWARFManager_InitDMI(void);
171 void DWARFManager_CloseDMI(void);
172 bool DWARFManager_ElfClose(void);
173 char *DWARFManager_GetLineSrcFromNumLine(char *PtrSrcFile
, size_t NumLine
);
174 void DWARFManager_InitInfosVariable(VariablesStruct
*PtrVariables
);
175 void DWARFManager_SourceFileSearchPathsInit(void);
176 void DWARFManager_SourceFileSearchPathsReset(void);
177 void DWARFManager_SourceFileSearchPathsClose(void);
178 void DWARFManager_ConformSlachesBackslashes(char *Ptr
);
182 Dwarf_Handler
DWARFManager_ErrorHandler(Dwarf_Ptr perrarg
)
188 // Dwarf manager list search paths init
189 void DWARFManager_SourceFileSearchPathsInit(void)
191 ListSearchPaths
= NULL
;
196 // Dwarf manager list search paths reset
197 void DWARFManager_SourceFileSearchPathsReset(void)
199 ListSearchPaths
= NULL
;
204 // Dwarf manager list search paths close
205 void DWARFManager_SourceFileSearchPathsClose(void)
207 DWARFManager_SourceFileSearchPathsReset();
211 // Dwarf manager init
212 void DWARFManager_Init(void)
214 DWARFManager_SourceFileSearchPathsInit();
215 LibDwarf
= DW_DLV_NO_ENTRY
;
219 // Dwarf manager settings
220 void DWARFManager_Set(size_t NbPathsInList
, char **PtrListPaths
)
223 ListSearchPaths
= PtrListPaths
;
224 NbSearchPaths
= NbPathsInList
;
228 // Dwarf manager Reset
229 bool DWARFManager_Reset(void)
231 DWARFManager_SourceFileSearchPathsReset();
232 return DWARFManager_ElfClose();
236 // Dwarf manager Close
237 bool DWARFManager_Close(void)
239 DWARFManager_SourceFileSearchPathsClose();
240 return(DWARFManager_Reset());
244 // Dwarf manager Elf init
245 int DWARFManager_ElfInit(Elf
*ElfPtr
)
247 if ((LibDwarf
= dwarf_elf_init(ElfPtr
, DW_DLC_READ
, (Dwarf_Handler
)DWARFManager_ErrorHandler
, errarg
, &dbg
, &error
)) == DW_DLV_OK
)
249 DWARFManager_InitDMI();
256 // Dwarf manager Elf close
257 bool DWARFManager_ElfClose(void)
259 if (LibDwarf
== DW_DLV_OK
)
261 DWARFManager_CloseDMI();
263 if (dwarf_finish(dbg
, &error
) == DW_DLV_OK
)
265 LibDwarf
= DW_DLV_NO_ENTRY
;
280 // Dwarf manager Compilation Units close
281 void DWARFManager_CloseDMI(void)
285 free(PtrCU
[NbCU
].PtrFullFilename
);
286 free(PtrCU
[NbCU
].PtrLoadSrc
);
287 free(PtrCU
[NbCU
].PtrProducer
);
288 free(PtrCU
[NbCU
].PtrSourceFilename
);
289 free(PtrCU
[NbCU
].PtrSourceFileDirectory
);
290 free(PtrCU
[NbCU
].PtrUsedLinesSrc
);
291 free(PtrCU
[NbCU
].PtrUsedLinesLoadSrc
);
292 free(PtrCU
[NbCU
].PtrUsedNumLines
);
294 while (PtrCU
[NbCU
].NbLinesLoadSrc
--)
296 free(PtrCU
[NbCU
].PtrLinesLoadSrc
[PtrCU
[NbCU
].NbLinesLoadSrc
]);
298 free(PtrCU
[NbCU
].PtrLinesLoadSrc
);
300 while (PtrCU
[NbCU
].NbSubProgs
--)
302 while (PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
--)
304 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].PtrName
);
305 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].PtrTypeName
);
307 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
);
309 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
);
310 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrSubprogramName
);
312 free(PtrCU
[NbCU
].PtrSubProgs
);
314 while (PtrCU
[NbCU
].NbTypes
--)
316 free(PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].PtrName
);
318 free(PtrCU
[NbCU
].PtrTypes
);
320 while (PtrCU
[NbCU
].NbVariables
--)
322 free(PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
);
323 free(PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrTypeName
);
325 free(PtrCU
[NbCU
].PtrVariables
);
332 // Dwarf manager Compilation Units initialisations
333 void DWARFManager_InitDMI(void)
335 Dwarf_Unsigned next_cu_header
, return_uvalue
;
337 Dwarf_Attribute
*atlist
;
338 Dwarf_Attribute return_attr1
;
339 Dwarf_Half return_tagval
, return_attr
;
340 Dwarf_Addr return_lowpc
, return_highpc
, return_lineaddr
;
341 Dwarf_Block
*return_block
;
342 Dwarf_Signed atcnt
, cnt
;
343 Dwarf_Die return_sib
, return_die
, return_sub
, return_subdie
;
344 Dwarf_Off return_offset
;
350 // Initialisation for the Compilation Units table
354 // loop on the available Compilation Unit
355 while (dwarf_next_cu_header(dbg
, NULL
, NULL
, NULL
, NULL
, &next_cu_header
, &error
) == DW_DLV_OK
)
357 // Allocation of an additional Compilation Unit structure in the table
358 if (Ptr
= (char *)realloc(PtrCU
, ((NbCU
+ 1) * sizeof(CUStruct
))))
360 // Compilation Unit RAZ
361 PtrCU
= (CUStruct
*)Ptr
;
362 memset(PtrCU
+ NbCU
, 0, sizeof(CUStruct
));
366 if (NbCU
== DEBUG_NumCU
)
369 // Get 1st Die from the Compilation Unit
370 if (dwarf_siblingof(dbg
, NULL
, &return_sib
, &error
) == DW_DLV_OK
)
373 if ((dwarf_tag(return_sib
, &return_tagval
, &error
) == DW_DLV_OK
))
375 PtrCU
[NbCU
].Tag
= return_tagval
;
377 // Die type detection
378 switch (return_tagval
)
380 case DW_TAG_compile_unit
:
381 if (dwarf_attrlist(return_sib
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
383 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
385 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
391 if (dwarf_lowpc(return_sib
, &return_lowpc
, &error
) == DW_DLV_OK
)
393 PtrCU
[NbCU
].LowPC
= return_lowpc
;
399 if (dwarf_highpc(return_sib
, &return_highpc
, &error
) == DW_DLV_OK
)
401 PtrCU
[NbCU
].HighPC
= return_highpc
;
405 // compilation information
407 if (dwarf_formstring(atlist
[i
], &return_string
, &error
) == DW_DLV_OK
)
409 PtrCU
[NbCU
].PtrProducer
= (char *)calloc(strlen(return_string
) + 1, 1);
410 strcpy(PtrCU
[NbCU
].PtrProducer
, return_string
);
411 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
417 if (dwarf_formstring(atlist
[i
], &return_string
, &error
) == DW_DLV_OK
)
419 #ifdef DEBUG_Filename
420 if (strstr(return_string
, DEBUG_Filename
))
423 PtrCU
[NbCU
].PtrSourceFilename
= (char *)calloc((strlen(return_string
) + 1), 1);
424 strcpy(PtrCU
[NbCU
].PtrSourceFilename
, return_string
);
426 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
432 if (dwarf_formstring(atlist
[i
], &return_string
, &error
) == DW_DLV_OK
)
434 PtrCU
[NbCU
].PtrSourceFileDirectory
= (char *)calloc((strlen(return_string
) + 1), 1);
435 strcpy(PtrCU
[NbCU
].PtrSourceFileDirectory
, return_string
);
436 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
442 if (dwarf_formudata(atlist
[i
], &return_uvalue
, &error
) == DW_DLV_OK
)
444 PtrCU
[NbCU
].Language
= return_uvalue
;
452 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
454 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
457 // Check filename presence
458 if (!PtrCU
[NbCU
].PtrSourceFilename
)
460 PtrCU
[NbCU
].PtrSourceFilename
= (char *)calloc(1, 1);
463 // Check directory presence
464 if (!PtrCU
[NbCU
].PtrSourceFileDirectory
)
466 // Check if file exists in the search paths
467 for (size_t i
= 0; i
< NbSearchPaths
; i
++)
469 PtrCU
[NbCU
].PtrFullFilename
= (char *)realloc(PtrCU
[NbCU
].PtrFullFilename
, strlen(PtrCU
[NbCU
].PtrSourceFilename
) + strlen((const char *)ListSearchPaths
[i
]) + 2);
471 sprintf(PtrCU
[NbCU
].PtrFullFilename
, "%s\\%s", ListSearchPaths
[i
], PtrCU
[NbCU
].PtrSourceFilename
);
473 sprintf(PtrCU
[NbCU
].PtrFullFilename
, "%s/%s", ListSearchPaths
[i
], PtrCU
[NbCU
].PtrSourceFilename
);
475 if (!fopen_s(&SrcFile
, PtrCU
[NbCU
].PtrFullFilename
, "rb"))
477 PtrCU
[NbCU
].PtrSourceFileDirectory
= (char *)realloc(PtrCU
[NbCU
].PtrSourceFileDirectory
, strlen(ListSearchPaths
[i
]) + 1);
478 strcpy(PtrCU
[NbCU
].PtrSourceFileDirectory
, ListSearchPaths
[i
]);
482 // File directory doesn't exits
483 if (!PtrCU
[NbCU
].PtrSourceFileDirectory
)
485 PtrCU
[NbCU
].PtrSourceFileDirectory
= (char *)realloc(PtrCU
[NbCU
].PtrSourceFileDirectory
, 2);
486 strcpy(PtrCU
[NbCU
].PtrSourceFileDirectory
, ".");
490 // Conform slashes / backslashes for the filename
491 DWARFManager_ConformSlachesBackslashes(PtrCU
[NbCU
].PtrSourceFilename
);
493 // Check if filename contains already the complete directory
494 if (PtrCU
[NbCU
].PtrSourceFilename
[1] == ':')
496 // Copy the filename as the full filename
497 PtrCU
[NbCU
].PtrFullFilename
= (char *)realloc(PtrCU
[NbCU
].PtrFullFilename
, strlen(PtrCU
[NbCU
].PtrSourceFilename
) + 1);
498 strcpy(PtrCU
[NbCU
].PtrFullFilename
, PtrCU
[NbCU
].PtrSourceFilename
);
502 // Create full filename and Conform slashes / backslashes
503 PtrCU
[NbCU
].PtrFullFilename
= (char *)realloc(PtrCU
[NbCU
].PtrFullFilename
, strlen(PtrCU
[NbCU
].PtrSourceFilename
) + strlen(PtrCU
[NbCU
].PtrSourceFileDirectory
) + 2);
505 sprintf(PtrCU
[NbCU
].PtrFullFilename
, "%s\\%s", PtrCU
[NbCU
].PtrSourceFileDirectory
, PtrCU
[NbCU
].PtrSourceFilename
);
507 sprintf(PtrCU
[NbCU
].PtrFullFilename
, "%s/%s", PtrCU
[NbCU
].PtrSourceFileDirectory
, PtrCU
[NbCU
].PtrSourceFilename
);
511 DWARFManager_ConformSlachesBackslashes(PtrCU
[NbCU
].PtrFullFilename
);
513 // Directory path clean-up
515 while ((Ptr1
= Ptr
= strstr(PtrCU
[NbCU
].PtrFullFilename
, "\\..\\")))
517 while ((Ptr1
= Ptr
= strstr(PtrCU
[NbCU
].PtrFullFilename
, "/../")))
521 while (*--Ptr1
!= '\\');
523 while (*--Ptr1
!= '/');
525 strcpy((Ptr1
+ 1), (Ptr
+ 4));
528 // Open the source file as a binary file
529 if (!fopen_s(&SrcFile
, PtrCU
[NbCU
].PtrFullFilename
, "rb"))
531 if (!fseek(SrcFile
, 0, SEEK_END
))
533 if ((PtrCU
[NbCU
].SizeLoadSrc
= ftell(SrcFile
)) > 0)
535 if (!fseek(SrcFile
, 0, SEEK_SET
))
537 if (PtrCU
[NbCU
].PtrLoadSrc
= Ptr
= Ptr1
= (char *)calloc(1, (PtrCU
[NbCU
].SizeLoadSrc
+ 2)))
540 if (fread_s(PtrCU
[NbCU
].PtrLoadSrc
, PtrCU
[NbCU
].SizeLoadSrc
, PtrCU
[NbCU
].SizeLoadSrc
, 1, SrcFile
) != 1)
542 free(PtrCU
[NbCU
].PtrLoadSrc
);
543 PtrCU
[NbCU
].PtrLoadSrc
= NULL
;
544 PtrCU
[NbCU
].SizeLoadSrc
= 0;
548 // Eliminate all carriage return code '\r' (oxd)
551 if ((*Ptr
= *Ptr1
) != '\r')
558 // Get back the new text file size
559 PtrCU
[NbCU
].SizeLoadSrc
= strlen(Ptr
= PtrCU
[NbCU
].PtrLoadSrc
);
561 // Make sure the text file finish with a new line code '\n' (0xa)
562 if (PtrCU
[NbCU
].PtrLoadSrc
[PtrCU
[NbCU
].SizeLoadSrc
- 1] != '\n')
564 PtrCU
[NbCU
].PtrLoadSrc
[PtrCU
[NbCU
].SizeLoadSrc
++] = '\n';
565 PtrCU
[NbCU
].PtrLoadSrc
[PtrCU
[NbCU
].SizeLoadSrc
] = 0;
568 // Reallocate text file
569 if (PtrCU
[NbCU
].PtrLoadSrc
= Ptr
= (char *)realloc(PtrCU
[NbCU
].PtrLoadSrc
, (PtrCU
[NbCU
].SizeLoadSrc
+ 1)))
571 // Count line numbers, based on the new line code '\n' (0xa), and finish each line with 0
576 PtrCU
[NbCU
].NbLinesLoadSrc
++;
595 // Get the source lines table located in the CU
596 if (dwarf_srclines(return_sib
, &linebuf
, &cnt
, &error
) == DW_DLV_OK
)
600 PtrCU
[NbCU
].NbUsedLinesSrc
= cnt
;
601 PtrCU
[NbCU
].PtrUsedLinesSrc
= (CUStruct_LineSrc
*)calloc(cnt
, sizeof(CUStruct_LineSrc
));
602 PtrCU
[NbCU
].PtrUsedLinesLoadSrc
= (char **)calloc(cnt
, sizeof(char *));
603 PtrCU
[NbCU
].PtrUsedNumLines
= (size_t *)calloc(cnt
, sizeof(size_t));
605 // Get the addresses and their source line numbers
606 for (Dwarf_Signed i
= 0; i
< cnt
; i
++)
608 if (dwarf_lineaddr(linebuf
[i
], &return_lineaddr
, &error
) == DW_DLV_OK
)
610 // Get the source line number
611 if (dwarf_lineno(linebuf
[i
], &return_uvalue
, &error
) == DW_DLV_OK
)
613 PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].StartPC
= return_lineaddr
;
614 PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].NumLineSrc
= return_uvalue
;
620 // Release the memory used by the source lines table located in the CU
621 dwarf_srclines_dealloc(dbg
, linebuf
, cnt
);
624 // Check if the CU has child
625 if (dwarf_child(return_sib
, &return_die
, &error
) == DW_DLV_OK
)
629 return_sib
= return_die
;
630 if ((dwarf_tag(return_die
, &return_tagval
, &error
) == DW_DLV_OK
))
632 switch (return_tagval
)
634 case DW_TAG_lexical_block
:
637 case DW_TAG_variable
:
638 if (dwarf_attrlist(return_die
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
640 PtrCU
[NbCU
].PtrVariables
= (VariablesStruct
*)realloc(PtrCU
[NbCU
].PtrVariables
, ((PtrCU
[NbCU
].NbVariables
+ 1) * sizeof(VariablesStruct
)));
641 memset(PtrCU
[NbCU
].PtrVariables
+ PtrCU
[NbCU
].NbVariables
, 0, sizeof(VariablesStruct
));
643 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
645 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
647 if (dwarf_attr(return_die
, return_attr
, &return_attr1
, &error
) == DW_DLV_OK
)
652 if (dwarf_formblock(return_attr1
, &return_block
, &error
) == DW_DLV_OK
)
654 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].Op
= (*((unsigned char *)(return_block
->bl_data
)));
656 switch (return_block
->bl_len
)
659 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].Addr
= (*((unsigned char *)(return_block
->bl_data
) + 1) << 24) + (*((unsigned char *)(return_block
->bl_data
) + 2) << 16) + (*((unsigned char *)(return_block
->bl_data
) + 3) << 8) + (*((unsigned char *)(return_block
->bl_data
) + 4));
665 dwarf_dealloc(dbg
, return_block
, DW_DLA_BLOCK
);
670 if (dwarf_global_formref(return_attr1
, &return_offset
, &error
) == DW_DLV_OK
)
672 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].TypeOffset
= return_offset
;
678 if (dwarf_formstring(return_attr1
, &return_string
, &error
) == DW_DLV_OK
)
680 #ifdef DEBUG_VariableName
681 if (!strcmp(return_string
, DEBUG_VariableName
))
684 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
= (char *)calloc(strlen(return_string
) + 1, 1);
685 strcpy(PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
, return_string
);
687 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
697 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
700 // Check variable's name validity
701 if (PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
)
703 // Check variable's memory address validity
704 if (PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].Addr
)
707 PtrCU
[NbCU
].NbVariables
++;
712 free(PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
);
713 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
= NULL
;
717 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
721 case DW_TAG_base_type
:
723 case DW_TAG_structure_type
:
724 case DW_TAG_pointer_type
:
725 case DW_TAG_const_type
:
726 case DW_TAG_array_type
:
727 case DW_TAG_subrange_type
:
728 case DW_TAG_subroutine_type
:
729 case DW_TAG_enumeration_type
:
730 if (dwarf_attrlist(return_die
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
732 // Allocate memory for this type
733 PtrCU
[NbCU
].PtrTypes
= (BaseTypeStruct
*)realloc(PtrCU
[NbCU
].PtrTypes
, ((PtrCU
[NbCU
].NbTypes
+ 1) * sizeof(BaseTypeStruct
)));
734 memset(PtrCU
[NbCU
].PtrTypes
+ PtrCU
[NbCU
].NbTypes
, 0, sizeof(BaseTypeStruct
));
735 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].Tag
= return_tagval
;
737 if (dwarf_dieoffset(return_die
, &return_offset
, &error
) == DW_DLV_OK
)
739 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].Offset
= return_offset
;
742 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
744 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
746 if (dwarf_attr(return_die
, return_attr
, &return_attr1
, &error
) == DW_DLV_OK
)
754 // Type's type offset
756 if (dwarf_global_formref(return_attr1
, &return_offset
, &error
) == DW_DLV_OK
)
758 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].TypeOffset
= return_offset
;
763 case DW_AT_byte_size
:
764 if (dwarf_formudata(return_attr1
, &return_uvalue
, &error
) == DW_DLV_OK
)
766 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].ByteSize
= return_uvalue
;
772 if (dwarf_formudata(return_attr1
, &return_uvalue
, &error
) == DW_DLV_OK
)
774 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].Encoding
= return_uvalue
;
780 if (dwarf_formstring(return_attr1
, &return_string
, &error
) == DW_DLV_OK
)
782 #ifdef DEBUG_TypeName
783 if (!strcmp(return_string
, DEBUG_TypeName
))
786 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].PtrName
= (char *)calloc(strlen(return_string
) + 1, 1);
787 strcpy(PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].PtrName
, return_string
);
789 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
793 // Type's file number
794 case DW_AT_decl_file
:
797 // Type's line number
798 case DW_AT_decl_line
:
807 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
810 PtrCU
[NbCU
].NbTypes
++;
812 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
816 case DW_TAG_subprogram
:
817 if (dwarf_attrlist(return_die
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
819 PtrCU
[NbCU
].PtrSubProgs
= (SubProgStruct
*)realloc(PtrCU
[NbCU
].PtrSubProgs
, ((PtrCU
[NbCU
].NbSubProgs
+ 1) * sizeof(SubProgStruct
)));
820 memset((void *)(PtrCU
[NbCU
].PtrSubProgs
+ PtrCU
[NbCU
].NbSubProgs
), 0, sizeof(SubProgStruct
));
821 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].Tag
= return_tagval
;
823 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
825 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
827 if (dwarf_attr(return_die
, return_attr
, &return_attr1
, &error
) == DW_DLV_OK
)
833 if (dwarf_lowpc(return_die
, &return_lowpc
, &error
) == DW_DLV_OK
)
835 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].StartPC
= return_lowpc
;
836 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].LowPC
= return_lowpc
;
842 if (dwarf_highpc(return_die
, &return_highpc
, &error
) == DW_DLV_OK
)
844 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].HighPC
= return_highpc
;
849 case DW_AT_decl_line
:
850 if (dwarf_formudata(return_attr1
, &return_uvalue
, &error
) == DW_DLV_OK
)
852 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NumLineSrc
= return_uvalue
;
857 case DW_AT_frame_base
:
858 if (dwarf_formudata(return_attr1
, &return_uvalue
, &error
) == DW_DLV_OK
)
860 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].FrameBase
= return_uvalue
;
861 PtrCU
[NbCU
].NbFrames
++;
867 if (dwarf_formstring(return_attr1
, &return_string
, &error
) == DW_DLV_OK
)
869 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrSubprogramName
= (char *)calloc(strlen(return_string
) + 1, 1);
870 strcpy(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrSubprogramName
, return_string
);
871 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
878 case DW_AT_GNU_all_tail_call_sites
:
884 case DW_AT_prototyped
:
888 case DW_AT_decl_file
:
899 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
901 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
903 // Get source line number and associated block of address
904 for (Dwarf_Signed i
= 0; i
< cnt
; ++i
)
906 if ((PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].StartPC
>= return_lowpc
) && (PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].StartPC
<= return_highpc
))
908 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
= (DMIStruct_LineSrc
*)realloc(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
, (PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
+ 1) * sizeof(DMIStruct_LineSrc
));
909 memset((void *)(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
+ PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
), 0, sizeof(DMIStruct_LineSrc
));
910 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
].StartPC
= PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].StartPC
;
911 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
].NumLineSrc
= PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].NumLineSrc
;
912 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
++;
916 if (dwarf_child(return_die
, &return_subdie
, &error
) == DW_DLV_OK
)
920 return_sub
= return_subdie
;
921 if ((dwarf_tag(return_subdie
, &return_tagval
, &error
) == DW_DLV_OK
))
923 switch (return_tagval
)
925 case DW_TAG_formal_parameter
:
926 case DW_TAG_variable
:
927 if (dwarf_attrlist(return_subdie
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
929 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
= (VariablesStruct
*)realloc(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
, ((PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
+ 1) * sizeof(VariablesStruct
)));
930 memset(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
+ PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
, 0, sizeof(VariablesStruct
));
932 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
934 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
936 if (dwarf_attr(return_subdie
, return_attr
, &return_attr1
, &error
) == DW_DLV_OK
)
941 if (dwarf_formblock(return_attr1
, &return_block
, &error
) == DW_DLV_OK
)
943 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].Op
= *((unsigned char *)(return_block
->bl_data
));
945 switch (return_block
->bl_len
)
952 switch (return_tagval
)
954 case DW_TAG_variable
:
955 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].Offset
= ReadLEB128((char *)return_block
->bl_data
+ 1);
958 case DW_TAG_formal_parameter
:
959 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].Offset
= ReadULEB128((char *)return_block
->bl_data
+ 1);
970 dwarf_dealloc(dbg
, return_block
, DW_DLA_BLOCK
);
975 if (dwarf_global_formref(return_attr1
, &return_offset
, &error
) == DW_DLV_OK
)
977 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].TypeOffset
= return_offset
;
982 if (dwarf_formstring(return_attr1
, &return_string
, &error
) == DW_DLV_OK
)
984 #ifdef DEBUG_VariableName
985 if (!strcmp(return_string
, DEBUG_VariableName
))
988 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].PtrName
= (char *)calloc(strlen(return_string
) + 1, 1);
989 strcpy(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].PtrName
, return_string
);
991 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
995 case DW_AT_decl_file
:
998 case DW_AT_decl_line
:
1007 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
1010 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
++;
1012 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
1024 while (dwarf_siblingof(dbg
, return_sub
, &return_subdie
, &error
) == DW_DLV_OK
);
1027 PtrCU
[NbCU
].NbSubProgs
++;
1036 while (dwarf_siblingof(dbg
, return_sib
, &return_die
, &error
) == DW_DLV_OK
);
1040 // Set the source code lines
1041 if (PtrCU
[NbCU
].NbLinesLoadSrc
)
1043 if (PtrCU
[NbCU
].PtrLinesLoadSrc
= (char **)calloc(PtrCU
[NbCU
].NbLinesLoadSrc
, sizeof(char *)))
1045 for (size_t j
= 0; j
< PtrCU
[NbCU
].NbLinesLoadSrc
; j
++)
1047 if (PtrCU
[NbCU
].PtrLinesLoadSrc
[j
] = (char *)calloc(10000, sizeof(char)))
1049 if (Ptr
= DWARFManager_GetLineSrcFromNumLine(PtrCU
[NbCU
].PtrLoadSrc
, (j
+ 1)))
1057 #ifdef CONVERT_QT_HML
1059 strcat(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], " ");
1064 strcat(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], "<");
1069 strcat(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], ">");
1074 strcpy(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], "&");
1075 i
+= strlen("&");
1080 strcpy(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], """);
1081 i
+= strlen(""");
1086 PtrCU
[NbCU
].PtrLinesLoadSrc
[j
][i
++] = *Ptr
;
1092 PtrCU
[NbCU
].PtrLinesLoadSrc
[j
] = (char *)realloc(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], strlen(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
]) + 1);
1096 // Init lines source information for each source code line numbers and for each subprogs
1097 for (size_t j
= 0; j
< PtrCU
[NbCU
].NbSubProgs
; j
++)
1099 // Check if the subprog / function's line exists in the source code
1100 if (PtrCU
[NbCU
].PtrSubProgs
[j
].NumLineSrc
<= PtrCU
[NbCU
].NbLinesLoadSrc
)
1102 PtrCU
[NbCU
].PtrSubProgs
[j
].PtrLineSrc
= PtrCU
[NbCU
].PtrLinesLoadSrc
[PtrCU
[NbCU
].PtrSubProgs
[j
].NumLineSrc
- 1];
1105 for (size_t k
= 0; k
< PtrCU
[NbCU
].PtrSubProgs
[j
].NbLinesSrc
; k
++)
1107 if (PtrCU
[NbCU
].PtrSubProgs
[j
].PtrLinesSrc
[k
].NumLineSrc
<= PtrCU
[NbCU
].NbLinesLoadSrc
)
1109 PtrCU
[NbCU
].PtrSubProgs
[j
].PtrLinesSrc
[k
].PtrLineSrc
= PtrCU
[NbCU
].PtrLinesLoadSrc
[PtrCU
[NbCU
].PtrSubProgs
[j
].PtrLinesSrc
[k
].NumLineSrc
- 1];
1117 // Set each source lines pointer to NULL
1118 if (PtrCU
[NbCU
].NbSubProgs
)
1120 // Check the presence of source lines dedicated to the sub progs
1121 if (PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
- 1].NbLinesSrc
)
1123 size_t i
= PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
- 1].PtrLinesSrc
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
- 1].NbLinesSrc
- 1].NumLineSrc
;
1124 if (PtrCU
[NbCU
].PtrLinesLoadSrc
= (char **)calloc(i
, sizeof(char *)))
1126 for (size_t j
= 0; j
< i
; j
++)
1128 PtrCU
[NbCU
].PtrLinesLoadSrc
[j
] = NULL
;
1135 // Check validity between used number lines and number lines in the source file
1136 if (PtrCU
[NbCU
].LastNumUsedLinesSrc
<= PtrCU
[NbCU
].NbLinesLoadSrc
)
1138 // Set information based on used line numbers
1139 if (PtrCU
[NbCU
].PtrUsedLinesSrc
)
1141 // Set the line source pointers for each used line numbers
1142 if (PtrCU
[NbCU
].PtrLinesLoadSrc
)
1144 for (size_t i
= 0; i
< PtrCU
[NbCU
].NbUsedLinesSrc
; i
++)
1146 PtrCU
[NbCU
].PtrUsedNumLines
[i
] = PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].NumLineSrc
- 1;
1147 PtrCU
[NbCU
].PtrUsedLinesLoadSrc
[i
] = PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].PtrLineSrc
= PtrCU
[NbCU
].PtrLinesLoadSrc
[PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].NumLineSrc
- 1];
1150 // Setup memory range for the code if CU doesn't have already this information
1151 // It is taken from the used lines structure
1152 if (!PtrCU
[NbCU
].LowPC
&& (!PtrCU
[NbCU
].HighPC
|| (PtrCU
[NbCU
].HighPC
== ~0)))
1154 PtrCU
[NbCU
].LowPC
= PtrCU
[NbCU
].PtrUsedLinesSrc
[0].StartPC
;
1155 PtrCU
[NbCU
].HighPC
= PtrCU
[NbCU
].PtrUsedLinesSrc
[PtrCU
[NbCU
].NbUsedLinesSrc
- 1].StartPC
;
1161 // Init global variables information based on types information
1162 for (size_t i
= 0; i
< PtrCU
[NbCU
].NbVariables
; i
++)
1164 DWARFManager_InitInfosVariable(PtrCU
[NbCU
].PtrVariables
+ i
);
1167 // Init local variables information based on types information
1168 for (size_t i
= 0; i
< PtrCU
[NbCU
].NbSubProgs
; i
++)
1170 for (size_t j
= 0; j
< PtrCU
[NbCU
].PtrSubProgs
[i
].NbVariables
; j
++)
1172 DWARFManager_InitInfosVariable(PtrCU
[NbCU
].PtrSubProgs
[i
].PtrVariables
+ j
);
1183 // Conform slashes and backslashes
1184 void DWARFManager_ConformSlachesBackslashes(char *Ptr
)
1204 // Variables information initialisation
1205 void DWARFManager_InitInfosVariable(VariablesStruct
*PtrVariables
)
1207 size_t j
, TypeOffset
;
1209 #ifdef DEBUG_VariableName
1210 if (PtrVariables
->PtrName
&& !strcmp(PtrVariables
->PtrName
, DEBUG_VariableName
))
1213 PtrVariables
->PtrTypeName
= (char *)calloc(1000, 1);
1214 TypeOffset
= PtrVariables
->TypeOffset
;
1216 for (j
= 0; j
< PtrCU
[NbCU
].NbTypes
; j
++)
1218 if (TypeOffset
== PtrCU
[NbCU
].PtrTypes
[j
].Offset
)
1220 switch (PtrCU
[NbCU
].PtrTypes
[j
].Tag
)
1222 case DW_TAG_subroutine_type
:
1223 PtrVariables
->TypeTag
|= TypeTag_subroutine_type
;
1224 strcat(PtrVariables
->PtrTypeName
, " (* ) ()");
1227 // Structure type tag
1228 case DW_TAG_structure_type
:
1229 PtrVariables
->TypeTag
|= TypeTag_structure
;
1230 if (!(PtrVariables
->TypeTag
& TypeTag_typedef
))
1232 if (PtrCU
[NbCU
].PtrTypes
[j
].PtrName
)
1234 strcat(PtrVariables
->PtrTypeName
, PtrCU
[NbCU
].PtrTypes
[j
].PtrName
);
1237 if ((TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1243 if ((PtrVariables
->TypeTag
& TypeTag_pointer
))
1245 strcat(PtrVariables
->PtrTypeName
, " *");
1251 case DW_TAG_pointer_type
:
1252 PtrVariables
->TypeTag
|= TypeTag_pointer
;
1253 PtrVariables
->TypeByteSize
= PtrCU
[NbCU
].PtrTypes
[j
].ByteSize
;
1254 PtrVariables
->TypeEncoding
= 0x10;
1255 if (!(TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1257 strcat(PtrVariables
->PtrTypeName
, "void *");
1265 case DW_TAG_enumeration_type
:
1266 PtrVariables
->TypeTag
|= TypeTag_enumeration_type
;
1267 PtrVariables
->TypeByteSize
= PtrCU
[NbCU
].PtrTypes
[j
].ByteSize
;
1268 if (!(PtrVariables
->TypeEncoding
= PtrCU
[NbCU
].PtrTypes
[j
].Encoding
))
1270 // Try to determine the possible size
1271 switch (PtrVariables
->TypeByteSize
)
1274 PtrVariables
->TypeEncoding
= 0x7;
1284 case DW_TAG_typedef
:
1285 if (!(PtrVariables
->TypeTag
& TypeTag_typedef
))
1287 PtrVariables
->TypeTag
|= TypeTag_typedef
;
1288 strcat(PtrVariables
->PtrTypeName
, PtrCU
[NbCU
].PtrTypes
[j
].PtrName
);
1290 if ((TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1297 case DW_TAG_subrange_type
:
1298 PtrVariables
->TypeTag
|= TypeTag_subrange
;
1302 case DW_TAG_array_type
:
1303 PtrVariables
->TypeTag
|= TypeTag_arraytype
;
1304 if ((TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1311 case DW_TAG_const_type
:
1312 PtrVariables
->TypeTag
|= TypeTag_consttype
;
1313 strcat(PtrVariables
->PtrTypeName
, "const ");
1314 if ((TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1321 case DW_TAG_base_type
:
1322 if (!(PtrVariables
->TypeTag
& TypeTag_typedef
))
1324 strcat(PtrVariables
->PtrTypeName
, PtrCU
[NbCU
].PtrTypes
[j
].PtrName
);
1326 if ((PtrVariables
->TypeTag
& TypeTag_pointer
))
1328 strcat(PtrVariables
->PtrTypeName
, " *");
1332 PtrVariables
->TypeByteSize
= PtrCU
[NbCU
].PtrTypes
[j
].ByteSize
;
1333 PtrVariables
->TypeEncoding
= PtrCU
[NbCU
].PtrTypes
[j
].Encoding
;
1335 if ((PtrVariables
->TypeTag
& TypeTag_arraytype
))
1337 strcat(PtrVariables
->PtrTypeName
, "[]");
1350 // Get symbol name based from address
1351 // Return NULL if no symbol name exists
1352 char *DWARFManager_GetSymbolnameFromAdr(size_t Adr
)
1354 for (size_t i
= 0; i
< NbCU
; i
++)
1356 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1358 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1360 if ((PtrCU
[i
].PtrSubProgs
[j
].StartPC
== Adr
))
1362 return PtrCU
[i
].PtrSubProgs
[j
].PtrSubprogramName
;
1372 // Get complete source filename based from address
1373 // Return NULL if no source filename exists
1374 // Return the existence status (true or false) in Error if pointer not NULL
1375 char *DWARFManager_GetFullSourceFilenameFromAdr(size_t Adr
, bool *Error
)
1377 for (size_t i
= 0; i
< NbCU
; i
++)
1379 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1383 *Error
= PtrCU
[i
].PtrLoadSrc
? true : false;
1386 return PtrCU
[i
].PtrFullFilename
;
1394 // Get text line source based on line number (starting from 1)
1395 // Return NULL if no text line exists or if line number is 0
1396 char *DWARFManager_GetLineSrcFromNumLine(char *PtrSrcFile
, size_t NumLine
)
1399 char *PtrLineSrc
= NULL
;
1403 while (i
!= NumLine
)
1405 PtrLineSrc
= PtrSrcFile
;
1406 while (*PtrSrcFile
++);
1415 // Get number of variables referenced by the function range address
1416 size_t DWARFManager_GetNbLocalVariables(size_t Adr
)
1418 for (size_t i
= 0; i
< NbCU
; i
++)
1420 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1422 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1424 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1426 return PtrCU
[i
].PtrSubProgs
[j
].NbVariables
;
1436 // Get local variable name based on his index (starting from 1)
1437 // Return name's pointer text found
1438 // Return NULL if not found
1439 char *DWARFManager_GetLocalVariableName(size_t Adr
, size_t Index
)
1441 for (size_t i
= 0; i
< NbCU
; i
++)
1443 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1445 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1447 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1449 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].PtrName
;
1459 // Get local variable's type tag based on his index (starting from 1)
1460 // Return 0 if not found
1461 size_t DWARFManager_GetLocalVariableTypeTag(size_t Adr
, size_t Index
)
1463 for (size_t i
= 0; i
< NbCU
; i
++)
1465 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1467 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1469 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1471 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].TypeTag
;
1481 // Get the local variable's offset based on a index (starting from 1)
1482 // Return 0 if no offset has been found
1483 int DWARFManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
1485 for (size_t i
= 0; i
< NbCU
; i
++)
1487 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1489 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1491 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1493 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].Offset
;
1503 // Get local variable Type Byte Size based on his address and index (starting from 1)
1504 // Return 0 if not found
1505 // May return 0 if there is no Type Byte Size linked to the variable's address and index
1506 size_t DWARFManager_GetLocalVariableTypeByteSize(size_t Adr
, size_t Index
)
1508 for (size_t i
= 0; i
< NbCU
; i
++)
1510 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1512 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1514 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1516 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].TypeByteSize
;
1526 // Get local variable Type Encoding based on his address and index (starting from 1)
1527 // Return 0 if not found
1528 // May return 0 if there is no Type Encoding linked to the variable's address and index
1529 size_t DWARFManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
1531 for (size_t i
= 0; i
< NbCU
; i
++)
1533 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1535 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1537 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1539 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].TypeEncoding
;
1549 // Get local variable Op based on his address and index (starting from 1)
1550 // Return 0 if not found, may return 0 if there isn't Op linked to the variable's index
1551 size_t DWARFManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
1553 for (size_t i
= 0; i
< NbCU
; i
++)
1555 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1557 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1559 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1561 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].Op
;
1571 // Get local variable type name based on his index (starting from 1) and an address
1572 // Return NULL if not found, may also return NULL if there is no type linked to the variable's index
1573 char *DWARFManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
1575 for (size_t i
= 0; i
< NbCU
; i
++)
1577 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1579 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1581 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1583 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].PtrTypeName
;
1593 // Get Compilation Unit / global variables numbers
1594 // Return number of variables
1595 size_t DWARFManager_GetNbGlobalVariables(void)
1597 size_t NbVariables
= 0;
1599 for (size_t i
= 0; i
< NbCU
; i
++)
1601 NbVariables
+= PtrCU
[i
].NbVariables
;
1608 // Get global variable type name based on his index (starting from 1)
1609 // Return NULL if not found
1610 // May return NULL if there is not type linked to the variable's index
1611 char *DWARFManager_GetGlobalVariableTypeName(size_t Index
)
1613 for (size_t i
= 0; i
< NbCU
; i
++)
1615 if (PtrCU
[i
].NbVariables
)
1617 if (Index
<= PtrCU
[i
].NbVariables
)
1619 return PtrCU
[i
].PtrVariables
[Index
- 1].PtrTypeName
;
1623 Index
-= PtrCU
[i
].NbVariables
;
1632 // Get global variable's type tag based on his index (starting from 1)
1633 // Return 0 if not found
1634 size_t DWARFManager_GetGlobalVariableTypeTag(size_t Index
)
1636 for (size_t i
= 0; i
< NbCU
; i
++)
1638 if (PtrCU
[i
].NbVariables
)
1640 if (Index
<= PtrCU
[i
].NbVariables
)
1642 return PtrCU
[i
].PtrVariables
[Index
- 1].TypeTag
;
1646 Index
-= PtrCU
[i
].NbVariables
;
1655 // Get global variable byte size based on his index (starting from 1)
1656 // Return 0 if not found
1657 size_t DWARFManager_GetGlobalVariableTypeByteSize(size_t Index
)
1659 for (size_t i
= 0; i
< NbCU
; i
++)
1661 if (PtrCU
[i
].NbVariables
)
1663 if (Index
<= PtrCU
[i
].NbVariables
)
1665 return PtrCU
[i
].PtrVariables
[Index
- 1].TypeByteSize
;
1669 Index
-= PtrCU
[i
].NbVariables
;
1678 // Get global variable encoding based on his index (starting from 1)
1679 // Return 0 if not found
1680 size_t DWARFManager_GetGlobalVariableTypeEncoding(size_t Index
)
1682 for (size_t i
= 0; i
< NbCU
; i
++)
1684 if (PtrCU
[i
].NbVariables
)
1686 if (Index
<= PtrCU
[i
].NbVariables
)
1688 return PtrCU
[i
].PtrVariables
[Index
- 1].TypeEncoding
;
1692 Index
-= PtrCU
[i
].NbVariables
;
1701 // Get global variable memory address based on his index (starting from 1)
1702 // Return 0 if not found
1703 size_t DWARFManager_GetGlobalVariableAdr(size_t Index
)
1705 for (size_t i
= 0; i
< NbCU
; i
++)
1707 if (PtrCU
[i
].NbVariables
)
1709 if (Index
<= PtrCU
[i
].NbVariables
)
1711 return PtrCU
[i
].PtrVariables
[Index
- 1].Addr
;
1715 Index
-= PtrCU
[i
].NbVariables
;
1724 // Get global variable memory address based on his name
1725 // Return 0 if not found, or will return the first occurence found
1726 size_t DWARFManager_GetGlobalVariableAdrFromName(char *VariableName
)
1728 for (size_t i
= 0; i
< NbCU
; i
++)
1730 if (PtrCU
[i
].NbVariables
)
1732 for (size_t j
= 0; j
< PtrCU
[i
].NbVariables
; j
++)
1734 if (!strcmp(PtrCU
[i
].PtrVariables
[j
].PtrName
,VariableName
))
1736 return PtrCU
[i
].PtrVariables
[j
].Addr
;
1746 // Get global variable name based on his index (starting from 1)
1747 // Return name's pointer text found, or will return NULL if no variable can be found
1748 char *DWARFManager_GetGlobalVariableName(size_t Index
)
1750 for (size_t i
= 0; i
< NbCU
; i
++)
1752 if (PtrCU
[i
].NbVariables
)
1754 if (Index
<= PtrCU
[i
].NbVariables
)
1756 return PtrCU
[i
].PtrVariables
[Index
- 1].PtrName
;
1760 Index
-= PtrCU
[i
].NbVariables
;
1769 // Get text line from source based on address and his tag
1770 // A tag can be either 0 or a DW_TAG_subprogram
1771 // DW_TAG_subprogram will look for the line pointing to the function
1772 // Return NULL if no text line has been found
1773 char *DWARFManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
1775 for (size_t i
= 0; i
< NbCU
; i
++)
1777 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1779 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1781 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1783 if ((PtrCU
[i
].PtrSubProgs
[j
].StartPC
== Adr
) && (!Tag
|| (Tag
== DW_TAG_subprogram
)))
1785 return PtrCU
[i
].PtrSubProgs
[j
].PtrLineSrc
;
1789 for (size_t k
= 0; k
< PtrCU
[i
].PtrSubProgs
[j
].NbLinesSrc
; k
++)
1791 if (PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].StartPC
<= Adr
)
1793 if ((PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].StartPC
== Adr
) && (!Tag
|| (PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].Tag
== Tag
)))
1795 return PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].PtrLineSrc
;
1800 return PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
- 1].PtrLineSrc
;
1813 // Get line number based on the address and a tag
1814 // A tag can be either 0 or a DW_TAG_subprogram
1815 // DW_TAG_subprogram will look for the line pointing to the function name as described in the source code
1816 // Return 0 if no line number has been found
1817 size_t DWARFManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
1819 for (size_t i
= 0; i
< NbCU
; i
++)
1821 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1823 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1825 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1827 if ((PtrCU
[i
].PtrSubProgs
[j
].StartPC
== Adr
) && (!Tag
|| (Tag
== DW_TAG_subprogram
)))
1829 return PtrCU
[i
].PtrSubProgs
[j
].NumLineSrc
;
1833 for (size_t k
= 0; k
< PtrCU
[i
].PtrSubProgs
[j
].NbLinesSrc
; k
++)
1835 if ((PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].StartPC
== Adr
) && (!Tag
|| (PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].Tag
== Tag
)))
1837 return PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].NumLineSrc
;
1842 if (!Tag
|| (Tag
== DW_TAG_subprogram
))
1844 return PtrCU
[i
].PtrSubProgs
[j
].NumLineSrc
;
1850 // Check if a used line is found with the address
1851 for (size_t j
= 0; j
< PtrCU
[i
].NbUsedLinesSrc
; j
++)
1853 if (PtrCU
[i
].PtrUsedLinesSrc
[j
].StartPC
== Adr
)
1855 return PtrCU
[i
].PtrUsedLinesSrc
[j
].NumLineSrc
;
1865 // Get function name based on an address
1866 // Return NULL if no function name has been found, otherwise will return the function name in the range of the provided address
1867 char *DWARFManager_GetFunctionName(size_t Adr
)
1869 for (size_t i
= 0; i
< NbCU
; i
++)
1871 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1873 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1875 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1877 return PtrCU
[i
].PtrSubProgs
[j
].PtrSubprogramName
;
1887 // Get number of lines of texts source list from source index
1888 size_t DWARFManager_GetSrcNbListPtrFromIndex(size_t Index
, bool Used
)
1892 return PtrCU
[Index
].NbLinesLoadSrc
;
1896 return PtrCU
[Index
].NbUsedLinesSrc
;
1901 // Get text source line number list pointer from source index
1902 // Return NULL for the text source used list
1903 size_t *DWARFManager_GetSrcNumLinesPtrFromIndex(size_t Index
, bool Used
)
1907 return PtrCU
[Index
].PtrUsedNumLines
;
1916 // Get text source list pointers from source index
1917 // Return NULL for the text source used list
1918 char **DWARFManager_GetSrcListPtrFromIndex(size_t Index
, bool Used
)
1922 return PtrCU
[Index
].PtrLinesLoadSrc
;
1926 return PtrCU
[Index
].PtrUsedLinesLoadSrc
;
1931 // Get source language
1932 size_t DWARFManager_GetSrcLanguageFromIndex(size_t Index
)
1934 return PtrCU
[Index
].Language
;
1938 // Get text line from source based on address and num line (starting from 1)
1939 // Return NULL if no text line has been found
1940 char *DWARFManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
1942 for (size_t i
= 0; i
< NbCU
; i
++)
1944 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1946 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1948 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1950 if (PtrCU
[i
].PtrSubProgs
[j
].NumLineSrc
== NumLine
)
1952 return PtrCU
[i
].PtrSubProgs
[j
].PtrLineSrc
;
1956 for (size_t k
= 0; k
< PtrCU
[i
].PtrSubProgs
[j
].NbLinesSrc
; k
++)
1958 if (PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].NumLineSrc
== NumLine
)
1960 return PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].PtrLineSrc
;
1973 // Get text line pointer from source, based on address and line number (starting from 1)
1974 // Return NULL if no text line has been found, or if requested number line is above the source total number of lines
1975 char *DWARFManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
1977 for (size_t i
= 0; i
< NbCU
; i
++)
1979 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1981 if (NumLine
<= PtrCU
[i
].NbLinesLoadSrc
)
1983 return PtrCU
[i
].PtrLinesLoadSrc
[NumLine
- 1];
1996 // Get number of source code filenames
1997 size_t DWARFManager_GetNbSources(void)
2003 // Get source code filename, including his directory, based on index (starting from 0)
2004 char *DWARFManager_GetNumFullSourceFilename(size_t Index
)
2006 return (PtrCU
[Index
].PtrFullFilename
);
2010 // Get source code filename based on index (starting from 0)
2011 char *DWARFManager_GetNumSourceFilename(size_t Index
)
2013 return (PtrCU
[Index
].PtrSourceFilename
);