]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/JITLoader/GDB/JITLoaderGDB.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / Plugins / JITLoader / GDB / JITLoaderGDB.cpp
1 //===-- JITLoaderGDB.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
12 #include "llvm/Support/MathExtras.h"
13
14 #include "lldb/Breakpoint/Breakpoint.h"
15 #include "lldb/Core/DataBufferHeap.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ModuleSpec.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Interpreter/OptionValueProperties.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Symbol/SymbolVendor.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/SectionLoadList.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/LLDBAssert.h"
29
30 #include "JITLoaderGDB.h"
31
32 using namespace lldb;
33 using namespace lldb_private;
34
35 //------------------------------------------------------------------
36 // Debug Interface Structures
37 //------------------------------------------------------------------
38 typedef enum {
39   JIT_NOACTION = 0,
40   JIT_REGISTER_FN,
41   JIT_UNREGISTER_FN
42 } jit_actions_t;
43
44 template <typename ptr_t> struct jit_code_entry {
45   ptr_t next_entry;   // pointer
46   ptr_t prev_entry;   // pointer
47   ptr_t symfile_addr; // pointer
48   uint64_t symfile_size;
49 };
50
51 template <typename ptr_t> struct jit_descriptor {
52   uint32_t version;
53   uint32_t action_flag; // Values are jit_action_t
54   ptr_t relevant_entry; // pointer
55   ptr_t first_entry;    // pointer
56 };
57
58 namespace {
59
60 PropertyDefinition g_properties[] = {
61     {"enable-jit-breakpoint", OptionValue::eTypeBoolean, true, true, nullptr,
62      nullptr, "Enable breakpoint on __jit_debug_register_code."},
63     {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
64
65 enum { ePropertyEnableJITBreakpoint };
66
67 class PluginProperties : public Properties {
68 public:
69   static ConstString GetSettingName() {
70     return JITLoaderGDB::GetPluginNameStatic();
71   }
72
73   PluginProperties() {
74     m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
75     m_collection_sp->Initialize(g_properties);
76   }
77
78   bool GetEnableJITBreakpoint() const {
79     return m_collection_sp->GetPropertyAtIndexAsBoolean(
80         nullptr, ePropertyEnableJITBreakpoint,
81         g_properties[ePropertyEnableJITBreakpoint].default_uint_value != 0);
82   }
83 };
84
85 typedef std::shared_ptr<PluginProperties> JITLoaderGDBPropertiesSP;
86
87 static const JITLoaderGDBPropertiesSP &GetGlobalPluginProperties() {
88   static const auto g_settings_sp(std::make_shared<PluginProperties>());
89   return g_settings_sp;
90 }
91
92 template <typename ptr_t>
93 bool ReadJITEntry(const addr_t from_addr, Process *process,
94                   jit_code_entry<ptr_t> *entry) {
95   lldbassert(from_addr % sizeof(ptr_t) == 0);
96
97   ArchSpec::Core core = process->GetTarget().GetArchitecture().GetCore();
98   bool i386_target = ArchSpec::kCore_x86_32_first <= core &&
99                      core <= ArchSpec::kCore_x86_32_last;
100   uint8_t uint64_align_bytes = i386_target ? 4 : 8;
101   const size_t data_byte_size =
102       llvm::alignTo(sizeof(ptr_t) * 3, uint64_align_bytes) + sizeof(uint64_t);
103
104   Error error;
105   DataBufferHeap data(data_byte_size, 0);
106   size_t bytes_read = process->ReadMemory(from_addr, data.GetBytes(),
107                                           data.GetByteSize(), error);
108   if (bytes_read != data_byte_size || !error.Success())
109     return false;
110
111   DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
112                           process->GetByteOrder(), sizeof(ptr_t));
113   lldb::offset_t offset = 0;
114   entry->next_entry = extractor.GetPointer(&offset);
115   entry->prev_entry = extractor.GetPointer(&offset);
116   entry->symfile_addr = extractor.GetPointer(&offset);
117   offset = llvm::alignTo(offset, uint64_align_bytes);
118   entry->symfile_size = extractor.GetU64(&offset);
119
120   return true;
121 }
122
123 } // anonymous namespace end
124
125 JITLoaderGDB::JITLoaderGDB(lldb_private::Process *process)
126     : JITLoader(process), m_jit_objects(),
127       m_jit_break_id(LLDB_INVALID_BREAK_ID),
128       m_jit_descriptor_addr(LLDB_INVALID_ADDRESS) {}
129
130 JITLoaderGDB::~JITLoaderGDB() {
131   if (LLDB_BREAK_ID_IS_VALID(m_jit_break_id))
132     m_process->GetTarget().RemoveBreakpointByID(m_jit_break_id);
133 }
134
135 void JITLoaderGDB::DebuggerInitialize(Debugger &debugger) {
136   if (!PluginManager::GetSettingForJITLoaderPlugin(
137           debugger, PluginProperties::GetSettingName())) {
138     const bool is_global_setting = true;
139     PluginManager::CreateSettingForJITLoaderPlugin(
140         debugger, GetGlobalPluginProperties()->GetValueProperties(),
141         ConstString("Properties for the JIT LoaderGDB plug-in."),
142         is_global_setting);
143   }
144 }
145
146 void JITLoaderGDB::DidAttach() {
147   Target &target = m_process->GetTarget();
148   ModuleList &module_list = target.GetImages();
149   SetJITBreakpoint(module_list);
150 }
151
152 void JITLoaderGDB::DidLaunch() {
153   Target &target = m_process->GetTarget();
154   ModuleList &module_list = target.GetImages();
155   SetJITBreakpoint(module_list);
156 }
157
158 void JITLoaderGDB::ModulesDidLoad(ModuleList &module_list) {
159   if (!DidSetJITBreakpoint() && m_process->IsAlive())
160     SetJITBreakpoint(module_list);
161 }
162
163 //------------------------------------------------------------------
164 // Setup the JIT Breakpoint
165 //------------------------------------------------------------------
166 void JITLoaderGDB::SetJITBreakpoint(lldb_private::ModuleList &module_list) {
167   if (!GetGlobalPluginProperties()->GetEnableJITBreakpoint())
168     return;
169
170   if (DidSetJITBreakpoint())
171     return;
172
173   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
174   if (log)
175     log->Printf("JITLoaderGDB::%s looking for JIT register hook", __FUNCTION__);
176
177   addr_t jit_addr = GetSymbolAddress(
178       module_list, ConstString("__jit_debug_register_code"), eSymbolTypeAny);
179   if (jit_addr == LLDB_INVALID_ADDRESS)
180     return;
181
182   m_jit_descriptor_addr = GetSymbolAddress(
183       module_list, ConstString("__jit_debug_descriptor"), eSymbolTypeData);
184   if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS) {
185     if (log)
186       log->Printf("JITLoaderGDB::%s failed to find JIT descriptor address",
187                   __FUNCTION__);
188     return;
189   }
190
191   if (log)
192     log->Printf("JITLoaderGDB::%s setting JIT breakpoint", __FUNCTION__);
193
194   Breakpoint *bp =
195       m_process->GetTarget().CreateBreakpoint(jit_addr, true, false).get();
196   bp->SetCallback(JITDebugBreakpointHit, this, true);
197   bp->SetBreakpointKind("jit-debug-register");
198   m_jit_break_id = bp->GetID();
199
200   ReadJITDescriptor(true);
201 }
202
203 bool JITLoaderGDB::JITDebugBreakpointHit(void *baton,
204                                          StoppointCallbackContext *context,
205                                          user_id_t break_id,
206                                          user_id_t break_loc_id) {
207   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
208   if (log)
209     log->Printf("JITLoaderGDB::%s hit JIT breakpoint", __FUNCTION__);
210   JITLoaderGDB *instance = static_cast<JITLoaderGDB *>(baton);
211   return instance->ReadJITDescriptor(false);
212 }
213
214 static void updateSectionLoadAddress(const SectionList &section_list,
215                                      Target &target, uint64_t symbolfile_addr,
216                                      uint64_t symbolfile_size,
217                                      uint64_t &vmaddrheuristic,
218                                      uint64_t &min_addr, uint64_t &max_addr) {
219   const uint32_t num_sections = section_list.GetSize();
220   for (uint32_t i = 0; i < num_sections; ++i) {
221     SectionSP section_sp(section_list.GetSectionAtIndex(i));
222     if (section_sp) {
223       if (section_sp->IsFake()) {
224         uint64_t lower = (uint64_t)-1;
225         uint64_t upper = 0;
226         updateSectionLoadAddress(section_sp->GetChildren(), target,
227                                  symbolfile_addr, symbolfile_size,
228                                  vmaddrheuristic, lower, upper);
229         if (lower < min_addr)
230           min_addr = lower;
231         if (upper > max_addr)
232           max_addr = upper;
233         const lldb::addr_t slide_amount = lower - section_sp->GetFileAddress();
234         section_sp->Slide(slide_amount, false);
235         section_sp->GetChildren().Slide(-slide_amount, false);
236         section_sp->SetByteSize(upper - lower);
237       } else {
238         vmaddrheuristic += 2 << section_sp->GetLog2Align();
239         uint64_t lower;
240         if (section_sp->GetFileAddress() > vmaddrheuristic)
241           lower = section_sp->GetFileAddress();
242         else {
243           lower = symbolfile_addr + section_sp->GetFileOffset();
244           section_sp->SetFileAddress(symbolfile_addr +
245                                      section_sp->GetFileOffset());
246         }
247         target.SetSectionLoadAddress(section_sp, lower, true);
248         uint64_t upper = lower + section_sp->GetByteSize();
249         if (lower < min_addr)
250           min_addr = lower;
251         if (upper > max_addr)
252           max_addr = upper;
253         // This is an upper bound, but a good enough heuristic
254         vmaddrheuristic += section_sp->GetByteSize();
255       }
256     }
257   }
258 }
259
260 bool JITLoaderGDB::ReadJITDescriptor(bool all_entries) {
261   if (m_process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
262     return ReadJITDescriptorImpl<uint64_t>(all_entries);
263   else
264     return ReadJITDescriptorImpl<uint32_t>(all_entries);
265 }
266
267 template <typename ptr_t>
268 bool JITLoaderGDB::ReadJITDescriptorImpl(bool all_entries) {
269   if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS)
270     return false;
271
272   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
273   Target &target = m_process->GetTarget();
274   ModuleList &module_list = target.GetImages();
275
276   jit_descriptor<ptr_t> jit_desc;
277   const size_t jit_desc_size = sizeof(jit_desc);
278   Error error;
279   size_t bytes_read = m_process->DoReadMemory(m_jit_descriptor_addr, &jit_desc,
280                                               jit_desc_size, error);
281   if (bytes_read != jit_desc_size || !error.Success()) {
282     if (log)
283       log->Printf("JITLoaderGDB::%s failed to read JIT descriptor",
284                   __FUNCTION__);
285     return false;
286   }
287
288   jit_actions_t jit_action = (jit_actions_t)jit_desc.action_flag;
289   addr_t jit_relevant_entry = (addr_t)jit_desc.relevant_entry;
290   if (all_entries) {
291     jit_action = JIT_REGISTER_FN;
292     jit_relevant_entry = (addr_t)jit_desc.first_entry;
293   }
294
295   while (jit_relevant_entry != 0) {
296     jit_code_entry<ptr_t> jit_entry;
297     if (!ReadJITEntry(jit_relevant_entry, m_process, &jit_entry)) {
298       if (log)
299         log->Printf("JITLoaderGDB::%s failed to read JIT entry at 0x%" PRIx64,
300                     __FUNCTION__, jit_relevant_entry);
301       return false;
302     }
303
304     const addr_t &symbolfile_addr = (addr_t)jit_entry.symfile_addr;
305     const size_t &symbolfile_size = (size_t)jit_entry.symfile_size;
306     ModuleSP module_sp;
307
308     if (jit_action == JIT_REGISTER_FN) {
309       if (log)
310         log->Printf("JITLoaderGDB::%s registering JIT entry at 0x%" PRIx64
311                     " (%" PRIu64 " bytes)",
312                     __FUNCTION__, symbolfile_addr, (uint64_t)symbolfile_size);
313
314       char jit_name[64];
315       snprintf(jit_name, 64, "JIT(0x%" PRIx64 ")", symbolfile_addr);
316       module_sp = m_process->ReadModuleFromMemory(
317           FileSpec(jit_name, false), symbolfile_addr, symbolfile_size);
318
319       if (module_sp && module_sp->GetObjectFile()) {
320         // load the symbol table right away
321         module_sp->GetObjectFile()->GetSymtab();
322
323         m_jit_objects.insert(std::make_pair(symbolfile_addr, module_sp));
324         if (module_sp->GetObjectFile()->GetPluginName() ==
325             ConstString("mach-o")) {
326           ObjectFile *image_object_file = module_sp->GetObjectFile();
327           if (image_object_file) {
328             const SectionList *section_list =
329                 image_object_file->GetSectionList();
330             if (section_list) {
331               uint64_t vmaddrheuristic = 0;
332               uint64_t lower = (uint64_t)-1;
333               uint64_t upper = 0;
334               updateSectionLoadAddress(*section_list, target, symbolfile_addr,
335                                        symbolfile_size, vmaddrheuristic, lower,
336                                        upper);
337             }
338           }
339         } else {
340           bool changed = false;
341           module_sp->SetLoadAddress(target, 0, true, changed);
342         }
343
344         module_list.AppendIfNeeded(module_sp);
345
346         ModuleList module_list;
347         module_list.Append(module_sp);
348         target.ModulesDidLoad(module_list);
349       } else {
350         if (log)
351           log->Printf("JITLoaderGDB::%s failed to load module for "
352                       "JIT entry at 0x%" PRIx64,
353                       __FUNCTION__, symbolfile_addr);
354       }
355     } else if (jit_action == JIT_UNREGISTER_FN) {
356       if (log)
357         log->Printf("JITLoaderGDB::%s unregistering JIT entry at 0x%" PRIx64,
358                     __FUNCTION__, symbolfile_addr);
359
360       JITObjectMap::iterator it = m_jit_objects.find(symbolfile_addr);
361       if (it != m_jit_objects.end()) {
362         module_sp = it->second;
363         ObjectFile *image_object_file = module_sp->GetObjectFile();
364         if (image_object_file) {
365           const SectionList *section_list = image_object_file->GetSectionList();
366           if (section_list) {
367             const uint32_t num_sections = section_list->GetSize();
368             for (uint32_t i = 0; i < num_sections; ++i) {
369               SectionSP section_sp(section_list->GetSectionAtIndex(i));
370               if (section_sp) {
371                 target.GetSectionLoadList().SetSectionUnloaded(section_sp);
372               }
373             }
374           }
375         }
376         module_list.Remove(module_sp);
377         m_jit_objects.erase(it);
378       }
379     } else if (jit_action == JIT_NOACTION) {
380       // Nothing to do
381     } else {
382       assert(false && "Unknown jit action");
383     }
384
385     if (all_entries)
386       jit_relevant_entry = (addr_t)jit_entry.next_entry;
387     else
388       jit_relevant_entry = 0;
389   }
390
391   return false; // Continue Running.
392 }
393
394 //------------------------------------------------------------------
395 // PluginInterface protocol
396 //------------------------------------------------------------------
397 lldb_private::ConstString JITLoaderGDB::GetPluginNameStatic() {
398   static ConstString g_name("gdb");
399   return g_name;
400 }
401
402 JITLoaderSP JITLoaderGDB::CreateInstance(Process *process, bool force) {
403   JITLoaderSP jit_loader_sp;
404   ArchSpec arch(process->GetTarget().GetArchitecture());
405   if (arch.GetTriple().getVendor() != llvm::Triple::Apple)
406     jit_loader_sp.reset(new JITLoaderGDB(process));
407   return jit_loader_sp;
408 }
409
410 const char *JITLoaderGDB::GetPluginDescriptionStatic() {
411   return "JIT loader plug-in that watches for JIT events using the GDB "
412          "interface.";
413 }
414
415 lldb_private::ConstString JITLoaderGDB::GetPluginName() {
416   return GetPluginNameStatic();
417 }
418
419 uint32_t JITLoaderGDB::GetPluginVersion() { return 1; }
420
421 void JITLoaderGDB::Initialize() {
422   PluginManager::RegisterPlugin(GetPluginNameStatic(),
423                                 GetPluginDescriptionStatic(), CreateInstance,
424                                 DebuggerInitialize);
425 }
426
427 void JITLoaderGDB::Terminate() {
428   PluginManager::UnregisterPlugin(CreateInstance);
429 }
430
431 bool JITLoaderGDB::DidSetJITBreakpoint() const {
432   return LLDB_BREAK_ID_IS_VALID(m_jit_break_id);
433 }
434
435 addr_t JITLoaderGDB::GetSymbolAddress(ModuleList &module_list,
436                                       const ConstString &name,
437                                       SymbolType symbol_type) const {
438   SymbolContextList target_symbols;
439   Target &target = m_process->GetTarget();
440
441   if (!module_list.FindSymbolsWithNameAndType(name, symbol_type,
442                                               target_symbols))
443     return LLDB_INVALID_ADDRESS;
444
445   SymbolContext sym_ctx;
446   target_symbols.GetContextAtIndex(0, sym_ctx);
447
448   const Address jit_descriptor_addr = sym_ctx.symbol->GetAddress();
449   if (!jit_descriptor_addr.IsValid())
450     return LLDB_INVALID_ADDRESS;
451
452   const addr_t jit_addr = jit_descriptor_addr.GetLoadAddress(&target);
453   return jit_addr;
454 }