2 // DWARFManager.cpp: DWARF format manager
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 // RG = Richard Goedeken
10 // --- ---------- ------------------------------------------------------------
11 // JPM Dec./2016 Created this file, and added the DWARF format support
12 // JPM Sept./2018 Added LEB128 decoding features, and improve the DWARF parsing information
13 // 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
14 // JPM Aug./2019 Added new functions to handle DWARF information, full filename fix
15 // JPM Mar./2020 Fix a random crash when reading the source lines information
16 // JPM Aug./2020 Added a source code file date check when reading DWARF information
17 // RG Jan./2021 Linux build fixes
21 // To use pointers instead of arrays usage
30 #include <sys/types.h>
35 #include "DWARFManager.h"
37 // Definitions for debugging
38 //#define DEBUG_NumCU 0x3 // CU number to debug or undefine it
39 //#define DEBUG_VariableName "sound_death" // Variable name to look for or undefine it
40 //#define DEBUG_TypeName "Cbuf_Execute" // Type name to look for or undefine it
41 //#define DEBUG_TypeDef DW_TAG_typedef // Type def to look for or undefine it (not supported)
42 //#define DEBUG_Filename "crt0" // Filename to look for or undefine it
44 // Definitions for handling data
45 //#define CONVERT_QT_HML // Text will be converted as HTML
47 // Definitions for the variables's typetag
48 #define TypeTag_structure 0x01 // structure
49 #define TypeTag_pointer 0x02 // pointer
50 #define TypeTag_subrange 0x04 // (subrange_type?)
51 #define TypeTag_arraytype 0x08 // array type
52 #define TypeTag_consttype 0x10 // const type
53 #define TypeTag_typedef 0x20 // typedef
54 #define TypeTag_enumeration_type 0x40 // enumeration
55 #define TypeTag_subroutine_type 0x80 // subroutine
58 // Source line CU structure
59 typedef struct CUStruct_LineSrc
66 // Source line internal structure
67 typedef struct DMIStruct_LineSrc
75 // Enumeration structure
76 typedef struct EnumerationStruct
78 char *PtrName
; // Enumeration's name
79 size_t value
; // Enumeration's value
82 // Structure members structure
83 //typedef struct StructureMembersStruct
85 //}S_StructureMembersStruct;
87 // Base type internal structure
88 typedef struct BaseTypeStruct
90 size_t Tag
; // Type's Tag
91 size_t Offset
; // Type's offset
92 size_t TypeOffset
; // Type's offset on another type
93 size_t ByteSize
; // Type's Byte Size
94 size_t Encoding
; // Type's encoding
95 char *PtrName
; // Type's name
96 size_t NbEnumeration
; // Type's enumeration numbers
97 EnumerationStruct
*PtrEnumeration
; // Type's enumeration
98 // StructureMembersStruct *PtrStructureMembers; // Type's structure members
101 // Variables internal structure
102 typedef struct VariablesStruct
104 size_t Op
; // Variable's DW_OP
107 size_t Addr
; // Variable memory address
108 int Offset
; // Variable stack offset (signed)
110 char *PtrName
; // Variable's name
111 size_t TypeOffset
; // Offset pointing on the Variable's Type
112 size_t TypeByteSize
; // Variable's Type byte size
113 size_t TypeTag
; // Variable's Type Tag
114 size_t TypeEncoding
; // Variable's Type encoding
115 char *PtrTypeName
; // Variable's Type name
118 // Sub program internal structure
119 typedef struct SubProgStruct
124 size_t LowPC
, HighPC
;
127 char *PtrSubprogramName
; // Sub program name
128 size_t NbLinesSrc
; // Number of lines source used by the sub program
129 DMIStruct_LineSrc
*PtrLinesSrc
; // Pointer of the lines source for the sub program
130 size_t NbVariables
; // Variables number
131 VariablesStruct
*PtrVariables
; // Pointer to the local variables list information structure
134 // Compilation Unit internal structure
135 typedef struct CUStruct
138 size_t Language
; // Language (C, etc.) used by the source code
139 size_t LowPC
, HighPC
; // Memory range for the code
140 char *PtrProducer
; // "Producer" text information (mostly compiler and compilation options used)
141 char *PtrSourceFilename
; // Source file name
142 char *PtrSourceFileDirectory
; // Directory of the source file
143 char *PtrFullFilename
; // Pointer to full namefile (directory & filename)
144 size_t SizeLoadSrc
; // Source code text size
145 char *PtrLoadSrc
; // Pointer to the source code text
146 size_t NbLinesLoadSrc
; // Total number of lines in the source code text
147 char **PtrLinesLoadSrc
; // Pointer lists to each source line put in QT html/text conformity
148 size_t NbSubProgs
; // Number of sub programs / routines
149 SubProgStruct
*PtrSubProgs
; // Pointer to the sub programs / routines structure
150 size_t NbTypes
; // Number of types
151 BaseTypeStruct
*PtrTypes
; // Pointer to types
152 size_t NbVariables
; // Variables number
153 VariablesStruct
*PtrVariables
; // Pointer to the global variables list structure
154 size_t NbFrames
; // Frames number
155 size_t NbUsedLinesSrc
; // Number of used source lines
156 size_t LastNumUsedLinesSrc
; // Last number line used
157 CUStruct_LineSrc
*PtrUsedLinesSrc
; // Pointer to the used source lines list structure
158 char **PtrUsedLinesLoadSrc
; // Pointer lists to each used source line referenced by the CUStruct_LineSrc structure
159 size_t *PtrUsedNumLines
; // List of the number lines used
160 struct stat _statbuf
; // File information
161 DWARFstatus Status
; // File status
172 char **ListSearchPaths
;
173 size_t NbSearchPaths
;
174 struct stat FileElfExeInfo
;
178 Dwarf_Handler
DWARFManager_ErrorHandler(Dwarf_Ptr perrarg
);
179 void DWARFManager_InitDMI(void);
180 void DWARFManager_CloseDMI(void);
181 bool DWARFManager_ElfClose(void);
182 char *DWARFManager_GetLineSrcFromNumLine(char *PtrSrcFile
, size_t NumLine
);
183 void DWARFManager_InitInfosVariable(VariablesStruct
*PtrVariables
);
184 void DWARFManager_SourceFileSearchPathsInit(void);
185 void DWARFManager_SourceFileSearchPathsReset(void);
186 void DWARFManager_SourceFileSearchPathsClose(void);
187 void DWARFManager_ConformSlachesBackslashes(char *Ptr
);
191 Dwarf_Handler
DWARFManager_ErrorHandler(Dwarf_Ptr perrarg
)
197 // Dwarf manager list search paths init
198 void DWARFManager_SourceFileSearchPathsInit(void)
200 ListSearchPaths
= NULL
;
205 // Dwarf manager list search paths reset
206 void DWARFManager_SourceFileSearchPathsReset(void)
208 ListSearchPaths
= NULL
;
213 // Dwarf manager list search paths close
214 void DWARFManager_SourceFileSearchPathsClose(void)
216 DWARFManager_SourceFileSearchPathsReset();
220 // Dwarf manager init
221 void DWARFManager_Init(void)
223 DWARFManager_SourceFileSearchPathsInit();
224 LibDwarf
= DW_DLV_NO_ENTRY
;
228 // Dwarf manager settings
229 void DWARFManager_Set(size_t NbPathsInList
, char **PtrListPaths
)
232 ListSearchPaths
= PtrListPaths
;
233 NbSearchPaths
= NbPathsInList
;
237 // Dwarf manager Reset
238 bool DWARFManager_Reset(void)
240 DWARFManager_SourceFileSearchPathsReset();
241 return DWARFManager_ElfClose();
245 // Dwarf manager Close
246 bool DWARFManager_Close(void)
248 DWARFManager_SourceFileSearchPathsClose();
249 return(DWARFManager_Reset());
253 // Dwarf manager Elf init
254 int DWARFManager_ElfInit(Elf
*ElfPtr
, struct stat FileElfInfo
)
256 if ((LibDwarf
= dwarf_elf_init(ElfPtr
, DW_DLC_READ
, (Dwarf_Handler
)DWARFManager_ErrorHandler
, errarg
, &dbg
, &error
)) == DW_DLV_OK
)
258 FileElfExeInfo
= FileElfInfo
;
259 DWARFManager_InitDMI();
266 // Dwarf manager Elf close
267 bool DWARFManager_ElfClose(void)
269 if (LibDwarf
== DW_DLV_OK
)
271 DWARFManager_CloseDMI();
273 if (dwarf_finish(dbg
, &error
) == DW_DLV_OK
)
275 LibDwarf
= DW_DLV_NO_ENTRY
;
290 // Dwarf manager Compilation Units close
291 void DWARFManager_CloseDMI(void)
295 free(PtrCU
[NbCU
].PtrFullFilename
);
296 free(PtrCU
[NbCU
].PtrLoadSrc
);
297 free(PtrCU
[NbCU
].PtrProducer
);
298 free(PtrCU
[NbCU
].PtrSourceFilename
);
299 free(PtrCU
[NbCU
].PtrSourceFileDirectory
);
300 free(PtrCU
[NbCU
].PtrUsedLinesSrc
);
301 free(PtrCU
[NbCU
].PtrUsedLinesLoadSrc
);
302 free(PtrCU
[NbCU
].PtrUsedNumLines
);
304 while (PtrCU
[NbCU
].NbLinesLoadSrc
--)
306 free(PtrCU
[NbCU
].PtrLinesLoadSrc
[PtrCU
[NbCU
].NbLinesLoadSrc
]);
308 free(PtrCU
[NbCU
].PtrLinesLoadSrc
);
310 while (PtrCU
[NbCU
].NbSubProgs
--)
312 while (PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
--)
314 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].PtrName
);
315 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].PtrTypeName
);
317 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
);
319 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
);
320 free(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrSubprogramName
);
322 free(PtrCU
[NbCU
].PtrSubProgs
);
324 while (PtrCU
[NbCU
].NbTypes
--)
326 free(PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].PtrName
);
328 free(PtrCU
[NbCU
].PtrTypes
);
330 while (PtrCU
[NbCU
].NbVariables
--)
332 free(PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
);
333 free(PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrTypeName
);
335 free(PtrCU
[NbCU
].PtrVariables
);
342 // Dwarf manager Compilation Units initialisations
343 void DWARFManager_InitDMI(void)
345 Dwarf_Unsigned next_cu_header
, return_uvalue
;
347 Dwarf_Attribute
*atlist
;
348 Dwarf_Attribute return_attr1
;
349 Dwarf_Half return_tagval
, return_attr
;
350 Dwarf_Addr return_lowpc
, return_highpc
, return_lineaddr
;
351 Dwarf_Block
*return_block
;
352 Dwarf_Signed atcnt
, cnt
;
353 Dwarf_Die return_sib
, return_die
, return_sub
, return_subdie
;
354 Dwarf_Off return_offset
;
360 // Initialisation for the Compilation Units table
364 // loop on the available Compilation Unit
365 while (dwarf_next_cu_header(dbg
, NULL
, NULL
, NULL
, NULL
, &next_cu_header
, &error
) == DW_DLV_OK
)
367 // Allocation of an additional Compilation Unit structure in the table
368 if (Ptr
= (char *)realloc(PtrCU
, ((NbCU
+ 1) * sizeof(CUStruct
))))
370 // Compilation Unit RAZ
371 PtrCU
= (CUStruct
*)Ptr
;
372 memset(PtrCU
+ NbCU
, 0, sizeof(CUStruct
));
376 if (NbCU
== DEBUG_NumCU
)
379 // Get 1st Die from the Compilation Unit
380 if (dwarf_siblingof(dbg
, NULL
, &return_sib
, &error
) == DW_DLV_OK
)
383 if ((dwarf_tag(return_sib
, &return_tagval
, &error
) == DW_DLV_OK
))
385 PtrCU
[NbCU
].Tag
= return_tagval
;
387 // Die type detection
388 switch (return_tagval
)
390 case DW_TAG_compile_unit
:
391 if (dwarf_attrlist(return_sib
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
393 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
395 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
401 if (dwarf_lowpc(return_sib
, &return_lowpc
, &error
) == DW_DLV_OK
)
403 PtrCU
[NbCU
].LowPC
= return_lowpc
;
409 if (dwarf_highpc(return_sib
, &return_highpc
, &error
) == DW_DLV_OK
)
411 PtrCU
[NbCU
].HighPC
= return_highpc
;
415 // compilation information
417 if (dwarf_formstring(atlist
[i
], &return_string
, &error
) == DW_DLV_OK
)
419 PtrCU
[NbCU
].PtrProducer
= (char *)calloc(strlen(return_string
) + 1, 1);
420 strcpy(PtrCU
[NbCU
].PtrProducer
, return_string
);
421 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
427 if (dwarf_formstring(atlist
[i
], &return_string
, &error
) == DW_DLV_OK
)
429 #ifdef DEBUG_Filename
430 if (strstr(return_string
, DEBUG_Filename
))
433 PtrCU
[NbCU
].PtrSourceFilename
= (char *)calloc((strlen(return_string
) + 1), 1);
434 strcpy(PtrCU
[NbCU
].PtrSourceFilename
, return_string
);
436 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
442 if (dwarf_formstring(atlist
[i
], &return_string
, &error
) == DW_DLV_OK
)
444 PtrCU
[NbCU
].PtrSourceFileDirectory
= (char *)calloc((strlen(return_string
) + 1), 1);
445 strcpy(PtrCU
[NbCU
].PtrSourceFileDirectory
, return_string
);
446 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
452 if (dwarf_formudata(atlist
[i
], &return_uvalue
, &error
) == DW_DLV_OK
)
454 PtrCU
[NbCU
].Language
= return_uvalue
;
462 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
464 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
467 // Check filename presence
468 if (!PtrCU
[NbCU
].PtrSourceFilename
)
470 PtrCU
[NbCU
].PtrSourceFilename
= (char *)calloc(1, 1);
473 // Check directory presence
474 if (!PtrCU
[NbCU
].PtrSourceFileDirectory
)
476 // Check if file exists in the search paths
477 for (size_t i
= 0; i
< NbSearchPaths
; i
++)
479 PtrCU
[NbCU
].PtrFullFilename
= (char *)realloc(PtrCU
[NbCU
].PtrFullFilename
, strlen(PtrCU
[NbCU
].PtrSourceFilename
) + strlen((const char *)ListSearchPaths
[i
]) + 2);
481 sprintf(PtrCU
[NbCU
].PtrFullFilename
, "%s\\%s", ListSearchPaths
[i
], PtrCU
[NbCU
].PtrSourceFilename
);
482 if (!fopen_s(&SrcFile
, PtrCU
[NbCU
].PtrFullFilename
, "rb"))
484 sprintf(PtrCU
[NbCU
].PtrFullFilename
, "%s/%s", ListSearchPaths
[i
], PtrCU
[NbCU
].PtrSourceFilename
);
485 SrcFile
= fopen(PtrCU
[NbCU
].PtrFullFilename
, "rb");
489 PtrCU
[NbCU
].PtrSourceFileDirectory
= (char *)realloc(PtrCU
[NbCU
].PtrSourceFileDirectory
, strlen(ListSearchPaths
[i
]) + 1);
490 strcpy(PtrCU
[NbCU
].PtrSourceFileDirectory
, ListSearchPaths
[i
]);
494 // File directory doesn't exits
495 if (!PtrCU
[NbCU
].PtrSourceFileDirectory
)
497 PtrCU
[NbCU
].PtrSourceFileDirectory
= (char *)realloc(PtrCU
[NbCU
].PtrSourceFileDirectory
, 2);
498 strcpy(PtrCU
[NbCU
].PtrSourceFileDirectory
, ".");
502 // Conform slashes / backslashes for the filename
503 DWARFManager_ConformSlachesBackslashes(PtrCU
[NbCU
].PtrSourceFilename
);
505 // Check if filename contains already the complete directory
506 if (PtrCU
[NbCU
].PtrSourceFilename
[1] == ':')
508 // Copy the filename as the full filename
509 PtrCU
[NbCU
].PtrFullFilename
= (char *)realloc(PtrCU
[NbCU
].PtrFullFilename
, strlen(PtrCU
[NbCU
].PtrSourceFilename
) + 1);
510 strcpy(PtrCU
[NbCU
].PtrFullFilename
, PtrCU
[NbCU
].PtrSourceFilename
);
514 // Create full filename and Conform slashes / backslashes
515 PtrCU
[NbCU
].PtrFullFilename
= (char *)realloc(PtrCU
[NbCU
].PtrFullFilename
, strlen(PtrCU
[NbCU
].PtrSourceFilename
) + strlen(PtrCU
[NbCU
].PtrSourceFileDirectory
) + 2);
517 sprintf(PtrCU
[NbCU
].PtrFullFilename
, "%s\\%s", PtrCU
[NbCU
].PtrSourceFileDirectory
, PtrCU
[NbCU
].PtrSourceFilename
);
519 sprintf(PtrCU
[NbCU
].PtrFullFilename
, "%s/%s", PtrCU
[NbCU
].PtrSourceFileDirectory
, PtrCU
[NbCU
].PtrSourceFilename
);
523 DWARFManager_ConformSlachesBackslashes(PtrCU
[NbCU
].PtrFullFilename
);
525 // Directory path clean-up
527 while ((Ptr1
= Ptr
= strstr(PtrCU
[NbCU
].PtrFullFilename
, "\\..\\")))
529 while ((Ptr1
= Ptr
= strstr(PtrCU
[NbCU
].PtrFullFilename
, "/../")))
533 while (*--Ptr1
!= '\\');
535 while (*--Ptr1
!= '/');
537 strcpy((Ptr1
+ 1), (Ptr
+ 4));
540 // Get the source file information
541 if (!stat(PtrCU
[NbCU
].PtrFullFilename
, &PtrCU
[NbCU
]._statbuf
))
543 // check the time stamp with the executable
544 if (PtrCU
[NbCU
]._statbuf
.st_mtime
<= FileElfExeInfo
.st_mtime
)
546 // Open the source file as a binary file
548 if (!fopen_s(&SrcFile
, PtrCU
[NbCU
].PtrFullFilename
, "rb"))
550 SrcFile
= fopen(PtrCU
[NbCU
].PtrFullFilename
, "rb");
554 if (!fseek(SrcFile
, 0, SEEK_END
))
556 if ((PtrCU
[NbCU
].SizeLoadSrc
= ftell(SrcFile
)) > 0)
558 if (!fseek(SrcFile
, 0, SEEK_SET
))
560 if (PtrCU
[NbCU
].PtrLoadSrc
= Ptr
= Ptr1
= (char *)calloc(1, (PtrCU
[NbCU
].SizeLoadSrc
+ 2)))
564 if (fread_s(PtrCU
[NbCU
].PtrLoadSrc
, PtrCU
[NbCU
].SizeLoadSrc
, PtrCU
[NbCU
].SizeLoadSrc
, 1, SrcFile
) != 1)
566 if (fread(PtrCU
[NbCU
].PtrLoadSrc
, PtrCU
[NbCU
].SizeLoadSrc
, 1, SrcFile
) != 1)
569 free(PtrCU
[NbCU
].PtrLoadSrc
);
570 PtrCU
[NbCU
].PtrLoadSrc
= NULL
;
571 PtrCU
[NbCU
].SizeLoadSrc
= 0;
575 // Eliminate all carriage return code '\r' (oxd)
578 if ((*Ptr
= *Ptr1
) != '\r')
584 // Get back the new text file size
585 PtrCU
[NbCU
].SizeLoadSrc
= strlen(Ptr
= PtrCU
[NbCU
].PtrLoadSrc
);
587 // Make sure the text file finish with a new line code '\n' (0xa)
588 if (PtrCU
[NbCU
].PtrLoadSrc
[PtrCU
[NbCU
].SizeLoadSrc
- 1] != '\n')
590 PtrCU
[NbCU
].PtrLoadSrc
[PtrCU
[NbCU
].SizeLoadSrc
++] = '\n';
591 PtrCU
[NbCU
].PtrLoadSrc
[PtrCU
[NbCU
].SizeLoadSrc
] = 0;
594 // Reallocate text file
595 if (PtrCU
[NbCU
].PtrLoadSrc
= Ptr
= (char *)realloc(PtrCU
[NbCU
].PtrLoadSrc
, (PtrCU
[NbCU
].SizeLoadSrc
+ 1)))
597 // Count line numbers, based on the new line code '\n' (0xa), and finish each line with 0
602 PtrCU
[NbCU
].NbLinesLoadSrc
++;
617 // Source file doesn't exist
618 PtrCU
[NbCU
].Status
= DWARFSTATUS_NOFILE
;
623 // Source file is outdated
624 PtrCU
[NbCU
].Status
= DWARFSTATUS_OUTDATEDFILE
;
629 // Source file doesn't have information
630 PtrCU
[NbCU
].Status
= DWARFSTATUS_NOFILEINFO
;
639 // Get the source lines table located in the CU
640 if ((dwarf_srclines(return_sib
, &linebuf
, &cnt
, &error
) == DW_DLV_OK
) && (PtrCU
[NbCU
].Status
== DWARFSTATUS_OK
))
644 PtrCU
[NbCU
].NbUsedLinesSrc
= cnt
;
645 PtrCU
[NbCU
].PtrUsedLinesSrc
= (CUStruct_LineSrc
*)calloc(cnt
, sizeof(CUStruct_LineSrc
));
646 PtrCU
[NbCU
].PtrUsedLinesLoadSrc
= (char **)calloc(cnt
, sizeof(char *));
647 PtrCU
[NbCU
].PtrUsedNumLines
= (size_t *)calloc(cnt
, sizeof(size_t));
649 // Get the addresses and their source line numbers
650 for (Dwarf_Signed i
= 0; i
< cnt
; i
++)
652 if (dwarf_lineaddr(linebuf
[i
], &return_lineaddr
, &error
) == DW_DLV_OK
)
654 // Get the source line number
655 if (dwarf_lineno(linebuf
[i
], &return_uvalue
, &error
) == DW_DLV_OK
)
657 PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].StartPC
= return_lineaddr
;
658 PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].NumLineSrc
= return_uvalue
;
664 // Release the memory used by the source lines table located in the CU
665 dwarf_srclines_dealloc(dbg
, linebuf
, cnt
);
668 // Check if the CU has child
669 if (dwarf_child(return_sib
, &return_die
, &error
) == DW_DLV_OK
)
673 return_sib
= return_die
;
674 if ((dwarf_tag(return_die
, &return_tagval
, &error
) == DW_DLV_OK
))
676 switch (return_tagval
)
678 case DW_TAG_lexical_block
:
681 case DW_TAG_variable
:
682 if (dwarf_attrlist(return_die
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
684 PtrCU
[NbCU
].PtrVariables
= (VariablesStruct
*)realloc(PtrCU
[NbCU
].PtrVariables
, ((PtrCU
[NbCU
].NbVariables
+ 1) * sizeof(VariablesStruct
)));
685 memset(PtrCU
[NbCU
].PtrVariables
+ PtrCU
[NbCU
].NbVariables
, 0, sizeof(VariablesStruct
));
687 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
689 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
691 if (dwarf_attr(return_die
, return_attr
, &return_attr1
, &error
) == DW_DLV_OK
)
696 if (dwarf_formblock(return_attr1
, &return_block
, &error
) == DW_DLV_OK
)
698 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].Op
= (*((unsigned char *)(return_block
->bl_data
)));
700 switch (return_block
->bl_len
)
703 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));
709 dwarf_dealloc(dbg
, return_block
, DW_DLA_BLOCK
);
714 if (dwarf_global_formref(return_attr1
, &return_offset
, &error
) == DW_DLV_OK
)
716 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].TypeOffset
= return_offset
;
722 if (dwarf_formstring(return_attr1
, &return_string
, &error
) == DW_DLV_OK
)
724 #ifdef DEBUG_VariableName
725 if (!strcmp(return_string
, DEBUG_VariableName
))
728 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
= (char *)calloc(strlen(return_string
) + 1, 1);
729 strcpy(PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
, return_string
);
731 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
741 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
744 // Check variable's name validity
745 if (PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
)
747 // Check variable's memory address validity
748 if (PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].Addr
)
751 PtrCU
[NbCU
].NbVariables
++;
756 free(PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
);
757 PtrCU
[NbCU
].PtrVariables
[PtrCU
[NbCU
].NbVariables
].PtrName
= NULL
;
761 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
765 case DW_TAG_base_type
:
767 case DW_TAG_structure_type
:
768 case DW_TAG_pointer_type
:
769 case DW_TAG_const_type
:
770 case DW_TAG_array_type
:
771 case DW_TAG_subrange_type
:
772 case DW_TAG_subroutine_type
:
773 case DW_TAG_enumeration_type
:
774 if (dwarf_attrlist(return_die
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
776 // Allocate memory for this type
777 PtrCU
[NbCU
].PtrTypes
= (BaseTypeStruct
*)realloc(PtrCU
[NbCU
].PtrTypes
, ((PtrCU
[NbCU
].NbTypes
+ 1) * sizeof(BaseTypeStruct
)));
778 memset(PtrCU
[NbCU
].PtrTypes
+ PtrCU
[NbCU
].NbTypes
, 0, sizeof(BaseTypeStruct
));
779 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].Tag
= return_tagval
;
781 if (dwarf_dieoffset(return_die
, &return_offset
, &error
) == DW_DLV_OK
)
783 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].Offset
= return_offset
;
786 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
788 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
790 if (dwarf_attr(return_die
, return_attr
, &return_attr1
, &error
) == DW_DLV_OK
)
798 // Type's type offset
800 if (dwarf_global_formref(return_attr1
, &return_offset
, &error
) == DW_DLV_OK
)
802 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].TypeOffset
= return_offset
;
807 case DW_AT_byte_size
:
808 if (dwarf_formudata(return_attr1
, &return_uvalue
, &error
) == DW_DLV_OK
)
810 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].ByteSize
= return_uvalue
;
816 if (dwarf_formudata(return_attr1
, &return_uvalue
, &error
) == DW_DLV_OK
)
818 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].Encoding
= return_uvalue
;
824 if (dwarf_formstring(return_attr1
, &return_string
, &error
) == DW_DLV_OK
)
826 #ifdef DEBUG_TypeName
827 if (!strcmp(return_string
, DEBUG_TypeName
))
830 PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].PtrName
= (char *)calloc(strlen(return_string
) + 1, 1);
831 strcpy(PtrCU
[NbCU
].PtrTypes
[PtrCU
[NbCU
].NbTypes
].PtrName
, return_string
);
833 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
837 // Type's file number
838 case DW_AT_decl_file
:
841 // Type's line number
842 case DW_AT_decl_line
:
851 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
854 PtrCU
[NbCU
].NbTypes
++;
856 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
860 case DW_TAG_subprogram
:
861 if (dwarf_attrlist(return_die
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
863 PtrCU
[NbCU
].PtrSubProgs
= (SubProgStruct
*)realloc(PtrCU
[NbCU
].PtrSubProgs
, ((PtrCU
[NbCU
].NbSubProgs
+ 1) * sizeof(SubProgStruct
)));
864 memset((void *)(PtrCU
[NbCU
].PtrSubProgs
+ PtrCU
[NbCU
].NbSubProgs
), 0, sizeof(SubProgStruct
));
865 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].Tag
= return_tagval
;
867 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
869 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
871 if (dwarf_attr(return_die
, return_attr
, &return_attr1
, &error
) == DW_DLV_OK
)
877 if (dwarf_lowpc(return_die
, &return_lowpc
, &error
) == DW_DLV_OK
)
879 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].StartPC
= return_lowpc
;
880 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].LowPC
= return_lowpc
;
886 if (dwarf_highpc(return_die
, &return_highpc
, &error
) == DW_DLV_OK
)
888 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].HighPC
= return_highpc
;
893 case DW_AT_decl_line
:
894 if (dwarf_formudata(return_attr1
, &return_uvalue
, &error
) == DW_DLV_OK
)
896 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NumLineSrc
= return_uvalue
;
901 case DW_AT_frame_base
:
902 if (dwarf_formudata(return_attr1
, &return_uvalue
, &error
) == DW_DLV_OK
)
904 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].FrameBase
= return_uvalue
;
905 PtrCU
[NbCU
].NbFrames
++;
911 if (dwarf_formstring(return_attr1
, &return_string
, &error
) == DW_DLV_OK
)
913 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrSubprogramName
= (char *)calloc(strlen(return_string
) + 1, 1);
914 strcpy(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrSubprogramName
, return_string
);
915 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
922 case DW_AT_GNU_all_tail_call_sites
:
928 case DW_AT_prototyped
:
932 case DW_AT_decl_file
:
943 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
945 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
947 // Get source line number and associated block of address
948 for (Dwarf_Signed i
= 0; i
< cnt
; ++i
)
950 // Check the presence of the line in the memory frame
951 if (PtrCU
[NbCU
].PtrUsedLinesSrc
&& (PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].StartPC
>= return_lowpc
) && (PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].StartPC
<= return_highpc
))
953 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
));
954 memset((void *)(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
+ PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
), 0, sizeof(DMIStruct_LineSrc
));
955 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
].StartPC
= PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].StartPC
;
956 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrLinesSrc
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
].NumLineSrc
= PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].NumLineSrc
;
957 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbLinesSrc
++;
961 if (dwarf_child(return_die
, &return_subdie
, &error
) == DW_DLV_OK
)
965 return_sub
= return_subdie
;
966 if ((dwarf_tag(return_subdie
, &return_tagval
, &error
) == DW_DLV_OK
))
968 switch (return_tagval
)
970 case DW_TAG_formal_parameter
:
971 case DW_TAG_variable
:
972 if (dwarf_attrlist(return_subdie
, &atlist
, &atcnt
, &error
) == DW_DLV_OK
)
974 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
)));
975 memset(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
+ PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
, 0, sizeof(VariablesStruct
));
977 for (Dwarf_Signed i
= 0; i
< atcnt
; ++i
)
979 if (dwarf_whatattr(atlist
[i
], &return_attr
, &error
) == DW_DLV_OK
)
981 if (dwarf_attr(return_subdie
, return_attr
, &return_attr1
, &error
) == DW_DLV_OK
)
986 if (dwarf_formblock(return_attr1
, &return_block
, &error
) == DW_DLV_OK
)
988 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].Op
= *((unsigned char *)(return_block
->bl_data
));
990 switch (return_block
->bl_len
)
997 switch (return_tagval
)
999 case DW_TAG_variable
:
1000 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].Offset
= ReadLEB128((char *)return_block
->bl_data
+ 1);
1003 case DW_TAG_formal_parameter
:
1004 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].Offset
= ReadULEB128((char *)return_block
->bl_data
+ 1);
1015 dwarf_dealloc(dbg
, return_block
, DW_DLA_BLOCK
);
1020 if (dwarf_global_formref(return_attr1
, &return_offset
, &error
) == DW_DLV_OK
)
1022 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].TypeOffset
= return_offset
;
1027 if (dwarf_formstring(return_attr1
, &return_string
, &error
) == DW_DLV_OK
)
1029 #ifdef DEBUG_VariableName
1030 if (!strcmp(return_string
, DEBUG_VariableName
))
1033 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].PtrName
= (char *)calloc(strlen(return_string
) + 1, 1);
1034 strcpy(PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].PtrVariables
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
].PtrName
, return_string
);
1036 dwarf_dealloc(dbg
, return_string
, DW_DLA_STRING
);
1040 case DW_AT_decl_file
:
1043 case DW_AT_decl_line
:
1052 dwarf_dealloc(dbg
, atlist
[i
], DW_DLA_ATTR
);
1055 PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
].NbVariables
++;
1057 dwarf_dealloc(dbg
, atlist
, DW_DLA_LIST
);
1069 while (dwarf_siblingof(dbg
, return_sub
, &return_subdie
, &error
) == DW_DLV_OK
);
1072 PtrCU
[NbCU
].NbSubProgs
++;
1081 while (dwarf_siblingof(dbg
, return_sib
, &return_die
, &error
) == DW_DLV_OK
);
1085 // Set the source code lines
1086 if (PtrCU
[NbCU
].NbLinesLoadSrc
)
1088 if (PtrCU
[NbCU
].PtrLinesLoadSrc
= (char **)calloc(PtrCU
[NbCU
].NbLinesLoadSrc
, sizeof(char *)))
1090 for (size_t j
= 0; j
< PtrCU
[NbCU
].NbLinesLoadSrc
; j
++)
1092 if (PtrCU
[NbCU
].PtrLinesLoadSrc
[j
] = (char *)calloc(10000, sizeof(char)))
1094 if (Ptr
= DWARFManager_GetLineSrcFromNumLine(PtrCU
[NbCU
].PtrLoadSrc
, (j
+ 1)))
1102 #ifdef CONVERT_QT_HML
1104 strcat(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], " ");
1109 strcat(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], "<");
1114 strcat(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], ">");
1119 strcpy(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], "&");
1120 i
+= strlen("&");
1125 strcpy(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], """);
1126 i
+= strlen(""");
1131 PtrCU
[NbCU
].PtrLinesLoadSrc
[j
][i
++] = *Ptr
;
1137 PtrCU
[NbCU
].PtrLinesLoadSrc
[j
] = (char *)realloc(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
], strlen(PtrCU
[NbCU
].PtrLinesLoadSrc
[j
]) + 1);
1141 // Init lines source information for each source code line numbers and for each subprogs
1142 for (size_t j
= 0; j
< PtrCU
[NbCU
].NbSubProgs
; j
++)
1144 // Check if the subprog / function's line exists in the source code
1145 if (PtrCU
[NbCU
].PtrSubProgs
[j
].NumLineSrc
<= PtrCU
[NbCU
].NbLinesLoadSrc
)
1147 PtrCU
[NbCU
].PtrSubProgs
[j
].PtrLineSrc
= PtrCU
[NbCU
].PtrLinesLoadSrc
[PtrCU
[NbCU
].PtrSubProgs
[j
].NumLineSrc
- 1];
1150 for (size_t k
= 0; k
< PtrCU
[NbCU
].PtrSubProgs
[j
].NbLinesSrc
; k
++)
1152 if (PtrCU
[NbCU
].PtrSubProgs
[j
].PtrLinesSrc
[k
].NumLineSrc
<= PtrCU
[NbCU
].NbLinesLoadSrc
)
1154 PtrCU
[NbCU
].PtrSubProgs
[j
].PtrLinesSrc
[k
].PtrLineSrc
= PtrCU
[NbCU
].PtrLinesLoadSrc
[PtrCU
[NbCU
].PtrSubProgs
[j
].PtrLinesSrc
[k
].NumLineSrc
- 1];
1162 // Set each source lines pointer to NULL
1163 if (PtrCU
[NbCU
].NbSubProgs
)
1165 // Check the presence of source lines dedicated to the sub progs
1166 if (PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
- 1].NbLinesSrc
)
1168 size_t i
= PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
- 1].PtrLinesSrc
[PtrCU
[NbCU
].PtrSubProgs
[PtrCU
[NbCU
].NbSubProgs
- 1].NbLinesSrc
- 1].NumLineSrc
;
1169 if (PtrCU
[NbCU
].PtrLinesLoadSrc
= (char **)calloc(i
, sizeof(char *)))
1171 for (size_t j
= 0; j
< i
; j
++)
1173 PtrCU
[NbCU
].PtrLinesLoadSrc
[j
] = NULL
;
1180 // Check validity between used number lines and number lines in the source file
1181 if (PtrCU
[NbCU
].LastNumUsedLinesSrc
<= PtrCU
[NbCU
].NbLinesLoadSrc
)
1183 // Set information based on used line numbers
1184 if (PtrCU
[NbCU
].PtrUsedLinesSrc
)
1186 // Set the line source pointers for each used line numbers
1187 if (PtrCU
[NbCU
].PtrLinesLoadSrc
)
1189 for (size_t i
= 0; i
< PtrCU
[NbCU
].NbUsedLinesSrc
; i
++)
1191 PtrCU
[NbCU
].PtrUsedNumLines
[i
] = PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].NumLineSrc
- 1;
1192 PtrCU
[NbCU
].PtrUsedLinesLoadSrc
[i
] = PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].PtrLineSrc
= PtrCU
[NbCU
].PtrLinesLoadSrc
[PtrCU
[NbCU
].PtrUsedLinesSrc
[i
].NumLineSrc
- 1];
1195 // Setup memory range for the code if CU doesn't have already this information
1196 // It is taken from the used lines structure
1197 if (!PtrCU
[NbCU
].LowPC
&& (!PtrCU
[NbCU
].HighPC
|| (PtrCU
[NbCU
].HighPC
== ~0)))
1199 PtrCU
[NbCU
].LowPC
= PtrCU
[NbCU
].PtrUsedLinesSrc
[0].StartPC
;
1200 PtrCU
[NbCU
].HighPC
= PtrCU
[NbCU
].PtrUsedLinesSrc
[PtrCU
[NbCU
].NbUsedLinesSrc
- 1].StartPC
;
1206 // Init global variables information based on types information
1207 for (size_t i
= 0; i
< PtrCU
[NbCU
].NbVariables
; i
++)
1209 DWARFManager_InitInfosVariable(PtrCU
[NbCU
].PtrVariables
+ i
);
1212 // Init local variables information based on types information
1213 for (size_t i
= 0; i
< PtrCU
[NbCU
].NbSubProgs
; i
++)
1215 for (size_t j
= 0; j
< PtrCU
[NbCU
].PtrSubProgs
[i
].NbVariables
; j
++)
1217 DWARFManager_InitInfosVariable(PtrCU
[NbCU
].PtrSubProgs
[i
].PtrVariables
+ j
);
1228 // Conform slashes and backslashes
1229 void DWARFManager_ConformSlachesBackslashes(char *Ptr
)
1249 // Variables information initialisation
1250 void DWARFManager_InitInfosVariable(VariablesStruct
*PtrVariables
)
1252 size_t j
, TypeOffset
;
1254 #ifdef DEBUG_VariableName
1255 if (PtrVariables
->PtrName
&& !strcmp(PtrVariables
->PtrName
, DEBUG_VariableName
))
1258 PtrVariables
->PtrTypeName
= (char *)calloc(1000, 1);
1259 TypeOffset
= PtrVariables
->TypeOffset
;
1261 for (j
= 0; j
< PtrCU
[NbCU
].NbTypes
; j
++)
1263 if (TypeOffset
== PtrCU
[NbCU
].PtrTypes
[j
].Offset
)
1265 switch (PtrCU
[NbCU
].PtrTypes
[j
].Tag
)
1267 case DW_TAG_subroutine_type
:
1268 PtrVariables
->TypeTag
|= TypeTag_subroutine_type
;
1269 strcat(PtrVariables
->PtrTypeName
, " (* ) ()");
1272 // Structure type tag
1273 case DW_TAG_structure_type
:
1274 PtrVariables
->TypeTag
|= TypeTag_structure
;
1275 if (!(PtrVariables
->TypeTag
& TypeTag_typedef
))
1277 if (PtrCU
[NbCU
].PtrTypes
[j
].PtrName
)
1279 strcat(PtrVariables
->PtrTypeName
, PtrCU
[NbCU
].PtrTypes
[j
].PtrName
);
1282 if ((TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1288 if ((PtrVariables
->TypeTag
& TypeTag_pointer
))
1290 strcat(PtrVariables
->PtrTypeName
, " *");
1296 case DW_TAG_pointer_type
:
1297 PtrVariables
->TypeTag
|= TypeTag_pointer
;
1298 PtrVariables
->TypeByteSize
= PtrCU
[NbCU
].PtrTypes
[j
].ByteSize
;
1299 PtrVariables
->TypeEncoding
= 0x10;
1300 if (!(TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1302 strcat(PtrVariables
->PtrTypeName
, "void *");
1310 case DW_TAG_enumeration_type
:
1311 PtrVariables
->TypeTag
|= TypeTag_enumeration_type
;
1312 PtrVariables
->TypeByteSize
= PtrCU
[NbCU
].PtrTypes
[j
].ByteSize
;
1313 if (!(PtrVariables
->TypeEncoding
= PtrCU
[NbCU
].PtrTypes
[j
].Encoding
))
1315 // Try to determine the possible size
1316 switch (PtrVariables
->TypeByteSize
)
1319 PtrVariables
->TypeEncoding
= 0x7;
1329 case DW_TAG_typedef
:
1330 if (!(PtrVariables
->TypeTag
& TypeTag_typedef
))
1332 PtrVariables
->TypeTag
|= TypeTag_typedef
;
1333 strcat(PtrVariables
->PtrTypeName
, PtrCU
[NbCU
].PtrTypes
[j
].PtrName
);
1335 if ((TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1342 case DW_TAG_subrange_type
:
1343 PtrVariables
->TypeTag
|= TypeTag_subrange
;
1347 case DW_TAG_array_type
:
1348 PtrVariables
->TypeTag
|= TypeTag_arraytype
;
1349 if ((TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1356 case DW_TAG_const_type
:
1357 PtrVariables
->TypeTag
|= TypeTag_consttype
;
1358 strcat(PtrVariables
->PtrTypeName
, "const ");
1359 if ((TypeOffset
= PtrCU
[NbCU
].PtrTypes
[j
].TypeOffset
))
1366 case DW_TAG_base_type
:
1367 if (!(PtrVariables
->TypeTag
& TypeTag_typedef
))
1369 strcat(PtrVariables
->PtrTypeName
, PtrCU
[NbCU
].PtrTypes
[j
].PtrName
);
1371 if ((PtrVariables
->TypeTag
& TypeTag_pointer
))
1373 strcat(PtrVariables
->PtrTypeName
, " *");
1377 PtrVariables
->TypeByteSize
= PtrCU
[NbCU
].PtrTypes
[j
].ByteSize
;
1378 PtrVariables
->TypeEncoding
= PtrCU
[NbCU
].PtrTypes
[j
].Encoding
;
1380 if ((PtrVariables
->TypeTag
& TypeTag_arraytype
))
1382 strcat(PtrVariables
->PtrTypeName
, "[]");
1395 // Get symbol name based from address
1396 // Return NULL if no symbol name exists
1397 char *DWARFManager_GetSymbolnameFromAdr(size_t Adr
)
1399 for (size_t i
= 0; i
< NbCU
; i
++)
1401 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1403 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1405 if ((PtrCU
[i
].PtrSubProgs
[j
].StartPC
== Adr
))
1407 return PtrCU
[i
].PtrSubProgs
[j
].PtrSubprogramName
;
1417 // Get complete source filename based from address
1418 // Return NULL if no source filename exists
1419 // Return the existence status in Status if pointer not NULL
1420 char *DWARFManager_GetFullSourceFilenameFromAdr(size_t Adr
, DWARFstatus
*Status
)
1422 for (size_t i
= 0; i
< NbCU
; i
++)
1424 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1428 *Status
= PtrCU
[i
].Status
;
1431 return PtrCU
[i
].PtrFullFilename
;
1439 // Get text line source based on line number (starting from 1)
1440 // Return NULL if no text line exists or if line number is 0
1441 char *DWARFManager_GetLineSrcFromNumLine(char *PtrSrcFile
, size_t NumLine
)
1444 char *PtrLineSrc
= NULL
;
1448 while (i
!= NumLine
)
1450 PtrLineSrc
= PtrSrcFile
;
1451 while (*PtrSrcFile
++);
1460 // Get number of variables referenced by the function range address
1461 size_t DWARFManager_GetNbLocalVariables(size_t Adr
)
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
].NbVariables
;
1481 // Get local variable name based on his index (starting from 1)
1482 // Return name's pointer text found
1483 // Return NULL if not found
1484 char *DWARFManager_GetLocalVariableName(size_t Adr
, size_t Index
)
1486 for (size_t i
= 0; i
< NbCU
; i
++)
1488 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1490 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1492 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1494 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].PtrName
;
1504 // Get local variable's type tag based on his index (starting from 1)
1505 // Return 0 if not found
1506 size_t DWARFManager_GetLocalVariableTypeTag(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].TypeTag
;
1526 // Get the local variable's offset based on a index (starting from 1)
1527 // Return 0 if no offset has been found
1528 int DWARFManager_GetLocalVariableOffset(size_t Adr
, size_t Index
)
1530 for (size_t i
= 0; i
< NbCU
; i
++)
1532 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1534 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1536 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1538 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].Offset
;
1548 // Get local variable Type Byte Size based on his address and index (starting from 1)
1549 // Return 0 if not found
1550 // May return 0 if there is no Type Byte Size linked to the variable's address and index
1551 size_t DWARFManager_GetLocalVariableTypeByteSize(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].TypeByteSize
;
1571 // Get local variable Type Encoding based on his address and index (starting from 1)
1572 // Return 0 if not found
1573 // May return 0 if there is no Type Encoding linked to the variable's address and index
1574 size_t DWARFManager_GetLocalVariableTypeEncoding(size_t Adr
, size_t Index
)
1576 for (size_t i
= 0; i
< NbCU
; i
++)
1578 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1580 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1582 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1584 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].TypeEncoding
;
1594 // Get local variable Op based on his address and index (starting from 1)
1595 // Return 0 if not found, may return 0 if there isn't Op linked to the variable's index
1596 size_t DWARFManager_GetLocalVariableOp(size_t Adr
, size_t Index
)
1598 for (size_t i
= 0; i
< NbCU
; i
++)
1600 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1602 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1604 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1606 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].Op
;
1616 // Get local variable type name based on his index (starting from 1) and an address
1617 // Return NULL if not found, may also return NULL if there is no type linked to the variable's index
1618 char *DWARFManager_GetLocalVariableTypeName(size_t Adr
, size_t Index
)
1620 for (size_t i
= 0; i
< NbCU
; i
++)
1622 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1624 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1626 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1628 return PtrCU
[i
].PtrSubProgs
[j
].PtrVariables
[Index
- 1].PtrTypeName
;
1638 // Get Compilation Unit / global variables numbers
1639 // Return number of variables
1640 size_t DWARFManager_GetNbGlobalVariables(void)
1642 size_t NbVariables
= 0;
1644 for (size_t i
= 0; i
< NbCU
; i
++)
1646 NbVariables
+= PtrCU
[i
].NbVariables
;
1653 // Get global variable type name based on his index (starting from 1)
1654 // Return NULL if not found
1655 // May return NULL if there is not type linked to the variable's index
1656 char *DWARFManager_GetGlobalVariableTypeName(size_t Index
)
1658 for (size_t i
= 0; i
< NbCU
; i
++)
1660 if (PtrCU
[i
].NbVariables
)
1662 if (Index
<= PtrCU
[i
].NbVariables
)
1664 return PtrCU
[i
].PtrVariables
[Index
- 1].PtrTypeName
;
1668 Index
-= PtrCU
[i
].NbVariables
;
1677 // Get global variable's type tag based on his index (starting from 1)
1678 // Return 0 if not found
1679 size_t DWARFManager_GetGlobalVariableTypeTag(size_t Index
)
1681 for (size_t i
= 0; i
< NbCU
; i
++)
1683 if (PtrCU
[i
].NbVariables
)
1685 if (Index
<= PtrCU
[i
].NbVariables
)
1687 return PtrCU
[i
].PtrVariables
[Index
- 1].TypeTag
;
1691 Index
-= PtrCU
[i
].NbVariables
;
1700 // Get global variable byte size based on his index (starting from 1)
1701 // Return 0 if not found
1702 size_t DWARFManager_GetGlobalVariableTypeByteSize(size_t Index
)
1704 for (size_t i
= 0; i
< NbCU
; i
++)
1706 if (PtrCU
[i
].NbVariables
)
1708 if (Index
<= PtrCU
[i
].NbVariables
)
1710 return PtrCU
[i
].PtrVariables
[Index
- 1].TypeByteSize
;
1714 Index
-= PtrCU
[i
].NbVariables
;
1723 // Get global variable encoding based on his index (starting from 1)
1724 // Return 0 if not found
1725 size_t DWARFManager_GetGlobalVariableTypeEncoding(size_t Index
)
1727 for (size_t i
= 0; i
< NbCU
; i
++)
1729 if (PtrCU
[i
].NbVariables
)
1731 if (Index
<= PtrCU
[i
].NbVariables
)
1733 return PtrCU
[i
].PtrVariables
[Index
- 1].TypeEncoding
;
1737 Index
-= PtrCU
[i
].NbVariables
;
1746 // Get global variable memory address based on his index (starting from 1)
1747 // Return 0 if not found
1748 size_t DWARFManager_GetGlobalVariableAdr(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].Addr
;
1760 Index
-= PtrCU
[i
].NbVariables
;
1769 // Get global variable memory address based on his name
1770 // Return 0 if not found, or will return the first occurence found
1771 size_t DWARFManager_GetGlobalVariableAdrFromName(char *VariableName
)
1773 for (size_t i
= 0; i
< NbCU
; i
++)
1775 if (PtrCU
[i
].NbVariables
)
1777 for (size_t j
= 0; j
< PtrCU
[i
].NbVariables
; j
++)
1779 if (!strcmp(PtrCU
[i
].PtrVariables
[j
].PtrName
,VariableName
))
1781 return PtrCU
[i
].PtrVariables
[j
].Addr
;
1791 // Get global variable name based on his index (starting from 1)
1792 // Return name's pointer text found, or will return NULL if no variable can be found
1793 char *DWARFManager_GetGlobalVariableName(size_t Index
)
1795 for (size_t i
= 0; i
< NbCU
; i
++)
1797 if (PtrCU
[i
].NbVariables
)
1799 if (Index
<= PtrCU
[i
].NbVariables
)
1801 return PtrCU
[i
].PtrVariables
[Index
- 1].PtrName
;
1805 Index
-= PtrCU
[i
].NbVariables
;
1814 // Get text line from source based on address and his tag
1815 // A tag can be either 0 or a DW_TAG_subprogram
1816 // DW_TAG_subprogram will look for the line pointing to the function
1817 // Return NULL if no text line has been found
1818 char *DWARFManager_GetLineSrcFromAdr(size_t Adr
, size_t Tag
)
1820 for (size_t i
= 0; i
< NbCU
; i
++)
1822 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1824 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1826 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1828 if ((PtrCU
[i
].PtrSubProgs
[j
].StartPC
== Adr
) && (!Tag
|| (Tag
== DW_TAG_subprogram
)))
1830 return PtrCU
[i
].PtrSubProgs
[j
].PtrLineSrc
;
1834 for (size_t k
= 0; k
< PtrCU
[i
].PtrSubProgs
[j
].NbLinesSrc
; k
++)
1836 if (PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].StartPC
<= Adr
)
1838 if ((PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].StartPC
== Adr
) && (!Tag
|| (PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].Tag
== Tag
)))
1840 return PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].PtrLineSrc
;
1845 return PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
- 1].PtrLineSrc
;
1858 // Get line number based on the address and a tag
1859 // A tag can be either 0 or a DW_TAG_subprogram
1860 // DW_TAG_subprogram will look for the line pointing to the function name as described in the source code
1861 // Return 0 if no line number has been found
1862 size_t DWARFManager_GetNumLineFromAdr(size_t Adr
, size_t Tag
)
1864 for (size_t i
= 0; i
< NbCU
; i
++)
1866 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1868 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1870 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1872 if ((PtrCU
[i
].PtrSubProgs
[j
].StartPC
== Adr
) && (!Tag
|| (Tag
== DW_TAG_subprogram
)))
1874 return PtrCU
[i
].PtrSubProgs
[j
].NumLineSrc
;
1878 for (size_t k
= 0; k
< PtrCU
[i
].PtrSubProgs
[j
].NbLinesSrc
; k
++)
1880 if ((PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].StartPC
== Adr
) && (!Tag
|| (PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].Tag
== Tag
)))
1882 return PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].NumLineSrc
;
1887 if (!Tag
|| (Tag
== DW_TAG_subprogram
))
1889 return PtrCU
[i
].PtrSubProgs
[j
].NumLineSrc
;
1895 // Check if a used line is found with the address
1896 for (size_t j
= 0; j
< PtrCU
[i
].NbUsedLinesSrc
; j
++)
1898 if (PtrCU
[i
].PtrUsedLinesSrc
[j
].StartPC
== Adr
)
1900 return PtrCU
[i
].PtrUsedLinesSrc
[j
].NumLineSrc
;
1910 // Get function name based on an address
1911 // Return NULL if no function name has been found, otherwise will return the function name in the range of the provided address
1912 char *DWARFManager_GetFunctionName(size_t Adr
)
1914 for (size_t i
= 0; i
< NbCU
; i
++)
1916 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1918 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1920 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1922 return PtrCU
[i
].PtrSubProgs
[j
].PtrSubprogramName
;
1932 // Get number of lines of texts source list from source index
1933 size_t DWARFManager_GetSrcNbListPtrFromIndex(size_t Index
, bool Used
)
1937 return PtrCU
[Index
].NbLinesLoadSrc
;
1941 return PtrCU
[Index
].NbUsedLinesSrc
;
1946 // Get text source line number list pointer from source index
1947 // Return NULL for the text source used list
1948 size_t *DWARFManager_GetSrcNumLinesPtrFromIndex(size_t Index
, bool Used
)
1952 return PtrCU
[Index
].PtrUsedNumLines
;
1961 // Get text source list pointers from source index
1962 // Return NULL for the text source used list
1963 char **DWARFManager_GetSrcListPtrFromIndex(size_t Index
, bool Used
)
1967 return PtrCU
[Index
].PtrLinesLoadSrc
;
1971 return PtrCU
[Index
].PtrUsedLinesLoadSrc
;
1976 // Get source language
1977 size_t DWARFManager_GetSrcLanguageFromIndex(size_t Index
)
1979 return PtrCU
[Index
].Language
;
1983 // Get text line from source based on address and num line (starting from 1)
1984 // Return NULL if no text line has been found
1985 char *DWARFManager_GetLineSrcFromAdrNumLine(size_t Adr
, size_t NumLine
)
1987 for (size_t i
= 0; i
< NbCU
; i
++)
1989 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
1991 for (size_t j
= 0; j
< PtrCU
[i
].NbSubProgs
; j
++)
1993 if ((Adr
>= PtrCU
[i
].PtrSubProgs
[j
].LowPC
) && (Adr
< PtrCU
[i
].PtrSubProgs
[j
].HighPC
))
1995 if (PtrCU
[i
].PtrSubProgs
[j
].NumLineSrc
== NumLine
)
1997 return PtrCU
[i
].PtrSubProgs
[j
].PtrLineSrc
;
2001 for (size_t k
= 0; k
< PtrCU
[i
].PtrSubProgs
[j
].NbLinesSrc
; k
++)
2003 if (PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].NumLineSrc
== NumLine
)
2005 return PtrCU
[i
].PtrSubProgs
[j
].PtrLinesSrc
[k
].PtrLineSrc
;
2018 // Get text line pointer from source, based on address and line number (starting from 1)
2019 // Return NULL if no text line has been found, or if requested number line is above the source total number of lines
2020 char *DWARFManager_GetLineSrcFromNumLineBaseAdr(size_t Adr
, size_t NumLine
)
2022 for (size_t i
= 0; i
< NbCU
; i
++)
2024 if ((Adr
>= PtrCU
[i
].LowPC
) && (Adr
< PtrCU
[i
].HighPC
))
2026 if (NumLine
<= PtrCU
[i
].NbLinesLoadSrc
)
2028 return PtrCU
[i
].PtrLinesLoadSrc
[NumLine
- 1];
2041 // Get number of source code filenames
2042 size_t DWARFManager_GetNbSources(void)
2048 // Get source code filename, including his directory, based on index (starting from 0)
2049 char *DWARFManager_GetNumFullSourceFilename(size_t Index
)
2051 return (PtrCU
[Index
].PtrFullFilename
);
2055 // Get source code filename based on index (starting from 0)
2056 char *DWARFManager_GetNumSourceFilename(size_t Index
)
2058 return (PtrCU
[Index
].PtrSourceFilename
);