xref: /haiku/src/kits/debug/SymbolLookup.h (revision eb333098fe361d36e330fe35707a75a21b18686f)
19a323d35SIngo Weinhold /*
268e6763bSIngo Weinhold  * Copyright 2005-2008, Ingo Weinhold, ingo_weinhold@gmx.de.
39a323d35SIngo Weinhold  * Distributed under the terms of the MIT License.
49a323d35SIngo Weinhold  */
59a323d35SIngo Weinhold 
69a323d35SIngo Weinhold #ifndef _SYMBOL_LOOKUP_H
79a323d35SIngo Weinhold #define _SYMBOL_LOOKUP_H
89a323d35SIngo Weinhold 
99a323d35SIngo Weinhold #include <stdio.h>
109a323d35SIngo Weinhold 
1168e6763bSIngo Weinhold #include <image.h>
129a323d35SIngo Weinhold #include <OS.h>
139a323d35SIngo Weinhold 
149a323d35SIngo Weinhold #include <util/DoublyLinkedList.h>
159a323d35SIngo Weinhold 
1668e6763bSIngo Weinhold 
179a323d35SIngo Weinhold //#define TRACE_DEBUG_SYMBOL_LOOKUP
189a323d35SIngo Weinhold #ifdef TRACE_DEBUG_SYMBOL_LOOKUP
199a323d35SIngo Weinhold #	define TRACE(x) printf x
209a323d35SIngo Weinhold #else
219a323d35SIngo Weinhold #	define TRACE(x) ;
229a323d35SIngo Weinhold #endif
239a323d35SIngo Weinhold 
2468e6763bSIngo Weinhold 
259a323d35SIngo Weinhold struct image_t;
269a323d35SIngo Weinhold struct runtime_loader_debug_area;
279a323d35SIngo Weinhold 
2868e6763bSIngo Weinhold 
299a323d35SIngo Weinhold namespace BPrivate {
309a323d35SIngo Weinhold 
319a323d35SIngo Weinhold // Exception
329a323d35SIngo Weinhold class Exception {
339a323d35SIngo Weinhold public:
349a323d35SIngo Weinhold 	Exception(status_t error)
359a323d35SIngo Weinhold 		: fError(error)
369a323d35SIngo Weinhold 	{
379a323d35SIngo Weinhold 	}
389a323d35SIngo Weinhold 
399a323d35SIngo Weinhold 	Exception(const Exception &other)
409a323d35SIngo Weinhold 		: fError(other.fError)
419a323d35SIngo Weinhold 	{
429a323d35SIngo Weinhold 	}
439a323d35SIngo Weinhold 
449a323d35SIngo Weinhold 	status_t Error() const	{ return fError; }
459a323d35SIngo Weinhold 
469a323d35SIngo Weinhold private:
479a323d35SIngo Weinhold 	status_t	fError;
489a323d35SIngo Weinhold };
499a323d35SIngo Weinhold 
5068e6763bSIngo Weinhold 
519a323d35SIngo Weinhold // Area
529a323d35SIngo Weinhold class Area : public DoublyLinkedListLinkImpl<Area> {
539a323d35SIngo Weinhold public:
549a323d35SIngo Weinhold 	Area(area_id id, const void *address, int32 size)
559a323d35SIngo Weinhold 		: fRemoteID(id),
569a323d35SIngo Weinhold 		  fLocalID(-1),
579a323d35SIngo Weinhold 		  fRemoteAddress(address),
589a323d35SIngo Weinhold 		  fLocalAddress(NULL),
599a323d35SIngo Weinhold 		  fSize(size)
609a323d35SIngo Weinhold 	{
619a323d35SIngo Weinhold 	}
629a323d35SIngo Weinhold 
639a323d35SIngo Weinhold 	~Area()
649a323d35SIngo Weinhold 	{
659a323d35SIngo Weinhold 		if (fLocalID >= 0)
669a323d35SIngo Weinhold 			delete_area(fLocalID);
679a323d35SIngo Weinhold 	}
689a323d35SIngo Weinhold 
699a323d35SIngo Weinhold 	const void* RemoteAddress() const	{ return fRemoteAddress; }
709a323d35SIngo Weinhold 	const void* LocalAddress() const	{ return fLocalAddress; }
719a323d35SIngo Weinhold 	int32 Size() const					{ return fSize; }
729a323d35SIngo Weinhold 
739a323d35SIngo Weinhold 	bool ContainsAddress(const void *address, int32 size) const
749a323d35SIngo Weinhold 	{
759a323d35SIngo Weinhold 		return ((addr_t)fRemoteAddress <= (addr_t)address
769a323d35SIngo Weinhold 			&& (addr_t)address + size <= (addr_t)fRemoteAddress + fSize);
779a323d35SIngo Weinhold 	}
789a323d35SIngo Weinhold 
799a323d35SIngo Weinhold 	const void *PrepareAddress(const void *address);
809a323d35SIngo Weinhold 
819a323d35SIngo Weinhold private:
829a323d35SIngo Weinhold 	area_id		fRemoteID;
839a323d35SIngo Weinhold 	area_id		fLocalID;
849a323d35SIngo Weinhold 	const void	*fRemoteAddress;
859a323d35SIngo Weinhold 	void		*fLocalAddress;
869a323d35SIngo Weinhold 	int32		fSize;
879a323d35SIngo Weinhold };
889a323d35SIngo Weinhold 
8968e6763bSIngo Weinhold 
909a323d35SIngo Weinhold // RemoteMemoryAccessor
919a323d35SIngo Weinhold class RemoteMemoryAccessor {
929a323d35SIngo Weinhold public:
939a323d35SIngo Weinhold 	RemoteMemoryAccessor(team_id team);
949a323d35SIngo Weinhold 	~RemoteMemoryAccessor();
959a323d35SIngo Weinhold 
969a323d35SIngo Weinhold 	status_t Init();
979a323d35SIngo Weinhold 
989a323d35SIngo Weinhold 	const void *PrepareAddress(const void *remoteAddress, int32 size);
999a323d35SIngo Weinhold 
1009a323d35SIngo Weinhold 	template<typename Type> inline const Type &Read(const Type &remoteData)
1019a323d35SIngo Weinhold 	{
1029a323d35SIngo Weinhold 		const void *remoteAddress = &remoteData;
1039a323d35SIngo Weinhold 		const void *localAddress = PrepareAddress(remoteAddress,
1049a323d35SIngo Weinhold 			sizeof(remoteData));
1059a323d35SIngo Weinhold 		return *(const Type*)localAddress;
1069a323d35SIngo Weinhold 	}
1079a323d35SIngo Weinhold 
1089a323d35SIngo Weinhold private:
1099a323d35SIngo Weinhold 	Area &_FindArea(const void *address, int32 size);
1109a323d35SIngo Weinhold 
1119a323d35SIngo Weinhold 	typedef DoublyLinkedList<Area>	AreaList;
1129a323d35SIngo Weinhold 
1139a323d35SIngo Weinhold protected:
1149a323d35SIngo Weinhold 	team_id		fTeam;
1159a323d35SIngo Weinhold 
1169a323d35SIngo Weinhold private:
1179a323d35SIngo Weinhold 	AreaList	fAreas;
1189a323d35SIngo Weinhold };
1199a323d35SIngo Weinhold 
12068e6763bSIngo Weinhold 
12168e6763bSIngo Weinhold // SymbolIterator
12268e6763bSIngo Weinhold struct SymbolIterator {
12368e6763bSIngo Weinhold 	const image_t*		image;
12468e6763bSIngo Weinhold 	int32				symbolCount;
12568e6763bSIngo Weinhold 	size_t				textDelta;
12668e6763bSIngo Weinhold 	int32				currentIndex;
12768e6763bSIngo Weinhold };
12868e6763bSIngo Weinhold 
12968e6763bSIngo Weinhold 
1309a323d35SIngo Weinhold // SymbolLookup
1319a323d35SIngo Weinhold class SymbolLookup : private RemoteMemoryAccessor {
1329a323d35SIngo Weinhold public:
1339a323d35SIngo Weinhold 	SymbolLookup(team_id team);
1349a323d35SIngo Weinhold 	~SymbolLookup();
1359a323d35SIngo Weinhold 
1369a323d35SIngo Weinhold 	status_t Init();
1379a323d35SIngo Weinhold 
1389a323d35SIngo Weinhold 	status_t LookupSymbolAddress(addr_t address, addr_t *_baseAddress,
1399a323d35SIngo Weinhold 		const char **_symbolName, const char **_imageName, bool *_exactMatch);
1409a323d35SIngo Weinhold 
14168e6763bSIngo Weinhold 	status_t InitSymbolIterator(image_id imageID, SymbolIterator& iterator);
142*eb333098SIngo Weinhold 	status_t InitSymbolIteratorByAddress(addr_t address,
143*eb333098SIngo Weinhold 		SymbolIterator& iterator);
14468e6763bSIngo Weinhold 	status_t NextSymbol(SymbolIterator& iterator, const char** _symbolName,
14568e6763bSIngo Weinhold 		addr_t* _symbolAddress, size_t* _symbolSize, int32* _symbolType);
14668e6763bSIngo Weinhold 
1479a323d35SIngo Weinhold private:
1489a323d35SIngo Weinhold 	const image_t *_FindImageAtAddress(addr_t address);
14968e6763bSIngo Weinhold 	const image_t *_FindImageByID(image_id id);
1509a323d35SIngo Weinhold 
1519a323d35SIngo Weinhold 	const runtime_loader_debug_area	*fDebugArea;
1529a323d35SIngo Weinhold };
1539a323d35SIngo Weinhold 
1549a323d35SIngo Weinhold }	// namespace BPrivate
1559a323d35SIngo Weinhold 
1569a323d35SIngo Weinhold using BPrivate::SymbolLookup;
1579a323d35SIngo Weinhold 
1589a323d35SIngo Weinhold #endif	// _SYMBOL_LOOKUP_H
159