blob: df02778e184190f3ba36c6d14f30ac3d627488fd [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"
Greg Clayton49ce8962012-08-29 21:13:06 +000034#include "lldb/Core/Module.h"
35#include "lldb/Core/ModuleSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036#include "lldb/Core/RegularExpression.h"
37#include "lldb/Core/SearchFilter.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000038#include "lldb/Core/Section.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039#include "lldb/Core/STLUtils.h"
Greg Clayton917c0002011-06-29 22:09:02 +000040#include "lldb/Core/ValueObjectList.h"
41#include "lldb/Core/ValueObjectVariable.h"
42#include "lldb/Host/FileSpec.h"
Greg Claytoncd548032011-02-01 01:31:41 +000043#include "lldb/Host/Host.h"
Greg Clayton917c0002011-06-29 22:09:02 +000044#include "lldb/Interpreter/Args.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000045#include "lldb/Symbol/ObjectFile.h"
Enrico Granata979e20d2011-07-29 19:53:35 +000046#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton917c0002011-06-29 22:09:02 +000047#include "lldb/Symbol/VariableList.h"
Jim Ingham4722b102012-03-06 00:37:27 +000048#include "lldb/Target/LanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000049#include "lldb/Target/Process.h"
50#include "lldb/Target/Target.h"
51#include "lldb/Target/TargetList.h"
52
53#include "lldb/Interpreter/CommandReturnObject.h"
54#include "../source/Commands/CommandObjectBreakpoint.h"
55
Chris Lattner24943d22010-06-08 16:52:24 +000056
57using namespace lldb;
58using namespace lldb_private;
59
60#define DEFAULT_DISASM_BYTE_SIZE 32
61
Greg Clayton98ca1e62012-02-24 20:59:25 +000062SBLaunchInfo::SBLaunchInfo (const char **argv) :
63 m_opaque_sp(new ProcessLaunchInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +000064{
Greg Clayton98ca1e62012-02-24 20:59:25 +000065 m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
66 if (argv && argv[0])
67 m_opaque_sp->GetArguments().SetArguments(argv);
Greg Clayton0a8dcac2012-02-24 05:03:03 +000068}
69
Greg Clayton66c2e192012-03-07 23:52:51 +000070SBLaunchInfo::~SBLaunchInfo()
71{
72}
73
74lldb_private::ProcessLaunchInfo &
75SBLaunchInfo::ref ()
76{
77 return *m_opaque_sp;
78}
79
80
Greg Clayton0a8dcac2012-02-24 05:03:03 +000081uint32_t
82SBLaunchInfo::GetUserID()
83{
84 return m_opaque_sp->GetUserID();
85}
86
87uint32_t
88SBLaunchInfo::GetGroupID()
89{
90 return m_opaque_sp->GetGroupID();
91}
92
93bool
94SBLaunchInfo::UserIDIsValid ()
95{
96 return m_opaque_sp->UserIDIsValid();
97}
98
99bool
100SBLaunchInfo::GroupIDIsValid ()
101{
102 return m_opaque_sp->GroupIDIsValid();
103}
104
105void
106SBLaunchInfo::SetUserID (uint32_t uid)
107{
108 m_opaque_sp->SetUserID (uid);
109}
110
111void
112SBLaunchInfo::SetGroupID (uint32_t gid)
113{
114 m_opaque_sp->SetGroupID (gid);
115}
116
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000117uint32_t
118SBLaunchInfo::GetNumArguments ()
119{
120 return m_opaque_sp->GetArguments().GetArgumentCount();
121}
122
123const char *
124SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
125{
126 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
127}
128
129void
130SBLaunchInfo::SetArguments (const char **argv, bool append)
131{
132 if (append)
133 {
134 if (argv)
135 m_opaque_sp->GetArguments().AppendArguments(argv);
136 }
137 else
138 {
139 if (argv)
140 m_opaque_sp->GetArguments().SetArguments(argv);
141 else
142 m_opaque_sp->GetArguments().Clear();
143 }
144}
145
146uint32_t
147SBLaunchInfo::GetNumEnvironmentEntries ()
148{
149 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
150}
151
152const char *
153SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
154{
155 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
156}
157
158void
159SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
160{
161 if (append)
162 {
163 if (envp)
164 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
165 }
166 else
167 {
168 if (envp)
169 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
170 else
171 m_opaque_sp->GetEnvironmentEntries().Clear();
172 }
173}
174
175void
176SBLaunchInfo::Clear ()
177{
178 m_opaque_sp->Clear();
179}
180
181const char *
182SBLaunchInfo::GetWorkingDirectory () const
183{
184 return m_opaque_sp->GetWorkingDirectory();
185}
186
187void
188SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
189{
190 m_opaque_sp->SetWorkingDirectory(working_dir);
191}
192
193uint32_t
194SBLaunchInfo::GetLaunchFlags ()
195{
196 return m_opaque_sp->GetFlags().Get();
197}
198
199void
200SBLaunchInfo::SetLaunchFlags (uint32_t flags)
201{
202 m_opaque_sp->GetFlags().Reset(flags);
203}
204
205const char *
206SBLaunchInfo::GetProcessPluginName ()
207{
208 return m_opaque_sp->GetProcessPluginName();
209}
210
211void
212SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
213{
214 return m_opaque_sp->SetProcessPluginName (plugin_name);
215}
216
217const char *
218SBLaunchInfo::GetShell ()
219{
220 return m_opaque_sp->GetShell();
221}
222
223void
224SBLaunchInfo::SetShell (const char * path)
225{
226 m_opaque_sp->SetShell (path);
227}
228
229uint32_t
230SBLaunchInfo::GetResumeCount ()
231{
232 return m_opaque_sp->GetResumeCount();
233}
234
235void
236SBLaunchInfo::SetResumeCount (uint32_t c)
237{
238 m_opaque_sp->SetResumeCount (c);
239}
240
241bool
242SBLaunchInfo::AddCloseFileAction (int fd)
243{
244 return m_opaque_sp->AppendCloseFileAction(fd);
245}
246
247bool
248SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
249{
250 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
251}
252
253bool
254SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
255{
256 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
257}
258
259bool
260SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
261{
262 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
263}
264
265
266SBAttachInfo::SBAttachInfo () :
Greg Clayton66c2e192012-03-07 23:52:51 +0000267 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000268{
269}
270
271SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
Greg Clayton66c2e192012-03-07 23:52:51 +0000272 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000273{
274 m_opaque_sp->SetProcessID (pid);
275}
276
277SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
Greg Clayton66c2e192012-03-07 23:52:51 +0000278 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000279{
280 if (path && path[0])
281 m_opaque_sp->GetExecutableFile().SetFile(path, false);
282 m_opaque_sp->SetWaitForLaunch (wait_for);
283}
284
285SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
Greg Clayton66c2e192012-03-07 23:52:51 +0000286 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000287{
288 *m_opaque_sp = *rhs.m_opaque_sp;
289}
290
Greg Clayton66c2e192012-03-07 23:52:51 +0000291SBAttachInfo::~SBAttachInfo()
292{
293}
294
295lldb_private::ProcessAttachInfo &
296SBAttachInfo::ref ()
297{
298 return *m_opaque_sp;
299}
300
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000301SBAttachInfo &
302SBAttachInfo::operator = (const SBAttachInfo &rhs)
303{
304 if (this != &rhs)
305 *m_opaque_sp = *rhs.m_opaque_sp;
306 return *this;
307}
308
309lldb::pid_t
310SBAttachInfo::GetProcessID ()
311{
312 return m_opaque_sp->GetProcessID();
313}
314
315void
316SBAttachInfo::SetProcessID (lldb::pid_t pid)
317{
318 m_opaque_sp->SetProcessID (pid);
319}
320
321
322uint32_t
323SBAttachInfo::GetResumeCount ()
324{
325 return m_opaque_sp->GetResumeCount();
326}
327
328void
329SBAttachInfo::SetResumeCount (uint32_t c)
330{
331 m_opaque_sp->SetResumeCount (c);
332}
333
334const char *
335SBAttachInfo::GetProcessPluginName ()
336{
337 return m_opaque_sp->GetProcessPluginName();
338}
339
340void
341SBAttachInfo::SetProcessPluginName (const char *plugin_name)
342{
343 return m_opaque_sp->SetProcessPluginName (plugin_name);
344}
345
346void
347SBAttachInfo::SetExecutable (const char *path)
348{
349 if (path && path[0])
350 m_opaque_sp->GetExecutableFile().SetFile(path, false);
351 else
352 m_opaque_sp->GetExecutableFile().Clear();
353}
354
355void
356SBAttachInfo::SetExecutable (SBFileSpec exe_file)
357{
358 if (exe_file.IsValid())
359 m_opaque_sp->GetExecutableFile() = exe_file.ref();
360 else
361 m_opaque_sp->GetExecutableFile().Clear();
362}
363
364bool
365SBAttachInfo::GetWaitForLaunch ()
366{
367 return m_opaque_sp->GetWaitForLaunch();
368}
369
370void
371SBAttachInfo::SetWaitForLaunch (bool b)
372{
373 m_opaque_sp->SetWaitForLaunch (b);
374}
375
Jim Ingham3a458eb2012-07-20 21:37:13 +0000376bool
377SBAttachInfo::GetIgnoreExisting ()
378{
379 return m_opaque_sp->GetIgnoreExisting();
380}
381
382void
383SBAttachInfo::SetIgnoreExisting (bool b)
384{
385 m_opaque_sp->SetIgnoreExisting (b);
386}
387
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000388uint32_t
Greg Clayton80efa5e2012-02-24 23:56:06 +0000389SBAttachInfo::GetUserID()
390{
391 return m_opaque_sp->GetUserID();
392}
393
394uint32_t
395SBAttachInfo::GetGroupID()
396{
397 return m_opaque_sp->GetGroupID();
398}
399
400bool
401SBAttachInfo::UserIDIsValid ()
402{
403 return m_opaque_sp->UserIDIsValid();
404}
405
406bool
407SBAttachInfo::GroupIDIsValid ()
408{
409 return m_opaque_sp->GroupIDIsValid();
410}
411
412void
413SBAttachInfo::SetUserID (uint32_t uid)
414{
415 m_opaque_sp->SetUserID (uid);
416}
417
418void
419SBAttachInfo::SetGroupID (uint32_t gid)
420{
421 m_opaque_sp->SetGroupID (gid);
422}
423
424uint32_t
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000425SBAttachInfo::GetEffectiveUserID()
426{
427 return m_opaque_sp->GetEffectiveUserID();
428}
429
430uint32_t
431SBAttachInfo::GetEffectiveGroupID()
432{
433 return m_opaque_sp->GetEffectiveGroupID();
434}
435
436bool
437SBAttachInfo::EffectiveUserIDIsValid ()
438{
439 return m_opaque_sp->EffectiveUserIDIsValid();
440}
441
442bool
443SBAttachInfo::EffectiveGroupIDIsValid ()
444{
445 return m_opaque_sp->EffectiveGroupIDIsValid ();
446}
447
448void
449SBAttachInfo::SetEffectiveUserID (uint32_t uid)
450{
451 m_opaque_sp->SetEffectiveUserID(uid);
452}
453
454void
455SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
456{
457 m_opaque_sp->SetEffectiveGroupID(gid);
458}
459
460lldb::pid_t
461SBAttachInfo::GetParentProcessID ()
462{
463 return m_opaque_sp->GetParentProcessID();
464}
465
466void
467SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
468{
469 m_opaque_sp->SetParentProcessID (pid);
470}
471
472bool
473SBAttachInfo::ParentProcessIDIsValid()
474{
475 return m_opaque_sp->ParentProcessIDIsValid();
476}
477
478
Chris Lattner24943d22010-06-08 16:52:24 +0000479//----------------------------------------------------------------------
480// SBTarget constructor
481//----------------------------------------------------------------------
Greg Claytonc3b61d22010-12-15 05:08:08 +0000482SBTarget::SBTarget () :
483 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +0000484{
485}
486
487SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +0000488 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000489{
490}
491
492SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +0000493 m_opaque_sp (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000494{
495}
496
Greg Clayton538eb822010-11-05 23:17:00 +0000497const SBTarget&
498SBTarget::operator = (const SBTarget& rhs)
499{
500 if (this != &rhs)
501 m_opaque_sp = rhs.m_opaque_sp;
502 return *this;
503}
504
Chris Lattner24943d22010-06-08 16:52:24 +0000505//----------------------------------------------------------------------
506// Destructor
507//----------------------------------------------------------------------
508SBTarget::~SBTarget()
509{
510}
511
Jim Ingham5a15e692012-02-16 06:50:00 +0000512const char *
513SBTarget::GetBroadcasterClassName ()
514{
515 return Target::GetStaticBroadcasterClass().AsCString();
516}
517
Chris Lattner24943d22010-06-08 16:52:24 +0000518bool
519SBTarget::IsValid () const
520{
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000521 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000522}
523
524SBProcess
525SBTarget::GetProcess ()
526{
527 SBProcess sb_process;
Greg Clayton334d33a2012-01-30 07:41:31 +0000528 ProcessSP process_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000529 TargetSP target_sp(GetSP());
530 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000531 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000532 process_sp = target_sp->GetProcessSP();
Greg Clayton334d33a2012-01-30 07:41:31 +0000533 sb_process.SetSP (process_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000534 }
Caroline Tice7826c882010-10-26 03:11:13 +0000535
Greg Claytone005f2c2010-11-06 01:53:30 +0000536 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000537 if (log)
538 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000539 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000540 target_sp.get(), process_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000541 }
542
Chris Lattner24943d22010-06-08 16:52:24 +0000543 return sb_process;
544}
545
Greg Clayton63094e02010-06-23 01:19:29 +0000546SBDebugger
547SBTarget::GetDebugger () const
548{
549 SBDebugger debugger;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000550 TargetSP target_sp(GetSP());
551 if (target_sp)
552 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton63094e02010-06-23 01:19:29 +0000553 return debugger;
554}
555
Jim Inghamb5871fe2011-03-31 00:01:24 +0000556SBProcess
557SBTarget::LaunchSimple
558(
559 char const **argv,
560 char const **envp,
561 const char *working_directory
562)
563{
564 char *stdin_path = NULL;
565 char *stdout_path = NULL;
566 char *stderr_path = NULL;
567 uint32_t launch_flags = 0;
568 bool stop_at_entry = false;
569 SBError error;
570 SBListener listener = GetDebugger().GetListener();
571 return Launch (listener,
572 argv,
573 envp,
574 stdin_path,
575 stdout_path,
576 stderr_path,
577 working_directory,
578 launch_flags,
579 stop_at_entry,
580 error);
581}
Greg Claytonde915be2011-01-23 05:56:20 +0000582
583SBProcess
584SBTarget::Launch
585(
Greg Clayton271a5db2011-02-03 21:28:34 +0000586 SBListener &listener,
Greg Claytonde915be2011-01-23 05:56:20 +0000587 char const **argv,
588 char const **envp,
589 const char *stdin_path,
590 const char *stdout_path,
591 const char *stderr_path,
592 const char *working_directory,
593 uint32_t launch_flags, // See LaunchFlags
594 bool stop_at_entry,
595 lldb::SBError& error
596)
597{
Greg Claytone005f2c2010-11-06 01:53:30 +0000598 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000599
Greg Clayton0416bdf2012-01-30 09:04:36 +0000600 SBProcess sb_process;
601 ProcessSP process_sp;
602 TargetSP target_sp(GetSP());
603
Caroline Tice7826c882010-10-26 03:11:13 +0000604 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000605 {
Greg Claytonde915be2011-01-23 05:56:20 +0000606 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 +0000607 target_sp.get(),
Greg Claytonde915be2011-01-23 05:56:20 +0000608 argv,
609 envp,
610 stdin_path ? stdin_path : "NULL",
611 stdout_path ? stdout_path : "NULL",
612 stderr_path ? stderr_path : "NULL",
613 working_directory ? working_directory : "NULL",
614 launch_flags,
615 stop_at_entry,
616 error.get());
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000617 }
Greg Clayton0416bdf2012-01-30 09:04:36 +0000618
619 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000620 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000621 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000622
Greg Clayton7c330d62011-01-27 01:01:10 +0000623 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
624 launch_flags |= eLaunchFlagDisableASLR;
625
Greg Clayton180546b2011-04-30 01:09:13 +0000626 StateType state = eStateInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000627 process_sp = target_sp->GetProcessSP();
Greg Clayton334d33a2012-01-30 07:41:31 +0000628 if (process_sp)
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000629 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000630 state = process_sp->GetState();
Greg Clayton180546b2011-04-30 01:09:13 +0000631
Greg Clayton334d33a2012-01-30 07:41:31 +0000632 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton180546b2011-04-30 01:09:13 +0000633 {
634 if (state == eStateAttaching)
635 error.SetErrorString ("process attach is in progress");
636 else
637 error.SetErrorString ("a process is already being debugged");
Greg Clayton180546b2011-04-30 01:09:13 +0000638 return sb_process;
639 }
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000640 }
641
Greg Clayton180546b2011-04-30 01:09:13 +0000642 if (state == eStateConnected)
643 {
644 // If we are already connected, then we have already specified the
645 // listener, so if a valid listener is supplied, we need to error out
646 // to let the client know.
647 if (listener.IsValid())
648 {
649 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton180546b2011-04-30 01:09:13 +0000650 return sb_process;
651 }
652 }
653 else
Greg Claytonc5f728c2010-10-06 22:10:17 +0000654 {
Greg Clayton271a5db2011-02-03 21:28:34 +0000655 if (listener.IsValid())
Greg Clayton46c9a352012-02-09 06:16:32 +0000656 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton271a5db2011-02-03 21:28:34 +0000657 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000658 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton180546b2011-04-30 01:09:13 +0000659 }
Greg Clayton7c330d62011-01-27 01:01:10 +0000660
Greg Clayton334d33a2012-01-30 07:41:31 +0000661 if (process_sp)
Greg Clayton180546b2011-04-30 01:09:13 +0000662 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000663 sb_process.SetSP (process_sp);
Greg Clayton180546b2011-04-30 01:09:13 +0000664 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
665 launch_flags |= eLaunchFlagDisableSTDIO;
666
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000667 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
668
Greg Clayton0416bdf2012-01-30 09:04:36 +0000669 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000670 if (exe_module)
Greg Clayton1d1f39e2011-11-29 04:03:30 +0000671 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000672 if (argv)
673 launch_info.GetArguments().AppendArguments (argv);
674 if (envp)
675 launch_info.GetEnvironmentEntries ().SetArguments (envp);
676
Greg Clayton334d33a2012-01-30 07:41:31 +0000677 error.SetError (process_sp->Launch (launch_info));
Greg Clayton180546b2011-04-30 01:09:13 +0000678 if (error.Success())
Greg Clayton7c330d62011-01-27 01:01:10 +0000679 {
Greg Clayton180546b2011-04-30 01:09:13 +0000680 // We we are stopping at the entry point, we can return now!
681 if (stop_at_entry)
682 return sb_process;
Greg Clayton7c330d62011-01-27 01:01:10 +0000683
Greg Clayton180546b2011-04-30 01:09:13 +0000684 // Make sure we are stopped at the entry
Greg Clayton334d33a2012-01-30 07:41:31 +0000685 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton180546b2011-04-30 01:09:13 +0000686 if (state == eStateStopped)
Greg Clayton7c330d62011-01-27 01:01:10 +0000687 {
Greg Clayton180546b2011-04-30 01:09:13 +0000688 // resume the process to skip the entry point
Greg Clayton334d33a2012-01-30 07:41:31 +0000689 error.SetError (process_sp->Resume());
Greg Clayton180546b2011-04-30 01:09:13 +0000690 if (error.Success())
Greg Clayton7c330d62011-01-27 01:01:10 +0000691 {
Greg Clayton180546b2011-04-30 01:09:13 +0000692 // If we are doing synchronous mode, then wait for the
693 // process to stop yet again!
Greg Clayton0416bdf2012-01-30 09:04:36 +0000694 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Clayton334d33a2012-01-30 07:41:31 +0000695 process_sp->WaitForProcessToStop (NULL);
Greg Clayton7c330d62011-01-27 01:01:10 +0000696 }
697 }
698 }
Greg Clayton180546b2011-04-30 01:09:13 +0000699 }
700 else
701 {
702 error.SetErrorString ("unable to create lldb_private::Process");
Greg Claytonc5f728c2010-10-06 22:10:17 +0000703 }
704 }
705 else
706 {
707 error.SetErrorString ("SBTarget is invalid");
Chris Lattner24943d22010-06-08 16:52:24 +0000708 }
Caroline Tice7826c882010-10-26 03:11:13 +0000709
Caroline Tice926060e2010-10-29 21:48:37 +0000710 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +0000711 if (log)
712 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000713 log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000714 target_sp.get(), process_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000715 }
716
Greg Clayton1a3083a2010-10-06 03:53:16 +0000717 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000718}
719
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000720SBProcess
721SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
722{
723 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
724
725 SBProcess sb_process;
726 ProcessSP process_sp;
727 TargetSP target_sp(GetSP());
728
729 if (log)
730 {
731 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
732 }
733
734 if (target_sp)
735 {
736 Mutex::Locker api_locker (target_sp->GetAPIMutex());
737 StateType state = eStateInvalid;
738 process_sp = target_sp->GetProcessSP();
739 if (process_sp)
740 {
741 state = process_sp->GetState();
742
743 if (process_sp->IsAlive() && state != eStateConnected)
744 {
745 if (state == eStateAttaching)
746 error.SetErrorString ("process attach is in progress");
747 else
748 error.SetErrorString ("a process is already being debugged");
749 return sb_process;
750 }
751 }
752
753 if (state != eStateConnected)
754 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
755
756 if (process_sp)
757 {
758 sb_process.SetSP (process_sp);
759 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
Greg Clayton98ca1e62012-02-24 20:59:25 +0000760
Greg Clayton98ca1e62012-02-24 20:59:25 +0000761 Module *exe_module = target_sp->GetExecutableModulePointer();
762 if (exe_module)
Han Ming Ongc86723f2012-03-02 01:02:04 +0000763 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton98ca1e62012-02-24 20:59:25 +0000764
765 const ArchSpec &arch_spec = target_sp->GetArchitecture();
766 if (arch_spec.IsValid())
767 launch_info.GetArchitecture () = arch_spec;
768
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000769 error.SetError (process_sp->Launch (launch_info));
Greg Clayton98ca1e62012-02-24 20:59:25 +0000770 const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000771 if (error.Success())
772 {
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000773 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Clayton98ca1e62012-02-24 20:59:25 +0000774 {
775 // If we are doing synchronous mode, then wait for the initial
776 // stop to happen, else, return and let the caller watch for
777 // the stop
778 if (synchronous_execution)
Greg Clayton4a379b12012-07-17 03:23:13 +0000779 process_sp->WaitForProcessToStop (NULL);
Greg Clayton98ca1e62012-02-24 20:59:25 +0000780 // We we are stopping at the entry point, we can return now!
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000781 return sb_process;
Greg Clayton98ca1e62012-02-24 20:59:25 +0000782 }
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000783
784 // Make sure we are stopped at the entry
Greg Clayton4a379b12012-07-17 03:23:13 +0000785 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000786 if (state == eStateStopped)
787 {
788 // resume the process to skip the entry point
789 error.SetError (process_sp->Resume());
790 if (error.Success())
791 {
792 // If we are doing synchronous mode, then wait for the
793 // process to stop yet again!
Greg Clayton98ca1e62012-02-24 20:59:25 +0000794 if (synchronous_execution)
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000795 process_sp->WaitForProcessToStop (NULL);
796 }
797 }
798 }
799 }
800 else
801 {
802 error.SetErrorString ("unable to create lldb_private::Process");
803 }
804 }
805 else
806 {
807 error.SetErrorString ("SBTarget is invalid");
808 }
809
810 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
811 if (log)
812 {
813 log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
814 target_sp.get(), process_sp.get());
815 }
816
817 return sb_process;
818}
819
820lldb::SBProcess
821SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
822{
823 SBProcess sb_process;
824 ProcessSP process_sp;
825 TargetSP target_sp(GetSP());
826 if (target_sp)
827 {
828 Mutex::Locker api_locker (target_sp->GetAPIMutex());
829
830 StateType state = eStateInvalid;
831 process_sp = target_sp->GetProcessSP();
832 if (process_sp)
833 {
834 state = process_sp->GetState();
835
836 if (process_sp->IsAlive() && state != eStateConnected)
837 {
838 if (state == eStateAttaching)
839 error.SetErrorString ("process attach is in progress");
840 else
841 error.SetErrorString ("a process is already being debugged");
842 return sb_process;
843 }
844 }
845
846 if (state != eStateConnected)
847 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
848
849 if (process_sp)
850 {
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000851 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ong435c5ef2012-02-29 00:12:56 +0000852 lldb::pid_t attach_pid = attach_info.GetProcessID();
853 if (attach_pid != LLDB_INVALID_PROCESS_ID)
854 {
855 PlatformSP platform_sp = target_sp->GetPlatform();
856 ProcessInstanceInfo instance_info;
857 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
858 {
859 attach_info.SetUserID(instance_info.GetEffectiveUserID());
860 }
861 }
Han Ming Ong94b4e9a2012-02-29 19:16:40 +0000862 error.SetError (process_sp->Attach (attach_info));
863 if (error.Success())
864 {
865 sb_process.SetSP (process_sp);
866 // If we are doing synchronous mode, then wait for the
867 // process to stop!
868 if (target_sp->GetDebugger().GetAsyncExecution () == false)
869 process_sp->WaitForProcessToStop (NULL);
870 }
Greg Clayton0a8dcac2012-02-24 05:03:03 +0000871 }
872 else
873 {
874 error.SetErrorString ("unable to create lldb_private::Process");
875 }
876 }
877 else
878 {
879 error.SetErrorString ("SBTarget is invalid");
880 }
881 return sb_process;
882}
883
884
Greg Claytond5b0b442011-12-02 02:10:57 +0000885#if defined(__APPLE__)
886
887lldb::SBProcess
888SBTarget::AttachToProcessWithID (SBListener &listener,
889 ::pid_t pid,
890 lldb::SBError& error)
891{
892 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
893}
894
895#endif // #if defined(__APPLE__)
Greg Claytonc5f728c2010-10-06 22:10:17 +0000896
897lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000898SBTarget::AttachToProcessWithID
Greg Claytonc5f728c2010-10-06 22:10:17 +0000899(
Greg Clayton271a5db2011-02-03 21:28:34 +0000900 SBListener &listener,
Greg Claytonc5f728c2010-10-06 22:10:17 +0000901 lldb::pid_t pid,// The process ID to attach to
902 SBError& error // An error explaining what went wrong if attach fails
903)
904{
905 SBProcess sb_process;
Greg Clayton334d33a2012-01-30 07:41:31 +0000906 ProcessSP process_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000907 TargetSP target_sp(GetSP());
908 if (target_sp)
Greg Claytonc5f728c2010-10-06 22:10:17 +0000909 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000910 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonde1dd812011-06-24 03:21:43 +0000911
912 StateType state = eStateInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000913 process_sp = target_sp->GetProcessSP();
Greg Clayton334d33a2012-01-30 07:41:31 +0000914 if (process_sp)
Greg Claytonde1dd812011-06-24 03:21:43 +0000915 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000916 state = process_sp->GetState();
Greg Claytonde1dd812011-06-24 03:21:43 +0000917
Greg Clayton334d33a2012-01-30 07:41:31 +0000918 if (process_sp->IsAlive() && state != eStateConnected)
Greg Claytonde1dd812011-06-24 03:21:43 +0000919 {
920 if (state == eStateAttaching)
921 error.SetErrorString ("process attach is in progress");
922 else
923 error.SetErrorString ("a process is already being debugged");
Greg Claytonde1dd812011-06-24 03:21:43 +0000924 return sb_process;
925 }
926 }
927
928 if (state == eStateConnected)
929 {
930 // If we are already connected, then we have already specified the
931 // listener, so if a valid listener is supplied, we need to error out
932 // to let the client know.
933 if (listener.IsValid())
934 {
935 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Claytonde1dd812011-06-24 03:21:43 +0000936 return sb_process;
937 }
938 }
Greg Clayton271a5db2011-02-03 21:28:34 +0000939 else
Greg Claytonde1dd812011-06-24 03:21:43 +0000940 {
941 if (listener.IsValid())
Greg Clayton46c9a352012-02-09 06:16:32 +0000942 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Claytonde1dd812011-06-24 03:21:43 +0000943 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000944 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Claytonde1dd812011-06-24 03:21:43 +0000945 }
Greg Clayton334d33a2012-01-30 07:41:31 +0000946 if (process_sp)
Greg Claytonc5f728c2010-10-06 22:10:17 +0000947 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000948 sb_process.SetSP (process_sp);
949
Greg Clayton527154d2011-11-15 03:53:30 +0000950 ProcessAttachInfo attach_info;
951 attach_info.SetProcessID (pid);
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000952
953 PlatformSP platform_sp = target_sp->GetPlatform();
954 ProcessInstanceInfo instance_info;
955 if (platform_sp->GetProcessInfo(pid, instance_info))
956 {
957 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ongd1040dd2012-02-25 01:07:38 +0000958 }
Greg Clayton334d33a2012-01-30 07:41:31 +0000959 error.SetError (process_sp->Attach (attach_info));
Johnny Chen535960e2011-06-17 00:51:15 +0000960 // If we are doing synchronous mode, then wait for the
961 // process to stop!
Greg Clayton0416bdf2012-01-30 09:04:36 +0000962 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Clayton334d33a2012-01-30 07:41:31 +0000963 process_sp->WaitForProcessToStop (NULL);
Greg Claytonc5f728c2010-10-06 22:10:17 +0000964 }
965 else
966 {
967 error.SetErrorString ("unable to create lldb_private::Process");
968 }
969 }
970 else
971 {
972 error.SetErrorString ("SBTarget is invalid");
973 }
974 return sb_process;
975
976}
977
978lldb::SBProcess
Greg Claytond8c62532010-10-07 04:19:01 +0000979SBTarget::AttachToProcessWithName
Greg Claytonc5f728c2010-10-06 22:10:17 +0000980(
Greg Clayton271a5db2011-02-03 21:28:34 +0000981 SBListener &listener,
Greg Claytonc5f728c2010-10-06 22:10:17 +0000982 const char *name, // basename of process to attach to
983 bool wait_for, // if true wait for a new instance of "name" to be launched
984 SBError& error // An error explaining what went wrong if attach fails
985)
986{
987 SBProcess sb_process;
Greg Clayton334d33a2012-01-30 07:41:31 +0000988 ProcessSP process_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000989 TargetSP target_sp(GetSP());
990 if (name && target_sp)
Greg Claytonc5f728c2010-10-06 22:10:17 +0000991 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000992 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonbdcda462010-12-20 20:49:23 +0000993
Greg Claytonde1dd812011-06-24 03:21:43 +0000994 StateType state = eStateInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000995 process_sp = target_sp->GetProcessSP();
Greg Clayton334d33a2012-01-30 07:41:31 +0000996 if (process_sp)
Greg Claytonde1dd812011-06-24 03:21:43 +0000997 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000998 state = process_sp->GetState();
Greg Claytonde1dd812011-06-24 03:21:43 +0000999
Greg Clayton334d33a2012-01-30 07:41:31 +00001000 if (process_sp->IsAlive() && state != eStateConnected)
Greg Claytonde1dd812011-06-24 03:21:43 +00001001 {
1002 if (state == eStateAttaching)
1003 error.SetErrorString ("process attach is in progress");
1004 else
1005 error.SetErrorString ("a process is already being debugged");
Greg Claytonde1dd812011-06-24 03:21:43 +00001006 return sb_process;
1007 }
1008 }
1009
1010 if (state == eStateConnected)
1011 {
1012 // If we are already connected, then we have already specified the
1013 // listener, so if a valid listener is supplied, we need to error out
1014 // to let the client know.
1015 if (listener.IsValid())
1016 {
1017 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Claytonde1dd812011-06-24 03:21:43 +00001018 return sb_process;
1019 }
1020 }
Greg Clayton271a5db2011-02-03 21:28:34 +00001021 else
Greg Claytonde1dd812011-06-24 03:21:43 +00001022 {
1023 if (listener.IsValid())
Greg Clayton46c9a352012-02-09 06:16:32 +00001024 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Claytonde1dd812011-06-24 03:21:43 +00001025 else
Greg Clayton46c9a352012-02-09 06:16:32 +00001026 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Claytonde1dd812011-06-24 03:21:43 +00001027 }
Greg Claytonc5f728c2010-10-06 22:10:17 +00001028
Greg Clayton334d33a2012-01-30 07:41:31 +00001029 if (process_sp)
Greg Claytonc5f728c2010-10-06 22:10:17 +00001030 {
Greg Clayton334d33a2012-01-30 07:41:31 +00001031 sb_process.SetSP (process_sp);
Greg Clayton527154d2011-11-15 03:53:30 +00001032 ProcessAttachInfo attach_info;
1033 attach_info.GetExecutableFile().SetFile(name, false);
1034 attach_info.SetWaitForLaunch(wait_for);
Greg Clayton334d33a2012-01-30 07:41:31 +00001035 error.SetError (process_sp->Attach (attach_info));
Johnny Chen58d02ff2011-06-17 19:21:30 +00001036 // If we are doing synchronous mode, then wait for the
1037 // process to stop!
Greg Clayton0416bdf2012-01-30 09:04:36 +00001038 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Clayton334d33a2012-01-30 07:41:31 +00001039 process_sp->WaitForProcessToStop (NULL);
Greg Claytonc5f728c2010-10-06 22:10:17 +00001040 }
1041 else
1042 {
1043 error.SetErrorString ("unable to create lldb_private::Process");
1044 }
1045 }
1046 else
1047 {
1048 error.SetErrorString ("SBTarget is invalid");
1049 }
1050 return sb_process;
1051
1052}
1053
James McIlree38093402011-03-04 00:31:13 +00001054lldb::SBProcess
1055SBTarget::ConnectRemote
1056(
1057 SBListener &listener,
1058 const char *url,
1059 const char *plugin_name,
1060 SBError& error
1061)
1062{
1063 SBProcess sb_process;
Greg Clayton334d33a2012-01-30 07:41:31 +00001064 ProcessSP process_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001065 TargetSP target_sp(GetSP());
1066 if (target_sp)
James McIlree38093402011-03-04 00:31:13 +00001067 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001068 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree38093402011-03-04 00:31:13 +00001069 if (listener.IsValid())
Greg Clayton46c9a352012-02-09 06:16:32 +00001070 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree38093402011-03-04 00:31:13 +00001071 else
Greg Clayton46c9a352012-02-09 06:16:32 +00001072 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
James McIlree38093402011-03-04 00:31:13 +00001073
1074
Greg Clayton334d33a2012-01-30 07:41:31 +00001075 if (process_sp)
James McIlree38093402011-03-04 00:31:13 +00001076 {
Greg Clayton334d33a2012-01-30 07:41:31 +00001077 sb_process.SetSP (process_sp);
1078 error.SetError (process_sp->ConnectRemote (url));
James McIlree38093402011-03-04 00:31:13 +00001079 }
1080 else
1081 {
1082 error.SetErrorString ("unable to create lldb_private::Process");
1083 }
1084 }
1085 else
1086 {
1087 error.SetErrorString ("SBTarget is invalid");
1088 }
1089 return sb_process;
1090}
1091
Chris Lattner24943d22010-06-08 16:52:24 +00001092SBFileSpec
1093SBTarget::GetExecutable ()
1094{
Caroline Tice7826c882010-10-26 03:11:13 +00001095
Chris Lattner24943d22010-06-08 16:52:24 +00001096 SBFileSpec exe_file_spec;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001097 TargetSP target_sp(GetSP());
1098 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001099 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001100 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Clayton5beb99d2011-08-11 02:48:45 +00001101 if (exe_module)
1102 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner24943d22010-06-08 16:52:24 +00001103 }
Caroline Tice7826c882010-10-26 03:11:13 +00001104
Greg Claytone005f2c2010-11-06 01:53:30 +00001105 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001106 if (log)
1107 {
Greg Clayton49ce6822010-10-31 03:01:06 +00001108 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001109 target_sp.get(), exe_file_spec.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001110 }
1111
Chris Lattner24943d22010-06-08 16:52:24 +00001112 return exe_file_spec;
1113}
1114
Chris Lattner24943d22010-06-08 16:52:24 +00001115bool
Chris Lattner24943d22010-06-08 16:52:24 +00001116SBTarget::operator == (const SBTarget &rhs) const
1117{
Greg Clayton63094e02010-06-23 01:19:29 +00001118 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +00001119}
1120
1121bool
1122SBTarget::operator != (const SBTarget &rhs) const
1123{
Greg Clayton63094e02010-06-23 01:19:29 +00001124 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +00001125}
1126
Greg Clayton334d33a2012-01-30 07:41:31 +00001127lldb::TargetSP
1128SBTarget::GetSP () const
Greg Clayton15afa9f2011-10-01 02:59:24 +00001129{
1130 return m_opaque_sp;
1131}
1132
Greg Clayton63094e02010-06-23 01:19:29 +00001133void
Greg Clayton334d33a2012-01-30 07:41:31 +00001134SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton63094e02010-06-23 01:19:29 +00001135{
1136 m_opaque_sp = target_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001137}
1138
Greg Claytona3955062011-07-22 16:46:35 +00001139lldb::SBAddress
1140SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonea49cc72010-12-12 19:25:26 +00001141{
Greg Claytona3955062011-07-22 16:46:35 +00001142 lldb::SBAddress sb_addr;
1143 Address &addr = sb_addr.ref();
Greg Clayton0416bdf2012-01-30 09:04:36 +00001144 TargetSP target_sp(GetSP());
1145 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001146 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001147 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1148 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
Greg Claytona3955062011-07-22 16:46:35 +00001149 return sb_addr;
Greg Claytonbdcda462010-12-20 20:49:23 +00001150 }
Greg Claytonea49cc72010-12-12 19:25:26 +00001151
Greg Claytona3955062011-07-22 16:46:35 +00001152 // We have a load address that isn't in a section, just return an address
1153 // with the offset filled in (the address) and the section set to NULL
Greg Clayton3508c382012-02-24 01:59:29 +00001154 addr.SetRawAddress(vm_addr);
Greg Claytona3955062011-07-22 16:46:35 +00001155 return sb_addr;
Greg Claytonea49cc72010-12-12 19:25:26 +00001156}
1157
Greg Claytonafb81862011-03-02 21:34:46 +00001158SBSymbolContext
1159SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1160{
1161 SBSymbolContext sc;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001162 if (addr.IsValid())
1163 {
1164 TargetSP target_sp(GetSP());
1165 if (target_sp)
1166 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1167 }
Greg Claytonafb81862011-03-02 21:34:46 +00001168 return sc;
1169}
1170
1171
Chris Lattner24943d22010-06-08 16:52:24 +00001172SBBreakpoint
1173SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1174{
Greg Claytond6d806c2010-11-08 00:28:40 +00001175 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner24943d22010-06-08 16:52:24 +00001176}
1177
1178SBBreakpoint
1179SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1180{
Greg Claytone005f2c2010-11-06 01:53:30 +00001181 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001182
Chris Lattner24943d22010-06-08 16:52:24 +00001183 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001184 TargetSP target_sp(GetSP());
1185 if (target_sp && line != 0)
Greg Claytonbdcda462010-12-20 20:49:23 +00001186 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001187 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001188
Greg Clayton49ce8962012-08-29 21:13:06 +00001189 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001190 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton49ce8962012-08-29 21:13:06 +00001191 const bool internal = false;
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001192 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
Greg Claytonbdcda462010-12-20 20:49:23 +00001193 }
Caroline Tice7826c882010-10-26 03:11:13 +00001194
1195 if (log)
1196 {
1197 SBStream sstr;
1198 sb_bp.GetDescription (sstr);
Greg Clayton49ce6822010-10-31 03:01:06 +00001199 char path[PATH_MAX];
1200 sb_file_spec->GetPath (path, sizeof(path));
1201 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001202 target_sp.get(),
Greg Clayton49ce6822010-10-31 03:01:06 +00001203 path,
Greg Claytona66ba462010-10-30 04:51:46 +00001204 line,
Greg Clayton49ce6822010-10-31 03:01:06 +00001205 sb_bp.get(),
Caroline Tice7826c882010-10-26 03:11:13 +00001206 sstr.GetData());
1207 }
1208
Chris Lattner24943d22010-06-08 16:52:24 +00001209 return sb_bp;
1210}
1211
1212SBBreakpoint
1213SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1214{
Greg Claytone005f2c2010-11-06 01:53:30 +00001215 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001216
Chris Lattner24943d22010-06-08 16:52:24 +00001217 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001218 TargetSP target_sp(GetSP());
1219 if (target_sp.get())
Chris Lattner24943d22010-06-08 16:52:24 +00001220 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001221 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001222
1223 const bool internal = false;
1224 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner24943d22010-06-08 16:52:24 +00001225 if (module_name && module_name[0])
1226 {
Jim Ingham03c8ee52011-09-21 01:17:13 +00001227 FileSpecList module_spec_list;
1228 module_spec_list.Append (FileSpec (module_name, false));
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001229 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner24943d22010-06-08 16:52:24 +00001230 }
1231 else
1232 {
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001233 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner24943d22010-06-08 16:52:24 +00001234 }
1235 }
Caroline Tice7826c882010-10-26 03:11:13 +00001236
1237 if (log)
1238 {
Greg Clayton49ce6822010-10-31 03:01:06 +00001239 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001240 target_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001241 }
1242
Chris Lattner24943d22010-06-08 16:52:24 +00001243 return sb_bp;
1244}
1245
Jim Inghamd6d47972011-09-23 00:54:11 +00001246lldb::SBBreakpoint
1247SBTarget::BreakpointCreateByName (const char *symbol_name,
1248 const SBFileSpecList &module_list,
1249 const SBFileSpecList &comp_unit_list)
1250{
Jim Ingham1fb8a2d2011-10-11 01:18:55 +00001251 uint32_t name_type_mask = eFunctionNameTypeAuto;
1252 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1253}
1254
1255lldb::SBBreakpoint
1256SBTarget::BreakpointCreateByName (const char *symbol_name,
1257 uint32_t name_type_mask,
1258 const SBFileSpecList &module_list,
1259 const SBFileSpecList &comp_unit_list)
1260{
Jim Inghamd6d47972011-09-23 00:54:11 +00001261 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1262
1263 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001264 TargetSP target_sp(GetSP());
1265 if (target_sp && symbol_name && symbol_name[0])
Jim Inghamd6d47972011-09-23 00:54:11 +00001266 {
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001267 const bool internal = false;
1268 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001269 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1270 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Jim Inghamd6d47972011-09-23 00:54:11 +00001271 comp_unit_list.get(),
1272 symbol_name,
Jim Ingham1fb8a2d2011-10-11 01:18:55 +00001273 name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001274 skip_prologue,
1275 internal);
Jim Inghamd6d47972011-09-23 00:54:11 +00001276 }
1277
1278 if (log)
1279 {
Jim Ingham1fb8a2d2011-10-11 01:18:55 +00001280 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001281 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
Jim Inghamd6d47972011-09-23 00:54:11 +00001282 }
1283
1284 return sb_bp;
1285}
1286
Jim Ingham4722b102012-03-06 00:37:27 +00001287lldb::SBBreakpoint
1288SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1289 uint32_t num_names,
1290 uint32_t name_type_mask,
1291 const SBFileSpecList &module_list,
1292 const SBFileSpecList &comp_unit_list)
1293{
1294 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1295
1296 SBBreakpoint sb_bp;
1297 TargetSP target_sp(GetSP());
1298 if (target_sp && num_names > 0)
1299 {
1300 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001301 const bool internal = false;
1302 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Ingham4722b102012-03-06 00:37:27 +00001303 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1304 comp_unit_list.get(),
1305 symbol_names,
1306 num_names,
1307 name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001308 skip_prologue,
1309 internal);
Jim Ingham4722b102012-03-06 00:37:27 +00001310 }
1311
1312 if (log)
1313 {
1314 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1315 for (uint32_t i = 0 ; i < num_names; i++)
1316 {
1317 char sep;
1318 if (i < num_names - 1)
1319 sep = ',';
1320 else
1321 sep = '}';
1322 if (symbol_names[i] != NULL)
1323 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1324 else
1325 log->Printf ("\"<NULL>\"%c ", sep);
1326
1327 }
1328 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1329 }
1330
1331 return sb_bp;
1332}
Jim Inghamd6d47972011-09-23 00:54:11 +00001333
Chris Lattner24943d22010-06-08 16:52:24 +00001334SBBreakpoint
1335SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1336{
Greg Claytone005f2c2010-11-06 01:53:30 +00001337 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001338
Chris Lattner24943d22010-06-08 16:52:24 +00001339 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001340 TargetSP target_sp(GetSP());
1341 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner24943d22010-06-08 16:52:24 +00001342 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001343 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner24943d22010-06-08 16:52:24 +00001344 RegularExpression regexp(symbol_name_regex);
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001345 const bool internal = false;
1346 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner24943d22010-06-08 16:52:24 +00001347
1348 if (module_name && module_name[0])
1349 {
Jim Ingham03c8ee52011-09-21 01:17:13 +00001350 FileSpecList module_spec_list;
1351 module_spec_list.Append (FileSpec (module_name, false));
Chris Lattner24943d22010-06-08 16:52:24 +00001352
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001353 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
Chris Lattner24943d22010-06-08 16:52:24 +00001354 }
1355 else
1356 {
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001357 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
Chris Lattner24943d22010-06-08 16:52:24 +00001358 }
1359 }
Caroline Tice7826c882010-10-26 03:11:13 +00001360
1361 if (log)
1362 {
Greg Clayton49ce6822010-10-31 03:01:06 +00001363 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001364 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001365 }
1366
Chris Lattner24943d22010-06-08 16:52:24 +00001367 return sb_bp;
1368}
1369
Jim Inghamd6d47972011-09-23 00:54:11 +00001370lldb::SBBreakpoint
1371SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1372 const SBFileSpecList &module_list,
1373 const SBFileSpecList &comp_unit_list)
1374{
1375 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner24943d22010-06-08 16:52:24 +00001376
Jim Inghamd6d47972011-09-23 00:54:11 +00001377 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001378 TargetSP target_sp(GetSP());
1379 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Inghamd6d47972011-09-23 00:54:11 +00001380 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001381 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghamd6d47972011-09-23 00:54:11 +00001382 RegularExpression regexp(symbol_name_regex);
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001383 const bool internal = false;
1384 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamd6d47972011-09-23 00:54:11 +00001385
Jim Ingham2cf5ccb2012-05-22 00:12:20 +00001386 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
Jim Inghamd6d47972011-09-23 00:54:11 +00001387 }
1388
1389 if (log)
1390 {
1391 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001392 target_sp.get(), symbol_name_regex, sb_bp.get());
Jim Inghamd6d47972011-09-23 00:54:11 +00001393 }
1394
1395 return sb_bp;
1396}
Chris Lattner24943d22010-06-08 16:52:24 +00001397
1398SBBreakpoint
1399SBTarget::BreakpointCreateByAddress (addr_t address)
1400{
Greg Claytone005f2c2010-11-06 01:53:30 +00001401 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001402
Chris Lattner24943d22010-06-08 16:52:24 +00001403 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001404 TargetSP target_sp(GetSP());
1405 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001406 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001407 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1408 *sb_bp = target_sp->CreateBreakpoint (address, false);
Greg Claytonbdcda462010-12-20 20:49:23 +00001409 }
Caroline Tice7826c882010-10-26 03:11:13 +00001410
1411 if (log)
1412 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001413 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 +00001414 }
1415
Chris Lattner24943d22010-06-08 16:52:24 +00001416 return sb_bp;
1417}
1418
Jim Ingham03c8ee52011-09-21 01:17:13 +00001419lldb::SBBreakpoint
1420SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1421{
1422 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1423
1424 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001425 TargetSP target_sp(GetSP());
1426 if (target_sp && source_regex && source_regex[0])
Jim Ingham03c8ee52011-09-21 01:17:13 +00001427 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001428 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham03c8ee52011-09-21 01:17:13 +00001429 RegularExpression regexp(source_regex);
Jim Inghamd6d47972011-09-23 00:54:11 +00001430 FileSpecList source_file_spec_list;
1431 source_file_spec_list.Append (source_file.ref());
Jim Ingham03c8ee52011-09-21 01:17:13 +00001432
1433 if (module_name && module_name[0])
1434 {
1435 FileSpecList module_spec_list;
1436 module_spec_list.Append (FileSpec (module_name, false));
1437
Greg Clayton0416bdf2012-01-30 09:04:36 +00001438 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
Jim Ingham03c8ee52011-09-21 01:17:13 +00001439 }
1440 else
1441 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001442 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
Jim Ingham03c8ee52011-09-21 01:17:13 +00001443 }
1444 }
1445
1446 if (log)
1447 {
1448 char path[PATH_MAX];
1449 source_file->GetPath (path, sizeof(path));
1450 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001451 target_sp.get(), source_regex, path, module_name, sb_bp.get());
Jim Ingham03c8ee52011-09-21 01:17:13 +00001452 }
1453
1454 return sb_bp;
1455}
1456
Jim Inghamd6d47972011-09-23 00:54:11 +00001457lldb::SBBreakpoint
1458SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1459 const SBFileSpecList &module_list,
1460 const lldb::SBFileSpecList &source_file_list)
1461{
1462 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1463
1464 SBBreakpoint sb_bp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001465 TargetSP target_sp(GetSP());
1466 if (target_sp && source_regex && source_regex[0])
Jim Inghamd6d47972011-09-23 00:54:11 +00001467 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001468 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghamd6d47972011-09-23 00:54:11 +00001469 RegularExpression regexp(source_regex);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001470 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
Jim Inghamd6d47972011-09-23 00:54:11 +00001471 }
1472
1473 if (log)
1474 {
1475 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001476 target_sp.get(), source_regex, sb_bp.get());
Jim Inghamd6d47972011-09-23 00:54:11 +00001477 }
1478
1479 return sb_bp;
1480}
Jim Ingham03c8ee52011-09-21 01:17:13 +00001481
Jim Ingham4722b102012-03-06 00:37:27 +00001482lldb::SBBreakpoint
1483SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1484 bool catch_bp,
1485 bool throw_bp)
1486{
1487 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1488
1489 SBBreakpoint sb_bp;
1490 TargetSP target_sp(GetSP());
1491 if (target_sp)
1492 {
1493 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1494 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1495 }
1496
1497 if (log)
1498 {
1499 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1500 target_sp.get(),
1501 LanguageRuntime::GetNameForLanguageType(language),
1502 catch_bp ? "on" : "off",
1503 throw_bp ? "on" : "off",
1504 sb_bp.get());
1505 }
1506
1507 return sb_bp;
1508}
1509
Greg Claytonc7f5d5c2010-07-23 23:33:17 +00001510uint32_t
1511SBTarget::GetNumBreakpoints () const
1512{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001513 TargetSP target_sp(GetSP());
1514 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001515 {
1516 // The breakpoint list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001517 return target_sp->GetBreakpointList().GetSize();
Greg Claytonbdcda462010-12-20 20:49:23 +00001518 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +00001519 return 0;
1520}
1521
1522SBBreakpoint
1523SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1524{
1525 SBBreakpoint sb_breakpoint;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001526 TargetSP target_sp(GetSP());
1527 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001528 {
1529 // The breakpoint list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001530 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonbdcda462010-12-20 20:49:23 +00001531 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +00001532 return sb_breakpoint;
1533}
Chris Lattner24943d22010-06-08 16:52:24 +00001534
1535bool
1536SBTarget::BreakpointDelete (break_id_t bp_id)
1537{
Greg Claytone005f2c2010-11-06 01:53:30 +00001538 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001539
Caroline Tice7826c882010-10-26 03:11:13 +00001540 bool result = false;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001541 TargetSP target_sp(GetSP());
1542 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001543 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001544 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1545 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonbdcda462010-12-20 20:49:23 +00001546 }
Caroline Tice7826c882010-10-26 03:11:13 +00001547
1548 if (log)
1549 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001550 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
Caroline Tice7826c882010-10-26 03:11:13 +00001551 }
1552
1553 return result;
Chris Lattner24943d22010-06-08 16:52:24 +00001554}
1555
Johnny Chen096c2932011-09-26 22:40:50 +00001556SBBreakpoint
1557SBTarget::FindBreakpointByID (break_id_t bp_id)
1558{
1559 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1560
1561 SBBreakpoint sb_breakpoint;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001562 TargetSP target_sp(GetSP());
1563 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen096c2932011-09-26 22:40:50 +00001564 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001565 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1566 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen096c2932011-09-26 22:40:50 +00001567 }
1568
1569 if (log)
1570 {
1571 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001572 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Johnny Chen096c2932011-09-26 22:40:50 +00001573 }
1574
1575 return sb_breakpoint;
1576}
1577
Chris Lattner24943d22010-06-08 16:52:24 +00001578bool
1579SBTarget::EnableAllBreakpoints ()
1580{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001581 TargetSP target_sp(GetSP());
1582 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001583 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001584 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1585 target_sp->EnableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +00001586 return true;
1587 }
1588 return false;
1589}
1590
1591bool
1592SBTarget::DisableAllBreakpoints ()
1593{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001594 TargetSP target_sp(GetSP());
1595 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001596 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001597 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1598 target_sp->DisableAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +00001599 return true;
1600 }
1601 return false;
1602}
1603
1604bool
1605SBTarget::DeleteAllBreakpoints ()
1606{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001607 TargetSP target_sp(GetSP());
1608 if (target_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001609 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001610 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1611 target_sp->RemoveAllBreakpoints ();
Chris Lattner24943d22010-06-08 16:52:24 +00001612 return true;
1613 }
1614 return false;
1615}
1616
Johnny Chen096c2932011-09-26 22:40:50 +00001617uint32_t
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001618SBTarget::GetNumWatchpoints () const
Johnny Chen096c2932011-09-26 22:40:50 +00001619{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001620 TargetSP target_sp(GetSP());
1621 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001622 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001623 // The watchpoint list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001624 return target_sp->GetWatchpointList().GetSize();
Johnny Chen096c2932011-09-26 22:40:50 +00001625 }
1626 return 0;
1627}
1628
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001629SBWatchpoint
1630SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen5eb54bb2011-09-27 20:29:45 +00001631{
Johnny Chenecd4feb2011-10-14 00:42:25 +00001632 SBWatchpoint sb_watchpoint;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001633 TargetSP target_sp(GetSP());
1634 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001635 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001636 // The watchpoint list is thread safe, no need to lock
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001637 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen096c2932011-09-26 22:40:50 +00001638 }
Johnny Chenecd4feb2011-10-14 00:42:25 +00001639 return sb_watchpoint;
Johnny Chen096c2932011-09-26 22:40:50 +00001640}
1641
1642bool
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001643SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen096c2932011-09-26 22:40:50 +00001644{
1645 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1646
1647 bool result = false;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001648 TargetSP target_sp(GetSP());
1649 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001650 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001651 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001652 Mutex::Locker locker;
1653 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001654 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen096c2932011-09-26 22:40:50 +00001655 }
1656
1657 if (log)
1658 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001659 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
Johnny Chen096c2932011-09-26 22:40:50 +00001660 }
1661
1662 return result;
1663}
1664
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001665SBWatchpoint
1666SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen096c2932011-09-26 22:40:50 +00001667{
1668 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1669
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001670 SBWatchpoint sb_watchpoint;
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001671 lldb::WatchpointSP watchpoint_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001672 TargetSP target_sp(GetSP());
1673 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen096c2932011-09-26 22:40:50 +00001674 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001675 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001676 Mutex::Locker locker;
1677 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001678 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1679 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen096c2932011-09-26 22:40:50 +00001680 }
1681
1682 if (log)
1683 {
Johnny Chenecd4feb2011-10-14 00:42:25 +00001684 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001685 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
Johnny Chen096c2932011-09-26 22:40:50 +00001686 }
1687
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001688 return sb_watchpoint;
1689}
1690
1691lldb::SBWatchpoint
Johnny Chen3f883492012-06-04 23:19:54 +00001692SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001693{
1694 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1695
1696 SBWatchpoint sb_watchpoint;
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001697 lldb::WatchpointSP watchpoint_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001698 TargetSP target_sp(GetSP());
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001699 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001700 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001701 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001702 uint32_t watch_type = 0;
1703 if (read)
1704 watch_type |= LLDB_WATCH_TYPE_READ;
1705 if (write)
1706 watch_type |= LLDB_WATCH_TYPE_WRITE;
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001707 // Target::CreateWatchpoint() is thread safe.
Johnny Chen3f883492012-06-04 23:19:54 +00001708 Error cw_error;
1709 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, watch_type, cw_error);
1710 error.SetError(cw_error);
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001711 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001712 }
1713
1714 if (log)
1715 {
1716 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%llx, 0x%u) => SBWatchpoint(%p)",
Greg Clayton0a19a1b2012-02-04 02:27:34 +00001717 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001718 }
1719
1720 return sb_watchpoint;
Johnny Chen096c2932011-09-26 22:40:50 +00001721}
1722
1723bool
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001724SBTarget::EnableAllWatchpoints ()
Johnny Chen096c2932011-09-26 22:40:50 +00001725{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001726 TargetSP target_sp(GetSP());
1727 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001728 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001729 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001730 Mutex::Locker locker;
1731 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001732 target_sp->EnableAllWatchpoints ();
Johnny Chen096c2932011-09-26 22:40:50 +00001733 return true;
1734 }
1735 return false;
1736}
1737
1738bool
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001739SBTarget::DisableAllWatchpoints ()
Johnny Chen096c2932011-09-26 22:40:50 +00001740{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001741 TargetSP target_sp(GetSP());
1742 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001743 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001744 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001745 Mutex::Locker locker;
1746 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001747 target_sp->DisableAllWatchpoints ();
Johnny Chen096c2932011-09-26 22:40:50 +00001748 return true;
1749 }
1750 return false;
1751}
1752
1753bool
Greg Clayton1fa6b3d2011-10-13 18:08:26 +00001754SBTarget::DeleteAllWatchpoints ()
Johnny Chen096c2932011-09-26 22:40:50 +00001755{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001756 TargetSP target_sp(GetSP());
1757 if (target_sp)
Johnny Chen096c2932011-09-26 22:40:50 +00001758 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001759 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chenbbf6aa52012-05-31 22:56:36 +00001760 Mutex::Locker locker;
1761 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001762 target_sp->RemoveAllWatchpoints ();
Johnny Chen096c2932011-09-26 22:40:50 +00001763 return true;
1764 }
1765 return false;
1766}
1767
Chris Lattner24943d22010-06-08 16:52:24 +00001768
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001769lldb::SBModule
1770SBTarget::AddModule (const char *path,
1771 const char *triple,
1772 const char *uuid_cstr)
1773{
Greg Claytonf0bc8152012-04-23 20:23:39 +00001774 return AddModule (path, triple, uuid_cstr, NULL);
1775}
1776
1777lldb::SBModule
1778SBTarget::AddModule (const char *path,
1779 const char *triple,
1780 const char *uuid_cstr,
1781 const char *symfile)
1782{
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001783 lldb::SBModule sb_module;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001784 TargetSP target_sp(GetSP());
1785 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001786 {
Greg Clayton444fe992012-02-26 05:51:37 +00001787 ModuleSpec module_spec;
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001788 if (path)
Greg Clayton444fe992012-02-26 05:51:37 +00001789 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonf0bc8152012-04-23 20:23:39 +00001790
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001791 if (uuid_cstr)
Greg Clayton444fe992012-02-26 05:51:37 +00001792 module_spec.GetUUID().SetfromCString(uuid_cstr);
Greg Claytonf0bc8152012-04-23 20:23:39 +00001793
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001794 if (triple)
Greg Clayton444fe992012-02-26 05:51:37 +00001795 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Greg Claytonf0bc8152012-04-23 20:23:39 +00001796
1797 if (symfile)
1798 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001799
Greg Clayton444fe992012-02-26 05:51:37 +00001800 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001801 }
1802 return sb_module;
1803}
1804
1805bool
1806SBTarget::AddModule (lldb::SBModule &module)
1807{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001808 TargetSP target_sp(GetSP());
1809 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001810 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001811 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001812 return true;
1813 }
1814 return false;
1815}
1816
Chris Lattner24943d22010-06-08 16:52:24 +00001817uint32_t
1818SBTarget::GetNumModules () const
1819{
Greg Claytone005f2c2010-11-06 01:53:30 +00001820 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001821
Caroline Tice7826c882010-10-26 03:11:13 +00001822 uint32_t num = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001823 TargetSP target_sp(GetSP());
1824 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001825 {
1826 // The module list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001827 num = target_sp->GetImages().GetSize();
Greg Claytonbdcda462010-12-20 20:49:23 +00001828 }
Caroline Tice7826c882010-10-26 03:11:13 +00001829
1830 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +00001831 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
Caroline Tice7826c882010-10-26 03:11:13 +00001832
1833 return num;
Chris Lattner24943d22010-06-08 16:52:24 +00001834}
1835
Greg Clayton43490d12010-07-30 20:12:55 +00001836void
1837SBTarget::Clear ()
1838{
Greg Claytone005f2c2010-11-06 01:53:30 +00001839 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001840
1841 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +00001842 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001843
Greg Clayton43490d12010-07-30 20:12:55 +00001844 m_opaque_sp.reset();
1845}
1846
1847
Chris Lattner24943d22010-06-08 16:52:24 +00001848SBModule
1849SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1850{
1851 SBModule sb_module;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001852 TargetSP target_sp(GetSP());
1853 if (target_sp && sb_file_spec.IsValid())
Greg Claytonbdcda462010-12-20 20:49:23 +00001854 {
Greg Clayton444fe992012-02-26 05:51:37 +00001855 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonbdcda462010-12-20 20:49:23 +00001856 // The module list is thread safe, no need to lock
Greg Clayton444fe992012-02-26 05:51:37 +00001857 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonbdcda462010-12-20 20:49:23 +00001858 }
Chris Lattner24943d22010-06-08 16:52:24 +00001859 return sb_module;
1860}
1861
Greg Clayton1b925202012-01-29 06:07:39 +00001862lldb::ByteOrder
1863SBTarget::GetByteOrder ()
1864{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001865 TargetSP target_sp(GetSP());
1866 if (target_sp)
1867 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton1b925202012-01-29 06:07:39 +00001868 return eByteOrderInvalid;
1869}
1870
1871const char *
1872SBTarget::GetTriple ()
1873{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001874 TargetSP target_sp(GetSP());
1875 if (target_sp)
Greg Clayton1b925202012-01-29 06:07:39 +00001876 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001877 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton1b925202012-01-29 06:07:39 +00001878 // Unique the string so we don't run into ownership issues since
1879 // the const strings put the string into the string pool once and
1880 // the strings never comes out
1881 ConstString const_triple (triple.c_str());
1882 return const_triple.GetCString();
1883 }
1884 return NULL;
1885}
1886
1887uint32_t
1888SBTarget::GetAddressByteSize()
1889{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001890 TargetSP target_sp(GetSP());
1891 if (target_sp)
1892 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton1b925202012-01-29 06:07:39 +00001893 return sizeof(void*);
1894}
1895
1896
Chris Lattner24943d22010-06-08 16:52:24 +00001897SBModule
1898SBTarget::GetModuleAtIndex (uint32_t idx)
1899{
Greg Claytone005f2c2010-11-06 01:53:30 +00001900 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001901
Chris Lattner24943d22010-06-08 16:52:24 +00001902 SBModule sb_module;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001903 ModuleSP module_sp;
1904 TargetSP target_sp(GetSP());
1905 if (target_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001906 {
1907 // The module list is thread safe, no need to lock
Greg Clayton0416bdf2012-01-30 09:04:36 +00001908 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1909 sb_module.SetSP (module_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +00001910 }
Caroline Tice7826c882010-10-26 03:11:13 +00001911
1912 if (log)
1913 {
Greg Clayton49ce6822010-10-31 03:01:06 +00001914 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001915 target_sp.get(), idx, module_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001916 }
1917
Chris Lattner24943d22010-06-08 16:52:24 +00001918 return sb_module;
1919}
1920
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001921bool
1922SBTarget::RemoveModule (lldb::SBModule module)
1923{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001924 TargetSP target_sp(GetSP());
1925 if (target_sp)
1926 return target_sp->GetImages().Remove(module.GetSP());
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001927 return false;
1928}
1929
Chris Lattner24943d22010-06-08 16:52:24 +00001930
1931SBBroadcaster
1932SBTarget::GetBroadcaster () const
1933{
Greg Claytone005f2c2010-11-06 01:53:30 +00001934 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +00001935
Greg Clayton0416bdf2012-01-30 09:04:36 +00001936 TargetSP target_sp(GetSP());
1937 SBBroadcaster broadcaster(target_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +00001938
1939 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +00001940 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001941 target_sp.get(), broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +00001942
Chris Lattner24943d22010-06-08 16:52:24 +00001943 return broadcaster;
1944}
1945
Caroline Tice98f930f2010-09-20 05:20:02 +00001946bool
Caroline Tice7826c882010-10-26 03:11:13 +00001947SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Tice98f930f2010-09-20 05:20:02 +00001948{
Greg Clayton96154be2011-11-13 06:57:31 +00001949 Stream &strm = description.ref();
1950
Greg Clayton0416bdf2012-01-30 09:04:36 +00001951 TargetSP target_sp(GetSP());
1952 if (target_sp)
Caroline Ticee7a566e2010-09-20 16:21:41 +00001953 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001954 target_sp->Dump (&strm, description_level);
Caroline Tice7826c882010-10-26 03:11:13 +00001955 }
1956 else
Greg Clayton96154be2011-11-13 06:57:31 +00001957 strm.PutCString ("No value");
Caroline Tice7826c882010-10-26 03:11:13 +00001958
1959 return true;
1960}
1961
Greg Clayton7dd5c512012-02-06 01:44:54 +00001962lldb::SBSymbolContextList
1963SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Clayton4ed315f2011-06-21 01:34:41 +00001964{
Greg Clayton7dd5c512012-02-06 01:44:54 +00001965 lldb::SBSymbolContextList sb_sc_list;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001966 if (name && name[0])
Greg Clayton4ed315f2011-06-21 01:34:41 +00001967 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001968 TargetSP target_sp(GetSP());
1969 if (target_sp)
1970 {
1971 const bool symbols_ok = true;
Sean Callanan302d78c2012-02-10 22:52:19 +00001972 const bool inlines_ok = true;
Greg Clayton7dd5c512012-02-06 01:44:54 +00001973 const bool append = true;
1974 target_sp->GetImages().FindFunctions (ConstString(name),
1975 name_type_mask,
Sean Callanan302d78c2012-02-10 22:52:19 +00001976 symbols_ok,
1977 inlines_ok,
Greg Clayton7dd5c512012-02-06 01:44:54 +00001978 append,
1979 *sb_sc_list);
Greg Clayton0416bdf2012-01-30 09:04:36 +00001980 }
Greg Clayton4ed315f2011-06-21 01:34:41 +00001981 }
Greg Clayton7dd5c512012-02-06 01:44:54 +00001982 return sb_sc_list;
Greg Clayton4ed315f2011-06-21 01:34:41 +00001983}
1984
Enrico Granata979e20d2011-07-29 19:53:35 +00001985lldb::SBType
1986SBTarget::FindFirstType (const char* type)
1987{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001988 TargetSP target_sp(GetSP());
1989 if (type && target_sp)
Enrico Granata979e20d2011-07-29 19:53:35 +00001990 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00001991 size_t count = target_sp->GetImages().GetSize();
Enrico Granata979e20d2011-07-29 19:53:35 +00001992 for (size_t idx = 0; idx < count; idx++)
1993 {
1994 SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
1995
1996 if (found_at_idx.IsValid())
1997 return found_at_idx;
1998 }
1999 }
2000 return SBType();
2001}
2002
2003lldb::SBTypeList
2004SBTarget::FindTypes (const char* type)
2005{
2006
2007 SBTypeList retval;
2008
Greg Clayton0416bdf2012-01-30 09:04:36 +00002009 TargetSP target_sp(GetSP());
2010 if (type && target_sp)
Enrico Granata979e20d2011-07-29 19:53:35 +00002011 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002012 ModuleList& images = target_sp->GetImages();
Enrico Granata979e20d2011-07-29 19:53:35 +00002013 ConstString name_const(type);
Greg Claytondc0a38c2012-03-26 23:03:23 +00002014 bool exact_match = false;
Enrico Granata979e20d2011-07-29 19:53:35 +00002015 SymbolContext sc;
2016 TypeList type_list;
2017
Greg Clayton9f95fb62012-04-06 17:41:13 +00002018 uint32_t num_matches = images.FindTypes (sc,
Greg Claytondc0a38c2012-03-26 23:03:23 +00002019 name_const,
2020 exact_match,
2021 UINT32_MAX,
2022 type_list);
Enrico Granata979e20d2011-07-29 19:53:35 +00002023
2024 for (size_t idx = 0; idx < num_matches; idx++)
2025 {
Greg Clayton0fb0bcc2011-08-03 22:57:10 +00002026 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2027 if (type_sp)
2028 retval.Append(SBType(type_sp));
Enrico Granata979e20d2011-07-29 19:53:35 +00002029 }
2030 }
2031 return retval;
2032}
2033
Greg Clayton917c0002011-06-29 22:09:02 +00002034SBValueList
2035SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2036{
2037 SBValueList sb_value_list;
2038
Greg Clayton0416bdf2012-01-30 09:04:36 +00002039 TargetSP target_sp(GetSP());
2040 if (name && target_sp)
Greg Clayton917c0002011-06-29 22:09:02 +00002041 {
2042 VariableList variable_list;
2043 const bool append = true;
Greg Clayton0416bdf2012-01-30 09:04:36 +00002044 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2045 append,
2046 max_matches,
2047 variable_list);
Greg Clayton917c0002011-06-29 22:09:02 +00002048
2049 if (match_count > 0)
2050 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002051 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Clayton917c0002011-06-29 22:09:02 +00002052 if (exe_scope == NULL)
Greg Clayton0416bdf2012-01-30 09:04:36 +00002053 exe_scope = target_sp.get();
Greg Clayton917c0002011-06-29 22:09:02 +00002054 ValueObjectList &value_object_list = sb_value_list.ref();
2055 for (uint32_t i=0; i<match_count; ++i)
2056 {
2057 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2058 if (valobj_sp)
2059 value_object_list.Append(valobj_sp);
2060 }
2061 }
2062 }
2063
2064 return sb_value_list;
2065}
2066
Jim Inghamcc637462011-09-13 00:29:56 +00002067SBSourceManager
2068SBTarget::GetSourceManager()
2069{
2070 SBSourceManager source_manager (*this);
2071 return source_manager;
2072}
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002073
Sean Callananef1f6902011-12-14 23:49:37 +00002074lldb::SBInstructionList
Greg Claytona9893072012-03-06 22:24:44 +00002075SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2076{
2077 SBInstructionList sb_instructions;
2078
2079 TargetSP target_sp(GetSP());
2080 if (target_sp)
2081 {
2082 Address *addr_ptr = base_addr.get();
2083
2084 if (addr_ptr)
2085 {
2086 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2087 bool prefer_file_cache = false;
2088 lldb_private::Error error;
2089 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(), data.GetByteSize(), error);
2090 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2091 NULL,
2092 *addr_ptr,
2093 data.GetBytes(),
2094 bytes_read,
2095 count));
2096 }
2097 }
2098
2099 return sb_instructions;
2100
2101}
2102
2103lldb::SBInstructionList
Sean Callananef1f6902011-12-14 23:49:37 +00002104SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2105{
2106 SBInstructionList sb_instructions;
2107
Greg Clayton0416bdf2012-01-30 09:04:36 +00002108 TargetSP target_sp(GetSP());
2109 if (target_sp)
Sean Callananef1f6902011-12-14 23:49:37 +00002110 {
2111 Address addr;
2112
2113 if (base_addr.get())
2114 addr = *base_addr.get();
2115
Greg Clayton0416bdf2012-01-30 09:04:36 +00002116 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callananef1f6902011-12-14 23:49:37 +00002117 NULL,
2118 addr,
2119 buf,
2120 size));
2121 }
2122
2123 return sb_instructions;
2124}
2125
2126lldb::SBInstructionList
2127SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2128{
2129 return GetInstructions (ResolveLoadAddress(base_addr), buf, size);
2130}
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002131
2132SBError
2133SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2134 lldb::addr_t section_base_addr)
2135{
2136 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +00002137 TargetSP target_sp(GetSP());
2138 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002139 {
2140 if (!section.IsValid())
2141 {
2142 sb_error.SetErrorStringWithFormat ("invalid section");
2143 }
2144 else
2145 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002146 SectionSP section_sp (section.GetSP());
2147 if (section_sp)
2148 {
2149 if (section_sp->IsThreadSpecific())
2150 {
2151 sb_error.SetErrorString ("thread specific sections are not yet supported");
2152 }
2153 else
2154 {
Greg Clayton545762f2012-07-07 01:24:12 +00002155 target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr);
Greg Clayton9ab696e2012-03-27 21:10:07 +00002156 }
2157 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002158 }
2159 }
2160 else
2161 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002162 sb_error.SetErrorString ("invalid target");
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002163 }
2164 return sb_error;
2165}
2166
2167SBError
2168SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2169{
2170 SBError sb_error;
2171
Greg Clayton0416bdf2012-01-30 09:04:36 +00002172 TargetSP target_sp(GetSP());
2173 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002174 {
2175 if (!section.IsValid())
2176 {
2177 sb_error.SetErrorStringWithFormat ("invalid section");
2178 }
2179 else
2180 {
Greg Clayton545762f2012-07-07 01:24:12 +00002181 target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP());
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002182 }
2183 }
2184 else
2185 {
2186 sb_error.SetErrorStringWithFormat ("invalid target");
2187 }
2188 return sb_error;
2189}
2190
2191SBError
2192SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2193{
2194 SBError sb_error;
2195
Greg Clayton0416bdf2012-01-30 09:04:36 +00002196 TargetSP target_sp(GetSP());
2197 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002198 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002199 ModuleSP module_sp (module.GetSP());
2200 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002201 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002202 bool changed = false;
2203 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002204 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002205 // The load was successful, make sure that at least some sections
2206 // changed before we notify that our module was loaded.
2207 if (changed)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002208 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002209 ModuleList module_list;
2210 module_list.Append(module_sp);
2211 target_sp->ModulesDidLoad (module_list);
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002212 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002213 }
2214 }
Greg Clayton0416bdf2012-01-30 09:04:36 +00002215 else
2216 {
2217 sb_error.SetErrorStringWithFormat ("invalid module");
2218 }
2219
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002220 }
2221 else
2222 {
2223 sb_error.SetErrorStringWithFormat ("invalid target");
2224 }
2225 return sb_error;
2226}
2227
2228SBError
2229SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2230{
2231 SBError sb_error;
2232
2233 char path[PATH_MAX];
Greg Clayton0416bdf2012-01-30 09:04:36 +00002234 TargetSP target_sp(GetSP());
2235 if (target_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002236 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002237 ModuleSP module_sp (module.GetSP());
2238 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002239 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002240 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002241 if (objfile)
2242 {
2243 SectionList *section_list = objfile->GetSectionList();
2244 if (section_list)
2245 {
2246 const size_t num_sections = section_list->GetSize();
2247 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2248 {
2249 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2250 if (section_sp)
Greg Clayton545762f2012-07-07 01:24:12 +00002251 target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp);
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002252 }
2253 }
2254 else
2255 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002256 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002257 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2258 }
2259 }
2260 else
2261 {
Greg Clayton0416bdf2012-01-30 09:04:36 +00002262 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002263 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2264 }
2265 }
Greg Clayton0416bdf2012-01-30 09:04:36 +00002266 else
2267 {
2268 sb_error.SetErrorStringWithFormat ("invalid module");
2269 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +00002270 }
2271 else
2272 {
2273 sb_error.SetErrorStringWithFormat ("invalid target");
2274 }
2275 return sb_error;
2276}
2277
2278