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