1 //===-- SBTarget.cpp --------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/API/SBTarget.h" 11 12 #include "lldb/lldb-public.h" 13 14 #include "lldb/API/SBBreakpoint.h" 15 #include "lldb/API/SBDebugger.h" 16 #include "lldb/API/SBEvent.h" 17 #include "lldb/API/SBExpressionOptions.h" 18 #include "lldb/API/SBFileSpec.h" 19 #include "lldb/API/SBListener.h" 20 #include "lldb/API/SBModule.h" 21 #include "lldb/API/SBModuleSpec.h" 22 #include "lldb/API/SBProcess.h" 23 #include "lldb/API/SBSourceManager.h" 24 #include "lldb/API/SBStream.h" 25 #include "lldb/API/SBStringList.h" 26 #include "lldb/API/SBSymbolContextList.h" 27 #include "lldb/Breakpoint/BreakpointID.h" 28 #include "lldb/Breakpoint/BreakpointIDList.h" 29 #include "lldb/Breakpoint/BreakpointList.h" 30 #include "lldb/Breakpoint/BreakpointLocation.h" 31 #include "lldb/Core/Address.h" 32 #include "lldb/Core/AddressResolver.h" 33 #include "lldb/Core/AddressResolverName.h" 34 #include "lldb/Core/ArchSpec.h" 35 #include "lldb/Core/Debugger.h" 36 #include "lldb/Core/Disassembler.h" 37 #include "lldb/Core/Module.h" 38 #include "lldb/Core/ModuleSpec.h" 39 #include "lldb/Core/STLUtils.h" 40 #include "lldb/Core/SearchFilter.h" 41 #include "lldb/Core/Section.h" 42 #include "lldb/Core/ValueObjectConstResult.h" 43 #include "lldb/Core/ValueObjectList.h" 44 #include "lldb/Core/ValueObjectVariable.h" 45 #include "lldb/Host/Host.h" 46 #include "lldb/Interpreter/Args.h" 47 #include "lldb/Symbol/ClangASTContext.h" 48 #include "lldb/Symbol/DeclVendor.h" 49 #include "lldb/Symbol/ObjectFile.h" 50 #include "lldb/Symbol/SymbolFile.h" 51 #include "lldb/Symbol/SymbolVendor.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/ObjCLanguageRuntime.h" 57 #include "lldb/Target/Process.h" 58 #include "lldb/Target/StackFrame.h" 59 #include "lldb/Target/Target.h" 60 #include "lldb/Target/TargetList.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 bool SBTarget::EnableAllBreakpoints() { 1091 TargetSP target_sp(GetSP()); 1092 if (target_sp) { 1093 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1094 target_sp->EnableAllBreakpoints(); 1095 return true; 1096 } 1097 return false; 1098 } 1099 1100 bool SBTarget::DisableAllBreakpoints() { 1101 TargetSP target_sp(GetSP()); 1102 if (target_sp) { 1103 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1104 target_sp->DisableAllBreakpoints(); 1105 return true; 1106 } 1107 return false; 1108 } 1109 1110 bool SBTarget::DeleteAllBreakpoints() { 1111 TargetSP target_sp(GetSP()); 1112 if (target_sp) { 1113 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1114 target_sp->RemoveAllBreakpoints(); 1115 return true; 1116 } 1117 return false; 1118 } 1119 1120 lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file, 1121 SBBreakpointList &new_bps) { 1122 SBStringList empty_name_list; 1123 return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps); 1124 } 1125 1126 lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file, 1127 SBStringList &matching_names, 1128 SBBreakpointList &new_bps) { 1129 SBError sberr; 1130 TargetSP target_sp(GetSP()); 1131 if (!target_sp) { 1132 sberr.SetErrorString( 1133 "BreakpointCreateFromFile called with invalid target."); 1134 return sberr; 1135 } 1136 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1137 1138 BreakpointIDList bp_ids; 1139 1140 std::vector<std::string> name_vector; 1141 size_t num_names = matching_names.GetSize(); 1142 for (size_t i = 0; i < num_names; i++) 1143 name_vector.push_back(matching_names.GetStringAtIndex(i)); 1144 1145 sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(), 1146 name_vector, bp_ids); 1147 if (sberr.Fail()) 1148 return sberr; 1149 1150 size_t num_bkpts = bp_ids.GetSize(); 1151 for (size_t i = 0; i < num_bkpts; i++) { 1152 BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i); 1153 new_bps.AppendByID(bp_id.GetBreakpointID()); 1154 } 1155 return sberr; 1156 } 1157 1158 lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) { 1159 SBError sberr; 1160 TargetSP target_sp(GetSP()); 1161 if (!target_sp) { 1162 sberr.SetErrorString("BreakpointWriteToFile called with invalid target."); 1163 return sberr; 1164 } 1165 SBBreakpointList bkpt_list(*this); 1166 return BreakpointsWriteToFile(dest_file, bkpt_list); 1167 } 1168 1169 lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file, 1170 SBBreakpointList &bkpt_list, 1171 bool append) { 1172 SBError sberr; 1173 TargetSP target_sp(GetSP()); 1174 if (!target_sp) { 1175 sberr.SetErrorString("BreakpointWriteToFile called with invalid target."); 1176 return sberr; 1177 } 1178 1179 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1180 BreakpointIDList bp_id_list; 1181 bkpt_list.CopyToBreakpointIDList(bp_id_list); 1182 sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(), 1183 bp_id_list, append); 1184 return sberr; 1185 } 1186 1187 uint32_t SBTarget::GetNumWatchpoints() const { 1188 TargetSP target_sp(GetSP()); 1189 if (target_sp) { 1190 // The watchpoint list is thread safe, no need to lock 1191 return target_sp->GetWatchpointList().GetSize(); 1192 } 1193 return 0; 1194 } 1195 1196 SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const { 1197 SBWatchpoint sb_watchpoint; 1198 TargetSP target_sp(GetSP()); 1199 if (target_sp) { 1200 // The watchpoint list is thread safe, no need to lock 1201 sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx)); 1202 } 1203 return sb_watchpoint; 1204 } 1205 1206 bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) { 1207 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1208 1209 bool result = false; 1210 TargetSP target_sp(GetSP()); 1211 if (target_sp) { 1212 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1213 std::unique_lock<std::recursive_mutex> lock; 1214 target_sp->GetWatchpointList().GetListMutex(lock); 1215 result = target_sp->RemoveWatchpointByID(wp_id); 1216 } 1217 1218 if (log) 1219 log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", 1220 static_cast<void *>(target_sp.get()), 1221 static_cast<uint32_t>(wp_id), result); 1222 1223 return result; 1224 } 1225 1226 SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) { 1227 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1228 1229 SBWatchpoint sb_watchpoint; 1230 lldb::WatchpointSP watchpoint_sp; 1231 TargetSP target_sp(GetSP()); 1232 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) { 1233 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1234 std::unique_lock<std::recursive_mutex> lock; 1235 target_sp->GetWatchpointList().GetListMutex(lock); 1236 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id); 1237 sb_watchpoint.SetSP(watchpoint_sp); 1238 } 1239 1240 if (log) 1241 log->Printf( 1242 "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)", 1243 static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id), 1244 static_cast<void *>(watchpoint_sp.get())); 1245 1246 return sb_watchpoint; 1247 } 1248 1249 lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size, 1250 bool read, bool write, 1251 SBError &error) { 1252 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1253 1254 SBWatchpoint sb_watchpoint; 1255 lldb::WatchpointSP watchpoint_sp; 1256 TargetSP target_sp(GetSP()); 1257 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && 1258 size > 0) { 1259 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1260 uint32_t watch_type = 0; 1261 if (read) 1262 watch_type |= LLDB_WATCH_TYPE_READ; 1263 if (write) 1264 watch_type |= LLDB_WATCH_TYPE_WRITE; 1265 if (watch_type == 0) { 1266 error.SetErrorString( 1267 "Can't create a watchpoint that is neither read nor write."); 1268 return sb_watchpoint; 1269 } 1270 1271 // Target::CreateWatchpoint() is thread safe. 1272 Status cw_error; 1273 // This API doesn't take in a type, so we can't figure out what it is. 1274 CompilerType *type = NULL; 1275 watchpoint_sp = 1276 target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error); 1277 error.SetError(cw_error); 1278 sb_watchpoint.SetSP(watchpoint_sp); 1279 } 1280 1281 if (log) 1282 log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 1283 ", 0x%u) => SBWatchpoint(%p)", 1284 static_cast<void *>(target_sp.get()), addr, 1285 static_cast<uint32_t>(size), 1286 static_cast<void *>(watchpoint_sp.get())); 1287 1288 return sb_watchpoint; 1289 } 1290 1291 bool SBTarget::EnableAllWatchpoints() { 1292 TargetSP target_sp(GetSP()); 1293 if (target_sp) { 1294 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1295 std::unique_lock<std::recursive_mutex> lock; 1296 target_sp->GetWatchpointList().GetListMutex(lock); 1297 target_sp->EnableAllWatchpoints(); 1298 return true; 1299 } 1300 return false; 1301 } 1302 1303 bool SBTarget::DisableAllWatchpoints() { 1304 TargetSP target_sp(GetSP()); 1305 if (target_sp) { 1306 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1307 std::unique_lock<std::recursive_mutex> lock; 1308 target_sp->GetWatchpointList().GetListMutex(lock); 1309 target_sp->DisableAllWatchpoints(); 1310 return true; 1311 } 1312 return false; 1313 } 1314 1315 SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr, 1316 SBType type) { 1317 SBValue sb_value; 1318 lldb::ValueObjectSP new_value_sp; 1319 if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) { 1320 lldb::addr_t load_addr(addr.GetLoadAddress(*this)); 1321 ExecutionContext exe_ctx( 1322 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false))); 1323 CompilerType ast_type(type.GetSP()->GetCompilerType(true)); 1324 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr, 1325 exe_ctx, ast_type); 1326 } 1327 sb_value.SetSP(new_value_sp); 1328 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1329 if (log) { 1330 if (new_value_sp) 1331 log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"", 1332 static_cast<void *>(m_opaque_sp.get()), 1333 new_value_sp->GetName().AsCString()); 1334 else 1335 log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL", 1336 static_cast<void *>(m_opaque_sp.get())); 1337 } 1338 return sb_value; 1339 } 1340 1341 lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data, 1342 lldb::SBType type) { 1343 SBValue sb_value; 1344 lldb::ValueObjectSP new_value_sp; 1345 if (IsValid() && name && *name && data.IsValid() && type.IsValid()) { 1346 DataExtractorSP extractor(*data); 1347 ExecutionContext exe_ctx( 1348 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false))); 1349 CompilerType ast_type(type.GetSP()->GetCompilerType(true)); 1350 new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor, 1351 exe_ctx, ast_type); 1352 } 1353 sb_value.SetSP(new_value_sp); 1354 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1355 if (log) { 1356 if (new_value_sp) 1357 log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"", 1358 static_cast<void *>(m_opaque_sp.get()), 1359 new_value_sp->GetName().AsCString()); 1360 else 1361 log->Printf("SBTarget(%p)::CreateValueFromData => NULL", 1362 static_cast<void *>(m_opaque_sp.get())); 1363 } 1364 return sb_value; 1365 } 1366 1367 lldb::SBValue SBTarget::CreateValueFromExpression(const char *name, 1368 const char *expr) { 1369 SBValue sb_value; 1370 lldb::ValueObjectSP new_value_sp; 1371 if (IsValid() && name && *name && expr && *expr) { 1372 ExecutionContext exe_ctx( 1373 ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false))); 1374 new_value_sp = 1375 ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx); 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)::CreateValueFromExpression => \"%s\"", 1382 static_cast<void *>(m_opaque_sp.get()), 1383 new_value_sp->GetName().AsCString()); 1384 else 1385 log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL", 1386 static_cast<void *>(m_opaque_sp.get())); 1387 } 1388 return sb_value; 1389 } 1390 1391 bool SBTarget::DeleteAllWatchpoints() { 1392 TargetSP target_sp(GetSP()); 1393 if (target_sp) { 1394 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1395 std::unique_lock<std::recursive_mutex> lock; 1396 target_sp->GetWatchpointList().GetListMutex(lock); 1397 target_sp->RemoveAllWatchpoints(); 1398 return true; 1399 } 1400 return false; 1401 } 1402 1403 lldb::SBModule SBTarget::AddModule(const char *path, const char *triple, 1404 const char *uuid_cstr) { 1405 return AddModule(path, triple, uuid_cstr, NULL); 1406 } 1407 1408 lldb::SBModule SBTarget::AddModule(const char *path, const char *triple, 1409 const char *uuid_cstr, const char *symfile) { 1410 lldb::SBModule sb_module; 1411 TargetSP target_sp(GetSP()); 1412 if (target_sp) { 1413 ModuleSpec module_spec; 1414 if (path) 1415 module_spec.GetFileSpec().SetFile(path, false); 1416 1417 if (uuid_cstr) 1418 module_spec.GetUUID().SetFromCString(uuid_cstr); 1419 1420 if (triple) 1421 module_spec.GetArchitecture().SetTriple(triple, 1422 target_sp->GetPlatform().get()); 1423 else 1424 module_spec.GetArchitecture() = target_sp->GetArchitecture(); 1425 1426 if (symfile) 1427 module_spec.GetSymbolFileSpec().SetFile(symfile, false); 1428 1429 sb_module.SetSP(target_sp->GetSharedModule(module_spec)); 1430 } 1431 return sb_module; 1432 } 1433 1434 lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) { 1435 lldb::SBModule sb_module; 1436 TargetSP target_sp(GetSP()); 1437 if (target_sp) 1438 sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_ap)); 1439 return sb_module; 1440 } 1441 1442 bool SBTarget::AddModule(lldb::SBModule &module) { 1443 TargetSP target_sp(GetSP()); 1444 if (target_sp) { 1445 target_sp->GetImages().AppendIfNeeded(module.GetSP()); 1446 return true; 1447 } 1448 return false; 1449 } 1450 1451 uint32_t SBTarget::GetNumModules() const { 1452 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1453 1454 uint32_t num = 0; 1455 TargetSP target_sp(GetSP()); 1456 if (target_sp) { 1457 // The module list is thread safe, no need to lock 1458 num = target_sp->GetImages().GetSize(); 1459 } 1460 1461 if (log) 1462 log->Printf("SBTarget(%p)::GetNumModules () => %d", 1463 static_cast<void *>(target_sp.get()), num); 1464 1465 return num; 1466 } 1467 1468 void SBTarget::Clear() { 1469 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1470 1471 if (log) 1472 log->Printf("SBTarget(%p)::Clear ()", 1473 static_cast<void *>(m_opaque_sp.get())); 1474 1475 m_opaque_sp.reset(); 1476 } 1477 1478 SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) { 1479 SBModule sb_module; 1480 TargetSP target_sp(GetSP()); 1481 if (target_sp && sb_file_spec.IsValid()) { 1482 ModuleSpec module_spec(*sb_file_spec); 1483 // The module list is thread safe, no need to lock 1484 sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec)); 1485 } 1486 return sb_module; 1487 } 1488 1489 lldb::ByteOrder SBTarget::GetByteOrder() { 1490 TargetSP target_sp(GetSP()); 1491 if (target_sp) 1492 return target_sp->GetArchitecture().GetByteOrder(); 1493 return eByteOrderInvalid; 1494 } 1495 1496 const char *SBTarget::GetTriple() { 1497 TargetSP target_sp(GetSP()); 1498 if (target_sp) { 1499 std::string triple(target_sp->GetArchitecture().GetTriple().str()); 1500 // Unique the string so we don't run into ownership issues since 1501 // the const strings put the string into the string pool once and 1502 // the strings never comes out 1503 ConstString const_triple(triple.c_str()); 1504 return const_triple.GetCString(); 1505 } 1506 return NULL; 1507 } 1508 1509 uint32_t SBTarget::GetDataByteSize() { 1510 TargetSP target_sp(GetSP()); 1511 if (target_sp) { 1512 return target_sp->GetArchitecture().GetDataByteSize(); 1513 } 1514 return 0; 1515 } 1516 1517 uint32_t SBTarget::GetCodeByteSize() { 1518 TargetSP target_sp(GetSP()); 1519 if (target_sp) { 1520 return target_sp->GetArchitecture().GetCodeByteSize(); 1521 } 1522 return 0; 1523 } 1524 1525 uint32_t SBTarget::GetAddressByteSize() { 1526 TargetSP target_sp(GetSP()); 1527 if (target_sp) 1528 return target_sp->GetArchitecture().GetAddressByteSize(); 1529 return sizeof(void *); 1530 } 1531 1532 SBModule SBTarget::GetModuleAtIndex(uint32_t idx) { 1533 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1534 1535 SBModule sb_module; 1536 ModuleSP module_sp; 1537 TargetSP target_sp(GetSP()); 1538 if (target_sp) { 1539 // The module list is thread safe, no need to lock 1540 module_sp = target_sp->GetImages().GetModuleAtIndex(idx); 1541 sb_module.SetSP(module_sp); 1542 } 1543 1544 if (log) 1545 log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)", 1546 static_cast<void *>(target_sp.get()), idx, 1547 static_cast<void *>(module_sp.get())); 1548 1549 return sb_module; 1550 } 1551 1552 bool SBTarget::RemoveModule(lldb::SBModule module) { 1553 TargetSP target_sp(GetSP()); 1554 if (target_sp) 1555 return target_sp->GetImages().Remove(module.GetSP()); 1556 return false; 1557 } 1558 1559 SBBroadcaster SBTarget::GetBroadcaster() const { 1560 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 1561 1562 TargetSP target_sp(GetSP()); 1563 SBBroadcaster broadcaster(target_sp.get(), false); 1564 1565 if (log) 1566 log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)", 1567 static_cast<void *>(target_sp.get()), 1568 static_cast<void *>(broadcaster.get())); 1569 1570 return broadcaster; 1571 } 1572 1573 bool SBTarget::GetDescription(SBStream &description, 1574 lldb::DescriptionLevel description_level) { 1575 Stream &strm = description.ref(); 1576 1577 TargetSP target_sp(GetSP()); 1578 if (target_sp) { 1579 target_sp->Dump(&strm, description_level); 1580 } else 1581 strm.PutCString("No value"); 1582 1583 return true; 1584 } 1585 1586 lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name, 1587 uint32_t name_type_mask) { 1588 lldb::SBSymbolContextList sb_sc_list; 1589 if (name && name[0]) { 1590 TargetSP target_sp(GetSP()); 1591 if (target_sp) { 1592 const bool symbols_ok = true; 1593 const bool inlines_ok = true; 1594 const bool append = true; 1595 target_sp->GetImages().FindFunctions(ConstString(name), name_type_mask, 1596 symbols_ok, inlines_ok, append, 1597 *sb_sc_list); 1598 } 1599 } 1600 return sb_sc_list; 1601 } 1602 1603 lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name, 1604 uint32_t max_matches, 1605 MatchType matchtype) { 1606 lldb::SBSymbolContextList sb_sc_list; 1607 if (name && name[0]) { 1608 llvm::StringRef name_ref(name); 1609 TargetSP target_sp(GetSP()); 1610 if (target_sp) { 1611 std::string regexstr; 1612 switch (matchtype) { 1613 case eMatchTypeRegex: 1614 target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true, 1615 true, true, *sb_sc_list); 1616 break; 1617 case eMatchTypeStartsWith: 1618 regexstr = llvm::Regex::escape(name) + ".*"; 1619 target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true, 1620 true, true, *sb_sc_list); 1621 break; 1622 default: 1623 target_sp->GetImages().FindFunctions(ConstString(name), 1624 eFunctionNameTypeAny, true, true, 1625 true, *sb_sc_list); 1626 break; 1627 } 1628 } 1629 } 1630 return sb_sc_list; 1631 } 1632 1633 lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) { 1634 TargetSP target_sp(GetSP()); 1635 if (typename_cstr && typename_cstr[0] && target_sp) { 1636 ConstString const_typename(typename_cstr); 1637 SymbolContext sc; 1638 const bool exact_match = false; 1639 1640 const ModuleList &module_list = target_sp->GetImages(); 1641 size_t count = module_list.GetSize(); 1642 for (size_t idx = 0; idx < count; idx++) { 1643 ModuleSP module_sp(module_list.GetModuleAtIndex(idx)); 1644 if (module_sp) { 1645 TypeSP type_sp( 1646 module_sp->FindFirstType(sc, const_typename, exact_match)); 1647 if (type_sp) 1648 return SBType(type_sp); 1649 } 1650 } 1651 1652 // Didn't find the type in the symbols; try the Objective-C runtime 1653 // if one is installed 1654 1655 ProcessSP process_sp(target_sp->GetProcessSP()); 1656 1657 if (process_sp) { 1658 ObjCLanguageRuntime *objc_language_runtime = 1659 process_sp->GetObjCLanguageRuntime(); 1660 1661 if (objc_language_runtime) { 1662 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor(); 1663 1664 if (objc_decl_vendor) { 1665 std::vector<clang::NamedDecl *> decls; 1666 1667 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) { 1668 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) { 1669 return SBType(type); 1670 } 1671 } 1672 } 1673 } 1674 } 1675 1676 // No matches, search for basic typename matches 1677 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext(); 1678 if (clang_ast) 1679 return SBType(ClangASTContext::GetBasicType(clang_ast->getASTContext(), 1680 const_typename)); 1681 } 1682 return SBType(); 1683 } 1684 1685 SBType SBTarget::GetBasicType(lldb::BasicType type) { 1686 TargetSP target_sp(GetSP()); 1687 if (target_sp) { 1688 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext(); 1689 if (clang_ast) 1690 return SBType( 1691 ClangASTContext::GetBasicType(clang_ast->getASTContext(), type)); 1692 } 1693 return SBType(); 1694 } 1695 1696 lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) { 1697 SBTypeList sb_type_list; 1698 TargetSP target_sp(GetSP()); 1699 if (typename_cstr && typename_cstr[0] && target_sp) { 1700 ModuleList &images = target_sp->GetImages(); 1701 ConstString const_typename(typename_cstr); 1702 bool exact_match = false; 1703 SymbolContext sc; 1704 TypeList type_list; 1705 llvm::DenseSet<SymbolFile *> searched_symbol_files; 1706 uint32_t num_matches = 1707 images.FindTypes(sc, const_typename, exact_match, UINT32_MAX, 1708 searched_symbol_files, type_list); 1709 1710 if (num_matches > 0) { 1711 for (size_t idx = 0; idx < num_matches; idx++) { 1712 TypeSP type_sp(type_list.GetTypeAtIndex(idx)); 1713 if (type_sp) 1714 sb_type_list.Append(SBType(type_sp)); 1715 } 1716 } 1717 1718 // Try the Objective-C runtime if one is installed 1719 1720 ProcessSP process_sp(target_sp->GetProcessSP()); 1721 1722 if (process_sp) { 1723 ObjCLanguageRuntime *objc_language_runtime = 1724 process_sp->GetObjCLanguageRuntime(); 1725 1726 if (objc_language_runtime) { 1727 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor(); 1728 1729 if (objc_decl_vendor) { 1730 std::vector<clang::NamedDecl *> decls; 1731 1732 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) { 1733 for (clang::NamedDecl *decl : decls) { 1734 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) { 1735 sb_type_list.Append(SBType(type)); 1736 } 1737 } 1738 } 1739 } 1740 } 1741 } 1742 1743 if (sb_type_list.GetSize() == 0) { 1744 // No matches, search for basic typename matches 1745 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext(); 1746 if (clang_ast) 1747 sb_type_list.Append(SBType(ClangASTContext::GetBasicType( 1748 clang_ast->getASTContext(), const_typename))); 1749 } 1750 } 1751 return sb_type_list; 1752 } 1753 1754 SBValueList SBTarget::FindGlobalVariables(const char *name, 1755 uint32_t max_matches) { 1756 SBValueList sb_value_list; 1757 1758 TargetSP target_sp(GetSP()); 1759 if (name && target_sp) { 1760 VariableList variable_list; 1761 const bool append = true; 1762 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables( 1763 ConstString(name), append, max_matches, variable_list); 1764 1765 if (match_count > 0) { 1766 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get(); 1767 if (exe_scope == NULL) 1768 exe_scope = target_sp.get(); 1769 for (uint32_t i = 0; i < match_count; ++i) { 1770 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create( 1771 exe_scope, variable_list.GetVariableAtIndex(i))); 1772 if (valobj_sp) 1773 sb_value_list.Append(SBValue(valobj_sp)); 1774 } 1775 } 1776 } 1777 1778 return sb_value_list; 1779 } 1780 1781 SBValueList SBTarget::FindGlobalVariables(const char *name, 1782 uint32_t max_matches, 1783 MatchType matchtype) { 1784 SBValueList sb_value_list; 1785 1786 TargetSP target_sp(GetSP()); 1787 if (name && target_sp) { 1788 llvm::StringRef name_ref(name); 1789 VariableList variable_list; 1790 const bool append = true; 1791 1792 std::string regexstr; 1793 uint32_t match_count; 1794 switch (matchtype) { 1795 case eMatchTypeNormal: 1796 match_count = target_sp->GetImages().FindGlobalVariables( 1797 ConstString(name), append, max_matches, variable_list); 1798 break; 1799 case eMatchTypeRegex: 1800 match_count = target_sp->GetImages().FindGlobalVariables( 1801 RegularExpression(name_ref), append, max_matches, variable_list); 1802 break; 1803 case eMatchTypeStartsWith: 1804 regexstr = llvm::Regex::escape(name) + ".*"; 1805 match_count = target_sp->GetImages().FindGlobalVariables( 1806 RegularExpression(regexstr), append, max_matches, variable_list); 1807 break; 1808 } 1809 1810 if (match_count > 0) { 1811 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get(); 1812 if (exe_scope == NULL) 1813 exe_scope = target_sp.get(); 1814 for (uint32_t i = 0; i < match_count; ++i) { 1815 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create( 1816 exe_scope, variable_list.GetVariableAtIndex(i))); 1817 if (valobj_sp) 1818 sb_value_list.Append(SBValue(valobj_sp)); 1819 } 1820 } 1821 } 1822 1823 return sb_value_list; 1824 } 1825 1826 lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) { 1827 SBValueList sb_value_list(FindGlobalVariables(name, 1)); 1828 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0) 1829 return sb_value_list.GetValueAtIndex(0); 1830 return SBValue(); 1831 } 1832 1833 SBSourceManager SBTarget::GetSourceManager() { 1834 SBSourceManager source_manager(*this); 1835 return source_manager; 1836 } 1837 1838 lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr, 1839 uint32_t count) { 1840 return ReadInstructions(base_addr, count, NULL); 1841 } 1842 1843 lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr, 1844 uint32_t count, 1845 const char *flavor_string) { 1846 SBInstructionList sb_instructions; 1847 1848 TargetSP target_sp(GetSP()); 1849 if (target_sp) { 1850 Address *addr_ptr = base_addr.get(); 1851 1852 if (addr_ptr) { 1853 DataBufferHeap data( 1854 target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0); 1855 bool prefer_file_cache = false; 1856 lldb_private::Status error; 1857 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS; 1858 const size_t bytes_read = 1859 target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(), 1860 data.GetByteSize(), error, &load_addr); 1861 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS; 1862 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes( 1863 target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr, 1864 data.GetBytes(), bytes_read, count, data_from_file)); 1865 } 1866 } 1867 1868 return sb_instructions; 1869 } 1870 1871 lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr, 1872 const void *buf, 1873 size_t size) { 1874 return GetInstructionsWithFlavor(base_addr, NULL, buf, size); 1875 } 1876 1877 lldb::SBInstructionList 1878 SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr, 1879 const char *flavor_string, const void *buf, 1880 size_t size) { 1881 SBInstructionList sb_instructions; 1882 1883 TargetSP target_sp(GetSP()); 1884 if (target_sp) { 1885 Address addr; 1886 1887 if (base_addr.get()) 1888 addr = *base_addr.get(); 1889 1890 const bool data_from_file = true; 1891 1892 sb_instructions.SetDisassembler(Disassembler::DisassembleBytes( 1893 target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size, 1894 UINT32_MAX, data_from_file)); 1895 } 1896 1897 return sb_instructions; 1898 } 1899 1900 lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr, 1901 const void *buf, 1902 size_t size) { 1903 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf, 1904 size); 1905 } 1906 1907 lldb::SBInstructionList 1908 SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr, 1909 const char *flavor_string, const void *buf, 1910 size_t size) { 1911 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string, 1912 buf, size); 1913 } 1914 1915 SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section, 1916 lldb::addr_t section_base_addr) { 1917 SBError sb_error; 1918 TargetSP target_sp(GetSP()); 1919 if (target_sp) { 1920 if (!section.IsValid()) { 1921 sb_error.SetErrorStringWithFormat("invalid section"); 1922 } else { 1923 SectionSP section_sp(section.GetSP()); 1924 if (section_sp) { 1925 if (section_sp->IsThreadSpecific()) { 1926 sb_error.SetErrorString( 1927 "thread specific sections are not yet supported"); 1928 } else { 1929 ProcessSP process_sp(target_sp->GetProcessSP()); 1930 if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) { 1931 ModuleSP module_sp(section_sp->GetModule()); 1932 if (module_sp) { 1933 ModuleList module_list; 1934 module_list.Append(module_sp); 1935 target_sp->ModulesDidLoad(module_list); 1936 } 1937 // Flush info in the process (stack frames, etc) 1938 if (process_sp) 1939 process_sp->Flush(); 1940 } 1941 } 1942 } 1943 } 1944 } else { 1945 sb_error.SetErrorString("invalid target"); 1946 } 1947 return sb_error; 1948 } 1949 1950 SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) { 1951 SBError sb_error; 1952 1953 TargetSP target_sp(GetSP()); 1954 if (target_sp) { 1955 if (!section.IsValid()) { 1956 sb_error.SetErrorStringWithFormat("invalid section"); 1957 } else { 1958 SectionSP section_sp(section.GetSP()); 1959 if (section_sp) { 1960 ProcessSP process_sp(target_sp->GetProcessSP()); 1961 if (target_sp->SetSectionUnloaded(section_sp)) { 1962 ModuleSP module_sp(section_sp->GetModule()); 1963 if (module_sp) { 1964 ModuleList module_list; 1965 module_list.Append(module_sp); 1966 target_sp->ModulesDidUnload(module_list, false); 1967 } 1968 // Flush info in the process (stack frames, etc) 1969 if (process_sp) 1970 process_sp->Flush(); 1971 } 1972 } else { 1973 sb_error.SetErrorStringWithFormat("invalid section"); 1974 } 1975 } 1976 } else { 1977 sb_error.SetErrorStringWithFormat("invalid target"); 1978 } 1979 return sb_error; 1980 } 1981 1982 SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module, 1983 int64_t slide_offset) { 1984 SBError sb_error; 1985 1986 TargetSP target_sp(GetSP()); 1987 if (target_sp) { 1988 ModuleSP module_sp(module.GetSP()); 1989 if (module_sp) { 1990 bool changed = false; 1991 if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) { 1992 // The load was successful, make sure that at least some sections 1993 // changed before we notify that our module was loaded. 1994 if (changed) { 1995 ModuleList module_list; 1996 module_list.Append(module_sp); 1997 target_sp->ModulesDidLoad(module_list); 1998 // Flush info in the process (stack frames, etc) 1999 ProcessSP process_sp(target_sp->GetProcessSP()); 2000 if (process_sp) 2001 process_sp->Flush(); 2002 } 2003 } 2004 } else { 2005 sb_error.SetErrorStringWithFormat("invalid module"); 2006 } 2007 2008 } else { 2009 sb_error.SetErrorStringWithFormat("invalid target"); 2010 } 2011 return sb_error; 2012 } 2013 2014 SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) { 2015 SBError sb_error; 2016 2017 char path[PATH_MAX]; 2018 TargetSP target_sp(GetSP()); 2019 if (target_sp) { 2020 ModuleSP module_sp(module.GetSP()); 2021 if (module_sp) { 2022 ObjectFile *objfile = module_sp->GetObjectFile(); 2023 if (objfile) { 2024 SectionList *section_list = objfile->GetSectionList(); 2025 if (section_list) { 2026 ProcessSP process_sp(target_sp->GetProcessSP()); 2027 2028 bool changed = false; 2029 const size_t num_sections = section_list->GetSize(); 2030 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 2031 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 2032 if (section_sp) 2033 changed |= target_sp->SetSectionUnloaded(section_sp); 2034 } 2035 if (changed) { 2036 ModuleList module_list; 2037 module_list.Append(module_sp); 2038 target_sp->ModulesDidUnload(module_list, false); 2039 // Flush info in the process (stack frames, etc) 2040 ProcessSP process_sp(target_sp->GetProcessSP()); 2041 if (process_sp) 2042 process_sp->Flush(); 2043 } 2044 } else { 2045 module_sp->GetFileSpec().GetPath(path, sizeof(path)); 2046 sb_error.SetErrorStringWithFormat("no sections in object file '%s'", 2047 path); 2048 } 2049 } else { 2050 module_sp->GetFileSpec().GetPath(path, sizeof(path)); 2051 sb_error.SetErrorStringWithFormat("no object file for module '%s'", 2052 path); 2053 } 2054 } else { 2055 sb_error.SetErrorStringWithFormat("invalid module"); 2056 } 2057 } else { 2058 sb_error.SetErrorStringWithFormat("invalid target"); 2059 } 2060 return sb_error; 2061 } 2062 2063 lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name, 2064 lldb::SymbolType symbol_type) { 2065 SBSymbolContextList sb_sc_list; 2066 if (name && name[0]) { 2067 TargetSP target_sp(GetSP()); 2068 if (target_sp) { 2069 bool append = true; 2070 target_sp->GetImages().FindSymbolsWithNameAndType( 2071 ConstString(name), symbol_type, *sb_sc_list, append); 2072 } 2073 } 2074 return sb_sc_list; 2075 } 2076 2077 lldb::SBValue SBTarget::EvaluateExpression(const char *expr) { 2078 TargetSP target_sp(GetSP()); 2079 if (!target_sp) 2080 return SBValue(); 2081 2082 SBExpressionOptions options; 2083 lldb::DynamicValueType fetch_dynamic_value = 2084 target_sp->GetPreferDynamicValue(); 2085 options.SetFetchDynamicValue(fetch_dynamic_value); 2086 options.SetUnwindOnError(true); 2087 return EvaluateExpression(expr, options); 2088 } 2089 2090 lldb::SBValue SBTarget::EvaluateExpression(const char *expr, 2091 const SBExpressionOptions &options) { 2092 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 2093 #if !defined(LLDB_DISABLE_PYTHON) 2094 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 2095 #endif 2096 SBValue expr_result; 2097 ExpressionResults exe_results = eExpressionSetupError; 2098 ValueObjectSP expr_value_sp; 2099 TargetSP target_sp(GetSP()); 2100 StackFrame *frame = NULL; 2101 if (target_sp) { 2102 if (expr == NULL || expr[0] == '\0') { 2103 if (log) 2104 log->Printf( 2105 "SBTarget::EvaluateExpression called with an empty expression"); 2106 return expr_result; 2107 } 2108 2109 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 2110 ExecutionContext exe_ctx(m_opaque_sp.get()); 2111 2112 if (log) 2113 log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr); 2114 2115 frame = exe_ctx.GetFramePtr(); 2116 Target *target = exe_ctx.GetTargetPtr(); 2117 2118 if (target) { 2119 #ifdef LLDB_CONFIGURATION_DEBUG 2120 StreamString frame_description; 2121 if (frame) 2122 frame->DumpUsingSettingsFormat(&frame_description); 2123 llvm::PrettyStackTraceFormat stack_trace( 2124 "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = " 2125 "%u) %s", 2126 expr, options.GetFetchDynamicValue(), 2127 frame_description.GetString().str().c_str()); 2128 #endif 2129 exe_results = 2130 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref()); 2131 2132 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue()); 2133 } else { 2134 if (log) 2135 log->Printf("SBTarget::EvaluateExpression () => error: could not " 2136 "reconstruct frame object for this SBTarget."); 2137 } 2138 } 2139 #ifndef LLDB_DISABLE_PYTHON 2140 if (expr_log) 2141 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is " 2142 "%s, summary %s **", 2143 expr_result.GetValue(), expr_result.GetSummary()); 2144 2145 if (log) 2146 log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) " 2147 "(execution result=%d)", 2148 static_cast<void *>(frame), expr, 2149 static_cast<void *>(expr_value_sp.get()), exe_results); 2150 #endif 2151 2152 return expr_result; 2153 } 2154 2155 lldb::addr_t SBTarget::GetStackRedZoneSize() { 2156 TargetSP target_sp(GetSP()); 2157 if (target_sp) { 2158 ABISP abi_sp; 2159 ProcessSP process_sp(target_sp->GetProcessSP()); 2160 if (process_sp) 2161 abi_sp = process_sp->GetABI(); 2162 else 2163 abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture()); 2164 if (abi_sp) 2165 return abi_sp->GetRedZoneSize(); 2166 } 2167 return 0; 2168 } 2169 2170 lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const { 2171 lldb::SBLaunchInfo launch_info(NULL); 2172 TargetSP target_sp(GetSP()); 2173 if (target_sp) 2174 launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo(); 2175 return launch_info; 2176 } 2177 2178 void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) { 2179 TargetSP target_sp(GetSP()); 2180 if (target_sp) 2181 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref()); 2182 } 2183