Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 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 | |
Siva Chandra | 02f593f | 2016-03-23 23:27:23 +0000 | [diff] [blame] | 12 | #include "llvm/Support/MathExtras.h" |
| 13 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 14 | #include "lldb/Breakpoint/Breakpoint.h" |
Siva Chandra | 02f593f | 2016-03-23 23:27:23 +0000 | [diff] [blame] | 15 | #include "lldb/Core/DataBufferHeap.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Log.h" |
| 17 | #include "lldb/Core/Module.h" |
| 18 | #include "lldb/Core/ModuleSpec.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 19 | #include "lldb/Core/PluginManager.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Section.h" |
| 21 | #include "lldb/Core/StreamString.h" |
Oleksiy Vyalov | eff9ad2 | 2015-09-16 17:38:36 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/OptionValueProperties.h" |
Zachary Turner | 32abc6e | 2015-03-03 19:23:09 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/SymbolContext.h" |
| 24 | #include "lldb/Symbol/SymbolVendor.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Process.h" |
| 26 | #include "lldb/Target/SectionLoadList.h" |
| 27 | #include "lldb/Target/Target.h" |
Siva Chandra | 02f593f | 2016-03-23 23:27:23 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/LLDBAssert.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 29 | |
| 30 | #include "JITLoaderGDB.h" |
| 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
Siva Chandra | 02f593f | 2016-03-23 23:27:23 +0000 | [diff] [blame] | 35 | //------------------------------------------------------------------ |
| 36 | // Debug Interface Structures |
| 37 | //------------------------------------------------------------------ |
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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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}}; |
Oleksiy Vyalov | eff9ad2 | 2015-09-16 17:38:36 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 65 | enum { ePropertyEnableJITBreakpoint }; |
Oleksiy Vyalov | eff9ad2 | 2015-09-16 17:38:36 +0000 | [diff] [blame] | 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 67 | class PluginProperties : public Properties { |
| 68 | public: |
| 69 | static ConstString GetSettingName() { |
| 70 | return JITLoaderGDB::GetPluginNameStatic(); |
| 71 | } |
Oleksiy Vyalov | eff9ad2 | 2015-09-16 17:38:36 +0000 | [diff] [blame] | 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 73 | PluginProperties() { |
| 74 | m_collection_sp.reset(new OptionValueProperties(GetSettingName())); |
| 75 | m_collection_sp->Initialize(g_properties); |
| 76 | } |
Oleksiy Vyalov | eff9ad2 | 2015-09-16 17:38:36 +0000 | [diff] [blame] | 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 78 | bool GetEnableJITBreakpoint() const { |
| 79 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 80 | nullptr, ePropertyEnableJITBreakpoint, |
| 81 | g_properties[ePropertyEnableJITBreakpoint].default_uint_value != 0); |
| 82 | } |
| 83 | }; |
Oleksiy Vyalov | eff9ad2 | 2015-09-16 17:38:36 +0000 | [diff] [blame] | 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 85 | typedef std::shared_ptr<PluginProperties> JITLoaderGDBPropertiesSP; |
Oleksiy Vyalov | eff9ad2 | 2015-09-16 17:38:36 +0000 | [diff] [blame] | 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 87 | static const JITLoaderGDBPropertiesSP &GetGlobalPluginProperties() { |
| 88 | static const auto g_settings_sp(std::make_shared<PluginProperties>()); |
| 89 | return g_settings_sp; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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); |
Oleksiy Vyalov | eff9ad2 | 2015-09-16 17:38:36 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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()) |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 160 | SetJITBreakpoint(module_list); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 163 | //------------------------------------------------------------------ |
| 164 | // Setup the JIT Breakpoint |
| 165 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 166 | void JITLoaderGDB::SetJITBreakpoint(lldb_private::ModuleList &module_list) { |
| 167 | if (!GetGlobalPluginProperties()->GetEnableJITBreakpoint()) |
| 168 | return; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 170 | if (DidSetJITBreakpoint()) |
| 171 | return; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 172 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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) { |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 185 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 186 | log->Printf("JITLoaderGDB::%s failed to find JIT descriptor address", |
| 187 | __FUNCTION__); |
| 188 | return; |
| 189 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 191 | if (log) |
| 192 | log->Printf("JITLoaderGDB::%s setting JIT breakpoint", __FUNCTION__); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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(); |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 199 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 200 | ReadJITDescriptor(true); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Greg Clayton | 48672af | 2014-06-24 22:22:43 +0000 | [diff] [blame] | 214 | static void updateSectionLoadAddress(const SectionList §ion_list, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 215 | Target &target, uint64_t symbolfile_addr, |
Greg Clayton | 48672af | 2014-06-24 22:22:43 +0000 | [diff] [blame] | 216 | uint64_t symbolfile_size, |
| 217 | uint64_t &vmaddrheuristic, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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()); |
Greg Clayton | 48672af | 2014-06-24 22:22:43 +0000 | [diff] [blame] | 246 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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 | } |
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 | } |
Greg Clayton | 48672af | 2014-06-24 22:22:43 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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); |
Todd Fiala | 49bc2d9d | 2014-09-15 19:55:27 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Siva Chandra | 02f593f | 2016-03-23 23:27:23 +0000 | [diff] [blame] | 267 | template <typename ptr_t> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 268 | bool JITLoaderGDB::ReadJITDescriptorImpl(bool all_entries) { |
| 269 | if (m_jit_descriptor_addr == LLDB_INVALID_ADDRESS) |
| 270 | return false; |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 271 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 272 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_JIT_LOADER)); |
| 273 | Target &target = m_process->GetTarget(); |
| 274 | ModuleList &module_list = target.GetImages(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 275 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 294 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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, |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 300 | __FUNCTION__, jit_relevant_entry); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 301 | return false; |
| 302 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 303 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 307 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 308 | if (jit_action == JIT_REGISTER_FN) { |
| 309 | if (log) |
| 310 | log->Printf("JITLoaderGDB::%s registering JIT entry at 0x%" PRIx64 |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 311 | " (%" PRIu64 " bytes)", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 312 | __FUNCTION__, symbolfile_addr, (uint64_t)symbolfile_size); |
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 | 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); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 318 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 319 | if (module_sp && module_sp->GetObjectFile()) { |
| 320 | // load the symbol table right away |
| 321 | module_sp->GetObjectFile()->GetSymtab(); |
Tamas Berghammer | 31a2f8f | 2016-02-25 12:23:43 +0000 | [diff] [blame] | 322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 337 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 338 | } |
| 339 | } else { |
| 340 | bool changed = false; |
| 341 | module_sp->SetLoadAddress(target, 0, true, changed); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 342 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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, |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 358 | __FUNCTION__, symbolfile_addr); |
| 359 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 373 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 374 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 375 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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"); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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. |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | //------------------------------------------------------------------ |
| 395 | // PluginInterface protocol |
| 396 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 397 | lldb_private::ConstString JITLoaderGDB::GetPluginNameStatic() { |
| 398 | static ConstString g_name("gdb"); |
| 399 | return g_name; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 410 | const char *JITLoaderGDB::GetPluginDescriptionStatic() { |
| 411 | return "JIT loader plug-in that watches for JIT events using the GDB " |
| 412 | "interface."; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 415 | lldb_private::ConstString JITLoaderGDB::GetPluginName() { |
| 416 | return GetPluginNameStatic(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 419 | uint32_t JITLoaderGDB::GetPluginVersion() { return 1; } |
| 420 | |
| 421 | void JITLoaderGDB::Initialize() { |
| 422 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 423 | GetPluginDescriptionStatic(), CreateInstance, |
| 424 | DebuggerInitialize); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 427 | void JITLoaderGDB::Terminate() { |
| 428 | PluginManager::UnregisterPlugin(CreateInstance); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 431 | bool JITLoaderGDB::DidSetJITBreakpoint() const { |
| 432 | return LLDB_BREAK_ID_IS_VALID(m_jit_break_id); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 441 | if (!module_list.FindSymbolsWithNameAndType(name, symbol_type, |
| 442 | target_symbols)) |
| 443 | return LLDB_INVALID_ADDRESS; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 444 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 445 | SymbolContext sym_ctx; |
| 446 | target_symbols.GetContextAtIndex(0, sym_ctx); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 447 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 448 | const Address jit_descriptor_addr = sym_ctx.symbol->GetAddress(); |
| 449 | if (!jit_descriptor_addr.IsValid()) |
| 450 | return LLDB_INVALID_ADDRESS; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 451 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 452 | const addr_t jit_addr = jit_descriptor_addr.GetLoadAddress(&target); |
| 453 | return jit_addr; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 454 | } |