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