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             error.SetError (sb_process->Attach (pid));
341             // If we are doing synchronous mode, then wait for the
342             // process to stop!
343             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
344                 sb_process->WaitForProcessToStop (NULL);
345         }
346         else
347         {
348             error.SetErrorString ("unable to create lldb_private::Process");
349         }
350     }
351     else
352     {
353         error.SetErrorString ("SBTarget is invalid");
354     }
355     return sb_process;
356 
357 }
358 
359 lldb::SBProcess
360 SBTarget::AttachToProcessWithName
361 (
362     SBListener &listener,
363     const char *name,   // basename of process to attach to
364     bool wait_for,      // if true wait for a new instance of "name" to be launched
365     SBError& error      // An error explaining what went wrong if attach fails
366 )
367 {
368     SBProcess sb_process;
369     if (m_opaque_sp)
370     {
371         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
372 
373         StateType state = eStateInvalid;
374         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
375         if (sb_process.IsValid())
376         {
377             state = sb_process->GetState();
378 
379             if (sb_process->IsAlive() && state != eStateConnected)
380             {
381                 if (state == eStateAttaching)
382                     error.SetErrorString ("process attach is in progress");
383                 else
384                     error.SetErrorString ("a process is already being debugged");
385                 sb_process.Clear();
386                 return sb_process;
387             }
388         }
389 
390         if (state == eStateConnected)
391         {
392             // If we are already connected, then we have already specified the
393             // listener, so if a valid listener is supplied, we need to error out
394             // to let the client know.
395             if (listener.IsValid())
396             {
397                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
398                 sb_process.Clear();
399                 return sb_process;
400             }
401         }
402         else
403         {
404             if (listener.IsValid())
405                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
406             else
407                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
408         }
409 
410         if (sb_process.IsValid())
411         {
412             error.SetError (sb_process->Attach (name, wait_for));
413             // If we are doing synchronous mode, then wait for the
414             // process to stop!
415             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
416                 sb_process->WaitForProcessToStop (NULL);
417         }
418         else
419         {
420             error.SetErrorString ("unable to create lldb_private::Process");
421         }
422     }
423     else
424     {
425         error.SetErrorString ("SBTarget is invalid");
426     }
427     return sb_process;
428 
429 }
430 
431 lldb::SBProcess
432 SBTarget::ConnectRemote
433 (
434     SBListener &listener,
435     const char *url,
436     const char *plugin_name,
437     SBError& error
438 )
439 {
440     SBProcess sb_process;
441     if (m_opaque_sp)
442     {
443         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
444         if (listener.IsValid())
445             sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref(), plugin_name));
446         else
447             sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener(), plugin_name));
448 
449 
450         if (sb_process.IsValid())
451         {
452             error.SetError (sb_process->ConnectRemote (url));
453         }
454         else
455         {
456             error.SetErrorString ("unable to create lldb_private::Process");
457         }
458     }
459     else
460     {
461         error.SetErrorString ("SBTarget is invalid");
462     }
463     return sb_process;
464 }
465 
466 SBFileSpec
467 SBTarget::GetExecutable ()
468 {
469 
470     SBFileSpec exe_file_spec;
471     if (m_opaque_sp)
472     {
473         Module *exe_module = m_opaque_sp->GetExecutableModulePointer();
474         if (exe_module)
475             exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
476     }
477 
478     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
479     if (log)
480     {
481         log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
482                      m_opaque_sp.get(), exe_file_spec.get());
483     }
484 
485     return exe_file_spec;
486 }
487 
488 bool
489 SBTarget::operator == (const SBTarget &rhs) const
490 {
491     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
492 }
493 
494 bool
495 SBTarget::operator != (const SBTarget &rhs) const
496 {
497     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
498 }
499 
500 lldb_private::Target *
501 SBTarget::operator ->() const
502 {
503     return m_opaque_sp.get();
504 }
505 
506 lldb_private::Target *
507 SBTarget::get() const
508 {
509     return m_opaque_sp.get();
510 }
511 
512 const lldb::TargetSP &
513 SBTarget::get_sp () const
514 {
515     return m_opaque_sp;
516 }
517 
518 void
519 SBTarget::reset (const lldb::TargetSP& target_sp)
520 {
521     m_opaque_sp = target_sp;
522 }
523 
524 lldb::SBAddress
525 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
526 {
527     lldb::SBAddress sb_addr;
528     Address &addr = sb_addr.ref();
529     if (m_opaque_sp)
530     {
531         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
532         if (m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
533             return sb_addr;
534     }
535 
536     // We have a load address that isn't in a section, just return an address
537     // with the offset filled in (the address) and the section set to NULL
538     addr.SetSection(NULL);
539     addr.SetOffset(vm_addr);
540     return sb_addr;
541 }
542 
543 SBSymbolContext
544 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
545 {
546     SBSymbolContext sc;
547     if (m_opaque_sp && addr.IsValid())
548         m_opaque_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
549     return sc;
550 }
551 
552 
553 SBBreakpoint
554 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
555 {
556     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
557 }
558 
559 SBBreakpoint
560 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
561 {
562     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
563 
564     SBBreakpoint sb_bp;
565     if (m_opaque_sp.get() && line != 0)
566     {
567         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
568         *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
569     }
570 
571     if (log)
572     {
573         SBStream sstr;
574         sb_bp.GetDescription (sstr);
575         char path[PATH_MAX];
576         sb_file_spec->GetPath (path, sizeof(path));
577         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
578                      m_opaque_sp.get(),
579                      path,
580                      line,
581                      sb_bp.get(),
582                      sstr.GetData());
583     }
584 
585     return sb_bp;
586 }
587 
588 SBBreakpoint
589 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
590 {
591     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
592 
593     SBBreakpoint sb_bp;
594     if (m_opaque_sp.get())
595     {
596         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
597         if (module_name && module_name[0])
598         {
599             FileSpecList module_spec_list;
600             module_spec_list.Append (FileSpec (module_name, false));
601             *sb_bp = m_opaque_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, false);
602         }
603         else
604         {
605             *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, false);
606         }
607     }
608 
609     if (log)
610     {
611         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
612                      m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
613     }
614 
615     return sb_bp;
616 }
617 
618 lldb::SBBreakpoint
619 SBTarget::BreakpointCreateByName (const char *symbol_name,
620                             const SBFileSpecList &module_list,
621                             const SBFileSpecList &comp_unit_list)
622 {
623     uint32_t name_type_mask = eFunctionNameTypeAuto;
624     return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
625 }
626 
627 lldb::SBBreakpoint
628 SBTarget::BreakpointCreateByName (const char *symbol_name,
629                             uint32_t name_type_mask,
630                             const SBFileSpecList &module_list,
631                             const SBFileSpecList &comp_unit_list)
632 {
633     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
634 
635     SBBreakpoint sb_bp;
636     if (m_opaque_sp.get() && symbol_name && symbol_name[0])
637     {
638         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
639         *sb_bp = m_opaque_sp->CreateBreakpoint (module_list.get(),
640                                                 comp_unit_list.get(),
641                                                 symbol_name,
642                                                 name_type_mask,
643                                                 false);
644     }
645 
646     if (log)
647     {
648         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
649                      m_opaque_sp.get(), symbol_name, name_type_mask, sb_bp.get());
650     }
651 
652     return sb_bp;
653 }
654 
655 
656 SBBreakpoint
657 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
658 {
659     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
660 
661     SBBreakpoint sb_bp;
662     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
663     {
664         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
665         RegularExpression regexp(symbol_name_regex);
666 
667         if (module_name && module_name[0])
668         {
669             FileSpecList module_spec_list;
670             module_spec_list.Append (FileSpec (module_name, false));
671 
672             *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, false);
673         }
674         else
675         {
676             *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, false);
677         }
678     }
679 
680     if (log)
681     {
682         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
683                      m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
684     }
685 
686     return sb_bp;
687 }
688 
689 lldb::SBBreakpoint
690 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
691                          const SBFileSpecList &module_list,
692                          const SBFileSpecList &comp_unit_list)
693 {
694     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
695 
696     SBBreakpoint sb_bp;
697     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
698     {
699         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
700         RegularExpression regexp(symbol_name_regex);
701 
702         *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, false);
703     }
704 
705     if (log)
706     {
707         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
708                      m_opaque_sp.get(), symbol_name_regex, sb_bp.get());
709     }
710 
711     return sb_bp;
712 }
713 
714 SBBreakpoint
715 SBTarget::BreakpointCreateByAddress (addr_t address)
716 {
717     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
718 
719     SBBreakpoint sb_bp;
720     if (m_opaque_sp.get())
721     {
722         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
723         *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
724     }
725 
726     if (log)
727     {
728         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%llu) => SBBreakpoint(%p)", m_opaque_sp.get(), (uint64_t) address, sb_bp.get());
729     }
730 
731     return sb_bp;
732 }
733 
734 lldb::SBBreakpoint
735 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
736 {
737     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
738 
739     SBBreakpoint sb_bp;
740     if (m_opaque_sp.get() && source_regex && source_regex[0])
741     {
742         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
743         RegularExpression regexp(source_regex);
744         FileSpecList source_file_spec_list;
745         source_file_spec_list.Append (source_file.ref());
746 
747         if (module_name && module_name[0])
748         {
749             FileSpecList module_spec_list;
750             module_spec_list.Append (FileSpec (module_name, false));
751 
752             *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
753         }
754         else
755         {
756             *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
757         }
758     }
759 
760     if (log)
761     {
762         char path[PATH_MAX];
763         source_file->GetPath (path, sizeof(path));
764         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
765                      m_opaque_sp.get(), source_regex, path, module_name, sb_bp.get());
766     }
767 
768     return sb_bp;
769 }
770 
771 lldb::SBBreakpoint
772 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
773                                const SBFileSpecList &module_list,
774                                const lldb::SBFileSpecList &source_file_list)
775 {
776     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
777 
778     SBBreakpoint sb_bp;
779     if (m_opaque_sp.get() && source_regex && source_regex[0])
780     {
781         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
782         RegularExpression regexp(source_regex);
783         *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
784     }
785 
786     if (log)
787     {
788         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
789                      m_opaque_sp.get(), source_regex, sb_bp.get());
790     }
791 
792     return sb_bp;
793 }
794 
795 uint32_t
796 SBTarget::GetNumBreakpoints () const
797 {
798     if (m_opaque_sp)
799     {
800         // The breakpoint list is thread safe, no need to lock
801         return m_opaque_sp->GetBreakpointList().GetSize();
802     }
803     return 0;
804 }
805 
806 SBBreakpoint
807 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
808 {
809     SBBreakpoint sb_breakpoint;
810     if (m_opaque_sp)
811     {
812         // The breakpoint list is thread safe, no need to lock
813         *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
814     }
815     return sb_breakpoint;
816 }
817 
818 bool
819 SBTarget::BreakpointDelete (break_id_t bp_id)
820 {
821     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
822 
823     bool result = false;
824     if (m_opaque_sp)
825     {
826         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
827         result = m_opaque_sp->RemoveBreakpointByID (bp_id);
828     }
829 
830     if (log)
831     {
832         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) bp_id, result);
833     }
834 
835     return result;
836 }
837 
838 SBBreakpoint
839 SBTarget::FindBreakpointByID (break_id_t bp_id)
840 {
841     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
842 
843     SBBreakpoint sb_breakpoint;
844     if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
845     {
846         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
847         *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
848     }
849 
850     if (log)
851     {
852         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
853                      m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
854     }
855 
856     return sb_breakpoint;
857 }
858 
859 bool
860 SBTarget::EnableAllBreakpoints ()
861 {
862     if (m_opaque_sp)
863     {
864         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
865         m_opaque_sp->EnableAllBreakpoints ();
866         return true;
867     }
868     return false;
869 }
870 
871 bool
872 SBTarget::DisableAllBreakpoints ()
873 {
874     if (m_opaque_sp)
875     {
876         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
877         m_opaque_sp->DisableAllBreakpoints ();
878         return true;
879     }
880     return false;
881 }
882 
883 bool
884 SBTarget::DeleteAllBreakpoints ()
885 {
886     if (m_opaque_sp)
887     {
888         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
889         m_opaque_sp->RemoveAllBreakpoints ();
890         return true;
891     }
892     return false;
893 }
894 
895 uint32_t
896 SBTarget::GetNumWatchpoints () const
897 {
898     if (m_opaque_sp)
899     {
900         // The watchpoint list is thread safe, no need to lock
901         return m_opaque_sp->GetWatchpointList().GetSize();
902     }
903     return 0;
904 }
905 
906 SBWatchpoint
907 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
908 {
909     SBWatchpoint sb_watchpoint;
910     if (m_opaque_sp)
911     {
912         // The watchpoint list is thread safe, no need to lock
913         *sb_watchpoint = m_opaque_sp->GetWatchpointList().GetByIndex(idx);
914     }
915     return sb_watchpoint;
916 }
917 
918 bool
919 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
920 {
921     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
922 
923     bool result = false;
924     if (m_opaque_sp)
925     {
926         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
927         result = m_opaque_sp->RemoveWatchpointByID (wp_id);
928     }
929 
930     if (log)
931     {
932         log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) wp_id, result);
933     }
934 
935     return result;
936 }
937 
938 SBWatchpoint
939 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
940 {
941     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
942 
943     SBWatchpoint sb_watchpoint;
944     if (m_opaque_sp && wp_id != LLDB_INVALID_WATCH_ID)
945     {
946         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
947         *sb_watchpoint = m_opaque_sp->GetWatchpointList().FindByID(wp_id);
948     }
949 
950     if (log)
951     {
952         log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
953                      m_opaque_sp.get(), (uint32_t) wp_id, sb_watchpoint.get());
954     }
955 
956     return sb_watchpoint;
957 }
958 
959 lldb::SBWatchpoint
960 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write)
961 {
962     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
963 
964     SBWatchpoint sb_watchpoint;
965     if (m_opaque_sp)
966     {
967         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
968         uint32_t watch_type = (read ? LLDB_WATCH_TYPE_READ : 0) |
969             (write ? LLDB_WATCH_TYPE_WRITE : 0);
970         sb_watchpoint = m_opaque_sp->CreateWatchpoint(addr, size, watch_type);
971     }
972 
973     if (log)
974     {
975         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%llx, 0x%u) => SBWatchpoint(%p)",
976                      m_opaque_sp.get(), addr, (uint32_t) size, sb_watchpoint.get());
977     }
978 
979     return sb_watchpoint;
980 }
981 
982 bool
983 SBTarget::EnableAllWatchpoints ()
984 {
985     if (m_opaque_sp)
986     {
987         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
988         m_opaque_sp->EnableAllWatchpoints ();
989         return true;
990     }
991     return false;
992 }
993 
994 bool
995 SBTarget::DisableAllWatchpoints ()
996 {
997     if (m_opaque_sp)
998     {
999         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
1000         m_opaque_sp->DisableAllWatchpoints ();
1001         return true;
1002     }
1003     return false;
1004 }
1005 
1006 bool
1007 SBTarget::DeleteAllWatchpoints ()
1008 {
1009     if (m_opaque_sp)
1010     {
1011         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
1012         m_opaque_sp->RemoveAllWatchpoints ();
1013         return true;
1014     }
1015     return false;
1016 }
1017 
1018 
1019 lldb::SBModule
1020 SBTarget::AddModule (const char *path,
1021                      const char *triple,
1022                      const char *uuid_cstr)
1023 {
1024     lldb::SBModule sb_module;
1025     if (m_opaque_sp)
1026     {
1027         FileSpec module_file_spec;
1028         UUID module_uuid;
1029         ArchSpec module_arch;
1030 
1031         if (path)
1032             module_file_spec.SetFile(path, false);
1033 
1034         if (uuid_cstr)
1035             module_uuid.SetfromCString(uuid_cstr);
1036 
1037         if (triple)
1038             module_arch.SetTriple (triple, m_opaque_sp->GetPlatform ().get());
1039 
1040         sb_module.SetModule(m_opaque_sp->GetSharedModule (module_file_spec,
1041                                                           module_arch,
1042                                                           uuid_cstr ? &module_uuid : NULL));
1043     }
1044     return sb_module;
1045 }
1046 
1047 bool
1048 SBTarget::AddModule (lldb::SBModule &module)
1049 {
1050     if (m_opaque_sp)
1051     {
1052         m_opaque_sp->GetImages().AppendIfNeeded (module.get_sp());
1053         return true;
1054     }
1055     return false;
1056 }
1057 
1058 lldb::SBModule
1059 AddModule (const char *path,
1060            const char *triple,
1061            const char *uuid);
1062 
1063 
1064 
1065 uint32_t
1066 SBTarget::GetNumModules () const
1067 {
1068     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1069 
1070     uint32_t num = 0;
1071     if (m_opaque_sp)
1072     {
1073         // The module list is thread safe, no need to lock
1074         num = m_opaque_sp->GetImages().GetSize();
1075     }
1076 
1077     if (log)
1078         log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
1079 
1080     return num;
1081 }
1082 
1083 void
1084 SBTarget::Clear ()
1085 {
1086     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1087 
1088     if (log)
1089         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1090 
1091     m_opaque_sp.reset();
1092 }
1093 
1094 
1095 SBModule
1096 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1097 {
1098     SBModule sb_module;
1099     if (m_opaque_sp && sb_file_spec.IsValid())
1100     {
1101         // The module list is thread safe, no need to lock
1102         sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL, NULL));
1103     }
1104     return sb_module;
1105 }
1106 
1107 SBModule
1108 SBTarget::GetModuleAtIndex (uint32_t idx)
1109 {
1110     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1111 
1112     SBModule sb_module;
1113     if (m_opaque_sp)
1114     {
1115         // The module list is thread safe, no need to lock
1116         sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
1117     }
1118 
1119     if (log)
1120     {
1121         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1122                      m_opaque_sp.get(), idx, sb_module.get());
1123     }
1124 
1125     return sb_module;
1126 }
1127 
1128 bool
1129 SBTarget::RemoveModule (lldb::SBModule module)
1130 {
1131     if (m_opaque_sp)
1132         return m_opaque_sp->GetImages().Remove(module.get_sp());
1133     return false;
1134 }
1135 
1136 
1137 SBBroadcaster
1138 SBTarget::GetBroadcaster () const
1139 {
1140     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1141 
1142     SBBroadcaster broadcaster(m_opaque_sp.get(), false);
1143 
1144     if (log)
1145         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1146                      m_opaque_sp.get(), broadcaster.get());
1147 
1148     return broadcaster;
1149 }
1150 
1151 
1152 bool
1153 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1154 {
1155     if (m_opaque_sp)
1156     {
1157         description.ref();
1158         m_opaque_sp->Dump (description.get(), description_level);
1159     }
1160     else
1161         description.Printf ("No value");
1162 
1163     return true;
1164 }
1165 
1166 bool
1167 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const
1168 {
1169     if (m_opaque_sp)
1170     {
1171         description.ref();
1172         m_opaque_sp->Dump (description.get(), description_level);
1173     }
1174     else
1175         description.Printf ("No value");
1176 
1177     return true;
1178 }
1179 
1180 
1181 uint32_t
1182 SBTarget::FindFunctions (const char *name,
1183                          uint32_t name_type_mask,
1184                          bool append,
1185                          lldb::SBSymbolContextList& sc_list)
1186 {
1187     if (!append)
1188         sc_list.Clear();
1189     if (m_opaque_sp)
1190     {
1191         const bool symbols_ok = true;
1192         return m_opaque_sp->GetImages().FindFunctions (ConstString(name),
1193                                                        name_type_mask,
1194                                                        symbols_ok,
1195                                                        append,
1196                                                        *sc_list);
1197     }
1198     return 0;
1199 }
1200 
1201 lldb::SBType
1202 SBTarget::FindFirstType (const char* type)
1203 {
1204     if (m_opaque_sp)
1205     {
1206         size_t count = m_opaque_sp->GetImages().GetSize();
1207         for (size_t idx = 0; idx < count; idx++)
1208         {
1209             SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
1210 
1211             if (found_at_idx.IsValid())
1212                 return found_at_idx;
1213         }
1214     }
1215     return SBType();
1216 }
1217 
1218 lldb::SBTypeList
1219 SBTarget::FindTypes (const char* type)
1220 {
1221 
1222     SBTypeList retval;
1223 
1224     if (m_opaque_sp)
1225     {
1226         ModuleList& images = m_opaque_sp->GetImages();
1227         ConstString name_const(type);
1228         SymbolContext sc;
1229         TypeList type_list;
1230 
1231         uint32_t num_matches = images.FindTypes(sc,
1232                                                 name_const,
1233                                                 true,
1234                                                 UINT32_MAX,
1235                                                 type_list);
1236 
1237         for (size_t idx = 0; idx < num_matches; idx++)
1238         {
1239             TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1240             if (type_sp)
1241                 retval.Append(SBType(type_sp));
1242         }
1243     }
1244     return retval;
1245 }
1246 
1247 SBValueList
1248 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1249 {
1250     SBValueList sb_value_list;
1251 
1252     if (m_opaque_sp)
1253     {
1254         VariableList variable_list;
1255         const bool append = true;
1256         const uint32_t match_count = m_opaque_sp->GetImages().FindGlobalVariables (ConstString (name),
1257                                                                                    append,
1258                                                                                    max_matches,
1259                                                                                    variable_list);
1260 
1261         if (match_count > 0)
1262         {
1263             ExecutionContextScope *exe_scope = m_opaque_sp->GetProcessSP().get();
1264             if (exe_scope == NULL)
1265                 exe_scope = m_opaque_sp.get();
1266             ValueObjectList &value_object_list = sb_value_list.ref();
1267             for (uint32_t i=0; i<match_count; ++i)
1268             {
1269                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1270                 if (valobj_sp)
1271                     value_object_list.Append(valobj_sp);
1272             }
1273         }
1274     }
1275 
1276     return sb_value_list;
1277 }
1278 
1279 SBSourceManager
1280 SBTarget::GetSourceManager()
1281 {
1282     SBSourceManager source_manager (*this);
1283     return source_manager;
1284 }
1285 
1286 
1287 SBError
1288 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
1289                                  lldb::addr_t section_base_addr)
1290 {
1291     SBError sb_error;
1292 
1293     if (IsValid())
1294     {
1295         if (!section.IsValid())
1296         {
1297             sb_error.SetErrorStringWithFormat ("invalid section");
1298         }
1299         else
1300         {
1301             m_opaque_sp->GetSectionLoadList().SetSectionLoadAddress (section.GetSection(), section_base_addr);
1302         }
1303     }
1304     else
1305     {
1306         sb_error.SetErrorStringWithFormat ("invalid target");
1307     }
1308     return sb_error;
1309 }
1310 
1311 SBError
1312 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
1313 {
1314     SBError sb_error;
1315 
1316     if (IsValid())
1317     {
1318         if (!section.IsValid())
1319         {
1320             sb_error.SetErrorStringWithFormat ("invalid section");
1321         }
1322         else
1323         {
1324             m_opaque_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSection());
1325         }
1326     }
1327     else
1328     {
1329         sb_error.SetErrorStringWithFormat ("invalid target");
1330     }
1331     return sb_error;
1332 }
1333 
1334 SBError
1335 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
1336 {
1337     SBError sb_error;
1338 
1339     char path[PATH_MAX];
1340     if (IsValid())
1341     {
1342         if (!module.IsValid())
1343         {
1344             sb_error.SetErrorStringWithFormat ("invalid module");
1345         }
1346         else
1347         {
1348             ObjectFile *objfile = module->GetObjectFile();
1349             if (objfile)
1350             {
1351                 SectionList *section_list = objfile->GetSectionList();
1352                 if (section_list)
1353                 {
1354                     const size_t num_sections = section_list->GetSize();
1355                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1356                     {
1357                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
1358                         if (section_sp)
1359                             m_opaque_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), section_sp->GetFileAddress() + slide_offset);
1360                     }
1361                 }
1362                 else
1363                 {
1364                     module->GetFileSpec().GetPath (path, sizeof(path));
1365                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
1366                 }
1367             }
1368             else
1369             {
1370                 module->GetFileSpec().GetPath (path, sizeof(path));
1371                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
1372             }
1373         }
1374     }
1375     else
1376     {
1377         sb_error.SetErrorStringWithFormat ("invalid target");
1378     }
1379     return sb_error;
1380 }
1381 
1382 SBError
1383 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
1384 {
1385     SBError sb_error;
1386 
1387     char path[PATH_MAX];
1388     if (IsValid())
1389     {
1390         if (!module.IsValid())
1391         {
1392             sb_error.SetErrorStringWithFormat ("invalid module");
1393         }
1394         else
1395         {
1396             ObjectFile *objfile = module->GetObjectFile();
1397             if (objfile)
1398             {
1399                 SectionList *section_list = objfile->GetSectionList();
1400                 if (section_list)
1401                 {
1402                     const size_t num_sections = section_list->GetSize();
1403                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1404                     {
1405                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
1406                         if (section_sp)
1407                             m_opaque_sp->GetSectionLoadList().SetSectionUnloaded (section_sp.get());
1408                     }
1409                 }
1410                 else
1411                 {
1412                     module->GetFileSpec().GetPath (path, sizeof(path));
1413                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
1414                 }
1415             }
1416             else
1417             {
1418                 module->GetFileSpec().GetPath (path, sizeof(path));
1419                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
1420             }
1421         }
1422     }
1423     else
1424     {
1425         sb_error.SetErrorStringWithFormat ("invalid target");
1426     }
1427     return sb_error;
1428 }
1429 
1430 
1431