1 //===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/API/SBTarget.h"
11 
12 #include "lldb/lldb-public.h"
13 
14 #include "lldb/API/SBDebugger.h"
15 #include "lldb/API/SBBreakpoint.h"
16 #include "lldb/API/SBFileSpec.h"
17 #include "lldb/API/SBListener.h"
18 #include "lldb/API/SBModule.h"
19 #include "lldb/API/SBProcess.h"
20 #include "lldb/API/SBStream.h"
21 #include "lldb/API/SBSymbolContextList.h"
22 #include "lldb/Breakpoint/BreakpointID.h"
23 #include "lldb/Breakpoint/BreakpointIDList.h"
24 #include "lldb/Breakpoint/BreakpointList.h"
25 #include "lldb/Breakpoint/BreakpointLocation.h"
26 #include "lldb/Core/Address.h"
27 #include "lldb/Core/AddressResolver.h"
28 #include "lldb/Core/AddressResolverName.h"
29 #include "lldb/Core/ArchSpec.h"
30 #include "lldb/Core/Debugger.h"
31 #include "lldb/Core/Disassembler.h"
32 #include "lldb/Core/Log.h"
33 #include "lldb/Core/RegularExpression.h"
34 #include "lldb/Core/SearchFilter.h"
35 #include "lldb/Core/STLUtils.h"
36 #include "lldb/Core/ValueObjectList.h"
37 #include "lldb/Core/ValueObjectVariable.h"
38 #include "lldb/Host/FileSpec.h"
39 #include "lldb/Host/Host.h"
40 #include "lldb/Interpreter/Args.h"
41 #include "lldb/Symbol/SymbolVendor.h"
42 #include "lldb/Symbol/VariableList.h"
43 #include "lldb/Target/Process.h"
44 #include "lldb/Target/Target.h"
45 #include "lldb/Target/TargetList.h"
46 
47 #include "lldb/Interpreter/CommandReturnObject.h"
48 #include "../source/Commands/CommandObjectBreakpoint.h"
49 
50 
51 using namespace lldb;
52 using namespace lldb_private;
53 
54 #define DEFAULT_DISASM_BYTE_SIZE 32
55 
56 //----------------------------------------------------------------------
57 // SBTarget constructor
58 //----------------------------------------------------------------------
59 SBTarget::SBTarget () :
60     m_opaque_sp ()
61 {
62 }
63 
64 SBTarget::SBTarget (const SBTarget& rhs) :
65     m_opaque_sp (rhs.m_opaque_sp)
66 {
67 }
68 
69 SBTarget::SBTarget(const TargetSP& target_sp) :
70     m_opaque_sp (target_sp)
71 {
72 }
73 
74 const SBTarget&
75 SBTarget::operator = (const SBTarget& rhs)
76 {
77     if (this != &rhs)
78         m_opaque_sp = rhs.m_opaque_sp;
79     return *this;
80 }
81 
82 //----------------------------------------------------------------------
83 // Destructor
84 //----------------------------------------------------------------------
85 SBTarget::~SBTarget()
86 {
87 }
88 
89 bool
90 SBTarget::IsValid () const
91 {
92     return m_opaque_sp.get() != NULL;
93 }
94 
95 SBProcess
96 SBTarget::GetProcess ()
97 {
98 
99     SBProcess sb_process;
100     if (m_opaque_sp)
101     {
102         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
103         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
104     }
105 
106     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
107     if (log)
108     {
109         log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
110                      m_opaque_sp.get(), sb_process.get());
111     }
112 
113     return sb_process;
114 }
115 
116 SBDebugger
117 SBTarget::GetDebugger () const
118 {
119     SBDebugger debugger;
120     if (m_opaque_sp)
121         debugger.reset (m_opaque_sp->GetDebugger().GetSP());
122     return debugger;
123 }
124 
125 SBProcess
126 SBTarget::LaunchSimple
127 (
128     char const **argv,
129     char const **envp,
130     const char *working_directory
131 )
132 {
133     char *stdin_path = NULL;
134     char *stdout_path = NULL;
135     char *stderr_path = NULL;
136     uint32_t launch_flags = 0;
137     bool stop_at_entry = false;
138     SBError error;
139     SBListener listener = GetDebugger().GetListener();
140     return Launch (listener,
141                    argv,
142                    envp,
143                    stdin_path,
144                    stdout_path,
145                    stderr_path,
146                    working_directory,
147                    launch_flags,
148                    stop_at_entry,
149                    error);
150 }
151 
152 SBProcess
153 SBTarget::Launch
154 (
155     SBListener &listener,
156     char const **argv,
157     char const **envp,
158     const char *stdin_path,
159     const char *stdout_path,
160     const char *stderr_path,
161     const char *working_directory,
162     uint32_t launch_flags,   // See LaunchFlags
163     bool stop_at_entry,
164     lldb::SBError& error
165 )
166 {
167     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
168 
169     if (log)
170     {
171         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))...",
172                      m_opaque_sp.get(),
173                      argv,
174                      envp,
175                      stdin_path ? stdin_path : "NULL",
176                      stdout_path ? stdout_path : "NULL",
177                      stderr_path ? stderr_path : "NULL",
178                      working_directory ? working_directory : "NULL",
179                      launch_flags,
180                      stop_at_entry,
181                      error.get());
182     }
183     SBProcess sb_process;
184     if (m_opaque_sp)
185     {
186         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
187 
188         if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
189             launch_flags |= eLaunchFlagDisableASLR;
190 
191         StateType state = eStateInvalid;
192         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
193         if (sb_process.IsValid())
194         {
195             state = sb_process->GetState();
196 
197             if (sb_process->IsAlive() && state != eStateConnected)
198             {
199                 if (state == eStateAttaching)
200                     error.SetErrorString ("process attach is in progress");
201                 else
202                     error.SetErrorString ("a process is already being debugged");
203                 sb_process.Clear();
204                 return sb_process;
205             }
206         }
207 
208         if (state == eStateConnected)
209         {
210             // If we are already connected, then we have already specified the
211             // listener, so if a valid listener is supplied, we need to error out
212             // to let the client know.
213             if (listener.IsValid())
214             {
215                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
216                 sb_process.Clear();
217                 return sb_process;
218             }
219         }
220         else
221         {
222             if (listener.IsValid())
223                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
224             else
225                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
226         }
227 
228         if (sb_process.IsValid())
229         {
230             if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
231                 launch_flags |= eLaunchFlagDisableSTDIO;
232 
233             error.SetError (sb_process->Launch (argv, envp, launch_flags, stdin_path, stdout_path, stderr_path, working_directory));
234             if (error.Success())
235             {
236                 // We we are stopping at the entry point, we can return now!
237                 if (stop_at_entry)
238                     return sb_process;
239 
240                 // Make sure we are stopped at the entry
241                 StateType state = sb_process->WaitForProcessToStop (NULL);
242                 if (state == eStateStopped)
243                 {
244                     // resume the process to skip the entry point
245                     error.SetError (sb_process->Resume());
246                     if (error.Success())
247                     {
248                         // If we are doing synchronous mode, then wait for the
249                         // process to stop yet again!
250                         if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
251                             sb_process->WaitForProcessToStop (NULL);
252                     }
253                 }
254             }
255         }
256         else
257         {
258             error.SetErrorString ("unable to create lldb_private::Process");
259         }
260     }
261     else
262     {
263         error.SetErrorString ("SBTarget is invalid");
264     }
265 
266     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
267     if (log)
268     {
269         log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
270                      m_opaque_sp.get(), sb_process.get());
271     }
272 
273     return sb_process;
274 }
275 
276 
277 lldb::SBProcess
278 SBTarget::AttachToProcessWithID
279 (
280     SBListener &listener,
281     lldb::pid_t pid,// The process ID to attach to
282     SBError& error  // An error explaining what went wrong if attach fails
283 )
284 {
285     SBProcess sb_process;
286     if (m_opaque_sp)
287     {
288         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
289 
290         StateType state = eStateInvalid;
291         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
292         if (sb_process.IsValid())
293         {
294             state = sb_process->GetState();
295 
296             if (sb_process->IsAlive() && state != eStateConnected)
297             {
298                 if (state == eStateAttaching)
299                     error.SetErrorString ("process attach is in progress");
300                 else
301                     error.SetErrorString ("a process is already being debugged");
302                 sb_process.Clear();
303                 return sb_process;
304             }
305         }
306 
307         if (state == eStateConnected)
308         {
309             // If we are already connected, then we have already specified the
310             // listener, so if a valid listener is supplied, we need to error out
311             // to let the client know.
312             if (listener.IsValid())
313             {
314                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
315                 sb_process.Clear();
316                 return sb_process;
317             }
318         }
319         else
320         {
321             if (listener.IsValid())
322                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
323             else
324                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
325         }
326 
327         if (sb_process.IsValid())
328         {
329             error.SetError (sb_process->Attach (pid));
330             // If we are doing synchronous mode, then wait for the
331             // process to stop!
332             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
333                 sb_process->WaitForProcessToStop (NULL);
334         }
335         else
336         {
337             error.SetErrorString ("unable to create lldb_private::Process");
338         }
339     }
340     else
341     {
342         error.SetErrorString ("SBTarget is invalid");
343     }
344     return sb_process;
345 
346 }
347 
348 lldb::SBProcess
349 SBTarget::AttachToProcessWithName
350 (
351     SBListener &listener,
352     const char *name,   // basename of process to attach to
353     bool wait_for,      // if true wait for a new instance of "name" to be launched
354     SBError& error      // An error explaining what went wrong if attach fails
355 )
356 {
357     SBProcess sb_process;
358     if (m_opaque_sp)
359     {
360         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
361 
362         StateType state = eStateInvalid;
363         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
364         if (sb_process.IsValid())
365         {
366             state = sb_process->GetState();
367 
368             if (sb_process->IsAlive() && state != eStateConnected)
369             {
370                 if (state == eStateAttaching)
371                     error.SetErrorString ("process attach is in progress");
372                 else
373                     error.SetErrorString ("a process is already being debugged");
374                 sb_process.Clear();
375                 return sb_process;
376             }
377         }
378 
379         if (state == eStateConnected)
380         {
381             // If we are already connected, then we have already specified the
382             // listener, so if a valid listener is supplied, we need to error out
383             // to let the client know.
384             if (listener.IsValid())
385             {
386                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
387                 sb_process.Clear();
388                 return sb_process;
389             }
390         }
391         else
392         {
393             if (listener.IsValid())
394                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
395             else
396                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
397         }
398 
399         if (sb_process.IsValid())
400         {
401             error.SetError (sb_process->Attach (name, wait_for));
402             // If we are doing synchronous mode, then wait for the
403             // process to stop!
404             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
405                 sb_process->WaitForProcessToStop (NULL);
406         }
407         else
408         {
409             error.SetErrorString ("unable to create lldb_private::Process");
410         }
411     }
412     else
413     {
414         error.SetErrorString ("SBTarget is invalid");
415     }
416     return sb_process;
417 
418 }
419 
420 lldb::SBProcess
421 SBTarget::ConnectRemote
422 (
423     SBListener &listener,
424     const char *url,
425     const char *plugin_name,
426     SBError& error
427 )
428 {
429     SBProcess sb_process;
430     if (m_opaque_sp)
431     {
432         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
433         if (listener.IsValid())
434             sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref(), plugin_name));
435         else
436             sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener(), plugin_name));
437 
438 
439         if (sb_process.IsValid())
440         {
441             error.SetError (sb_process->ConnectRemote (url));
442         }
443         else
444         {
445             error.SetErrorString ("unable to create lldb_private::Process");
446         }
447     }
448     else
449     {
450         error.SetErrorString ("SBTarget is invalid");
451     }
452     return sb_process;
453 }
454 
455 SBFileSpec
456 SBTarget::GetExecutable ()
457 {
458 
459     SBFileSpec exe_file_spec;
460     if (m_opaque_sp)
461     {
462         Module *exe_module = m_opaque_sp->GetExecutableModulePointer();
463         if (exe_module)
464             exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
465     }
466 
467     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
468     if (log)
469     {
470         log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
471                      m_opaque_sp.get(), exe_file_spec.get());
472     }
473 
474     return exe_file_spec;
475 }
476 
477 bool
478 SBTarget::operator == (const SBTarget &rhs) const
479 {
480     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
481 }
482 
483 bool
484 SBTarget::operator != (const SBTarget &rhs) const
485 {
486     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
487 }
488 
489 lldb_private::Target *
490 SBTarget::operator ->() const
491 {
492     return m_opaque_sp.get();
493 }
494 
495 lldb_private::Target *
496 SBTarget::get() const
497 {
498     return m_opaque_sp.get();
499 }
500 
501 void
502 SBTarget::reset (const lldb::TargetSP& target_sp)
503 {
504     m_opaque_sp = target_sp;
505 }
506 
507 lldb::SBAddress
508 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
509 {
510     lldb::SBAddress sb_addr;
511     Address &addr = sb_addr.ref();
512     if (m_opaque_sp)
513     {
514         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
515         if (m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
516             return sb_addr;
517     }
518 
519     // We have a load address that isn't in a section, just return an address
520     // with the offset filled in (the address) and the section set to NULL
521     addr.SetSection(NULL);
522     addr.SetOffset(vm_addr);
523     return sb_addr;
524 }
525 
526 SBSymbolContext
527 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
528 {
529     SBSymbolContext sc;
530     if (m_opaque_sp && addr.IsValid())
531         m_opaque_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
532     return sc;
533 }
534 
535 
536 SBBreakpoint
537 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
538 {
539     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
540 }
541 
542 SBBreakpoint
543 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
544 {
545     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
546 
547     SBBreakpoint sb_bp;
548     if (m_opaque_sp.get() && line != 0)
549     {
550         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
551         *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
552     }
553 
554     if (log)
555     {
556         SBStream sstr;
557         sb_bp.GetDescription (sstr);
558         char path[PATH_MAX];
559         sb_file_spec->GetPath (path, sizeof(path));
560         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
561                      m_opaque_sp.get(),
562                      path,
563                      line,
564                      sb_bp.get(),
565                      sstr.GetData());
566     }
567 
568     return sb_bp;
569 }
570 
571 SBBreakpoint
572 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
573 {
574     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
575 
576     SBBreakpoint sb_bp;
577     if (m_opaque_sp.get() && symbol_name && symbol_name[0])
578     {
579         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
580         if (module_name && module_name[0])
581         {
582             FileSpec module_file_spec(module_name, false);
583             *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
584         }
585         else
586         {
587             *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
588         }
589     }
590 
591     if (log)
592     {
593         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
594                      m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
595     }
596 
597     return sb_bp;
598 }
599 
600 SBBreakpoint
601 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
602 {
603     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
604 
605     SBBreakpoint sb_bp;
606     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
607     {
608         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
609         RegularExpression regexp(symbol_name_regex);
610 
611         if (module_name && module_name[0])
612         {
613             FileSpec module_file_spec(module_name, false);
614 
615             *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, regexp, false);
616         }
617         else
618         {
619             *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, regexp, false);
620         }
621     }
622 
623     if (log)
624     {
625         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
626                      m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
627     }
628 
629     return sb_bp;
630 }
631 
632 
633 
634 SBBreakpoint
635 SBTarget::BreakpointCreateByAddress (addr_t address)
636 {
637     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
638 
639     SBBreakpoint sb_bp;
640     if (m_opaque_sp.get())
641     {
642         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
643         *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
644     }
645 
646     if (log)
647     {
648         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (%p, address=%p) => SBBreakpoint(%p)", m_opaque_sp.get(), address, sb_bp.get());
649     }
650 
651     return sb_bp;
652 }
653 
654 SBBreakpoint
655 SBTarget::FindBreakpointByID (break_id_t bp_id)
656 {
657     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
658 
659     SBBreakpoint sb_breakpoint;
660     if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
661     {
662         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
663         *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
664     }
665 
666     if (log)
667     {
668         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
669                      m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
670     }
671 
672     return sb_breakpoint;
673 }
674 
675 uint32_t
676 SBTarget::GetNumBreakpoints () const
677 {
678     if (m_opaque_sp)
679     {
680         // The breakpoint list is thread safe, no need to lock
681         return m_opaque_sp->GetBreakpointList().GetSize();
682     }
683     return 0;
684 }
685 
686 SBBreakpoint
687 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
688 {
689     SBBreakpoint sb_breakpoint;
690     if (m_opaque_sp)
691     {
692         // The breakpoint list is thread safe, no need to lock
693         *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
694     }
695     return sb_breakpoint;
696 }
697 
698 bool
699 SBTarget::BreakpointDelete (break_id_t bp_id)
700 {
701     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
702 
703     bool result = false;
704     if (m_opaque_sp)
705     {
706         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
707         result = m_opaque_sp->RemoveBreakpointByID (bp_id);
708     }
709 
710     if (log)
711     {
712         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) bp_id, result);
713     }
714 
715     return result;
716 }
717 
718 bool
719 SBTarget::EnableAllBreakpoints ()
720 {
721     if (m_opaque_sp)
722     {
723         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
724         m_opaque_sp->EnableAllBreakpoints ();
725         return true;
726     }
727     return false;
728 }
729 
730 bool
731 SBTarget::DisableAllBreakpoints ()
732 {
733     if (m_opaque_sp)
734     {
735         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
736         m_opaque_sp->DisableAllBreakpoints ();
737         return true;
738     }
739     return false;
740 }
741 
742 bool
743 SBTarget::DeleteAllBreakpoints ()
744 {
745     if (m_opaque_sp)
746     {
747         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
748         m_opaque_sp->RemoveAllBreakpoints ();
749         return true;
750     }
751     return false;
752 }
753 
754 
755 uint32_t
756 SBTarget::GetNumModules () const
757 {
758     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
759 
760     uint32_t num = 0;
761     if (m_opaque_sp)
762     {
763         // The module list is thread safe, no need to lock
764         num = m_opaque_sp->GetImages().GetSize();
765     }
766 
767     if (log)
768         log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
769 
770     return num;
771 }
772 
773 void
774 SBTarget::Clear ()
775 {
776     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
777 
778     if (log)
779         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
780 
781     m_opaque_sp.reset();
782 }
783 
784 
785 SBModule
786 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
787 {
788     SBModule sb_module;
789     if (m_opaque_sp && sb_file_spec.IsValid())
790     {
791         // The module list is thread safe, no need to lock
792         sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL, NULL));
793     }
794     return sb_module;
795 }
796 
797 SBModule
798 SBTarget::GetModuleAtIndex (uint32_t idx)
799 {
800     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
801 
802     SBModule sb_module;
803     if (m_opaque_sp)
804     {
805         // The module list is thread safe, no need to lock
806         sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
807     }
808 
809     if (log)
810     {
811         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
812                      m_opaque_sp.get(), idx, sb_module.get());
813     }
814 
815     return sb_module;
816 }
817 
818 
819 SBBroadcaster
820 SBTarget::GetBroadcaster () const
821 {
822     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
823 
824     SBBroadcaster broadcaster(m_opaque_sp.get(), false);
825 
826     if (log)
827         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
828                      m_opaque_sp.get(), broadcaster.get());
829 
830     return broadcaster;
831 }
832 
833 
834 bool
835 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
836 {
837     if (m_opaque_sp)
838     {
839         description.ref();
840         m_opaque_sp->Dump (description.get(), description_level);
841     }
842     else
843         description.Printf ("No value");
844 
845     return true;
846 }
847 
848 bool
849 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const
850 {
851     if (m_opaque_sp)
852     {
853         description.ref();
854         m_opaque_sp->Dump (description.get(), description_level);
855     }
856     else
857         description.Printf ("No value");
858 
859     return true;
860 }
861 
862 
863 uint32_t
864 SBTarget::FindFunctions (const char *name,
865                          uint32_t name_type_mask,
866                          bool append,
867                          lldb::SBSymbolContextList& sc_list)
868 {
869     if (!append)
870         sc_list.Clear();
871     if (m_opaque_sp)
872     {
873         const bool symbols_ok = true;
874         return m_opaque_sp->GetImages().FindFunctions (ConstString(name),
875                                                        name_type_mask,
876                                                        symbols_ok,
877                                                        append,
878                                                        *sc_list);
879     }
880     return 0;
881 }
882 
883 lldb::SBType
884 SBTarget::FindFirstType (const char* type)
885 {
886     if (m_opaque_sp)
887     {
888         size_t count = m_opaque_sp->GetImages().GetSize();
889         for (size_t idx = 0; idx < count; idx++)
890         {
891             SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
892 
893             if (found_at_idx.IsValid())
894                 return found_at_idx;
895         }
896     }
897     return SBType();
898 }
899 
900 lldb::SBTypeList
901 SBTarget::FindTypes (const char* type)
902 {
903 
904     SBTypeList retval;
905 
906     if (m_opaque_sp)
907     {
908         ModuleList& images = m_opaque_sp->GetImages();
909         ConstString name_const(type);
910         SymbolContext sc;
911         TypeList type_list;
912 
913         uint32_t num_matches = images.FindTypes(sc,
914                                                 name_const,
915                                                 true,
916                                                 UINT32_MAX,
917                                                 type_list);
918 
919         for (size_t idx = 0; idx < num_matches; idx++)
920         {
921             TypeSP type_sp (type_list.GetTypeAtIndex(idx));
922             if (type_sp)
923                 retval.Append(SBType(type_sp));
924         }
925     }
926     return retval;
927 }
928 
929 SBValueList
930 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
931 {
932     SBValueList sb_value_list;
933 
934     if (m_opaque_sp)
935     {
936         VariableList variable_list;
937         const bool append = true;
938         const uint32_t match_count = m_opaque_sp->GetImages().FindGlobalVariables (ConstString (name),
939                                                                                    append,
940                                                                                    max_matches,
941                                                                                    variable_list);
942 
943         if (match_count > 0)
944         {
945             ExecutionContextScope *exe_scope = m_opaque_sp->GetProcessSP().get();
946             if (exe_scope == NULL)
947                 exe_scope = m_opaque_sp.get();
948             ValueObjectList &value_object_list = sb_value_list.ref();
949             for (uint32_t i=0; i<match_count; ++i)
950             {
951                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
952                 if (valobj_sp)
953                     value_object_list.Append(valobj_sp);
954             }
955         }
956     }
957 
958     return sb_value_list;
959 }
960 
961