1 //===-- HexagonDYLDRendezvous.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/Symbol/SymbolContext.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
22 #include "lldb/Symbol/ObjectFile.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
26 #include "HexagonDYLDRendezvous.h"
29 using namespace lldb_private;
31 /// Locates the address of the rendezvous structure. Returns the address on
32 /// success and LLDB_INVALID_ADDRESS on failure.
34 ResolveRendezvousAddress(Process *process)
40 info_location = process->GetImageInfoAddress();
42 if (info_location == LLDB_INVALID_ADDRESS)
43 return LLDB_INVALID_ADDRESS;
45 info_addr = process->ReadPointerFromMemory(info_location, error);
47 return LLDB_INVALID_ADDRESS;
50 return LLDB_INVALID_ADDRESS;
55 HexagonDYLDRendezvous::HexagonDYLDRendezvous(Process *process)
57 m_rendezvous_addr(LLDB_INVALID_ADDRESS),
64 m_thread_info.valid = false;
66 // Cache a copy of the executable path
69 Module *exe_mod = m_process->GetTarget().GetExecutableModulePointer();
71 exe_mod->GetFileSpec().GetPath(m_exe_path, PATH_MAX);
76 HexagonDYLDRendezvous::Resolve()
78 const size_t word_size = 4;
85 address_size = m_process->GetAddressByteSize();
86 padding = address_size - word_size;
88 if (m_rendezvous_addr == LLDB_INVALID_ADDRESS)
89 cursor = info_addr = ResolveRendezvousAddress(m_process);
91 cursor = info_addr = m_rendezvous_addr;
93 if (cursor == LLDB_INVALID_ADDRESS)
96 if (!(cursor = ReadWord(cursor, &info.version, word_size)))
99 if (!(cursor = ReadPointer(cursor + padding, &info.map_addr)))
102 if (!(cursor = ReadPointer(cursor, &info.brk)))
105 if (!(cursor = ReadWord(cursor, &info.state, word_size)))
108 if (!(cursor = ReadPointer(cursor + padding, &info.ldbase)))
111 // The rendezvous was successfully read. Update our internal state.
112 m_rendezvous_addr = info_addr;
113 m_previous = m_current;
116 return UpdateSOEntries();
120 HexagonDYLDRendezvous::SetRendezvousAddress( lldb::addr_t addr )
122 m_rendezvous_addr = addr;
126 HexagonDYLDRendezvous::IsValid()
128 return m_rendezvous_addr != LLDB_INVALID_ADDRESS;
132 HexagonDYLDRendezvous::UpdateSOEntries()
136 if (m_current.map_addr == 0)
139 // When the previous and current states are consistent this is the first
140 // time we have been asked to update. Just take a snapshot of the currently
142 if (m_previous.state == eConsistent && m_current.state == eConsistent)
143 return TakeSnapshot(m_soentries);
145 // If we are about to add or remove a shared object clear out the current
146 // state and take a snapshot of the currently loaded images.
147 if (m_current.state == eAdd || m_current.state == eDelete)
149 // this is a fudge so that we can clear the assert below.
150 m_previous.state = eConsistent;
151 // We hit this assert on the 2nd run of this function after running the calc example
152 assert(m_previous.state == eConsistent);
154 m_added_soentries.clear();
155 m_removed_soentries.clear();
156 return TakeSnapshot(m_soentries);
158 assert(m_current.state == eConsistent);
160 // Otherwise check the previous state to determine what to expect and update
162 if (m_previous.state == eAdd)
163 return UpdateSOEntriesForAddition();
164 else if (m_previous.state == eDelete)
165 return UpdateSOEntriesForDeletion();
171 HexagonDYLDRendezvous::UpdateSOEntriesForAddition()
176 assert(m_previous.state == eAdd);
178 if (m_current.map_addr == 0)
181 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next)
183 if (!ReadSOEntryFromMemory(cursor, entry))
186 // Only add shared libraries and not the executable.
187 // On Linux this is indicated by an empty path in the entry.
188 // On FreeBSD it is the name of the executable.
189 if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)
192 pos = std::find(m_soentries.begin(), m_soentries.end(), entry);
193 if (pos == m_soentries.end())
195 m_soentries.push_back(entry);
196 m_added_soentries.push_back(entry);
204 HexagonDYLDRendezvous::UpdateSOEntriesForDeletion()
206 SOEntryList entry_list;
209 assert(m_previous.state == eDelete);
211 if (!TakeSnapshot(entry_list))
214 for (iterator I = begin(); I != end(); ++I)
216 pos = std::find(entry_list.begin(), entry_list.end(), *I);
217 if (pos == entry_list.end())
218 m_removed_soentries.push_back(*I);
221 m_soentries = entry_list;
226 HexagonDYLDRendezvous::TakeSnapshot(SOEntryList &entry_list)
230 if (m_current.map_addr == 0)
233 for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next)
235 if (!ReadSOEntryFromMemory(cursor, entry))
238 // Only add shared libraries and not the executable.
239 // On Linux this is indicated by an empty path in the entry.
240 // On FreeBSD it is the name of the executable.
241 if (entry.path.empty() || ::strcmp(entry.path.c_str(), m_exe_path) == 0)
244 entry_list.push_back(entry);
251 HexagonDYLDRendezvous::ReadWord(addr_t addr, uint64_t *dst, size_t size)
255 *dst = m_process->ReadUnsignedIntegerFromMemory(addr, size, 0, error);
263 HexagonDYLDRendezvous::ReadPointer(addr_t addr, addr_t *dst)
267 *dst = m_process->ReadPointerFromMemory(addr, error);
271 return addr + m_process->GetAddressByteSize();
275 HexagonDYLDRendezvous::ReadStringFromMemory(addr_t addr)
282 if (addr == LLDB_INVALID_ADDRESS)
283 return std::string();
286 size = m_process->DoReadMemory(addr, &c, 1, error);
287 if (size != 1 || error.Fail())
288 return std::string();
301 HexagonDYLDRendezvous::ReadSOEntryFromMemory(lldb::addr_t addr, SOEntry &entry)
304 entry.link_addr = addr;
306 if (!(addr = ReadPointer(addr, &entry.base_addr)))
309 if (!(addr = ReadPointer(addr, &entry.path_addr)))
312 if (!(addr = ReadPointer(addr, &entry.dyn_addr)))
315 if (!(addr = ReadPointer(addr, &entry.next)))
318 if (!(addr = ReadPointer(addr, &entry.prev)))
321 entry.path = ReadStringFromMemory(entry.path_addr);
327 HexagonDYLDRendezvous::FindMetadata(const char *name, PThreadField field, uint32_t& value)
329 Target& target = m_process->GetTarget();
331 SymbolContextList list;
332 if (!target.GetImages().FindSymbolsWithNameAndType (ConstString(name), eSymbolTypeAny, list))
335 Address address = list[0].symbol->GetAddress();
336 addr_t addr = address.GetLoadAddress (&target);
337 if (addr == LLDB_INVALID_ADDRESS)
341 value = (uint32_t)m_process->ReadUnsignedIntegerFromMemory(addr + field*sizeof(uint32_t), sizeof(uint32_t), 0, error);
346 value /= 8; // convert bits to bytes
351 const HexagonDYLDRendezvous::ThreadInfo&
352 HexagonDYLDRendezvous::GetThreadInfo()
354 if (!m_thread_info.valid)
358 ok &= FindMetadata ("_thread_db_pthread_dtvp", eOffset, m_thread_info.dtv_offset);
359 ok &= FindMetadata ("_thread_db_dtv_dtv", eSize, m_thread_info.dtv_slot_size);
360 ok &= FindMetadata ("_thread_db_link_map_l_tls_modid", eOffset, m_thread_info.modid_offset);
361 ok &= FindMetadata ("_thread_db_dtv_t_pointer_val", eOffset, m_thread_info.tls_offset);
364 m_thread_info.valid = true;
367 return m_thread_info;
371 HexagonDYLDRendezvous::DumpToLog(Log *log) const
373 int state = GetState();
378 log->PutCString("HexagonDYLDRendezvous:");
379 log->Printf(" Address: %" PRIx64, GetRendezvousAddress());
380 log->Printf(" Version: %" PRIu64, GetVersion());
381 log->Printf(" Link : %" PRIx64, GetLinkMapAddress());
382 log->Printf(" Break : %" PRIx64, GetBreakAddress());
383 log->Printf(" LDBase : %" PRIx64, GetLDBase());
384 log->Printf(" State : %s",
385 (state == eConsistent) ? "consistent" :
386 (state == eAdd) ? "add" :
387 (state == eDelete) ? "delete" : "unknown");
389 iterator I = begin();
393 log->PutCString("HexagonDYLDRendezvous SOEntries:");
395 for (int i = 1; I != E; ++I, ++i)
397 log->Printf("\n SOEntry [%d] %s", i, I->path.c_str());
398 log->Printf(" Base : %" PRIx64, I->base_addr);
399 log->Printf(" Path : %" PRIx64, I->path_addr);
400 log->Printf(" Dyn : %" PRIx64, I->dyn_addr);
401 log->Printf(" Next : %" PRIx64, I->next);
402 log->Printf(" Prev : %" PRIx64, I->prev);