1 //===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBTarget.h"
12 #include "lldb/lldb-public.h"
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"
65 #include "lldb/Interpreter/CommandReturnObject.h"
66 #include "../source/Commands/CommandObjectBreakpoint.h"
67 #include "llvm/Support/Regex.h"
71 using namespace lldb_private;
73 #define DEFAULT_DISASM_BYTE_SIZE 32
78 AttachToProcess (ProcessAttachInfo &attach_info, Target &target)
80 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
82 auto process_sp = target.GetProcessSP ();
85 const auto state = process_sp->GetState ();
86 if (process_sp->IsAlive () && state == eStateConnected)
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");
96 return target.Attach (attach_info, nullptr);
101 //----------------------------------------------------------------------
102 // SBTarget constructor
103 //----------------------------------------------------------------------
104 SBTarget::SBTarget () :
109 SBTarget::SBTarget (const SBTarget& rhs) :
110 m_opaque_sp (rhs.m_opaque_sp)
114 SBTarget::SBTarget(const TargetSP& target_sp) :
115 m_opaque_sp (target_sp)
120 SBTarget::operator = (const SBTarget& rhs)
123 m_opaque_sp = rhs.m_opaque_sp;
127 //----------------------------------------------------------------------
129 //----------------------------------------------------------------------
130 SBTarget::~SBTarget()
135 SBTarget::EventIsTargetEvent (const SBEvent &event)
137 return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
141 SBTarget::GetTargetFromEvent (const SBEvent &event)
143 return Target::TargetEventData::GetTargetFromEvent (event.get());
147 SBTarget::GetNumModulesFromEvent (const SBEvent &event)
149 const ModuleList module_list = Target::TargetEventData::GetModuleListFromEvent (event.get());
150 return module_list.GetSize();
154 SBTarget::GetModuleAtIndexFromEvent (const uint32_t idx, const SBEvent &event)
156 const ModuleList module_list = Target::TargetEventData::GetModuleListFromEvent (event.get());
157 return SBModule(module_list.GetModuleAtIndex(idx));
161 SBTarget::GetBroadcasterClassName ()
163 return Target::GetStaticBroadcasterClass().AsCString();
167 SBTarget::IsValid () const
169 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
173 SBTarget::GetProcess ()
175 SBProcess sb_process;
176 ProcessSP process_sp;
177 TargetSP target_sp(GetSP());
180 process_sp = target_sp->GetProcessSP();
181 sb_process.SetSP (process_sp);
184 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
186 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
187 static_cast<void*>(target_sp.get()),
188 static_cast<void*>(process_sp.get()));
194 SBTarget::GetPlatform ()
196 TargetSP target_sp(GetSP());
201 platform.m_opaque_sp = target_sp->GetPlatform();
207 SBTarget::GetDebugger () const
210 TargetSP target_sp(GetSP());
212 debugger.reset (target_sp->GetDebugger().shared_from_this());
217 SBTarget::LoadCore (const char *core_file)
219 SBProcess sb_process;
220 TargetSP target_sp(GetSP());
223 FileSpec filespec(core_file, true);
224 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
229 process_sp->LoadCore();
230 sb_process.SetSP (process_sp);
237 SBTarget::LaunchSimple
241 const char *working_directory
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;
250 SBListener listener = GetDebugger().GetListener();
251 return Launch (listener,
267 TargetSP target_sp(GetSP());
270 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
271 sb_error.ref() = target_sp->Install(NULL);
279 SBListener &listener,
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
291 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
293 SBProcess sb_process;
294 ProcessSP process_sp;
295 TargetSP target_sp(GetSP());
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()));
310 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
313 launch_flags |= eLaunchFlagStopAtEntry;
315 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
316 launch_flags |= eLaunchFlagDisableASLR;
318 StateType state = eStateInvalid;
319 process_sp = target_sp->GetProcessSP();
322 state = process_sp->GetState();
324 if (process_sp->IsAlive() && state != eStateConnected)
326 if (state == eStateAttaching)
327 error.SetErrorString ("process attach is in progress");
329 error.SetErrorString ("a process is already being debugged");
334 if (state == eStateConnected)
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())
341 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
346 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
347 launch_flags |= eLaunchFlagDisableSTDIO;
349 ProcessLaunchInfo launch_info(FileSpec{stdin_path, false},
350 FileSpec{stdout_path, false},
351 FileSpec{stderr_path, false},
352 FileSpec{working_directory, false},
355 Module *exe_module = target_sp->GetExecutableModulePointer();
357 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
359 launch_info.GetArguments().AppendArguments (argv);
361 launch_info.GetEnvironmentEntries ().SetArguments (envp);
363 if (listener.IsValid())
364 launch_info.SetListener(listener.GetSP());
366 error.SetError (target_sp->Launch(launch_info, NULL));
368 sb_process.SetSP(target_sp->GetProcessSP());
372 error.SetErrorString ("SBTarget is invalid");
375 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
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());
384 SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
386 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
388 SBProcess sb_process;
389 TargetSP target_sp(GetSP());
392 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...",
393 static_cast<void*>(target_sp.get()));
397 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
398 StateType state = eStateInvalid;
400 ProcessSP process_sp = target_sp->GetProcessSP();
403 state = process_sp->GetState();
405 if (process_sp->IsAlive() && state != eStateConnected)
407 if (state == eStateAttaching)
408 error.SetErrorString ("process attach is in progress");
410 error.SetErrorString ("a process is already being debugged");
416 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
418 if (!launch_info.GetExecutableFile())
420 Module *exe_module = target_sp->GetExecutableModulePointer();
422 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
425 const ArchSpec &arch_spec = target_sp->GetArchitecture();
426 if (arch_spec.IsValid())
427 launch_info.GetArchitecture () = arch_spec;
429 error.SetError (target_sp->Launch (launch_info, NULL));
430 sb_process.SetSP(target_sp->GetProcessSP());
434 error.SetErrorString ("SBTarget is invalid");
437 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
439 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
440 static_cast<void*>(target_sp.get()),
441 static_cast<void*>(sb_process.GetSP().get()));
447 SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
449 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
451 SBProcess sb_process;
452 TargetSP target_sp(GetSP());
455 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...",
456 static_cast<void*>(target_sp.get()));
460 ProcessAttachInfo &attach_info = sb_attach_info.ref();
461 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
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())
467 lldb::pid_t attach_pid = attach_info.GetProcessID();
468 ProcessInstanceInfo instance_info;
469 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
471 attach_info.SetUserID(instance_info.GetEffectiveUserID());
475 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
478 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
479 static_cast<void*>(target_sp.get()), error.GetCString());
485 error.SetError(AttachToProcess(attach_info, *target_sp));
487 sb_process.SetSP(target_sp->GetProcessSP());
491 error.SetErrorString ("SBTarget is invalid");
495 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
496 static_cast<void*>(target_sp.get()),
497 static_cast<void*>(sb_process.GetSP().get()));
503 #if defined(__APPLE__)
506 SBTarget::AttachToProcessWithID (SBListener &listener,
508 lldb::SBError& error)
510 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
513 #endif // #if defined(__APPLE__)
516 SBTarget::AttachToProcessWithID
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
523 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
525 SBProcess sb_process;
526 TargetSP target_sp(GetSP());
529 log->Printf ("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
530 static_cast<void*>(target_sp.get()),
536 ProcessAttachInfo attach_info;
537 attach_info.SetProcessID (pid);
538 if (listener.IsValid())
539 attach_info.SetListener(listener.GetSP());
541 ProcessInstanceInfo instance_info;
542 if (target_sp->GetPlatform ()->GetProcessInfo (pid, instance_info))
543 attach_info.SetUserID (instance_info.GetEffectiveUserID ());
545 error.SetError (AttachToProcess (attach_info, *target_sp));
546 if (error.Success ())
547 sb_process.SetSP (target_sp->GetProcessSP ());
550 error.SetErrorString ("SBTarget is invalid");
553 log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
554 static_cast<void*>(target_sp.get ()),
556 static_cast<void*>(sb_process.GetSP().get ()));
561 SBTarget::AttachToProcessWithName
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
569 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
571 SBProcess sb_process;
572 TargetSP target_sp(GetSP());
575 log->Printf ("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
576 static_cast<void*>(target_sp.get()),
579 wait_for ? "true" : "false");
581 if (name && target_sp)
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());
589 error.SetError (AttachToProcess (attach_info, *target_sp));
590 if (error.Success ())
591 sb_process.SetSP (target_sp->GetProcessSP ());
594 error.SetErrorString ("SBTarget is invalid");
597 log->Printf ("SBTarget(%p)::%s (...) => SBProcess(%p)",
598 static_cast<void*>(target_sp.get()),
600 static_cast<void*>(sb_process.GetSP().get()));
605 SBTarget::ConnectRemote
607 SBListener &listener,
609 const char *plugin_name,
613 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
615 SBProcess sb_process;
616 ProcessSP process_sp;
617 TargetSP target_sp(GetSP());
620 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...",
621 static_cast<void*>(target_sp.get()), url, plugin_name);
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);
629 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
633 sb_process.SetSP (process_sp);
634 error.SetError (process_sp->ConnectRemote (NULL, url));
638 error.SetErrorString ("unable to create lldb_private::Process");
643 error.SetErrorString ("SBTarget is invalid");
647 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
648 static_cast<void*>(target_sp.get()),
649 static_cast<void*>(process_sp.get()));
654 SBTarget::GetExecutable ()
657 SBFileSpec exe_file_spec;
658 TargetSP target_sp(GetSP());
661 Module *exe_module = target_sp->GetExecutableModulePointer();
663 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
666 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
669 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
670 static_cast<void*>(target_sp.get()),
671 static_cast<const void*>(exe_file_spec.get()));
674 return exe_file_spec;
678 SBTarget::operator == (const SBTarget &rhs) const
680 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
684 SBTarget::operator != (const SBTarget &rhs) const
686 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
690 SBTarget::GetSP () const
696 SBTarget::SetSP (const lldb::TargetSP& target_sp)
698 m_opaque_sp = target_sp;
702 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
704 lldb::SBAddress sb_addr;
705 Address &addr = sb_addr.ref();
706 TargetSP target_sp(GetSP());
709 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
710 if (target_sp->ResolveLoadAddress (vm_addr, addr))
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);
721 SBTarget::ResolveFileAddress (lldb::addr_t file_addr)
723 lldb::SBAddress sb_addr;
724 Address &addr = sb_addr.ref();
725 TargetSP target_sp(GetSP());
728 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
729 if (target_sp->ResolveFileAddress (file_addr, addr))
733 addr.SetRawAddress(file_addr);
738 SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
740 lldb::SBAddress sb_addr;
741 Address &addr = sb_addr.ref();
742 TargetSP target_sp(GetSP());
745 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
746 if (target_sp->ResolveLoadAddress (vm_addr, addr))
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);
757 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
758 uint32_t resolve_scope)
763 TargetSP target_sp(GetSP());
765 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
771 SBTarget::ReadMemory (const SBAddress addr,
774 lldb::SBError &error)
777 size_t bytes_read = 0;
778 TargetSP target_sp(GetSP());
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());
786 sb_error.SetErrorString("invalid target");
793 SBTarget::BreakpointCreateByLocation (const char *file,
796 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
800 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
803 return BreakpointCreateByLocation(sb_file_spec, line, 0);
807 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
811 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
814 TargetSP target_sp(GetSP());
815 if (target_sp && line != 0)
817 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
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,
832 move_to_nearest_code);
838 sb_bp.GetDescription (sstr);
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());
850 SBTarget::BreakpointCreateByName (const char *symbol_name,
851 const char *module_name)
853 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
856 TargetSP target_sp(GetSP());
859 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
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])
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);
873 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
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()));
886 SBTarget::BreakpointCreateByName (const char *symbol_name,
887 const SBFileSpecList &module_list,
888 const SBFileSpecList &comp_unit_list)
890 uint32_t name_type_mask = eFunctionNameTypeAuto;
891 return BreakpointCreateByName (symbol_name, name_type_mask, eLanguageTypeUnknown, module_list, comp_unit_list);
895 SBTarget::BreakpointCreateByName (const char *symbol_name,
896 uint32_t name_type_mask,
897 const SBFileSpecList &module_list,
898 const SBFileSpecList &comp_unit_list)
900 return BreakpointCreateByName (symbol_name, name_type_mask, eLanguageTypeUnknown, module_list, comp_unit_list);
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)
910 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
913 TargetSP target_sp(GetSP());
914 if (target_sp && symbol_name && symbol_name[0])
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(),
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()));
940 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
942 uint32_t name_type_mask,
943 const SBFileSpecList &module_list,
944 const SBFileSpecList &comp_unit_list)
946 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask, eLanguageTypeUnknown, module_list, comp_unit_list);
950 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
952 uint32_t name_type_mask,
953 LanguageType symbol_language,
954 const SBFileSpecList &module_list,
955 const SBFileSpecList &comp_unit_list)
957 return BreakpointCreateByNames(symbol_names, num_names, name_type_mask, eLanguageTypeUnknown, 0, module_list, comp_unit_list);
961 SBTarget::BreakpointCreateByNames (const char *symbol_names[],
963 uint32_t name_type_mask,
964 LanguageType symbol_language,
966 const SBFileSpecList &module_list,
967 const SBFileSpecList &comp_unit_list)
969 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
972 TargetSP target_sp(GetSP());
973 if (target_sp && num_names > 0)
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(),
993 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
994 static_cast<void*>(target_sp.get()));
995 for (uint32_t i = 0 ; i < num_names; i++)
998 if (i < num_names - 1)
1002 if (symbol_names[i] != NULL)
1003 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1005 log->Printf ("\"<NULL>\"%c ", sep);
1007 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
1008 static_cast<void*>(sb_bp.get()));
1015 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1016 const char *module_name)
1018 SBFileSpecList module_spec_list;
1019 SBFileSpecList comp_unit_list;
1020 if (module_name && module_name[0])
1022 module_spec_list.Append (FileSpec (module_name, false));
1025 return BreakpointCreateByRegex (symbol_name_regex, eLanguageTypeUnknown, module_spec_list, comp_unit_list);
1029 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1030 const SBFileSpecList &module_list,
1031 const SBFileSpecList &comp_unit_list)
1033 return BreakpointCreateByRegex (symbol_name_regex, eLanguageTypeUnknown, module_list, comp_unit_list);
1037 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1038 LanguageType symbol_language,
1039 const SBFileSpecList &module_list,
1040 const SBFileSpecList &comp_unit_list)
1042 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1045 TargetSP target_sp(GetSP());
1046 if (target_sp && symbol_name_regex && symbol_name_regex[0])
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;
1054 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, symbol_language, skip_prologue, internal, hardware);
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()));
1066 SBTarget::BreakpointCreateByAddress (addr_t address)
1068 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1071 TargetSP target_sp(GetSP());
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);
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()));
1089 SBTarget::BreakpointCreateBySBAddress (SBAddress &sb_address)
1091 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1094 TargetSP target_sp(GetSP());
1095 if (!sb_address.IsValid())
1098 log->Printf ("SBTarget(%p)::BreakpointCreateBySBAddress called with invalid address",
1099 static_cast<void*>(target_sp.get()));
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);
1113 sb_address.GetDescription(s);
1114 log->Printf ("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => SBBreakpoint(%p)",
1115 static_cast<void*>(target_sp.get()),
1117 static_cast<void*>(sb_bp.get()));
1124 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1125 const lldb::SBFileSpec &source_file,
1126 const char *module_name)
1128 SBFileSpecList module_spec_list;
1130 if (module_name && module_name[0])
1132 module_spec_list.Append (FileSpec (module_name, false));
1135 SBFileSpecList source_file_list;
1136 if (source_file.IsValid())
1138 source_file_list.Append(source_file);
1141 return BreakpointCreateBySourceRegex (source_regex, module_spec_list, source_file_list);
1146 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1147 const SBFileSpecList &module_list,
1148 const lldb::SBFileSpecList &source_file_list)
1150 return BreakpointCreateBySourceRegex(source_regex, module_list, source_file_list, SBStringList());
1154 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1155 const SBFileSpecList &module_list,
1156 const lldb::SBFileSpecList &source_file_list,
1157 const SBStringList &func_names)
1159 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1162 TargetSP target_sp(GetSP());
1163 if (target_sp && source_regex && source_regex[0])
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++)
1172 func_names_set.insert(func_names.GetStringAtIndex(i));
1175 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(),
1176 source_file_list.get(),
1181 move_to_nearest_code);
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()));
1193 SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1197 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1200 TargetSP target_sp(GetSP());
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);
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()));
1219 SBTarget::GetNumBreakpoints () const
1221 TargetSP target_sp(GetSP());
1224 // The breakpoint list is thread safe, no need to lock
1225 return target_sp->GetBreakpointList().GetSize();
1231 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1233 SBBreakpoint sb_breakpoint;
1234 TargetSP target_sp(GetSP());
1237 // The breakpoint list is thread safe, no need to lock
1238 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1240 return sb_breakpoint;
1244 SBTarget::BreakpointDelete (break_id_t bp_id)
1246 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1248 bool result = false;
1249 TargetSP target_sp(GetSP());
1252 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1253 result = target_sp->RemoveBreakpointByID (bp_id);
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);
1265 SBTarget::FindBreakpointByID (break_id_t bp_id)
1267 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1269 SBBreakpoint sb_breakpoint;
1270 TargetSP target_sp(GetSP());
1271 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1273 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1274 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
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()));
1283 return sb_breakpoint;
1287 SBTarget::EnableAllBreakpoints ()
1289 TargetSP target_sp(GetSP());
1292 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1293 target_sp->EnableAllBreakpoints ();
1300 SBTarget::DisableAllBreakpoints ()
1302 TargetSP target_sp(GetSP());
1305 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1306 target_sp->DisableAllBreakpoints ();
1313 SBTarget::DeleteAllBreakpoints ()
1315 TargetSP target_sp(GetSP());
1318 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1319 target_sp->RemoveAllBreakpoints ();
1326 SBTarget::GetNumWatchpoints () const
1328 TargetSP target_sp(GetSP());
1331 // The watchpoint list is thread safe, no need to lock
1332 return target_sp->GetWatchpointList().GetSize();
1338 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1340 SBWatchpoint sb_watchpoint;
1341 TargetSP target_sp(GetSP());
1344 // The watchpoint list is thread safe, no need to lock
1345 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1347 return sb_watchpoint;
1351 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1353 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1355 bool result = false;
1356 TargetSP target_sp(GetSP());
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);
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);
1374 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1376 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1378 SBWatchpoint sb_watchpoint;
1379 lldb::WatchpointSP watchpoint_sp;
1380 TargetSP target_sp(GetSP());
1381 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
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);
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()));
1396 return sb_watchpoint;
1400 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1402 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
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)
1409 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1410 uint32_t watch_type = 0;
1412 watch_type |= LLDB_WATCH_TYPE_READ;
1414 watch_type |= LLDB_WATCH_TYPE_WRITE;
1415 if (watch_type == 0)
1417 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1418 return sb_watchpoint;
1421 // Target::CreateWatchpoint() is thread safe.
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);
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()));
1436 return sb_watchpoint;
1440 SBTarget::EnableAllWatchpoints ()
1442 TargetSP target_sp(GetSP());
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 ();
1455 SBTarget::DisableAllWatchpoints ()
1457 TargetSP target_sp(GetSP());
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 ();
1470 SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1473 lldb::ValueObjectSP new_value_sp;
1474 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
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);
1481 sb_value.SetSP(new_value_sp);
1482 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1486 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1487 static_cast<void*>(m_opaque_sp.get()),
1488 new_value_sp->GetName().AsCString());
1490 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1491 static_cast<void*>(m_opaque_sp.get()));
1497 SBTarget::CreateValueFromData (const char *name, lldb::SBData data, lldb::SBType type)
1500 lldb::ValueObjectSP new_value_sp;
1501 if (IsValid() && name && *name && data.IsValid() && type.IsValid())
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);
1508 sb_value.SetSP(new_value_sp);
1509 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1513 log->Printf ("SBTarget(%p)::CreateValueFromData => \"%s\"",
1514 static_cast<void*>(m_opaque_sp.get()),
1515 new_value_sp->GetName().AsCString());
1517 log->Printf ("SBTarget(%p)::CreateValueFromData => NULL",
1518 static_cast<void*>(m_opaque_sp.get()));
1524 SBTarget::CreateValueFromExpression (const char *name, const char* expr)
1527 lldb::ValueObjectSP new_value_sp;
1528 if (IsValid() && name && *name && expr && *expr)
1530 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1531 new_value_sp = ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1533 sb_value.SetSP(new_value_sp);
1534 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1538 log->Printf ("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1539 static_cast<void*>(m_opaque_sp.get()),
1540 new_value_sp->GetName().AsCString());
1542 log->Printf ("SBTarget(%p)::CreateValueFromExpression => NULL",
1543 static_cast<void*>(m_opaque_sp.get()));
1549 SBTarget::DeleteAllWatchpoints ()
1551 TargetSP target_sp(GetSP());
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 ();
1565 SBTarget::AddModule (const char *path,
1567 const char *uuid_cstr)
1569 return AddModule (path, triple, uuid_cstr, NULL);
1573 SBTarget::AddModule (const char *path,
1575 const char *uuid_cstr,
1576 const char *symfile)
1578 lldb::SBModule sb_module;
1579 TargetSP target_sp(GetSP());
1582 ModuleSpec module_spec;
1584 module_spec.GetFileSpec().SetFile(path, false);
1587 module_spec.GetUUID().SetFromCString(uuid_cstr);
1590 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1592 module_spec.GetArchitecture() = target_sp->GetArchitecture();
1595 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1597 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1603 SBTarget::AddModule (const SBModuleSpec &module_spec)
1605 lldb::SBModule sb_module;
1606 TargetSP target_sp(GetSP());
1608 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1613 SBTarget::AddModule (lldb::SBModule &module)
1615 TargetSP target_sp(GetSP());
1618 target_sp->GetImages().AppendIfNeeded (module.GetSP());
1625 SBTarget::GetNumModules () const
1627 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1630 TargetSP target_sp(GetSP());
1633 // The module list is thread safe, no need to lock
1634 num = target_sp->GetImages().GetSize();
1638 log->Printf ("SBTarget(%p)::GetNumModules () => %d",
1639 static_cast<void*>(target_sp.get()), num);
1647 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1650 log->Printf ("SBTarget(%p)::Clear ()",
1651 static_cast<void*>(m_opaque_sp.get()));
1653 m_opaque_sp.reset();
1658 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1661 TargetSP target_sp(GetSP());
1662 if (target_sp && sb_file_spec.IsValid())
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));
1672 SBTarget::GetByteOrder ()
1674 TargetSP target_sp(GetSP());
1676 return target_sp->GetArchitecture().GetByteOrder();
1677 return eByteOrderInvalid;
1681 SBTarget::GetTriple ()
1683 TargetSP target_sp(GetSP());
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();
1697 SBTarget::GetDataByteSize ()
1699 TargetSP target_sp(GetSP());
1702 return target_sp->GetArchitecture().GetDataByteSize() ;
1708 SBTarget::GetCodeByteSize ()
1710 TargetSP target_sp(GetSP());
1713 return target_sp->GetArchitecture().GetCodeByteSize() ;
1719 SBTarget::GetAddressByteSize()
1721 TargetSP target_sp(GetSP());
1723 return target_sp->GetArchitecture().GetAddressByteSize();
1724 return sizeof(void*);
1729 SBTarget::GetModuleAtIndex (uint32_t idx)
1731 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1735 TargetSP target_sp(GetSP());
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);
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()));
1752 SBTarget::RemoveModule (lldb::SBModule module)
1754 TargetSP target_sp(GetSP());
1756 return target_sp->GetImages().Remove(module.GetSP());
1762 SBTarget::GetBroadcaster () const
1764 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1766 TargetSP target_sp(GetSP());
1767 SBBroadcaster broadcaster(target_sp.get(), false);
1770 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1771 static_cast<void*>(target_sp.get()),
1772 static_cast<void*>(broadcaster.get()));
1778 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1780 Stream &strm = description.ref();
1782 TargetSP target_sp(GetSP());
1785 target_sp->Dump (&strm, description_level);
1788 strm.PutCString ("No value");
1793 lldb::SBSymbolContextList
1794 SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
1796 lldb::SBSymbolContextList sb_sc_list;
1797 if (name && name[0])
1799 TargetSP target_sp(GetSP());
1802 const bool symbols_ok = true;
1803 const bool inlines_ok = true;
1804 const bool append = true;
1805 target_sp->GetImages().FindFunctions (ConstString(name),
1816 lldb::SBSymbolContextList
1817 SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
1819 lldb::SBSymbolContextList sb_sc_list;
1820 if (name && name[0])
1822 TargetSP target_sp(GetSP());
1825 std::string regexstr;
1828 case eMatchTypeRegex:
1829 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
1831 case eMatchTypeStartsWith:
1832 regexstr = llvm::Regex::escape(name) + ".*";
1833 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
1836 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
1845 SBTarget::FindFirstType (const char* typename_cstr)
1847 TargetSP target_sp(GetSP());
1848 if (typename_cstr && typename_cstr[0] && target_sp)
1850 ConstString const_typename(typename_cstr);
1852 const bool exact_match = false;
1854 const ModuleList &module_list = target_sp->GetImages();
1855 size_t count = module_list.GetSize();
1856 for (size_t idx = 0; idx < count; idx++)
1858 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
1861 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
1863 return SBType(type_sp);
1867 // Didn't find the type in the symbols; try the Objective-C runtime
1868 // if one is installed
1870 ProcessSP process_sp(target_sp->GetProcessSP());
1874 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1876 if (objc_language_runtime)
1878 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1880 if (objc_decl_vendor)
1882 std::vector <clang::NamedDecl *> decls;
1884 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1886 if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0]))
1888 return SBType(type);
1895 // No matches, search for basic typename matches
1896 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1898 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
1904 SBTarget::GetBasicType(lldb::BasicType type)
1906 TargetSP target_sp(GetSP());
1909 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1911 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
1918 SBTarget::FindTypes (const char* typename_cstr)
1920 SBTypeList sb_type_list;
1921 TargetSP target_sp(GetSP());
1922 if (typename_cstr && typename_cstr[0] && target_sp)
1924 ModuleList& images = target_sp->GetImages();
1925 ConstString const_typename(typename_cstr);
1926 bool exact_match = false;
1929 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1930 uint32_t num_matches = images.FindTypes (sc,
1934 searched_symbol_files,
1937 if (num_matches > 0)
1939 for (size_t idx = 0; idx < num_matches; idx++)
1941 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1943 sb_type_list.Append(SBType(type_sp));
1947 // Try the Objective-C runtime if one is installed
1949 ProcessSP process_sp(target_sp->GetProcessSP());
1953 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
1955 if (objc_language_runtime)
1957 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
1959 if (objc_decl_vendor)
1961 std::vector <clang::NamedDecl *> decls;
1963 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
1965 for (clang::NamedDecl *decl : decls)
1967 if (CompilerType type = ClangASTContext::GetTypeForDecl(decl))
1969 sb_type_list.Append(SBType(type));
1977 if (sb_type_list.GetSize() == 0)
1979 // No matches, search for basic typename matches
1980 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
1982 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
1985 return sb_type_list;
1989 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1991 SBValueList sb_value_list;
1993 TargetSP target_sp(GetSP());
1994 if (name && target_sp)
1996 VariableList variable_list;
1997 const bool append = true;
1998 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2003 if (match_count > 0)
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)
2010 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2012 sb_value_list.Append(SBValue(valobj_sp));
2017 return sb_value_list;
2021 SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
2023 SBValueList sb_value_list;
2025 TargetSP target_sp(GetSP());
2026 if (name && target_sp)
2028 VariableList variable_list;
2029 const bool append = true;
2031 std::string regexstr;
2032 uint32_t match_count;
2035 case eMatchTypeNormal:
2036 match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
2041 case eMatchTypeRegex:
2042 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
2047 case eMatchTypeStartsWith:
2048 regexstr = llvm::Regex::escape(name) + ".*";
2049 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
2057 if (match_count > 0)
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)
2064 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
2066 sb_value_list.Append(SBValue(valobj_sp));
2071 return sb_value_list;
2076 SBTarget::FindFirstGlobalVariable (const char* name)
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);
2085 SBTarget::GetSourceManager()
2087 SBSourceManager source_manager (*this);
2088 return source_manager;
2091 lldb::SBInstructionList
2092 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2094 return ReadInstructions (base_addr, count, NULL);
2097 lldb::SBInstructionList
2098 SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2100 SBInstructionList sb_instructions;
2102 TargetSP target_sp(GetSP());
2105 Address *addr_ptr = base_addr.get();
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,
2119 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2120 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2131 return sb_instructions;
2135 lldb::SBInstructionList
2136 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2138 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2141 lldb::SBInstructionList
2142 SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2144 SBInstructionList sb_instructions;
2146 TargetSP target_sp(GetSP());
2151 if (base_addr.get())
2152 addr = *base_addr.get();
2154 const bool data_from_file = true;
2156 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2166 return sb_instructions;
2169 lldb::SBInstructionList
2170 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2172 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2175 lldb::SBInstructionList
2176 SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2178 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2182 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2183 lldb::addr_t section_base_addr)
2186 TargetSP target_sp(GetSP());
2189 if (!section.IsValid())
2191 sb_error.SetErrorStringWithFormat ("invalid section");
2195 SectionSP section_sp (section.GetSP());
2198 if (section_sp->IsThreadSpecific())
2200 sb_error.SetErrorString ("thread specific sections are not yet supported");
2204 ProcessSP process_sp (target_sp->GetProcessSP());
2205 if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
2207 ModuleSP module_sp(section_sp->GetModule());
2210 ModuleList module_list;
2211 module_list.Append(module_sp);
2212 target_sp->ModulesDidLoad (module_list);
2214 // Flush info in the process (stack frames, etc)
2216 process_sp->Flush();
2224 sb_error.SetErrorString ("invalid target");
2230 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2234 TargetSP target_sp(GetSP());
2237 if (!section.IsValid())
2239 sb_error.SetErrorStringWithFormat ("invalid section");
2243 SectionSP section_sp (section.GetSP());
2246 ProcessSP process_sp (target_sp->GetProcessSP());
2247 if (target_sp->SetSectionUnloaded(section_sp))
2249 ModuleSP module_sp(section_sp->GetModule());
2252 ModuleList module_list;
2253 module_list.Append(module_sp);
2254 target_sp->ModulesDidUnload(module_list, false);
2256 // Flush info in the process (stack frames, etc)
2258 process_sp->Flush();
2263 sb_error.SetErrorStringWithFormat ("invalid section");
2269 sb_error.SetErrorStringWithFormat ("invalid target");
2275 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2279 TargetSP target_sp(GetSP());
2282 ModuleSP module_sp (module.GetSP());
2285 bool changed = false;
2286 if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
2288 // The load was successful, make sure that at least some sections
2289 // changed before we notify that our module was loaded.
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());
2298 process_sp->Flush();
2304 sb_error.SetErrorStringWithFormat ("invalid module");
2310 sb_error.SetErrorStringWithFormat ("invalid target");
2316 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2320 char path[PATH_MAX];
2321 TargetSP target_sp(GetSP());
2324 ModuleSP module_sp (module.GetSP());
2327 ObjectFile *objfile = module_sp->GetObjectFile();
2330 SectionList *section_list = objfile->GetSectionList();
2333 ProcessSP process_sp (target_sp->GetProcessSP());
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)
2339 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2341 changed |= target_sp->SetSectionUnloaded (section_sp);
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());
2351 process_sp->Flush();
2356 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2357 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2362 module_sp->GetFileSpec().GetPath (path, sizeof(path));
2363 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2368 sb_error.SetErrorStringWithFormat ("invalid module");
2373 sb_error.SetErrorStringWithFormat ("invalid target");
2379 lldb::SBSymbolContextList
2380 SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2382 SBSymbolContextList sb_sc_list;
2383 if (name && name[0])
2385 TargetSP target_sp(GetSP());
2389 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2400 SBTarget::EvaluateExpression (const char *expr)
2402 TargetSP target_sp(GetSP());
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);
2414 SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2416 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2417 #if !defined(LLDB_DISABLE_PYTHON)
2418 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2420 SBValue expr_result;
2421 ExpressionResults exe_results = eExpressionSetupError;
2422 ValueObjectSP expr_value_sp;
2423 TargetSP target_sp(GetSP());
2424 StackFrame *frame = NULL;
2427 if (expr == NULL || expr[0] == '\0')
2430 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2434 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2435 ExecutionContext exe_ctx (m_opaque_sp.get());
2438 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2440 frame = exe_ctx.GetFramePtr();
2441 Target *target = exe_ctx.GetTargetPtr();
2445 #ifdef LLDB_CONFIGURATION_DEBUG
2446 StreamString frame_description;
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());
2452 exe_results = target->EvaluateExpression (expr,
2457 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2458 #ifdef LLDB_CONFIGURATION_DEBUG
2459 Host::SetCrashDescription (NULL);
2465 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2468 #ifndef LLDB_DISABLE_PYTHON
2470 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2471 expr_result.GetValue(), expr_result.GetSummary());
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);
2484 SBTarget::GetStackRedZoneSize()
2486 TargetSP target_sp(GetSP());
2490 ProcessSP process_sp (target_sp->GetProcessSP());
2492 abi_sp = process_sp->GetABI();
2494 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2496 return abi_sp->GetRedZoneSize();
2502 SBTarget::GetLaunchInfo () const
2504 lldb::SBLaunchInfo launch_info(NULL);
2505 TargetSP target_sp(GetSP());
2507 launch_info.ref() = m_opaque_sp->GetProcessLaunchInfo();
2512 SBTarget::SetLaunchInfo (const lldb::SBLaunchInfo &launch_info)
2514 TargetSP target_sp(GetSP());
2516 m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());