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