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