1 //===-- SBTarget.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 #include "lldb/API/SBTarget.h" 11 12 #include "lldb/lldb-public.h" 13 14 #include "lldb/API/SBBreakpoint.h" 15 #include "lldb/API/SBDebugger.h" 16 #include "lldb/API/SBEvent.h" 17 #include "lldb/API/SBExpressionOptions.h" 18 #include "lldb/API/SBFileSpec.h" 19 #include "lldb/API/SBListener.h" 20 #include "lldb/API/SBModule.h" 21 #include "lldb/API/SBModuleSpec.h" 22 #include "lldb/API/SBProcess.h" 23 #include "lldb/API/SBSourceManager.h" 24 #include "lldb/API/SBStream.h" 25 #include "lldb/API/SBStringList.h" 26 #include "lldb/API/SBSymbolContextList.h" 27 #include "lldb/Breakpoint/BreakpointID.h" 28 #include "lldb/Breakpoint/BreakpointIDList.h" 29 #include "lldb/Breakpoint/BreakpointList.h" 30 #include "lldb/Breakpoint/BreakpointLocation.h" 31 #include "lldb/Core/Address.h" 32 #include "lldb/Core/AddressResolver.h" 33 #include "lldb/Core/AddressResolverName.h" 34 #include "lldb/Core/ArchSpec.h" 35 #include "lldb/Core/Debugger.h" 36 #include "lldb/Core/Disassembler.h" 37 #include "lldb/Core/Log.h" 38 #include "lldb/Core/Module.h" 39 #include "lldb/Core/ModuleSpec.h" 40 #include "lldb/Core/RegularExpression.h" 41 #include "lldb/Core/STLUtils.h" 42 #include "lldb/Core/SearchFilter.h" 43 #include "lldb/Core/Section.h" 44 #include "lldb/Core/ValueObjectConstResult.h" 45 #include "lldb/Core/ValueObjectList.h" 46 #include "lldb/Core/ValueObjectVariable.h" 47 #include "lldb/Host/FileSpec.h" 48 #include "lldb/Host/Host.h" 49 #include "lldb/Interpreter/Args.h" 50 #include "lldb/Symbol/ClangASTContext.h" 51 #include "lldb/Symbol/DeclVendor.h" 52 #include "lldb/Symbol/ObjectFile.h" 53 #include "lldb/Symbol/SymbolFile.h" 54 #include "lldb/Symbol/SymbolVendor.h" 55 #include "lldb/Symbol/VariableList.h" 56 #include "lldb/Target/ABI.h" 57 #include "lldb/Target/Language.h" 58 #include "lldb/Target/LanguageRuntime.h" 59 #include "lldb/Target/ObjCLanguageRuntime.h" 60 #include "lldb/Target/Process.h" 61 #include "lldb/Target/StackFrame.h" 62 #include "lldb/Target/Target.h" 63 #include "lldb/Target/TargetList.h" 64 65 #include "../source/Commands/CommandObjectBreakpoint.h" 66 #include "lldb/Interpreter/CommandReturnObject.h" 67 #include "llvm/Support/Regex.h" 68 69 using namespace lldb; 70 using namespace lldb_private; 71 72 #define DEFAULT_DISASM_BYTE_SIZE 32 73 74 namespace { 75 76 Error AttachToProcess(ProcessAttachInfo &attach_info, Target &target) { 77 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex()); 78 79 auto process_sp = target.GetProcessSP(); 80 if (process_sp) { 81 const auto state = process_sp->GetState(); 82 if (process_sp->IsAlive() && state == eStateConnected) { 83 // If we are already connected, then we have already specified the 84 // listener, so if a valid listener is supplied, we need to error out 85 // to let the client know. 86 if (attach_info.GetListener()) 87 return Error("process is connected and already has a listener, pass " 88 "empty listener"); 89 } 90 } 91 92 return target.Attach(attach_info, nullptr); 93 } 94 95 } // namespace 96 97 //---------------------------------------------------------------------- 98 // SBTarget constructor 99 //---------------------------------------------------------------------- 100 SBTarget::SBTarget() : m_opaque_sp() {} 101 102 SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {} 103 104 SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {} 105 106 const SBTarget &SBTarget::operator=(const SBTarget &rhs) { 107 if (this != &rhs) 108 m_opaque_sp = rhs.m_opaque_sp; 109 return *this; 110 } 111 112 //---------------------------------------------------------------------- 113 // Destructor 114 //---------------------------------------------------------------------- 115 SBTarget::~SBTarget() {} 116 117 bool SBTarget::EventIsTargetEvent(const SBEvent &event) { 118 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL; 119 } 120 121 SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) { 122 return Target::TargetEventData::GetTargetFromEvent(event.get()); 123 } 124 125 uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) { 126 const ModuleList module_list = 127 Target::TargetEventData::GetModuleListFromEvent(event.get()); 128 return module_list.GetSize(); 129 } 130 131 SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx, 132 const SBEvent &event) { 133 const ModuleList module_list = 134 Target::TargetEventData::GetModuleListFromEvent(event.get()); 135 return SBModule(module_list.GetModuleAtIndex(idx)); 136 } 137 138 const char *SBTarget::GetBroadcasterClassName() { 139 return Target::GetStaticBroadcasterClass().AsCString(); 140 } 141 142 bool SBTarget::IsValid() const { 143 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid(); 144 } 145 146 SBProcess SBTarget::GetProcess() { 147 SBProcess sb_process; 148 ProcessSP process_sp; 149 TargetSP target_sp(GetSP()); 150 if (target_sp) { 151 process_sp = target_sp->GetProcessSP(); 152 sb_process.SetSP(process_sp); 153 } 154 155 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 156 if (log) 157 log->Printf("SBTarget(%p)::GetProcess () => SBProcess(%p)", 158 static_cast<void *>(target_sp.get()), 159 static_cast<void *>(process_sp.get())); 160 161 return sb_process; 162 } 163 164 SBPlatform SBTarget::GetPlatform() { 165 TargetSP target_sp(GetSP()); 166 if (!target_sp) 167 return SBPlatform(); 168 169 SBPlatform platform; 170 platform.m_opaque_sp = target_sp->GetPlatform(); 171 172 return platform; 173 } 174 175 SBDebugger SBTarget::GetDebugger() const { 176 SBDebugger debugger; 177 TargetSP target_sp(GetSP()); 178 if (target_sp) 179 debugger.reset(target_sp->GetDebugger().shared_from_this()); 180 return debugger; 181 } 182 183 SBProcess SBTarget::LoadCore(const char *core_file) { 184 SBProcess sb_process; 185 TargetSP target_sp(GetSP()); 186 if (target_sp) { 187 FileSpec filespec(core_file, true); 188 ProcessSP process_sp(target_sp->CreateProcess( 189 target_sp->GetDebugger().GetListener(), NULL, &filespec)); 190 if (process_sp) { 191 process_sp->LoadCore(); 192 sb_process.SetSP(process_sp); 193 } 194 } 195 return sb_process; 196 } 197 198 SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp, 199 const char *working_directory) { 200 char *stdin_path = NULL; 201 char *stdout_path = NULL; 202 char *stderr_path = NULL; 203 uint32_t launch_flags = 0; 204 bool stop_at_entry = false; 205 SBError error; 206 SBListener listener = GetDebugger().GetListener(); 207 return Launch(listener, argv, envp, stdin_path, stdout_path, stderr_path, 208 working_directory, launch_flags, stop_at_entry, error); 209 } 210 211 SBError SBTarget::Install() { 212 SBError sb_error; 213 TargetSP target_sp(GetSP()); 214 if (target_sp) { 215 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 216 sb_error.ref() = target_sp->Install(NULL); 217 } 218 return sb_error; 219 } 220 221 SBProcess SBTarget::Launch(SBListener &listener, char const **argv, 222 char const **envp, const char *stdin_path, 223 const char *stdout_path, const char *stderr_path, 224 const char *working_directory, 225 uint32_t launch_flags, // See LaunchFlags 226 bool stop_at_entry, lldb::SBError &error) { 227 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 228 229 SBProcess sb_process; 230 ProcessSP process_sp; 231 TargetSP target_sp(GetSP()); 232 233 if (log) 234 log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, " 235 "stderr=%s, working-dir=%s, launch_flags=0x%x, " 236 "stop_at_entry=%i, &error (%p))...", 237 static_cast<void *>(target_sp.get()), static_cast<void *>(argv), 238 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL", 239 stdout_path ? stdout_path : "NULL", 240 stderr_path ? stderr_path : "NULL", 241 working_directory ? working_directory : "NULL", launch_flags, 242 stop_at_entry, static_cast<void *>(error.get())); 243 244 if (target_sp) { 245 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 246 247 if (stop_at_entry) 248 launch_flags |= eLaunchFlagStopAtEntry; 249 250 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR")) 251 launch_flags |= eLaunchFlagDisableASLR; 252 253 StateType state = eStateInvalid; 254 process_sp = target_sp->GetProcessSP(); 255 if (process_sp) { 256 state = process_sp->GetState(); 257 258 if (process_sp->IsAlive() && state != eStateConnected) { 259 if (state == eStateAttaching) 260 error.SetErrorString("process attach is in progress"); 261 else 262 error.SetErrorString("a process is already being debugged"); 263 return sb_process; 264 } 265 } 266 267 if (state == eStateConnected) { 268 // If we are already connected, then we have already specified the 269 // listener, so if a valid listener is supplied, we need to error out 270 // to let the client know. 271 if (listener.IsValid()) { 272 error.SetErrorString("process is connected and already has a listener, " 273 "pass empty listener"); 274 return sb_process; 275 } 276 } 277 278 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO")) 279 launch_flags |= eLaunchFlagDisableSTDIO; 280 281 ProcessLaunchInfo launch_info( 282 FileSpec{stdin_path, false}, FileSpec{stdout_path, false}, 283 FileSpec{stderr_path, false}, FileSpec{working_directory, false}, 284 launch_flags); 285 286 Module *exe_module = target_sp->GetExecutableModulePointer(); 287 if (exe_module) 288 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true); 289 if (argv) 290 launch_info.GetArguments().AppendArguments(argv); 291 if (envp) 292 launch_info.GetEnvironmentEntries().SetArguments(envp); 293 294 if (listener.IsValid()) 295 launch_info.SetListener(listener.GetSP()); 296 297 error.SetError(target_sp->Launch(launch_info, NULL)); 298 299 sb_process.SetSP(target_sp->GetProcessSP()); 300 } else { 301 error.SetErrorString("SBTarget is invalid"); 302 } 303 304 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API); 305 if (log) 306 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)", 307 static_cast<void *>(target_sp.get()), 308 static_cast<void *>(sb_process.GetSP().get()), 309 error.GetCString()); 310 311 return sb_process; 312 } 313 314 SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) { 315 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 316 317 SBProcess sb_process; 318 TargetSP target_sp(GetSP()); 319 320 if (log) 321 log->Printf("SBTarget(%p)::Launch (launch_info, error)...", 322 static_cast<void *>(target_sp.get())); 323 324 if (target_sp) { 325 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 326 StateType state = eStateInvalid; 327 { 328 ProcessSP process_sp = target_sp->GetProcessSP(); 329 if (process_sp) { 330 state = process_sp->GetState(); 331 332 if (process_sp->IsAlive() && state != eStateConnected) { 333 if (state == eStateAttaching) 334 error.SetErrorString("process attach is in progress"); 335 else 336 error.SetErrorString("a process is already being debugged"); 337 return sb_process; 338 } 339 } 340 } 341 342 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref(); 343 344 if (!launch_info.GetExecutableFile()) { 345 Module *exe_module = target_sp->GetExecutableModulePointer(); 346 if (exe_module) 347 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true); 348 } 349 350 const ArchSpec &arch_spec = target_sp->GetArchitecture(); 351 if (arch_spec.IsValid()) 352 launch_info.GetArchitecture() = arch_spec; 353 354 error.SetError(target_sp->Launch(launch_info, NULL)); 355 sb_process.SetSP(target_sp->GetProcessSP()); 356 } else { 357 error.SetErrorString("SBTarget is invalid"); 358 } 359 360 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API); 361 if (log) 362 log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)", 363 static_cast<void *>(target_sp.get()), 364 static_cast<void *>(sb_process.GetSP().get())); 365 366 return sb_process; 367 } 368 369 lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) { 370 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 371 372 SBProcess sb_process; 373 TargetSP target_sp(GetSP()); 374 375 if (log) 376 log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...", 377 static_cast<void *>(target_sp.get())); 378 379 if (target_sp) { 380 ProcessAttachInfo &attach_info = sb_attach_info.ref(); 381 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) { 382 PlatformSP platform_sp = target_sp->GetPlatform(); 383 // See if we can pre-verify if a process exists or not 384 if (platform_sp && platform_sp->IsConnected()) { 385 lldb::pid_t attach_pid = attach_info.GetProcessID(); 386 ProcessInstanceInfo instance_info; 387 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) { 388 attach_info.SetUserID(instance_info.GetEffectiveUserID()); 389 } else { 390 error.ref().SetErrorStringWithFormat( 391 "no process found with process ID %" PRIu64, attach_pid); 392 if (log) { 393 log->Printf("SBTarget(%p)::Attach (...) => error %s", 394 static_cast<void *>(target_sp.get()), 395 error.GetCString()); 396 } 397 return sb_process; 398 } 399 } 400 } 401 error.SetError(AttachToProcess(attach_info, *target_sp)); 402 if (error.Success()) 403 sb_process.SetSP(target_sp->GetProcessSP()); 404 } else { 405 error.SetErrorString("SBTarget is invalid"); 406 } 407 408 if (log) 409 log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)", 410 static_cast<void *>(target_sp.get()), 411 static_cast<void *>(sb_process.GetSP().get())); 412 413 return sb_process; 414 } 415 416 #if defined(__APPLE__) 417 418 lldb::SBProcess SBTarget::AttachToProcessWithID(SBListener &listener, 419 ::pid_t pid, 420 lldb::SBError &error) { 421 return AttachToProcessWithID(listener, (lldb::pid_t)pid, error); 422 } 423 424 #endif // #if defined(__APPLE__) 425 426 lldb::SBProcess SBTarget::AttachToProcessWithID( 427 SBListener &listener, 428 lldb::pid_t pid, // The process ID to attach to 429 SBError &error // An error explaining what went wrong if attach fails 430 ) { 431 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 432 433 SBProcess sb_process; 434 TargetSP target_sp(GetSP()); 435 436 if (log) 437 log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...", 438 static_cast<void *>(target_sp.get()), __FUNCTION__, pid); 439 440 if (target_sp) { 441 ProcessAttachInfo attach_info; 442 attach_info.SetProcessID(pid); 443 if (listener.IsValid()) 444 attach_info.SetListener(listener.GetSP()); 445 446 ProcessInstanceInfo instance_info; 447 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info)) 448 attach_info.SetUserID(instance_info.GetEffectiveUserID()); 449 450 error.SetError(AttachToProcess(attach_info, *target_sp)); 451 if (error.Success()) 452 sb_process.SetSP(target_sp->GetProcessSP()); 453 } else 454 error.SetErrorString("SBTarget is invalid"); 455 456 if (log) 457 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)", 458 static_cast<void *>(target_sp.get()), __FUNCTION__, 459 static_cast<void *>(sb_process.GetSP().get())); 460 return sb_process; 461 } 462 463 lldb::SBProcess SBTarget::AttachToProcessWithName( 464 SBListener &listener, 465 const char *name, // basename of process to attach to 466 bool wait_for, // if true wait for a new instance of "name" to be launched 467 SBError &error // An error explaining what went wrong if attach fails 468 ) { 469 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 470 471 SBProcess sb_process; 472 TargetSP target_sp(GetSP()); 473 474 if (log) 475 log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...", 476 static_cast<void *>(target_sp.get()), __FUNCTION__, name, 477 wait_for ? "true" : "false"); 478 479 if (name && target_sp) { 480 ProcessAttachInfo attach_info; 481 attach_info.GetExecutableFile().SetFile(name, false); 482 attach_info.SetWaitForLaunch(wait_for); 483 if (listener.IsValid()) 484 attach_info.SetListener(listener.GetSP()); 485 486 error.SetError(AttachToProcess(attach_info, *target_sp)); 487 if (error.Success()) 488 sb_process.SetSP(target_sp->GetProcessSP()); 489 } else 490 error.SetErrorString("SBTarget is invalid"); 491 492 if (log) 493 log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)", 494 static_cast<void *>(target_sp.get()), __FUNCTION__, 495 static_cast<void *>(sb_process.GetSP().get())); 496 return sb_process; 497 } 498 499 lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url, 500 const char *plugin_name, 501 SBError &error) { 502 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 503 504 SBProcess sb_process; 505 ProcessSP process_sp; 506 TargetSP target_sp(GetSP()); 507 508 if (log) 509 log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, " 510 "plugin_name=%s, error)...", 511 static_cast<void *>(target_sp.get()), url, plugin_name); 512 513 if (target_sp) { 514 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 515 if (listener.IsValid()) 516 process_sp = 517 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL); 518 else 519 process_sp = target_sp->CreateProcess( 520 target_sp->GetDebugger().GetListener(), plugin_name, NULL); 521 522 if (process_sp) { 523 sb_process.SetSP(process_sp); 524 error.SetError(process_sp->ConnectRemote(NULL, url)); 525 } else { 526 error.SetErrorString("unable to create lldb_private::Process"); 527 } 528 } else { 529 error.SetErrorString("SBTarget is invalid"); 530 } 531 532 if (log) 533 log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)", 534 static_cast<void *>(target_sp.get()), 535 static_cast<void *>(process_sp.get())); 536 return sb_process; 537 } 538 539 SBFileSpec SBTarget::GetExecutable() { 540 541 SBFileSpec exe_file_spec; 542 TargetSP target_sp(GetSP()); 543 if (target_sp) { 544 Module *exe_module = target_sp->GetExecutableModulePointer(); 545 if (exe_module) 546 exe_file_spec.SetFileSpec(exe_module->GetFileSpec()); 547 } 548 549 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 550 if (log) { 551 log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)", 552 static_cast<void *>(target_sp.get()), 553 static_cast<const void *>(exe_file_spec.get())); 554 } 555 556 return exe_file_spec; 557 } 558 559 bool SBTarget::operator==(const SBTarget &rhs) const { 560 return m_opaque_sp.get() == rhs.m_opaque_sp.get(); 561 } 562 563 bool SBTarget::operator!=(const SBTarget &rhs) const { 564 return m_opaque_sp.get() != rhs.m_opaque_sp.get(); 565 } 566 567 lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; } 568 569 void SBTarget::SetSP(const lldb::TargetSP &target_sp) { 570 m_opaque_sp = target_sp; 571 } 572 573 lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) { 574 lldb::SBAddress sb_addr; 575 Address &addr = sb_addr.ref(); 576 TargetSP target_sp(GetSP()); 577 if (target_sp) { 578 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 579 if (target_sp->ResolveLoadAddress(vm_addr, addr)) 580 return sb_addr; 581 } 582 583 // We have a load address that isn't in a section, just return an address 584 // with the offset filled in (the address) and the section set to NULL 585 addr.SetRawAddress(vm_addr); 586 return sb_addr; 587 } 588 589 lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) { 590 lldb::SBAddress sb_addr; 591 Address &addr = sb_addr.ref(); 592 TargetSP target_sp(GetSP()); 593 if (target_sp) { 594 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 595 if (target_sp->ResolveFileAddress(file_addr, addr)) 596 return sb_addr; 597 } 598 599 addr.SetRawAddress(file_addr); 600 return sb_addr; 601 } 602 603 lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id, 604 lldb::addr_t vm_addr) { 605 lldb::SBAddress sb_addr; 606 Address &addr = sb_addr.ref(); 607 TargetSP target_sp(GetSP()); 608 if (target_sp) { 609 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 610 if (target_sp->ResolveLoadAddress(vm_addr, addr)) 611 return sb_addr; 612 } 613 614 // We have a load address that isn't in a section, just return an address 615 // with the offset filled in (the address) and the section set to NULL 616 addr.SetRawAddress(vm_addr); 617 return sb_addr; 618 } 619 620 SBSymbolContext 621 SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr, 622 uint32_t resolve_scope) { 623 SBSymbolContext sc; 624 if (addr.IsValid()) { 625 TargetSP target_sp(GetSP()); 626 if (target_sp) 627 target_sp->GetImages().ResolveSymbolContextForAddress( 628 addr.ref(), resolve_scope, sc.ref()); 629 } 630 return sc; 631 } 632 633 size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size, 634 lldb::SBError &error) { 635 SBError sb_error; 636 size_t bytes_read = 0; 637 TargetSP target_sp(GetSP()); 638 if (target_sp) { 639 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 640 bytes_read = 641 target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref()); 642 } else { 643 sb_error.SetErrorString("invalid target"); 644 } 645 646 return bytes_read; 647 } 648 649 SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file, 650 uint32_t line) { 651 return SBBreakpoint( 652 BreakpointCreateByLocation(SBFileSpec(file, false), line)); 653 } 654 655 SBBreakpoint 656 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec, 657 uint32_t line) { 658 return BreakpointCreateByLocation(sb_file_spec, line, 0); 659 } 660 661 SBBreakpoint 662 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec, 663 uint32_t line, lldb::addr_t offset) { 664 SBFileSpecList empty_list; 665 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list); 666 } 667 668 SBBreakpoint 669 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec, 670 uint32_t line, lldb::addr_t offset, 671 SBFileSpecList &sb_module_list) { 672 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 673 674 SBBreakpoint sb_bp; 675 TargetSP target_sp(GetSP()); 676 if (target_sp && line != 0) { 677 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 678 679 const LazyBool check_inlines = eLazyBoolCalculate; 680 const LazyBool skip_prologue = eLazyBoolCalculate; 681 const bool internal = false; 682 const bool hardware = false; 683 const LazyBool move_to_nearest_code = eLazyBoolCalculate; 684 const FileSpecList *module_list = nullptr; 685 if (sb_module_list.GetSize() > 0) { 686 module_list = sb_module_list.get(); 687 } 688 *sb_bp = target_sp->CreateBreakpoint( 689 module_list, *sb_file_spec, line, offset, check_inlines, skip_prologue, 690 internal, hardware, move_to_nearest_code); 691 } 692 693 if (log) { 694 SBStream sstr; 695 sb_bp.GetDescription(sstr); 696 char path[PATH_MAX]; 697 sb_file_spec->GetPath(path, sizeof(path)); 698 log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => " 699 "SBBreakpoint(%p): %s", 700 static_cast<void *>(target_sp.get()), path, line, 701 static_cast<void *>(sb_bp.get()), sstr.GetData()); 702 } 703 704 return sb_bp; 705 } 706 707 SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name, 708 const char *module_name) { 709 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 710 711 SBBreakpoint sb_bp; 712 TargetSP target_sp(GetSP()); 713 if (target_sp.get()) { 714 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 715 716 const bool internal = false; 717 const bool hardware = false; 718 const LazyBool skip_prologue = eLazyBoolCalculate; 719 const lldb::addr_t offset = 0; 720 if (module_name && module_name[0]) { 721 FileSpecList module_spec_list; 722 module_spec_list.Append(FileSpec(module_name, false)); 723 *sb_bp = target_sp->CreateBreakpoint( 724 &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, 725 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware); 726 } else { 727 *sb_bp = target_sp->CreateBreakpoint( 728 NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown, 729 offset, skip_prologue, internal, hardware); 730 } 731 } 732 733 if (log) 734 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", " 735 "module=\"%s\") => SBBreakpoint(%p)", 736 static_cast<void *>(target_sp.get()), symbol_name, module_name, 737 static_cast<void *>(sb_bp.get())); 738 739 return sb_bp; 740 } 741 742 lldb::SBBreakpoint 743 SBTarget::BreakpointCreateByName(const char *symbol_name, 744 const SBFileSpecList &module_list, 745 const SBFileSpecList &comp_unit_list) { 746 uint32_t name_type_mask = eFunctionNameTypeAuto; 747 return BreakpointCreateByName(symbol_name, name_type_mask, 748 eLanguageTypeUnknown, module_list, 749 comp_unit_list); 750 } 751 752 lldb::SBBreakpoint SBTarget::BreakpointCreateByName( 753 const char *symbol_name, uint32_t name_type_mask, 754 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) { 755 return BreakpointCreateByName(symbol_name, name_type_mask, 756 eLanguageTypeUnknown, module_list, 757 comp_unit_list); 758 } 759 760 lldb::SBBreakpoint SBTarget::BreakpointCreateByName( 761 const char *symbol_name, uint32_t name_type_mask, 762 LanguageType symbol_language, const SBFileSpecList &module_list, 763 const SBFileSpecList &comp_unit_list) { 764 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 765 766 SBBreakpoint sb_bp; 767 TargetSP target_sp(GetSP()); 768 if (target_sp && symbol_name && symbol_name[0]) { 769 const bool internal = false; 770 const bool hardware = false; 771 const LazyBool skip_prologue = eLazyBoolCalculate; 772 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 773 *sb_bp = target_sp->CreateBreakpoint( 774 module_list.get(), comp_unit_list.get(), symbol_name, name_type_mask, 775 symbol_language, 0, skip_prologue, internal, hardware); 776 } 777 778 if (log) 779 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", " 780 "name_type: %d) => SBBreakpoint(%p)", 781 static_cast<void *>(target_sp.get()), symbol_name, 782 name_type_mask, static_cast<void *>(sb_bp.get())); 783 784 return sb_bp; 785 } 786 787 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames( 788 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask, 789 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) { 790 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask, 791 eLanguageTypeUnknown, module_list, 792 comp_unit_list); 793 } 794 795 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames( 796 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask, 797 LanguageType symbol_language, const SBFileSpecList &module_list, 798 const SBFileSpecList &comp_unit_list) { 799 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask, 800 eLanguageTypeUnknown, 0, module_list, 801 comp_unit_list); 802 } 803 804 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames( 805 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask, 806 LanguageType symbol_language, lldb::addr_t offset, 807 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) { 808 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 809 810 SBBreakpoint sb_bp; 811 TargetSP target_sp(GetSP()); 812 if (target_sp && num_names > 0) { 813 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 814 const bool internal = false; 815 const bool hardware = false; 816 const LazyBool skip_prologue = eLazyBoolCalculate; 817 *sb_bp = target_sp->CreateBreakpoint( 818 module_list.get(), comp_unit_list.get(), symbol_names, num_names, 819 name_type_mask, symbol_language, offset, skip_prologue, internal, 820 hardware); 821 } 822 823 if (log) { 824 log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={", 825 static_cast<void *>(target_sp.get())); 826 for (uint32_t i = 0; i < num_names; i++) { 827 char sep; 828 if (i < num_names - 1) 829 sep = ','; 830 else 831 sep = '}'; 832 if (symbol_names[i] != NULL) 833 log->Printf("\"%s\"%c ", symbol_names[i], sep); 834 else 835 log->Printf("\"<NULL>\"%c ", sep); 836 } 837 log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask, 838 static_cast<void *>(sb_bp.get())); 839 } 840 841 return sb_bp; 842 } 843 844 SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex, 845 const char *module_name) { 846 SBFileSpecList module_spec_list; 847 SBFileSpecList comp_unit_list; 848 if (module_name && module_name[0]) { 849 module_spec_list.Append(FileSpec(module_name, false)); 850 } 851 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown, 852 module_spec_list, comp_unit_list); 853 } 854 855 lldb::SBBreakpoint 856 SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex, 857 const SBFileSpecList &module_list, 858 const SBFileSpecList &comp_unit_list) { 859 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown, 860 module_list, comp_unit_list); 861 } 862 863 lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex( 864 const char *symbol_name_regex, LanguageType symbol_language, 865 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) { 866 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 867 868 SBBreakpoint sb_bp; 869 TargetSP target_sp(GetSP()); 870 if (target_sp && symbol_name_regex && symbol_name_regex[0]) { 871 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 872 RegularExpression regexp((llvm::StringRef(symbol_name_regex))); 873 const bool internal = false; 874 const bool hardware = false; 875 const LazyBool skip_prologue = eLazyBoolCalculate; 876 877 *sb_bp = target_sp->CreateFuncRegexBreakpoint( 878 module_list.get(), comp_unit_list.get(), regexp, symbol_language, 879 skip_prologue, internal, hardware); 880 } 881 882 if (log) 883 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") " 884 "=> SBBreakpoint(%p)", 885 static_cast<void *>(target_sp.get()), symbol_name_regex, 886 static_cast<void *>(sb_bp.get())); 887 888 return sb_bp; 889 } 890 891 SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) { 892 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 893 894 SBBreakpoint sb_bp; 895 TargetSP target_sp(GetSP()); 896 if (target_sp) { 897 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 898 const bool hardware = false; 899 *sb_bp = target_sp->CreateBreakpoint(address, false, hardware); 900 } 901 902 if (log) 903 log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 904 ") => SBBreakpoint(%p)", 905 static_cast<void *>(target_sp.get()), 906 static_cast<uint64_t>(address), 907 static_cast<void *>(sb_bp.get())); 908 909 return sb_bp; 910 } 911 912 SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) { 913 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 914 915 SBBreakpoint sb_bp; 916 TargetSP target_sp(GetSP()); 917 if (!sb_address.IsValid()) { 918 if (log) 919 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with " 920 "invalid address", 921 static_cast<void *>(target_sp.get())); 922 return sb_bp; 923 } 924 925 if (target_sp) { 926 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 927 const bool hardware = false; 928 *sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware); 929 } 930 931 if (log) { 932 SBStream s; 933 sb_address.GetDescription(s); 934 log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => " 935 "SBBreakpoint(%p)", 936 static_cast<void *>(target_sp.get()), s.GetData(), 937 static_cast<void *>(sb_bp.get())); 938 } 939 940 return sb_bp; 941 } 942 943 lldb::SBBreakpoint 944 SBTarget::BreakpointCreateBySourceRegex(const char *source_regex, 945 const lldb::SBFileSpec &source_file, 946 const char *module_name) { 947 SBFileSpecList module_spec_list; 948 949 if (module_name && module_name[0]) { 950 module_spec_list.Append(FileSpec(module_name, false)); 951 } 952 953 SBFileSpecList source_file_list; 954 if (source_file.IsValid()) { 955 source_file_list.Append(source_file); 956 } 957 958 return BreakpointCreateBySourceRegex(source_regex, module_spec_list, 959 source_file_list); 960 } 961 962 lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex( 963 const char *source_regex, const SBFileSpecList &module_list, 964 const lldb::SBFileSpecList &source_file_list) { 965 return BreakpointCreateBySourceRegex(source_regex, module_list, 966 source_file_list, SBStringList()); 967 } 968 969 lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex( 970 const char *source_regex, const SBFileSpecList &module_list, 971 const lldb::SBFileSpecList &source_file_list, 972 const SBStringList &func_names) { 973 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 974 975 SBBreakpoint sb_bp; 976 TargetSP target_sp(GetSP()); 977 if (target_sp && source_regex && source_regex[0]) { 978 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 979 const bool hardware = false; 980 const LazyBool move_to_nearest_code = eLazyBoolCalculate; 981 RegularExpression regexp((llvm::StringRef(source_regex))); 982 std::unordered_set<std::string> func_names_set; 983 for (size_t i = 0; i < func_names.GetSize(); i++) { 984 func_names_set.insert(func_names.GetStringAtIndex(i)); 985 } 986 987 *sb_bp = target_sp->CreateSourceRegexBreakpoint( 988 module_list.get(), source_file_list.get(), func_names_set, regexp, 989 false, hardware, move_to_nearest_code); 990 } 991 992 if (log) 993 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") " 994 "=> SBBreakpoint(%p)", 995 static_cast<void *>(target_sp.get()), source_regex, 996 static_cast<void *>(sb_bp.get())); 997 998 return sb_bp; 999 } 1000 1001 lldb::SBBreakpoint 1002 SBTarget::BreakpointCreateForException(lldb::LanguageType language, 1003 bool catch_bp, bool throw_bp) { 1004 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1005 1006 SBBreakpoint sb_bp; 1007 TargetSP target_sp(GetSP()); 1008 if (target_sp) { 1009 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1010 const bool hardware = false; 1011 *sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp, 1012 hardware); 1013 } 1014 1015 if (log) 1016 log->Printf("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: " 1017 "%s throw: %s) => SBBreakpoint(%p)", 1018 static_cast<void *>(target_sp.get()), 1019 Language::GetNameForLanguageType(language), 1020 catch_bp ? "on" : "off", throw_bp ? "on" : "off", 1021 static_cast<void *>(sb_bp.get())); 1022 1023 return sb_bp; 1024 } 1025 1026 uint32_t SBTarget::GetNumBreakpoints() const { 1027 TargetSP target_sp(GetSP()); 1028 if (target_sp) { 1029 // The breakpoint list is thread safe, no need to lock 1030 return target_sp->GetBreakpointList().GetSize(); 1031 } 1032 return 0; 1033 } 1034 1035 SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const { 1036 SBBreakpoint sb_breakpoint; 1037 TargetSP target_sp(GetSP()); 1038 if (target_sp) { 1039 // The breakpoint list is thread safe, no need to lock 1040 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx); 1041 } 1042 return sb_breakpoint; 1043 } 1044 1045 bool SBTarget::BreakpointDelete(break_id_t bp_id) { 1046 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1047 1048 bool result = false; 1049 TargetSP target_sp(GetSP()); 1050 if (target_sp) { 1051 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1052 result = target_sp->RemoveBreakpointByID(bp_id); 1053 } 1054 1055 if (log) 1056 log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", 1057 static_cast<void *>(target_sp.get()), 1058 static_cast<uint32_t>(bp_id), result); 1059 1060 return result; 1061 } 1062 1063 SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) { 1064 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1065 1066 SBBreakpoint sb_breakpoint; 1067 TargetSP target_sp(GetSP()); 1068 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) { 1069 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1070 *sb_breakpoint = target_sp->GetBreakpointByID(bp_id); 1071 } 1072 1073 if (log) 1074 log->Printf( 1075 "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)", 1076 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id), 1077 static_cast<void *>(sb_breakpoint.get())); 1078 1079 return sb_breakpoint; 1080 } 1081 1082 bool SBTarget::FindBreakpointsByName(const char *name, 1083 SBBreakpointList &bkpts) { 1084 TargetSP target_sp(GetSP()); 1085 if (target_sp) { 1086 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1087 BreakpointList bkpt_list(false); 1088 bool is_valid = 1089 target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list); 1090 if (!is_valid) 1091 return false; 1092 for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) { 1093 bkpts.AppendByID(bkpt_sp->GetID()); 1094 } 1095 } 1096 return true; 1097 } 1098 1099 bool SBTarget::EnableAllBreakpoints() { 1100 TargetSP target_sp(GetSP()); 1101 if (target_sp) { 1102 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1103 target_sp->EnableAllBreakpoints(); 1104 return true; 1105 } 1106 return false; 1107 } 1108 1109 bool SBTarget::DisableAllBreakpoints() { 1110 TargetSP target_sp(GetSP()); 1111 if (target_sp) { 1112 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1113 target_sp->DisableAllBreakpoints(); 1114 return true; 1115 } 1116 return false; 1117 } 1118 1119 bool SBTarget::DeleteAllBreakpoints() { 1120 TargetSP target_sp(GetSP()); 1121 if (target_sp) { 1122 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1123 target_sp->RemoveAllBreakpoints(); 1124 return true; 1125 } 1126 return false; 1127 } 1128 1129 lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file, 1130 SBBreakpointList &new_bps) { 1131 SBError sberr; 1132 TargetSP target_sp(GetSP()); 1133 if (!target_sp) { 1134 sberr.SetErrorString( 1135 "BreakpointCreateFromFile called with invalid target."); 1136 return sberr; 1137 } 1138 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1139 1140 BreakpointIDList bp_ids; 1141 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(), bp_ids); 1142 if (sberr.Fail()) 1143 return sberr; 1144 1145 size_t num_bkpts = bp_ids.GetSize(); 1146 for (size_t i = 0; i < num_bkpts; i++) { 1147 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i); 1148 new_bps.AppendByID(bp_id.GetBreakpointID()); 1149 } 1150 return sberr; 1151 } 1152 1153 lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) { 1154 SBError sberr; 1155 TargetSP target_sp(GetSP()); 1156 if (!target_sp) { 1157 sberr.SetErrorString("BreakpointWriteToFile called with invalid target."); 1158 return sberr; 1159 } 1160 SBBreakpointList bkpt_list(*this); 1161 return BreakpointsWriteToFile(dest_file, bkpt_list); 1162 } 1163 1164 lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file, 1165 SBBreakpointList &bkpt_list) { 1166 SBError sberr; 1167 TargetSP target_sp(GetSP()); 1168 if (!target_sp) { 1169 sberr.SetErrorString("BreakpointWriteToFile called with invalid target."); 1170 return sberr; 1171 } 1172 1173 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1174 BreakpointIDList bp_id_list; 1175 bkpt_list.CopyToBreakpointIDList(bp_id_list); 1176 sberr.ref() = 1177 target_sp->SerializeBreakpointsToFile(dest_file.ref(), bp_id_list); 1178 return sberr; 1179 } 1180 1181 uint32_t SBTarget::GetNumWatchpoints() const { 1182 TargetSP target_sp(GetSP()); 1183 if (target_sp) { 1184 // The watchpoint list is thread safe, no need to lock 1185 return target_sp->GetWatchpointList().GetSize(); 1186 } 1187 return 0; 1188 } 1189 1190 SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const { 1191 SBWatchpoint sb_watchpoint; 1192 TargetSP target_sp(GetSP()); 1193 if (target_sp) { 1194 // The watchpoint list is thread safe, no need to lock 1195 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx)); 1196 } 1197 return sb_watchpoint; 1198 } 1199 1200 bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) { 1201 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1202 1203 bool result = false; 1204 TargetSP target_sp(GetSP()); 1205 if (target_sp) { 1206 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1207 std::unique_lock<std::recursive_mutex> lock; 1208 target_sp->GetWatchpointList().GetListMutex(lock); 1209 result = target_sp->RemoveWatchpointByID(wp_id); 1210 } 1211 1212 if (log) 1213 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", 1214 static_cast<void *>(target_sp.get()), 1215 static_cast<uint32_t>(wp_id), result); 1216 1217 return result; 1218 } 1219 1220 SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) { 1221 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1222 1223 SBWatchpoint sb_watchpoint; 1224 lldb::WatchpointSP watchpoint_sp; 1225 TargetSP target_sp(GetSP()); 1226 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) { 1227 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1228 std::unique_lock<std::recursive_mutex> lock; 1229 target_sp->GetWatchpointList().GetListMutex(lock); 1230 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id); 1231 sb_watchpoint.SetSP(watchpoint_sp); 1232 } 1233 1234 if (log) 1235 log->Printf( 1236 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)", 1237 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id), 1238 static_cast<void *>(watchpoint_sp.get())); 1239 1240 return sb_watchpoint; 1241 } 1242 1243 lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size, 1244 bool read, bool write, 1245 SBError &error) { 1246 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1247 1248 SBWatchpoint sb_watchpoint; 1249 lldb::WatchpointSP watchpoint_sp; 1250 TargetSP target_sp(GetSP()); 1251 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && 1252 size > 0) { 1253 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1254 uint32_t watch_type = 0; 1255 if (read) 1256 watch_type |= LLDB_WATCH_TYPE_READ; 1257 if (write) 1258 watch_type |= LLDB_WATCH_TYPE_WRITE; 1259 if (watch_type == 0) { 1260 error.SetErrorString( 1261 "Can't create a watchpoint that is neither read nor write."); 1262 return sb_watchpoint; 1263 } 1264 1265 // Target::CreateWatchpoint() is thread safe. 1266 Error cw_error; 1267 // This API doesn't take in a type, so we can't figure out what it is. 1268 CompilerType *type = NULL; 1269 watchpoint_sp = 1270 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error); 1271 error.SetError(cw_error); 1272 sb_watchpoint.SetSP(watchpoint_sp); 1273 } 1274 1275 if (log) 1276 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 1277 ", 0x%u) => SBWatchpoint(%p)", 1278 static_cast<void *>(target_sp.get()), addr, 1279 static_cast<uint32_t>(size), 1280 static_cast<void *>(watchpoint_sp.get())); 1281 1282 return sb_watchpoint; 1283 } 1284 1285 bool SBTarget::EnableAllWatchpoints() { 1286 TargetSP target_sp(GetSP()); 1287 if (target_sp) { 1288 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1289 std::unique_lock<std::recursive_mutex> lock; 1290 target_sp->GetWatchpointList().GetListMutex(lock); 1291 target_sp->EnableAllWatchpoints(); 1292 return true; 1293 } 1294 return false; 1295 } 1296 1297 bool SBTarget::DisableAllWatchpoints() { 1298 TargetSP target_sp(GetSP()); 1299 if (target_sp) { 1300 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1301 std::unique_lock<std::recursive_mutex> lock; 1302 target_sp->GetWatchpointList().GetListMutex(lock); 1303 target_sp->DisableAllWatchpoints(); 1304 return true; 1305 } 1306 return false; 1307 } 1308 1309 SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr, 1310 SBType type) { 1311 SBValue sb_value; 1312 lldb::ValueObjectSP new_value_sp; 1313 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) { 1314 lldb::addr_t load_addr(addr.GetLoadAddress(*this)); 1315 ExecutionContext exe_ctx( 1316 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false))); 1317 CompilerType ast_type(type.GetSP()->GetCompilerType(true)); 1318 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr, 1319 exe_ctx, ast_type); 1320 } 1321 sb_value.SetSP(new_value_sp); 1322 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1323 if (log) { 1324 if (new_value_sp) 1325 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"", 1326 static_cast<void *>(m_opaque_sp.get()), 1327 new_value_sp->GetName().AsCString()); 1328 else 1329 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL", 1330 static_cast<void *>(m_opaque_sp.get())); 1331 } 1332 return sb_value; 1333 } 1334 1335 lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data, 1336 lldb::SBType type) { 1337 SBValue sb_value; 1338 lldb::ValueObjectSP new_value_sp; 1339 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) { 1340 DataExtractorSP extractor(*data); 1341 ExecutionContext exe_ctx( 1342 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false))); 1343 CompilerType ast_type(type.GetSP()->GetCompilerType(true)); 1344 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor, 1345 exe_ctx, ast_type); 1346 } 1347 sb_value.SetSP(new_value_sp); 1348 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1349 if (log) { 1350 if (new_value_sp) 1351 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"", 1352 static_cast<void *>(m_opaque_sp.get()), 1353 new_value_sp->GetName().AsCString()); 1354 else 1355 log->Printf("SBTarget(%p)::CreateValueFromData => NULL", 1356 static_cast<void *>(m_opaque_sp.get())); 1357 } 1358 return sb_value; 1359 } 1360 1361 lldb::SBValue SBTarget::CreateValueFromExpression(const char *name, 1362 const char *expr) { 1363 SBValue sb_value; 1364 lldb::ValueObjectSP new_value_sp; 1365 if (IsValid() && name && *name && expr && *expr) { 1366 ExecutionContext exe_ctx( 1367 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false))); 1368 new_value_sp = 1369 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx); 1370 } 1371 sb_value.SetSP(new_value_sp); 1372 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1373 if (log) { 1374 if (new_value_sp) 1375 log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"", 1376 static_cast<void *>(m_opaque_sp.get()), 1377 new_value_sp->GetName().AsCString()); 1378 else 1379 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL", 1380 static_cast<void *>(m_opaque_sp.get())); 1381 } 1382 return sb_value; 1383 } 1384 1385 bool SBTarget::DeleteAllWatchpoints() { 1386 TargetSP target_sp(GetSP()); 1387 if (target_sp) { 1388 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1389 std::unique_lock<std::recursive_mutex> lock; 1390 target_sp->GetWatchpointList().GetListMutex(lock); 1391 target_sp->RemoveAllWatchpoints(); 1392 return true; 1393 } 1394 return false; 1395 } 1396 1397 lldb::SBModule SBTarget::AddModule(const char *path, const char *triple, 1398 const char *uuid_cstr) { 1399 return AddModule(path, triple, uuid_cstr, NULL); 1400 } 1401 1402 lldb::SBModule SBTarget::AddModule(const char *path, const char *triple, 1403 const char *uuid_cstr, const char *symfile) { 1404 lldb::SBModule sb_module; 1405 TargetSP target_sp(GetSP()); 1406 if (target_sp) { 1407 ModuleSpec module_spec; 1408 if (path) 1409 module_spec.GetFileSpec().SetFile(path, false); 1410 1411 if (uuid_cstr) 1412 module_spec.GetUUID().SetFromCString(uuid_cstr); 1413 1414 if (triple) 1415 module_spec.GetArchitecture().SetTriple(triple, 1416 target_sp->GetPlatform().get()); 1417 else 1418 module_spec.GetArchitecture() = target_sp->GetArchitecture(); 1419 1420 if (symfile) 1421 module_spec.GetSymbolFileSpec().SetFile(symfile, false); 1422 1423 sb_module.SetSP(target_sp->GetSharedModule(module_spec)); 1424 } 1425 return sb_module; 1426 } 1427 1428 lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) { 1429 lldb::SBModule sb_module; 1430 TargetSP target_sp(GetSP()); 1431 if (target_sp) 1432 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap)); 1433 return sb_module; 1434 } 1435 1436 bool SBTarget::AddModule(lldb::SBModule &module) { 1437 TargetSP target_sp(GetSP()); 1438 if (target_sp) { 1439 target_sp->GetImages().AppendIfNeeded(module.GetSP()); 1440 return true; 1441 } 1442 return false; 1443 } 1444 1445 uint32_t SBTarget::GetNumModules() const { 1446 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1447 1448 uint32_t num = 0; 1449 TargetSP target_sp(GetSP()); 1450 if (target_sp) { 1451 // The module list is thread safe, no need to lock 1452 num = target_sp->GetImages().GetSize(); 1453 } 1454 1455 if (log) 1456 log->Printf("SBTarget(%p)::GetNumModules () => %d", 1457 static_cast<void *>(target_sp.get()), num); 1458 1459 return num; 1460 } 1461 1462 void SBTarget::Clear() { 1463 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1464 1465 if (log) 1466 log->Printf("SBTarget(%p)::Clear ()", 1467 static_cast<void *>(m_opaque_sp.get())); 1468 1469 m_opaque_sp.reset(); 1470 } 1471 1472 SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) { 1473 SBModule sb_module; 1474 TargetSP target_sp(GetSP()); 1475 if (target_sp && sb_file_spec.IsValid()) { 1476 ModuleSpec module_spec(*sb_file_spec); 1477 // The module list is thread safe, no need to lock 1478 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec)); 1479 } 1480 return sb_module; 1481 } 1482 1483 lldb::ByteOrder SBTarget::GetByteOrder() { 1484 TargetSP target_sp(GetSP()); 1485 if (target_sp) 1486 return target_sp->GetArchitecture().GetByteOrder(); 1487 return eByteOrderInvalid; 1488 } 1489 1490 const char *SBTarget::GetTriple() { 1491 TargetSP target_sp(GetSP()); 1492 if (target_sp) { 1493 std::string triple(target_sp->GetArchitecture().GetTriple().str()); 1494 // Unique the string so we don't run into ownership issues since 1495 // the const strings put the string into the string pool once and 1496 // the strings never comes out 1497 ConstString const_triple(triple.c_str()); 1498 return const_triple.GetCString(); 1499 } 1500 return NULL; 1501 } 1502 1503 uint32_t SBTarget::GetDataByteSize() { 1504 TargetSP target_sp(GetSP()); 1505 if (target_sp) { 1506 return target_sp->GetArchitecture().GetDataByteSize(); 1507 } 1508 return 0; 1509 } 1510 1511 uint32_t SBTarget::GetCodeByteSize() { 1512 TargetSP target_sp(GetSP()); 1513 if (target_sp) { 1514 return target_sp->GetArchitecture().GetCodeByteSize(); 1515 } 1516 return 0; 1517 } 1518 1519 uint32_t SBTarget::GetAddressByteSize() { 1520 TargetSP target_sp(GetSP()); 1521 if (target_sp) 1522 return target_sp->GetArchitecture().GetAddressByteSize(); 1523 return sizeof(void *); 1524 } 1525 1526 SBModule SBTarget::GetModuleAtIndex(uint32_t idx) { 1527 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1528 1529 SBModule sb_module; 1530 ModuleSP module_sp; 1531 TargetSP target_sp(GetSP()); 1532 if (target_sp) { 1533 // The module list is thread safe, no need to lock 1534 module_sp = target_sp->GetImages().GetModuleAtIndex(idx); 1535 sb_module.SetSP(module_sp); 1536 } 1537 1538 if (log) 1539 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)", 1540 static_cast<void *>(target_sp.get()), idx, 1541 static_cast<void *>(module_sp.get())); 1542 1543 return sb_module; 1544 } 1545 1546 bool SBTarget::RemoveModule(lldb::SBModule module) { 1547 TargetSP target_sp(GetSP()); 1548 if (target_sp) 1549 return target_sp->GetImages().Remove(module.GetSP()); 1550 return false; 1551 } 1552 1553 SBBroadcaster SBTarget::GetBroadcaster() const { 1554 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1555 1556 TargetSP target_sp(GetSP()); 1557 SBBroadcaster broadcaster(target_sp.get(), false); 1558 1559 if (log) 1560 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)", 1561 static_cast<void *>(target_sp.get()), 1562 static_cast<void *>(broadcaster.get())); 1563 1564 return broadcaster; 1565 } 1566 1567 bool SBTarget::GetDescription(SBStream &description, 1568 lldb::DescriptionLevel description_level) { 1569 Stream &strm = description.ref(); 1570 1571 TargetSP target_sp(GetSP()); 1572 if (target_sp) { 1573 target_sp->Dump(&strm, description_level); 1574 } else 1575 strm.PutCString("No value"); 1576 1577 return true; 1578 } 1579 1580 lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name, 1581 uint32_t name_type_mask) { 1582 lldb::SBSymbolContextList sb_sc_list; 1583 if (name && name[0]) { 1584 TargetSP target_sp(GetSP()); 1585 if (target_sp) { 1586 const bool symbols_ok = true; 1587 const bool inlines_ok = true; 1588 const bool append = true; 1589 target_sp->GetImages().FindFunctions(ConstString(name), name_type_mask, 1590 symbols_ok, inlines_ok, append, 1591 *sb_sc_list); 1592 } 1593 } 1594 return sb_sc_list; 1595 } 1596 1597 lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name, 1598 uint32_t max_matches, 1599 MatchType matchtype) { 1600 lldb::SBSymbolContextList sb_sc_list; 1601 if (name && name[0]) { 1602 llvm::StringRef name_ref(name); 1603 TargetSP target_sp(GetSP()); 1604 if (target_sp) { 1605 std::string regexstr; 1606 switch (matchtype) { 1607 case eMatchTypeRegex: 1608 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true, 1609 true, true, *sb_sc_list); 1610 break; 1611 case eMatchTypeStartsWith: 1612 regexstr = llvm::Regex::escape(name) + ".*"; 1613 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true, 1614 true, true, *sb_sc_list); 1615 break; 1616 default: 1617 target_sp->GetImages().FindFunctions(ConstString(name), 1618 eFunctionNameTypeAny, true, true, 1619 true, *sb_sc_list); 1620 break; 1621 } 1622 } 1623 } 1624 return sb_sc_list; 1625 } 1626 1627 lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) { 1628 TargetSP target_sp(GetSP()); 1629 if (typename_cstr && typename_cstr[0] && target_sp) { 1630 ConstString const_typename(typename_cstr); 1631 SymbolContext sc; 1632 const bool exact_match = false; 1633 1634 const ModuleList &module_list = target_sp->GetImages(); 1635 size_t count = module_list.GetSize(); 1636 for (size_t idx = 0; idx < count; idx++) { 1637 ModuleSP module_sp(module_list.GetModuleAtIndex(idx)); 1638 if (module_sp) { 1639 TypeSP type_sp( 1640 module_sp->FindFirstType(sc, const_typename, exact_match)); 1641 if (type_sp) 1642 return SBType(type_sp); 1643 } 1644 } 1645 1646 // Didn't find the type in the symbols; try the Objective-C runtime 1647 // if one is installed 1648 1649 ProcessSP process_sp(target_sp->GetProcessSP()); 1650 1651 if (process_sp) { 1652 ObjCLanguageRuntime *objc_language_runtime = 1653 process_sp->GetObjCLanguageRuntime(); 1654 1655 if (objc_language_runtime) { 1656 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor(); 1657 1658 if (objc_decl_vendor) { 1659 std::vector<clang::NamedDecl *> decls; 1660 1661 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) { 1662 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) { 1663 return SBType(type); 1664 } 1665 } 1666 } 1667 } 1668 } 1669 1670 // No matches, search for basic typename matches 1671 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext(); 1672 if (clang_ast) 1673 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(), 1674 const_typename)); 1675 } 1676 return SBType(); 1677 } 1678 1679 SBType SBTarget::GetBasicType(lldb::BasicType type) { 1680 TargetSP target_sp(GetSP()); 1681 if (target_sp) { 1682 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext(); 1683 if (clang_ast) 1684 return SBType( 1685 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type)); 1686 } 1687 return SBType(); 1688 } 1689 1690 lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) { 1691 SBTypeList sb_type_list; 1692 TargetSP target_sp(GetSP()); 1693 if (typename_cstr && typename_cstr[0] && target_sp) { 1694 ModuleList &images = target_sp->GetImages(); 1695 ConstString const_typename(typename_cstr); 1696 bool exact_match = false; 1697 SymbolContext sc; 1698 TypeList type_list; 1699 llvm::DenseSet<SymbolFile *> searched_symbol_files; 1700 uint32_t num_matches = 1701 images.FindTypes(sc, const_typename, exact_match, UINT32_MAX, 1702 searched_symbol_files, type_list); 1703 1704 if (num_matches > 0) { 1705 for (size_t idx = 0; idx < num_matches; idx++) { 1706 TypeSP type_sp(type_list.GetTypeAtIndex(idx)); 1707 if (type_sp) 1708 sb_type_list.Append(SBType(type_sp)); 1709 } 1710 } 1711 1712 // Try the Objective-C runtime if one is installed 1713 1714 ProcessSP process_sp(target_sp->GetProcessSP()); 1715 1716 if (process_sp) { 1717 ObjCLanguageRuntime *objc_language_runtime = 1718 process_sp->GetObjCLanguageRuntime(); 1719 1720 if (objc_language_runtime) { 1721 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor(); 1722 1723 if (objc_decl_vendor) { 1724 std::vector<clang::NamedDecl *> decls; 1725 1726 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) { 1727 for (clang::NamedDecl *decl : decls) { 1728 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) { 1729 sb_type_list.Append(SBType(type)); 1730 } 1731 } 1732 } 1733 } 1734 } 1735 } 1736 1737 if (sb_type_list.GetSize() == 0) { 1738 // No matches, search for basic typename matches 1739 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext(); 1740 if (clang_ast) 1741 sb_type_list.Append(SBType(ClangASTContext::GetBasicType( 1742 clang_ast->getASTContext(), const_typename))); 1743 } 1744 } 1745 return sb_type_list; 1746 } 1747 1748 SBValueList SBTarget::FindGlobalVariables(const char *name, 1749 uint32_t max_matches) { 1750 SBValueList sb_value_list; 1751 1752 TargetSP target_sp(GetSP()); 1753 if (name && target_sp) { 1754 VariableList variable_list; 1755 const bool append = true; 1756 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables( 1757 ConstString(name), append, max_matches, variable_list); 1758 1759 if (match_count > 0) { 1760 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get(); 1761 if (exe_scope == NULL) 1762 exe_scope = target_sp.get(); 1763 for (uint32_t i = 0; i < match_count; ++i) { 1764 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create( 1765 exe_scope, variable_list.GetVariableAtIndex(i))); 1766 if (valobj_sp) 1767 sb_value_list.Append(SBValue(valobj_sp)); 1768 } 1769 } 1770 } 1771 1772 return sb_value_list; 1773 } 1774 1775 SBValueList SBTarget::FindGlobalVariables(const char *name, 1776 uint32_t max_matches, 1777 MatchType matchtype) { 1778 SBValueList sb_value_list; 1779 1780 TargetSP target_sp(GetSP()); 1781 if (name && target_sp) { 1782 llvm::StringRef name_ref(name); 1783 VariableList variable_list; 1784 const bool append = true; 1785 1786 std::string regexstr; 1787 uint32_t match_count; 1788 switch (matchtype) { 1789 case eMatchTypeNormal: 1790 match_count = target_sp->GetImages().FindGlobalVariables( 1791 ConstString(name), append, max_matches, variable_list); 1792 break; 1793 case eMatchTypeRegex: 1794 match_count = target_sp->GetImages().FindGlobalVariables( 1795 RegularExpression(name_ref), append, max_matches, variable_list); 1796 break; 1797 case eMatchTypeStartsWith: 1798 regexstr = llvm::Regex::escape(name) + ".*"; 1799 match_count = target_sp->GetImages().FindGlobalVariables( 1800 RegularExpression(regexstr), append, max_matches, variable_list); 1801 break; 1802 } 1803 1804 if (match_count > 0) { 1805 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get(); 1806 if (exe_scope == NULL) 1807 exe_scope = target_sp.get(); 1808 for (uint32_t i = 0; i < match_count; ++i) { 1809 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create( 1810 exe_scope, variable_list.GetVariableAtIndex(i))); 1811 if (valobj_sp) 1812 sb_value_list.Append(SBValue(valobj_sp)); 1813 } 1814 } 1815 } 1816 1817 return sb_value_list; 1818 } 1819 1820 lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) { 1821 SBValueList sb_value_list(FindGlobalVariables(name, 1)); 1822 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0) 1823 return sb_value_list.GetValueAtIndex(0); 1824 return SBValue(); 1825 } 1826 1827 SBSourceManager SBTarget::GetSourceManager() { 1828 SBSourceManager source_manager(*this); 1829 return source_manager; 1830 } 1831 1832 lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr, 1833 uint32_t count) { 1834 return ReadInstructions(base_addr, count, NULL); 1835 } 1836 1837 lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr, 1838 uint32_t count, 1839 const char *flavor_string) { 1840 SBInstructionList sb_instructions; 1841 1842 TargetSP target_sp(GetSP()); 1843 if (target_sp) { 1844 Address *addr_ptr = base_addr.get(); 1845 1846 if (addr_ptr) { 1847 DataBufferHeap data( 1848 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0); 1849 bool prefer_file_cache = false; 1850 lldb_private::Error error; 1851 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS; 1852 const size_t bytes_read = 1853 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(), 1854 data.GetByteSize(), error, &load_addr); 1855 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS; 1856 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes( 1857 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr, 1858 data.GetBytes(), bytes_read, count, data_from_file)); 1859 } 1860 } 1861 1862 return sb_instructions; 1863 } 1864 1865 lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr, 1866 const void *buf, 1867 size_t size) { 1868 return GetInstructionsWithFlavor(base_addr, NULL, buf, size); 1869 } 1870 1871 lldb::SBInstructionList 1872 SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr, 1873 const char *flavor_string, const void *buf, 1874 size_t size) { 1875 SBInstructionList sb_instructions; 1876 1877 TargetSP target_sp(GetSP()); 1878 if (target_sp) { 1879 Address addr; 1880 1881 if (base_addr.get()) 1882 addr = *base_addr.get(); 1883 1884 const bool data_from_file = true; 1885 1886 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes( 1887 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size, 1888 UINT32_MAX, data_from_file)); 1889 } 1890 1891 return sb_instructions; 1892 } 1893 1894 lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr, 1895 const void *buf, 1896 size_t size) { 1897 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf, 1898 size); 1899 } 1900 1901 lldb::SBInstructionList 1902 SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr, 1903 const char *flavor_string, const void *buf, 1904 size_t size) { 1905 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string, 1906 buf, size); 1907 } 1908 1909 SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section, 1910 lldb::addr_t section_base_addr) { 1911 SBError sb_error; 1912 TargetSP target_sp(GetSP()); 1913 if (target_sp) { 1914 if (!section.IsValid()) { 1915 sb_error.SetErrorStringWithFormat("invalid section"); 1916 } else { 1917 SectionSP section_sp(section.GetSP()); 1918 if (section_sp) { 1919 if (section_sp->IsThreadSpecific()) { 1920 sb_error.SetErrorString( 1921 "thread specific sections are not yet supported"); 1922 } else { 1923 ProcessSP process_sp(target_sp->GetProcessSP()); 1924 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) { 1925 ModuleSP module_sp(section_sp->GetModule()); 1926 if (module_sp) { 1927 ModuleList module_list; 1928 module_list.Append(module_sp); 1929 target_sp->ModulesDidLoad(module_list); 1930 } 1931 // Flush info in the process (stack frames, etc) 1932 if (process_sp) 1933 process_sp->Flush(); 1934 } 1935 } 1936 } 1937 } 1938 } else { 1939 sb_error.SetErrorString("invalid target"); 1940 } 1941 return sb_error; 1942 } 1943 1944 SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) { 1945 SBError sb_error; 1946 1947 TargetSP target_sp(GetSP()); 1948 if (target_sp) { 1949 if (!section.IsValid()) { 1950 sb_error.SetErrorStringWithFormat("invalid section"); 1951 } else { 1952 SectionSP section_sp(section.GetSP()); 1953 if (section_sp) { 1954 ProcessSP process_sp(target_sp->GetProcessSP()); 1955 if (target_sp->SetSectionUnloaded(section_sp)) { 1956 ModuleSP module_sp(section_sp->GetModule()); 1957 if (module_sp) { 1958 ModuleList module_list; 1959 module_list.Append(module_sp); 1960 target_sp->ModulesDidUnload(module_list, false); 1961 } 1962 // Flush info in the process (stack frames, etc) 1963 if (process_sp) 1964 process_sp->Flush(); 1965 } 1966 } else { 1967 sb_error.SetErrorStringWithFormat("invalid section"); 1968 } 1969 } 1970 } else { 1971 sb_error.SetErrorStringWithFormat("invalid target"); 1972 } 1973 return sb_error; 1974 } 1975 1976 SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module, 1977 int64_t slide_offset) { 1978 SBError sb_error; 1979 1980 TargetSP target_sp(GetSP()); 1981 if (target_sp) { 1982 ModuleSP module_sp(module.GetSP()); 1983 if (module_sp) { 1984 bool changed = false; 1985 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) { 1986 // The load was successful, make sure that at least some sections 1987 // changed before we notify that our module was loaded. 1988 if (changed) { 1989 ModuleList module_list; 1990 module_list.Append(module_sp); 1991 target_sp->ModulesDidLoad(module_list); 1992 // Flush info in the process (stack frames, etc) 1993 ProcessSP process_sp(target_sp->GetProcessSP()); 1994 if (process_sp) 1995 process_sp->Flush(); 1996 } 1997 } 1998 } else { 1999 sb_error.SetErrorStringWithFormat("invalid module"); 2000 } 2001 2002 } else { 2003 sb_error.SetErrorStringWithFormat("invalid target"); 2004 } 2005 return sb_error; 2006 } 2007 2008 SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) { 2009 SBError sb_error; 2010 2011 char path[PATH_MAX]; 2012 TargetSP target_sp(GetSP()); 2013 if (target_sp) { 2014 ModuleSP module_sp(module.GetSP()); 2015 if (module_sp) { 2016 ObjectFile *objfile = module_sp->GetObjectFile(); 2017 if (objfile) { 2018 SectionList *section_list = objfile->GetSectionList(); 2019 if (section_list) { 2020 ProcessSP process_sp(target_sp->GetProcessSP()); 2021 2022 bool changed = false; 2023 const size_t num_sections = section_list->GetSize(); 2024 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 2025 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 2026 if (section_sp) 2027 changed |= target_sp->SetSectionUnloaded(section_sp); 2028 } 2029 if (changed) { 2030 ModuleList module_list; 2031 module_list.Append(module_sp); 2032 target_sp->ModulesDidUnload(module_list, false); 2033 // Flush info in the process (stack frames, etc) 2034 ProcessSP process_sp(target_sp->GetProcessSP()); 2035 if (process_sp) 2036 process_sp->Flush(); 2037 } 2038 } else { 2039 module_sp->GetFileSpec().GetPath(path, sizeof(path)); 2040 sb_error.SetErrorStringWithFormat("no sections in object file '%s'", 2041 path); 2042 } 2043 } else { 2044 module_sp->GetFileSpec().GetPath(path, sizeof(path)); 2045 sb_error.SetErrorStringWithFormat("no object file for module '%s'", 2046 path); 2047 } 2048 } else { 2049 sb_error.SetErrorStringWithFormat("invalid module"); 2050 } 2051 } else { 2052 sb_error.SetErrorStringWithFormat("invalid target"); 2053 } 2054 return sb_error; 2055 } 2056 2057 lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name, 2058 lldb::SymbolType symbol_type) { 2059 SBSymbolContextList sb_sc_list; 2060 if (name && name[0]) { 2061 TargetSP target_sp(GetSP()); 2062 if (target_sp) { 2063 bool append = true; 2064 target_sp->GetImages().FindSymbolsWithNameAndType( 2065 ConstString(name), symbol_type, *sb_sc_list, append); 2066 } 2067 } 2068 return sb_sc_list; 2069 } 2070 2071 lldb::SBValue SBTarget::EvaluateExpression(const char *expr) { 2072 TargetSP target_sp(GetSP()); 2073 if (!target_sp) 2074 return SBValue(); 2075 2076 SBExpressionOptions options; 2077 lldb::DynamicValueType fetch_dynamic_value = 2078 target_sp->GetPreferDynamicValue(); 2079 options.SetFetchDynamicValue(fetch_dynamic_value); 2080 options.SetUnwindOnError(true); 2081 return EvaluateExpression(expr, options); 2082 } 2083 2084 lldb::SBValue SBTarget::EvaluateExpression(const char *expr, 2085 const SBExpressionOptions &options) { 2086 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 2087 #if !defined(LLDB_DISABLE_PYTHON) 2088 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 2089 #endif 2090 SBValue expr_result; 2091 ExpressionResults exe_results = eExpressionSetupError; 2092 ValueObjectSP expr_value_sp; 2093 TargetSP target_sp(GetSP()); 2094 StackFrame *frame = NULL; 2095 if (target_sp) { 2096 if (expr == NULL || expr[0] == '\0') { 2097 if (log) 2098 log->Printf( 2099 "SBTarget::EvaluateExpression called with an empty expression"); 2100 return expr_result; 2101 } 2102 2103 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 2104 ExecutionContext exe_ctx(m_opaque_sp.get()); 2105 2106 if (log) 2107 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr); 2108 2109 frame = exe_ctx.GetFramePtr(); 2110 Target *target = exe_ctx.GetTargetPtr(); 2111 2112 if (target) { 2113 #ifdef LLDB_CONFIGURATION_DEBUG 2114 StreamString frame_description; 2115 if (frame) 2116 frame->DumpUsingSettingsFormat(&frame_description); 2117 Host::SetCrashDescriptionWithFormat( 2118 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = " 2119 "%u) %s", 2120 expr, options.GetFetchDynamicValue(), 2121 frame_description.GetString().c_str()); 2122 #endif 2123 exe_results = 2124 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref()); 2125 2126 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue()); 2127 #ifdef LLDB_CONFIGURATION_DEBUG 2128 Host::SetCrashDescription(NULL); 2129 #endif 2130 } else { 2131 if (log) 2132 log->Printf("SBTarget::EvaluateExpression () => error: could not " 2133 "reconstruct frame object for this SBTarget."); 2134 } 2135 } 2136 #ifndef LLDB_DISABLE_PYTHON 2137 if (expr_log) 2138 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is " 2139 "%s, summary %s **", 2140 expr_result.GetValue(), expr_result.GetSummary()); 2141 2142 if (log) 2143 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) " 2144 "(execution result=%d)", 2145 static_cast<void *>(frame), expr, 2146 static_cast<void *>(expr_value_sp.get()), exe_results); 2147 #endif 2148 2149 return expr_result; 2150 } 2151 2152 lldb::addr_t SBTarget::GetStackRedZoneSize() { 2153 TargetSP target_sp(GetSP()); 2154 if (target_sp) { 2155 ABISP abi_sp; 2156 ProcessSP process_sp(target_sp->GetProcessSP()); 2157 if (process_sp) 2158 abi_sp = process_sp->GetABI(); 2159 else 2160 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture()); 2161 if (abi_sp) 2162 return abi_sp->GetRedZoneSize(); 2163 } 2164 return 0; 2165 } 2166 2167 lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const { 2168 lldb::SBLaunchInfo launch_info(NULL); 2169 TargetSP target_sp(GetSP()); 2170 if (target_sp) 2171 launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo(); 2172 return launch_info; 2173 } 2174 2175 void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) { 2176 TargetSP target_sp(GetSP()); 2177 if (target_sp) 2178 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref()); 2179 } 2180