LLDB mainline
HexagonDYLDRendezvous.cpp
Go to the documentation of this file.
1//===-- HexagonDYLDRendezvous.cpp -----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Core/Module.h"
11#include "lldb/Symbol/Symbol.h"
13#include "lldb/Target/Process.h"
14#include "lldb/Target/Target.h"
15#include "lldb/Utility/Log.h"
16#include "lldb/Utility/Status.h"
17
19
20using namespace lldb;
21using namespace lldb_private;
22
23/// Locates the address of the rendezvous structure. Returns the address on
24/// success and LLDB_INVALID_ADDRESS on failure.
26 addr_t info_location;
27 addr_t info_addr;
29
30 info_location = process->GetImageInfoAddress();
31
32 if (info_location == LLDB_INVALID_ADDRESS)
34
35 info_addr = process->ReadPointerFromMemory(info_location, error);
36 if (error.Fail())
38
39 if (info_addr == 0)
41
42 return info_addr;
43}
44
48 m_thread_info.valid = false;
49 m_thread_info.dtv_offset = 0;
50 m_thread_info.dtv_slot_size = 0;
51 m_thread_info.modid_offset = 0;
52 m_thread_info.tls_offset = 0;
53
54 // Cache a copy of the executable path
55 if (m_process) {
56 Module *exe_mod = m_process->GetTarget().GetExecutableModulePointer();
57 if (exe_mod)
59 }
60}
61
63 const size_t word_size = 4;
64 Rendezvous info;
65 size_t address_size;
66 size_t padding;
67 addr_t info_addr;
68 addr_t cursor;
69
70 address_size = m_process->GetAddressByteSize();
71 padding = address_size - word_size;
72
74 cursor = info_addr = ResolveRendezvousAddress(m_process);
75 else
76 cursor = info_addr = m_rendezvous_addr;
77
78 if (cursor == LLDB_INVALID_ADDRESS)
79 return false;
80
81 if (!(cursor = ReadWord(cursor, &info.version, word_size)))
82 return false;
83
84 if (!(cursor = ReadPointer(cursor + padding, &info.map_addr)))
85 return false;
86
87 if (!(cursor = ReadPointer(cursor, &info.brk)))
88 return false;
89
90 if (!(cursor = ReadWord(cursor, &info.state, word_size)))
91 return false;
92
93 if (!(cursor = ReadPointer(cursor + padding, &info.ldbase)))
94 return false;
95
96 // The rendezvous was successfully read. Update our internal state.
97 m_rendezvous_addr = info_addr;
99 m_current = info;
100
101 return UpdateSOEntries();
102}
103
107
111
113 SOEntry entry;
114
115 if (m_current.map_addr == 0)
116 return false;
117
118 // When the previous and current states are consistent this is the first time
119 // we have been asked to update. Just take a snapshot of the currently
120 // loaded modules.
121 if (m_previous.state == eConsistent && m_current.state == eConsistent)
123
124 // If we are about to add or remove a shared object clear out the current
125 // state and take a snapshot of the currently loaded images.
126 if (m_current.state == eAdd || m_current.state == eDelete) {
127 // this is a fudge so that we can clear the assert below.
128 m_previous.state = eConsistent;
129 // We hit this assert on the 2nd run of this function after running the
130 // calc example
131 assert(m_previous.state == eConsistent);
132 m_soentries.clear();
133 m_added_soentries.clear();
134 m_removed_soentries.clear();
136 }
137 assert(m_current.state == eConsistent);
138
139 // Otherwise check the previous state to determine what to expect and update
140 // accordingly.
141 if (m_previous.state == eAdd)
143 else if (m_previous.state == eDelete)
145
146 return false;
147}
148
150 SOEntry entry;
151
152 assert(m_previous.state == eAdd);
153
154 if (m_current.map_addr == 0)
155 return false;
156
157 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next) {
158 if (!ReadSOEntryFromMemory(cursor, entry))
159 return false;
160
161 // Only add shared libraries and not the executable. On Linux this is
162 // indicated by an empty path in the entry. On FreeBSD it is the name of
163 // the executable.
164 if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)
165 continue;
166
167 if (!llvm::is_contained(m_soentries, entry)) {
168 m_soentries.push_back(entry);
169 m_added_soentries.push_back(entry);
170 }
171 }
172
173 return true;
174}
175
177 SOEntryList entry_list;
178
179 assert(m_previous.state == eDelete);
180
181 if (!TakeSnapshot(entry_list))
182 return false;
183
184 for (iterator I = begin(); I != end(); ++I) {
185 if (!llvm::is_contained(entry_list, *I))
186 m_removed_soentries.push_back(*I);
187 }
188
189 m_soentries = entry_list;
190 return true;
191}
192
194 SOEntry entry;
195
196 if (m_current.map_addr == 0)
197 return false;
198
199 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next) {
200 if (!ReadSOEntryFromMemory(cursor, entry))
201 return false;
202
203 // Only add shared libraries and not the executable. On Linux this is
204 // indicated by an empty path in the entry. On FreeBSD it is the name of
205 // the executable.
206 if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)
207 continue;
208
209 entry_list.push_back(entry);
210 }
211
212 return true;
213}
214
216 size_t size) {
218
219 *dst = m_process->ReadUnsignedIntegerFromMemory(addr, size, 0, error);
220 if (error.Fail())
221 return 0;
222
223 return addr + size;
224}
225
228
229 *dst = m_process->ReadPointerFromMemory(addr, error);
230 if (error.Fail())
231 return 0;
232
233 return addr + m_process->GetAddressByteSize();
234}
235
237 std::string str;
239 size_t size;
240 char c;
241
242 if (addr == LLDB_INVALID_ADDRESS)
243 return std::string();
244
245 for (;;) {
246 size = m_process->ReadMemory(addr, &c, 1, error);
247 if (size != 1 || error.Fail())
248 return std::string();
249 if (c == 0)
250 break;
251 else {
252 str.push_back(c);
253 addr++;
254 }
255 }
256
257 return str;
258}
259
261 SOEntry &entry) {
262 entry.clear();
263 entry.link_addr = addr;
264
265 if (!(addr = ReadPointer(addr, &entry.base_addr)))
266 return false;
267
268 if (!(addr = ReadPointer(addr, &entry.path_addr)))
269 return false;
270
271 if (!(addr = ReadPointer(addr, &entry.dyn_addr)))
272 return false;
273
274 if (!(addr = ReadPointer(addr, &entry.next)))
275 return false;
276
277 if (!(addr = ReadPointer(addr, &entry.prev)))
278 return false;
279
280 entry.path = ReadStringFromMemory(entry.path_addr);
281
282 return true;
283}
284
286 uint32_t &value) {
287 Target &target = m_process->GetTarget();
288
291 eSymbolTypeAny, list);
292 if (list.IsEmpty())
293 return false;
294
295 Address address = list[0].symbol->GetAddress();
296 addr_t addr = address.GetLoadAddress(&target);
297 if (addr == LLDB_INVALID_ADDRESS)
298 return false;
299
301 value = (uint32_t)m_process->ReadUnsignedIntegerFromMemory(
302 addr + field * sizeof(uint32_t), sizeof(uint32_t), 0, error);
303 if (error.Fail())
304 return false;
305
306 if (field == eSize)
307 value /= 8; // convert bits to bytes
308
309 return true;
310}
311
314 if (!m_thread_info.valid) {
315 bool ok = true;
316
317 ok &= FindMetadata("_thread_db_pthread_dtvp", eOffset,
318 m_thread_info.dtv_offset);
319 ok &=
320 FindMetadata("_thread_db_dtv_dtv", eSize, m_thread_info.dtv_slot_size);
321 ok &= FindMetadata("_thread_db_link_map_l_tls_modid", eOffset,
322 m_thread_info.modid_offset);
323 ok &= FindMetadata("_thread_db_dtv_t_pointer_val", eOffset,
324 m_thread_info.tls_offset);
325
326 if (ok)
327 m_thread_info.valid = true;
328 }
329
330 return m_thread_info;
331}
332
334 int state = GetState();
335
336 if (!log)
337 return;
338
339 log->PutCString("HexagonDYLDRendezvous:");
340 LLDB_LOGF(log, " Address: %" PRIx64, GetRendezvousAddress());
341 LLDB_LOGF(log, " Version: %" PRIu64, GetVersion());
342 LLDB_LOGF(log, " Link : %" PRIx64, GetLinkMapAddress());
343 LLDB_LOGF(log, " Break : %" PRIx64, GetBreakAddress());
344 LLDB_LOGF(log, " LDBase : %" PRIx64, GetLDBase());
345 LLDB_LOGF(log, " State : %s",
346 (state == eConsistent)
347 ? "consistent"
348 : (state == eAdd) ? "add"
349 : (state == eDelete) ? "delete" : "unknown");
350
351 iterator I = begin();
352 iterator E = end();
353
354 if (I != E)
355 log->PutCString("HexagonDYLDRendezvous SOEntries:");
356
357 for (int i = 1; I != E; ++I, ++i) {
358 LLDB_LOGF(log, "\n SOEntry [%d] %s", i, I->path.c_str());
359 LLDB_LOGF(log, " Base : %" PRIx64, I->base_addr);
360 LLDB_LOGF(log, " Path : %" PRIx64, I->path_addr);
361 LLDB_LOGF(log, " Dyn : %" PRIx64, I->dyn_addr);
362 LLDB_LOGF(log, " Next : %" PRIx64, I->next);
363 LLDB_LOGF(log, " Prev : %" PRIx64, I->prev);
364 }
365}
static const size_t word_size
static llvm::raw_ostream & error(Stream &strm)
static addr_t ResolveRendezvousAddress(Process *process)
Locates the address of the rendezvous structure.
#define LLDB_LOGF(log,...)
Definition Log.h:378
Rendezvous m_current
Current and previous snapshots of the rendezvous structure.
std::list< SOEntry > SOEntryList
SOEntryList m_added_soentries
List of SOEntry's added to the link map since the last call to Resolve().
void DumpToLog(lldb_private::Log *log) const
lldb_private::Process * m_process
lldb::addr_t GetLinkMapAddress() const
SOEntryList m_soentries
List of SOEntry objects corresponding to the current link map state.
lldb::addr_t m_rendezvous_addr
Location of the r_debug structure in the inferiors address space.
bool Resolve()
Update the internal snapshot of runtime linker rendezvous and recompute the currently loaded modules.
HexagonDYLDRendezvous(lldb_private::Process *process)
bool UpdateSOEntries()
Updates the current set of SOEntries, the set of added entries, and the set of removed entries.
uint64_t GetState() const
Returns the current state of the rendezvous structure.
bool TakeSnapshot(SOEntryList &entry_list)
Reads the current list of shared objects according to the link map supplied by the runtime linker.
const ThreadInfo & GetThreadInfo()
lldb::addr_t ReadPointer(lldb::addr_t addr, lldb::addr_t *dst)
Reads an address from the inferior's address space starting at addr.
SOEntryList::const_iterator iterator
lldb::addr_t GetBreakAddress() const
A breakpoint should be set at this address and Resolve called on each hit.
std::string ReadStringFromMemory(lldb::addr_t addr)
Reads a null-terminated C string from the memory location starting at addr.
lldb::addr_t GetLDBase() const
bool ReadSOEntryFromMemory(lldb::addr_t addr, SOEntry &entry)
Reads an SOEntry starting at addr.
SOEntryList m_removed_soentries
List of SOEntry's removed from the link map since the last call to Resolve().
lldb::addr_t GetRendezvousAddress() const
iterator begin() const
Iterators over all currently loaded modules.
void SetRendezvousAddress(lldb::addr_t)
Provide the dyld structure address.
ThreadInfo m_thread_info
Threading metadata read from the inferior.
bool FindMetadata(const char *name, PThreadField field, uint32_t &value)
lldb::addr_t ReadWord(lldb::addr_t addr, uint64_t *dst, size_t size)
Reads an unsigned integer of size bytes from the inferior's address space starting at addr.
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:301
A uniqued constant string class.
Definition ConstString.h:40
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:364
void PutCString(const char *cstr)
Definition Log.cpp:145
void FindSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:91
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition Module.h:447
A plug-in interface definition class for debugging a process.
Definition Process.h:357
virtual lldb::addr_t GetImageInfoAddress()
Get the image information address for the current process.
Definition Process.cpp:1481
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2508
An error handling class.
Definition Status.h:118
Defines a list of symbol context objects.
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1241
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
uint64_t addr_t
Definition lldb-types.h:80
Structure representing the shared objects currently loaded into the inferior process.
lldb::addr_t path_addr
String naming the shared object.
lldb::addr_t dyn_addr
Dynamic section of shared object.
std::string path
File name of shared object.
lldb::addr_t base_addr
Base address of the loaded object.
lldb::addr_t next
Address of next so_entry.
lldb::addr_t prev
Address of previous so_entry.
lldb::addr_t link_addr
Address of this link_map.
#define PATH_MAX