blob: 8b3f6156e204c5f65265ab4efa30fdc0024590d7 [file] [log] [blame]
Chris Lattner30fdc8d2010-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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Eli Friedman4c5de692010-06-09 07:44:37 +000012#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytone0d378b2011-03-24 21:19:54 +000014#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Greg Claytondea8cb42011-06-29 22:09:02 +000016#include "lldb/API/SBDebugger.h"
17#include "lldb/API/SBBreakpoint.h"
Greg Clayton4b63a5c2013-01-04 18:10:18 +000018#include "lldb/API/SBExpressionOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/API/SBFileSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000020#include "lldb/API/SBListener.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/API/SBModule.h"
Greg Clayton226cce22013-07-08 22:22:41 +000022#include "lldb/API/SBModuleSpec.h"
Jim Inghame37d6052011-09-13 00:29:56 +000023#include "lldb/API/SBSourceManager.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000024#include "lldb/API/SBProcess.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000025#include "lldb/API/SBStream.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000026#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Breakpoint/BreakpointID.h"
28#include "lldb/Breakpoint/BreakpointIDList.h"
29#include "lldb/Breakpoint/BreakpointList.h"
30#include "lldb/Breakpoint/BreakpointLocation.h"
31#include "lldb/Core/Address.h"
32#include "lldb/Core/AddressResolver.h"
33#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/ArchSpec.h"
35#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000037#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000038#include "lldb/Core/Module.h"
39#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/RegularExpression.h"
41#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000042#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Core/STLUtils.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000044#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000045#include "lldb/Core/ValueObjectList.h"
46#include "lldb/Core/ValueObjectVariable.h"
47#include "lldb/Host/FileSpec.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000048#include "lldb/Host/Host.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000049#include "lldb/Interpreter/Args.h"
Greg Clayton1f746072012-08-29 21:13:06 +000050#include "lldb/Symbol/ObjectFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000051#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000052#include "lldb/Symbol/VariableList.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000053#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000055
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#include "lldb/Target/Target.h"
57#include "lldb/Target/TargetList.h"
58
59#include "lldb/Interpreter/CommandReturnObject.h"
60#include "../source/Commands/CommandObjectBreakpoint.h"
Carlo Kok0fd6fd42014-09-19 19:38:19 +000061#include "llvm/Support/Regex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
64using namespace lldb;
65using namespace lldb_private;
66
67#define DEFAULT_DISASM_BYTE_SIZE 32
68
Greg Clayton38d1f052012-02-24 20:59:25 +000069SBLaunchInfo::SBLaunchInfo (const char **argv) :
70 m_opaque_sp(new ProcessLaunchInfo())
Greg Clayton0e615682012-02-24 05:03:03 +000071{
Greg Clayton38d1f052012-02-24 20:59:25 +000072 m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
73 if (argv && argv[0])
74 m_opaque_sp->GetArguments().SetArguments(argv);
Greg Clayton0e615682012-02-24 05:03:03 +000075}
76
Greg Claytonecc7c0d2012-03-07 23:52:51 +000077SBLaunchInfo::~SBLaunchInfo()
78{
79}
80
81lldb_private::ProcessLaunchInfo &
82SBLaunchInfo::ref ()
83{
84 return *m_opaque_sp;
85}
86
87
Greg Clayton0e615682012-02-24 05:03:03 +000088uint32_t
89SBLaunchInfo::GetUserID()
90{
91 return m_opaque_sp->GetUserID();
92}
93
94uint32_t
95SBLaunchInfo::GetGroupID()
96{
97 return m_opaque_sp->GetGroupID();
98}
99
100bool
101SBLaunchInfo::UserIDIsValid ()
102{
103 return m_opaque_sp->UserIDIsValid();
104}
105
106bool
107SBLaunchInfo::GroupIDIsValid ()
108{
109 return m_opaque_sp->GroupIDIsValid();
110}
111
112void
113SBLaunchInfo::SetUserID (uint32_t uid)
114{
115 m_opaque_sp->SetUserID (uid);
116}
117
118void
119SBLaunchInfo::SetGroupID (uint32_t gid)
120{
121 m_opaque_sp->SetGroupID (gid);
122}
123
Greg Clayton3e32ad62014-05-07 20:16:06 +0000124SBFileSpec
125SBLaunchInfo::GetExecutableFile ()
126{
127 return SBFileSpec (m_opaque_sp->GetExecutableFile());
128}
129
130void
131SBLaunchInfo::SetExecutableFile (SBFileSpec exe_file, bool add_as_first_arg)
132{
133 m_opaque_sp->SetExecutableFile(exe_file.ref(), add_as_first_arg);
134}
135
Greg Clayton8012cad2014-11-17 19:39:20 +0000136SBListener
137SBLaunchInfo::GetListener ()
138{
139 return SBListener(m_opaque_sp->GetListener());
140}
141
142void
143SBLaunchInfo::SetListener (SBListener &listener)
144{
145 m_opaque_sp->SetListener(listener.GetSP());
146}
147
Greg Clayton0e615682012-02-24 05:03:03 +0000148uint32_t
149SBLaunchInfo::GetNumArguments ()
150{
151 return m_opaque_sp->GetArguments().GetArgumentCount();
152}
153
154const char *
155SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
156{
157 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
158}
159
160void
161SBLaunchInfo::SetArguments (const char **argv, bool append)
162{
163 if (append)
164 {
165 if (argv)
166 m_opaque_sp->GetArguments().AppendArguments(argv);
167 }
168 else
169 {
170 if (argv)
171 m_opaque_sp->GetArguments().SetArguments(argv);
172 else
173 m_opaque_sp->GetArguments().Clear();
174 }
175}
176
177uint32_t
178SBLaunchInfo::GetNumEnvironmentEntries ()
179{
180 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
181}
182
183const char *
184SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
185{
186 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
187}
188
189void
190SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
191{
192 if (append)
193 {
194 if (envp)
195 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
196 }
197 else
198 {
199 if (envp)
200 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
201 else
202 m_opaque_sp->GetEnvironmentEntries().Clear();
203 }
204}
205
206void
207SBLaunchInfo::Clear ()
208{
209 m_opaque_sp->Clear();
210}
211
212const char *
213SBLaunchInfo::GetWorkingDirectory () const
214{
215 return m_opaque_sp->GetWorkingDirectory();
216}
217
218void
219SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
220{
221 m_opaque_sp->SetWorkingDirectory(working_dir);
222}
223
224uint32_t
225SBLaunchInfo::GetLaunchFlags ()
226{
227 return m_opaque_sp->GetFlags().Get();
228}
229
230void
231SBLaunchInfo::SetLaunchFlags (uint32_t flags)
232{
233 m_opaque_sp->GetFlags().Reset(flags);
234}
235
236const char *
237SBLaunchInfo::GetProcessPluginName ()
238{
239 return m_opaque_sp->GetProcessPluginName();
240}
241
242void
243SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
244{
245 return m_opaque_sp->SetProcessPluginName (plugin_name);
246}
247
248const char *
249SBLaunchInfo::GetShell ()
250{
Zachary Turner10687b02014-10-20 17:46:43 +0000251 // Constify this string so that it is saved in the string pool. Otherwise
252 // it would be freed when this function goes out of scope.
253 ConstString shell(m_opaque_sp->GetShell().GetPath().c_str());
254 return shell.AsCString();
Greg Clayton0e615682012-02-24 05:03:03 +0000255}
256
257void
258SBLaunchInfo::SetShell (const char * path)
259{
Zachary Turner10687b02014-10-20 17:46:43 +0000260 m_opaque_sp->SetShell (FileSpec(path, false));
Greg Clayton0e615682012-02-24 05:03:03 +0000261}
262
263uint32_t
264SBLaunchInfo::GetResumeCount ()
265{
266 return m_opaque_sp->GetResumeCount();
267}
268
269void
270SBLaunchInfo::SetResumeCount (uint32_t c)
271{
272 m_opaque_sp->SetResumeCount (c);
273}
274
275bool
276SBLaunchInfo::AddCloseFileAction (int fd)
277{
278 return m_opaque_sp->AppendCloseFileAction(fd);
279}
280
281bool
282SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
283{
284 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
285}
286
287bool
288SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
289{
290 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
291}
292
293bool
294SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
295{
296 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
297}
298
Jason Molendaa3329782014-03-29 18:54:20 +0000299void
300SBLaunchInfo::SetLaunchEventData (const char *data)
301{
302 m_opaque_sp->SetLaunchEventData (data);
303}
304
305const char *
306SBLaunchInfo::GetLaunchEventData () const
307{
308 return m_opaque_sp->GetLaunchEventData ();
309}
Greg Clayton0e615682012-02-24 05:03:03 +0000310
Jim Ingham106d0282014-06-25 02:32:56 +0000311void
312SBLaunchInfo::SetDetachOnError (bool enable)
313{
314 m_opaque_sp->SetDetachOnError (enable);
315}
316
317bool
318SBLaunchInfo::GetDetachOnError () const
319{
320 return m_opaque_sp->GetDetachOnError ();
321}
322
Greg Clayton0e615682012-02-24 05:03:03 +0000323SBAttachInfo::SBAttachInfo () :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000324 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000325{
326}
327
328SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000329 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000330{
331 m_opaque_sp->SetProcessID (pid);
332}
333
334SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000335 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000336{
337 if (path && path[0])
338 m_opaque_sp->GetExecutableFile().SetFile(path, false);
339 m_opaque_sp->SetWaitForLaunch (wait_for);
340}
341
342SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000343 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000344{
345 *m_opaque_sp = *rhs.m_opaque_sp;
346}
347
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000348SBAttachInfo::~SBAttachInfo()
349{
350}
351
352lldb_private::ProcessAttachInfo &
353SBAttachInfo::ref ()
354{
355 return *m_opaque_sp;
356}
357
Greg Clayton0e615682012-02-24 05:03:03 +0000358SBAttachInfo &
359SBAttachInfo::operator = (const SBAttachInfo &rhs)
360{
361 if (this != &rhs)
362 *m_opaque_sp = *rhs.m_opaque_sp;
363 return *this;
364}
365
366lldb::pid_t
367SBAttachInfo::GetProcessID ()
368{
369 return m_opaque_sp->GetProcessID();
370}
371
372void
373SBAttachInfo::SetProcessID (lldb::pid_t pid)
374{
375 m_opaque_sp->SetProcessID (pid);
376}
377
378
379uint32_t
380SBAttachInfo::GetResumeCount ()
381{
382 return m_opaque_sp->GetResumeCount();
383}
384
385void
386SBAttachInfo::SetResumeCount (uint32_t c)
387{
388 m_opaque_sp->SetResumeCount (c);
389}
390
391const char *
392SBAttachInfo::GetProcessPluginName ()
393{
394 return m_opaque_sp->GetProcessPluginName();
395}
396
397void
398SBAttachInfo::SetProcessPluginName (const char *plugin_name)
399{
400 return m_opaque_sp->SetProcessPluginName (plugin_name);
401}
402
403void
404SBAttachInfo::SetExecutable (const char *path)
405{
406 if (path && path[0])
407 m_opaque_sp->GetExecutableFile().SetFile(path, false);
408 else
409 m_opaque_sp->GetExecutableFile().Clear();
410}
411
412void
413SBAttachInfo::SetExecutable (SBFileSpec exe_file)
414{
415 if (exe_file.IsValid())
416 m_opaque_sp->GetExecutableFile() = exe_file.ref();
417 else
418 m_opaque_sp->GetExecutableFile().Clear();
419}
420
421bool
422SBAttachInfo::GetWaitForLaunch ()
423{
424 return m_opaque_sp->GetWaitForLaunch();
425}
426
427void
428SBAttachInfo::SetWaitForLaunch (bool b)
429{
430 m_opaque_sp->SetWaitForLaunch (b);
431}
432
Jim Inghamcd16df92012-07-20 21:37:13 +0000433bool
434SBAttachInfo::GetIgnoreExisting ()
435{
436 return m_opaque_sp->GetIgnoreExisting();
437}
438
439void
440SBAttachInfo::SetIgnoreExisting (bool b)
441{
442 m_opaque_sp->SetIgnoreExisting (b);
443}
444
Greg Clayton0e615682012-02-24 05:03:03 +0000445uint32_t
Greg Clayton41bd8ac2012-02-24 23:56:06 +0000446SBAttachInfo::GetUserID()
447{
448 return m_opaque_sp->GetUserID();
449}
450
451uint32_t
452SBAttachInfo::GetGroupID()
453{
454 return m_opaque_sp->GetGroupID();
455}
456
457bool
458SBAttachInfo::UserIDIsValid ()
459{
460 return m_opaque_sp->UserIDIsValid();
461}
462
463bool
464SBAttachInfo::GroupIDIsValid ()
465{
466 return m_opaque_sp->GroupIDIsValid();
467}
468
469void
470SBAttachInfo::SetUserID (uint32_t uid)
471{
472 m_opaque_sp->SetUserID (uid);
473}
474
475void
476SBAttachInfo::SetGroupID (uint32_t gid)
477{
478 m_opaque_sp->SetGroupID (gid);
479}
480
481uint32_t
Greg Clayton0e615682012-02-24 05:03:03 +0000482SBAttachInfo::GetEffectiveUserID()
483{
484 return m_opaque_sp->GetEffectiveUserID();
485}
486
487uint32_t
488SBAttachInfo::GetEffectiveGroupID()
489{
490 return m_opaque_sp->GetEffectiveGroupID();
491}
492
493bool
494SBAttachInfo::EffectiveUserIDIsValid ()
495{
496 return m_opaque_sp->EffectiveUserIDIsValid();
497}
498
499bool
500SBAttachInfo::EffectiveGroupIDIsValid ()
501{
502 return m_opaque_sp->EffectiveGroupIDIsValid ();
503}
504
505void
506SBAttachInfo::SetEffectiveUserID (uint32_t uid)
507{
508 m_opaque_sp->SetEffectiveUserID(uid);
509}
510
511void
512SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
513{
514 m_opaque_sp->SetEffectiveGroupID(gid);
515}
516
517lldb::pid_t
518SBAttachInfo::GetParentProcessID ()
519{
520 return m_opaque_sp->GetParentProcessID();
521}
522
523void
524SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
525{
526 m_opaque_sp->SetParentProcessID (pid);
527}
528
529bool
530SBAttachInfo::ParentProcessIDIsValid()
531{
532 return m_opaque_sp->ParentProcessIDIsValid();
533}
534
Greg Clayton8012cad2014-11-17 19:39:20 +0000535SBListener
536SBAttachInfo::GetListener ()
537{
538 return SBListener(m_opaque_sp->GetListener());
539}
540
541void
542SBAttachInfo::SetListener (SBListener &listener)
543{
544 m_opaque_sp->SetListener(listener.GetSP());
545}
Greg Clayton0e615682012-02-24 05:03:03 +0000546
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547//----------------------------------------------------------------------
548// SBTarget constructor
549//----------------------------------------------------------------------
Greg Clayton54979cd2010-12-15 05:08:08 +0000550SBTarget::SBTarget () :
551 m_opaque_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552{
553}
554
555SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton66111032010-06-23 01:19:29 +0000556 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557{
558}
559
560SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton66111032010-06-23 01:19:29 +0000561 m_opaque_sp (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562{
563}
564
Greg Claytonefabb122010-11-05 23:17:00 +0000565const SBTarget&
566SBTarget::operator = (const SBTarget& rhs)
567{
568 if (this != &rhs)
569 m_opaque_sp = rhs.m_opaque_sp;
570 return *this;
571}
572
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573//----------------------------------------------------------------------
574// Destructor
575//----------------------------------------------------------------------
576SBTarget::~SBTarget()
577{
578}
579
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000580const char *
581SBTarget::GetBroadcasterClassName ()
582{
583 return Target::GetStaticBroadcasterClass().AsCString();
584}
585
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586bool
587SBTarget::IsValid () const
588{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000589 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590}
591
592SBProcess
593SBTarget::GetProcess ()
594{
595 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000596 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000597 TargetSP target_sp(GetSP());
598 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000599 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000600 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000601 sb_process.SetSP (process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000602 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000603
Greg Clayton5160ce52013-03-27 23:08:40 +0000604 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000605 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000606 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
607 static_cast<void*>(target_sp.get()),
608 static_cast<void*>(process_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000609
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 return sb_process;
611}
612
Matthew Gardinerc928de32014-10-22 07:22:56 +0000613SBPlatform
614SBTarget::GetPlatform ()
615{
616 TargetSP target_sp(GetSP());
617 if (!target_sp)
618 return SBPlatform();
619
620 SBPlatform platform;
621 platform.m_opaque_sp = target_sp->GetPlatform();
622
623 return platform;
624}
625
Greg Clayton66111032010-06-23 01:19:29 +0000626SBDebugger
627SBTarget::GetDebugger () const
628{
629 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000630 TargetSP target_sp(GetSP());
631 if (target_sp)
632 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000633 return debugger;
634}
635
Jim Ingham270684d2011-03-31 00:01:24 +0000636SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000637SBTarget::LoadCore (const char *core_file)
638{
639 SBProcess sb_process;
640 TargetSP target_sp(GetSP());
641 if (target_sp)
642 {
643 FileSpec filespec(core_file, true);
644 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
645 NULL,
646 &filespec));
647 if (process_sp)
648 {
649 process_sp->LoadCore();
650 sb_process.SetSP (process_sp);
651 }
652 }
653 return sb_process;
654}
655
656SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000657SBTarget::LaunchSimple
658(
659 char const **argv,
660 char const **envp,
661 const char *working_directory
662)
663{
664 char *stdin_path = NULL;
665 char *stdout_path = NULL;
666 char *stderr_path = NULL;
667 uint32_t launch_flags = 0;
668 bool stop_at_entry = false;
669 SBError error;
670 SBListener listener = GetDebugger().GetListener();
671 return Launch (listener,
672 argv,
673 envp,
674 stdin_path,
675 stdout_path,
676 stderr_path,
677 working_directory,
678 launch_flags,
679 stop_at_entry,
680 error);
681}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000682
Greg Claytonfbb76342013-11-20 21:07:01 +0000683SBError
684SBTarget::Install()
685{
686 SBError sb_error;
687 TargetSP target_sp(GetSP());
688 if (target_sp)
689 {
690 Mutex::Locker api_locker (target_sp->GetAPIMutex());
691 sb_error.ref() = target_sp->Install(NULL);
692 }
693 return sb_error;
694}
695
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000696SBProcess
697SBTarget::Launch
698(
Greg Clayton4b045622011-02-03 21:28:34 +0000699 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000700 char const **argv,
701 char const **envp,
702 const char *stdin_path,
703 const char *stdout_path,
704 const char *stderr_path,
705 const char *working_directory,
706 uint32_t launch_flags, // See LaunchFlags
707 bool stop_at_entry,
708 lldb::SBError& error
709)
710{
Greg Clayton5160ce52013-03-27 23:08:40 +0000711 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000712
Greg Claytonacdbe812012-01-30 09:04:36 +0000713 SBProcess sb_process;
714 ProcessSP process_sp;
715 TargetSP target_sp(GetSP());
716
Caroline Ticeceb6b132010-10-26 03:11:13 +0000717 if (log)
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000718 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))...",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000719 static_cast<void*>(target_sp.get()),
720 static_cast<void*>(argv), static_cast<void*>(envp),
721 stdin_path ? stdin_path : "NULL",
722 stdout_path ? stdout_path : "NULL",
723 stderr_path ? stderr_path : "NULL",
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000724 working_directory ? working_directory : "NULL",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000725 launch_flags, stop_at_entry,
726 static_cast<void*>(error.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000727
728 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000730 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000731
Greg Clayton645bf542011-01-27 01:01:10 +0000732 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
733 launch_flags |= eLaunchFlagDisableASLR;
734
Greg Clayton2289fa42011-04-30 01:09:13 +0000735 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000736 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000737 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000738 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000739 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000740
Greg Claytonb9556ac2012-01-30 07:41:31 +0000741 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000742 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000743 if (state == eStateAttaching)
744 error.SetErrorString ("process attach is in progress");
745 else
746 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000747 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000748 }
Greg Clayton931180e2011-01-27 06:44:37 +0000749 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000750
Greg Clayton2289fa42011-04-30 01:09:13 +0000751 if (state == eStateConnected)
752 {
753 // If we are already connected, then we have already specified the
754 // listener, so if a valid listener is supplied, we need to error out
755 // to let the client know.
756 if (listener.IsValid())
757 {
758 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000759 return sb_process;
760 }
761 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000762
763 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
764 launch_flags |= eLaunchFlagDisableSTDIO;
765
766 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000767
Greg Claytonb09c5382013-12-13 17:20:18 +0000768 Module *exe_module = target_sp->GetExecutableModulePointer();
769 if (exe_module)
770 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
771 if (argv)
772 launch_info.GetArguments().AppendArguments (argv);
773 if (envp)
774 launch_info.GetEnvironmentEntries ().SetArguments (envp);
775
776 if (listener.IsValid())
Greg Clayton8012cad2014-11-17 19:39:20 +0000777 launch_info.SetListener(listener.GetSP());
778
779 error.SetError (target_sp->Launch(launch_info, NULL));
Greg Clayton645bf542011-01-27 01:01:10 +0000780
Greg Claytonb09c5382013-12-13 17:20:18 +0000781 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton524e60b2010-10-06 22:10:17 +0000782 }
783 else
784 {
785 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000787
Caroline Tice20ad3c42010-10-29 21:48:37 +0000788 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000789 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000790 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
791 static_cast<void*>(target_sp.get()),
792 static_cast<void*>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000793
Greg Clayton5d5028b2010-10-06 03:53:16 +0000794 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795}
796
Greg Clayton0e615682012-02-24 05:03:03 +0000797SBProcess
798SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
799{
Greg Clayton5160ce52013-03-27 23:08:40 +0000800 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000801
Greg Clayton0e615682012-02-24 05:03:03 +0000802 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000803 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000804
Greg Clayton0e615682012-02-24 05:03:03 +0000805 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000806 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...",
807 static_cast<void*>(target_sp.get()));
808
Greg Clayton0e615682012-02-24 05:03:03 +0000809 if (target_sp)
810 {
811 Mutex::Locker api_locker (target_sp->GetAPIMutex());
812 StateType state = eStateInvalid;
Greg Claytonb09c5382013-12-13 17:20:18 +0000813 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000814 ProcessSP process_sp = target_sp->GetProcessSP();
815 if (process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000816 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000817 state = process_sp->GetState();
818
819 if (process_sp->IsAlive() && state != eStateConnected)
820 {
821 if (state == eStateAttaching)
822 error.SetErrorString ("process attach is in progress");
823 else
824 error.SetErrorString ("a process is already being debugged");
825 return sb_process;
826 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000827 }
Greg Clayton0e615682012-02-24 05:03:03 +0000828 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000829
830 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
831
Greg Clayton3e32ad62014-05-07 20:16:06 +0000832 if (!launch_info.GetExecutableFile())
833 {
834 Module *exe_module = target_sp->GetExecutableModulePointer();
835 if (exe_module)
836 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
837 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000838
839 const ArchSpec &arch_spec = target_sp->GetArchitecture();
840 if (arch_spec.IsValid())
841 launch_info.GetArchitecture () = arch_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000842
Greg Clayton8012cad2014-11-17 19:39:20 +0000843 error.SetError (target_sp->Launch (launch_info, NULL));
Greg Claytonb09c5382013-12-13 17:20:18 +0000844 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000845 }
846 else
847 {
848 error.SetErrorString ("SBTarget is invalid");
849 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000850
Greg Clayton0e615682012-02-24 05:03:03 +0000851 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
852 if (log)
Greg Claytonb09c5382013-12-13 17:20:18 +0000853 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000854 static_cast<void*>(target_sp.get()),
855 static_cast<void*>(sb_process.GetSP().get()));
856
Greg Clayton0e615682012-02-24 05:03:03 +0000857 return sb_process;
858}
859
860lldb::SBProcess
861SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
862{
Greg Clayton5160ce52013-03-27 23:08:40 +0000863 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000864
Greg Clayton0e615682012-02-24 05:03:03 +0000865 SBProcess sb_process;
866 ProcessSP process_sp;
867 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000868
Sean Callanan575a4542012-10-20 00:21:31 +0000869 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000870 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...",
871 static_cast<void*>(target_sp.get()));
872
Greg Clayton0e615682012-02-24 05:03:03 +0000873 if (target_sp)
874 {
875 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000876
Greg Clayton0e615682012-02-24 05:03:03 +0000877 StateType state = eStateInvalid;
878 process_sp = target_sp->GetProcessSP();
879 if (process_sp)
880 {
881 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000882
Greg Clayton0e615682012-02-24 05:03:03 +0000883 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000884 {
Greg Clayton0e615682012-02-24 05:03:03 +0000885 if (state == eStateAttaching)
886 error.SetErrorString ("process attach is in progress");
887 else
888 error.SetErrorString ("a process is already being debugged");
Sean Callanan575a4542012-10-20 00:21:31 +0000889 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000890 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000891 static_cast<void*>(target_sp.get()),
892 error.GetCString());
Greg Clayton0e615682012-02-24 05:03:03 +0000893 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000894 }
Greg Clayton0e615682012-02-24 05:03:03 +0000895 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000896
Greg Clayton0e615682012-02-24 05:03:03 +0000897 if (state != eStateConnected)
898 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
899
900 if (process_sp)
901 {
Greg Clayton0e615682012-02-24 05:03:03 +0000902 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000903 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000904 {
905 PlatformSP platform_sp = target_sp->GetPlatform();
Greg Clayton91e407e2012-09-27 00:03:39 +0000906 // See if we can pre-verify if a process exists or not
907 if (platform_sp && platform_sp->IsConnected())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000908 {
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000909 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Clayton91e407e2012-09-27 00:03:39 +0000910 ProcessInstanceInfo instance_info;
911 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
912 {
913 attach_info.SetUserID(instance_info.GetEffectiveUserID());
914 }
915 else
916 {
Daniel Malead01b2952012-11-29 21:49:15 +0000917 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000918 if (log)
919 {
920 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000921 static_cast<void*>(target_sp.get()), error.GetCString());
Sean Callanan575a4542012-10-20 00:21:31 +0000922 }
Greg Clayton91e407e2012-09-27 00:03:39 +0000923 return sb_process;
924 }
Han Ming Ongcec8c902012-02-29 00:12:56 +0000925 }
926 }
Han Ming Ongbee98392012-02-29 19:16:40 +0000927 error.SetError (process_sp->Attach (attach_info));
928 if (error.Success())
929 {
930 sb_process.SetSP (process_sp);
931 // If we are doing synchronous mode, then wait for the
932 // process to stop!
933 if (target_sp->GetDebugger().GetAsyncExecution () == false)
934 process_sp->WaitForProcessToStop (NULL);
935 }
Greg Clayton0e615682012-02-24 05:03:03 +0000936 }
937 else
938 {
939 error.SetErrorString ("unable to create lldb_private::Process");
940 }
941 }
942 else
943 {
944 error.SetErrorString ("SBTarget is invalid");
945 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000946
Sean Callanan575a4542012-10-20 00:21:31 +0000947 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000948 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000949 static_cast<void*>(target_sp.get()),
950 static_cast<void*>(process_sp.get()));
951
Greg Clayton0e615682012-02-24 05:03:03 +0000952 return sb_process;
953}
954
955
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000956#if defined(__APPLE__)
957
958lldb::SBProcess
959SBTarget::AttachToProcessWithID (SBListener &listener,
960 ::pid_t pid,
961 lldb::SBError& error)
962{
963 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
964}
965
966#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000967
968lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000969SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000970(
Greg Clayton4b045622011-02-03 21:28:34 +0000971 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000972 lldb::pid_t pid,// The process ID to attach to
973 SBError& error // An error explaining what went wrong if attach fails
974)
975{
Greg Clayton5160ce52013-03-27 23:08:40 +0000976 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000977
Greg Clayton524e60b2010-10-06 22:10:17 +0000978 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000979 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000980 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000981
982 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000983 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...",
984 static_cast<void*>(target_sp.get()), pid);
985
Greg Claytonacdbe812012-01-30 09:04:36 +0000986 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000987 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000988 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0c74e782011-06-24 03:21:43 +0000989
990 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000991 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000992 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +0000993 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000994 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000995
Greg Claytonb9556ac2012-01-30 07:41:31 +0000996 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000997 {
Greg Clayton0c74e782011-06-24 03:21:43 +0000998 if (state == eStateAttaching)
999 error.SetErrorString ("process attach is in progress");
1000 else
1001 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001002 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001003 }
Greg Clayton0c74e782011-06-24 03:21:43 +00001004 }
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 Clayton0c74e782011-06-24 03:21:43 +00001014 return sb_process;
1015 }
1016 }
Greg Clayton4b045622011-02-03 21:28:34 +00001017 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001018 {
1019 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001020 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001021 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001022 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001023 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001024 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001025 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001026 sb_process.SetSP (process_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001027
Greg Clayton144f3a92011-11-15 03:53:30 +00001028 ProcessAttachInfo attach_info;
1029 attach_info.SetProcessID (pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001030
Han Ming Ong84647042012-02-25 01:07:38 +00001031 PlatformSP platform_sp = target_sp->GetPlatform();
1032 ProcessInstanceInfo instance_info;
1033 if (platform_sp->GetProcessInfo(pid, instance_info))
1034 {
1035 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ong84647042012-02-25 01:07:38 +00001036 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001037 error.SetError (process_sp->Attach (attach_info));
Greg Claytone2186ed2012-09-07 17:51:47 +00001038 if (error.Success())
1039 {
1040 // If we are doing synchronous mode, then wait for the
1041 // process to stop!
1042 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytondc6224e2014-10-21 01:00:42 +00001043 process_sp->WaitForProcessToStop (NULL);
Greg Claytone2186ed2012-09-07 17:51:47 +00001044 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001045 }
1046 else
1047 {
1048 error.SetErrorString ("unable to create lldb_private::Process");
1049 }
1050 }
1051 else
1052 {
1053 error.SetErrorString ("SBTarget is invalid");
1054 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001055
Sean Callanan575a4542012-10-20 00:21:31 +00001056 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001057 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001058 static_cast<void*>(target_sp.get()),
1059 static_cast<void*>(process_sp.get()));
Greg Clayton524e60b2010-10-06 22:10:17 +00001060 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001061}
1062
1063lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +00001064SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +00001065(
Greg Clayton4b045622011-02-03 21:28:34 +00001066 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +00001067 const char *name, // basename of process to attach to
1068 bool wait_for, // if true wait for a new instance of "name" to be launched
1069 SBError& error // An error explaining what went wrong if attach fails
1070)
1071{
Greg Clayton5160ce52013-03-27 23:08:40 +00001072 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001073
Greg Clayton524e60b2010-10-06 22:10:17 +00001074 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001075 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001076 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001077
Sean Callanan575a4542012-10-20 00:21:31 +00001078 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001079 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...",
1080 static_cast<void*>(target_sp.get()), name,
1081 wait_for ? "true" : "false");
1082
Greg Claytonacdbe812012-01-30 09:04:36 +00001083 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001084 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001085 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +00001086
Greg Clayton0c74e782011-06-24 03:21:43 +00001087 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +00001088 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +00001089 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +00001090 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001091 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001092
Greg Claytonb9556ac2012-01-30 07:41:31 +00001093 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001094 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001095 if (state == eStateAttaching)
1096 error.SetErrorString ("process attach is in progress");
1097 else
1098 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001099 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001100 }
Greg Clayton0c74e782011-06-24 03:21:43 +00001101 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001102
Greg Clayton0c74e782011-06-24 03:21:43 +00001103 if (state == eStateConnected)
1104 {
1105 // If we are already connected, then we have already specified the
1106 // listener, so if a valid listener is supplied, we need to error out
1107 // to let the client know.
1108 if (listener.IsValid())
1109 {
1110 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001111 return sb_process;
1112 }
1113 }
Greg Clayton4b045622011-02-03 21:28:34 +00001114 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001115 {
1116 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001117 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001118 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001119 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001120 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001121
Greg Claytonb9556ac2012-01-30 07:41:31 +00001122 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001123 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001124 sb_process.SetSP (process_sp);
Greg Clayton144f3a92011-11-15 03:53:30 +00001125 ProcessAttachInfo attach_info;
1126 attach_info.GetExecutableFile().SetFile(name, false);
1127 attach_info.SetWaitForLaunch(wait_for);
Greg Claytonb9556ac2012-01-30 07:41:31 +00001128 error.SetError (process_sp->Attach (attach_info));
Daniel Malead659dc12013-04-01 19:47:00 +00001129 if (error.Success())
1130 {
1131 // If we are doing synchronous mode, then wait for the
1132 // process to stop!
1133 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1134 process_sp->WaitForProcessToStop (NULL);
1135 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001136 }
1137 else
1138 {
1139 error.SetErrorString ("unable to create lldb_private::Process");
1140 }
1141 }
1142 else
1143 {
1144 error.SetErrorString ("SBTarget is invalid");
1145 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001146
Sean Callanan575a4542012-10-20 00:21:31 +00001147 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001148 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001149 static_cast<void*>(target_sp.get()),
1150 static_cast<void*>(process_sp.get()));
Greg Clayton524e60b2010-10-06 22:10:17 +00001151 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001152}
1153
James McIlree9631aae2011-03-04 00:31:13 +00001154lldb::SBProcess
1155SBTarget::ConnectRemote
1156(
1157 SBListener &listener,
1158 const char *url,
1159 const char *plugin_name,
1160 SBError& error
1161)
1162{
Greg Clayton5160ce52013-03-27 23:08:40 +00001163 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001164
James McIlree9631aae2011-03-04 00:31:13 +00001165 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001166 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001167 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001168
Sean Callanan575a4542012-10-20 00:21:31 +00001169 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001170 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...",
1171 static_cast<void*>(target_sp.get()), url, plugin_name);
1172
Greg Claytonacdbe812012-01-30 09:04:36 +00001173 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001174 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001175 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +00001176 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001177 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001178 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001179 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001180
Greg Claytonb9556ac2012-01-30 07:41:31 +00001181 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001182 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001183 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001184 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +00001185 }
1186 else
1187 {
1188 error.SetErrorString ("unable to create lldb_private::Process");
1189 }
1190 }
1191 else
1192 {
1193 error.SetErrorString ("SBTarget is invalid");
1194 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001195
Sean Callanan575a4542012-10-20 00:21:31 +00001196 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001197 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001198 static_cast<void*>(target_sp.get()),
1199 static_cast<void*>(process_sp.get()));
James McIlree9631aae2011-03-04 00:31:13 +00001200 return sb_process;
1201}
1202
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203SBFileSpec
1204SBTarget::GetExecutable ()
1205{
Caroline Ticeceb6b132010-10-26 03:11:13 +00001206
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +00001208 TargetSP target_sp(GetSP());
1209 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001211 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +00001212 if (exe_module)
1213 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001215
Greg Clayton5160ce52013-03-27 23:08:40 +00001216 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001217 if (log)
1218 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001219 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
1220 static_cast<void*>(target_sp.get()),
1221 static_cast<const void*>(exe_file_spec.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001222 }
1223
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224 return exe_file_spec;
1225}
1226
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228SBTarget::operator == (const SBTarget &rhs) const
1229{
Greg Clayton66111032010-06-23 01:19:29 +00001230 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231}
1232
1233bool
1234SBTarget::operator != (const SBTarget &rhs) const
1235{
Greg Clayton66111032010-06-23 01:19:29 +00001236 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237}
1238
Greg Claytonb9556ac2012-01-30 07:41:31 +00001239lldb::TargetSP
1240SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +00001241{
1242 return m_opaque_sp;
1243}
1244
Greg Clayton66111032010-06-23 01:19:29 +00001245void
Greg Claytonb9556ac2012-01-30 07:41:31 +00001246SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +00001247{
1248 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249}
1250
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001251lldb::SBAddress
1252SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001253{
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001254 lldb::SBAddress sb_addr;
1255 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +00001256 TargetSP target_sp(GetSP());
1257 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001258 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001259 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytond5944cd2013-12-06 01:12:00 +00001260 if (target_sp->ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001261 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001262 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001263
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001264 // We have a load address that isn't in a section, just return an address
1265 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +00001266 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001267 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001268}
1269
Matthew Gardinerc928de32014-10-22 07:22:56 +00001270lldb::SBAddress
1271SBTarget::ResolveFileAddress (lldb::addr_t file_addr)
1272{
1273 lldb::SBAddress sb_addr;
1274 Address &addr = sb_addr.ref();
1275 TargetSP target_sp(GetSP());
1276 if (target_sp)
1277 {
1278 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1279 if (target_sp->ResolveFileAddress (file_addr, addr))
1280 return sb_addr;
1281 }
1282
1283 addr.SetRawAddress(file_addr);
1284 return sb_addr;
1285}
Greg Claytond5944cd2013-12-06 01:12:00 +00001286
1287lldb::SBAddress
1288SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
1289{
1290 lldb::SBAddress sb_addr;
1291 Address &addr = sb_addr.ref();
1292 TargetSP target_sp(GetSP());
1293 if (target_sp)
1294 {
1295 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1296 if (target_sp->ResolveLoadAddress (vm_addr, addr))
1297 return sb_addr;
1298 }
1299
1300 // We have a load address that isn't in a section, just return an address
1301 // with the offset filled in (the address) and the section set to NULL
1302 addr.SetRawAddress(vm_addr);
1303 return sb_addr;
1304}
1305
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001306SBSymbolContext
Greg Claytoneb023e72013-10-11 19:48:25 +00001307SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
1308 uint32_t resolve_scope)
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001309{
1310 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001311 if (addr.IsValid())
1312 {
1313 TargetSP target_sp(GetSP());
1314 if (target_sp)
1315 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1316 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001317 return sc;
1318}
1319
Matthew Gardinerc928de32014-10-22 07:22:56 +00001320size_t
1321SBTarget::ReadMemory (const SBAddress addr,
1322 void *buf,
1323 size_t size,
1324 lldb::SBError &error)
1325{
1326 SBError sb_error;
1327 size_t bytes_read = 0;
1328 TargetSP target_sp(GetSP());
1329 if (target_sp)
1330 {
1331 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton8691dc52014-11-04 00:56:30 +00001332 bytes_read = target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
1333 }
1334 else
1335 {
1336 sb_error.SetErrorString("invalid target");
Matthew Gardinerc928de32014-10-22 07:22:56 +00001337 }
1338
1339 return bytes_read;
1340}
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001341
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001343SBTarget::BreakpointCreateByLocation (const char *file,
1344 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345{
Greg Clayton7481c202010-11-08 00:28:40 +00001346 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347}
1348
1349SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001350SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
1351 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001352{
Greg Clayton5160ce52013-03-27 23:08:40 +00001353 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001354
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001356 TargetSP target_sp(GetSP());
1357 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001358 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001359 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001360
Greg Clayton1f746072012-08-29 21:13:06 +00001361 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001362 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001363 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001364 const bool hardware = false;
1365 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001366 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001367
1368 if (log)
1369 {
1370 SBStream sstr;
1371 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001372 char path[PATH_MAX];
1373 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001374 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1375 static_cast<void*>(target_sp.get()), path, line,
1376 static_cast<void*>(sb_bp.get()), sstr.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001377 }
1378
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 return sb_bp;
1380}
1381
1382SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001383SBTarget::BreakpointCreateByName (const char *symbol_name,
1384 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385{
Greg Clayton5160ce52013-03-27 23:08:40 +00001386 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001387
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001389 TargetSP target_sp(GetSP());
1390 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001392 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001393
Jim Inghama8558b62012-05-22 00:12:20 +00001394 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001395 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001396 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397 if (module_name && module_name[0])
1398 {
Jim Ingham969795f2011-09-21 01:17:13 +00001399 FileSpecList module_spec_list;
1400 module_spec_list.Append (FileSpec (module_name, false));
Greg Claytoneb023e72013-10-11 19:48:25 +00001401 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 }
1403 else
1404 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001405 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406 }
1407 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001408
Caroline Ticeceb6b132010-10-26 03:11:13 +00001409 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001410 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1411 static_cast<void*>(target_sp.get()), symbol_name,
1412 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001413
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414 return sb_bp;
1415}
1416
Jim Ingham87df91b2011-09-23 00:54:11 +00001417lldb::SBBreakpoint
1418SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001419 const SBFileSpecList &module_list,
1420 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001421{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001422 uint32_t name_type_mask = eFunctionNameTypeAuto;
1423 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1424}
1425
1426lldb::SBBreakpoint
1427SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001428 uint32_t name_type_mask,
1429 const SBFileSpecList &module_list,
1430 const SBFileSpecList &comp_unit_list)
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001431{
Greg Clayton5160ce52013-03-27 23:08:40 +00001432 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001433
1434 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001435 TargetSP target_sp(GetSP());
1436 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001437 {
Jim Inghama8558b62012-05-22 00:12:20 +00001438 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001439 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001440 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001441 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1442 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Greg Claytoneb023e72013-10-11 19:48:25 +00001443 comp_unit_list.get(),
1444 symbol_name,
1445 name_type_mask,
1446 skip_prologue,
1447 internal,
1448 hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001449 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001450
Jim Ingham87df91b2011-09-23 00:54:11 +00001451 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001452 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1453 static_cast<void*>(target_sp.get()), symbol_name,
1454 name_type_mask, static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001455
1456 return sb_bp;
1457}
1458
Jim Inghamfab10e82012-03-06 00:37:27 +00001459lldb::SBBreakpoint
1460SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1461 uint32_t num_names,
1462 uint32_t name_type_mask,
1463 const SBFileSpecList &module_list,
1464 const SBFileSpecList &comp_unit_list)
1465{
Greg Clayton5160ce52013-03-27 23:08:40 +00001466 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001467
1468 SBBreakpoint sb_bp;
1469 TargetSP target_sp(GetSP());
1470 if (target_sp && num_names > 0)
1471 {
1472 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001473 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001474 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001475 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001476 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1477 comp_unit_list.get(),
1478 symbol_names,
1479 num_names,
1480 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001481 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001482 internal,
1483 hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001484 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001485
Jim Inghamfab10e82012-03-06 00:37:27 +00001486 if (log)
1487 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001488 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
1489 static_cast<void*>(target_sp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001490 for (uint32_t i = 0 ; i < num_names; i++)
1491 {
1492 char sep;
1493 if (i < num_names - 1)
1494 sep = ',';
1495 else
1496 sep = '}';
1497 if (symbol_names[i] != NULL)
1498 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1499 else
1500 log->Printf ("\"<NULL>\"%c ", sep);
Jim Inghamfab10e82012-03-06 00:37:27 +00001501 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001502 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
1503 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001504 }
1505
1506 return sb_bp;
1507}
Jim Ingham87df91b2011-09-23 00:54:11 +00001508
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001510SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1511 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512{
Greg Clayton5160ce52013-03-27 23:08:40 +00001513 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001514
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001516 TargetSP target_sp(GetSP());
1517 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001518 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001519 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001521 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001522 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001523 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001524
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001525 if (module_name && module_name[0])
1526 {
Jim Ingham969795f2011-09-21 01:17:13 +00001527 FileSpecList module_spec_list;
1528 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001529
Greg Claytoneb023e72013-10-11 19:48:25 +00001530 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531 }
1532 else
1533 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001534 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535 }
1536 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001537
1538 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001539 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1540 static_cast<void*>(target_sp.get()), symbol_name_regex,
1541 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001542
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001543 return sb_bp;
1544}
1545
Jim Ingham87df91b2011-09-23 00:54:11 +00001546lldb::SBBreakpoint
1547SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001548 const SBFileSpecList &module_list,
1549 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001550{
Greg Clayton5160ce52013-03-27 23:08:40 +00001551 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001552
Jim Ingham87df91b2011-09-23 00:54:11 +00001553 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001554 TargetSP target_sp(GetSP());
1555 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001556 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001557 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001558 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001559 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001560 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001561 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001562
Greg Claytoneb023e72013-10-11 19:48:25 +00001563 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001564 }
1565
1566 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001567 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1568 static_cast<void*>(target_sp.get()), symbol_name_regex,
1569 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001570
1571 return sb_bp;
1572}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573
1574SBBreakpoint
1575SBTarget::BreakpointCreateByAddress (addr_t address)
1576{
Greg Clayton5160ce52013-03-27 23:08:40 +00001577 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001578
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001580 TargetSP target_sp(GetSP());
1581 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001582 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001583 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001584 const bool hardware = false;
1585 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001586 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001587
Caroline Ticeceb6b132010-10-26 03:11:13 +00001588 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001589 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1590 static_cast<void*>(target_sp.get()),
1591 static_cast<uint64_t>(address),
1592 static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001593
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001594 return sb_bp;
1595}
1596
Jim Ingham969795f2011-09-21 01:17:13 +00001597lldb::SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001598SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1599 const lldb::SBFileSpec &source_file,
1600 const char *module_name)
Jim Ingham969795f2011-09-21 01:17:13 +00001601{
Greg Clayton5160ce52013-03-27 23:08:40 +00001602 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001603
1604 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001605 TargetSP target_sp(GetSP());
1606 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001607 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001608 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001609 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001610 FileSpecList source_file_spec_list;
Greg Claytoneb023e72013-10-11 19:48:25 +00001611 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001612 source_file_spec_list.Append (source_file.ref());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001613
Jim Ingham969795f2011-09-21 01:17:13 +00001614 if (module_name && module_name[0])
1615 {
1616 FileSpecList module_spec_list;
1617 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001618
Greg Claytoneb023e72013-10-11 19:48:25 +00001619 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001620 }
1621 else
1622 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001623 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001624 }
1625 }
1626
1627 if (log)
1628 {
1629 char path[PATH_MAX];
1630 source_file->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001631 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1632 static_cast<void*>(target_sp.get()), source_regex, path,
1633 module_name, static_cast<void*>(sb_bp.get()));
Jim Ingham969795f2011-09-21 01:17:13 +00001634 }
1635
1636 return sb_bp;
1637}
1638
Jim Ingham87df91b2011-09-23 00:54:11 +00001639lldb::SBBreakpoint
1640SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001641 const SBFileSpecList &module_list,
1642 const lldb::SBFileSpecList &source_file_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001643{
Greg Clayton5160ce52013-03-27 23:08:40 +00001644 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001645
1646 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001647 TargetSP target_sp(GetSP());
1648 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001649 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001650 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001651 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001652 RegularExpression regexp(source_regex);
Greg Claytoneb023e72013-10-11 19:48:25 +00001653 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001654 }
1655
1656 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001657 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1658 static_cast<void*>(target_sp.get()), source_regex,
1659 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001660
1661 return sb_bp;
1662}
Jim Ingham969795f2011-09-21 01:17:13 +00001663
Jim Inghamfab10e82012-03-06 00:37:27 +00001664lldb::SBBreakpoint
1665SBTarget::BreakpointCreateForException (lldb::LanguageType language,
Greg Claytoneb023e72013-10-11 19:48:25 +00001666 bool catch_bp,
1667 bool throw_bp)
Jim Inghamfab10e82012-03-06 00:37:27 +00001668{
Greg Clayton5160ce52013-03-27 23:08:40 +00001669 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001670
1671 SBBreakpoint sb_bp;
1672 TargetSP target_sp(GetSP());
1673 if (target_sp)
1674 {
1675 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001676 const bool hardware = false;
1677 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001678 }
1679
1680 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001681 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1682 static_cast<void*>(target_sp.get()),
Jim Inghamfab10e82012-03-06 00:37:27 +00001683 LanguageRuntime::GetNameForLanguageType(language),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001684 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1685 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001686
1687 return sb_bp;
1688}
1689
Greg Clayton9fed0d82010-07-23 23:33:17 +00001690uint32_t
1691SBTarget::GetNumBreakpoints () const
1692{
Greg Claytonacdbe812012-01-30 09:04:36 +00001693 TargetSP target_sp(GetSP());
1694 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001695 {
1696 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001697 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001698 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001699 return 0;
1700}
1701
1702SBBreakpoint
1703SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1704{
1705 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001706 TargetSP target_sp(GetSP());
1707 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001708 {
1709 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001710 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001711 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001712 return sb_breakpoint;
1713}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001714
1715bool
1716SBTarget::BreakpointDelete (break_id_t bp_id)
1717{
Greg Clayton5160ce52013-03-27 23:08:40 +00001718 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001719
Caroline Ticeceb6b132010-10-26 03:11:13 +00001720 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001721 TargetSP target_sp(GetSP());
1722 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001723 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001724 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1725 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001726 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001727
1728 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001729 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1730 static_cast<void*>(target_sp.get()),
1731 static_cast<uint32_t>(bp_id), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001732
1733 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001734}
1735
Johnny Chen5d043462011-09-26 22:40:50 +00001736SBBreakpoint
1737SBTarget::FindBreakpointByID (break_id_t bp_id)
1738{
Greg Clayton5160ce52013-03-27 23:08:40 +00001739 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001740
1741 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001742 TargetSP target_sp(GetSP());
1743 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001744 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001745 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1746 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001747 }
1748
1749 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001750 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1751 static_cast<void*>(target_sp.get()),
1752 static_cast<uint32_t>(bp_id),
1753 static_cast<void*>(sb_breakpoint.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001754
1755 return sb_breakpoint;
1756}
1757
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001758bool
1759SBTarget::EnableAllBreakpoints ()
1760{
Greg Claytonacdbe812012-01-30 09:04:36 +00001761 TargetSP target_sp(GetSP());
1762 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001763 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001764 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1765 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001766 return true;
1767 }
1768 return false;
1769}
1770
1771bool
1772SBTarget::DisableAllBreakpoints ()
1773{
Greg Claytonacdbe812012-01-30 09:04:36 +00001774 TargetSP target_sp(GetSP());
1775 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001776 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001777 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1778 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001779 return true;
1780 }
1781 return false;
1782}
1783
1784bool
1785SBTarget::DeleteAllBreakpoints ()
1786{
Greg Claytonacdbe812012-01-30 09:04:36 +00001787 TargetSP target_sp(GetSP());
1788 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001790 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1791 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001792 return true;
1793 }
1794 return false;
1795}
1796
Johnny Chen5d043462011-09-26 22:40:50 +00001797uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001798SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001799{
Greg Claytonacdbe812012-01-30 09:04:36 +00001800 TargetSP target_sp(GetSP());
1801 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001802 {
Johnny Chen01a67862011-10-14 00:42:25 +00001803 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001804 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001805 }
1806 return 0;
1807}
1808
Greg Clayton1b282f92011-10-13 18:08:26 +00001809SBWatchpoint
1810SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001811{
Johnny Chen01a67862011-10-14 00:42:25 +00001812 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001813 TargetSP target_sp(GetSP());
1814 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001815 {
Johnny Chen01a67862011-10-14 00:42:25 +00001816 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001817 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001818 }
Johnny Chen01a67862011-10-14 00:42:25 +00001819 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001820}
1821
1822bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001823SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001824{
Greg Clayton5160ce52013-03-27 23:08:40 +00001825 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001826
1827 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001828 TargetSP target_sp(GetSP());
1829 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001830 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001831 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001832 Mutex::Locker locker;
1833 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001834 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001835 }
1836
1837 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001838 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1839 static_cast<void*>(target_sp.get()),
1840 static_cast<uint32_t>(wp_id), result);
Johnny Chen5d043462011-09-26 22:40:50 +00001841
1842 return result;
1843}
1844
Greg Clayton1b282f92011-10-13 18:08:26 +00001845SBWatchpoint
1846SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001847{
Greg Clayton5160ce52013-03-27 23:08:40 +00001848 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001849
Greg Clayton1b282f92011-10-13 18:08:26 +00001850 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001851 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001852 TargetSP target_sp(GetSP());
1853 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001854 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001855 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001856 Mutex::Locker locker;
1857 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001858 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1859 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001860 }
1861
1862 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001863 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1864 static_cast<void*>(target_sp.get()),
1865 static_cast<uint32_t>(wp_id),
1866 static_cast<void*>(watchpoint_sp.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001867
Greg Clayton1b282f92011-10-13 18:08:26 +00001868 return sb_watchpoint;
1869}
1870
1871lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001872SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001873{
Greg Clayton5160ce52013-03-27 23:08:40 +00001874 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001875
Greg Clayton1b282f92011-10-13 18:08:26 +00001876 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001877 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001878 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001879 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001880 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001881 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001882 uint32_t watch_type = 0;
1883 if (read)
1884 watch_type |= LLDB_WATCH_TYPE_READ;
1885 if (write)
1886 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001887 if (watch_type == 0)
1888 {
1889 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1890 return sb_watchpoint;
1891 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001892
Johnny Chen7385a5a2012-05-31 22:56:36 +00001893 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001894 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001895 // This API doesn't take in a type, so we can't figure out what it is.
1896 ClangASTType *type = NULL;
1897 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001898 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001899 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001900 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001901
Greg Clayton1b282f92011-10-13 18:08:26 +00001902 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001903 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001904 static_cast<void*>(target_sp.get()), addr,
1905 static_cast<uint32_t>(size),
1906 static_cast<void*>(watchpoint_sp.get()));
1907
Greg Clayton1b282f92011-10-13 18:08:26 +00001908 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001909}
1910
1911bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001912SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001913{
Greg Claytonacdbe812012-01-30 09:04:36 +00001914 TargetSP target_sp(GetSP());
1915 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001916 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001917 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001918 Mutex::Locker locker;
1919 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001920 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001921 return true;
1922 }
1923 return false;
1924}
1925
1926bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001927SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001928{
Greg Claytonacdbe812012-01-30 09:04:36 +00001929 TargetSP target_sp(GetSP());
1930 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001931 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001932 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001933 Mutex::Locker locker;
1934 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001935 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001936 return true;
1937 }
1938 return false;
1939}
1940
Enrico Granata347c2aa2013-10-08 21:49:02 +00001941SBValue
1942SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1943{
1944 SBValue sb_value;
1945 lldb::ValueObjectSP new_value_sp;
1946 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1947 {
1948 lldb::addr_t address(addr.GetLoadAddress(*this));
1949 lldb::TypeImplSP type_impl_sp (type.GetSP());
Enrico Granatadc4db5a2013-10-29 00:28:35 +00001950 ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType(true).GetPointerType ());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001951 if (pointer_ast_type)
1952 {
1953 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001954
Enrico Granata347c2aa2013-10-08 21:49:02 +00001955 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1956 ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1957 pointer_ast_type,
1958 ConstString(name),
1959 buffer,
1960 exe_ctx.GetByteOrder(),
1961 exe_ctx.GetAddressByteSize()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001962
Enrico Granata347c2aa2013-10-08 21:49:02 +00001963 if (ptr_result_valobj_sp)
1964 {
1965 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1966 Error err;
1967 new_value_sp = ptr_result_valobj_sp->Dereference(err);
1968 if (new_value_sp)
1969 new_value_sp->SetName(ConstString(name));
1970 }
1971 }
1972 }
1973 sb_value.SetSP(new_value_sp);
1974 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1975 if (log)
1976 {
1977 if (new_value_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001978 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1979 static_cast<void*>(m_opaque_sp.get()),
1980 new_value_sp->GetName().AsCString());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001981 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001982 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1983 static_cast<void*>(m_opaque_sp.get()));
Enrico Granata347c2aa2013-10-08 21:49:02 +00001984 }
1985 return sb_value;
1986}
1987
Johnny Chen5d043462011-09-26 22:40:50 +00001988bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001989SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001990{
Greg Claytonacdbe812012-01-30 09:04:36 +00001991 TargetSP target_sp(GetSP());
1992 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001993 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001994 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001995 Mutex::Locker locker;
1996 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001997 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001998 return true;
1999 }
2000 return false;
2001}
2002
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002003
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002004lldb::SBModule
2005SBTarget::AddModule (const char *path,
2006 const char *triple,
2007 const char *uuid_cstr)
2008{
Greg Claytonb210aec2012-04-23 20:23:39 +00002009 return AddModule (path, triple, uuid_cstr, NULL);
2010}
2011
2012lldb::SBModule
2013SBTarget::AddModule (const char *path,
2014 const char *triple,
2015 const char *uuid_cstr,
2016 const char *symfile)
2017{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002018 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002019 TargetSP target_sp(GetSP());
2020 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002021 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002022 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002023 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00002024 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00002025
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002026 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00002027 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00002028
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002029 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00002030 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00002031 else
2032 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00002033
2034 if (symfile)
2035 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002036
Greg Claytonb9a01b32012-02-26 05:51:37 +00002037 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002038 }
2039 return sb_module;
2040}
2041
Greg Clayton226cce22013-07-08 22:22:41 +00002042lldb::SBModule
2043SBTarget::AddModule (const SBModuleSpec &module_spec)
2044{
2045 lldb::SBModule sb_module;
2046 TargetSP target_sp(GetSP());
2047 if (target_sp)
2048 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
2049 return sb_module;
2050}
2051
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002052bool
2053SBTarget::AddModule (lldb::SBModule &module)
2054{
Greg Claytonacdbe812012-01-30 09:04:36 +00002055 TargetSP target_sp(GetSP());
2056 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002057 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002058 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002059 return true;
2060 }
2061 return false;
2062}
2063
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002064uint32_t
2065SBTarget::GetNumModules () const
2066{
Greg Clayton5160ce52013-03-27 23:08:40 +00002067 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002068
Caroline Ticeceb6b132010-10-26 03:11:13 +00002069 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00002070 TargetSP target_sp(GetSP());
2071 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002072 {
2073 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002074 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00002075 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002076
2077 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002078 log->Printf ("SBTarget(%p)::GetNumModules () => %d",
2079 static_cast<void*>(target_sp.get()), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002080
2081 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002082}
2083
Greg Clayton48e42542010-07-30 20:12:55 +00002084void
2085SBTarget::Clear ()
2086{
Greg Clayton5160ce52013-03-27 23:08:40 +00002087 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002088
2089 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002090 log->Printf ("SBTarget(%p)::Clear ()",
2091 static_cast<void*>(m_opaque_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002092
Greg Clayton48e42542010-07-30 20:12:55 +00002093 m_opaque_sp.reset();
2094}
2095
2096
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002097SBModule
2098SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2099{
2100 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002101 TargetSP target_sp(GetSP());
2102 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00002103 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002104 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002105 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00002106 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00002107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002108 return sb_module;
2109}
2110
Greg Clayton13d19502012-01-29 06:07:39 +00002111lldb::ByteOrder
2112SBTarget::GetByteOrder ()
2113{
Greg Claytonacdbe812012-01-30 09:04:36 +00002114 TargetSP target_sp(GetSP());
2115 if (target_sp)
2116 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00002117 return eByteOrderInvalid;
2118}
2119
2120const char *
2121SBTarget::GetTriple ()
2122{
Greg Claytonacdbe812012-01-30 09:04:36 +00002123 TargetSP target_sp(GetSP());
2124 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00002125 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002126 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00002127 // Unique the string so we don't run into ownership issues since
2128 // the const strings put the string into the string pool once and
2129 // the strings never comes out
2130 ConstString const_triple (triple.c_str());
2131 return const_triple.GetCString();
2132 }
2133 return NULL;
2134}
2135
2136uint32_t
Matthew Gardinerc928de32014-10-22 07:22:56 +00002137SBTarget::GetDataByteSize ()
2138{
2139 TargetSP target_sp(GetSP());
2140 if (target_sp)
2141 {
2142 return target_sp->GetArchitecture().GetDataByteSize() ;
2143 }
2144 return 0;
2145}
2146
2147uint32_t
2148SBTarget::GetCodeByteSize ()
2149{
2150 TargetSP target_sp(GetSP());
2151 if (target_sp)
2152 {
2153 return target_sp->GetArchitecture().GetCodeByteSize() ;
2154 }
2155 return 0;
2156}
2157
2158uint32_t
Greg Clayton13d19502012-01-29 06:07:39 +00002159SBTarget::GetAddressByteSize()
2160{
Greg Claytonacdbe812012-01-30 09:04:36 +00002161 TargetSP target_sp(GetSP());
2162 if (target_sp)
2163 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00002164 return sizeof(void*);
2165}
2166
2167
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002168SBModule
2169SBTarget::GetModuleAtIndex (uint32_t idx)
2170{
Greg Clayton5160ce52013-03-27 23:08:40 +00002171 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002172
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002173 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002174 ModuleSP module_sp;
2175 TargetSP target_sp(GetSP());
2176 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002177 {
2178 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002179 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2180 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002181 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002182
2183 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002184 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2185 static_cast<void*>(target_sp.get()), idx,
2186 static_cast<void*>(module_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002187
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002188 return sb_module;
2189}
2190
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002191bool
2192SBTarget::RemoveModule (lldb::SBModule module)
2193{
Greg Claytonacdbe812012-01-30 09:04:36 +00002194 TargetSP target_sp(GetSP());
2195 if (target_sp)
2196 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002197 return false;
2198}
2199
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002200
2201SBBroadcaster
2202SBTarget::GetBroadcaster () const
2203{
Greg Clayton5160ce52013-03-27 23:08:40 +00002204 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002205
Greg Claytonacdbe812012-01-30 09:04:36 +00002206 TargetSP target_sp(GetSP());
2207 SBBroadcaster broadcaster(target_sp.get(), false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002208
Caroline Ticeceb6b132010-10-26 03:11:13 +00002209 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002210 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2211 static_cast<void*>(target_sp.get()),
2212 static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002213
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002214 return broadcaster;
2215}
2216
Caroline Ticedde9cff2010-09-20 05:20:02 +00002217bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002218SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002219{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002220 Stream &strm = description.ref();
2221
Greg Claytonacdbe812012-01-30 09:04:36 +00002222 TargetSP target_sp(GetSP());
2223 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002224 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002225 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002226 }
2227 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002228 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002229
2230 return true;
2231}
2232
Greg Clayton5569e642012-02-06 01:44:54 +00002233lldb::SBSymbolContextList
2234SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002235{
Greg Clayton5569e642012-02-06 01:44:54 +00002236 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002237 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002238 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002239 TargetSP target_sp(GetSP());
2240 if (target_sp)
2241 {
2242 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002243 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002244 const bool append = true;
2245 target_sp->GetImages().FindFunctions (ConstString(name),
2246 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002247 symbols_ok,
2248 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002249 append,
2250 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002251 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002252 }
Greg Clayton5569e642012-02-06 01:44:54 +00002253 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002254}
2255
Carlo Kok0fd6fd42014-09-19 19:38:19 +00002256lldb::SBSymbolContextList
2257SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
2258{
2259 lldb::SBSymbolContextList sb_sc_list;
2260 if (name && name[0])
2261 {
2262 TargetSP target_sp(GetSP());
2263 if (target_sp)
2264 {
2265 std::string regexstr;
2266 switch (matchtype)
2267 {
2268 case eMatchTypeRegex:
2269 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
2270 break;
2271 case eMatchTypeStartsWith:
2272 regexstr = llvm::Regex::escape(name) + ".*";
2273 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
2274 break;
2275 default:
2276 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
2277 break;
2278 }
2279 }
2280 }
2281 return sb_sc_list;
2282}
2283
Enrico Granata6f3533f2011-07-29 19:53:35 +00002284lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002285SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002286{
Greg Claytonacdbe812012-01-30 09:04:36 +00002287 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002288 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002289 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002290 ConstString const_typename(typename_cstr);
2291 SymbolContext sc;
2292 const bool exact_match = false;
2293
2294 const ModuleList &module_list = target_sp->GetImages();
2295 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002296 for (size_t idx = 0; idx < count; idx++)
2297 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002298 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2299 if (module_sp)
2300 {
2301 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2302 if (type_sp)
2303 return SBType(type_sp);
2304 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002305 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002306
2307 // Didn't find the type in the symbols; try the Objective-C runtime
2308 // if one is installed
2309
2310 ProcessSP process_sp(target_sp->GetProcessSP());
2311
2312 if (process_sp)
2313 {
2314 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2315
2316 if (objc_language_runtime)
2317 {
Sean Callanan9998acd2014-12-05 01:21:59 +00002318 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
Sean Callanan7be70e82012-12-19 23:05:01 +00002319
Sean Callanan9998acd2014-12-05 01:21:59 +00002320 if (objc_decl_vendor)
Sean Callanan7be70e82012-12-19 23:05:01 +00002321 {
Sean Callanan9998acd2014-12-05 01:21:59 +00002322 std::vector <clang::NamedDecl *> decls;
Sean Callanan7be70e82012-12-19 23:05:01 +00002323
Sean Callanan9998acd2014-12-05 01:21:59 +00002324 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
2325 {
2326 if (ClangASTType type = ClangASTContext::GetTypeForDecl(decls[0]))
2327 {
2328 return SBType(type);
2329 }
2330 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002331 }
2332 }
2333 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002334
2335 // No matches, search for basic typename matches
2336 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2337 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002338 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002339 }
2340 return SBType();
2341}
2342
Greg Claytonb43165b2012-12-05 21:24:42 +00002343SBType
2344SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002345{
Greg Claytonacdbe812012-01-30 09:04:36 +00002346 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002347 if (target_sp)
2348 {
2349 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2350 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002351 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00002352 }
2353 return SBType();
2354}
2355
2356
2357lldb::SBTypeList
2358SBTarget::FindTypes (const char* typename_cstr)
2359{
2360 SBTypeList sb_type_list;
2361 TargetSP target_sp(GetSP());
2362 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002363 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002364 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002365 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002366 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002367 SymbolContext sc;
2368 TypeList type_list;
2369
Greg Clayton29399a22012-04-06 17:41:13 +00002370 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002371 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002372 exact_match,
2373 UINT32_MAX,
2374 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002375
Greg Claytonb43165b2012-12-05 21:24:42 +00002376 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002377 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002378 for (size_t idx = 0; idx < num_matches; idx++)
2379 {
2380 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2381 if (type_sp)
2382 sb_type_list.Append(SBType(type_sp));
2383 }
2384 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002385
2386 // Try the Objective-C runtime if one is installed
2387
2388 ProcessSP process_sp(target_sp->GetProcessSP());
2389
2390 if (process_sp)
2391 {
2392 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2393
2394 if (objc_language_runtime)
2395 {
Sean Callanan9998acd2014-12-05 01:21:59 +00002396 DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
Sean Callanan7be70e82012-12-19 23:05:01 +00002397
Sean Callanan9998acd2014-12-05 01:21:59 +00002398 if (objc_decl_vendor)
Sean Callanan7be70e82012-12-19 23:05:01 +00002399 {
Sean Callanan9998acd2014-12-05 01:21:59 +00002400 std::vector <clang::NamedDecl *> decls;
Sean Callanan7be70e82012-12-19 23:05:01 +00002401
Sean Callanan9998acd2014-12-05 01:21:59 +00002402 if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0)
Sean Callanan7be70e82012-12-19 23:05:01 +00002403 {
Sean Callanan9998acd2014-12-05 01:21:59 +00002404 for (clang::NamedDecl *decl : decls)
Sean Callanan7be70e82012-12-19 23:05:01 +00002405 {
Sean Callanan9998acd2014-12-05 01:21:59 +00002406 if (ClangASTType type = ClangASTContext::GetTypeForDecl(decl))
2407 {
2408 sb_type_list.Append(SBType(type));
2409 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002410 }
2411 }
2412 }
2413 }
2414 }
2415
2416 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002417 {
2418 // No matches, search for basic typename matches
2419 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2420 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002421 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002422 }
2423 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002424 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002425}
2426
Greg Claytondea8cb42011-06-29 22:09:02 +00002427SBValueList
2428SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2429{
2430 SBValueList sb_value_list;
2431
Greg Claytonacdbe812012-01-30 09:04:36 +00002432 TargetSP target_sp(GetSP());
2433 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002434 {
2435 VariableList variable_list;
2436 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002437 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2438 append,
2439 max_matches,
2440 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002441
2442 if (match_count > 0)
2443 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002444 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002445 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002446 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002447 for (uint32_t i=0; i<match_count; ++i)
2448 {
2449 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2450 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002451 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002452 }
2453 }
2454 }
2455
2456 return sb_value_list;
2457}
2458
Carlo Kok0fd6fd42014-09-19 19:38:19 +00002459SBValueList
2460SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
2461{
2462 SBValueList sb_value_list;
2463
2464 TargetSP target_sp(GetSP());
2465 if (name && target_sp)
2466 {
2467 VariableList variable_list;
2468 const bool append = true;
2469
2470 std::string regexstr;
2471 uint32_t match_count;
2472 switch (matchtype)
2473 {
2474 case eMatchTypeNormal:
2475 match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
2476 append,
2477 max_matches,
2478 variable_list);
2479 break;
2480 case eMatchTypeRegex:
2481 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
2482 append,
2483 max_matches,
2484 variable_list);
2485 break;
2486 case eMatchTypeStartsWith:
2487 regexstr = llvm::Regex::escape(name) + ".*";
2488 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
2489 append,
2490 max_matches,
2491 variable_list);
2492 break;
2493 }
2494
2495
2496 if (match_count > 0)
2497 {
2498 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2499 if (exe_scope == NULL)
2500 exe_scope = target_sp.get();
2501 for (uint32_t i = 0; i<match_count; ++i)
2502 {
2503 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
2504 if (valobj_sp)
2505 sb_value_list.Append(SBValue(valobj_sp));
2506 }
2507 }
2508 }
2509
2510 return sb_value_list;
2511}
2512
2513
Enrico Granatabcd80b42013-01-16 18:53:52 +00002514lldb::SBValue
2515SBTarget::FindFirstGlobalVariable (const char* name)
2516{
2517 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2518 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2519 return sb_value_list.GetValueAtIndex(0);
2520 return SBValue();
2521}
2522
Jim Inghame37d6052011-09-13 00:29:56 +00002523SBSourceManager
2524SBTarget::GetSourceManager()
2525{
2526 SBSourceManager source_manager (*this);
2527 return source_manager;
2528}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002529
Sean Callanan50952e92011-12-14 23:49:37 +00002530lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002531SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2532{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002533 return ReadInstructions (base_addr, count, NULL);
2534}
2535
2536lldb::SBInstructionList
2537SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2538{
Greg Clayton9c766112012-03-06 22:24:44 +00002539 SBInstructionList sb_instructions;
2540
2541 TargetSP target_sp(GetSP());
2542 if (target_sp)
2543 {
2544 Address *addr_ptr = base_addr.get();
2545
2546 if (addr_ptr)
2547 {
2548 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2549 bool prefer_file_cache = false;
2550 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002551 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2552 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2553 prefer_file_cache,
2554 data.GetBytes(),
2555 data.GetByteSize(),
2556 error,
2557 &load_addr);
2558 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002559 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2560 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002561 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002562 *addr_ptr,
2563 data.GetBytes(),
2564 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002565 count,
2566 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002567 }
2568 }
2569
2570 return sb_instructions;
2571
2572}
2573
2574lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002575SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2576{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002577 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2578}
2579
2580lldb::SBInstructionList
2581SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2582{
Sean Callanan50952e92011-12-14 23:49:37 +00002583 SBInstructionList sb_instructions;
2584
Greg Claytonacdbe812012-01-30 09:04:36 +00002585 TargetSP target_sp(GetSP());
2586 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002587 {
2588 Address addr;
2589
2590 if (base_addr.get())
2591 addr = *base_addr.get();
2592
Greg Clayton3faf47c2013-03-28 23:42:53 +00002593 const bool data_from_file = true;
2594
Greg Claytonacdbe812012-01-30 09:04:36 +00002595 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002596 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002597 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002598 addr,
2599 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002600 size,
2601 UINT32_MAX,
2602 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002603 }
2604
2605 return sb_instructions;
2606}
2607
2608lldb::SBInstructionList
2609SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2610{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002611 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2612}
2613
2614lldb::SBInstructionList
2615SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2616{
2617 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002618}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002619
2620SBError
2621SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2622 lldb::addr_t section_base_addr)
2623{
2624 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002625 TargetSP target_sp(GetSP());
2626 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002627 {
2628 if (!section.IsValid())
2629 {
2630 sb_error.SetErrorStringWithFormat ("invalid section");
2631 }
2632 else
2633 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002634 SectionSP section_sp (section.GetSP());
2635 if (section_sp)
2636 {
2637 if (section_sp->IsThreadSpecific())
2638 {
2639 sb_error.SetErrorString ("thread specific sections are not yet supported");
2640 }
2641 else
2642 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002643 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002644 if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
Greg Clayton3c947372013-01-29 01:17:09 +00002645 {
2646 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002647 if (process_sp)
2648 process_sp->Flush();
2649 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002650 }
2651 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002652 }
2653 }
2654 else
2655 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002656 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002657 }
2658 return sb_error;
2659}
2660
2661SBError
2662SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2663{
2664 SBError sb_error;
2665
Greg Claytonacdbe812012-01-30 09:04:36 +00002666 TargetSP target_sp(GetSP());
2667 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002668 {
2669 if (!section.IsValid())
2670 {
2671 sb_error.SetErrorStringWithFormat ("invalid section");
2672 }
2673 else
2674 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002675 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002676 if (target_sp->SetSectionUnloaded (section.GetSP()))
Greg Clayton3c947372013-01-29 01:17:09 +00002677 {
2678 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002679 if (process_sp)
2680 process_sp->Flush();
2681 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002682 }
2683 }
2684 else
2685 {
2686 sb_error.SetErrorStringWithFormat ("invalid target");
2687 }
2688 return sb_error;
2689}
2690
2691SBError
2692SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2693{
2694 SBError sb_error;
2695
Greg Claytonacdbe812012-01-30 09:04:36 +00002696 TargetSP target_sp(GetSP());
2697 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002698 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002699 ModuleSP module_sp (module.GetSP());
2700 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002701 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002702 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +00002703 if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002704 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002705 // The load was successful, make sure that at least some sections
2706 // changed before we notify that our module was loaded.
2707 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002708 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002709 ModuleList module_list;
2710 module_list.Append(module_sp);
2711 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002712 // Flush info in the process (stack frames, etc)
2713 ProcessSP process_sp (target_sp->GetProcessSP());
2714 if (process_sp)
2715 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002716 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002717 }
2718 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002719 else
2720 {
2721 sb_error.SetErrorStringWithFormat ("invalid module");
2722 }
2723
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002724 }
2725 else
2726 {
2727 sb_error.SetErrorStringWithFormat ("invalid target");
2728 }
2729 return sb_error;
2730}
2731
2732SBError
2733SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2734{
2735 SBError sb_error;
2736
2737 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002738 TargetSP target_sp(GetSP());
2739 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002740 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002741 ModuleSP module_sp (module.GetSP());
2742 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002743 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002744 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002745 if (objfile)
2746 {
2747 SectionList *section_list = objfile->GetSectionList();
2748 if (section_list)
2749 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002750 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002751
Greg Clayton3c947372013-01-29 01:17:09 +00002752 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002753 const size_t num_sections = section_list->GetSize();
2754 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2755 {
2756 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2757 if (section_sp)
Zachary Turner40411162014-07-16 20:28:24 +00002758 changed |= target_sp->SetSectionUnloaded (section_sp);
Greg Clayton3c947372013-01-29 01:17:09 +00002759 }
2760 if (changed)
2761 {
2762 // Flush info in the process (stack frames, etc)
2763 ProcessSP process_sp (target_sp->GetProcessSP());
2764 if (process_sp)
2765 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002766 }
2767 }
2768 else
2769 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002770 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002771 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2772 }
2773 }
2774 else
2775 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002776 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002777 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2778 }
2779 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002780 else
2781 {
2782 sb_error.SetErrorStringWithFormat ("invalid module");
2783 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002784 }
2785 else
2786 {
2787 sb_error.SetErrorStringWithFormat ("invalid target");
2788 }
2789 return sb_error;
2790}
2791
2792
Greg Claytone14e1922012-12-04 02:22:16 +00002793lldb::SBSymbolContextList
2794SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2795{
2796 SBSymbolContextList sb_sc_list;
2797 if (name && name[0])
2798 {
2799 TargetSP target_sp(GetSP());
2800 if (target_sp)
2801 {
2802 bool append = true;
2803 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2804 symbol_type,
2805 *sb_sc_list,
2806 append);
2807 }
2808 }
2809 return sb_sc_list;
2810
2811}
2812
2813
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002814lldb::SBValue
2815SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2816{
Greg Clayton5160ce52013-03-27 23:08:40 +00002817 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2818 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002819 SBValue expr_result;
Jim Ingham8646d3c2014-05-05 02:47:44 +00002820 ExpressionResults exe_results = eExpressionSetupError;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002821 ValueObjectSP expr_value_sp;
2822 TargetSP target_sp(GetSP());
Jason Molendab57e4a12013-11-04 09:33:30 +00002823 StackFrame *frame = NULL;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002824 if (target_sp)
2825 {
2826 if (expr == NULL || expr[0] == '\0')
2827 {
2828 if (log)
2829 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2830 return expr_result;
2831 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002832
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002833 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2834 ExecutionContext exe_ctx (m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002835
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002836 if (log)
2837 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002838
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002839 frame = exe_ctx.GetFramePtr();
2840 Target *target = exe_ctx.GetTargetPtr();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002841
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002842 if (target)
2843 {
2844#ifdef LLDB_CONFIGURATION_DEBUG
2845 StreamString frame_description;
2846 if (frame)
2847 frame->DumpUsingSettingsFormat (&frame_description);
2848 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2849 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2850#endif
2851 exe_results = target->EvaluateExpression (expr,
2852 frame,
2853 expr_value_sp,
2854 options.ref());
2855
2856 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2857#ifdef LLDB_CONFIGURATION_DEBUG
2858 Host::SetCrashDescription (NULL);
2859#endif
2860 }
2861 else
2862 {
2863 if (log)
2864 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2865 }
2866 }
2867#ifndef LLDB_DISABLE_PYTHON
2868 if (expr_log)
2869 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002870 expr_result.GetValue(), expr_result.GetSummary());
2871
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002872 if (log)
2873 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002874 static_cast<void*>(frame), expr,
2875 static_cast<void*>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002876#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002877
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002878 return expr_result;
2879}
2880
Greg Clayton13fbb992013-02-01 00:47:49 +00002881
2882lldb::addr_t
2883SBTarget::GetStackRedZoneSize()
2884{
2885 TargetSP target_sp(GetSP());
2886 if (target_sp)
2887 {
2888 ABISP abi_sp;
2889 ProcessSP process_sp (target_sp->GetProcessSP());
2890 if (process_sp)
2891 abi_sp = process_sp->GetABI();
2892 else
2893 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2894 if (abi_sp)
2895 return abi_sp->GetRedZoneSize();
2896 }
2897 return 0;
2898}
2899