blob: 7635a364901fde5e2a7bbdf7020a8db6a7e138af [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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
Eli Friedman7a62c8b2010-06-09 07:44:37 +000010#include "lldb/API/SBTarget.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
Greg Claytonb3448432011-03-24 21:19:54 +000012#include "lldb/lldb-public.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013
Greg Clayton917c0002011-06-29 22:09:02 +000014#include "lldb/API/SBDebugger.h"
15#include "lldb/API/SBBreakpoint.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/API/SBFileSpec.h"
Greg Clayton917c0002011-06-29 22:09:02 +000017#include "lldb/API/SBListener.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/API/SBModule.h"
Jim Inghamcc637462011-09-13 00:29:56 +000019#include "lldb/API/SBSourceManager.h"
Greg Clayton917c0002011-06-29 22:09:02 +000020#include "lldb/API/SBProcess.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000021#include "lldb/API/SBStream.h"
Greg Clayton4ed315f2011-06-21 01:34:41 +000022#include "lldb/API/SBSymbolContextList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#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"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Core/ArchSpec.h"
31#include "lldb/Core/Debugger.h"
32#include "lldb/Core/Disassembler.h"
Caroline Tice7826c882010-10-26 03:11:13 +000033#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034#include "lldb/Core/RegularExpression.h"
35#include "lldb/Core/SearchFilter.h"
36#include "lldb/Core/STLUtils.h"
Greg Clayton917c0002011-06-29 22:09:02 +000037#include "lldb/Core/ValueObjectList.h"
38#include "lldb/Core/ValueObjectVariable.h"
39#include "lldb/Host/FileSpec.h"
Greg Claytoncd548032011-02-01 01:31:41 +000040#include "lldb/Host/Host.h"
Greg Clayton917c0002011-06-29 22:09:02 +000041#include "lldb/Interpreter/Args.h"
Enrico Granata979e20d2011-07-29 19:53:35 +000042#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton917c0002011-06-29 22:09:02 +000043#include "lldb/Symbol/VariableList.h"
Jim Ingham4722b102012-03-06 00:37:27 +000044#include "lldb/Target/LanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000045#include "lldb/Target/Process.h"
46#include "lldb/Target/Target.h"
47#include "lldb/Target/TargetList.h"
48
49#include "lldb/Interpreter/CommandReturnObject.h"
50#include "../source/Commands/CommandObjectBreakpoint.h"
51
Chris Lattner24943d22010-06-08 16:52:24 +000052
53using namespace lldb;
54using namespace lldb_private;
55
56#define DEFAULT_DISASM_BYTE_SIZE 32
57
Greg Clayton98ca1e62012-02-24 20:59:25 +000058SBLaunchInfo::SBLaunchInfo (const char **argv) :
59 m_opaque_sp(new ProcessLaunchInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +000060{
Greg Clayton98ca1e62012-02-24 20:59:25 +000061 m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
62 if (argv && argv[0])
63 m_opaque_sp->GetArguments().SetArguments(argv);
Greg Clayton0a8dcac2012-02-24 05:03:03 +000064}
65
Greg Clayton66c2e192012-03-07 23:52:51 +000066SBLaunchInfo::~SBLaunchInfo()
67{
68}
69
70lldb_private::ProcessLaunchInfo &
71SBLaunchInfo::ref ()
72{
73 return *m_opaque_sp;
74}
75
76
Greg Clayton0a8dcac2012-02-24 05:03:03 +000077uint32_t
78SBLaunchInfo::GetUserID()
79{
80 return m_opaque_sp->GetUserID();
81}
82
83uint32_t
84SBLaunchInfo::GetGroupID()
85{
86 return m_opaque_sp->GetGroupID();
87}
88
89bool
90SBLaunchInfo::UserIDIsValid ()
91{
92 return m_opaque_sp->UserIDIsValid();
93}
94
95bool
96SBLaunchInfo::GroupIDIsValid ()
97{
98 return m_opaque_sp->GroupIDIsValid();
99}
100
101void
102SBLaunchInfo::SetUserID (uint32_t uid)
103{
104 m_opaque_sp->SetUserID (uid);
105}
106
107void
108SBLaunchInfo::SetGroupID (uint32_t gid)
109{
110 m_opaque_sp->SetGroupID (gid);
111}
112
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000113uint32_t
114SBLaunchInfo::GetNumArguments ()
115{
116 return m_opaque_sp->GetArguments().GetArgumentCount();
117}
118
119const char *
120SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
121{
122 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
123}
124
125void
126SBLaunchInfo::SetArguments (const char **argv, bool append)
127{
128 if (append)
129 {
130 if (argv)
131 m_opaque_sp->GetArguments().AppendArguments(argv);
132 }
133 else
134 {
135 if (argv)
136 m_opaque_sp->GetArguments().SetArguments(argv);
137 else
138 m_opaque_sp->GetArguments().Clear();
139 }
140}
141
142uint32_t
143SBLaunchInfo::GetNumEnvironmentEntries ()
144{
145 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
146}
147
148const char *
149SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
150{
151 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
152}
153
154void
155SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
156{
157 if (append)
158 {
159 if (envp)
160 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
161 }
162 else
163 {
164 if (envp)
165 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
166 else
167 m_opaque_sp->GetEnvironmentEntries().Clear();
168 }
169}
170
171void
172SBLaunchInfo::Clear ()
173{
174 m_opaque_sp->Clear();
175}
176
177const char *
178SBLaunchInfo::GetWorkingDirectory () const
179{
180 return m_opaque_sp->GetWorkingDirectory();
181}
182
183void
184SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
185{
186 m_opaque_sp->SetWorkingDirectory(working_dir);
187}
188
189uint32_t
190SBLaunchInfo::GetLaunchFlags ()
191{
192 return m_opaque_sp->GetFlags().Get();
193}
194
195void
196SBLaunchInfo::SetLaunchFlags (uint32_t flags)
197{
198 m_opaque_sp->GetFlags().Reset(flags);
199}
200
201const char *
202SBLaunchInfo::GetProcessPluginName ()
203{
204 return m_opaque_sp->GetProcessPluginName();
205}
206
207void
208SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
209{
210 return m_opaque_sp->SetProcessPluginName (plugin_name);
211}
212
213const char *
214SBLaunchInfo::GetShell ()
215{
216 return m_opaque_sp->GetShell();
217}
218
219void
220SBLaunchInfo::SetShell (const char * path)
221{
222 m_opaque_sp->SetShell (path);
223}
224
225uint32_t
226SBLaunchInfo::GetResumeCount ()
227{
228 return m_opaque_sp->GetResumeCount();
229}
230
231void
232SBLaunchInfo::SetResumeCount (uint32_t c)
233{
234 m_opaque_sp->SetResumeCount (c);
235}
236
237bool
238SBLaunchInfo::AddCloseFileAction (int fd)
239{
240 return m_opaque_sp->AppendCloseFileAction(fd);
241}
242
243bool
244SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
245{
246 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
247}
248
249bool
250SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
251{
252 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
253}
254
255bool
256SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
257{
258 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
259}
260
261
262SBAttachInfo::SBAttachInfo () :
Greg Clayton66c2e192012-03-07 23:52:51 +0000263 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000264{
265}
266
267SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
Greg Clayton66c2e192012-03-07 23:52:51 +0000268 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000269{
270 m_opaque_sp->SetProcessID (pid);
271}
272
273SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
Greg Clayton66c2e192012-03-07 23:52:51 +0000274 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000275{
276 if (path && path[0])
277 m_opaque_sp->GetExecutableFile().SetFile(path, false);
278 m_opaque_sp->SetWaitForLaunch (wait_for);
279}
280
281SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
Greg Clayton66c2e192012-03-07 23:52:51 +0000282 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000283{
284 *m_opaque_sp = *rhs.m_opaque_sp;
285}
286
Greg Clayton66c2e192012-03-07 23:52:51 +0000287SBAttachInfo::~SBAttachInfo()
288{
289}
290
291lldb_private::ProcessAttachInfo &
292SBAttachInfo::ref ()
293{
294 return *m_opaque_sp;
295}
296
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000297SBAttachInfo &
298SBAttachInfo::operator = (const SBAttachInfo &rhs)
299{
300 if (this != &rhs)
301 *m_opaque_sp = *rhs.m_opaque_sp;
302 return *this;
303}
304
305lldb::pid_t
306SBAttachInfo::GetProcessID ()
307{
308 return m_opaque_sp->GetProcessID();
309}
310
311void
312SBAttachInfo::SetProcessID (lldb::pid_t pid)
313{
314 m_opaque_sp->SetProcessID (pid);
315}
316
317
318uint32_t
319SBAttachInfo::GetResumeCount ()
320{
321 return m_opaque_sp->GetResumeCount();
322}
323
324void
325SBAttachInfo::SetResumeCount (uint32_t c)
326{
327 m_opaque_sp->SetResumeCount (c);
328}
329
330const char *
331SBAttachInfo::GetProcessPluginName ()
332{
333 return m_opaque_sp->GetProcessPluginName();
334}
335
336void
337SBAttachInfo::SetProcessPluginName (const char *plugin_name)
338{
339 return m_opaque_sp->SetProcessPluginName (plugin_name);
340}
341
342void
343SBAttachInfo::SetExecutable (const char *path)
344{
345 if (path && path[0])
346 m_opaque_sp->GetExecutableFile().SetFile(path, false);
347 else
348 m_opaque_sp->GetExecutableFile().Clear();
349}
350
351void
352SBAttachInfo::SetExecutable (SBFileSpec exe_file)
353{
354 if (exe_file.IsValid())
355 m_opaque_sp->GetExecutableFile() = exe_file.ref();
356 else
357 m_opaque_sp->GetExecutableFile().Clear();
358}
359
360bool
361SBAttachInfo::GetWaitForLaunch ()
362{
363 return m_opaque_sp->GetWaitForLaunch();
364}
365
366void
367SBAttachInfo::SetWaitForLaunch (bool b)
368{
369 m_opaque_sp->SetWaitForLaunch (b);
370}
371
Jim Ingham3a458eb2012-07-20 21:37:13 +0000372bool
373SBAttachInfo::GetIgnoreExisting ()
374{
375 return m_opaque_sp->GetIgnoreExisting();
376}
377
378void
379SBAttachInfo::SetIgnoreExisting (bool b)
380{
381 m_opaque_sp->SetIgnoreExisting (b);
382}
383
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000384uint32_t
Greg Clayton80efa5e2012-02-24 23:56:06 +0000385SBAttachInfo::GetUserID()
386{
387 return m_opaque_sp->GetUserID();
388}
389
390uint32_t
391SBAttachInfo::GetGroupID()
392{
393 return m_opaque_sp->GetGroupID();
394}
395
396bool
397SBAttachInfo::UserIDIsValid ()
398{
399 return m_opaque_sp->UserIDIsValid();
400}
401
402bool
403SBAttachInfo::GroupIDIsValid ()
404{
405 return m_opaque_sp->GroupIDIsValid();
406}
407
408void
409SBAttachInfo::SetUserID (uint32_t uid)
410{
411 m_opaque_sp->SetUserID (uid);
412}
413
414void
415SBAttachInfo::SetGroupID (uint32_t gid)
416{
417 m_opaque_sp->SetGroupID (gid);
418}
419
420uint32_t
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000421SBAttachInfo::GetEffectiveUserID()
422{
423 return m_opaque_sp->GetEffectiveUserID();
424}
425
426uint32_t
427SBAttachInfo::GetEffectiveGroupID()
428{
429 return m_opaque_sp->GetEffectiveGroupID();
430}
431
432bool
433SBAttachInfo::EffectiveUserIDIsValid ()
434{
435 return m_opaque_sp->EffectiveUserIDIsValid();
436}
437
438bool
439SBAttachInfo::EffectiveGroupIDIsValid ()
440{
441 return m_opaque_sp->EffectiveGroupIDIsValid ();
442}
443
444void
445SBAttachInfo::SetEffectiveUserID (uint32_t uid)
446{
447 m_opaque_sp->SetEffectiveUserID(uid);
448}
449
450void
451SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
452{
453 m_opaque_sp->SetEffectiveGroupID(gid);
454}
455
456lldb::pid_t
457SBAttachInfo::GetParentProcessID ()
458{
459 return m_opaque_sp->GetParentProcessID();
460}
461
462void
463SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
464{
465 m_opaque_sp->SetParentProcessID (pid);
466}
467
468bool
469SBAttachInfo::ParentProcessIDIsValid()
470{
471 return m_opaque_sp->ParentProcessIDIsValid();
472}
473
474
Chris Lattner24943d22010-06-08 16:52:24 +0000475//----------------------------------------------------------------------
476// SBTarget constructor
477//----------------------------------------------------------------------
Greg Claytonc3b61d22010-12-15 05:08:08 +0000478SBTarget::SBTarget () :
479 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +0000480{
481}
482
483SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +0000484 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000485{
486}
487
488SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +0000489 m_opaque_sp (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000490{
491}
492
Greg Clayton538eb822010-11-05 23:17:00 +0000493const SBTarget&
494SBTarget::operator = (const SBTarget& rhs)
495{
496 if (this != &rhs)
497 m_opaque_sp = rhs.m_opaque_sp;
498 return *this;
499}
500
Chris Lattner24943d22010-06-08 16:52:24 +0000501//----------------------------------------------------------------------
502// Destructor
503//----------------------------------------------------------------------
504SBTarget::~SBTarget()
505{
506}
507
Jim Ingham5a15e692012-02-16 06:50:00 +0000508const char *
509SBTarget::GetBroadcasterClassName ()
510{
511 return Target::GetStaticBroadcasterClass().AsCString();
512}
513
Chris Lattner24943d22010-06-08 16:52:24 +0000514bool
515SBTarget::IsValid () const
516{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000517 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000518}
519
520SBProcess
521SBTarget::GetProcess ()
522{
523 SBProcess sb_process;
Greg Clayton334d33a2012-01-30 07:41:31 +0000524 ProcessSP process_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000525 TargetSP target_sp(GetSP());
526 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000527 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000528 process_sp = target_sp->GetProcessSP();
Greg Clayton334d33a2012-01-30 07:41:31 +0000529 sb_process.SetSP (process_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000530 }
Caroline Tice7826c882010-10-26 03:11:13 +0000531
Greg Claytone005f2c2010-11-06 01:53:30 +0000532 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000533 if (log)
534 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000535 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000536 target_sp.get(), process_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000537 }
538
Chris Lattner24943d22010-06-08 16:52:24 +0000539 return sb_process;
540}
541
Greg Clayton63094e02010-06-23 01:19:29 +0000542SBDebugger
543SBTarget::GetDebugger () const
544{
545 SBDebugger debugger;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000546 TargetSP target_sp(GetSP());
547 if (target_sp)
548 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton63094e02010-06-23 01:19:29 +0000549 return debugger;
550}
551
Jim Inghamb5871fe2011-03-31 00:01:24 +0000552SBProcess
553SBTarget::LaunchSimple
554(
555 char const **argv,
556 char const **envp,
557 const char *working_directory
558)
559{
560 char *stdin_path = NULL;
561 char *stdout_path = NULL;
562 char *stderr_path = NULL;
563 uint32_t launch_flags = 0;
564 bool stop_at_entry = false;
565 SBError error;
566 SBListener listener = GetDebugger().GetListener();
567 return Launch (listener,
568 argv,
569 envp,
570 stdin_path,
571 stdout_path,
572 stderr_path,
573 working_directory,
574 launch_flags,
575 stop_at_entry,
576 error);
577}
Greg Claytonde915be2011-01-23 05:56:20 +0000578
579SBProcess
580SBTarget::Launch
581(
Greg Clayton271a5db2011-02-03 21:28:34 +0000582 SBListener &listener,
Greg Claytonde915be2011-01-23 05:56:20 +0000583 char const **argv,
584 char const **envp,
585 const char *stdin_path,
586 const char *stdout_path,
587 const char *stderr_path,
588 const char *working_directory,
589 uint32_t launch_flags, // See LaunchFlags
590 bool stop_at_entry,
591 lldb::SBError& error
592)
593{
Greg Claytone005f2c2010-11-06 01:53:30 +0000594 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000595
Greg Clayton0416bdf2012-01-30 09:04:36 +0000596 SBProcess sb_process;
597 ProcessSP process_sp;
598 TargetSP target_sp(GetSP());
599
Caroline Tice7826c882010-10-26 03:11:13 +0000600 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000601 {
Greg Claytonde915be2011-01-23 05:56:20 +0000602 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))...",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000603 target_sp.get(),
Greg Claytonde915be2011-01-23 05:56:20 +0000604 argv,
605 envp,
606 stdin_path ? stdin_path : "NULL",
607 stdout_path ? stdout_path : "NULL",
608 stderr_path ? stderr_path : "NULL",
609 working_directory ? working_directory : "NULL",
610 launch_flags,
611 stop_at_entry,
612 error.get());
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000613 }
Greg Clayton0416bdf2012-01-30 09:04:36 +0000614
615 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000616 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000617 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000618
Greg Clayton7c330d62011-01-27 01:01:10 +0000619 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
620 launch_flags |= eLaunchFlagDisableASLR;
621
Greg Clayton180546b2011-04-30 01:09:13 +0000622 StateType state = eStateInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000623 process_sp = target_sp->GetProcessSP();
Greg Clayton334d33a2012-01-30 07:41:31 +0000624 if (process_sp)
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000625 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000626 state = process_sp->GetState();
Greg Clayton180546b2011-04-30 01:09:13 +0000627
Greg Clayton334d33a2012-01-30 07:41:31 +0000628 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton180546b2011-04-30 01:09:13 +0000629 {
630 if (state == eStateAttaching)
631 error.SetErrorString ("process attach is in progress");
632 else
633 error.SetErrorString ("a process is already being debugged");
Greg Clayton180546b2011-04-30 01:09:13 +0000634 return sb_process;
635 }
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000636 }
637
Greg Clayton180546b2011-04-30 01:09:13 +0000638 if (state == eStateConnected)
639 {
640 // If we are already connected, then we have already specified the
641 // listener, so if a valid listener is supplied, we need to error out
642 // to let the client know.
643 if (listener.IsValid())
644 {
645 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton180546b2011-04-30 01:09:13 +0000646 return sb_process;
647 }
648 }
649 else
Greg Claytonc5f728c2010-10-06 22:10:17 +0000650 {
Greg Clayton271a5db2011-02-03 21:28:34 +0000651 if (listener.IsValid())
Greg Clayton46c9a352012-02-09 06:16:32 +0000652 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton271a5db2011-02-03 21:28:34 +0000653 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000654 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton180546b2011-04-30 01:09:13 +0000655 }
Greg Clayton7c330d62011-01-27 01:01:10 +0000656
Greg Clayton334d33a2012-01-30 07:41:31 +0000657 if (process_sp)
Greg Clayton180546b2011-04-30 01:09:13 +0000658 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000659 sb_process.SetSP (process_sp);
Greg Clayton180546b2011-04-30 01:09:13 +0000660 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
661 launch_flags |= eLaunchFlagDisableSTDIO;
662
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000663 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
664
Greg Clayton0416bdf2012-01-30 09:04:36 +0000665 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000666 if (exe_module)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000667 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000668 if (argv)
669 launch_info.GetArguments().AppendArguments (argv);
670 if (envp)
671 launch_info.GetEnvironmentEntries ().SetArguments (envp);
672
Greg Clayton334d33a2012-01-30 07:41:31 +0000673 error.SetError (process_sp->Launch (launch_info));
Greg Clayton180546b2011-04-30 01:09:13 +0000674 if (error.Success())
Greg Clayton7c330d62011-01-27 01:01:10 +0000675 {
Greg Clayton180546b2011-04-30 01:09:13 +0000676 // We we are stopping at the entry point, we can return now!
677 if (stop_at_entry)
678 return sb_process;
Greg Clayton7c330d62011-01-27 01:01:10 +0000679
Greg Clayton180546b2011-04-30 01:09:13 +0000680 // Make sure we are stopped at the entry
Greg Clayton334d33a2012-01-30 07:41:31 +0000681 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton180546b2011-04-30 01:09:13 +0000682 if (state == eStateStopped)
Greg Clayton7c330d62011-01-27 01:01:10 +0000683 {
Greg Clayton180546b2011-04-30 01:09:13 +0000684 // resume the process to skip the entry point
Greg Clayton334d33a2012-01-30 07:41:31 +0000685 error.SetError (process_sp->Resume());
Greg Clayton180546b2011-04-30 01:09:13 +0000686 if (error.Success())
Greg Clayton7c330d62011-01-27 01:01:10 +0000687 {
Greg Clayton180546b2011-04-30 01:09:13 +0000688 // If we are doing synchronous mode, then wait for the
689 // process to stop yet again!
Greg Clayton0416bdf2012-01-30 09:04:36 +0000690 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Clayton334d33a2012-01-30 07:41:31 +0000691 process_sp->WaitForProcessToStop (NULL);
Greg Clayton7c330d62011-01-27 01:01:10 +0000692 }
693 }
694 }
Greg Clayton180546b2011-04-30 01:09:13 +0000695 }
696 else
697 {
698 error.SetErrorString ("unable to create lldb_private::Process");
Greg Claytonc5f728c2010-10-06 22:10:17 +0000699 }
700 }
701 else
702 {
703 error.SetErrorString ("SBTarget is invalid");
Chris Lattner24943d22010-06-08 16:52:24 +0000704 }
Caroline Tice7826c882010-10-26 03:11:13 +0000705
Caroline Tice926060e2010-10-29 21:48:37 +0000706 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +0000707 if (log)
708 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000709 log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000710 target_sp.get(), process_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000711 }
712
Greg Clayton1a3083a2010-10-06 03:53:16 +0000713 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000714}
715
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000716SBProcess
717SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
718{
719 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
720
721 SBProcess sb_process;
722 ProcessSP process_sp;
723 TargetSP target_sp(GetSP());
724
725 if (log)
726 {
727 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
728 }
729
730 if (target_sp)
731 {
732 Mutex::Locker api_locker (target_sp->GetAPIMutex());
733 StateType state = eStateInvalid;
734 process_sp = target_sp->GetProcessSP();
735 if (process_sp)
736 {
737 state = process_sp->GetState();
738
739 if (process_sp->IsAlive() && state != eStateConnected)
740 {
741 if (state == eStateAttaching)
742 error.SetErrorString ("process attach is in progress");
743 else
744 error.SetErrorString ("a process is already being debugged");
745 return sb_process;
746 }
747 }
748
749 if (state != eStateConnected)
750 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
751
752 if (process_sp)
753 {
754 sb_process.SetSP (process_sp);
755 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
Greg Clayton98ca1e62012-02-24 20:59:25 +0000756
Greg Clayton98ca1e62012-02-24 20:59:25 +0000757 Module *exe_module = target_sp->GetExecutableModulePointer();
758 if (exe_module)
Han Ming Ongc86723f2012-03-02 01:02:04 +0000759 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton98ca1e62012-02-24 20:59:25 +0000760
761 const ArchSpec &arch_spec = target_sp->GetArchitecture();
762 if (arch_spec.IsValid())
763 launch_info.GetArchitecture () = arch_spec;
764
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000765 error.SetError (process_sp->Launch (launch_info));
Greg Clayton98ca1e62012-02-24 20:59:25 +0000766 const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000767 if (error.Success())
768 {
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000769 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Clayton98ca1e62012-02-24 20:59:25 +0000770 {
771 // If we are doing synchronous mode, then wait for the initial
772 // stop to happen, else, return and let the caller watch for
773 // the stop
774 if (synchronous_execution)
Greg Clayton4a379b12012-07-17 03:23:13 +0000775 process_sp->WaitForProcessToStop (NULL);
Greg Clayton98ca1e62012-02-24 20:59:25 +0000776 // We we are stopping at the entry point, we can return now!
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000777 return sb_process;
Greg Clayton98ca1e62012-02-24 20:59:25 +0000778 }
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000779
780 // Make sure we are stopped at the entry
Greg Clayton4a379b12012-07-17 03:23:13 +0000781 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000782 if (state == eStateStopped)
783 {
784 // resume the process to skip the entry point
785 error.SetError (process_sp->Resume());
786 if (error.Success())
787 {
788 // If we are doing synchronous mode, then wait for the
789 // process to stop yet again!
Greg Clayton98ca1e62012-02-24 20:59:25 +0000790 if (synchronous_execution)
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000791 process_sp->WaitForProcessToStop (NULL);
792 }
793 }
794 }
795 }
796 else
797 {
798 error.SetErrorString ("unable to create lldb_private::Process");
799 }
800 }
801 else
802 {
803 error.SetErrorString ("SBTarget is invalid");
804 }
805
806 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
807 if (log)
808 {
809 log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
810 target_sp.get(), process_sp.get());
811 }
812
813 return sb_process;
814}
815
816lldb::SBProcess
817SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
818{
819 SBProcess sb_process;
820 ProcessSP process_sp;
821 TargetSP target_sp(GetSP());
822 if (target_sp)
823 {
824 Mutex::Locker api_locker (target_sp->GetAPIMutex());
825
826 StateType state = eStateInvalid;
827 process_sp = target_sp->GetProcessSP();
828 if (process_sp)
829 {
830 state = process_sp->GetState();
831
832 if (process_sp->IsAlive() && state != eStateConnected)
833 {
834 if (state == eStateAttaching)
835 error.SetErrorString ("process attach is in progress");
836 else
837 error.SetErrorString ("a process is already being debugged");
838 return sb_process;
839 }
840 }
841
842 if (state != eStateConnected)
843 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
844
845 if (process_sp)
846 {
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000847 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ong435c5ef2012-02-29 00:12:56 +0000848 lldb::pid_t attach_pid = attach_info.GetProcessID();
849 if (attach_pid != LLDB_INVALID_PROCESS_ID)
850 {
851 PlatformSP platform_sp = target_sp->GetPlatform();
852 ProcessInstanceInfo instance_info;
853 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
854 {
855 attach_info.SetUserID(instance_info.GetEffectiveUserID());
856 }
857 }
Han Ming Ong94b4e9a2012-02-29 19:16:40 +0000858 error.SetError (process_sp->Attach (attach_info));
859 if (error.Success())
860 {
861 sb_process.SetSP (process_sp);
862 // If we are doing synchronous mode, then wait for the
863 // process to stop!
864 if (target_sp->GetDebugger().GetAsyncExecution () == false)
865 process_sp->WaitForProcessToStop (NULL);
866 }
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000867 }
868 else
869 {
870 error.SetErrorString ("unable to create lldb_private::Process");
871 }
872 }
873 else
874 {
875 error.SetErrorString ("SBTarget is invalid");
876 }
877 return sb_process;
878}
879
880
Greg Claytond5b0b442011-12-02 02:10:57 +0000881#if defined(__APPLE__)
882
883lldb::SBProcess
884SBTarget::AttachToProcessWithID (SBListener &listener,
885 ::pid_t pid,
886 lldb::SBError& error)
887{
888 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
889}
890
891#endif // #if defined(__APPLE__)
Greg Claytonc5f728c2010-10-06 22:10:17 +0000892
893lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000894SBTarget::AttachToProcessWithID
Greg Claytonc5f728c2010-10-06 22:10:17 +0000895(
Greg Clayton271a5db2011-02-03 21:28:34 +0000896 SBListener &listener,
Greg Claytonc5f728c2010-10-06 22:10:17 +0000897 lldb::pid_t pid,// The process ID to attach to
898 SBError& error // An error explaining what went wrong if attach fails
899)
900{
901 SBProcess sb_process;
Greg Clayton334d33a2012-01-30 07:41:31 +0000902 ProcessSP process_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000903 TargetSP target_sp(GetSP());
904 if (target_sp)
Greg Claytonc5f728c2010-10-06 22:10:17 +0000905 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000906 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonde1dd812011-06-24 03:21:43 +0000907
908 StateType state = eStateInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000909 process_sp = target_sp->GetProcessSP();
Greg Clayton334d33a2012-01-30 07:41:31 +0000910 if (process_sp)
Greg Claytonde1dd812011-06-24 03:21:43 +0000911 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000912 state = process_sp->GetState();
Greg Claytonde1dd812011-06-24 03:21:43 +0000913
Greg Clayton334d33a2012-01-30 07:41:31 +0000914 if (process_sp->IsAlive() && state != eStateConnected)
Greg Claytonde1dd812011-06-24 03:21:43 +0000915 {
916 if (state == eStateAttaching)
917 error.SetErrorString ("process attach is in progress");
918 else
919 error.SetErrorString ("a process is already being debugged");
Greg Claytonde1dd812011-06-24 03:21:43 +0000920 return sb_process;
921 }
922 }
923
924 if (state == eStateConnected)
925 {
926 // If we are already connected, then we have already specified the
927 // listener, so if a valid listener is supplied, we need to error out
928 // to let the client know.
929 if (listener.IsValid())
930 {
931 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Claytonde1dd812011-06-24 03:21:43 +0000932 return sb_process;
933 }
934 }
Greg Clayton271a5db2011-02-03 21:28:34 +0000935 else
Greg Claytonde1dd812011-06-24 03:21:43 +0000936 {
937 if (listener.IsValid())
Greg Clayton46c9a352012-02-09 06:16:32 +0000938 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Claytonde1dd812011-06-24 03:21:43 +0000939 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000940 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Claytonde1dd812011-06-24 03:21:43 +0000941 }
Greg Clayton334d33a2012-01-30 07:41:31 +0000942 if (process_sp)
Greg Claytonc5f728c2010-10-06 22:10:17 +0000943 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000944 sb_process.SetSP (process_sp);
945
Greg Clayton527154d2011-11-15 03:53:30 +0000946 ProcessAttachInfo attach_info;
947 attach_info.SetProcessID (pid);
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000948
949 PlatformSP platform_sp = target_sp->GetPlatform();
950 ProcessInstanceInfo instance_info;
951 if (platform_sp->GetProcessInfo(pid, instance_info))
952 {
953 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000954 }
Greg Clayton334d33a2012-01-30 07:41:31 +0000955 error.SetError (process_sp->Attach (attach_info));
Johnny Chen535960e2011-06-17 00:51:15 +0000956 // If we are doing synchronous mode, then wait for the
957 // process to stop!
Greg Clayton0416bdf2012-01-30 09:04:36 +0000958 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Clayton334d33a2012-01-30 07:41:31 +0000959 process_sp->WaitForProcessToStop (NULL);
Greg Claytonc5f728c2010-10-06 22:10:17 +0000960 }
961 else
962 {
963 error.SetErrorString ("unable to create lldb_private::Process");
964 }
965 }
966 else
967 {
968 error.SetErrorString ("SBTarget is invalid");
969 }
970 return sb_process;
971
972}
973
974lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000975SBTarget::AttachToProcessWithName
Greg Claytonc5f728c2010-10-06 22:10:17 +0000976(
Greg Clayton271a5db2011-02-03 21:28:34 +0000977 SBListener &listener,
Greg Claytonc5f728c2010-10-06 22:10:17 +0000978 const char *name, // basename of process to attach to
979 bool wait_for, // if true wait for a new instance of "name" to be launched
980 SBError& error // An error explaining what went wrong if attach fails
981)
982{
983 SBProcess sb_process;
Greg Clayton334d33a2012-01-30 07:41:31 +0000984 ProcessSP process_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000985 TargetSP target_sp(GetSP());
986 if (name && target_sp)
Greg Claytonc5f728c2010-10-06 22:10:17 +0000987 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000988 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonbdcda462010-12-20 20:49:23 +0000989
Greg Claytonde1dd812011-06-24 03:21:43 +0000990 StateType state = eStateInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000991 process_sp = target_sp->GetProcessSP();
Greg Clayton334d33a2012-01-30 07:41:31 +0000992 if (process_sp)
Greg Claytonde1dd812011-06-24 03:21:43 +0000993 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000994 state = process_sp->GetState();
Greg Claytonde1dd812011-06-24 03:21:43 +0000995
Greg Clayton334d33a2012-01-30 07:41:31 +0000996 if (process_sp->IsAlive() && state != eStateConnected)
Greg Claytonde1dd812011-06-24 03:21:43 +0000997 {
998 if (state == eStateAttaching)
999 error.SetErrorString ("process attach is in progress");
1000 else
1001 error.SetErrorString ("a process is already being debugged");
Greg Claytonde1dd812011-06-24 03:21:43 +00001002 return sb_process;
1003 }
1004 }
1005
1006 if (state == eStateConnected)
1007 {
1008 // If we are already connected, then we have already specified the
1009 // listener, so if a valid listener is supplied, we need to error out
1010 // to let the client know.
1011 if (listener.IsValid())
1012 {
1013 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Claytonde1dd812011-06-24 03:21:43 +00001014 return sb_process;
1015 }
1016 }
Greg Clayton271a5db2011-02-03 21:28:34 +00001017 else
Greg Claytonde1dd812011-06-24 03:21:43 +00001018 {
1019 if (listener.IsValid())
Greg Clayton46c9a352012-02-09 06:16:32 +00001020 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Claytonde1dd812011-06-24 03:21:43 +00001021 else
Greg Clayton46c9a352012-02-09 06:16:32 +00001022 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Claytonde1dd812011-06-24 03:21:43 +00001023 }
Greg Claytonc5f728c2010-10-06 22:10:17 +00001024
Greg Clayton334d33a2012-01-30 07:41:31 +00001025 if (process_sp)
Greg Claytonc5f728c2010-10-06 22:10:17 +00001026 {
Greg Clayton334d33a2012-01-30 07:41:31 +00001027 sb_process.SetSP (process_sp);
Greg Clayton527154d2011-11-15 03:53:30 +00001028 ProcessAttachInfo attach_info;
1029 attach_info.GetExecutableFile().SetFile(name, false);
1030 attach_info.SetWaitForLaunch(wait_for);
Greg Clayton334d33a2012-01-30 07:41:31 +00001031 error.SetError (process_sp->Attach (attach_info));
Johnny Chen58d02ff2011-06-17 19:21:30 +00001032 // If we are doing synchronous mode, then wait for the
1033 // process to stop!
Greg Clayton0416bdf2012-01-30 09:04:36 +00001034 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Clayton334d33a2012-01-30 07:41:31 +00001035 process_sp->WaitForProcessToStop (NULL);
Greg Claytonc5f728c2010-10-06 22:10:17 +00001036 }
1037 else
1038 {
1039 error.SetErrorString ("unable to create lldb_private::Process");
1040 }
1041 }
1042 else
1043 {
1044 error.SetErrorString ("SBTarget is invalid");
1045 }
1046 return sb_process;
1047
1048}
1049
James McIlree38093402011-03-04 00:31:13 +00001050lldb::SBProcess
1051SBTarget::ConnectRemote
1052(
1053 SBListener &listener,
1054 const char *url,
1055 const char *plugin_name,
1056 SBError& error
1057)
1058{
1059 SBProcess sb_process;
Greg Clayton334d33a2012-01-30 07:41:31 +00001060 ProcessSP process_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001061 TargetSP target_sp(GetSP());
1062 if (target_sp)
James McIlree38093402011-03-04 00:31:13 +00001063 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001064 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree38093402011-03-04 00:31:13 +00001065 if (listener.IsValid())
Greg Clayton46c9a352012-02-09 06:16:32 +00001066 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree38093402011-03-04 00:31:13 +00001067 else
Greg Clayton46c9a352012-02-09 06:16:32 +00001068 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
James McIlree38093402011-03-04 00:31:13 +00001069
1070
Greg Clayton334d33a2012-01-30 07:41:31 +00001071 if (process_sp)
James McIlree38093402011-03-04 00:31:13 +00001072 {
Greg Clayton334d33a2012-01-30 07:41:31 +00001073 sb_process.SetSP (process_sp);
1074 error.SetError (process_sp->ConnectRemote (url));
James McIlree38093402011-03-04 00:31:13 +00001075 }
1076 else
1077 {
1078 error.SetErrorString ("unable to create lldb_private::Process");
1079 }
1080 }
1081 else
1082 {
1083 error.SetErrorString ("SBTarget is invalid");
1084 }
1085 return sb_process;
1086}
1087
Chris Lattner24943d22010-06-08 16:52:24 +00001088SBFileSpec
1089SBTarget::GetExecutable ()
1090{
Caroline Tice7826c882010-10-26 03:11:13 +00001091
Chris Lattner24943d22010-06-08 16:52:24 +00001092 SBFileSpec exe_file_spec;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001093 TargetSP target_sp(GetSP());
1094 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001095 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001096 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Clayton5beb99d2011-08-11 02:48:45 +00001097 if (exe_module)
1098 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner24943d22010-06-08 16:52:24 +00001099 }
Caroline Tice7826c882010-10-26 03:11:13 +00001100
Greg Claytone005f2c2010-11-06 01:53:30 +00001101 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001102 if (log)
1103 {
Greg Clayton49ce6822010-10-31 03:01:06 +00001104 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001105 target_sp.get(), exe_file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001106 }
1107
Chris Lattner24943d22010-06-08 16:52:24 +00001108 return exe_file_spec;
1109}
1110
Chris Lattner24943d22010-06-08 16:52:24 +00001111bool
Chris Lattner24943d22010-06-08 16:52:24 +00001112SBTarget::operator == (const SBTarget &rhs) const
1113{
Greg Clayton63094e02010-06-23 01:19:29 +00001114 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +00001115}
1116
1117bool
1118SBTarget::operator != (const SBTarget &rhs) const
1119{
Greg Clayton63094e02010-06-23 01:19:29 +00001120 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +00001121}
1122
Greg Clayton334d33a2012-01-30 07:41:31 +00001123lldb::TargetSP
1124SBTarget::GetSP () const
Greg Clayton15afa9f2011-10-01 02:59:24 +00001125{
1126 return m_opaque_sp;
1127}
1128
Greg Clayton63094e02010-06-23 01:19:29 +00001129void
Greg Clayton334d33a2012-01-30 07:41:31 +00001130SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton63094e02010-06-23 01:19:29 +00001131{
1132 m_opaque_sp = target_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001133}
1134
Greg Claytona3955062011-07-22 16:46:35 +00001135lldb::SBAddress
1136SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonea49cc72010-12-12 19:25:26 +00001137{
Greg Claytona3955062011-07-22 16:46:35 +00001138 lldb::SBAddress sb_addr;
1139 Address &addr = sb_addr.ref();
Greg Clayton0416bdf2012-01-30 09:04:36 +00001140 TargetSP target_sp(GetSP());
1141 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001142 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001143 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1144 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
Greg Claytona3955062011-07-22 16:46:35 +00001145 return sb_addr;
Greg Claytonbdcda462010-12-20 20:49:23 +00001146 }
Greg Claytonea49cc72010-12-12 19:25:26 +00001147
Greg Claytona3955062011-07-22 16:46:35 +00001148 // We have a load address that isn't in a section, just return an address
1149 // with the offset filled in (the address) and the section set to NULL
Greg Clayton3508c382012-02-24 01:59:29 +00001150 addr.SetRawAddress(vm_addr);
Greg Claytona3955062011-07-22 16:46:35 +00001151 return sb_addr;
Greg Claytonea49cc72010-12-12 19:25:26 +00001152}
1153
Greg Claytonafb81862011-03-02 21:34:46 +00001154SBSymbolContext
1155SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1156{
1157 SBSymbolContext sc;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001158 if (addr.IsValid())
1159 {
1160 TargetSP target_sp(GetSP());
1161 if (target_sp)
1162 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1163 }
Greg Claytonafb81862011-03-02 21:34:46 +00001164 return sc;
1165}
1166
1167
Chris Lattner24943d22010-06-08 16:52:24 +00001168SBBreakpoint
1169SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1170{
Greg Claytond6d806c2010-11-08 00:28:40 +00001171 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner24943d22010-06-08 16:52:24 +00001172}
1173
1174SBBreakpoint
1175SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1176{
Greg Claytone005f2c2010-11-06 01:53:30 +00001177 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001178
Chris Lattner24943d22010-06-08 16:52:24 +00001179 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001180 TargetSP target_sp(GetSP());
1181 if (target_sp && line != 0)
Greg Claytonbdcda462010-12-20 20:49:23 +00001182 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001183 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001184
1185 const bool check_inlines = true;
1186 const bool internal = false;
1187 const LazyBool skip_prologue = eLazyBoolCalculate;
1188 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
Greg Claytonbdcda462010-12-20 20:49:23 +00001189 }
Caroline Tice7826c882010-10-26 03:11:13 +00001190
1191 if (log)
1192 {
1193 SBStream sstr;
1194 sb_bp.GetDescription (sstr);
Greg Clayton49ce6822010-10-31 03:01:06 +00001195 char path[PATH_MAX];
1196 sb_file_spec->GetPath (path, sizeof(path));
1197 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001198 target_sp.get(),
Greg Clayton49ce6822010-10-31 03:01:06 +00001199 path,
Greg Claytona66ba462010-10-30 04:51:46 +00001200 line,
Greg Clayton49ce6822010-10-31 03:01:06 +00001201 sb_bp.get(),
Caroline Tice7826c882010-10-26 03:11:13 +00001202 sstr.GetData());
1203 }
1204
Chris Lattner24943d22010-06-08 16:52:24 +00001205 return sb_bp;
1206}
1207
1208SBBreakpoint
1209SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1210{
Greg Claytone005f2c2010-11-06 01:53:30 +00001211 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001212
Chris Lattner24943d22010-06-08 16:52:24 +00001213 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001214 TargetSP target_sp(GetSP());
1215 if (target_sp.get())
Chris Lattner24943d22010-06-08 16:52:24 +00001216 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001217 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001218
1219 const bool internal = false;
1220 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner24943d22010-06-08 16:52:24 +00001221 if (module_name && module_name[0])
1222 {
Jim Ingham03c8ee52011-09-21 01:17:13 +00001223 FileSpecList module_spec_list;
1224 module_spec_list.Append (FileSpec (module_name, false));
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001225 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner24943d22010-06-08 16:52:24 +00001226 }
1227 else
1228 {
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001229 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner24943d22010-06-08 16:52:24 +00001230 }
1231 }
Caroline Tice7826c882010-10-26 03:11:13 +00001232
1233 if (log)
1234 {
Greg Clayton49ce6822010-10-31 03:01:06 +00001235 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001236 target_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001237 }
1238
Chris Lattner24943d22010-06-08 16:52:24 +00001239 return sb_bp;
1240}
1241
Jim Inghamd6d47972011-09-23 00:54:11 +00001242lldb::SBBreakpoint
1243SBTarget::BreakpointCreateByName (const char *symbol_name,
1244 const SBFileSpecList &module_list,
1245 const SBFileSpecList &comp_unit_list)
1246{
Jim Ingham1fb8a2d2011-10-11 01:18:55 +00001247 uint32_t name_type_mask = eFunctionNameTypeAuto;
1248 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1249}
1250
1251lldb::SBBreakpoint
1252SBTarget::BreakpointCreateByName (const char *symbol_name,
1253 uint32_t name_type_mask,
1254 const SBFileSpecList &module_list,
1255 const SBFileSpecList &comp_unit_list)
1256{
Jim Inghamd6d47972011-09-23 00:54:11 +00001257 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1258
1259 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001260 TargetSP target_sp(GetSP());
1261 if (target_sp && symbol_name && symbol_name[0])
Jim Inghamd6d47972011-09-23 00:54:11 +00001262 {
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001263 const bool internal = false;
1264 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001265 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1266 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Jim Inghamd6d47972011-09-23 00:54:11 +00001267 comp_unit_list.get(),
1268 symbol_name,
Jim Ingham1fb8a2d2011-10-11 01:18:55 +00001269 name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001270 skip_prologue,
1271 internal);
Jim Inghamd6d47972011-09-23 00:54:11 +00001272 }
1273
1274 if (log)
1275 {
Jim Ingham1fb8a2d2011-10-11 01:18:55 +00001276 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001277 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
Jim Inghamd6d47972011-09-23 00:54:11 +00001278 }
1279
1280 return sb_bp;
1281}
1282
Jim Ingham4722b102012-03-06 00:37:27 +00001283lldb::SBBreakpoint
1284SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1285 uint32_t num_names,
1286 uint32_t name_type_mask,
1287 const SBFileSpecList &module_list,
1288 const SBFileSpecList &comp_unit_list)
1289{
1290 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1291
1292 SBBreakpoint sb_bp;
1293 TargetSP target_sp(GetSP());
1294 if (target_sp && num_names > 0)
1295 {
1296 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001297 const bool internal = false;
1298 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Ingham4722b102012-03-06 00:37:27 +00001299 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1300 comp_unit_list.get(),
1301 symbol_names,
1302 num_names,
1303 name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001304 skip_prologue,
1305 internal);
Jim Ingham4722b102012-03-06 00:37:27 +00001306 }
1307
1308 if (log)
1309 {
1310 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1311 for (uint32_t i = 0 ; i < num_names; i++)
1312 {
1313 char sep;
1314 if (i < num_names - 1)
1315 sep = ',';
1316 else
1317 sep = '}';
1318 if (symbol_names[i] != NULL)
1319 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1320 else
1321 log->Printf ("\"<NULL>\"%c ", sep);
1322
1323 }
1324 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1325 }
1326
1327 return sb_bp;
1328}
Jim Inghamd6d47972011-09-23 00:54:11 +00001329
Chris Lattner24943d22010-06-08 16:52:24 +00001330SBBreakpoint
1331SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1332{
Greg Claytone005f2c2010-11-06 01:53:30 +00001333 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001334
Chris Lattner24943d22010-06-08 16:52:24 +00001335 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001336 TargetSP target_sp(GetSP());
1337 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner24943d22010-06-08 16:52:24 +00001338 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001339 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +00001340 RegularExpression regexp(symbol_name_regex);
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001341 const bool internal = false;
1342 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner24943d22010-06-08 16:52:24 +00001343
1344 if (module_name && module_name[0])
1345 {
Jim Ingham03c8ee52011-09-21 01:17:13 +00001346 FileSpecList module_spec_list;
1347 module_spec_list.Append (FileSpec (module_name, false));
Chris Lattner24943d22010-06-08 16:52:24 +00001348
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001349 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
Chris Lattner24943d22010-06-08 16:52:24 +00001350 }
1351 else
1352 {
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001353 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
Chris Lattner24943d22010-06-08 16:52:24 +00001354 }
1355 }
Caroline Tice7826c882010-10-26 03:11:13 +00001356
1357 if (log)
1358 {
Greg Clayton49ce6822010-10-31 03:01:06 +00001359 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001360 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001361 }
1362
Chris Lattner24943d22010-06-08 16:52:24 +00001363 return sb_bp;
1364}
1365
Jim Inghamd6d47972011-09-23 00:54:11 +00001366lldb::SBBreakpoint
1367SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1368 const SBFileSpecList &module_list,
1369 const SBFileSpecList &comp_unit_list)
1370{
1371 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner24943d22010-06-08 16:52:24 +00001372
Jim Inghamd6d47972011-09-23 00:54:11 +00001373 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001374 TargetSP target_sp(GetSP());
1375 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Inghamd6d47972011-09-23 00:54:11 +00001376 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001377 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghamd6d47972011-09-23 00:54:11 +00001378 RegularExpression regexp(symbol_name_regex);
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001379 const bool internal = false;
1380 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamd6d47972011-09-23 00:54:11 +00001381
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001382 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
Jim Inghamd6d47972011-09-23 00:54:11 +00001383 }
1384
1385 if (log)
1386 {
1387 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001388 target_sp.get(), symbol_name_regex, sb_bp.get());
Jim Inghamd6d47972011-09-23 00:54:11 +00001389 }
1390
1391 return sb_bp;
1392}
Chris Lattner24943d22010-06-08 16:52:24 +00001393
1394SBBreakpoint
1395SBTarget::BreakpointCreateByAddress (addr_t address)
1396{
Greg Claytone005f2c2010-11-06 01:53:30 +00001397 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001398
Chris Lattner24943d22010-06-08 16:52:24 +00001399 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001400 TargetSP target_sp(GetSP());
1401 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001402 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001403 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1404 *sb_bp = target_sp->CreateBreakpoint (address, false);
Greg Claytonbdcda462010-12-20 20:49:23 +00001405 }
Caroline Tice7826c882010-10-26 03:11:13 +00001406
1407 if (log)
1408 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001409 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%llu) => SBBreakpoint(%p)", target_sp.get(), (uint64_t) address, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001410 }
1411
Chris Lattner24943d22010-06-08 16:52:24 +00001412 return sb_bp;
1413}
1414
Jim Ingham03c8ee52011-09-21 01:17:13 +00001415lldb::SBBreakpoint
1416SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1417{
1418 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1419
1420 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001421 TargetSP target_sp(GetSP());
1422 if (target_sp && source_regex && source_regex[0])
Jim Ingham03c8ee52011-09-21 01:17:13 +00001423 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001424 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham03c8ee52011-09-21 01:17:13 +00001425 RegularExpression regexp(source_regex);
Jim Inghamd6d47972011-09-23 00:54:11 +00001426 FileSpecList source_file_spec_list;
1427 source_file_spec_list.Append (source_file.ref());
Jim Ingham03c8ee52011-09-21 01:17:13 +00001428
1429 if (module_name && module_name[0])
1430 {
1431 FileSpecList module_spec_list;
1432 module_spec_list.Append (FileSpec (module_name, false));
1433
Greg Clayton0416bdf2012-01-30 09:04:36 +00001434 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
Jim Ingham03c8ee52011-09-21 01:17:13 +00001435 }
1436 else
1437 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001438 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
Jim Ingham03c8ee52011-09-21 01:17:13 +00001439 }
1440 }
1441
1442 if (log)
1443 {
1444 char path[PATH_MAX];
1445 source_file->GetPath (path, sizeof(path));
1446 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001447 target_sp.get(), source_regex, path, module_name, sb_bp.get());
Jim Ingham03c8ee52011-09-21 01:17:13 +00001448 }
1449
1450 return sb_bp;
1451}
1452
Jim Inghamd6d47972011-09-23 00:54:11 +00001453lldb::SBBreakpoint
1454SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1455 const SBFileSpecList &module_list,
1456 const lldb::SBFileSpecList &source_file_list)
1457{
1458 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1459
1460 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001461 TargetSP target_sp(GetSP());
1462 if (target_sp && source_regex && source_regex[0])
Jim Inghamd6d47972011-09-23 00:54:11 +00001463 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001464 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghamd6d47972011-09-23 00:54:11 +00001465 RegularExpression regexp(source_regex);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001466 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
Jim Inghamd6d47972011-09-23 00:54:11 +00001467 }
1468
1469 if (log)
1470 {
1471 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001472 target_sp.get(), source_regex, sb_bp.get());
Jim Inghamd6d47972011-09-23 00:54:11 +00001473 }
1474
1475 return sb_bp;
1476}
Jim Ingham03c8ee52011-09-21 01:17:13 +00001477
Jim Ingham4722b102012-03-06 00:37:27 +00001478lldb::SBBreakpoint
1479SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1480 bool catch_bp,
1481 bool throw_bp)
1482{
1483 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1484
1485 SBBreakpoint sb_bp;
1486 TargetSP target_sp(GetSP());
1487 if (target_sp)
1488 {
1489 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1490 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1491 }
1492
1493 if (log)
1494 {
1495 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1496 target_sp.get(),
1497 LanguageRuntime::GetNameForLanguageType(language),
1498 catch_bp ? "on" : "off",
1499 throw_bp ? "on" : "off",
1500 sb_bp.get());
1501 }
1502
1503 return sb_bp;
1504}
1505
Greg Claytonc7f5d5c2010-07-23 23:33:17 +00001506uint32_t
1507SBTarget::GetNumBreakpoints () const
1508{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001509 TargetSP target_sp(GetSP());
1510 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001511 {
1512 // The breakpoint list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001513 return target_sp->GetBreakpointList().GetSize();
Greg Claytonbdcda462010-12-20 20:49:23 +00001514 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +00001515 return 0;
1516}
1517
1518SBBreakpoint
1519SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1520{
1521 SBBreakpoint sb_breakpoint;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001522 TargetSP target_sp(GetSP());
1523 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001524 {
1525 // The breakpoint list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001526 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonbdcda462010-12-20 20:49:23 +00001527 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +00001528 return sb_breakpoint;
1529}
Chris Lattner24943d22010-06-08 16:52:24 +00001530
1531bool
1532SBTarget::BreakpointDelete (break_id_t bp_id)
1533{
Greg Claytone005f2c2010-11-06 01:53:30 +00001534 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001535
Caroline Tice7826c882010-10-26 03:11:13 +00001536 bool result = false;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001537 TargetSP target_sp(GetSP());
1538 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001539 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001540 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1541 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +00001542 }
Caroline Tice7826c882010-10-26 03:11:13 +00001543
1544 if (log)
1545 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001546 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
Caroline Tice7826c882010-10-26 03:11:13 +00001547 }
1548
1549 return result;
Chris Lattner24943d22010-06-08 16:52:24 +00001550}
1551
Johnny Chen096c2932011-09-26 22:40:50 +00001552SBBreakpoint
1553SBTarget::FindBreakpointByID (break_id_t bp_id)
1554{
1555 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1556
1557 SBBreakpoint sb_breakpoint;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001558 TargetSP target_sp(GetSP());
1559 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen096c2932011-09-26 22:40:50 +00001560 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001561 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1562 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen096c2932011-09-26 22:40:50 +00001563 }
1564
1565 if (log)
1566 {
1567 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001568 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Johnny Chen096c2932011-09-26 22:40:50 +00001569 }
1570
1571 return sb_breakpoint;
1572}
1573
Chris Lattner24943d22010-06-08 16:52:24 +00001574bool
1575SBTarget::EnableAllBreakpoints ()
1576{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001577 TargetSP target_sp(GetSP());
1578 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001579 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001580 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1581 target_sp->EnableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +00001582 return true;
1583 }
1584 return false;
1585}
1586
1587bool
1588SBTarget::DisableAllBreakpoints ()
1589{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001590 TargetSP target_sp(GetSP());
1591 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001592 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001593 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1594 target_sp->DisableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +00001595 return true;
1596 }
1597 return false;
1598}
1599
1600bool
1601SBTarget::DeleteAllBreakpoints ()
1602{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001603 TargetSP target_sp(GetSP());
1604 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001605 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001606 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1607 target_sp->RemoveAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +00001608 return true;
1609 }
1610 return false;
1611}
1612
Johnny Chen096c2932011-09-26 22:40:50 +00001613uint32_t
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001614SBTarget::GetNumWatchpoints () const
Johnny Chen096c2932011-09-26 22:40:50 +00001615{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001616 TargetSP target_sp(GetSP());
1617 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001618 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001619 // The watchpoint list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001620 return target_sp->GetWatchpointList().GetSize();
Johnny Chen096c2932011-09-26 22:40:50 +00001621 }
1622 return 0;
1623}
1624
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001625SBWatchpoint
1626SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen5eb54bb2011-09-27 20:29:45 +00001627{
Johnny Chenecd4feb2011-10-14 00:42:25 +00001628 SBWatchpoint sb_watchpoint;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001629 TargetSP target_sp(GetSP());
1630 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001631 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001632 // The watchpoint list is thread safe, no need to lock
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001633 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen096c2932011-09-26 22:40:50 +00001634 }
Johnny Chenecd4feb2011-10-14 00:42:25 +00001635 return sb_watchpoint;
Johnny Chen096c2932011-09-26 22:40:50 +00001636}
1637
1638bool
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001639SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen096c2932011-09-26 22:40:50 +00001640{
1641 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1642
1643 bool result = false;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001644 TargetSP target_sp(GetSP());
1645 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001646 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001647 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001648 Mutex::Locker locker;
1649 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001650 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen096c2932011-09-26 22:40:50 +00001651 }
1652
1653 if (log)
1654 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001655 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
Johnny Chen096c2932011-09-26 22:40:50 +00001656 }
1657
1658 return result;
1659}
1660
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001661SBWatchpoint
1662SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen096c2932011-09-26 22:40:50 +00001663{
1664 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1665
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001666 SBWatchpoint sb_watchpoint;
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001667 lldb::WatchpointSP watchpoint_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001668 TargetSP target_sp(GetSP());
1669 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen096c2932011-09-26 22:40:50 +00001670 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001671 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001672 Mutex::Locker locker;
1673 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001674 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1675 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen096c2932011-09-26 22:40:50 +00001676 }
1677
1678 if (log)
1679 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001680 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001681 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
Johnny Chen096c2932011-09-26 22:40:50 +00001682 }
1683
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001684 return sb_watchpoint;
1685}
1686
1687lldb::SBWatchpoint
Johnny Chen3f883492012-06-04 23:19:54 +00001688SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001689{
1690 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1691
1692 SBWatchpoint sb_watchpoint;
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001693 lldb::WatchpointSP watchpoint_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001694 TargetSP target_sp(GetSP());
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001695 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001696 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001697 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001698 uint32_t watch_type = 0;
1699 if (read)
1700 watch_type |= LLDB_WATCH_TYPE_READ;
1701 if (write)
1702 watch_type |= LLDB_WATCH_TYPE_WRITE;
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001703 // Target::CreateWatchpoint() is thread safe.
Johnny Chen3f883492012-06-04 23:19:54 +00001704 Error cw_error;
1705 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, watch_type, cw_error);
1706 error.SetError(cw_error);
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001707 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001708 }
1709
1710 if (log)
1711 {
1712 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%llx, 0x%u) => SBWatchpoint(%p)",
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001713 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001714 }
1715
1716 return sb_watchpoint;
Johnny Chen096c2932011-09-26 22:40:50 +00001717}
1718
1719bool
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001720SBTarget::EnableAllWatchpoints ()
Johnny Chen096c2932011-09-26 22:40:50 +00001721{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001722 TargetSP target_sp(GetSP());
1723 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001724 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001725 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001726 Mutex::Locker locker;
1727 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001728 target_sp->EnableAllWatchpoints ();
Johnny Chen096c2932011-09-26 22:40:50 +00001729 return true;
1730 }
1731 return false;
1732}
1733
1734bool
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001735SBTarget::DisableAllWatchpoints ()
Johnny Chen096c2932011-09-26 22:40:50 +00001736{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001737 TargetSP target_sp(GetSP());
1738 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001739 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001740 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001741 Mutex::Locker locker;
1742 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001743 target_sp->DisableAllWatchpoints ();
Johnny Chen096c2932011-09-26 22:40:50 +00001744 return true;
1745 }
1746 return false;
1747}
1748
1749bool
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001750SBTarget::DeleteAllWatchpoints ()
Johnny Chen096c2932011-09-26 22:40:50 +00001751{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001752 TargetSP target_sp(GetSP());
1753 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001754 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001755 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001756 Mutex::Locker locker;
1757 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001758 target_sp->RemoveAllWatchpoints ();
Johnny Chen096c2932011-09-26 22:40:50 +00001759 return true;
1760 }
1761 return false;
1762}
1763
Chris Lattner24943d22010-06-08 16:52:24 +00001764
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001765lldb::SBModule
1766SBTarget::AddModule (const char *path,
1767 const char *triple,
1768 const char *uuid_cstr)
1769{
Greg Claytonf0bc8152012-04-23 20:23:39 +00001770 return AddModule (path, triple, uuid_cstr, NULL);
1771}
1772
1773lldb::SBModule
1774SBTarget::AddModule (const char *path,
1775 const char *triple,
1776 const char *uuid_cstr,
1777 const char *symfile)
1778{
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001779 lldb::SBModule sb_module;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001780 TargetSP target_sp(GetSP());
1781 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001782 {
Greg Clayton444fe992012-02-26 05:51:37 +00001783 ModuleSpec module_spec;
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001784 if (path)
Greg Clayton444fe992012-02-26 05:51:37 +00001785 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonf0bc8152012-04-23 20:23:39 +00001786
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001787 if (uuid_cstr)
Greg Clayton444fe992012-02-26 05:51:37 +00001788 module_spec.GetUUID().SetfromCString(uuid_cstr);
Greg Claytonf0bc8152012-04-23 20:23:39 +00001789
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001790 if (triple)
Greg Clayton444fe992012-02-26 05:51:37 +00001791 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Greg Claytonf0bc8152012-04-23 20:23:39 +00001792
1793 if (symfile)
1794 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001795
Greg Clayton444fe992012-02-26 05:51:37 +00001796 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001797 }
1798 return sb_module;
1799}
1800
1801bool
1802SBTarget::AddModule (lldb::SBModule &module)
1803{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001804 TargetSP target_sp(GetSP());
1805 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001806 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001807 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001808 return true;
1809 }
1810 return false;
1811}
1812
Chris Lattner24943d22010-06-08 16:52:24 +00001813uint32_t
1814SBTarget::GetNumModules () const
1815{
Greg Claytone005f2c2010-11-06 01:53:30 +00001816 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001817
Caroline Tice7826c882010-10-26 03:11:13 +00001818 uint32_t num = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001819 TargetSP target_sp(GetSP());
1820 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001821 {
1822 // The module list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001823 num = target_sp->GetImages().GetSize();
Greg Claytonbdcda462010-12-20 20:49:23 +00001824 }
Caroline Tice7826c882010-10-26 03:11:13 +00001825
1826 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +00001827 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
Caroline Tice7826c882010-10-26 03:11:13 +00001828
1829 return num;
Chris Lattner24943d22010-06-08 16:52:24 +00001830}
1831
Greg Clayton43490d12010-07-30 20:12:55 +00001832void
1833SBTarget::Clear ()
1834{
Greg Claytone005f2c2010-11-06 01:53:30 +00001835 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001836
1837 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +00001838 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001839
Greg Clayton43490d12010-07-30 20:12:55 +00001840 m_opaque_sp.reset();
1841}
1842
1843
Chris Lattner24943d22010-06-08 16:52:24 +00001844SBModule
1845SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1846{
1847 SBModule sb_module;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001848 TargetSP target_sp(GetSP());
1849 if (target_sp && sb_file_spec.IsValid())
Greg Claytonbdcda462010-12-20 20:49:23 +00001850 {
Greg Clayton444fe992012-02-26 05:51:37 +00001851 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonbdcda462010-12-20 20:49:23 +00001852 // The module list is thread safe, no need to lock
Greg Clayton444fe992012-02-26 05:51:37 +00001853 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonbdcda462010-12-20 20:49:23 +00001854 }
Chris Lattner24943d22010-06-08 16:52:24 +00001855 return sb_module;
1856}
1857
Greg Clayton1b925202012-01-29 06:07:39 +00001858lldb::ByteOrder
1859SBTarget::GetByteOrder ()
1860{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001861 TargetSP target_sp(GetSP());
1862 if (target_sp)
1863 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton1b925202012-01-29 06:07:39 +00001864 return eByteOrderInvalid;
1865}
1866
1867const char *
1868SBTarget::GetTriple ()
1869{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001870 TargetSP target_sp(GetSP());
1871 if (target_sp)
Greg Clayton1b925202012-01-29 06:07:39 +00001872 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001873 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton1b925202012-01-29 06:07:39 +00001874 // Unique the string so we don't run into ownership issues since
1875 // the const strings put the string into the string pool once and
1876 // the strings never comes out
1877 ConstString const_triple (triple.c_str());
1878 return const_triple.GetCString();
1879 }
1880 return NULL;
1881}
1882
1883uint32_t
1884SBTarget::GetAddressByteSize()
1885{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001886 TargetSP target_sp(GetSP());
1887 if (target_sp)
1888 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1b925202012-01-29 06:07:39 +00001889 return sizeof(void*);
1890}
1891
1892
Chris Lattner24943d22010-06-08 16:52:24 +00001893SBModule
1894SBTarget::GetModuleAtIndex (uint32_t idx)
1895{
Greg Claytone005f2c2010-11-06 01:53:30 +00001896 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001897
Chris Lattner24943d22010-06-08 16:52:24 +00001898 SBModule sb_module;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001899 ModuleSP module_sp;
1900 TargetSP target_sp(GetSP());
1901 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001902 {
1903 // The module list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001904 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1905 sb_module.SetSP (module_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +00001906 }
Caroline Tice7826c882010-10-26 03:11:13 +00001907
1908 if (log)
1909 {
Greg Clayton49ce6822010-10-31 03:01:06 +00001910 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001911 target_sp.get(), idx, module_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001912 }
1913
Chris Lattner24943d22010-06-08 16:52:24 +00001914 return sb_module;
1915}
1916
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001917bool
1918SBTarget::RemoveModule (lldb::SBModule module)
1919{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001920 TargetSP target_sp(GetSP());
1921 if (target_sp)
1922 return target_sp->GetImages().Remove(module.GetSP());
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001923 return false;
1924}
1925
Chris Lattner24943d22010-06-08 16:52:24 +00001926
1927SBBroadcaster
1928SBTarget::GetBroadcaster () const
1929{
Greg Claytone005f2c2010-11-06 01:53:30 +00001930 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001931
Greg Clayton0416bdf2012-01-30 09:04:36 +00001932 TargetSP target_sp(GetSP());
1933 SBBroadcaster broadcaster(target_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +00001934
1935 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +00001936 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001937 target_sp.get(), broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001938
Chris Lattner24943d22010-06-08 16:52:24 +00001939 return broadcaster;
1940}
1941
Caroline Tice98f930f2010-09-20 05:20:02 +00001942bool
Caroline Tice7826c882010-10-26 03:11:13 +00001943SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Tice98f930f2010-09-20 05:20:02 +00001944{
Greg Clayton96154be2011-11-13 06:57:31 +00001945 Stream &strm = description.ref();
1946
Greg Clayton0416bdf2012-01-30 09:04:36 +00001947 TargetSP target_sp(GetSP());
1948 if (target_sp)
Caroline Ticee7a566e2010-09-20 16:21:41 +00001949 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001950 target_sp->Dump (&strm, description_level);
Caroline Tice7826c882010-10-26 03:11:13 +00001951 }
1952 else
Greg Clayton96154be2011-11-13 06:57:31 +00001953 strm.PutCString ("No value");
Caroline Tice7826c882010-10-26 03:11:13 +00001954
1955 return true;
1956}
1957
Greg Clayton7dd5c512012-02-06 01:44:54 +00001958lldb::SBSymbolContextList
1959SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Clayton4ed315f2011-06-21 01:34:41 +00001960{
Greg Clayton7dd5c512012-02-06 01:44:54 +00001961 lldb::SBSymbolContextList sb_sc_list;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001962 if (name && name[0])
Greg Clayton4ed315f2011-06-21 01:34:41 +00001963 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001964 TargetSP target_sp(GetSP());
1965 if (target_sp)
1966 {
1967 const bool symbols_ok = true;
Sean Callanan302d78c2012-02-10 22:52:19 +00001968 const bool inlines_ok = true;
Greg Clayton7dd5c512012-02-06 01:44:54 +00001969 const bool append = true;
1970 target_sp->GetImages().FindFunctions (ConstString(name),
1971 name_type_mask,
Sean Callanan302d78c2012-02-10 22:52:19 +00001972 symbols_ok,
1973 inlines_ok,
Greg Clayton7dd5c512012-02-06 01:44:54 +00001974 append,
1975 *sb_sc_list);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001976 }
Greg Clayton4ed315f2011-06-21 01:34:41 +00001977 }
Greg Clayton7dd5c512012-02-06 01:44:54 +00001978 return sb_sc_list;
Greg Clayton4ed315f2011-06-21 01:34:41 +00001979}
1980
Enrico Granata979e20d2011-07-29 19:53:35 +00001981lldb::SBType
1982SBTarget::FindFirstType (const char* type)
1983{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001984 TargetSP target_sp(GetSP());
1985 if (type && target_sp)
Enrico Granata979e20d2011-07-29 19:53:35 +00001986 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001987 size_t count = target_sp->GetImages().GetSize();
Enrico Granata979e20d2011-07-29 19:53:35 +00001988 for (size_t idx = 0; idx < count; idx++)
1989 {
1990 SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
1991
1992 if (found_at_idx.IsValid())
1993 return found_at_idx;
1994 }
1995 }
1996 return SBType();
1997}
1998
1999lldb::SBTypeList
2000SBTarget::FindTypes (const char* type)
2001{
2002
2003 SBTypeList retval;
2004
Greg Clayton0416bdf2012-01-30 09:04:36 +00002005 TargetSP target_sp(GetSP());
2006 if (type && target_sp)
Enrico Granata979e20d2011-07-29 19:53:35 +00002007 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002008 ModuleList& images = target_sp->GetImages();
Enrico Granata979e20d2011-07-29 19:53:35 +00002009 ConstString name_const(type);
Greg Claytondc0a38c2012-03-26 23:03:23 +00002010 bool exact_match = false;
Enrico Granata979e20d2011-07-29 19:53:35 +00002011 SymbolContext sc;
2012 TypeList type_list;
2013
Greg Clayton9f95fb62012-04-06 17:41:13 +00002014 uint32_t num_matches = images.FindTypes (sc,
Greg Claytondc0a38c2012-03-26 23:03:23 +00002015 name_const,
2016 exact_match,
2017 UINT32_MAX,
2018 type_list);
Enrico Granata979e20d2011-07-29 19:53:35 +00002019
2020 for (size_t idx = 0; idx < num_matches; idx++)
2021 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +00002022 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2023 if (type_sp)
2024 retval.Append(SBType(type_sp));
Enrico Granata979e20d2011-07-29 19:53:35 +00002025 }
2026 }
2027 return retval;
2028}
2029
Greg Clayton917c0002011-06-29 22:09:02 +00002030SBValueList
2031SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2032{
2033 SBValueList sb_value_list;
2034
Greg Clayton0416bdf2012-01-30 09:04:36 +00002035 TargetSP target_sp(GetSP());
2036 if (name && target_sp)
Greg Clayton917c0002011-06-29 22:09:02 +00002037 {
2038 VariableList variable_list;
2039 const bool append = true;
Greg Clayton0416bdf2012-01-30 09:04:36 +00002040 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2041 append,
2042 max_matches,
2043 variable_list);
Greg Clayton917c0002011-06-29 22:09:02 +00002044
2045 if (match_count > 0)
2046 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002047 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Clayton917c0002011-06-29 22:09:02 +00002048 if (exe_scope == NULL)
Greg Clayton0416bdf2012-01-30 09:04:36 +00002049 exe_scope = target_sp.get();
Greg Clayton917c0002011-06-29 22:09:02 +00002050 ValueObjectList &value_object_list = sb_value_list.ref();
2051 for (uint32_t i=0; i<match_count; ++i)
2052 {
2053 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2054 if (valobj_sp)
2055 value_object_list.Append(valobj_sp);
2056 }
2057 }
2058 }
2059
2060 return sb_value_list;
2061}
2062
Jim Inghamcc637462011-09-13 00:29:56 +00002063SBSourceManager
2064SBTarget::GetSourceManager()
2065{
2066 SBSourceManager source_manager (*this);
2067 return source_manager;
2068}
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002069
Sean Callananef1f6902011-12-14 23:49:37 +00002070lldb::SBInstructionList
Greg Claytona9893072012-03-06 22:24:44 +00002071SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2072{
2073 SBInstructionList sb_instructions;
2074
2075 TargetSP target_sp(GetSP());
2076 if (target_sp)
2077 {
2078 Address *addr_ptr = base_addr.get();
2079
2080 if (addr_ptr)
2081 {
2082 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2083 bool prefer_file_cache = false;
2084 lldb_private::Error error;
2085 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(), data.GetByteSize(), error);
2086 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2087 NULL,
2088 *addr_ptr,
2089 data.GetBytes(),
2090 bytes_read,
2091 count));
2092 }
2093 }
2094
2095 return sb_instructions;
2096
2097}
2098
2099lldb::SBInstructionList
Sean Callananef1f6902011-12-14 23:49:37 +00002100SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2101{
2102 SBInstructionList sb_instructions;
2103
Greg Clayton0416bdf2012-01-30 09:04:36 +00002104 TargetSP target_sp(GetSP());
2105 if (target_sp)
Sean Callananef1f6902011-12-14 23:49:37 +00002106 {
2107 Address addr;
2108
2109 if (base_addr.get())
2110 addr = *base_addr.get();
2111
Greg Clayton0416bdf2012-01-30 09:04:36 +00002112 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callananef1f6902011-12-14 23:49:37 +00002113 NULL,
2114 addr,
2115 buf,
2116 size));
2117 }
2118
2119 return sb_instructions;
2120}
2121
2122lldb::SBInstructionList
2123SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2124{
2125 return GetInstructions (ResolveLoadAddress(base_addr), buf, size);
2126}
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002127
2128SBError
2129SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2130 lldb::addr_t section_base_addr)
2131{
2132 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +00002133 TargetSP target_sp(GetSP());
2134 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002135 {
2136 if (!section.IsValid())
2137 {
2138 sb_error.SetErrorStringWithFormat ("invalid section");
2139 }
2140 else
2141 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002142 SectionSP section_sp (section.GetSP());
2143 if (section_sp)
2144 {
2145 if (section_sp->IsThreadSpecific())
2146 {
2147 sb_error.SetErrorString ("thread specific sections are not yet supported");
2148 }
2149 else
2150 {
Greg Clayton545762f2012-07-07 01:24:12 +00002151 target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr);
Greg Clayton9ab696e2012-03-27 21:10:07 +00002152 }
2153 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002154 }
2155 }
2156 else
2157 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002158 sb_error.SetErrorString ("invalid target");
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002159 }
2160 return sb_error;
2161}
2162
2163SBError
2164SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2165{
2166 SBError sb_error;
2167
Greg Clayton0416bdf2012-01-30 09:04:36 +00002168 TargetSP target_sp(GetSP());
2169 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002170 {
2171 if (!section.IsValid())
2172 {
2173 sb_error.SetErrorStringWithFormat ("invalid section");
2174 }
2175 else
2176 {
Greg Clayton545762f2012-07-07 01:24:12 +00002177 target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP());
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002178 }
2179 }
2180 else
2181 {
2182 sb_error.SetErrorStringWithFormat ("invalid target");
2183 }
2184 return sb_error;
2185}
2186
2187SBError
2188SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2189{
2190 SBError sb_error;
2191
Greg Clayton0416bdf2012-01-30 09:04:36 +00002192 TargetSP target_sp(GetSP());
2193 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002194 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002195 ModuleSP module_sp (module.GetSP());
2196 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002197 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002198 bool changed = false;
2199 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002200 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002201 // The load was successful, make sure that at least some sections
2202 // changed before we notify that our module was loaded.
2203 if (changed)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002204 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002205 ModuleList module_list;
2206 module_list.Append(module_sp);
2207 target_sp->ModulesDidLoad (module_list);
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002208 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002209 }
2210 }
Greg Clayton0416bdf2012-01-30 09:04:36 +00002211 else
2212 {
2213 sb_error.SetErrorStringWithFormat ("invalid module");
2214 }
2215
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002216 }
2217 else
2218 {
2219 sb_error.SetErrorStringWithFormat ("invalid target");
2220 }
2221 return sb_error;
2222}
2223
2224SBError
2225SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2226{
2227 SBError sb_error;
2228
2229 char path[PATH_MAX];
Greg Clayton0416bdf2012-01-30 09:04:36 +00002230 TargetSP target_sp(GetSP());
2231 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002232 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002233 ModuleSP module_sp (module.GetSP());
2234 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002235 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002236 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002237 if (objfile)
2238 {
2239 SectionList *section_list = objfile->GetSectionList();
2240 if (section_list)
2241 {
2242 const size_t num_sections = section_list->GetSize();
2243 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2244 {
2245 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2246 if (section_sp)
Greg Clayton545762f2012-07-07 01:24:12 +00002247 target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp);
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002248 }
2249 }
2250 else
2251 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002252 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002253 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2254 }
2255 }
2256 else
2257 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002258 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002259 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2260 }
2261 }
Greg Clayton0416bdf2012-01-30 09:04:36 +00002262 else
2263 {
2264 sb_error.SetErrorStringWithFormat ("invalid module");
2265 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002266 }
2267 else
2268 {
2269 sb_error.SetErrorStringWithFormat ("invalid target");
2270 }
2271 return sb_error;
2272}
2273
2274