xref: /haiku/src/bin/debug/profile/SharedImage.cpp (revision 7d7de072af2318450eaf85eb67c27121a40b0fa4)
1 /*
2  * Copyright 2008-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "SharedImage.h"
8 
9 #include <stdio.h>
10 
11 #include <algorithm>
12 #include <new>
13 
14 #include <debug_support.h>
15 #include <ObjectList.h>
16 
17 #include "Options.h"
18 
19 
20 SharedImage::SharedImage()
21 	:
22 	fSymbols(NULL),
23 	fSymbolCount(0)
24 {
25 }
26 
27 
28 SharedImage::~SharedImage()
29 {
30 	if (fSymbols != NULL) {
31 		for (int32 i = 0; i < fSymbolCount; i++)
32 			delete fSymbols[i];
33 		delete[] fSymbols;
34 	}
35 }
36 
37 
38 status_t
39 SharedImage::Init(team_id owner, image_id imageID)
40 {
41 	// we need a temporary symbol lookup context
42 	debug_symbol_lookup_context* lookupContext;
43 	status_t error = debug_create_symbol_lookup_context(owner, &lookupContext);
44 	if (error != B_OK) {
45 		fprintf(stderr, "%s: Failed to create symbol lookup context "
46 			"for team %ld: %s\n", kCommandName, owner, strerror(error));
47 		return error;
48 	}
49 
50 	// TODO: Creating a symbol lookup just for loading the symbols of a single
51 	// image is unnecessarily expensive.
52 
53 	// create a symbol iterator
54 	debug_symbol_iterator* iterator;
55 	error = debug_create_image_symbol_iterator(lookupContext, imageID,
56 		&iterator);
57 	if (error != B_OK) {
58 		fprintf(stderr, "Failed to init symbol iterator: %s\n",
59 			strerror(error));
60 		return error;
61 	}
62 
63 	// init
64 	error = _Init(iterator);
65 
66 	// cleanup
67 	debug_delete_symbol_iterator(iterator);
68 	debug_delete_symbol_lookup_context(lookupContext);
69 
70 	return error;
71 }
72 
73 
74 status_t
75 SharedImage::Init(const char* path)
76 {
77 	// create a symbol iterator
78 	debug_symbol_iterator* iterator;
79 	status_t error = debug_create_file_symbol_iterator(path, &iterator);
80 	if (error != B_OK) {
81 		fprintf(stderr, "Failed to init symbol iterator: %s\n",
82 			strerror(error));
83 		return error;
84 	}
85 
86 	error = _Init(iterator);
87 
88 	debug_delete_symbol_iterator(iterator);
89 
90 	return error;
91 }
92 
93 
94 int32
95 SharedImage::FindSymbol(addr_t address) const
96 {
97 	// binary search the function
98 	int32 lower = 0;
99 	int32 upper = fSymbolCount;
100 
101 	while (lower < upper) {
102 		int32 mid = (lower + upper) / 2;
103 		if (address >= fSymbols[mid]->base + fSymbols[mid]->size)
104 			lower = mid + 1;
105 		else
106 			upper = mid;
107 	}
108 
109 	if (lower == fSymbolCount)
110 		return -1;
111 
112 	const Symbol* symbol = fSymbols[lower];
113 	if (address >= symbol->base && address < symbol->base + symbol->size)
114 		return lower;
115 	return -1;
116 }
117 
118 
119 status_t
120 SharedImage::_Init(debug_symbol_iterator* iterator)
121 {
122 	// get an image info
123 	status_t error = debug_get_symbol_iterator_image_info(iterator, &fInfo);
124 	if (error != B_OK)
125 		return error;
126 
127 	// iterate through the symbols
128 	BObjectList<Symbol>	symbols(512, true);
129 	char symbolName[1024];
130 	int32 symbolType;
131 	void* symbolLocation;
132 	size_t symbolSize;
133 	while (debug_next_image_symbol(iterator, symbolName, sizeof(symbolName),
134 			&symbolType, &symbolLocation, &symbolSize) == B_OK) {
135 //		printf("  %s %p (%6lu) %s\n",
136 //			symbolType == B_SYMBOL_TYPE_TEXT ? "text" : "data",
137 //			symbolLocation, symbolSize, symbolName);
138 		if (symbolSize > 0 && symbolType == B_SYMBOL_TYPE_TEXT) {
139 			Symbol* symbol = new(std::nothrow) Symbol(this,
140 				(addr_t)symbolLocation, symbolSize, symbolName);
141 			if (symbol == NULL || !symbols.AddItem(symbol)) {
142 				delete symbol;
143 				fprintf(stderr, "%s: Out of memory\n", kCommandName);
144 				debug_delete_symbol_iterator(iterator);
145 				return B_NO_MEMORY;
146 			}
147 		}
148 	}
149 
150 	// sort the symbols
151 	fSymbolCount = symbols.CountItems();
152 	fSymbols = new(std::nothrow) Symbol*[fSymbolCount];
153 	if (fSymbols == NULL)
154 		return B_NO_MEMORY;
155 
156 	for (int32 i = fSymbolCount - 1; i >= 0 ; i--)
157 		fSymbols[i] = symbols.RemoveItemAt(i);
158 
159 	std::sort(fSymbols, fSymbols + fSymbolCount, SymbolComparator());
160 
161 	return B_OK;
162 }
163