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