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