Debugger: Add dedicated functions for global {un}init.
[haiku.git] / src / apps / debugger / debugger_interface / DebuggerInterface.cpp
blob41e5edd808cb8100aec1ddaeb37d34791c0d6b8f
1 /*
2 * Copyright 2009-2016, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2010-2016, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
5 */
8 #include "DebuggerInterface.h"
10 #include <algorithm>
12 #include <AutoDeleter.h>
14 #include "ElfSymbolLookup.h"
17 // #pragma mark - SymbolTableLookupSource
20 struct DebuggerInterface::SymbolTableLookupSource : ElfSymbolLookupSource {
21 public:
22 SymbolTableLookupSource(const void* symbolTable, size_t symbolTableSize,
23 const char* stringTable, size_t stringTableSize)
25 fSymbolTable((const uint8*)symbolTable),
26 fStringTable(stringTable),
27 fSymbolTableSize(symbolTableSize),
28 fStringTableEnd(symbolTableSize + stringTableSize)
32 virtual ssize_t Read(uint64 address, void* buffer, size_t size)
34 ssize_t copied = 0;
36 if (address > fStringTableEnd)
37 return B_BAD_VALUE;
39 if (address < fSymbolTableSize) {
40 size_t toCopy = std::min(size, size_t(fSymbolTableSize - address));
41 memcpy(buffer, fSymbolTable + address, toCopy);
42 address -= toCopy;
43 size -= toCopy;
44 copied += toCopy;
47 if (address < fStringTableEnd) {
48 size_t toCopy = std::min(size, size_t(fStringTableEnd - address));
49 memcpy(buffer, fStringTable + address - fSymbolTableSize, toCopy);
50 address -= toCopy;
51 size -= toCopy;
52 copied += toCopy;
55 return copied;
58 private:
59 const uint8* fSymbolTable;
60 const char* fStringTable;
61 size_t fSymbolTableSize;
62 size_t fStringTableEnd;
66 // #pragma mark - DebuggerInterface
69 DebuggerInterface::~DebuggerInterface()
74 bool
75 DebuggerInterface::IsPostMortem() const
77 // only true for core file interfaces
78 return false;
82 status_t
83 DebuggerInterface::GetElfSymbols(const char* filePath, int64 textDelta,
84 BObjectList<SymbolInfo>& infos)
86 // open the ELF file
87 ElfFile elfFile;
88 status_t error = elfFile.Init(filePath);
89 if (error != B_OK)
90 return error;
92 // create the symbol lookup
93 ElfSymbolLookup* symbolLookup;
94 error = elfFile.CreateSymbolLookup(textDelta, symbolLookup);
95 if (error != B_OK)
96 return error;
98 ObjectDeleter<ElfSymbolLookup> symbolLookupDeleter(symbolLookup);
100 // get the symbols
101 return GetElfSymbols(symbolLookup, infos);
105 status_t
106 DebuggerInterface::GetElfSymbols(const void* symbolTable, uint32 symbolCount,
107 uint32 symbolTableEntrySize, const char* stringTable,
108 uint32 stringTableSize, bool is64Bit, bool swappedByteOrder,
109 int64 textDelta, BObjectList<SymbolInfo>& infos)
111 size_t symbolTableSize = symbolCount * symbolTableEntrySize;
112 SymbolTableLookupSource* source = new(std::nothrow) SymbolTableLookupSource(
113 symbolTable, symbolTableSize, stringTable, stringTableSize);
114 if (source == NULL)
115 return B_NO_MEMORY;
116 BReference<SymbolTableLookupSource> sourceReference(source, true);
118 ElfSymbolLookup* symbolLookup;
119 status_t error = ElfSymbolLookup::Create(
120 source, 0, 0, symbolTableSize, symbolCount, symbolTableEntrySize,
121 textDelta, is64Bit, swappedByteOrder, false, symbolLookup);
122 if (error != B_OK)
123 return error;
125 ObjectDeleter<ElfSymbolLookup> symbolLookupDeleter(symbolLookup);
127 // get the symbols
128 return GetElfSymbols(symbolLookup, infos);
132 status_t
133 DebuggerInterface::GetElfSymbols(ElfSymbolLookup* symbolLookup,
134 BObjectList<SymbolInfo>& infos)
136 SymbolInfo symbolInfo;
137 uint32 index = 0;
138 while (symbolLookup->NextSymbolInfo(index, symbolInfo) == B_OK) {
139 SymbolInfo* info = new(std::nothrow) SymbolInfo(symbolInfo);
140 if (info == NULL || !infos.AddItem(info)) {
141 delete info;
142 return B_NO_MEMORY;
146 return B_OK;