xref: /haiku/src/kits/debug/SymbolLookup.h (revision b8ded2f89783a220c7b3019d48266a682cc79158)
19a323d35SIngo Weinhold /*
273677f78SIngo Weinhold  * Copyright 2005-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3*196ab88dSRene Gollent  * Copyright 2013, Rene Gollent, rene@gollent.com.
49a323d35SIngo Weinhold  * Distributed under the terms of the MIT License.
59a323d35SIngo Weinhold  */
69a323d35SIngo Weinhold 
773677f78SIngo Weinhold #ifndef SYMBOL_LOOKUP_H
873677f78SIngo Weinhold #define SYMBOL_LOOKUP_H
99a323d35SIngo Weinhold 
109a323d35SIngo Weinhold #include <stdio.h>
119a323d35SIngo Weinhold 
1268e6763bSIngo Weinhold #include <image.h>
139a323d35SIngo Weinhold #include <OS.h>
149a323d35SIngo Weinhold 
159a323d35SIngo Weinhold #include <util/DoublyLinkedList.h>
169a323d35SIngo Weinhold 
1768e6763bSIngo Weinhold 
189a323d35SIngo Weinhold struct image_t;
199a323d35SIngo Weinhold struct runtime_loader_debug_area;
209a323d35SIngo Weinhold 
2168e6763bSIngo Weinhold 
229a323d35SIngo Weinhold namespace BPrivate {
2373677f78SIngo Weinhold namespace Debug {
2473677f78SIngo Weinhold 
2573677f78SIngo Weinhold class Image;
2673677f78SIngo Weinhold 
279a323d35SIngo Weinhold 
289a323d35SIngo Weinhold // Exception
299a323d35SIngo Weinhold class Exception {
309a323d35SIngo Weinhold public:
Exception(status_t error)319a323d35SIngo Weinhold 	Exception(status_t error)
329a323d35SIngo Weinhold 		: fError(error)
339a323d35SIngo Weinhold 	{
349a323d35SIngo Weinhold 	}
359a323d35SIngo Weinhold 
Exception(const Exception & other)369a323d35SIngo Weinhold 	Exception(const Exception &other)
379a323d35SIngo Weinhold 		: fError(other.fError)
389a323d35SIngo Weinhold 	{
399a323d35SIngo Weinhold 	}
409a323d35SIngo Weinhold 
Error()419a323d35SIngo Weinhold 	status_t Error() const	{ return fError; }
429a323d35SIngo Weinhold 
439a323d35SIngo Weinhold private:
449a323d35SIngo Weinhold 	status_t	fError;
459a323d35SIngo Weinhold };
469a323d35SIngo Weinhold 
4768e6763bSIngo Weinhold 
489a323d35SIngo Weinhold // Area
499a323d35SIngo Weinhold class Area : public DoublyLinkedListLinkImpl<Area> {
509a323d35SIngo Weinhold public:
Area(area_id id,const void * address,int32 size)519a323d35SIngo Weinhold 	Area(area_id id, const void *address, int32 size)
529a323d35SIngo Weinhold 		: fRemoteID(id),
539a323d35SIngo Weinhold 		  fLocalID(-1),
549a323d35SIngo Weinhold 		  fRemoteAddress(address),
559a323d35SIngo Weinhold 		  fLocalAddress(NULL),
569a323d35SIngo Weinhold 		  fSize(size)
579a323d35SIngo Weinhold 	{
589a323d35SIngo Weinhold 	}
599a323d35SIngo Weinhold 
~Area()609a323d35SIngo Weinhold 	~Area()
619a323d35SIngo Weinhold 	{
629a323d35SIngo Weinhold 		if (fLocalID >= 0)
639a323d35SIngo Weinhold 			delete_area(fLocalID);
649a323d35SIngo Weinhold 	}
659a323d35SIngo Weinhold 
RemoteAddress()669a323d35SIngo Weinhold 	const void* RemoteAddress() const	{ return fRemoteAddress; }
LocalAddress()679a323d35SIngo Weinhold 	const void* LocalAddress() const	{ return fLocalAddress; }
Size()689a323d35SIngo Weinhold 	int32 Size() const					{ return fSize; }
699a323d35SIngo Weinhold 
ContainsAddress(const void * address,int32 size)709a323d35SIngo Weinhold 	bool ContainsAddress(const void *address, int32 size) const
719a323d35SIngo Weinhold 	{
729a323d35SIngo Weinhold 		return ((addr_t)fRemoteAddress <= (addr_t)address
739a323d35SIngo Weinhold 			&& (addr_t)address + size <= (addr_t)fRemoteAddress + fSize);
749a323d35SIngo Weinhold 	}
759a323d35SIngo Weinhold 
ContainsLocalAddress(const void * address)76abb2df34SIngo Weinhold 	bool ContainsLocalAddress(const void* address) const
77abb2df34SIngo Weinhold 	{
78abb2df34SIngo Weinhold 		return (addr_t)address >= (addr_t)fLocalAddress
79abb2df34SIngo Weinhold 			&& (addr_t)address < (addr_t)fLocalAddress + fSize;
80abb2df34SIngo Weinhold 	}
81abb2df34SIngo Weinhold 
829a323d35SIngo Weinhold 	const void *PrepareAddress(const void *address);
839a323d35SIngo Weinhold 
849a323d35SIngo Weinhold private:
859a323d35SIngo Weinhold 	area_id		fRemoteID;
869a323d35SIngo Weinhold 	area_id		fLocalID;
879a323d35SIngo Weinhold 	const void	*fRemoteAddress;
889a323d35SIngo Weinhold 	void		*fLocalAddress;
899a323d35SIngo Weinhold 	int32		fSize;
909a323d35SIngo Weinhold };
919a323d35SIngo Weinhold 
9268e6763bSIngo Weinhold 
939a323d35SIngo Weinhold // RemoteMemoryAccessor
949a323d35SIngo Weinhold class RemoteMemoryAccessor {
959a323d35SIngo Weinhold public:
969a323d35SIngo Weinhold 	RemoteMemoryAccessor(team_id team);
979a323d35SIngo Weinhold 	~RemoteMemoryAccessor();
989a323d35SIngo Weinhold 
999a323d35SIngo Weinhold 	status_t Init();
1009a323d35SIngo Weinhold 
101abb2df34SIngo Weinhold 	const void *PrepareAddress(const void *remoteAddress, int32 size) const;
102abb2df34SIngo Weinhold 	const void *PrepareAddressNoThrow(const void *remoteAddress,
103abb2df34SIngo Weinhold 		int32 size) const;
1049a323d35SIngo Weinhold 
Read(const Type & remoteData)105abb2df34SIngo Weinhold 	template<typename Type> inline const Type &Read(
106abb2df34SIngo Weinhold 		const Type &remoteData) const
1079a323d35SIngo Weinhold 	{
1089a323d35SIngo Weinhold 		const void *remoteAddress = &remoteData;
1099a323d35SIngo Weinhold 		const void *localAddress = PrepareAddress(remoteAddress,
1109a323d35SIngo Weinhold 			sizeof(remoteData));
1119a323d35SIngo Weinhold 		return *(const Type*)localAddress;
1129a323d35SIngo Weinhold 	}
1139a323d35SIngo Weinhold 
114abb2df34SIngo Weinhold 	Area* AreaForLocalAddress(const void* address) const;
115abb2df34SIngo Weinhold 
1169a323d35SIngo Weinhold private:
117abb2df34SIngo Weinhold 	Area &_FindArea(const void *address, int32 size) const;
118abb2df34SIngo Weinhold 	Area* _FindAreaNoThrow(const void *address, int32 size) const;
1199a323d35SIngo Weinhold 
1209a323d35SIngo Weinhold 	typedef DoublyLinkedList<Area>	AreaList;
1219a323d35SIngo Weinhold 
1229a323d35SIngo Weinhold protected:
1239a323d35SIngo Weinhold 	team_id		fTeam;
1249a323d35SIngo Weinhold 
1259a323d35SIngo Weinhold private:
1269a323d35SIngo Weinhold 	AreaList	fAreas;
1279a323d35SIngo Weinhold };
1289a323d35SIngo Weinhold 
12968e6763bSIngo Weinhold 
13068e6763bSIngo Weinhold // SymbolIterator
13168e6763bSIngo Weinhold struct SymbolIterator {
13273677f78SIngo Weinhold 	const Image*		image;
13368e6763bSIngo Weinhold 	int32				currentIndex;
13468e6763bSIngo Weinhold };
13568e6763bSIngo Weinhold 
13668e6763bSIngo Weinhold 
1379a323d35SIngo Weinhold // SymbolLookup
1389a323d35SIngo Weinhold class SymbolLookup : private RemoteMemoryAccessor {
1399a323d35SIngo Weinhold public:
140*196ab88dSRene Gollent 	SymbolLookup(team_id team, image_id image);
1419a323d35SIngo Weinhold 	~SymbolLookup();
1429a323d35SIngo Weinhold 
1439a323d35SIngo Weinhold 	status_t Init();
1449a323d35SIngo Weinhold 
1459a323d35SIngo Weinhold 	status_t LookupSymbolAddress(addr_t address, addr_t *_baseAddress,
146abb2df34SIngo Weinhold 		const char **_symbolName, size_t *_symbolNameLen,
147abb2df34SIngo Weinhold 		const char **_imageName, bool *_exactMatch) const;
1489a323d35SIngo Weinhold 
149abb2df34SIngo Weinhold 	status_t InitSymbolIterator(image_id imageID,
150abb2df34SIngo Weinhold 		SymbolIterator& iterator) const;
151eb333098SIngo Weinhold 	status_t InitSymbolIteratorByAddress(addr_t address,
152abb2df34SIngo Weinhold 		SymbolIterator& iterator) const;
15368e6763bSIngo Weinhold 	status_t NextSymbol(SymbolIterator& iterator, const char** _symbolName,
154abb2df34SIngo Weinhold 		size_t* _symbolNameLen, addr_t* _symbolAddress, size_t* _symbolSize,
155abb2df34SIngo Weinhold 		int32* _symbolType) const;
15668e6763bSIngo Weinhold 
1579774f385SIngo Weinhold 	status_t GetSymbol(image_id imageID, const char* name, int32 symbolType,
1589774f385SIngo Weinhold 		void** _symbolLocation, size_t* _symbolSize, int32* _symbolType) const;
1599774f385SIngo Weinhold 
1609a323d35SIngo Weinhold private:
16173677f78SIngo Weinhold 	class LoadedImage;
16273677f78SIngo Weinhold 	friend class LoadedImage;
16373677f78SIngo Weinhold 
16473677f78SIngo Weinhold private:
16573677f78SIngo Weinhold 	const image_t* _FindLoadedImageAtAddress(addr_t address) const;
16673677f78SIngo Weinhold 	const image_t* _FindLoadedImageByID(image_id id) const;
16773677f78SIngo Weinhold 	Image* _FindImageAtAddress(addr_t address) const;
16873677f78SIngo Weinhold 	Image* _FindImageByID(image_id id) const;
169abb2df34SIngo Weinhold 	size_t _SymbolNameLen(const char* address) const;
170*196ab88dSRene Gollent 	status_t _LoadImageInfo(const image_info& imageInfo);
1719a323d35SIngo Weinhold 
172abb2df34SIngo Weinhold private:
1739a323d35SIngo Weinhold 	const runtime_loader_debug_area	*fDebugArea;
17473677f78SIngo Weinhold 	DoublyLinkedList<Image>	fImages;
175*196ab88dSRene Gollent 	image_id fImageID;
1769a323d35SIngo Weinhold };
1779a323d35SIngo Weinhold 
17873677f78SIngo Weinhold }	// namespace Debug
1799a323d35SIngo Weinhold }	// namespace BPrivate
1809a323d35SIngo Weinhold 
18173677f78SIngo Weinhold using BPrivate::Debug::SymbolLookup;
1829a323d35SIngo Weinhold 
18373677f78SIngo Weinhold #endif	// SYMBOL_LOOKUP_H
184