2 * Copyright 2008-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "SharedImage.h"
14 #include <debug_support.h>
15 #include <ObjectList.h>
20 SharedImage::SharedImage()
28 SharedImage::~SharedImage()
30 if (fSymbols
!= NULL
) {
31 for (int32 i
= 0; i
< fSymbolCount
; i
++)
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
,
46 fprintf(stderr
, "%s: Failed to create symbol lookup context "
47 "for team %ld: %s\n", kCommandName
, owner
, strerror(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
,
59 fprintf(stderr
, "Failed to init symbol iterator for image %ld: %s\n",
60 imageID
, strerror(error
));
61 debug_delete_symbol_lookup_context(lookupContext
);
66 error
= _Init(iterator
);
69 debug_delete_symbol_iterator(iterator
);
70 debug_delete_symbol_lookup_context(lookupContext
);
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
);
83 fprintf(stderr
, "Failed to init symbol iterator for \"%s\": %s\n",
84 path
, strerror(error
));
88 error
= _Init(iterator
);
90 debug_delete_symbol_iterator(iterator
);
97 SharedImage::FindSymbol(addr_t address
) const
99 // binary search the function
101 int32 upper
= fSymbolCount
;
103 while (lower
< upper
) {
104 int32 mid
= (lower
+ upper
) / 2;
105 if (address
>= fSymbols
[mid
]->base
+ fSymbols
[mid
]->size
)
111 if (lower
== fSymbolCount
)
114 const Symbol
* symbol
= fSymbols
[lower
];
115 if (address
>= symbol
->base
&& address
< symbol
->base
+ symbol
->size
)
122 SharedImage::_Init(debug_symbol_iterator
* iterator
)
125 status_t error
= debug_get_symbol_iterator_image_info(iterator
, &fInfo
);
129 // iterate through the symbols
130 BObjectList
<Symbol
> symbols(512, true);
131 char symbolName
[1024];
133 void* symbolLocation
;
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
)) {
145 fprintf(stderr
, "%s: Out of memory\n", kCommandName
);
152 fSymbolCount
= symbols
.CountItems();
153 fSymbols
= new(std::nothrow
) Symbol
*[fSymbolCount
];
154 if (fSymbols
== NULL
)
157 for (int32 i
= fSymbolCount
- 1; i
>= 0 ; i
--)
158 fSymbols
[i
] = symbols
.RemoveItemAt(i
);
160 std::sort(fSymbols
, fSymbols
+ fSymbolCount
, SymbolComparator());