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