]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
Merge ^/head r274961 through r276342.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / DynamicLoader / POSIX-DYLD / DYLDRendezvous.cpp
1 //===-- DYLDRendezvous.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // C Includes
11 // C++ Includes
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"
21
22 #include "DYLDRendezvous.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 /// Locates the address of the rendezvous structure.  Returns the address on
28 /// success and LLDB_INVALID_ADDRESS on failure.
29 static addr_t
30 ResolveRendezvousAddress(Process *process)
31 {
32     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
33     addr_t info_location;
34     addr_t info_addr;
35     Error error;
36
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();
40     if (log)
41         log->Printf ("%s info_location = 0x%" PRIx64, __FUNCTION__, info_location);
42
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)
45     {
46         Target *target = process ? &process->GetTarget() : nullptr;
47         if (target)
48         {
49             ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
50             Address addr = obj_file->GetImageInfoAddress(target);
51
52             if (addr.IsValid())
53             {
54                 info_location = addr.GetLoadAddress(target);
55                 if (log)
56                     log->Printf ("%s resolved via direct object file approach to 0x%" PRIx64, __FUNCTION__, info_location);
57             }
58             else
59             {
60                 if (log)
61                     log->Printf ("%s FAILED - direct object file approach did not yield a valid address", __FUNCTION__);
62             }
63         }
64     }
65
66     if (info_location == LLDB_INVALID_ADDRESS)
67     {
68         if (log)
69             log->Printf ("%s FAILED - invalid info address", __FUNCTION__);
70         return LLDB_INVALID_ADDRESS;
71     }
72
73     info_addr = process->ReadPointerFromMemory(info_location, error);
74     if (error.Fail())
75     {
76         if (log)
77             log->Printf ("%s FAILED - could not read from the info location: %s", __FUNCTION__, error.AsCString ());
78         return LLDB_INVALID_ADDRESS;
79     }
80
81     if (info_addr == 0)
82     {
83         if (log)
84             log->Printf ("%s FAILED - the rendezvous address contained at 0x%" PRIx64 " returned a null value", __FUNCTION__, info_location);
85         return LLDB_INVALID_ADDRESS;
86     }
87
88     return info_addr;
89 }
90
91 DYLDRendezvous::DYLDRendezvous(Process *process)
92     : m_process(process),
93       m_rendezvous_addr(LLDB_INVALID_ADDRESS),
94       m_current(),
95       m_previous(),
96       m_soentries(),
97       m_added_soentries(),
98       m_removed_soentries()
99 {
100     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
101
102     m_thread_info.valid = false;
103
104     // Cache a copy of the executable path
105     if (m_process)
106     {
107         Module *exe_mod = m_process->GetTarget().GetExecutableModulePointer();
108         if (exe_mod)
109         {
110             exe_mod->GetFileSpec().GetPath(m_exe_path, PATH_MAX);
111             if (log)
112                 log->Printf ("DYLDRendezvous::%s exe module executable path set: '%s'", __FUNCTION__, m_exe_path);
113         }
114         else
115         {
116             if (log)
117                 log->Printf ("DYLDRendezvous::%s cannot cache exe module path: null executable module pointer", __FUNCTION__);
118         }
119     }
120 }
121
122 bool
123 DYLDRendezvous::Resolve()
124 {
125     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER));
126
127     const size_t word_size = 4;
128     Rendezvous info;
129     size_t address_size;
130     size_t padding;
131     addr_t info_addr;
132     addr_t cursor;
133
134     address_size = m_process->GetAddressByteSize();
135     padding = address_size - word_size;
136     if (log)
137         log->Printf ("DYLDRendezvous::%s address size: %zu, padding %zu", __FUNCTION__, address_size, padding);
138
139     if (m_rendezvous_addr == LLDB_INVALID_ADDRESS)
140         cursor = info_addr = ResolveRendezvousAddress(m_process);
141     else
142         cursor = info_addr = m_rendezvous_addr;
143     if (log)
144         log->Printf ("DYLDRendezvous::%s cursor = 0x%" PRIx64, __FUNCTION__, cursor);
145
146     if (cursor == LLDB_INVALID_ADDRESS)
147         return false;
148
149     if (!(cursor = ReadWord(cursor, &info.version, word_size)))
150         return false;
151
152     if (!(cursor = ReadPointer(cursor + padding, &info.map_addr)))
153         return false;
154
155     if (!(cursor = ReadPointer(cursor, &info.brk)))
156         return false;
157
158     if (!(cursor = ReadWord(cursor, &info.state, word_size)))
159         return false;
160
161     if (!(cursor = ReadPointer(cursor + padding, &info.ldbase)))
162         return false;
163
164     // The rendezvous was successfully read.  Update our internal state.
165     m_rendezvous_addr = info_addr;
166     m_previous = m_current;
167     m_current = info;
168
169     return UpdateSOEntries();
170 }
171
172 bool
173 DYLDRendezvous::IsValid()
174 {
175     return m_rendezvous_addr != LLDB_INVALID_ADDRESS;
176 }
177
178 bool
179 DYLDRendezvous::UpdateSOEntries()
180 {
181     SOEntry entry;
182
183     if (m_current.map_addr == 0)
184         return false;
185
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
188     // loaded modules.
189     if (m_previous.state == eConsistent && m_current.state == eConsistent) 
190         return TakeSnapshot(m_soentries);
191
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)
195     {
196         assert(m_previous.state == eConsistent);
197         m_soentries.clear();
198         m_added_soentries.clear();
199         m_removed_soentries.clear();
200         return TakeSnapshot(m_soentries);
201     }
202     assert(m_current.state == eConsistent);
203
204     // Otherwise check the previous state to determine what to expect and update
205     // accordingly.
206     if (m_previous.state == eAdd)
207         return UpdateSOEntriesForAddition();
208     else if (m_previous.state == eDelete)
209         return UpdateSOEntriesForDeletion();
210
211     return false;
212 }
213  
214 bool
215 DYLDRendezvous::UpdateSOEntriesForAddition()
216 {
217     SOEntry entry;
218     iterator pos;
219
220     assert(m_previous.state == eAdd);
221
222     if (m_current.map_addr == 0)
223         return false;
224
225     for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next)
226     {
227         if (!ReadSOEntryFromMemory(cursor, entry))
228             return false;
229
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)
234             continue;
235
236         pos = std::find(m_soentries.begin(), m_soentries.end(), entry);
237         if (pos == m_soentries.end())
238         {
239             m_soentries.push_back(entry);
240             m_added_soentries.push_back(entry);
241         }
242     }
243
244     return true;
245 }
246
247 bool
248 DYLDRendezvous::UpdateSOEntriesForDeletion()
249 {
250     SOEntryList entry_list;
251     iterator pos;
252
253     assert(m_previous.state == eDelete);
254
255     if (!TakeSnapshot(entry_list))
256         return false;
257
258     for (iterator I = begin(); I != end(); ++I)
259     {
260         pos = std::find(entry_list.begin(), entry_list.end(), *I);
261         if (pos == entry_list.end())
262             m_removed_soentries.push_back(*I);
263     }
264
265     m_soentries = entry_list;
266     return true;
267 }
268
269 bool
270 DYLDRendezvous::TakeSnapshot(SOEntryList &entry_list)
271 {
272     SOEntry entry;
273
274     if (m_current.map_addr == 0)
275         return false;
276
277     for (addr_t cursor = m_current.map_addr; cursor != 0; cursor = entry.next)
278     {
279         if (!ReadSOEntryFromMemory(cursor, entry))
280             return false;
281
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)
286             continue;
287
288         entry_list.push_back(entry);
289     }
290
291     return true;
292 }
293
294 addr_t
295 DYLDRendezvous::ReadWord(addr_t addr, uint64_t *dst, size_t size)
296 {
297     Error error;
298
299     *dst = m_process->ReadUnsignedIntegerFromMemory(addr, size, 0, error);
300     if (error.Fail())
301         return 0;
302
303     return addr + size;
304 }
305
306 addr_t
307 DYLDRendezvous::ReadPointer(addr_t addr, addr_t *dst)
308 {
309     Error error;
310  
311     *dst = m_process->ReadPointerFromMemory(addr, error);
312     if (error.Fail())
313         return 0;
314
315     return addr + m_process->GetAddressByteSize();
316 }
317
318 std::string
319 DYLDRendezvous::ReadStringFromMemory(addr_t addr)
320 {
321     std::string str;
322     Error error;
323     size_t size;
324     char c;
325
326     if (addr == LLDB_INVALID_ADDRESS)
327         return std::string();
328
329     for (;;) {
330         size = m_process->DoReadMemory(addr, &c, 1, error);
331         if (size != 1 || error.Fail())
332             return std::string();
333         if (c == 0)
334             break;
335         else {
336             str.push_back(c);
337             addr++;
338         }
339     }
340
341     return str;
342 }
343
344 bool
345 DYLDRendezvous::ReadSOEntryFromMemory(lldb::addr_t addr, SOEntry &entry)
346 {
347     entry.clear();
348
349     entry.link_addr = addr;
350     
351     if (!(addr = ReadPointer(addr, &entry.base_addr)))
352         return false;
353
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)
359     {
360         assert (arch.GetTriple().getOS() == llvm::Triple::FreeBSD ||
361                 arch.GetTriple().getOS() == llvm::Triple::NetBSD);
362         addr_t mips_l_offs;
363         if (!(addr = ReadPointer(addr, &mips_l_offs)))
364             return false;
365         if (mips_l_offs != 0 && mips_l_offs != entry.base_addr)
366             return false;
367     }
368     
369     if (!(addr = ReadPointer(addr, &entry.path_addr)))
370         return false;
371     
372     if (!(addr = ReadPointer(addr, &entry.dyn_addr)))
373         return false;
374     
375     if (!(addr = ReadPointer(addr, &entry.next)))
376         return false;
377     
378     if (!(addr = ReadPointer(addr, &entry.prev)))
379         return false;
380     
381     entry.path = ReadStringFromMemory(entry.path_addr);
382     
383     return true;
384 }
385
386
387 bool
388 DYLDRendezvous::FindMetadata(const char *name, PThreadField field, uint32_t& value)
389 {
390     Target& target = m_process->GetTarget();
391
392     SymbolContextList list;
393     if (!target.GetImages().FindSymbolsWithNameAndType (ConstString(name), eSymbolTypeAny, list))
394         return false;
395
396     Address address = list[0].symbol->GetAddress();
397     addr_t addr = address.GetLoadAddress (&target);
398     if (addr == LLDB_INVALID_ADDRESS)
399         return false;
400
401     Error error;
402     value = (uint32_t)m_process->ReadUnsignedIntegerFromMemory(addr + field*sizeof(uint32_t), sizeof(uint32_t), 0, error);
403     if (error.Fail())
404         return false;
405
406     if (field == eSize)
407         value /= 8; // convert bits to bytes
408
409     return true;
410 }
411
412 const DYLDRendezvous::ThreadInfo&
413 DYLDRendezvous::GetThreadInfo()
414 {
415     if (!m_thread_info.valid)
416     {
417         bool ok = true;
418
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);
423
424         if (ok)
425             m_thread_info.valid = true;
426     }
427
428     return m_thread_info;
429 }
430
431 void
432 DYLDRendezvous::DumpToLog(Log *log) const
433 {
434     int state = GetState();
435
436     if (!log)
437         return;
438
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");
449     
450     iterator I = begin();
451     iterator E = end();
452
453     if (I != E) 
454         log->PutCString("DYLDRendezvous SOEntries:");
455     
456     for (int i = 1; I != E; ++I, ++i) 
457     {
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);
464     }
465 }