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/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"
30 #include "JITLoaderGDB.h"
33 using namespace lldb_private;
35 //------------------------------------------------------------------
36 // Debug Interface Structures
37 //------------------------------------------------------------------
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;
51 template <typename ptr_t> struct jit_descriptor {
53 uint32_t action_flag; // Values are jit_action_t
54 ptr_t relevant_entry; // pointer
55 ptr_t first_entry; // pointer
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}};
65 enum { ePropertyEnableJITBreakpoint };
67 class PluginProperties : public Properties {
69 static ConstString GetSettingName() {
70 return JITLoaderGDB::GetPluginNameStatic();
74 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
75 m_collection_sp->Initialize(g_properties);
78 bool GetEnableJITBreakpoint() const {
79 return m_collection_sp->GetPropertyAtIndexAsBoolean(
80 nullptr, ePropertyEnableJITBreakpoint,
81 g_properties[ePropertyEnableJITBreakpoint].default_uint_value != 0);
85 typedef std::shared_ptr<PluginProperties> JITLoaderGDBPropertiesSP;
87 static const JITLoaderGDBPropertiesSP &GetGlobalPluginProperties() {
88 static const auto g_settings_sp(std::make_shared<PluginProperties>());
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);
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);
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())
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);
123 } // anonymous namespace end
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) {}
130 JITLoaderGDB::~JITLoaderGDB() {
131 if (LLDB_BREAK_ID_IS_VALID(m_jit_break_id))
132 m_process->GetTarget().RemoveBreakpointByID(m_jit_break_id);
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."),
146 void JITLoaderGDB::DidAttach() {
147 Target &target = m_process->GetTarget();
148 ModuleList &module_list = target.GetImages();
149 SetJITBreakpoint(module_list);
152 void JITLoaderGDB::DidLaunch() {
153 Target &target = m_process->GetTarget();
154 ModuleList &module_list = target.GetImages();
155 SetJITBreakpoint(module_list);
158 void JITLoaderGDB::ModulesDidLoad(ModuleList &module_list) {
159 if (!DidSetJITBreakpoint() && m_process->IsAlive())
160 SetJITBreakpoint(module_list);
163 //------------------------------------------------------------------
164 // Setup the JIT Breakpoint
165 //------------------------------------------------------------------
166 void JITLoaderGDB::SetJITBreakpoint(lldb_private::ModuleList &module_list) {
167 if (!GetGlobalPluginProperties()->GetEnableJITBreakpoint())
170 if (DidSetJITBreakpoint())
173 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
175 log->Printf("JITLoaderGDB::%s looking for JIT register hook", __FUNCTION__);
177 addr_t jit_addr = GetSymbolAddress(
178 module_list, ConstString("__jit_debug_register_code"), eSymbolTypeAny);
179 if (jit_addr == LLDB_INVALID_ADDRESS)
182 m_jit_descriptor_addr = GetSymbolAddress(
183 module_list, ConstString("__jit_debug_descriptor"), eSymbolTypeData);
184 if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS) {
186 log->Printf("JITLoaderGDB::%s failed to find JIT descriptor address",
192 log->Printf("JITLoaderGDB::%s setting JIT breakpoint", __FUNCTION__);
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();
200 ReadJITDescriptor(true);
203 bool JITLoaderGDB::JITDebugBreakpointHit(void *baton,
204 StoppointCallbackContext *context,
206 user_id_t break_loc_id) {
207 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
209 log->Printf("JITLoaderGDB::%s hit JIT breakpoint", __FUNCTION__);
210 JITLoaderGDB *instance = static_cast<JITLoaderGDB *>(baton);
211 return instance->ReadJITDescriptor(false);
214 static void updateSectionLoadAddress(const SectionList §ion_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));
223 if (section_sp->IsFake()) {
224 uint64_t lower = (uint64_t)-1;
226 updateSectionLoadAddress(section_sp->GetChildren(), target,
227 symbolfile_addr, symbolfile_size,
228 vmaddrheuristic, lower, upper);
229 if (lower < min_addr)
231 if (upper > max_addr)
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);
238 vmaddrheuristic += 2 << section_sp->GetLog2Align();
240 if (section_sp->GetFileAddress() > vmaddrheuristic)
241 lower = section_sp->GetFileAddress();
243 lower = symbolfile_addr + section_sp->GetFileOffset();
244 section_sp->SetFileAddress(symbolfile_addr +
245 section_sp->GetFileOffset());
247 target.SetSectionLoadAddress(section_sp, lower, true);
248 uint64_t upper = lower + section_sp->GetByteSize();
249 if (lower < min_addr)
251 if (upper > max_addr)
253 // This is an upper bound, but a good enough heuristic
254 vmaddrheuristic += section_sp->GetByteSize();
260 bool JITLoaderGDB::ReadJITDescriptor(bool all_entries) {
261 if (m_process->GetTarget().GetArchitecture().GetAddressByteSize() == 8)
262 return ReadJITDescriptorImpl<uint64_t>(all_entries);
264 return ReadJITDescriptorImpl<uint32_t>(all_entries);
267 template <typename ptr_t>
268 bool JITLoaderGDB::ReadJITDescriptorImpl(bool all_entries) {
269 if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS)
272 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER));
273 Target &target = m_process->GetTarget();
274 ModuleList &module_list = target.GetImages();
276 jit_descriptor<ptr_t> jit_desc;
277 const size_t jit_desc_size = sizeof(jit_desc);
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()) {
283 log->Printf("JITLoaderGDB::%s failed to read JIT descriptor",
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;
291 jit_action = JIT_REGISTER_FN;
292 jit_relevant_entry = (addr_t)jit_desc.first_entry;
295 while (jit_relevant_entry != 0) {
296 jit_code_entry<ptr_t> jit_entry;
297 if (!ReadJITEntry(jit_relevant_entry, m_process, &jit_entry)) {
299 log->Printf("JITLoaderGDB::%s failed to read JIT entry at 0x%" PRIx64,
300 __FUNCTION__, jit_relevant_entry);
304 const addr_t &symbolfile_addr = (addr_t)jit_entry.symfile_addr;
305 const size_t &symbolfile_size = (size_t)jit_entry.symfile_size;
308 if (jit_action == JIT_REGISTER_FN) {
310 log->Printf("JITLoaderGDB::%s registering JIT entry at 0x%" PRIx64
311 " (%" PRIu64 " bytes)",
312 __FUNCTION__, symbolfile_addr, (uint64_t)symbolfile_size);
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);
319 if (module_sp && module_sp->GetObjectFile()) {
320 // load the symbol table right away
321 module_sp->GetObjectFile()->GetSymtab();
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();
331 uint64_t vmaddrheuristic = 0;
332 uint64_t lower = (uint64_t)-1;
334 updateSectionLoadAddress(*section_list, target, symbolfile_addr,
335 symbolfile_size, vmaddrheuristic, lower,
340 bool changed = false;
341 module_sp->SetLoadAddress(target, 0, true, changed);
344 module_list.AppendIfNeeded(module_sp);
346 ModuleList module_list;
347 module_list.Append(module_sp);
348 target.ModulesDidLoad(module_list);
351 log->Printf("JITLoaderGDB::%s failed to load module for "
352 "JIT entry at 0x%" PRIx64,
353 __FUNCTION__, symbolfile_addr);
355 } else if (jit_action == JIT_UNREGISTER_FN) {
357 log->Printf("JITLoaderGDB::%s unregistering JIT entry at 0x%" PRIx64,
358 __FUNCTION__, symbolfile_addr);
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();
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));
371 target.GetSectionLoadList().SetSectionUnloaded(section_sp);
376 module_list.Remove(module_sp);
377 m_jit_objects.erase(it);
379 } else if (jit_action == JIT_NOACTION) {
382 assert(false && "Unknown jit action");
386 jit_relevant_entry = (addr_t)jit_entry.next_entry;
388 jit_relevant_entry = 0;
391 return false; // Continue Running.
394 //------------------------------------------------------------------
395 // PluginInterface protocol
396 //------------------------------------------------------------------
397 lldb_private::ConstString JITLoaderGDB::GetPluginNameStatic() {
398 static ConstString g_name("gdb");
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;
410 const char *JITLoaderGDB::GetPluginDescriptionStatic() {
411 return "JIT loader plug-in that watches for JIT events using the GDB "
415 lldb_private::ConstString JITLoaderGDB::GetPluginName() {
416 return GetPluginNameStatic();
419 uint32_t JITLoaderGDB::GetPluginVersion() { return 1; }
421 void JITLoaderGDB::Initialize() {
422 PluginManager::RegisterPlugin(GetPluginNameStatic(),
423 GetPluginDescriptionStatic(), CreateInstance,
427 void JITLoaderGDB::Terminate() {
428 PluginManager::UnregisterPlugin(CreateInstance);
431 bool JITLoaderGDB::DidSetJITBreakpoint() const {
432 return LLDB_BREAK_ID_IS_VALID(m_jit_break_id);
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();
441 if (!module_list.FindSymbolsWithNameAndType(name, symbol_type,
443 return LLDB_INVALID_ADDRESS;
445 SymbolContext sym_ctx;
446 target_symbols.GetContextAtIndex(0, sym_ctx);
448 const Address jit_descriptor_addr = sym_ctx.symbol->GetAddress();
449 if (!jit_descriptor_addr.IsValid())
450 return LLDB_INVALID_ADDRESS;
452 const addr_t jit_addr = jit_descriptor_addr.GetLoadAddress(&target);