1 //===-- JITLoaderGDB.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 #include "llvm/Support/MathExtras.h"
14 #include "lldb/Breakpoint/Breakpoint.h"
15 #include "lldb/Core/DataBufferHeap.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/ModuleSpec.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"
30 #include "JITLoaderGDB.h"
33 using namespace lldb_private;
35 //------------------------------------------------------------------
36 // Debug Interface Structures
37 //------------------------------------------------------------------
45 template <typename ptr_t>
48 ptr_t next_entry; // pointer
49 ptr_t prev_entry; // pointer
50 ptr_t symfile_addr; // pointer
51 uint64_t symfile_size;
54 template <typename ptr_t>
58 uint32_t action_flag; // Values are jit_action_t
59 ptr_t relevant_entry; // pointer
60 ptr_t first_entry; // pointer
68 { "enable-jit-breakpoint", OptionValue::eTypeBoolean, true, true , nullptr, nullptr, "Enable breakpoint on __jit_debug_register_code." },
69 { nullptr , OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr }
74 ePropertyEnableJITBreakpoint
78 class PluginProperties : public Properties
84 return JITLoaderGDB::GetPluginNameStatic();
89 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
90 m_collection_sp->Initialize(g_properties);
94 GetEnableJITBreakpoint() const
96 return m_collection_sp->GetPropertyAtIndexAsBoolean(
98 ePropertyEnableJITBreakpoint,
99 g_properties[ePropertyEnableJITBreakpoint].default_uint_value != 0);
104 typedef std::shared_ptr<PluginProperties> JITLoaderGDBPropertiesSP;
106 static const JITLoaderGDBPropertiesSP&
107 GetGlobalPluginProperties()
109 static const auto g_settings_sp(std::make_shared<PluginProperties>());
110 return g_settings_sp;
113 template <typename ptr_t>
114 bool ReadJITEntry(const addr_t from_addr, Process *process, jit_code_entry<ptr_t> *entry)
116 lldbassert(from_addr % sizeof(ptr_t) == 0);
118 ArchSpec::Core core = process->GetTarget().GetArchitecture().GetCore();
119 bool i386_target = ArchSpec::kCore_x86_32_first <= core && core <= ArchSpec::kCore_x86_32_last;
120 uint8_t uint64_align_bytes = i386_target ? 4 : 8;
121 const size_t data_byte_size = llvm::alignTo(sizeof(ptr_t) * 3, uint64_align_bytes) + sizeof(uint64_t);
124 DataBufferHeap data(data_byte_size, 0);
125 size_t bytes_read = process->ReadMemory(from_addr, data.GetBytes(), data.GetByteSize(), error);
126 if (bytes_read != data_byte_size || !error.Success())
129 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), process->GetByteOrder(), sizeof(ptr_t));
130 lldb::offset_t offset = 0;
131 entry->next_entry = extractor.GetPointer(&offset);
132 entry->prev_entry = extractor.GetPointer(&offset);
133 entry->symfile_addr = extractor.GetPointer(&offset);
134 offset = llvm::alignTo(offset, uint64_align_bytes);
135 entry->symfile_size = extractor.GetU64(&offset);
140 } // anonymous namespace end
142 JITLoaderGDB::JITLoaderGDB (lldb_private::Process *process) :
145 m_jit_break_id(LLDB_INVALID_BREAK_ID),
146 m_jit_descriptor_addr(LLDB_INVALID_ADDRESS)
150 JITLoaderGDB::~JITLoaderGDB ()
152 if (LLDB_BREAK_ID_IS_VALID(m_jit_break_id))
153 m_process->GetTarget().RemoveBreakpointByID (m_jit_break_id);
157 JITLoaderGDB::DebuggerInitialize(Debugger &debugger)
159 if (!PluginManager::GetSettingForJITLoaderPlugin(debugger, PluginProperties::GetSettingName()))
161 const bool is_global_setting = true;
162 PluginManager::CreateSettingForJITLoaderPlugin(debugger,
163 GetGlobalPluginProperties()->GetValueProperties(),
164 ConstString ("Properties for the JIT LoaderGDB plug-in."),
169 void JITLoaderGDB::DidAttach()
171 Target &target = m_process->GetTarget();
172 ModuleList &module_list = target.GetImages();
173 SetJITBreakpoint(module_list);
176 void JITLoaderGDB::DidLaunch()
178 Target &target = m_process->GetTarget();
179 ModuleList &module_list = target.GetImages();
180 SetJITBreakpoint(module_list);
184 JITLoaderGDB::ModulesDidLoad(ModuleList &module_list)
186 if (!DidSetJITBreakpoint() && m_process->IsAlive())
187 SetJITBreakpoint(module_list);
190 //------------------------------------------------------------------
191 // Setup the JIT Breakpoint
192 //------------------------------------------------------------------
194 JITLoaderGDB::SetJITBreakpoint(lldb_private::ModuleList &module_list)
196 if (!GetGlobalPluginProperties()->GetEnableJITBreakpoint())
199 if ( DidSetJITBreakpoint() )
202 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
204 log->Printf("JITLoaderGDB::%s looking for JIT register hook",
207 addr_t jit_addr = GetSymbolAddress(module_list,
208 ConstString("__jit_debug_register_code"),
210 if (jit_addr == LLDB_INVALID_ADDRESS)
213 m_jit_descriptor_addr = GetSymbolAddress(module_list,
214 ConstString("__jit_debug_descriptor"),
216 if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS)
220 "JITLoaderGDB::%s failed to find JIT descriptor address",
226 log->Printf("JITLoaderGDB::%s setting JIT breakpoint",
230 m_process->GetTarget().CreateBreakpoint(jit_addr, true, false).get();
231 bp->SetCallback(JITDebugBreakpointHit, this, true);
232 bp->SetBreakpointKind("jit-debug-register");
233 m_jit_break_id = bp->GetID();
235 ReadJITDescriptor(true);
239 JITLoaderGDB::JITDebugBreakpointHit(void *baton,
240 StoppointCallbackContext *context,
241 user_id_t break_id, user_id_t break_loc_id)
243 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
245 log->Printf("JITLoaderGDB::%s hit JIT breakpoint",
247 JITLoaderGDB *instance = static_cast<JITLoaderGDB *>(baton);
248 return instance->ReadJITDescriptor(false);
251 static void updateSectionLoadAddress(const SectionList §ion_list,
253 uint64_t symbolfile_addr,
254 uint64_t symbolfile_size,
255 uint64_t &vmaddrheuristic,
259 const uint32_t num_sections = section_list.GetSize();
260 for (uint32_t i = 0; i<num_sections; ++i)
262 SectionSP section_sp(section_list.GetSectionAtIndex(i));
265 if(section_sp->IsFake()) {
266 uint64_t lower = (uint64_t)-1;
268 updateSectionLoadAddress(section_sp->GetChildren(), target, symbolfile_addr, symbolfile_size, vmaddrheuristic,
270 if (lower < min_addr)
272 if (upper > max_addr)
274 const lldb::addr_t slide_amount = lower - section_sp->GetFileAddress();
275 section_sp->Slide(slide_amount, false);
276 section_sp->GetChildren().Slide(-slide_amount, false);
277 section_sp->SetByteSize (upper - lower);
279 vmaddrheuristic += 2<<section_sp->GetLog2Align();
281 if (section_sp->GetFileAddress() > vmaddrheuristic)
282 lower = section_sp->GetFileAddress();
284 lower = symbolfile_addr+section_sp->GetFileOffset();
285 section_sp->SetFileAddress(symbolfile_addr+section_sp->GetFileOffset());
287 target.SetSectionLoadAddress(section_sp, lower, true);
288 uint64_t upper = lower + section_sp->GetByteSize();
289 if (lower < min_addr)
291 if (upper > max_addr)
293 // This is an upper bound, but a good enough heuristic
294 vmaddrheuristic += section_sp->GetByteSize();
301 JITLoaderGDB::ReadJITDescriptor(bool all_entries)
303 if (m_process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
304 return ReadJITDescriptorImpl<uint64_t>(all_entries);
306 return ReadJITDescriptorImpl<uint32_t>(all_entries);
309 template <typename ptr_t>
311 JITLoaderGDB::ReadJITDescriptorImpl(bool all_entries)
313 if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS)
316 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
317 Target &target = m_process->GetTarget();
318 ModuleList &module_list = target.GetImages();
320 jit_descriptor<ptr_t> jit_desc;
321 const size_t jit_desc_size = sizeof(jit_desc);
323 size_t bytes_read = m_process->DoReadMemory(
324 m_jit_descriptor_addr, &jit_desc, jit_desc_size, error);
325 if (bytes_read != jit_desc_size || !error.Success())
328 log->Printf("JITLoaderGDB::%s failed to read JIT descriptor",
333 jit_actions_t jit_action = (jit_actions_t)jit_desc.action_flag;
334 addr_t jit_relevant_entry = (addr_t)jit_desc.relevant_entry;
337 jit_action = JIT_REGISTER_FN;
338 jit_relevant_entry = (addr_t)jit_desc.first_entry;
341 while (jit_relevant_entry != 0)
343 jit_code_entry<ptr_t> jit_entry;
344 if (!ReadJITEntry(jit_relevant_entry, m_process, &jit_entry))
348 "JITLoaderGDB::%s failed to read JIT entry at 0x%" PRIx64,
349 __FUNCTION__, jit_relevant_entry);
353 const addr_t &symbolfile_addr = (addr_t)jit_entry.symfile_addr;
354 const size_t &symbolfile_size = (size_t)jit_entry.symfile_size;
357 if (jit_action == JIT_REGISTER_FN)
361 "JITLoaderGDB::%s registering JIT entry at 0x%" PRIx64
362 " (%" PRIu64 " bytes)",
363 __FUNCTION__, symbolfile_addr, (uint64_t) symbolfile_size);
366 snprintf(jit_name, 64, "JIT(0x%" PRIx64 ")", symbolfile_addr);
367 module_sp = m_process->ReadModuleFromMemory(
368 FileSpec(jit_name, false), symbolfile_addr, symbolfile_size);
370 if (module_sp && module_sp->GetObjectFile())
372 // load the symbol table right away
373 module_sp->GetObjectFile()->GetSymtab();
375 m_jit_objects.insert(std::make_pair(symbolfile_addr, module_sp));
376 if (module_sp->GetObjectFile()->GetPluginName() == ConstString("mach-o"))
378 ObjectFile *image_object_file = module_sp->GetObjectFile();
379 if (image_object_file)
381 const SectionList *section_list = image_object_file->GetSectionList ();
384 uint64_t vmaddrheuristic = 0;
385 uint64_t lower = (uint64_t)-1;
387 updateSectionLoadAddress(*section_list, target, symbolfile_addr, symbolfile_size,
388 vmaddrheuristic, lower, upper);
394 bool changed = false;
395 module_sp->SetLoadAddress(target, 0, true, changed);
398 module_list.AppendIfNeeded(module_sp);
400 ModuleList module_list;
401 module_list.Append(module_sp);
402 target.ModulesDidLoad(module_list);
407 log->Printf("JITLoaderGDB::%s failed to load module for "
408 "JIT entry at 0x%" PRIx64,
409 __FUNCTION__, symbolfile_addr);
412 else if (jit_action == JIT_UNREGISTER_FN)
416 "JITLoaderGDB::%s unregistering JIT entry at 0x%" PRIx64,
417 __FUNCTION__, symbolfile_addr);
419 JITObjectMap::iterator it = m_jit_objects.find(symbolfile_addr);
420 if (it != m_jit_objects.end())
422 module_sp = it->second;
423 ObjectFile *image_object_file = module_sp->GetObjectFile();
424 if (image_object_file)
426 const SectionList *section_list = image_object_file->GetSectionList ();
429 const uint32_t num_sections = section_list->GetSize();
430 for (uint32_t i = 0; i<num_sections; ++i)
432 SectionSP section_sp(section_list->GetSectionAtIndex(i));
435 target.GetSectionLoadList().SetSectionUnloaded (section_sp);
440 module_list.Remove(module_sp);
441 m_jit_objects.erase(it);
444 else if (jit_action == JIT_NOACTION)
450 assert(false && "Unknown jit action");
454 jit_relevant_entry = (addr_t)jit_entry.next_entry;
456 jit_relevant_entry = 0;
459 return false; // Continue Running.
462 //------------------------------------------------------------------
463 // PluginInterface protocol
464 //------------------------------------------------------------------
465 lldb_private::ConstString
466 JITLoaderGDB::GetPluginNameStatic()
468 static ConstString g_name("gdb");
473 JITLoaderGDB::CreateInstance(Process *process, bool force)
475 JITLoaderSP jit_loader_sp;
476 ArchSpec arch (process->GetTarget().GetArchitecture());
477 if (arch.GetTriple().getVendor() != llvm::Triple::Apple)
478 jit_loader_sp.reset(new JITLoaderGDB(process));
479 return jit_loader_sp;
483 JITLoaderGDB::GetPluginDescriptionStatic()
485 return "JIT loader plug-in that watches for JIT events using the GDB interface.";
488 lldb_private::ConstString
489 JITLoaderGDB::GetPluginName()
491 return GetPluginNameStatic();
495 JITLoaderGDB::GetPluginVersion()
501 JITLoaderGDB::Initialize()
503 PluginManager::RegisterPlugin (GetPluginNameStatic(),
504 GetPluginDescriptionStatic(),
510 JITLoaderGDB::Terminate()
512 PluginManager::UnregisterPlugin (CreateInstance);
516 JITLoaderGDB::DidSetJITBreakpoint() const
518 return LLDB_BREAK_ID_IS_VALID(m_jit_break_id);
522 JITLoaderGDB::GetSymbolAddress(ModuleList &module_list, const ConstString &name,
523 SymbolType symbol_type) const
525 SymbolContextList target_symbols;
526 Target &target = m_process->GetTarget();
528 if (!module_list.FindSymbolsWithNameAndType(name, symbol_type,
530 return LLDB_INVALID_ADDRESS;
532 SymbolContext sym_ctx;
533 target_symbols.GetContextAtIndex(0, sym_ctx);
535 const Address jit_descriptor_addr = sym_ctx.symbol->GetAddress();
536 if (!jit_descriptor_addr.IsValid())
537 return LLDB_INVALID_ADDRESS;
539 const addr_t jit_addr = jit_descriptor_addr.GetLoadAddress(&target);