1 //===-- DYLDRendezvous.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
13 #include "lldb/Core/ArchSpec.h"
14 #include "lldb/Core/Error.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Symbol/Symbol.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
22 #include "DYLDRendezvous.h"
25 using namespace lldb_private;
27 /// Locates the address of the rendezvous structure. Returns the address on
28 /// success and LLDB_INVALID_ADDRESS on failure.
30 ResolveRendezvousAddress(Process *process)
32 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
37 // Try to get it from our process. This might be a remote process and might
38 // grab it via some remote-specific mechanism.
39 info_location = process->GetImageInfoAddress();
41 log->Printf ("%s info_location = 0x%" PRIx64, __FUNCTION__, info_location);
43 // If the process fails to return an address, fall back to seeing if the local object file can help us find it.
44 if (info_location == LLDB_INVALID_ADDRESS)
46 Target *target = process ? &process->GetTarget() : nullptr;
49 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
50 Address addr = obj_file->GetImageInfoAddress(target);
54 info_location = addr.GetLoadAddress(target);
56 log->Printf ("%s resolved via direct object file approach to 0x%" PRIx64, __FUNCTION__, info_location);
61 log->Printf ("%s FAILED - direct object file approach did not yield a valid address", __FUNCTION__);
66 if (info_location == LLDB_INVALID_ADDRESS)
69 log->Printf ("%s FAILED - invalid info address", __FUNCTION__);
70 return LLDB_INVALID_ADDRESS;
73 info_addr = process->ReadPointerFromMemory(info_location, error);
77 log->Printf ("%s FAILED - could not read from the info location: %s", __FUNCTION__, error.AsCString ());
78 return LLDB_INVALID_ADDRESS;
84 log->Printf ("%s FAILED - the rendezvous address contained at 0x%" PRIx64 " returned a null value", __FUNCTION__, info_location);
85 return LLDB_INVALID_ADDRESS;
91 DYLDRendezvous::DYLDRendezvous(Process *process)
93 m_rendezvous_addr(LLDB_INVALID_ADDRESS),
100 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
102 m_thread_info.valid = false;
104 // Cache a copy of the executable path
107 Module *exe_mod = m_process->GetTarget().GetExecutableModulePointer();
110 exe_mod->GetFileSpec().GetPath(m_exe_path, PATH_MAX);
112 log->Printf ("DYLDRendezvous::%s exe module executable path set: '%s'", __FUNCTION__, m_exe_path);
117 log->Printf ("DYLDRendezvous::%s cannot cache exe module path: null executable module pointer", __FUNCTION__);
123 DYLDRendezvous::Resolve()
125 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
127 const size_t word_size = 4;
134 address_size = m_process->GetAddressByteSize();
135 padding = address_size - word_size;
137 log->Printf ("DYLDRendezvous::%s address size: %zu, padding %zu", __FUNCTION__, address_size, padding);
139 if (m_rendezvous_addr == LLDB_INVALID_ADDRESS)
140 cursor = info_addr = ResolveRendezvousAddress(m_process);
142 cursor = info_addr = m_rendezvous_addr;
144 log->Printf ("DYLDRendezvous::%s cursor = 0x%" PRIx64, __FUNCTION__, cursor);
146 if (cursor == LLDB_INVALID_ADDRESS)
149 if (!(cursor = ReadWord(cursor, &info.version, word_size)))
152 if (!(cursor = ReadPointer(cursor + padding, &info.map_addr)))
155 if (!(cursor = ReadPointer(cursor, &info.brk)))
158 if (!(cursor = ReadWord(cursor, &info.state, word_size)))
161 if (!(cursor = ReadPointer(cursor + padding, &info.ldbase)))
164 // The rendezvous was successfully read. Update our internal state.
165 m_rendezvous_addr = info_addr;
166 m_previous = m_current;
169 return UpdateSOEntries();
173 DYLDRendezvous::IsValid()
175 return m_rendezvous_addr != LLDB_INVALID_ADDRESS;
179 DYLDRendezvous::UpdateSOEntries()
183 if (m_current.map_addr == 0)
186 // When the previous and current states are consistent this is the first
187 // time we have been asked to update. Just take a snapshot of the currently
189 if (m_previous.state == eConsistent && m_current.state == eConsistent)
190 return TakeSnapshot(m_soentries);
192 // If we are about to add or remove a shared object clear out the current
193 // state and take a snapshot of the currently loaded images.
194 if (m_current.state == eAdd || m_current.state == eDelete)
196 assert(m_previous.state == eConsistent);
198 m_added_soentries.clear();
199 m_removed_soentries.clear();
200 return TakeSnapshot(m_soentries);
202 assert(m_current.state == eConsistent);
204 // Otherwise check the previous state to determine what to expect and update
206 if (m_previous.state == eAdd)
207 return UpdateSOEntriesForAddition();
208 else if (m_previous.state == eDelete)
209 return UpdateSOEntriesForDeletion();
215 DYLDRendezvous::UpdateSOEntriesForAddition()
220 assert(m_previous.state == eAdd);
222 if (m_current.map_addr == 0)
225 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next)
227 if (!ReadSOEntryFromMemory(cursor, entry))
230 // Only add shared libraries and not the executable.
231 // On Linux this is indicated by an empty path in the entry.
232 // On FreeBSD it is the name of the executable.
233 if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)
236 pos = std::find(m_soentries.begin(), m_soentries.end(), entry);
237 if (pos == m_soentries.end())
239 m_soentries.push_back(entry);
240 m_added_soentries.push_back(entry);
248 DYLDRendezvous::UpdateSOEntriesForDeletion()
250 SOEntryList entry_list;
253 assert(m_previous.state == eDelete);
255 if (!TakeSnapshot(entry_list))
258 for (iterator I = begin(); I != end(); ++I)
260 pos = std::find(entry_list.begin(), entry_list.end(), *I);
261 if (pos == entry_list.end())
262 m_removed_soentries.push_back(*I);
265 m_soentries = entry_list;
270 DYLDRendezvous::TakeSnapshot(SOEntryList &entry_list)
274 if (m_current.map_addr == 0)
277 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next)
279 if (!ReadSOEntryFromMemory(cursor, entry))
282 // Only add shared libraries and not the executable.
283 // On Linux this is indicated by an empty path in the entry.
284 // On FreeBSD it is the name of the executable.
285 if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)
288 entry_list.push_back(entry);
295 DYLDRendezvous::ReadWord(addr_t addr, uint64_t *dst, size_t size)
299 *dst = m_process->ReadUnsignedIntegerFromMemory(addr, size, 0, error);
307 DYLDRendezvous::ReadPointer(addr_t addr, addr_t *dst)
311 *dst = m_process->ReadPointerFromMemory(addr, error);
315 return addr + m_process->GetAddressByteSize();
319 DYLDRendezvous::ReadStringFromMemory(addr_t addr)
326 if (addr == LLDB_INVALID_ADDRESS)
327 return std::string();
330 size = m_process->DoReadMemory(addr, &c, 1, error);
331 if (size != 1 || error.Fail())
332 return std::string();
345 DYLDRendezvous::ReadSOEntryFromMemory(lldb::addr_t addr, SOEntry &entry)
349 entry.link_addr = addr;
351 if (!(addr = ReadPointer(addr, &entry.base_addr)))
354 // mips adds an extra load offset field to the link map struct on
355 // FreeBSD and NetBSD (need to validate other OSes).
356 // http://svnweb.freebsd.org/base/head/sys/sys/link_elf.h?revision=217153&view=markup#l57
357 const ArchSpec &arch = m_process->GetTarget().GetArchitecture();
358 if (arch.GetCore() == ArchSpec::eCore_mips64)
360 assert (arch.GetTriple().getOS() == llvm::Triple::FreeBSD ||
361 arch.GetTriple().getOS() == llvm::Triple::NetBSD);
363 if (!(addr = ReadPointer(addr, &mips_l_offs)))
365 if (mips_l_offs != 0 && mips_l_offs != entry.base_addr)
369 if (!(addr = ReadPointer(addr, &entry.path_addr)))
372 if (!(addr = ReadPointer(addr, &entry.dyn_addr)))
375 if (!(addr = ReadPointer(addr, &entry.next)))
378 if (!(addr = ReadPointer(addr, &entry.prev)))
381 entry.path = ReadStringFromMemory(entry.path_addr);
388 DYLDRendezvous::FindMetadata(const char *name, PThreadField field, uint32_t& value)
390 Target& target = m_process->GetTarget();
392 SymbolContextList list;
393 if (!target.GetImages().FindSymbolsWithNameAndType (ConstString(name), eSymbolTypeAny, list))
396 Address address = list[0].symbol->GetAddress();
397 addr_t addr = address.GetLoadAddress (&target);
398 if (addr == LLDB_INVALID_ADDRESS)
402 value = (uint32_t)m_process->ReadUnsignedIntegerFromMemory(addr + field*sizeof(uint32_t), sizeof(uint32_t), 0, error);
407 value /= 8; // convert bits to bytes
412 const DYLDRendezvous::ThreadInfo&
413 DYLDRendezvous::GetThreadInfo()
415 if (!m_thread_info.valid)
419 ok &= FindMetadata ("_thread_db_pthread_dtvp", eOffset, m_thread_info.dtv_offset);
420 ok &= FindMetadata ("_thread_db_dtv_dtv", eSize, m_thread_info.dtv_slot_size);
421 ok &= FindMetadata ("_thread_db_link_map_l_tls_modid", eOffset, m_thread_info.modid_offset);
422 ok &= FindMetadata ("_thread_db_dtv_t_pointer_val", eOffset, m_thread_info.tls_offset);
425 m_thread_info.valid = true;
428 return m_thread_info;
432 DYLDRendezvous::DumpToLog(Log *log) const
434 int state = GetState();
439 log->PutCString("DYLDRendezvous:");
440 log->Printf(" Address: %" PRIx64, GetRendezvousAddress());
441 log->Printf(" Version: %" PRIu64, GetVersion());
442 log->Printf(" Link : %" PRIx64, GetLinkMapAddress());
443 log->Printf(" Break : %" PRIx64, GetBreakAddress());
444 log->Printf(" LDBase : %" PRIx64, GetLDBase());
445 log->Printf(" State : %s",
446 (state == eConsistent) ? "consistent" :
447 (state == eAdd) ? "add" :
448 (state == eDelete) ? "delete" : "unknown");
450 iterator I = begin();
454 log->PutCString("DYLDRendezvous SOEntries:");
456 for (int i = 1; I != E; ++I, ++i)
458 log->Printf("\n SOEntry [%d] %s", i, I->path.c_str());
459 log->Printf(" Base : %" PRIx64, I->base_addr);
460 log->Printf(" Path : %" PRIx64, I->path_addr);
461 log->Printf(" Dyn : %" PRIx64, I->dyn_addr);
462 log->Printf(" Next : %" PRIx64, I->next);
463 log->Printf(" Prev : %" PRIx64, I->prev);