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 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 PlatformSP platform_sp = target_sp->GetPlatform(); 439 // See if we can pre-verify if a process exists or not 440 if (platform_sp && platform_sp->IsConnected()) { 441 lldb::pid_t attach_pid = attach_info.GetProcessID(); 442 ProcessInstanceInfo instance_info; 443 if (platform_sp->GetProcessInfo(attach_pid, instance_info)) { 444 attach_info.SetUserID(instance_info.GetEffectiveUserID()); 445 } else { 446 error.ref().SetErrorStringWithFormat( 447 "no process found with process ID %" PRIu64, attach_pid); 448 return sb_process; 449 } 450 } 451 } 452 error.SetError(AttachToProcess(attach_info, *target_sp)); 453 if (error.Success()) 454 sb_process.SetSP(target_sp->GetProcessSP()); 455 } else { 456 error.SetErrorString("SBTarget is invalid"); 457 } 458 459 return sb_process; 460 } 461 462 lldb::SBProcess SBTarget::AttachToProcessWithID( 463 SBListener &listener, 464 lldb::pid_t pid, // The process ID to attach to 465 SBError &error // An error explaining what went wrong if attach fails 466 ) { 467 LLDB_INSTRUMENT_VA(this, listener, pid, error); 468 469 SBProcess sb_process; 470 TargetSP target_sp(GetSP()); 471 472 if (target_sp) { 473 ProcessAttachInfo attach_info; 474 attach_info.SetProcessID(pid); 475 if (listener.IsValid()) 476 attach_info.SetListener(listener.GetSP()); 477 478 ProcessInstanceInfo instance_info; 479 if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info)) 480 attach_info.SetUserID(instance_info.GetEffectiveUserID()); 481 482 error.SetError(AttachToProcess(attach_info, *target_sp)); 483 if (error.Success()) 484 sb_process.SetSP(target_sp->GetProcessSP()); 485 } else 486 error.SetErrorString("SBTarget is invalid"); 487 488 return sb_process; 489 } 490 491 lldb::SBProcess SBTarget::AttachToProcessWithName( 492 SBListener &listener, 493 const char *name, // basename of process to attach to 494 bool wait_for, // if true wait for a new instance of "name" to be launched 495 SBError &error // An error explaining what went wrong if attach fails 496 ) { 497 LLDB_INSTRUMENT_VA(this, listener, name, wait_for, error); 498 499 SBProcess sb_process; 500 TargetSP target_sp(GetSP()); 501 502 if (name && target_sp) { 503 ProcessAttachInfo attach_info; 504 attach_info.GetExecutableFile().SetFile(name, FileSpec::Style::native); 505 attach_info.SetWaitForLaunch(wait_for); 506 if (listener.IsValid()) 507 attach_info.SetListener(listener.GetSP()); 508 509 error.SetError(AttachToProcess(attach_info, *target_sp)); 510 if (error.Success()) 511 sb_process.SetSP(target_sp->GetProcessSP()); 512 } else 513 error.SetErrorString("SBTarget is invalid"); 514 515 return sb_process; 516 } 517 518 lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url, 519 const char *plugin_name, 520 SBError &error) { 521 LLDB_INSTRUMENT_VA(this, listener, url, plugin_name, error); 522 523 SBProcess sb_process; 524 ProcessSP process_sp; 525 TargetSP target_sp(GetSP()); 526 527 if (target_sp) { 528 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 529 if (listener.IsValid()) 530 process_sp = 531 target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, nullptr, 532 true); 533 else 534 process_sp = target_sp->CreateProcess( 535 target_sp->GetDebugger().GetListener(), plugin_name, nullptr, true); 536 537 if (process_sp) { 538 sb_process.SetSP(process_sp); 539 error.SetError(process_sp->ConnectRemote(url)); 540 } else { 541 error.SetErrorString("unable to create lldb_private::Process"); 542 } 543 } else { 544 error.SetErrorString("SBTarget is invalid"); 545 } 546 547 return sb_process; 548 } 549 550 SBFileSpec SBTarget::GetExecutable() { 551 LLDB_INSTRUMENT_VA(this); 552 553 SBFileSpec exe_file_spec; 554 TargetSP target_sp(GetSP()); 555 if (target_sp) { 556 Module *exe_module = target_sp->GetExecutableModulePointer(); 557 if (exe_module) 558 exe_file_spec.SetFileSpec(exe_module->GetFileSpec()); 559 } 560 561 return exe_file_spec; 562 } 563 564 bool SBTarget::operator==(const SBTarget &rhs) const { 565 LLDB_INSTRUMENT_VA(this, rhs); 566 567 return m_opaque_sp.get() == rhs.m_opaque_sp.get(); 568 } 569 570 bool SBTarget::operator!=(const SBTarget &rhs) const { 571 LLDB_INSTRUMENT_VA(this, rhs); 572 573 return m_opaque_sp.get() != rhs.m_opaque_sp.get(); 574 } 575 576 lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; } 577 578 void SBTarget::SetSP(const lldb::TargetSP &target_sp) { 579 m_opaque_sp = target_sp; 580 } 581 582 lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) { 583 LLDB_INSTRUMENT_VA(this, vm_addr); 584 585 lldb::SBAddress sb_addr; 586 Address &addr = sb_addr.ref(); 587 TargetSP target_sp(GetSP()); 588 if (target_sp) { 589 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 590 if (target_sp->ResolveLoadAddress(vm_addr, addr)) 591 return sb_addr; 592 } 593 594 // We have a load address that isn't in a section, just return an address 595 // with the offset filled in (the address) and the section set to NULL 596 addr.SetRawAddress(vm_addr); 597 return sb_addr; 598 } 599 600 lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) { 601 LLDB_INSTRUMENT_VA(this, file_addr); 602 603 lldb::SBAddress sb_addr; 604 Address &addr = sb_addr.ref(); 605 TargetSP target_sp(GetSP()); 606 if (target_sp) { 607 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 608 if (target_sp->ResolveFileAddress(file_addr, addr)) 609 return sb_addr; 610 } 611 612 addr.SetRawAddress(file_addr); 613 return sb_addr; 614 } 615 616 lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id, 617 lldb::addr_t vm_addr) { 618 LLDB_INSTRUMENT_VA(this, stop_id, vm_addr); 619 620 lldb::SBAddress sb_addr; 621 Address &addr = sb_addr.ref(); 622 TargetSP target_sp(GetSP()); 623 if (target_sp) { 624 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 625 if (target_sp->ResolveLoadAddress(vm_addr, addr)) 626 return sb_addr; 627 } 628 629 // We have a load address that isn't in a section, just return an address 630 // with the offset filled in (the address) and the section set to NULL 631 addr.SetRawAddress(vm_addr); 632 return sb_addr; 633 } 634 635 SBSymbolContext 636 SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr, 637 uint32_t resolve_scope) { 638 LLDB_INSTRUMENT_VA(this, addr, resolve_scope); 639 640 SBSymbolContext sc; 641 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope); 642 if (addr.IsValid()) { 643 TargetSP target_sp(GetSP()); 644 if (target_sp) 645 target_sp->GetImages().ResolveSymbolContextForAddress(addr.ref(), scope, 646 sc.ref()); 647 } 648 return sc; 649 } 650 651 size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size, 652 lldb::SBError &error) { 653 LLDB_INSTRUMENT_VA(this, addr, buf, size, error); 654 655 SBError sb_error; 656 size_t bytes_read = 0; 657 TargetSP target_sp(GetSP()); 658 if (target_sp) { 659 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 660 bytes_read = 661 target_sp->ReadMemory(addr.ref(), buf, size, sb_error.ref(), true); 662 } else { 663 sb_error.SetErrorString("invalid target"); 664 } 665 666 return bytes_read; 667 } 668 669 SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file, 670 uint32_t line) { 671 LLDB_INSTRUMENT_VA(this, file, line); 672 673 return SBBreakpoint( 674 BreakpointCreateByLocation(SBFileSpec(file, false), line)); 675 } 676 677 SBBreakpoint 678 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec, 679 uint32_t line) { 680 LLDB_INSTRUMENT_VA(this, sb_file_spec, line); 681 682 return BreakpointCreateByLocation(sb_file_spec, line, 0); 683 } 684 685 SBBreakpoint 686 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec, 687 uint32_t line, lldb::addr_t offset) { 688 LLDB_INSTRUMENT_VA(this, sb_file_spec, line, offset); 689 690 SBFileSpecList empty_list; 691 return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list); 692 } 693 694 SBBreakpoint 695 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec, 696 uint32_t line, lldb::addr_t offset, 697 SBFileSpecList &sb_module_list) { 698 LLDB_INSTRUMENT_VA(this, sb_file_spec, line, offset, sb_module_list); 699 700 return BreakpointCreateByLocation(sb_file_spec, line, 0, offset, 701 sb_module_list); 702 } 703 704 SBBreakpoint SBTarget::BreakpointCreateByLocation( 705 const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column, 706 lldb::addr_t offset, SBFileSpecList &sb_module_list) { 707 LLDB_INSTRUMENT_VA(this, sb_file_spec, line, column, offset, sb_module_list); 708 709 SBBreakpoint sb_bp; 710 TargetSP target_sp(GetSP()); 711 if (target_sp && line != 0) { 712 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 713 714 const LazyBool check_inlines = eLazyBoolCalculate; 715 const LazyBool skip_prologue = eLazyBoolCalculate; 716 const bool internal = false; 717 const bool hardware = false; 718 const LazyBool move_to_nearest_code = eLazyBoolCalculate; 719 const FileSpecList *module_list = nullptr; 720 if (sb_module_list.GetSize() > 0) { 721 module_list = sb_module_list.get(); 722 } 723 sb_bp = target_sp->CreateBreakpoint( 724 module_list, *sb_file_spec, line, column, offset, check_inlines, 725 skip_prologue, internal, hardware, move_to_nearest_code); 726 } 727 728 return sb_bp; 729 } 730 731 SBBreakpoint SBTarget::BreakpointCreateByLocation( 732 const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column, 733 lldb::addr_t offset, SBFileSpecList &sb_module_list, 734 bool move_to_nearest_code) { 735 LLDB_INSTRUMENT_VA(this, sb_file_spec, line, column, offset, sb_module_list, 736 move_to_nearest_code); 737 738 SBBreakpoint sb_bp; 739 TargetSP target_sp(GetSP()); 740 if (target_sp && line != 0) { 741 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 742 743 const LazyBool check_inlines = eLazyBoolCalculate; 744 const LazyBool skip_prologue = eLazyBoolCalculate; 745 const bool internal = false; 746 const bool hardware = false; 747 const FileSpecList *module_list = nullptr; 748 if (sb_module_list.GetSize() > 0) { 749 module_list = sb_module_list.get(); 750 } 751 sb_bp = target_sp->CreateBreakpoint( 752 module_list, *sb_file_spec, line, column, offset, check_inlines, 753 skip_prologue, internal, hardware, 754 move_to_nearest_code ? eLazyBoolYes : eLazyBoolNo); 755 } 756 757 return sb_bp; 758 } 759 760 SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name, 761 const char *module_name) { 762 LLDB_INSTRUMENT_VA(this, symbol_name, module_name); 763 764 SBBreakpoint sb_bp; 765 TargetSP target_sp(GetSP()); 766 if (target_sp.get()) { 767 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 768 769 const bool internal = false; 770 const bool hardware = false; 771 const LazyBool skip_prologue = eLazyBoolCalculate; 772 const lldb::addr_t offset = 0; 773 if (module_name && module_name[0]) { 774 FileSpecList module_spec_list; 775 module_spec_list.Append(FileSpec(module_name)); 776 sb_bp = target_sp->CreateBreakpoint( 777 &module_spec_list, nullptr, symbol_name, eFunctionNameTypeAuto, 778 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware); 779 } else { 780 sb_bp = target_sp->CreateBreakpoint( 781 nullptr, nullptr, symbol_name, eFunctionNameTypeAuto, 782 eLanguageTypeUnknown, offset, skip_prologue, internal, hardware); 783 } 784 } 785 786 return sb_bp; 787 } 788 789 lldb::SBBreakpoint 790 SBTarget::BreakpointCreateByName(const char *symbol_name, 791 const SBFileSpecList &module_list, 792 const SBFileSpecList &comp_unit_list) { 793 LLDB_INSTRUMENT_VA(this, symbol_name, module_list, comp_unit_list); 794 795 lldb::FunctionNameType name_type_mask = eFunctionNameTypeAuto; 796 return BreakpointCreateByName(symbol_name, name_type_mask, 797 eLanguageTypeUnknown, module_list, 798 comp_unit_list); 799 } 800 801 lldb::SBBreakpoint SBTarget::BreakpointCreateByName( 802 const char *symbol_name, uint32_t name_type_mask, 803 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) { 804 LLDB_INSTRUMENT_VA(this, symbol_name, name_type_mask, module_list, 805 comp_unit_list); 806 807 return BreakpointCreateByName(symbol_name, name_type_mask, 808 eLanguageTypeUnknown, module_list, 809 comp_unit_list); 810 } 811 812 lldb::SBBreakpoint SBTarget::BreakpointCreateByName( 813 const char *symbol_name, uint32_t name_type_mask, 814 LanguageType symbol_language, const SBFileSpecList &module_list, 815 const SBFileSpecList &comp_unit_list) { 816 LLDB_INSTRUMENT_VA(this, symbol_name, name_type_mask, symbol_language, 817 module_list, comp_unit_list); 818 819 SBBreakpoint sb_bp; 820 TargetSP target_sp(GetSP()); 821 if (target_sp && symbol_name && symbol_name[0]) { 822 const bool internal = false; 823 const bool hardware = false; 824 const LazyBool skip_prologue = eLazyBoolCalculate; 825 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 826 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask); 827 sb_bp = target_sp->CreateBreakpoint(module_list.get(), comp_unit_list.get(), 828 symbol_name, mask, symbol_language, 0, 829 skip_prologue, internal, hardware); 830 } 831 832 return sb_bp; 833 } 834 835 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames( 836 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask, 837 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) { 838 LLDB_INSTRUMENT_VA(this, symbol_names, num_names, name_type_mask, module_list, 839 comp_unit_list); 840 841 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask, 842 eLanguageTypeUnknown, module_list, 843 comp_unit_list); 844 } 845 846 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames( 847 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask, 848 LanguageType symbol_language, const SBFileSpecList &module_list, 849 const SBFileSpecList &comp_unit_list) { 850 LLDB_INSTRUMENT_VA(this, symbol_names, num_names, name_type_mask, 851 symbol_language, module_list, comp_unit_list); 852 853 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask, 854 eLanguageTypeUnknown, 0, module_list, 855 comp_unit_list); 856 } 857 858 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames( 859 const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask, 860 LanguageType symbol_language, lldb::addr_t offset, 861 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) { 862 LLDB_INSTRUMENT_VA(this, symbol_names, num_names, name_type_mask, 863 symbol_language, offset, module_list, comp_unit_list); 864 865 SBBreakpoint sb_bp; 866 TargetSP target_sp(GetSP()); 867 if (target_sp && num_names > 0) { 868 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 869 const bool internal = false; 870 const bool hardware = false; 871 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask); 872 const LazyBool skip_prologue = eLazyBoolCalculate; 873 sb_bp = target_sp->CreateBreakpoint( 874 module_list.get(), comp_unit_list.get(), symbol_names, num_names, mask, 875 symbol_language, offset, skip_prologue, internal, hardware); 876 } 877 878 return sb_bp; 879 } 880 881 SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex, 882 const char *module_name) { 883 LLDB_INSTRUMENT_VA(this, symbol_name_regex, module_name); 884 885 SBFileSpecList module_spec_list; 886 SBFileSpecList comp_unit_list; 887 if (module_name && module_name[0]) { 888 module_spec_list.Append(FileSpec(module_name)); 889 } 890 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown, 891 module_spec_list, comp_unit_list); 892 } 893 894 lldb::SBBreakpoint 895 SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex, 896 const SBFileSpecList &module_list, 897 const SBFileSpecList &comp_unit_list) { 898 LLDB_INSTRUMENT_VA(this, symbol_name_regex, module_list, comp_unit_list); 899 900 return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown, 901 module_list, comp_unit_list); 902 } 903 904 lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex( 905 const char *symbol_name_regex, LanguageType symbol_language, 906 const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) { 907 LLDB_INSTRUMENT_VA(this, symbol_name_regex, symbol_language, module_list, 908 comp_unit_list); 909 910 SBBreakpoint sb_bp; 911 TargetSP target_sp(GetSP()); 912 if (target_sp && symbol_name_regex && symbol_name_regex[0]) { 913 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 914 RegularExpression regexp((llvm::StringRef(symbol_name_regex))); 915 const bool internal = false; 916 const bool hardware = false; 917 const LazyBool skip_prologue = eLazyBoolCalculate; 918 919 sb_bp = target_sp->CreateFuncRegexBreakpoint( 920 module_list.get(), comp_unit_list.get(), std::move(regexp), 921 symbol_language, skip_prologue, internal, hardware); 922 } 923 924 return sb_bp; 925 } 926 927 SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) { 928 LLDB_INSTRUMENT_VA(this, address); 929 930 SBBreakpoint sb_bp; 931 TargetSP target_sp(GetSP()); 932 if (target_sp) { 933 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 934 const bool hardware = false; 935 sb_bp = target_sp->CreateBreakpoint(address, false, hardware); 936 } 937 938 return sb_bp; 939 } 940 941 SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) { 942 LLDB_INSTRUMENT_VA(this, sb_address); 943 944 SBBreakpoint sb_bp; 945 TargetSP target_sp(GetSP()); 946 if (!sb_address.IsValid()) { 947 return sb_bp; 948 } 949 950 if (target_sp) { 951 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 952 const bool hardware = false; 953 sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware); 954 } 955 956 return sb_bp; 957 } 958 959 lldb::SBBreakpoint 960 SBTarget::BreakpointCreateBySourceRegex(const char *source_regex, 961 const lldb::SBFileSpec &source_file, 962 const char *module_name) { 963 LLDB_INSTRUMENT_VA(this, source_regex, source_file, module_name); 964 965 SBFileSpecList module_spec_list; 966 967 if (module_name && module_name[0]) { 968 module_spec_list.Append(FileSpec(module_name)); 969 } 970 971 SBFileSpecList source_file_list; 972 if (source_file.IsValid()) { 973 source_file_list.Append(source_file); 974 } 975 976 return BreakpointCreateBySourceRegex(source_regex, module_spec_list, 977 source_file_list); 978 } 979 980 lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex( 981 const char *source_regex, const SBFileSpecList &module_list, 982 const lldb::SBFileSpecList &source_file_list) { 983 LLDB_INSTRUMENT_VA(this, source_regex, module_list, source_file_list); 984 985 return BreakpointCreateBySourceRegex(source_regex, module_list, 986 source_file_list, SBStringList()); 987 } 988 989 lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex( 990 const char *source_regex, const SBFileSpecList &module_list, 991 const lldb::SBFileSpecList &source_file_list, 992 const SBStringList &func_names) { 993 LLDB_INSTRUMENT_VA(this, source_regex, module_list, source_file_list, 994 func_names); 995 996 SBBreakpoint sb_bp; 997 TargetSP target_sp(GetSP()); 998 if (target_sp && source_regex && source_regex[0]) { 999 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1000 const bool hardware = false; 1001 const LazyBool move_to_nearest_code = eLazyBoolCalculate; 1002 RegularExpression regexp((llvm::StringRef(source_regex))); 1003 std::unordered_set<std::string> func_names_set; 1004 for (size_t i = 0; i < func_names.GetSize(); i++) { 1005 func_names_set.insert(func_names.GetStringAtIndex(i)); 1006 } 1007 1008 sb_bp = target_sp->CreateSourceRegexBreakpoint( 1009 module_list.get(), source_file_list.get(), func_names_set, 1010 std::move(regexp), false, hardware, move_to_nearest_code); 1011 } 1012 1013 return sb_bp; 1014 } 1015 1016 lldb::SBBreakpoint 1017 SBTarget::BreakpointCreateForException(lldb::LanguageType language, 1018 bool catch_bp, bool throw_bp) { 1019 LLDB_INSTRUMENT_VA(this, language, catch_bp, throw_bp); 1020 1021 SBBreakpoint sb_bp; 1022 TargetSP target_sp(GetSP()); 1023 if (target_sp) { 1024 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1025 const bool hardware = false; 1026 sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp, 1027 hardware); 1028 } 1029 1030 return sb_bp; 1031 } 1032 1033 lldb::SBBreakpoint SBTarget::BreakpointCreateFromScript( 1034 const char *class_name, SBStructuredData &extra_args, 1035 const SBFileSpecList &module_list, const SBFileSpecList &file_list, 1036 bool request_hardware) { 1037 LLDB_INSTRUMENT_VA(this, class_name, extra_args, module_list, file_list, 1038 request_hardware); 1039 1040 SBBreakpoint sb_bp; 1041 TargetSP target_sp(GetSP()); 1042 if (target_sp) { 1043 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1044 Status error; 1045 1046 StructuredData::ObjectSP obj_sp = extra_args.m_impl_up->GetObjectSP(); 1047 sb_bp = 1048 target_sp->CreateScriptedBreakpoint(class_name, 1049 module_list.get(), 1050 file_list.get(), 1051 false, /* internal */ 1052 request_hardware, 1053 obj_sp, 1054 &error); 1055 } 1056 1057 return sb_bp; 1058 } 1059 1060 uint32_t SBTarget::GetNumBreakpoints() const { 1061 LLDB_INSTRUMENT_VA(this); 1062 1063 TargetSP target_sp(GetSP()); 1064 if (target_sp) { 1065 // The breakpoint list is thread safe, no need to lock 1066 return target_sp->GetBreakpointList().GetSize(); 1067 } 1068 return 0; 1069 } 1070 1071 SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const { 1072 LLDB_INSTRUMENT_VA(this, idx); 1073 1074 SBBreakpoint sb_breakpoint; 1075 TargetSP target_sp(GetSP()); 1076 if (target_sp) { 1077 // The breakpoint list is thread safe, no need to lock 1078 sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx); 1079 } 1080 return sb_breakpoint; 1081 } 1082 1083 bool SBTarget::BreakpointDelete(break_id_t bp_id) { 1084 LLDB_INSTRUMENT_VA(this, bp_id); 1085 1086 bool result = false; 1087 TargetSP target_sp(GetSP()); 1088 if (target_sp) { 1089 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1090 result = target_sp->RemoveBreakpointByID(bp_id); 1091 } 1092 1093 return result; 1094 } 1095 1096 SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) { 1097 LLDB_INSTRUMENT_VA(this, bp_id); 1098 1099 SBBreakpoint sb_breakpoint; 1100 TargetSP target_sp(GetSP()); 1101 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) { 1102 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1103 sb_breakpoint = target_sp->GetBreakpointByID(bp_id); 1104 } 1105 1106 return sb_breakpoint; 1107 } 1108 1109 bool SBTarget::FindBreakpointsByName(const char *name, 1110 SBBreakpointList &bkpts) { 1111 LLDB_INSTRUMENT_VA(this, name, bkpts); 1112 1113 TargetSP target_sp(GetSP()); 1114 if (target_sp) { 1115 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1116 llvm::Expected<std::vector<BreakpointSP>> expected_vector = 1117 target_sp->GetBreakpointList().FindBreakpointsByName(name); 1118 if (!expected_vector) { 1119 LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS), 1120 "invalid breakpoint name: {}", 1121 llvm::toString(expected_vector.takeError())); 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 std::string triple(target_sp->GetArchitecture().GetTriple().str()); 1583 // Unique the string so we don't run into ownership issues since the const 1584 // strings put the string into the string pool once and the strings never 1585 // comes out 1586 ConstString const_triple(triple.c_str()); 1587 return const_triple.GetCString(); 1588 } 1589 return nullptr; 1590 } 1591 1592 uint32_t SBTarget::GetDataByteSize() { 1593 LLDB_INSTRUMENT_VA(this); 1594 1595 TargetSP target_sp(GetSP()); 1596 if (target_sp) { 1597 return target_sp->GetArchitecture().GetDataByteSize(); 1598 } 1599 return 0; 1600 } 1601 1602 uint32_t SBTarget::GetCodeByteSize() { 1603 LLDB_INSTRUMENT_VA(this); 1604 1605 TargetSP target_sp(GetSP()); 1606 if (target_sp) { 1607 return target_sp->GetArchitecture().GetCodeByteSize(); 1608 } 1609 return 0; 1610 } 1611 1612 uint32_t SBTarget::GetMaximumNumberOfChildrenToDisplay() const { 1613 LLDB_INSTRUMENT_VA(this); 1614 1615 TargetSP target_sp(GetSP()); 1616 if(target_sp){ 1617 return target_sp->GetMaximumNumberOfChildrenToDisplay(); 1618 } 1619 return 0; 1620 } 1621 1622 uint32_t SBTarget::GetAddressByteSize() { 1623 LLDB_INSTRUMENT_VA(this); 1624 1625 TargetSP target_sp(GetSP()); 1626 if (target_sp) 1627 return target_sp->GetArchitecture().GetAddressByteSize(); 1628 return sizeof(void *); 1629 } 1630 1631 SBModule SBTarget::GetModuleAtIndex(uint32_t idx) { 1632 LLDB_INSTRUMENT_VA(this, idx); 1633 1634 SBModule sb_module; 1635 ModuleSP module_sp; 1636 TargetSP target_sp(GetSP()); 1637 if (target_sp) { 1638 // The module list is thread safe, no need to lock 1639 module_sp = target_sp->GetImages().GetModuleAtIndex(idx); 1640 sb_module.SetSP(module_sp); 1641 } 1642 1643 return sb_module; 1644 } 1645 1646 bool SBTarget::RemoveModule(lldb::SBModule module) { 1647 LLDB_INSTRUMENT_VA(this, module); 1648 1649 TargetSP target_sp(GetSP()); 1650 if (target_sp) 1651 return target_sp->GetImages().Remove(module.GetSP()); 1652 return false; 1653 } 1654 1655 SBBroadcaster SBTarget::GetBroadcaster() const { 1656 LLDB_INSTRUMENT_VA(this); 1657 1658 TargetSP target_sp(GetSP()); 1659 SBBroadcaster broadcaster(target_sp.get(), false); 1660 1661 return broadcaster; 1662 } 1663 1664 bool SBTarget::GetDescription(SBStream &description, 1665 lldb::DescriptionLevel description_level) { 1666 LLDB_INSTRUMENT_VA(this, description, description_level); 1667 1668 Stream &strm = description.ref(); 1669 1670 TargetSP target_sp(GetSP()); 1671 if (target_sp) { 1672 target_sp->Dump(&strm, description_level); 1673 } else 1674 strm.PutCString("No value"); 1675 1676 return true; 1677 } 1678 1679 lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name, 1680 uint32_t name_type_mask) { 1681 LLDB_INSTRUMENT_VA(this, name, name_type_mask); 1682 1683 lldb::SBSymbolContextList sb_sc_list; 1684 if (!name || !name[0]) 1685 return sb_sc_list; 1686 1687 TargetSP target_sp(GetSP()); 1688 if (!target_sp) 1689 return sb_sc_list; 1690 1691 ModuleFunctionSearchOptions function_options; 1692 function_options.include_symbols = true; 1693 function_options.include_inlines = true; 1694 1695 FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask); 1696 target_sp->GetImages().FindFunctions(ConstString(name), mask, 1697 function_options, *sb_sc_list); 1698 return sb_sc_list; 1699 } 1700 1701 lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name, 1702 uint32_t max_matches, 1703 MatchType matchtype) { 1704 LLDB_INSTRUMENT_VA(this, name, max_matches, matchtype); 1705 1706 lldb::SBSymbolContextList sb_sc_list; 1707 if (name && name[0]) { 1708 llvm::StringRef name_ref(name); 1709 TargetSP target_sp(GetSP()); 1710 if (target_sp) { 1711 ModuleFunctionSearchOptions function_options; 1712 function_options.include_symbols = true; 1713 function_options.include_inlines = true; 1714 1715 std::string regexstr; 1716 switch (matchtype) { 1717 case eMatchTypeRegex: 1718 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), 1719 function_options, *sb_sc_list); 1720 break; 1721 case eMatchTypeStartsWith: 1722 regexstr = llvm::Regex::escape(name) + ".*"; 1723 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), 1724 function_options, *sb_sc_list); 1725 break; 1726 default: 1727 target_sp->GetImages().FindFunctions(ConstString(name), 1728 eFunctionNameTypeAny, 1729 function_options, *sb_sc_list); 1730 break; 1731 } 1732 } 1733 } 1734 return sb_sc_list; 1735 } 1736 1737 lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) { 1738 LLDB_INSTRUMENT_VA(this, typename_cstr); 1739 1740 TargetSP target_sp(GetSP()); 1741 if (typename_cstr && typename_cstr[0] && target_sp) { 1742 ConstString const_typename(typename_cstr); 1743 SymbolContext sc; 1744 const bool exact_match = false; 1745 1746 const ModuleList &module_list = target_sp->GetImages(); 1747 size_t count = module_list.GetSize(); 1748 for (size_t idx = 0; idx < count; idx++) { 1749 ModuleSP module_sp(module_list.GetModuleAtIndex(idx)); 1750 if (module_sp) { 1751 TypeSP type_sp( 1752 module_sp->FindFirstType(sc, const_typename, exact_match)); 1753 if (type_sp) 1754 return SBType(type_sp); 1755 } 1756 } 1757 1758 // Didn't find the type in the symbols; Try the loaded language runtimes 1759 if (auto process_sp = target_sp->GetProcessSP()) { 1760 for (auto *runtime : process_sp->GetLanguageRuntimes()) { 1761 if (auto vendor = runtime->GetDeclVendor()) { 1762 auto types = vendor->FindTypes(const_typename, /*max_matches*/ 1); 1763 if (!types.empty()) 1764 return SBType(types.front()); 1765 } 1766 } 1767 } 1768 1769 // No matches, search for basic typename matches 1770 for (auto *type_system : target_sp->GetScratchTypeSystems()) 1771 if (auto type = type_system->GetBuiltinTypeByName(const_typename)) 1772 return SBType(type); 1773 } 1774 1775 return SBType(); 1776 } 1777 1778 SBType SBTarget::GetBasicType(lldb::BasicType type) { 1779 LLDB_INSTRUMENT_VA(this, type); 1780 1781 TargetSP target_sp(GetSP()); 1782 if (target_sp) { 1783 for (auto *type_system : target_sp->GetScratchTypeSystems()) 1784 if (auto compiler_type = type_system->GetBasicTypeFromAST(type)) 1785 return SBType(compiler_type); 1786 } 1787 return SBType(); 1788 } 1789 1790 lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) { 1791 LLDB_INSTRUMENT_VA(this, typename_cstr); 1792 1793 SBTypeList sb_type_list; 1794 TargetSP target_sp(GetSP()); 1795 if (typename_cstr && typename_cstr[0] && target_sp) { 1796 ModuleList &images = target_sp->GetImages(); 1797 ConstString const_typename(typename_cstr); 1798 bool exact_match = false; 1799 TypeList type_list; 1800 llvm::DenseSet<SymbolFile *> searched_symbol_files; 1801 images.FindTypes(nullptr, const_typename, exact_match, UINT32_MAX, 1802 searched_symbol_files, type_list); 1803 1804 for (size_t idx = 0; idx < type_list.GetSize(); idx++) { 1805 TypeSP type_sp(type_list.GetTypeAtIndex(idx)); 1806 if (type_sp) 1807 sb_type_list.Append(SBType(type_sp)); 1808 } 1809 1810 // Try the loaded language runtimes 1811 if (auto process_sp = target_sp->GetProcessSP()) { 1812 for (auto *runtime : process_sp->GetLanguageRuntimes()) { 1813 if (auto *vendor = runtime->GetDeclVendor()) { 1814 auto types = 1815 vendor->FindTypes(const_typename, /*max_matches*/ UINT32_MAX); 1816 for (auto type : types) 1817 sb_type_list.Append(SBType(type)); 1818 } 1819 } 1820 } 1821 1822 if (sb_type_list.GetSize() == 0) { 1823 // No matches, search for basic typename matches 1824 for (auto *type_system : target_sp->GetScratchTypeSystems()) 1825 if (auto compiler_type = 1826 type_system->GetBuiltinTypeByName(const_typename)) 1827 sb_type_list.Append(SBType(compiler_type)); 1828 } 1829 } 1830 return sb_type_list; 1831 } 1832 1833 SBValueList SBTarget::FindGlobalVariables(const char *name, 1834 uint32_t max_matches) { 1835 LLDB_INSTRUMENT_VA(this, name, max_matches); 1836 1837 SBValueList sb_value_list; 1838 1839 TargetSP target_sp(GetSP()); 1840 if (name && target_sp) { 1841 VariableList variable_list; 1842 target_sp->GetImages().FindGlobalVariables(ConstString(name), max_matches, 1843 variable_list); 1844 if (!variable_list.Empty()) { 1845 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get(); 1846 if (exe_scope == nullptr) 1847 exe_scope = target_sp.get(); 1848 for (const VariableSP &var_sp : variable_list) { 1849 lldb::ValueObjectSP valobj_sp( 1850 ValueObjectVariable::Create(exe_scope, var_sp)); 1851 if (valobj_sp) 1852 sb_value_list.Append(SBValue(valobj_sp)); 1853 } 1854 } 1855 } 1856 1857 return sb_value_list; 1858 } 1859 1860 SBValueList SBTarget::FindGlobalVariables(const char *name, 1861 uint32_t max_matches, 1862 MatchType matchtype) { 1863 LLDB_INSTRUMENT_VA(this, name, max_matches, matchtype); 1864 1865 SBValueList sb_value_list; 1866 1867 TargetSP target_sp(GetSP()); 1868 if (name && target_sp) { 1869 llvm::StringRef name_ref(name); 1870 VariableList variable_list; 1871 1872 std::string regexstr; 1873 switch (matchtype) { 1874 case eMatchTypeNormal: 1875 target_sp->GetImages().FindGlobalVariables(ConstString(name), max_matches, 1876 variable_list); 1877 break; 1878 case eMatchTypeRegex: 1879 target_sp->GetImages().FindGlobalVariables(RegularExpression(name_ref), 1880 max_matches, variable_list); 1881 break; 1882 case eMatchTypeStartsWith: 1883 regexstr = llvm::Regex::escape(name) + ".*"; 1884 target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr), 1885 max_matches, variable_list); 1886 break; 1887 } 1888 if (!variable_list.Empty()) { 1889 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get(); 1890 if (exe_scope == nullptr) 1891 exe_scope = target_sp.get(); 1892 for (const VariableSP &var_sp : variable_list) { 1893 lldb::ValueObjectSP valobj_sp( 1894 ValueObjectVariable::Create(exe_scope, var_sp)); 1895 if (valobj_sp) 1896 sb_value_list.Append(SBValue(valobj_sp)); 1897 } 1898 } 1899 } 1900 1901 return sb_value_list; 1902 } 1903 1904 lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) { 1905 LLDB_INSTRUMENT_VA(this, name); 1906 1907 SBValueList sb_value_list(FindGlobalVariables(name, 1)); 1908 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0) 1909 return sb_value_list.GetValueAtIndex(0); 1910 return SBValue(); 1911 } 1912 1913 SBSourceManager SBTarget::GetSourceManager() { 1914 LLDB_INSTRUMENT_VA(this); 1915 1916 SBSourceManager source_manager(*this); 1917 return source_manager; 1918 } 1919 1920 lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr, 1921 uint32_t count) { 1922 LLDB_INSTRUMENT_VA(this, base_addr, count); 1923 1924 return ReadInstructions(base_addr, count, nullptr); 1925 } 1926 1927 lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr, 1928 uint32_t count, 1929 const char *flavor_string) { 1930 LLDB_INSTRUMENT_VA(this, base_addr, count, flavor_string); 1931 1932 SBInstructionList sb_instructions; 1933 1934 TargetSP target_sp(GetSP()); 1935 if (target_sp) { 1936 Address *addr_ptr = base_addr.get(); 1937 1938 if (addr_ptr) { 1939 DataBufferHeap data( 1940 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0); 1941 bool force_live_memory = true; 1942 lldb_private::Status error; 1943 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS; 1944 const size_t bytes_read = 1945 target_sp->ReadMemory(*addr_ptr, data.GetBytes(), data.GetByteSize(), 1946 error, force_live_memory, &load_addr); 1947 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS; 1948 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes( 1949 target_sp->GetArchitecture(), nullptr, flavor_string, *addr_ptr, 1950 data.GetBytes(), bytes_read, count, data_from_file)); 1951 } 1952 } 1953 1954 return sb_instructions; 1955 } 1956 1957 lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr, 1958 const void *buf, 1959 size_t size) { 1960 LLDB_INSTRUMENT_VA(this, base_addr, buf, size); 1961 1962 return GetInstructionsWithFlavor(base_addr, nullptr, buf, size); 1963 } 1964 1965 lldb::SBInstructionList 1966 SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr, 1967 const char *flavor_string, const void *buf, 1968 size_t size) { 1969 LLDB_INSTRUMENT_VA(this, base_addr, flavor_string, buf, size); 1970 1971 SBInstructionList sb_instructions; 1972 1973 TargetSP target_sp(GetSP()); 1974 if (target_sp) { 1975 Address addr; 1976 1977 if (base_addr.get()) 1978 addr = *base_addr.get(); 1979 1980 const bool data_from_file = true; 1981 1982 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes( 1983 target_sp->GetArchitecture(), nullptr, flavor_string, addr, buf, size, 1984 UINT32_MAX, data_from_file)); 1985 } 1986 1987 return sb_instructions; 1988 } 1989 1990 lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr, 1991 const void *buf, 1992 size_t size) { 1993 LLDB_INSTRUMENT_VA(this, base_addr, buf, size); 1994 1995 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), nullptr, buf, 1996 size); 1997 } 1998 1999 lldb::SBInstructionList 2000 SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr, 2001 const char *flavor_string, const void *buf, 2002 size_t size) { 2003 LLDB_INSTRUMENT_VA(this, base_addr, flavor_string, buf, size); 2004 2005 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string, 2006 buf, size); 2007 } 2008 2009 SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section, 2010 lldb::addr_t section_base_addr) { 2011 LLDB_INSTRUMENT_VA(this, section, section_base_addr); 2012 2013 SBError sb_error; 2014 TargetSP target_sp(GetSP()); 2015 if (target_sp) { 2016 if (!section.IsValid()) { 2017 sb_error.SetErrorStringWithFormat("invalid section"); 2018 } else { 2019 SectionSP section_sp(section.GetSP()); 2020 if (section_sp) { 2021 if (section_sp->IsThreadSpecific()) { 2022 sb_error.SetErrorString( 2023 "thread specific sections are not yet supported"); 2024 } else { 2025 ProcessSP process_sp(target_sp->GetProcessSP()); 2026 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) { 2027 ModuleSP module_sp(section_sp->GetModule()); 2028 if (module_sp) { 2029 ModuleList module_list; 2030 module_list.Append(module_sp); 2031 target_sp->ModulesDidLoad(module_list); 2032 } 2033 // Flush info in the process (stack frames, etc) 2034 if (process_sp) 2035 process_sp->Flush(); 2036 } 2037 } 2038 } 2039 } 2040 } else { 2041 sb_error.SetErrorString("invalid target"); 2042 } 2043 return sb_error; 2044 } 2045 2046 SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) { 2047 LLDB_INSTRUMENT_VA(this, section); 2048 2049 SBError sb_error; 2050 2051 TargetSP target_sp(GetSP()); 2052 if (target_sp) { 2053 if (!section.IsValid()) { 2054 sb_error.SetErrorStringWithFormat("invalid section"); 2055 } else { 2056 SectionSP section_sp(section.GetSP()); 2057 if (section_sp) { 2058 ProcessSP process_sp(target_sp->GetProcessSP()); 2059 if (target_sp->SetSectionUnloaded(section_sp)) { 2060 ModuleSP module_sp(section_sp->GetModule()); 2061 if (module_sp) { 2062 ModuleList module_list; 2063 module_list.Append(module_sp); 2064 target_sp->ModulesDidUnload(module_list, false); 2065 } 2066 // Flush info in the process (stack frames, etc) 2067 if (process_sp) 2068 process_sp->Flush(); 2069 } 2070 } else { 2071 sb_error.SetErrorStringWithFormat("invalid section"); 2072 } 2073 } 2074 } else { 2075 sb_error.SetErrorStringWithFormat("invalid target"); 2076 } 2077 return sb_error; 2078 } 2079 2080 SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module, 2081 int64_t slide_offset) { 2082 LLDB_INSTRUMENT_VA(this, module, slide_offset); 2083 2084 SBError sb_error; 2085 2086 TargetSP target_sp(GetSP()); 2087 if (target_sp) { 2088 ModuleSP module_sp(module.GetSP()); 2089 if (module_sp) { 2090 bool changed = false; 2091 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) { 2092 // The load was successful, make sure that at least some sections 2093 // changed before we notify that our module was loaded. 2094 if (changed) { 2095 ModuleList module_list; 2096 module_list.Append(module_sp); 2097 target_sp->ModulesDidLoad(module_list); 2098 // Flush info in the process (stack frames, etc) 2099 ProcessSP process_sp(target_sp->GetProcessSP()); 2100 if (process_sp) 2101 process_sp->Flush(); 2102 } 2103 } 2104 } else { 2105 sb_error.SetErrorStringWithFormat("invalid module"); 2106 } 2107 2108 } else { 2109 sb_error.SetErrorStringWithFormat("invalid target"); 2110 } 2111 return sb_error; 2112 } 2113 2114 SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) { 2115 LLDB_INSTRUMENT_VA(this, module); 2116 2117 SBError sb_error; 2118 2119 char path[PATH_MAX]; 2120 TargetSP target_sp(GetSP()); 2121 if (target_sp) { 2122 ModuleSP module_sp(module.GetSP()); 2123 if (module_sp) { 2124 ObjectFile *objfile = module_sp->GetObjectFile(); 2125 if (objfile) { 2126 SectionList *section_list = objfile->GetSectionList(); 2127 if (section_list) { 2128 ProcessSP process_sp(target_sp->GetProcessSP()); 2129 2130 bool changed = false; 2131 const size_t num_sections = section_list->GetSize(); 2132 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 2133 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 2134 if (section_sp) 2135 changed |= target_sp->SetSectionUnloaded(section_sp); 2136 } 2137 if (changed) { 2138 ModuleList module_list; 2139 module_list.Append(module_sp); 2140 target_sp->ModulesDidUnload(module_list, false); 2141 // Flush info in the process (stack frames, etc) 2142 ProcessSP process_sp(target_sp->GetProcessSP()); 2143 if (process_sp) 2144 process_sp->Flush(); 2145 } 2146 } else { 2147 module_sp->GetFileSpec().GetPath(path, sizeof(path)); 2148 sb_error.SetErrorStringWithFormat("no sections in object file '%s'", 2149 path); 2150 } 2151 } else { 2152 module_sp->GetFileSpec().GetPath(path, sizeof(path)); 2153 sb_error.SetErrorStringWithFormat("no object file for module '%s'", 2154 path); 2155 } 2156 } else { 2157 sb_error.SetErrorStringWithFormat("invalid module"); 2158 } 2159 } else { 2160 sb_error.SetErrorStringWithFormat("invalid target"); 2161 } 2162 return sb_error; 2163 } 2164 2165 lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name, 2166 lldb::SymbolType symbol_type) { 2167 LLDB_INSTRUMENT_VA(this, name, symbol_type); 2168 2169 SBSymbolContextList sb_sc_list; 2170 if (name && name[0]) { 2171 TargetSP target_sp(GetSP()); 2172 if (target_sp) 2173 target_sp->GetImages().FindSymbolsWithNameAndType( 2174 ConstString(name), symbol_type, *sb_sc_list); 2175 } 2176 return sb_sc_list; 2177 } 2178 2179 lldb::SBValue SBTarget::EvaluateExpression(const char *expr) { 2180 LLDB_INSTRUMENT_VA(this, expr); 2181 2182 TargetSP target_sp(GetSP()); 2183 if (!target_sp) 2184 return SBValue(); 2185 2186 SBExpressionOptions options; 2187 lldb::DynamicValueType fetch_dynamic_value = 2188 target_sp->GetPreferDynamicValue(); 2189 options.SetFetchDynamicValue(fetch_dynamic_value); 2190 options.SetUnwindOnError(true); 2191 return EvaluateExpression(expr, options); 2192 } 2193 2194 lldb::SBValue SBTarget::EvaluateExpression(const char *expr, 2195 const SBExpressionOptions &options) { 2196 LLDB_INSTRUMENT_VA(this, expr, options); 2197 2198 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 2199 SBValue expr_result; 2200 ValueObjectSP expr_value_sp; 2201 TargetSP target_sp(GetSP()); 2202 StackFrame *frame = nullptr; 2203 if (target_sp) { 2204 if (expr == nullptr || expr[0] == '\0') { 2205 return expr_result; 2206 } 2207 2208 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 2209 ExecutionContext exe_ctx(m_opaque_sp.get()); 2210 2211 2212 frame = exe_ctx.GetFramePtr(); 2213 Target *target = exe_ctx.GetTargetPtr(); 2214 2215 if (target) { 2216 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref()); 2217 2218 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue()); 2219 } 2220 } 2221 LLDB_LOGF(expr_log, 2222 "** [SBTarget::EvaluateExpression] Expression result is " 2223 "%s, summary %s **", 2224 expr_result.GetValue(), expr_result.GetSummary()); 2225 return expr_result; 2226 } 2227 2228 lldb::addr_t SBTarget::GetStackRedZoneSize() { 2229 LLDB_INSTRUMENT_VA(this); 2230 2231 TargetSP target_sp(GetSP()); 2232 if (target_sp) { 2233 ABISP abi_sp; 2234 ProcessSP process_sp(target_sp->GetProcessSP()); 2235 if (process_sp) 2236 abi_sp = process_sp->GetABI(); 2237 else 2238 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture()); 2239 if (abi_sp) 2240 return abi_sp->GetRedZoneSize(); 2241 } 2242 return 0; 2243 } 2244 2245 bool SBTarget::IsLoaded(const SBModule &module) const { 2246 LLDB_INSTRUMENT_VA(this, module); 2247 2248 TargetSP target_sp(GetSP()); 2249 if (!target_sp) 2250 return false; 2251 2252 ModuleSP module_sp(module.GetSP()); 2253 if (!module_sp) 2254 return false; 2255 2256 return module_sp->IsLoadedInTarget(target_sp.get()); 2257 } 2258 2259 lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const { 2260 LLDB_INSTRUMENT_VA(this); 2261 2262 lldb::SBLaunchInfo launch_info(nullptr); 2263 TargetSP target_sp(GetSP()); 2264 if (target_sp) 2265 launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo()); 2266 return launch_info; 2267 } 2268 2269 void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) { 2270 LLDB_INSTRUMENT_VA(this, launch_info); 2271 2272 TargetSP target_sp(GetSP()); 2273 if (target_sp) 2274 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref()); 2275 } 2276 2277 SBEnvironment SBTarget::GetEnvironment() { 2278 LLDB_INSTRUMENT_VA(this); 2279 TargetSP target_sp(GetSP()); 2280 2281 if (target_sp) { 2282 return SBEnvironment(target_sp->GetEnvironment()); 2283 } 2284 2285 return SBEnvironment(); 2286 } 2287 2288 lldb::SBTrace SBTarget::GetTrace() { 2289 LLDB_INSTRUMENT_VA(this); 2290 TargetSP target_sp(GetSP()); 2291 2292 if (target_sp) 2293 return SBTrace(target_sp->GetTrace()); 2294 2295 return SBTrace(); 2296 } 2297 2298 lldb::SBTrace SBTarget::CreateTrace(lldb::SBError &error) { 2299 LLDB_INSTRUMENT_VA(this, error); 2300 TargetSP target_sp(GetSP()); 2301 error.Clear(); 2302 2303 if (target_sp) { 2304 if (llvm::Expected<lldb::TraceSP> trace_sp = target_sp->CreateTrace()) { 2305 return SBTrace(*trace_sp); 2306 } else { 2307 error.SetErrorString(llvm::toString(trace_sp.takeError()).c_str()); 2308 } 2309 } else { 2310 error.SetErrorString("missing target"); 2311 } 2312 return SBTrace(); 2313 } 2314