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