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/SBSourceManager.h"
20 #include "lldb/API/SBProcess.h"
21 #include "lldb/API/SBStream.h"
22 #include "lldb/API/SBSymbolContextList.h"
23 #include "lldb/Breakpoint/BreakpointID.h"
24 #include "lldb/Breakpoint/BreakpointIDList.h"
25 #include "lldb/Breakpoint/BreakpointList.h"
26 #include "lldb/Breakpoint/BreakpointLocation.h"
27 #include "lldb/Core/Address.h"
28 #include "lldb/Core/AddressResolver.h"
29 #include "lldb/Core/AddressResolverName.h"
30 #include "lldb/Core/ArchSpec.h"
31 #include "lldb/Core/Debugger.h"
32 #include "lldb/Core/Disassembler.h"
33 #include "lldb/Core/Log.h"
34 #include "lldb/Core/RegularExpression.h"
35 #include "lldb/Core/SearchFilter.h"
36 #include "lldb/Core/STLUtils.h"
37 #include "lldb/Core/ValueObjectList.h"
38 #include "lldb/Core/ValueObjectVariable.h"
39 #include "lldb/Host/FileSpec.h"
40 #include "lldb/Host/Host.h"
41 #include "lldb/Interpreter/Args.h"
42 #include "lldb/Symbol/SymbolVendor.h"
43 #include "lldb/Symbol/VariableList.h"
44 #include "lldb/Target/Process.h"
45 #include "lldb/Target/Target.h"
46 #include "lldb/Target/TargetList.h"
47 
48 #include "lldb/Interpreter/CommandReturnObject.h"
49 #include "../source/Commands/CommandObjectBreakpoint.h"
50 
51 
52 using namespace lldb;
53 using namespace lldb_private;
54 
55 #define DEFAULT_DISASM_BYTE_SIZE 32
56 
57 //----------------------------------------------------------------------
58 // SBTarget constructor
59 //----------------------------------------------------------------------
60 SBTarget::SBTarget () :
61     m_opaque_sp ()
62 {
63 }
64 
65 SBTarget::SBTarget (const SBTarget& rhs) :
66     m_opaque_sp (rhs.m_opaque_sp)
67 {
68 }
69 
70 SBTarget::SBTarget(const TargetSP& target_sp) :
71     m_opaque_sp (target_sp)
72 {
73 }
74 
75 const SBTarget&
76 SBTarget::operator = (const SBTarget& rhs)
77 {
78     if (this != &rhs)
79         m_opaque_sp = rhs.m_opaque_sp;
80     return *this;
81 }
82 
83 //----------------------------------------------------------------------
84 // Destructor
85 //----------------------------------------------------------------------
86 SBTarget::~SBTarget()
87 {
88 }
89 
90 bool
91 SBTarget::IsValid () const
92 {
93     return m_opaque_sp.get() != NULL;
94 }
95 
96 SBProcess
97 SBTarget::GetProcess ()
98 {
99 
100     SBProcess sb_process;
101     if (m_opaque_sp)
102     {
103         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
104         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
105     }
106 
107     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
108     if (log)
109     {
110         log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
111                      m_opaque_sp.get(), sb_process.get());
112     }
113 
114     return sb_process;
115 }
116 
117 SBDebugger
118 SBTarget::GetDebugger () const
119 {
120     SBDebugger debugger;
121     if (m_opaque_sp)
122         debugger.reset (m_opaque_sp->GetDebugger().GetSP());
123     return debugger;
124 }
125 
126 SBProcess
127 SBTarget::LaunchSimple
128 (
129     char const **argv,
130     char const **envp,
131     const char *working_directory
132 )
133 {
134     char *stdin_path = NULL;
135     char *stdout_path = NULL;
136     char *stderr_path = NULL;
137     uint32_t launch_flags = 0;
138     bool stop_at_entry = false;
139     SBError error;
140     SBListener listener = GetDebugger().GetListener();
141     return Launch (listener,
142                    argv,
143                    envp,
144                    stdin_path,
145                    stdout_path,
146                    stderr_path,
147                    working_directory,
148                    launch_flags,
149                    stop_at_entry,
150                    error);
151 }
152 
153 SBProcess
154 SBTarget::Launch
155 (
156     SBListener &listener,
157     char const **argv,
158     char const **envp,
159     const char *stdin_path,
160     const char *stdout_path,
161     const char *stderr_path,
162     const char *working_directory,
163     uint32_t launch_flags,   // See LaunchFlags
164     bool stop_at_entry,
165     lldb::SBError& error
166 )
167 {
168     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
169 
170     if (log)
171     {
172         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))...",
173                      m_opaque_sp.get(),
174                      argv,
175                      envp,
176                      stdin_path ? stdin_path : "NULL",
177                      stdout_path ? stdout_path : "NULL",
178                      stderr_path ? stderr_path : "NULL",
179                      working_directory ? working_directory : "NULL",
180                      launch_flags,
181                      stop_at_entry,
182                      error.get());
183     }
184     SBProcess sb_process;
185     if (m_opaque_sp)
186     {
187         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
188 
189         if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
190             launch_flags |= eLaunchFlagDisableASLR;
191 
192         StateType state = eStateInvalid;
193         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
194         if (sb_process.IsValid())
195         {
196             state = sb_process->GetState();
197 
198             if (sb_process->IsAlive() && state != eStateConnected)
199             {
200                 if (state == eStateAttaching)
201                     error.SetErrorString ("process attach is in progress");
202                 else
203                     error.SetErrorString ("a process is already being debugged");
204                 sb_process.Clear();
205                 return sb_process;
206             }
207         }
208 
209         if (state == eStateConnected)
210         {
211             // If we are already connected, then we have already specified the
212             // listener, so if a valid listener is supplied, we need to error out
213             // to let the client know.
214             if (listener.IsValid())
215             {
216                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
217                 sb_process.Clear();
218                 return sb_process;
219             }
220         }
221         else
222         {
223             if (listener.IsValid())
224                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
225             else
226                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
227         }
228 
229         if (sb_process.IsValid())
230         {
231             if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
232                 launch_flags |= eLaunchFlagDisableSTDIO;
233 
234             ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
235 
236             Module *exe_module = m_opaque_sp->GetExecutableModulePointer();
237             if (exe_module)
238                 launch_info.SetExecutableFile(exe_module->GetFileSpec(), true);
239             if (argv)
240                 launch_info.GetArguments().AppendArguments (argv);
241             if (envp)
242                 launch_info.GetEnvironmentEntries ().SetArguments (envp);
243 
244             error.SetError (sb_process->Launch (launch_info));
245             if (error.Success())
246             {
247                 // We we are stopping at the entry point, we can return now!
248                 if (stop_at_entry)
249                     return sb_process;
250 
251                 // Make sure we are stopped at the entry
252                 StateType state = sb_process->WaitForProcessToStop (NULL);
253                 if (state == eStateStopped)
254                 {
255                     // resume the process to skip the entry point
256                     error.SetError (sb_process->Resume());
257                     if (error.Success())
258                     {
259                         // If we are doing synchronous mode, then wait for the
260                         // process to stop yet again!
261                         if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
262                             sb_process->WaitForProcessToStop (NULL);
263                     }
264                 }
265             }
266         }
267         else
268         {
269             error.SetErrorString ("unable to create lldb_private::Process");
270         }
271     }
272     else
273     {
274         error.SetErrorString ("SBTarget is invalid");
275     }
276 
277     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
278     if (log)
279     {
280         log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
281                      m_opaque_sp.get(), sb_process.get());
282     }
283 
284     return sb_process;
285 }
286 
287 
288 lldb::SBProcess
289 SBTarget::AttachToProcessWithID
290 (
291     SBListener &listener,
292     lldb::pid_t pid,// The process ID to attach to
293     SBError& error  // An error explaining what went wrong if attach fails
294 )
295 {
296     SBProcess sb_process;
297     if (m_opaque_sp)
298     {
299         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
300 
301         StateType state = eStateInvalid;
302         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
303         if (sb_process.IsValid())
304         {
305             state = sb_process->GetState();
306 
307             if (sb_process->IsAlive() && state != eStateConnected)
308             {
309                 if (state == eStateAttaching)
310                     error.SetErrorString ("process attach is in progress");
311                 else
312                     error.SetErrorString ("a process is already being debugged");
313                 sb_process.Clear();
314                 return sb_process;
315             }
316         }
317 
318         if (state == eStateConnected)
319         {
320             // If we are already connected, then we have already specified the
321             // listener, so if a valid listener is supplied, we need to error out
322             // to let the client know.
323             if (listener.IsValid())
324             {
325                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
326                 sb_process.Clear();
327                 return sb_process;
328             }
329         }
330         else
331         {
332             if (listener.IsValid())
333                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
334             else
335                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
336         }
337 
338         if (sb_process.IsValid())
339         {
340             ProcessAttachInfo attach_info;
341             attach_info.SetProcessID (pid);
342             error.SetError (sb_process->Attach (attach_info));
343             // If we are doing synchronous mode, then wait for the
344             // process to stop!
345             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
346                 sb_process->WaitForProcessToStop (NULL);
347         }
348         else
349         {
350             error.SetErrorString ("unable to create lldb_private::Process");
351         }
352     }
353     else
354     {
355         error.SetErrorString ("SBTarget is invalid");
356     }
357     return sb_process;
358 
359 }
360 
361 lldb::SBProcess
362 SBTarget::AttachToProcessWithName
363 (
364     SBListener &listener,
365     const char *name,   // basename of process to attach to
366     bool wait_for,      // if true wait for a new instance of "name" to be launched
367     SBError& error      // An error explaining what went wrong if attach fails
368 )
369 {
370     SBProcess sb_process;
371     if (m_opaque_sp)
372     {
373         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
374 
375         StateType state = eStateInvalid;
376         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
377         if (sb_process.IsValid())
378         {
379             state = sb_process->GetState();
380 
381             if (sb_process->IsAlive() && state != eStateConnected)
382             {
383                 if (state == eStateAttaching)
384                     error.SetErrorString ("process attach is in progress");
385                 else
386                     error.SetErrorString ("a process is already being debugged");
387                 sb_process.Clear();
388                 return sb_process;
389             }
390         }
391 
392         if (state == eStateConnected)
393         {
394             // If we are already connected, then we have already specified the
395             // listener, so if a valid listener is supplied, we need to error out
396             // to let the client know.
397             if (listener.IsValid())
398             {
399                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
400                 sb_process.Clear();
401                 return sb_process;
402             }
403         }
404         else
405         {
406             if (listener.IsValid())
407                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
408             else
409                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
410         }
411 
412         if (sb_process.IsValid())
413         {
414             ProcessAttachInfo attach_info;
415             attach_info.GetExecutableFile().SetFile(name, false);
416             attach_info.SetWaitForLaunch(wait_for);
417             error.SetError (sb_process->Attach (attach_info));
418             // If we are doing synchronous mode, then wait for the
419             // process to stop!
420             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
421                 sb_process->WaitForProcessToStop (NULL);
422         }
423         else
424         {
425             error.SetErrorString ("unable to create lldb_private::Process");
426         }
427     }
428     else
429     {
430         error.SetErrorString ("SBTarget is invalid");
431     }
432     return sb_process;
433 
434 }
435 
436 lldb::SBProcess
437 SBTarget::ConnectRemote
438 (
439     SBListener &listener,
440     const char *url,
441     const char *plugin_name,
442     SBError& error
443 )
444 {
445     SBProcess sb_process;
446     if (m_opaque_sp)
447     {
448         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
449         if (listener.IsValid())
450             sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref(), plugin_name));
451         else
452             sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener(), plugin_name));
453 
454 
455         if (sb_process.IsValid())
456         {
457             error.SetError (sb_process->ConnectRemote (url));
458         }
459         else
460         {
461             error.SetErrorString ("unable to create lldb_private::Process");
462         }
463     }
464     else
465     {
466         error.SetErrorString ("SBTarget is invalid");
467     }
468     return sb_process;
469 }
470 
471 SBFileSpec
472 SBTarget::GetExecutable ()
473 {
474 
475     SBFileSpec exe_file_spec;
476     if (m_opaque_sp)
477     {
478         Module *exe_module = m_opaque_sp->GetExecutableModulePointer();
479         if (exe_module)
480             exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
481     }
482 
483     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
484     if (log)
485     {
486         log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
487                      m_opaque_sp.get(), exe_file_spec.get());
488     }
489 
490     return exe_file_spec;
491 }
492 
493 bool
494 SBTarget::operator == (const SBTarget &rhs) const
495 {
496     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
497 }
498 
499 bool
500 SBTarget::operator != (const SBTarget &rhs) const
501 {
502     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
503 }
504 
505 lldb_private::Target *
506 SBTarget::operator ->() const
507 {
508     return m_opaque_sp.get();
509 }
510 
511 lldb_private::Target *
512 SBTarget::get() const
513 {
514     return m_opaque_sp.get();
515 }
516 
517 const lldb::TargetSP &
518 SBTarget::get_sp () const
519 {
520     return m_opaque_sp;
521 }
522 
523 void
524 SBTarget::reset (const lldb::TargetSP& target_sp)
525 {
526     m_opaque_sp = target_sp;
527 }
528 
529 lldb::SBAddress
530 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
531 {
532     lldb::SBAddress sb_addr;
533     Address &addr = sb_addr.ref();
534     if (m_opaque_sp)
535     {
536         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
537         if (m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
538             return sb_addr;
539     }
540 
541     // We have a load address that isn't in a section, just return an address
542     // with the offset filled in (the address) and the section set to NULL
543     addr.SetSection(NULL);
544     addr.SetOffset(vm_addr);
545     return sb_addr;
546 }
547 
548 SBSymbolContext
549 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
550 {
551     SBSymbolContext sc;
552     if (m_opaque_sp && addr.IsValid())
553         m_opaque_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
554     return sc;
555 }
556 
557 
558 SBBreakpoint
559 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
560 {
561     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
562 }
563 
564 SBBreakpoint
565 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
566 {
567     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
568 
569     SBBreakpoint sb_bp;
570     if (m_opaque_sp.get() && line != 0)
571     {
572         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
573         *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
574     }
575 
576     if (log)
577     {
578         SBStream sstr;
579         sb_bp.GetDescription (sstr);
580         char path[PATH_MAX];
581         sb_file_spec->GetPath (path, sizeof(path));
582         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
583                      m_opaque_sp.get(),
584                      path,
585                      line,
586                      sb_bp.get(),
587                      sstr.GetData());
588     }
589 
590     return sb_bp;
591 }
592 
593 SBBreakpoint
594 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
595 {
596     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
597 
598     SBBreakpoint sb_bp;
599     if (m_opaque_sp.get())
600     {
601         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
602         if (module_name && module_name[0])
603         {
604             FileSpecList module_spec_list;
605             module_spec_list.Append (FileSpec (module_name, false));
606             *sb_bp = m_opaque_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, false);
607         }
608         else
609         {
610             *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, false);
611         }
612     }
613 
614     if (log)
615     {
616         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
617                      m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
618     }
619 
620     return sb_bp;
621 }
622 
623 lldb::SBBreakpoint
624 SBTarget::BreakpointCreateByName (const char *symbol_name,
625                             const SBFileSpecList &module_list,
626                             const SBFileSpecList &comp_unit_list)
627 {
628     uint32_t name_type_mask = eFunctionNameTypeAuto;
629     return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
630 }
631 
632 lldb::SBBreakpoint
633 SBTarget::BreakpointCreateByName (const char *symbol_name,
634                             uint32_t name_type_mask,
635                             const SBFileSpecList &module_list,
636                             const SBFileSpecList &comp_unit_list)
637 {
638     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
639 
640     SBBreakpoint sb_bp;
641     if (m_opaque_sp.get() && symbol_name && symbol_name[0])
642     {
643         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
644         *sb_bp = m_opaque_sp->CreateBreakpoint (module_list.get(),
645                                                 comp_unit_list.get(),
646                                                 symbol_name,
647                                                 name_type_mask,
648                                                 false);
649     }
650 
651     if (log)
652     {
653         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
654                      m_opaque_sp.get(), symbol_name, name_type_mask, sb_bp.get());
655     }
656 
657     return sb_bp;
658 }
659 
660 
661 SBBreakpoint
662 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
663 {
664     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
665 
666     SBBreakpoint sb_bp;
667     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
668     {
669         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
670         RegularExpression regexp(symbol_name_regex);
671 
672         if (module_name && module_name[0])
673         {
674             FileSpecList module_spec_list;
675             module_spec_list.Append (FileSpec (module_name, false));
676 
677             *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, false);
678         }
679         else
680         {
681             *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, false);
682         }
683     }
684 
685     if (log)
686     {
687         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
688                      m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
689     }
690 
691     return sb_bp;
692 }
693 
694 lldb::SBBreakpoint
695 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
696                          const SBFileSpecList &module_list,
697                          const SBFileSpecList &comp_unit_list)
698 {
699     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
700 
701     SBBreakpoint sb_bp;
702     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
703     {
704         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
705         RegularExpression regexp(symbol_name_regex);
706 
707         *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, false);
708     }
709 
710     if (log)
711     {
712         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
713                      m_opaque_sp.get(), symbol_name_regex, sb_bp.get());
714     }
715 
716     return sb_bp;
717 }
718 
719 SBBreakpoint
720 SBTarget::BreakpointCreateByAddress (addr_t address)
721 {
722     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
723 
724     SBBreakpoint sb_bp;
725     if (m_opaque_sp.get())
726     {
727         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
728         *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
729     }
730 
731     if (log)
732     {
733         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%llu) => SBBreakpoint(%p)", m_opaque_sp.get(), (uint64_t) address, sb_bp.get());
734     }
735 
736     return sb_bp;
737 }
738 
739 lldb::SBBreakpoint
740 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
741 {
742     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
743 
744     SBBreakpoint sb_bp;
745     if (m_opaque_sp.get() && source_regex && source_regex[0])
746     {
747         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
748         RegularExpression regexp(source_regex);
749         FileSpecList source_file_spec_list;
750         source_file_spec_list.Append (source_file.ref());
751 
752         if (module_name && module_name[0])
753         {
754             FileSpecList module_spec_list;
755             module_spec_list.Append (FileSpec (module_name, false));
756 
757             *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
758         }
759         else
760         {
761             *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
762         }
763     }
764 
765     if (log)
766     {
767         char path[PATH_MAX];
768         source_file->GetPath (path, sizeof(path));
769         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
770                      m_opaque_sp.get(), source_regex, path, module_name, sb_bp.get());
771     }
772 
773     return sb_bp;
774 }
775 
776 lldb::SBBreakpoint
777 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
778                                const SBFileSpecList &module_list,
779                                const lldb::SBFileSpecList &source_file_list)
780 {
781     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
782 
783     SBBreakpoint sb_bp;
784     if (m_opaque_sp.get() && source_regex && source_regex[0])
785     {
786         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
787         RegularExpression regexp(source_regex);
788         *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
789     }
790 
791     if (log)
792     {
793         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
794                      m_opaque_sp.get(), source_regex, sb_bp.get());
795     }
796 
797     return sb_bp;
798 }
799 
800 uint32_t
801 SBTarget::GetNumBreakpoints () const
802 {
803     if (m_opaque_sp)
804     {
805         // The breakpoint list is thread safe, no need to lock
806         return m_opaque_sp->GetBreakpointList().GetSize();
807     }
808     return 0;
809 }
810 
811 SBBreakpoint
812 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
813 {
814     SBBreakpoint sb_breakpoint;
815     if (m_opaque_sp)
816     {
817         // The breakpoint list is thread safe, no need to lock
818         *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
819     }
820     return sb_breakpoint;
821 }
822 
823 bool
824 SBTarget::BreakpointDelete (break_id_t bp_id)
825 {
826     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
827 
828     bool result = false;
829     if (m_opaque_sp)
830     {
831         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
832         result = m_opaque_sp->RemoveBreakpointByID (bp_id);
833     }
834 
835     if (log)
836     {
837         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) bp_id, result);
838     }
839 
840     return result;
841 }
842 
843 SBBreakpoint
844 SBTarget::FindBreakpointByID (break_id_t bp_id)
845 {
846     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
847 
848     SBBreakpoint sb_breakpoint;
849     if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
850     {
851         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
852         *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
853     }
854 
855     if (log)
856     {
857         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
858                      m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
859     }
860 
861     return sb_breakpoint;
862 }
863 
864 bool
865 SBTarget::EnableAllBreakpoints ()
866 {
867     if (m_opaque_sp)
868     {
869         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
870         m_opaque_sp->EnableAllBreakpoints ();
871         return true;
872     }
873     return false;
874 }
875 
876 bool
877 SBTarget::DisableAllBreakpoints ()
878 {
879     if (m_opaque_sp)
880     {
881         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
882         m_opaque_sp->DisableAllBreakpoints ();
883         return true;
884     }
885     return false;
886 }
887 
888 bool
889 SBTarget::DeleteAllBreakpoints ()
890 {
891     if (m_opaque_sp)
892     {
893         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
894         m_opaque_sp->RemoveAllBreakpoints ();
895         return true;
896     }
897     return false;
898 }
899 
900 uint32_t
901 SBTarget::GetNumWatchpoints () const
902 {
903     if (m_opaque_sp)
904     {
905         // The watchpoint list is thread safe, no need to lock
906         return m_opaque_sp->GetWatchpointList().GetSize();
907     }
908     return 0;
909 }
910 
911 SBWatchpoint
912 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
913 {
914     SBWatchpoint sb_watchpoint;
915     if (m_opaque_sp)
916     {
917         // The watchpoint list is thread safe, no need to lock
918         *sb_watchpoint = m_opaque_sp->GetWatchpointList().GetByIndex(idx);
919     }
920     return sb_watchpoint;
921 }
922 
923 bool
924 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
925 {
926     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
927 
928     bool result = false;
929     if (m_opaque_sp)
930     {
931         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
932         result = m_opaque_sp->RemoveWatchpointByID (wp_id);
933     }
934 
935     if (log)
936     {
937         log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) wp_id, result);
938     }
939 
940     return result;
941 }
942 
943 SBWatchpoint
944 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
945 {
946     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
947 
948     SBWatchpoint sb_watchpoint;
949     if (m_opaque_sp && wp_id != LLDB_INVALID_WATCH_ID)
950     {
951         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
952         *sb_watchpoint = m_opaque_sp->GetWatchpointList().FindByID(wp_id);
953     }
954 
955     if (log)
956     {
957         log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
958                      m_opaque_sp.get(), (uint32_t) wp_id, sb_watchpoint.get());
959     }
960 
961     return sb_watchpoint;
962 }
963 
964 lldb::SBWatchpoint
965 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write)
966 {
967     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
968 
969     SBWatchpoint sb_watchpoint;
970     if (m_opaque_sp)
971     {
972         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
973         uint32_t watch_type = (read ? LLDB_WATCH_TYPE_READ : 0) |
974             (write ? LLDB_WATCH_TYPE_WRITE : 0);
975         sb_watchpoint = m_opaque_sp->CreateWatchpoint(addr, size, watch_type);
976     }
977 
978     if (log)
979     {
980         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%llx, 0x%u) => SBWatchpoint(%p)",
981                      m_opaque_sp.get(), addr, (uint32_t) size, sb_watchpoint.get());
982     }
983 
984     return sb_watchpoint;
985 }
986 
987 bool
988 SBTarget::EnableAllWatchpoints ()
989 {
990     if (m_opaque_sp)
991     {
992         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
993         m_opaque_sp->EnableAllWatchpoints ();
994         return true;
995     }
996     return false;
997 }
998 
999 bool
1000 SBTarget::DisableAllWatchpoints ()
1001 {
1002     if (m_opaque_sp)
1003     {
1004         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
1005         m_opaque_sp->DisableAllWatchpoints ();
1006         return true;
1007     }
1008     return false;
1009 }
1010 
1011 bool
1012 SBTarget::DeleteAllWatchpoints ()
1013 {
1014     if (m_opaque_sp)
1015     {
1016         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
1017         m_opaque_sp->RemoveAllWatchpoints ();
1018         return true;
1019     }
1020     return false;
1021 }
1022 
1023 
1024 lldb::SBModule
1025 SBTarget::AddModule (const char *path,
1026                      const char *triple,
1027                      const char *uuid_cstr)
1028 {
1029     lldb::SBModule sb_module;
1030     if (m_opaque_sp)
1031     {
1032         FileSpec module_file_spec;
1033         UUID module_uuid;
1034         ArchSpec module_arch;
1035 
1036         if (path)
1037             module_file_spec.SetFile(path, false);
1038 
1039         if (uuid_cstr)
1040             module_uuid.SetfromCString(uuid_cstr);
1041 
1042         if (triple)
1043             module_arch.SetTriple (triple, m_opaque_sp->GetPlatform ().get());
1044 
1045         sb_module.SetModule(m_opaque_sp->GetSharedModule (module_file_spec,
1046                                                           module_arch,
1047                                                           uuid_cstr ? &module_uuid : NULL));
1048     }
1049     return sb_module;
1050 }
1051 
1052 bool
1053 SBTarget::AddModule (lldb::SBModule &module)
1054 {
1055     if (m_opaque_sp)
1056     {
1057         m_opaque_sp->GetImages().AppendIfNeeded (module.get_sp());
1058         return true;
1059     }
1060     return false;
1061 }
1062 
1063 lldb::SBModule
1064 AddModule (const char *path,
1065            const char *triple,
1066            const char *uuid);
1067 
1068 
1069 
1070 uint32_t
1071 SBTarget::GetNumModules () const
1072 {
1073     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1074 
1075     uint32_t num = 0;
1076     if (m_opaque_sp)
1077     {
1078         // The module list is thread safe, no need to lock
1079         num = m_opaque_sp->GetImages().GetSize();
1080     }
1081 
1082     if (log)
1083         log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
1084 
1085     return num;
1086 }
1087 
1088 void
1089 SBTarget::Clear ()
1090 {
1091     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1092 
1093     if (log)
1094         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1095 
1096     m_opaque_sp.reset();
1097 }
1098 
1099 
1100 SBModule
1101 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1102 {
1103     SBModule sb_module;
1104     if (m_opaque_sp && sb_file_spec.IsValid())
1105     {
1106         // The module list is thread safe, no need to lock
1107         sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL, NULL));
1108     }
1109     return sb_module;
1110 }
1111 
1112 SBModule
1113 SBTarget::GetModuleAtIndex (uint32_t idx)
1114 {
1115     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1116 
1117     SBModule sb_module;
1118     if (m_opaque_sp)
1119     {
1120         // The module list is thread safe, no need to lock
1121         sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
1122     }
1123 
1124     if (log)
1125     {
1126         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1127                      m_opaque_sp.get(), idx, sb_module.get());
1128     }
1129 
1130     return sb_module;
1131 }
1132 
1133 bool
1134 SBTarget::RemoveModule (lldb::SBModule module)
1135 {
1136     if (m_opaque_sp)
1137         return m_opaque_sp->GetImages().Remove(module.get_sp());
1138     return false;
1139 }
1140 
1141 
1142 SBBroadcaster
1143 SBTarget::GetBroadcaster () const
1144 {
1145     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1146 
1147     SBBroadcaster broadcaster(m_opaque_sp.get(), false);
1148 
1149     if (log)
1150         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1151                      m_opaque_sp.get(), broadcaster.get());
1152 
1153     return broadcaster;
1154 }
1155 
1156 
1157 bool
1158 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1159 {
1160     Stream &strm = description.ref();
1161 
1162     if (m_opaque_sp)
1163     {
1164         m_opaque_sp->Dump (&strm, description_level);
1165     }
1166     else
1167         strm.PutCString ("No value");
1168 
1169     return true;
1170 }
1171 
1172 uint32_t
1173 SBTarget::FindFunctions (const char *name,
1174                          uint32_t name_type_mask,
1175                          bool append,
1176                          lldb::SBSymbolContextList& sc_list)
1177 {
1178     if (!append)
1179         sc_list.Clear();
1180     if (m_opaque_sp)
1181     {
1182         const bool symbols_ok = true;
1183         return m_opaque_sp->GetImages().FindFunctions (ConstString(name),
1184                                                        name_type_mask,
1185                                                        symbols_ok,
1186                                                        append,
1187                                                        *sc_list);
1188     }
1189     return 0;
1190 }
1191 
1192 lldb::SBType
1193 SBTarget::FindFirstType (const char* type)
1194 {
1195     if (m_opaque_sp)
1196     {
1197         size_t count = m_opaque_sp->GetImages().GetSize();
1198         for (size_t idx = 0; idx < count; idx++)
1199         {
1200             SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
1201 
1202             if (found_at_idx.IsValid())
1203                 return found_at_idx;
1204         }
1205     }
1206     return SBType();
1207 }
1208 
1209 lldb::SBTypeList
1210 SBTarget::FindTypes (const char* type)
1211 {
1212 
1213     SBTypeList retval;
1214 
1215     if (m_opaque_sp)
1216     {
1217         ModuleList& images = m_opaque_sp->GetImages();
1218         ConstString name_const(type);
1219         SymbolContext sc;
1220         TypeList type_list;
1221 
1222         uint32_t num_matches = images.FindTypes(sc,
1223                                                 name_const,
1224                                                 true,
1225                                                 UINT32_MAX,
1226                                                 type_list);
1227 
1228         for (size_t idx = 0; idx < num_matches; idx++)
1229         {
1230             TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1231             if (type_sp)
1232                 retval.Append(SBType(type_sp));
1233         }
1234     }
1235     return retval;
1236 }
1237 
1238 SBValueList
1239 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1240 {
1241     SBValueList sb_value_list;
1242 
1243     if (m_opaque_sp)
1244     {
1245         VariableList variable_list;
1246         const bool append = true;
1247         const uint32_t match_count = m_opaque_sp->GetImages().FindGlobalVariables (ConstString (name),
1248                                                                                    append,
1249                                                                                    max_matches,
1250                                                                                    variable_list);
1251 
1252         if (match_count > 0)
1253         {
1254             ExecutionContextScope *exe_scope = m_opaque_sp->GetProcessSP().get();
1255             if (exe_scope == NULL)
1256                 exe_scope = m_opaque_sp.get();
1257             ValueObjectList &value_object_list = sb_value_list.ref();
1258             for (uint32_t i=0; i<match_count; ++i)
1259             {
1260                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1261                 if (valobj_sp)
1262                     value_object_list.Append(valobj_sp);
1263             }
1264         }
1265     }
1266 
1267     return sb_value_list;
1268 }
1269 
1270 SBSourceManager
1271 SBTarget::GetSourceManager()
1272 {
1273     SBSourceManager source_manager (*this);
1274     return source_manager;
1275 }
1276 
1277 
1278 SBError
1279 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
1280                                  lldb::addr_t section_base_addr)
1281 {
1282     SBError sb_error;
1283 
1284     if (IsValid())
1285     {
1286         if (!section.IsValid())
1287         {
1288             sb_error.SetErrorStringWithFormat ("invalid section");
1289         }
1290         else
1291         {
1292             m_opaque_sp->GetSectionLoadList().SetSectionLoadAddress (section.GetSection(), section_base_addr);
1293         }
1294     }
1295     else
1296     {
1297         sb_error.SetErrorStringWithFormat ("invalid target");
1298     }
1299     return sb_error;
1300 }
1301 
1302 SBError
1303 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
1304 {
1305     SBError sb_error;
1306 
1307     if (IsValid())
1308     {
1309         if (!section.IsValid())
1310         {
1311             sb_error.SetErrorStringWithFormat ("invalid section");
1312         }
1313         else
1314         {
1315             m_opaque_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSection());
1316         }
1317     }
1318     else
1319     {
1320         sb_error.SetErrorStringWithFormat ("invalid target");
1321     }
1322     return sb_error;
1323 }
1324 
1325 SBError
1326 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
1327 {
1328     SBError sb_error;
1329 
1330     char path[PATH_MAX];
1331     if (IsValid())
1332     {
1333         if (!module.IsValid())
1334         {
1335             sb_error.SetErrorStringWithFormat ("invalid module");
1336         }
1337         else
1338         {
1339             ObjectFile *objfile = module->GetObjectFile();
1340             if (objfile)
1341             {
1342                 SectionList *section_list = objfile->GetSectionList();
1343                 if (section_list)
1344                 {
1345                     const size_t num_sections = section_list->GetSize();
1346                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1347                     {
1348                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
1349                         if (section_sp)
1350                             m_opaque_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), section_sp->GetFileAddress() + slide_offset);
1351                     }
1352                 }
1353                 else
1354                 {
1355                     module->GetFileSpec().GetPath (path, sizeof(path));
1356                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
1357                 }
1358             }
1359             else
1360             {
1361                 module->GetFileSpec().GetPath (path, sizeof(path));
1362                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
1363             }
1364         }
1365     }
1366     else
1367     {
1368         sb_error.SetErrorStringWithFormat ("invalid target");
1369     }
1370     return sb_error;
1371 }
1372 
1373 SBError
1374 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
1375 {
1376     SBError sb_error;
1377 
1378     char path[PATH_MAX];
1379     if (IsValid())
1380     {
1381         if (!module.IsValid())
1382         {
1383             sb_error.SetErrorStringWithFormat ("invalid module");
1384         }
1385         else
1386         {
1387             ObjectFile *objfile = module->GetObjectFile();
1388             if (objfile)
1389             {
1390                 SectionList *section_list = objfile->GetSectionList();
1391                 if (section_list)
1392                 {
1393                     const size_t num_sections = section_list->GetSize();
1394                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1395                     {
1396                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
1397                         if (section_sp)
1398                             m_opaque_sp->GetSectionLoadList().SetSectionUnloaded (section_sp.get());
1399                     }
1400                 }
1401                 else
1402                 {
1403                     module->GetFileSpec().GetPath (path, sizeof(path));
1404                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
1405                 }
1406             }
1407             else
1408             {
1409                 module->GetFileSpec().GetPath (path, sizeof(path));
1410                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
1411             }
1412         }
1413     }
1414     else
1415     {
1416         sb_error.SetErrorStringWithFormat ("invalid target");
1417     }
1418     return sb_error;
1419 }
1420 
1421 
1422