xref: /haiku/src/kits/debug/SymbolLookup.h (revision 3cb015b1ee509d69c643506e8ff573808c86dcfc)
1 /*
2  * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef _SYMBOL_LOOKUP_H
7 #define _SYMBOL_LOOKUP_H
8 
9 #include <stdio.h>
10 
11 #include <OS.h>
12 
13 #include <util/DoublyLinkedList.h>
14 
15 //#define TRACE_DEBUG_SYMBOL_LOOKUP
16 #ifdef TRACE_DEBUG_SYMBOL_LOOKUP
17 #	define TRACE(x) printf x
18 #else
19 #	define TRACE(x) ;
20 #endif
21 
22 struct image_t;
23 struct runtime_loader_debug_area;
24 
25 namespace BPrivate {
26 
27 // Exception
28 class Exception {
29 public:
30 	Exception(status_t error)
31 		: fError(error)
32 	{
33 	}
34 
35 	Exception(const Exception &other)
36 		: fError(other.fError)
37 	{
38 	}
39 
40 	status_t Error() const	{ return fError; }
41 
42 private:
43 	status_t	fError;
44 };
45 
46 // Area
47 class Area : public DoublyLinkedListLinkImpl<Area> {
48 public:
49 	Area(area_id id, const void *address, int32 size)
50 		: fRemoteID(id),
51 		  fLocalID(-1),
52 		  fRemoteAddress(address),
53 		  fLocalAddress(NULL),
54 		  fSize(size)
55 	{
56 	}
57 
58 	~Area()
59 	{
60 		if (fLocalID >= 0)
61 			delete_area(fLocalID);
62 	}
63 
64 	const void* RemoteAddress() const	{ return fRemoteAddress; }
65 	const void* LocalAddress() const	{ return fLocalAddress; }
66 	int32 Size() const					{ return fSize; }
67 
68 	bool ContainsAddress(const void *address, int32 size) const
69 	{
70 		return ((addr_t)fRemoteAddress <= (addr_t)address
71 			&& (addr_t)address + size <= (addr_t)fRemoteAddress + fSize);
72 	}
73 
74 	const void *PrepareAddress(const void *address);
75 
76 private:
77 	area_id		fRemoteID;
78 	area_id		fLocalID;
79 	const void	*fRemoteAddress;
80 	void		*fLocalAddress;
81 	int32		fSize;
82 };
83 
84 // RemoteMemoryAccessor
85 class RemoteMemoryAccessor {
86 public:
87 	RemoteMemoryAccessor(team_id team);
88 	~RemoteMemoryAccessor();
89 
90 	status_t Init();
91 
92 	const void *PrepareAddress(const void *remoteAddress, int32 size);
93 
94 	template<typename Type> inline const Type &Read(const Type &remoteData)
95 	{
96 		const void *remoteAddress = &remoteData;
97 		const void *localAddress = PrepareAddress(remoteAddress,
98 			sizeof(remoteData));
99 		return *(const Type*)localAddress;
100 	}
101 
102 private:
103 	Area &_FindArea(const void *address, int32 size);
104 
105 	typedef DoublyLinkedList<Area>	AreaList;
106 
107 protected:
108 	team_id		fTeam;
109 
110 private:
111 	AreaList	fAreas;
112 };
113 
114 // SymbolLookup
115 class SymbolLookup : private RemoteMemoryAccessor {
116 public:
117 	SymbolLookup(team_id team);
118 	~SymbolLookup();
119 
120 	status_t Init();
121 
122 	status_t LookupSymbolAddress(addr_t address, addr_t *_baseAddress,
123 		const char **_symbolName, const char **_imageName, bool *_exactMatch);
124 
125 private:
126 	const image_t *_FindImageAtAddress(addr_t address);
127 
128 	const runtime_loader_debug_area	*fDebugArea;
129 };
130 
131 }	// namespace BPrivate
132 
133 using BPrivate::SymbolLookup;
134 
135 #endif	// _SYMBOL_LOOKUP_H
136