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