xref: /haiku/src/kits/debugger/dwarf/DwarfFile.h (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 /*
2  * Copyright 2009-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2012-2013, Rene Gollent, rene@gollent.com.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef DWARF_FILE_H
7 #define DWARF_FILE_H
8 
9 
10 #include <ObjectList.h>
11 #include <Referenceable.h>
12 #include <util/DoublyLinkedList.h>
13 #include <util/OpenHashTable.h>
14 
15 #include "DebugInfoEntries.h"
16 #include "TypeUnit.h"
17 
18 
19 struct AbbreviationEntry;
20 class AbbreviationTable;
21 class BVariant;
22 class CfaContext;
23 class CompilationUnit;
24 class DataReader;
25 class DwarfTargetInterface;
26 class ElfFile;
27 class ElfSection;
28 class TargetAddressRangeList;
29 class ValueLocation;
30 
31 
32 class DwarfFile : public BReferenceable,
33 	public DoublyLinkedListLinkImpl<DwarfFile> {
34 public:
35 								DwarfFile();
36 								~DwarfFile();
37 
38 			status_t			StartLoading(const char* fileName,
39 									BString& _requiredExternalFile);
40 			status_t			Load(uint8 addressSize,
41 									const BString& externalFilePath);
42 			status_t			FinishLoading();
43 
44 			const char*			Name() const		{ return fName; }
45 			ElfFile*			GetElfFile() const	{ return fElfFile; }
46 
47 			bool				HasFrameInformation() const
48 									{ return fDebugFrameSection != NULL
49 										|| fEHFrameSection != NULL; }
50 
51 			int32				CountCompilationUnits() const;
52 			CompilationUnit*	CompilationUnitAt(int32 index) const;
53 			CompilationUnit*	CompilationUnitForDIE(
54 									const DebugInfoEntry* entry) const;
55 
56 			TargetAddressRangeList* ResolveRangeList(CompilationUnit* unit,
57 									uint64 offset) const;
58 
59 			status_t			UnwindCallFrame(CompilationUnit* unit,
60 									uint8 addressSize,
61 									DIESubprogram* subprogramEntry,
62 									target_addr_t location,
63 									const DwarfTargetInterface* inputInterface,
64 									DwarfTargetInterface* outputInterface,
65 									target_addr_t& _framePointer);
66 
67 			status_t			EvaluateExpression(CompilationUnit* unit,
68 									uint8 addressSize,
69 									DIESubprogram* subprogramEntry,
70 									const void* expression,
71 									off_t expressionLength,
72 									const DwarfTargetInterface* targetInterface,
73 									target_addr_t instructionPointer,
74 									target_addr_t framePointer,
75 									target_addr_t valueToPush, bool pushValue,
76 									target_addr_t& _result);
77 			status_t			ResolveLocation(CompilationUnit* unit,
78 									uint8 addressSize,
79 									DIESubprogram* subprogramEntry,
80 									const LocationDescription* location,
81 									const DwarfTargetInterface* targetInterface,
82 									target_addr_t instructionPointer,
83 									target_addr_t objectPointer,
84 									bool hasObjectPointer,
85 									target_addr_t framePointer,
86 									target_addr_t relocationDelta,
87 									ValueLocation& _result);
88 									// The returned location will have DWARF
89 									// semantics regarding register numbers and
90 									// bit offsets/sizes (cf. bit pieces).
91 
92 			status_t			EvaluateConstantValue(CompilationUnit* unit,
93 									uint8 addressSize,
94 									DIESubprogram* subprogramEntry,
95 									const ConstantAttributeValue* value,
96 									const DwarfTargetInterface* targetInterface,
97 									target_addr_t instructionPointer,
98 									target_addr_t framePointer,
99 									BVariant& _result);
100 			status_t			EvaluateDynamicValue(CompilationUnit* unit, uint8 addressSize,
101 									DIESubprogram* subprogramEntry,
102 									const DynamicAttributeValue* value,
103 									const DwarfTargetInterface* targetInterface,
104 									target_addr_t instructionPointer,
105 									target_addr_t framePointer,
106 									BVariant& _result, DIEType** _type = NULL);
107 
108 private:
109 			struct ExpressionEvaluationContext;
110 			struct FDEAugmentation;
111 			struct CIEAugmentation;
112 			struct FDELookupInfo;
113 
114 			typedef DoublyLinkedList<AbbreviationTable> AbbreviationTableList;
115 			typedef BObjectList<CompilationUnit> CompilationUnitList;
116 			typedef BOpenHashTable<TypeUnitTableHashDefinition> TypeUnitTable;
117 			typedef BObjectList<FDELookupInfo> FDEInfoList;
118 
119 private:
120 			status_t			_ParseDebugInfoSection();
121 			status_t			_ParseTypesSection();
122 			status_t			_ParseFrameSection(ElfSection* section,
123 									uint8 addressSize, bool ehFrame,
124 									FDEInfoList& infos);
125 			status_t			_ParseCompilationUnit(CompilationUnit* unit);
126 			status_t			_ParseTypeUnit(TypeUnit* unit);
127 			status_t			_ParseDebugInfoEntry(DataReader& dataReader,
128 									BaseUnit* unit,
129 									AbbreviationTable* abbreviationTable,
130 									DebugInfoEntry*& _entry,
131 									bool& _endOfEntryList, int level = 0);
132 			status_t			_FinishUnit(BaseUnit* unit);
133 			status_t			_ParseEntryAttributes(DataReader& dataReader,
134 									BaseUnit* unit,
135 									DebugInfoEntry* entry,
136 									AbbreviationEntry& abbreviationEntry);
137 
138 			status_t			_ParseLineInfoFormatString(CompilationUnit* unit,
139 									DataReader &dataReader,
140 									uint64 format, const char*& value);
141 			status_t			_ParseLineInfoFormatUint(CompilationUnit* unit,
142 									DataReader &dataReader,
143 									uint64 format, uint64 &value);
144 			status_t			_ParseLineInfo(CompilationUnit* unit);
145 
146 			status_t			_UnwindCallFrame(CompilationUnit* unit,
147 									uint8 addressSize,
148 									DIESubprogram* subprogramEntry,
149 									target_addr_t location,
150 									const FDELookupInfo* info,
151 									const DwarfTargetInterface* inputInterface,
152 									DwarfTargetInterface* outputInterface,
153 									target_addr_t& _framePointer);
154 
155 			status_t			_ParseCIEHeader(ElfSection* debugFrameSection,
156 									bool usingEHFrameSection,
157 									CompilationUnit* unit,
158 									uint8 addressSize,
159 									CfaContext& context, off_t cieOffset,
160 									CIEAugmentation& cieAugmentation,
161 									DataReader& reader,
162 									off_t& _cieRemaining);
163 			status_t			_ParseFrameInfoInstructions(
164 									CompilationUnit* unit, CfaContext& context,
165 									DataReader& dataReader,
166 									CIEAugmentation& cieAugmentation);
167 
168 			status_t			_ParsePublicTypesInfo();
169 			status_t			_ParsePublicTypesInfo(DataReader& dataReader,
170 									bool dwarf64);
171 
172 			status_t			_GetAbbreviationTable(off_t offset,
173 									AbbreviationTable*& _table);
174 
175 			DebugInfoEntry*		_ResolveReference(BaseUnit* unit,
176 									uint64 offset,
177 									uint8 refType) const;
178 
179 			status_t			_GetLocationExpression(CompilationUnit* unit,
180 									const LocationDescription* location,
181 									target_addr_t instructionPointer,
182 									const void*& _expression,
183 									off_t& _length) const;
184 			status_t			_FindLocationExpression(CompilationUnit* unit,
185 									uint64 offset, target_addr_t address,
186 									const void*& _expression,
187 									off_t& _length) const;
188 
189 			status_t			_LocateDebugInfo(
190 									BString& _requiredExternalFileName,
191 									const char* locatedFilePath = NULL);
192 
193 			status_t			_GetDebugInfoPath(const char* fileName,
194 									BString& _infoPath) const;
195 
196 			TypeUnitTableEntry*	_GetTypeUnit(uint64 signature) const;
197 			CompilationUnit*	_GetContainingCompilationUnit(
198 									off_t refAddr) const;
199 
200 			FDELookupInfo*		_GetContainingFDEInfo(
201 									target_addr_t offset) const;
202 
203 			FDELookupInfo*		_GetContainingFDEInfo(
204 									target_addr_t offset,
205 									const FDEInfoList& infoList) const;
206 
207 private:
208 			friend struct 		DwarfFile::ExpressionEvaluationContext;
209 
210 private:
211 			char*				fName;
212 			char*				fAlternateName;
213 			ElfFile*			fElfFile;
214 			ElfFile*			fAlternateElfFile;
215 			ElfSection*			fDebugInfoSection;
216 			ElfSection*			fDebugAbbrevSection;
217 			ElfSection*			fDebugStringSection;
218 			ElfSection*			fDebugRangesSection;
219 			ElfSection*			fDebugLineSection;
220 			ElfSection*			fDebugLineStrSection;
221 			ElfSection*			fDebugFrameSection;
222 			ElfSection*			fEHFrameSection;
223 			ElfSection*			fDebugLocationSection;
224 			ElfSection*			fDebugPublicTypesSection;
225 			ElfSection*			fDebugTypesSection;
226 			AbbreviationTableList fAbbreviationTables;
227 			DebugInfoEntryFactory fDebugInfoFactory;
228 			CompilationUnitList	fCompilationUnits;
229 			TypeUnitTable		fTypeUnits;
230 			FDEInfoList			fDebugFrameInfos;
231 			FDEInfoList			fEHFrameInfos;
232 			bool				fTypesSectionRequired;
233 			bool				fFinished;
234 			bool				fItaniumEHFrameFormat;
235 			status_t			fFinishError;
236 };
237 
238 
239 #endif	// DWARF_FILE_H
240