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