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