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/Symbol.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/Target.h"
21 #include "DYLDRendezvous.h"
24 using namespace lldb_private;
26 /// Locates the address of the rendezvous structure. Returns the address on
27 /// success and LLDB_INVALID_ADDRESS on failure.
29 ResolveRendezvousAddress(Process *process)
35 info_location = process->GetImageInfoAddress();
37 if (info_location == LLDB_INVALID_ADDRESS)
38 return LLDB_INVALID_ADDRESS;
40 info_addr = process->ReadPointerFromMemory(info_location, error);
42 return LLDB_INVALID_ADDRESS;
45 return LLDB_INVALID_ADDRESS;
50 DYLDRendezvous::DYLDRendezvous(Process *process)
52 m_rendezvous_addr(LLDB_INVALID_ADDRESS),
59 m_thread_info.valid = false;
61 // Cache a copy of the executable path
64 Module *exe_mod = m_process->GetTarget().GetExecutableModulePointer();
66 exe_mod->GetFileSpec().GetPath(m_exe_path, PATH_MAX);
71 DYLDRendezvous::Resolve()
73 const size_t word_size = 4;
80 address_size = m_process->GetAddressByteSize();
81 padding = address_size - word_size;
83 if (m_rendezvous_addr == LLDB_INVALID_ADDRESS)
84 cursor = info_addr = ResolveRendezvousAddress(m_process);
86 cursor = info_addr = m_rendezvous_addr;
88 if (cursor == LLDB_INVALID_ADDRESS)
91 if (!(cursor = ReadWord(cursor, &info.version, word_size)))
94 if (!(cursor = ReadPointer(cursor + padding, &info.map_addr)))
97 if (!(cursor = ReadPointer(cursor, &info.brk)))
100 if (!(cursor = ReadWord(cursor, &info.state, word_size)))
103 if (!(cursor = ReadPointer(cursor + padding, &info.ldbase)))
106 // The rendezvous was successfully read. Update our internal state.
107 m_rendezvous_addr = info_addr;
108 m_previous = m_current;
111 return UpdateSOEntries();
115 DYLDRendezvous::IsValid()
117 return m_rendezvous_addr != LLDB_INVALID_ADDRESS;
121 DYLDRendezvous::UpdateSOEntries()
125 if (m_current.map_addr == 0)
128 // When the previous and current states are consistent this is the first
129 // time we have been asked to update. Just take a snapshot of the currently
131 if (m_previous.state == eConsistent && m_current.state == eConsistent)
132 return TakeSnapshot(m_soentries);
134 // If we are about to add or remove a shared object clear out the current
135 // state and take a snapshot of the currently loaded images.
136 if (m_current.state == eAdd || m_current.state == eDelete)
138 assert(m_previous.state == eConsistent);
140 m_added_soentries.clear();
141 m_removed_soentries.clear();
142 return TakeSnapshot(m_soentries);
144 assert(m_current.state == eConsistent);
146 // Otherwise check the previous state to determine what to expect and update
148 if (m_previous.state == eAdd)
149 return UpdateSOEntriesForAddition();
150 else if (m_previous.state == eDelete)
151 return UpdateSOEntriesForDeletion();
157 DYLDRendezvous::UpdateSOEntriesForAddition()
162 assert(m_previous.state == eAdd);
164 if (m_current.map_addr == 0)
167 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next)
169 if (!ReadSOEntryFromMemory(cursor, entry))
172 // Only add shared libraries and not the executable.
173 // On Linux this is indicated by an empty path in the entry.
174 // On FreeBSD it is the name of the executable.
175 if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)
178 pos = std::find(m_soentries.begin(), m_soentries.end(), entry);
179 if (pos == m_soentries.end())
181 m_soentries.push_back(entry);
182 m_added_soentries.push_back(entry);
190 DYLDRendezvous::UpdateSOEntriesForDeletion()
192 SOEntryList entry_list;
195 assert(m_previous.state == eDelete);
197 if (!TakeSnapshot(entry_list))
200 for (iterator I = begin(); I != end(); ++I)
202 pos = std::find(entry_list.begin(), entry_list.end(), *I);
203 if (pos == entry_list.end())
204 m_removed_soentries.push_back(*I);
207 m_soentries = entry_list;
212 DYLDRendezvous::TakeSnapshot(SOEntryList &entry_list)
216 if (m_current.map_addr == 0)
219 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next)
221 if (!ReadSOEntryFromMemory(cursor, entry))
224 // Only add shared libraries and not the executable.
225 // On Linux this is indicated by an empty path in the entry.
226 // On FreeBSD it is the name of the executable.
227 if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)
230 entry_list.push_back(entry);
237 DYLDRendezvous::ReadWord(addr_t addr, uint64_t *dst, size_t size)
241 *dst = m_process->ReadUnsignedIntegerFromMemory(addr, size, 0, error);
249 DYLDRendezvous::ReadPointer(addr_t addr, addr_t *dst)
253 *dst = m_process->ReadPointerFromMemory(addr, error);
257 return addr + m_process->GetAddressByteSize();
261 DYLDRendezvous::ReadStringFromMemory(addr_t addr)
268 if (addr == LLDB_INVALID_ADDRESS)
269 return std::string();
272 size = m_process->DoReadMemory(addr, &c, 1, error);
273 if (size != 1 || error.Fail())
274 return std::string();
287 DYLDRendezvous::ReadSOEntryFromMemory(lldb::addr_t addr, SOEntry &entry)
291 entry.link_addr = addr;
293 if (!(addr = ReadPointer(addr, &entry.base_addr)))
296 // mips adds an extra load offset field to the link map struct on
297 // FreeBSD and NetBSD (need to validate other OSes).
298 // http://svnweb.freebsd.org/base/head/sys/sys/link_elf.h?revision=217153&view=markup#l57
299 const ArchSpec &arch = m_process->GetTarget().GetArchitecture();
300 if (arch.GetCore() == ArchSpec::eCore_mips64)
302 assert (arch.GetTriple().getOS() == llvm::Triple::FreeBSD ||
303 arch.GetTriple().getOS() == llvm::Triple::NetBSD);
305 if (!(addr = ReadPointer(addr, &mips_l_offs)))
307 if (mips_l_offs != 0 && mips_l_offs != entry.base_addr)
311 if (!(addr = ReadPointer(addr, &entry.path_addr)))
314 if (!(addr = ReadPointer(addr, &entry.dyn_addr)))
317 if (!(addr = ReadPointer(addr, &entry.next)))
320 if (!(addr = ReadPointer(addr, &entry.prev)))
323 entry.path = ReadStringFromMemory(entry.path_addr);
330 DYLDRendezvous::FindMetadata(const char *name, PThreadField field, uint32_t& value)
332 Target& target = m_process->GetTarget();
334 SymbolContextList list;
335 if (!target.GetImages().FindSymbolsWithNameAndType (ConstString(name), eSymbolTypeAny, list))
338 Address address = list[0].symbol->GetAddress();
339 addr_t addr = address.GetLoadAddress (&target);
340 if (addr == LLDB_INVALID_ADDRESS)
344 value = (uint32_t)m_process->ReadUnsignedIntegerFromMemory(addr + field*sizeof(uint32_t), sizeof(uint32_t), 0, error);
349 value /= 8; // convert bits to bytes
354 const DYLDRendezvous::ThreadInfo&
355 DYLDRendezvous::GetThreadInfo()
357 if (!m_thread_info.valid)
361 ok &= FindMetadata ("_thread_db_pthread_dtvp", eOffset, m_thread_info.dtv_offset);
362 ok &= FindMetadata ("_thread_db_dtv_dtv", eSize, m_thread_info.dtv_slot_size);
363 ok &= FindMetadata ("_thread_db_link_map_l_tls_modid", eOffset, m_thread_info.modid_offset);
364 ok &= FindMetadata ("_thread_db_dtv_t_pointer_val", eOffset, m_thread_info.tls_offset);
367 m_thread_info.valid = true;
370 return m_thread_info;
374 DYLDRendezvous::DumpToLog(Log *log) const
376 int state = GetState();
381 log->PutCString("DYLDRendezvous:");
382 log->Printf(" Address: %" PRIx64, GetRendezvousAddress());
383 log->Printf(" Version: %" PRIu64, GetVersion());
384 log->Printf(" Link : %" PRIx64, GetLinkMapAddress());
385 log->Printf(" Break : %" PRIx64, GetBreakAddress());
386 log->Printf(" LDBase : %" PRIx64, GetLDBase());
387 log->Printf(" State : %s",
388 (state == eConsistent) ? "consistent" :
389 (state == eAdd) ? "add" :
390 (state == eDelete) ? "delete" : "unknown");
392 iterator I = begin();
396 log->PutCString("DYLDRendezvous SOEntries:");
398 for (int i = 1; I != E; ++I, ++i)
400 log->Printf("\n SOEntry [%d] %s", i, I->path.c_str());
401 log->Printf(" Base : %" PRIx64, I->base_addr);
402 log->Printf(" Path : %" PRIx64, I->path_addr);
403 log->Printf(" Dyn : %" PRIx64, I->dyn_addr);
404 log->Printf(" Next : %" PRIx64, I->next);
405 log->Printf(" Prev : %" PRIx64, I->prev);