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