xref: /haiku/src/kits/debugger/elf/CoreFile.h (revision 899e0ef82b5624ace2ccfa5f5a58c8ebee54aaef)
1 /*
2  * Copyright 2016, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef CORE_FILE_H
6 #define CORE_FILE_H
7 
8 
9 #include "ElfFile.h"
10 
11 #include <String.h>
12 
13 
14 struct CoreFileTeamInfo {
15 								CoreFileTeamInfo();
16 			void				Init(int32 id, int32 uid, int32 gid,
17 									const BString& args);
18 
19 			int32				Id() const			{ return fId; }
20 			const BString&		Arguments() const	{ return fArgs; }
21 
22 private:
23 			int32				fId;
24 			int32				fUid;
25 			int32				fGid;
26 			BString				fArgs;
27 };
28 
29 
30 struct CoreFileAreaInfo {
31 								CoreFileAreaInfo(ElfSegment* segment, int32 id,
32 									uint64 baseAddress, uint64 size,
33 									uint64 ramSize, uint32 locking,
34 									uint32 protection, const BString& name);
35 
36 			uint64				BaseAddress() const	{ return fBaseAddress; }
37 			uint64				Size() const		{ return fSize; }
38 			uint64				EndAddress() const
39 									{ return fBaseAddress + fSize; }
40 			uint32				Locking() const		{ return fLocking; }
41 			uint32				Protection() const	{ return fProtection; }
42 
43 			ElfSegment*			Segment() const		{ return fSegment; }
44 
45 private:
46 			ElfSegment*			fSegment;
47 			uint64				fBaseAddress;
48 			uint64				fSize;
49 			uint64				fRamSize;
50 			uint32				fLocking;
51 			uint32				fProtection;
52 			int32				fId;
53 			BString				fName;
54 };
55 
56 
57 struct CoreFileSymbolsInfo {
58 								CoreFileSymbolsInfo();
59 								~CoreFileSymbolsInfo();
60 
61 			bool				Init(const void* symbolTable,
62 									uint32 symbolCount,
63 									uint32 symbolTableEntrySize,
64 									const char* stringTable,
65 									uint32 stringTableSize);
66 
67 			const void*			SymbolTable() const	{ return fSymbolTable; }
68 			const char*			StringTable() const	{ return fStringTable; }
69 			uint32				SymbolCount() const	{ return fSymbolCount; }
70 			uint32				SymbolTableEntrySize() const
71 									{ return fSymbolTableEntrySize; }
72 			uint32				StringTableSize() const
73 									{ return fStringTableSize; }
74 
75 private:
76 			void*				fSymbolTable;
77 			char*				fStringTable;
78 			uint32				fSymbolCount;
79 			uint32				fSymbolTableEntrySize;
80 			uint32				fStringTableSize;
81 };
82 
83 
84 struct CoreFileImageInfo {
85 								CoreFileImageInfo(int32 id, int32 type,
86 									uint64 initRoutine, uint64 termRoutine,
87 									uint64 textBase, uint64 textSize,
88 									int64 textDelta,
89 									uint64 dataBase, uint64 dataSize,
90 									int32 deviceId, int64 nodeId,
91 									uint64 symbolTable, uint64 symbolHash,
92 									uint64 stringTable,
93 									CoreFileAreaInfo* textArea,
94 									CoreFileAreaInfo* dataArea,
95 									const BString& name);
96 								~CoreFileImageInfo();
97 
98 			int32				Id() const			{ return fId; }
99 			int32				Type() const		{ return fType; }
100 			uint64				TextBase() const	{ return fTextBase; }
101 			uint64				TextSize() const	{ return fTextSize; }
102 			int64				TextDelta() const	{ return fTextDelta; }
103 			uint64				DataBase() const	{ return fDataBase; }
104 			uint64				DataSize() const	{ return fDataSize; }
105 			uint64				SymbolTable() const	{ return fSymbolTable; }
106 			uint64				SymbolHash() const	{ return fSymbolHash; }
107 			uint64				StringTable() const	{ return fStringTable; }
108 			const BString&		Name() const		{ return fName; }
109 			CoreFileAreaInfo*	TextArea() const	{ return fTextArea; }
110 			CoreFileAreaInfo*	DataArea() const	{ return fDataArea; }
111 
112 			const CoreFileSymbolsInfo* SymbolsInfo() const
113 									{ return fSymbolsInfo; }
114 			void				SetSymbolsInfo(
115 									CoreFileSymbolsInfo* symbolsInfo);
116 
117 private:
118 			int32				fId;
119 			int32				fType;
120 			uint64				fInitRoutine;
121 			uint64				fTermRoutine;
122 			uint64				fTextBase;
123 			uint64				fTextSize;
124 			int64				fTextDelta;
125 			uint64				fDataBase;
126 			uint64				fDataSize;
127 			int32				fDeviceId;
128 			int64				fNodeId;
129 			uint64				fSymbolTable;
130 			uint64				fSymbolHash;
131 			uint64				fStringTable;
132 			CoreFileAreaInfo*	fTextArea;
133 			CoreFileAreaInfo*	fDataArea;
134 			BString				fName;
135 			CoreFileSymbolsInfo* fSymbolsInfo;
136 };
137 
138 
139 struct CoreFileThreadInfo {
140 								CoreFileThreadInfo(int32 id, int32 state,
141 									int32 priority, uint64 stackBase,
142 									uint64 stackEnd, const BString& name);
143 								~CoreFileThreadInfo();
144 
145 			bool				SetCpuState(const void* state, size_t size);
146 			const void*			GetCpuState() const
147 									{ return fCpuState; }
148 			size_t				CpuStateSize() const
149 									{ return fCpuStateSize; }
150 
151 			int32				Id() const		{ return fId; }
152 			const BString&		Name() const	{ return fName; }
153 
154 private:
155 			int32				fId;
156 			int32				fState;
157 			int32				fPriority;
158 			uint64				fStackBase;
159 			uint64				fStackEnd;
160 			BString				fName;
161 			void*				fCpuState;
162 			size_t				fCpuStateSize;
163 };
164 
165 
166 class CoreFile {
167 public:
168 								CoreFile();
169 								~CoreFile();
170 
171 			status_t			Init(const char* fileName);
172 
173 			ElfFile&			GetElfFile()
174 									{ return fElfFile; }
175 
176 			const CoreFileTeamInfo& GetTeamInfo() const
177 									{ return fTeamInfo; }
178 
179 			int32				CountAreaInfos() const
180 									{ return fAreaInfos.CountItems(); }
181 			const CoreFileAreaInfo* AreaInfoAt(int32 index) const
182 									{ return fAreaInfos.ItemAt(index); }
183 			const CoreFileAreaInfo* AreaInfoForAddress(uint64 address) const
184 									{ return _FindArea(address); }
185 
186 			int32				CountImageInfos() const
187 									{ return fImageInfos.CountItems(); }
188 			const CoreFileImageInfo* ImageInfoAt(int32 index) const
189 									{ return fImageInfos.ItemAt(index); }
190 			const CoreFileImageInfo* ImageInfoForId(int32 id) const
191 									{ return _ImageInfoForId(id); }
192 
193 			int32				CountThreadInfos() const
194 									{ return fThreadInfos.CountItems(); }
195 			const CoreFileThreadInfo* ThreadInfoAt(int32 index) const
196 									{ return fThreadInfos.ItemAt(index); }
197 			const CoreFileThreadInfo* ThreadInfoForId(int32 id) const;
198 
199 			status_t			CreateSymbolLookup(
200 									const CoreFileImageInfo* imageInfo,
201 									ElfSymbolLookup*& _lookup);
202 
203 private:
204 			typedef BObjectList<CoreFileAreaInfo> AreaInfoList;
205 			typedef BObjectList<CoreFileImageInfo> ImageInfoList;
206 			typedef BObjectList<CoreFileThreadInfo> ThreadInfoList;
207 
208 private:
209 			template<typename ElfClass>
210 			status_t			_Init();
211 
212 			template<typename ElfClass>
213 			status_t			_ReadNotes();
214 			template<typename ElfClass>
215 			status_t			_ReadNotes(ElfSegment* segment);
216 			template<typename ElfClass>
217 			status_t			_ReadNote(const char* name, uint32 type,
218 									const void* data, uint32 dataSize);
219 			template<typename ElfClass>
220 			status_t			_ReadTeamNote(const void* data,
221 									uint32 dataSize);
222 			template<typename ElfClass>
223 			status_t			_ReadAreasNote(const void* data,
224 									uint32 dataSize);
225 			template<typename ElfClass>
226 			status_t			_ReadImagesNote(const void* data,
227 									uint32 dataSize);
228 			template<typename ElfClass>
229 			status_t			_ReadSymbolsNote(const void* data,
230 									uint32 dataSize);
231 			template<typename ElfClass>
232 			status_t			_ReadThreadsNote(const void* data,
233 									uint32 dataSize);
234 
235 			CoreFileAreaInfo*	_FindArea(uint64 address) const;
236 			ElfSegment*			_FindAreaSegment(uint64 address) const;
237 
238 			CoreFileImageInfo*	_ImageInfoForId(int32 id) const;
239 
240 			template<typename Type>
241 			Type				_ReadValue(const void*& data, uint32& dataSize);
242 			template<typename Entry>
243 			void				_ReadEntry(const void*& data, uint32& dataSize,
244 									Entry& entry, size_t entrySize);
245 			void				_Advance(const void*& data, uint32& dataSize,
246 									size_t by);
247 
248 			template<typename Value>
249 			Value				Get(const Value& value) const
250 									{ return fElfFile.Get(value); }
251 
252 private:
253 			ElfFile				fElfFile;
254 			CoreFileTeamInfo	fTeamInfo;
255 			AreaInfoList		fAreaInfos;
256 			ImageInfoList		fImageInfos;
257 			ThreadInfoList		fThreadInfos;
258 };
259 
260 
261 #endif	// CORE_FILE_H
262