blob: dbf4a47841358d334dac32389c0f3e6cdee43ca9 [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 Clayton0e615682012-02-24 05:03:03 +0000136uint32_t
137SBLaunchInfo::GetNumArguments ()
138{
139 return m_opaque_sp->GetArguments().GetArgumentCount();
140}
141
142const char *
143SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
144{
145 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
146}
147
148void
149SBLaunchInfo::SetArguments (const char **argv, bool append)
150{
151 if (append)
152 {
153 if (argv)
154 m_opaque_sp->GetArguments().AppendArguments(argv);
155 }
156 else
157 {
158 if (argv)
159 m_opaque_sp->GetArguments().SetArguments(argv);
160 else
161 m_opaque_sp->GetArguments().Clear();
162 }
163}
164
165uint32_t
166SBLaunchInfo::GetNumEnvironmentEntries ()
167{
168 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
169}
170
171const char *
172SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
173{
174 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
175}
176
177void
178SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
179{
180 if (append)
181 {
182 if (envp)
183 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
184 }
185 else
186 {
187 if (envp)
188 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
189 else
190 m_opaque_sp->GetEnvironmentEntries().Clear();
191 }
192}
193
194void
195SBLaunchInfo::Clear ()
196{
197 m_opaque_sp->Clear();
198}
199
200const char *
201SBLaunchInfo::GetWorkingDirectory () const
202{
203 return m_opaque_sp->GetWorkingDirectory();
204}
205
206void
207SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
208{
209 m_opaque_sp->SetWorkingDirectory(working_dir);
210}
211
212uint32_t
213SBLaunchInfo::GetLaunchFlags ()
214{
215 return m_opaque_sp->GetFlags().Get();
216}
217
218void
219SBLaunchInfo::SetLaunchFlags (uint32_t flags)
220{
221 m_opaque_sp->GetFlags().Reset(flags);
222}
223
224const char *
225SBLaunchInfo::GetProcessPluginName ()
226{
227 return m_opaque_sp->GetProcessPluginName();
228}
229
230void
231SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
232{
233 return m_opaque_sp->SetProcessPluginName (plugin_name);
234}
235
236const char *
237SBLaunchInfo::GetShell ()
238{
Zachary Turner10687b02014-10-20 17:46:43 +0000239 // Constify this string so that it is saved in the string pool. Otherwise
240 // it would be freed when this function goes out of scope.
241 ConstString shell(m_opaque_sp->GetShell().GetPath().c_str());
242 return shell.AsCString();
Greg Clayton0e615682012-02-24 05:03:03 +0000243}
244
245void
246SBLaunchInfo::SetShell (const char * path)
247{
Zachary Turner10687b02014-10-20 17:46:43 +0000248 m_opaque_sp->SetShell (FileSpec(path, false));
Greg Clayton0e615682012-02-24 05:03:03 +0000249}
250
251uint32_t
252SBLaunchInfo::GetResumeCount ()
253{
254 return m_opaque_sp->GetResumeCount();
255}
256
257void
258SBLaunchInfo::SetResumeCount (uint32_t c)
259{
260 m_opaque_sp->SetResumeCount (c);
261}
262
263bool
264SBLaunchInfo::AddCloseFileAction (int fd)
265{
266 return m_opaque_sp->AppendCloseFileAction(fd);
267}
268
269bool
270SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
271{
272 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
273}
274
275bool
276SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
277{
278 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
279}
280
281bool
282SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
283{
284 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
285}
286
Jason Molendaa3329782014-03-29 18:54:20 +0000287void
288SBLaunchInfo::SetLaunchEventData (const char *data)
289{
290 m_opaque_sp->SetLaunchEventData (data);
291}
292
293const char *
294SBLaunchInfo::GetLaunchEventData () const
295{
296 return m_opaque_sp->GetLaunchEventData ();
297}
Greg Clayton0e615682012-02-24 05:03:03 +0000298
Jim Ingham106d0282014-06-25 02:32:56 +0000299void
300SBLaunchInfo::SetDetachOnError (bool enable)
301{
302 m_opaque_sp->SetDetachOnError (enable);
303}
304
305bool
306SBLaunchInfo::GetDetachOnError () const
307{
308 return m_opaque_sp->GetDetachOnError ();
309}
310
Greg Clayton0e615682012-02-24 05:03:03 +0000311SBAttachInfo::SBAttachInfo () :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000312 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000313{
314}
315
316SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000317 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000318{
319 m_opaque_sp->SetProcessID (pid);
320}
321
322SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000323 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000324{
325 if (path && path[0])
326 m_opaque_sp->GetExecutableFile().SetFile(path, false);
327 m_opaque_sp->SetWaitForLaunch (wait_for);
328}
329
330SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000331 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000332{
333 *m_opaque_sp = *rhs.m_opaque_sp;
334}
335
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000336SBAttachInfo::~SBAttachInfo()
337{
338}
339
340lldb_private::ProcessAttachInfo &
341SBAttachInfo::ref ()
342{
343 return *m_opaque_sp;
344}
345
Greg Clayton0e615682012-02-24 05:03:03 +0000346SBAttachInfo &
347SBAttachInfo::operator = (const SBAttachInfo &rhs)
348{
349 if (this != &rhs)
350 *m_opaque_sp = *rhs.m_opaque_sp;
351 return *this;
352}
353
354lldb::pid_t
355SBAttachInfo::GetProcessID ()
356{
357 return m_opaque_sp->GetProcessID();
358}
359
360void
361SBAttachInfo::SetProcessID (lldb::pid_t pid)
362{
363 m_opaque_sp->SetProcessID (pid);
364}
365
366
367uint32_t
368SBAttachInfo::GetResumeCount ()
369{
370 return m_opaque_sp->GetResumeCount();
371}
372
373void
374SBAttachInfo::SetResumeCount (uint32_t c)
375{
376 m_opaque_sp->SetResumeCount (c);
377}
378
379const char *
380SBAttachInfo::GetProcessPluginName ()
381{
382 return m_opaque_sp->GetProcessPluginName();
383}
384
385void
386SBAttachInfo::SetProcessPluginName (const char *plugin_name)
387{
388 return m_opaque_sp->SetProcessPluginName (plugin_name);
389}
390
391void
392SBAttachInfo::SetExecutable (const char *path)
393{
394 if (path && path[0])
395 m_opaque_sp->GetExecutableFile().SetFile(path, false);
396 else
397 m_opaque_sp->GetExecutableFile().Clear();
398}
399
400void
401SBAttachInfo::SetExecutable (SBFileSpec exe_file)
402{
403 if (exe_file.IsValid())
404 m_opaque_sp->GetExecutableFile() = exe_file.ref();
405 else
406 m_opaque_sp->GetExecutableFile().Clear();
407}
408
409bool
410SBAttachInfo::GetWaitForLaunch ()
411{
412 return m_opaque_sp->GetWaitForLaunch();
413}
414
415void
416SBAttachInfo::SetWaitForLaunch (bool b)
417{
418 m_opaque_sp->SetWaitForLaunch (b);
419}
420
Jim Inghamcd16df92012-07-20 21:37:13 +0000421bool
422SBAttachInfo::GetIgnoreExisting ()
423{
424 return m_opaque_sp->GetIgnoreExisting();
425}
426
427void
428SBAttachInfo::SetIgnoreExisting (bool b)
429{
430 m_opaque_sp->SetIgnoreExisting (b);
431}
432
Greg Clayton0e615682012-02-24 05:03:03 +0000433uint32_t
Greg Clayton41bd8ac2012-02-24 23:56:06 +0000434SBAttachInfo::GetUserID()
435{
436 return m_opaque_sp->GetUserID();
437}
438
439uint32_t
440SBAttachInfo::GetGroupID()
441{
442 return m_opaque_sp->GetGroupID();
443}
444
445bool
446SBAttachInfo::UserIDIsValid ()
447{
448 return m_opaque_sp->UserIDIsValid();
449}
450
451bool
452SBAttachInfo::GroupIDIsValid ()
453{
454 return m_opaque_sp->GroupIDIsValid();
455}
456
457void
458SBAttachInfo::SetUserID (uint32_t uid)
459{
460 m_opaque_sp->SetUserID (uid);
461}
462
463void
464SBAttachInfo::SetGroupID (uint32_t gid)
465{
466 m_opaque_sp->SetGroupID (gid);
467}
468
469uint32_t
Greg Clayton0e615682012-02-24 05:03:03 +0000470SBAttachInfo::GetEffectiveUserID()
471{
472 return m_opaque_sp->GetEffectiveUserID();
473}
474
475uint32_t
476SBAttachInfo::GetEffectiveGroupID()
477{
478 return m_opaque_sp->GetEffectiveGroupID();
479}
480
481bool
482SBAttachInfo::EffectiveUserIDIsValid ()
483{
484 return m_opaque_sp->EffectiveUserIDIsValid();
485}
486
487bool
488SBAttachInfo::EffectiveGroupIDIsValid ()
489{
490 return m_opaque_sp->EffectiveGroupIDIsValid ();
491}
492
493void
494SBAttachInfo::SetEffectiveUserID (uint32_t uid)
495{
496 m_opaque_sp->SetEffectiveUserID(uid);
497}
498
499void
500SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
501{
502 m_opaque_sp->SetEffectiveGroupID(gid);
503}
504
505lldb::pid_t
506SBAttachInfo::GetParentProcessID ()
507{
508 return m_opaque_sp->GetParentProcessID();
509}
510
511void
512SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
513{
514 m_opaque_sp->SetParentProcessID (pid);
515}
516
517bool
518SBAttachInfo::ParentProcessIDIsValid()
519{
520 return m_opaque_sp->ParentProcessIDIsValid();
521}
522
523
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524//----------------------------------------------------------------------
525// SBTarget constructor
526//----------------------------------------------------------------------
Greg Clayton54979cd2010-12-15 05:08:08 +0000527SBTarget::SBTarget () :
528 m_opaque_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529{
530}
531
532SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton66111032010-06-23 01:19:29 +0000533 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534{
535}
536
537SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton66111032010-06-23 01:19:29 +0000538 m_opaque_sp (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539{
540}
541
Greg Claytonefabb122010-11-05 23:17:00 +0000542const SBTarget&
543SBTarget::operator = (const SBTarget& rhs)
544{
545 if (this != &rhs)
546 m_opaque_sp = rhs.m_opaque_sp;
547 return *this;
548}
549
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550//----------------------------------------------------------------------
551// Destructor
552//----------------------------------------------------------------------
553SBTarget::~SBTarget()
554{
555}
556
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000557const char *
558SBTarget::GetBroadcasterClassName ()
559{
560 return Target::GetStaticBroadcasterClass().AsCString();
561}
562
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563bool
564SBTarget::IsValid () const
565{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000566 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567}
568
569SBProcess
570SBTarget::GetProcess ()
571{
572 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000573 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000574 TargetSP target_sp(GetSP());
575 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000576 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000577 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000578 sb_process.SetSP (process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000579 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000580
Greg Clayton5160ce52013-03-27 23:08:40 +0000581 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000582 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000583 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
584 static_cast<void*>(target_sp.get()),
585 static_cast<void*>(process_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000586
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 return sb_process;
588}
589
Matthew Gardinerc928de32014-10-22 07:22:56 +0000590SBPlatform
591SBTarget::GetPlatform ()
592{
593 TargetSP target_sp(GetSP());
594 if (!target_sp)
595 return SBPlatform();
596
597 SBPlatform platform;
598 platform.m_opaque_sp = target_sp->GetPlatform();
599
600 return platform;
601}
602
Greg Clayton66111032010-06-23 01:19:29 +0000603SBDebugger
604SBTarget::GetDebugger () const
605{
606 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000607 TargetSP target_sp(GetSP());
608 if (target_sp)
609 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000610 return debugger;
611}
612
Jim Ingham270684d2011-03-31 00:01:24 +0000613SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000614SBTarget::LoadCore (const char *core_file)
615{
616 SBProcess sb_process;
617 TargetSP target_sp(GetSP());
618 if (target_sp)
619 {
620 FileSpec filespec(core_file, true);
621 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
622 NULL,
623 &filespec));
624 if (process_sp)
625 {
626 process_sp->LoadCore();
627 sb_process.SetSP (process_sp);
628 }
629 }
630 return sb_process;
631}
632
633SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000634SBTarget::LaunchSimple
635(
636 char const **argv,
637 char const **envp,
638 const char *working_directory
639)
640{
641 char *stdin_path = NULL;
642 char *stdout_path = NULL;
643 char *stderr_path = NULL;
644 uint32_t launch_flags = 0;
645 bool stop_at_entry = false;
646 SBError error;
647 SBListener listener = GetDebugger().GetListener();
648 return Launch (listener,
649 argv,
650 envp,
651 stdin_path,
652 stdout_path,
653 stderr_path,
654 working_directory,
655 launch_flags,
656 stop_at_entry,
657 error);
658}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000659
Greg Claytonfbb76342013-11-20 21:07:01 +0000660SBError
661SBTarget::Install()
662{
663 SBError sb_error;
664 TargetSP target_sp(GetSP());
665 if (target_sp)
666 {
667 Mutex::Locker api_locker (target_sp->GetAPIMutex());
668 sb_error.ref() = target_sp->Install(NULL);
669 }
670 return sb_error;
671}
672
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000673SBProcess
674SBTarget::Launch
675(
Greg Clayton4b045622011-02-03 21:28:34 +0000676 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000677 char const **argv,
678 char const **envp,
679 const char *stdin_path,
680 const char *stdout_path,
681 const char *stderr_path,
682 const char *working_directory,
683 uint32_t launch_flags, // See LaunchFlags
684 bool stop_at_entry,
685 lldb::SBError& error
686)
687{
Greg Clayton5160ce52013-03-27 23:08:40 +0000688 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000689
Greg Claytonacdbe812012-01-30 09:04:36 +0000690 SBProcess sb_process;
691 ProcessSP process_sp;
692 TargetSP target_sp(GetSP());
693
Caroline Ticeceb6b132010-10-26 03:11:13 +0000694 if (log)
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000695 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 +0000696 static_cast<void*>(target_sp.get()),
697 static_cast<void*>(argv), static_cast<void*>(envp),
698 stdin_path ? stdin_path : "NULL",
699 stdout_path ? stdout_path : "NULL",
700 stderr_path ? stderr_path : "NULL",
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000701 working_directory ? working_directory : "NULL",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000702 launch_flags, stop_at_entry,
703 static_cast<void*>(error.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000704
705 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000707 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000708
Greg Clayton645bf542011-01-27 01:01:10 +0000709 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
710 launch_flags |= eLaunchFlagDisableASLR;
711
Greg Clayton2289fa42011-04-30 01:09:13 +0000712 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000713 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000714 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000715 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000716 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000717
Greg Claytonb9556ac2012-01-30 07:41:31 +0000718 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000719 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000720 if (state == eStateAttaching)
721 error.SetErrorString ("process attach is in progress");
722 else
723 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000724 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000725 }
Greg Clayton931180e2011-01-27 06:44:37 +0000726 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000727
Greg Clayton2289fa42011-04-30 01:09:13 +0000728 if (state == eStateConnected)
729 {
730 // If we are already connected, then we have already specified the
731 // listener, so if a valid listener is supplied, we need to error out
732 // to let the client know.
733 if (listener.IsValid())
734 {
735 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000736 return sb_process;
737 }
738 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000739
740 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
741 launch_flags |= eLaunchFlagDisableSTDIO;
742
743 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000744
Greg Claytonb09c5382013-12-13 17:20:18 +0000745 Module *exe_module = target_sp->GetExecutableModulePointer();
746 if (exe_module)
747 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
748 if (argv)
749 launch_info.GetArguments().AppendArguments (argv);
750 if (envp)
751 launch_info.GetEnvironmentEntries ().SetArguments (envp);
752
753 if (listener.IsValid())
Greg Claytondc6224e2014-10-21 01:00:42 +0000754 error.SetError (target_sp->Launch(listener.ref(), launch_info, NULL));
Greg Clayton2289fa42011-04-30 01:09:13 +0000755 else
Greg Claytondc6224e2014-10-21 01:00:42 +0000756 error.SetError (target_sp->Launch(target_sp->GetDebugger().GetListener(), launch_info, NULL));
Greg Clayton645bf542011-01-27 01:01:10 +0000757
Greg Claytonb09c5382013-12-13 17:20:18 +0000758 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton524e60b2010-10-06 22:10:17 +0000759 }
760 else
761 {
762 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000764
Caroline Tice20ad3c42010-10-29 21:48:37 +0000765 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000766 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000767 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
768 static_cast<void*>(target_sp.get()),
769 static_cast<void*>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000770
Greg Clayton5d5028b2010-10-06 03:53:16 +0000771 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772}
773
Greg Clayton0e615682012-02-24 05:03:03 +0000774SBProcess
775SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
776{
Greg Clayton5160ce52013-03-27 23:08:40 +0000777 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000778
Greg Clayton0e615682012-02-24 05:03:03 +0000779 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000780 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000781
Greg Clayton0e615682012-02-24 05:03:03 +0000782 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000783 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...",
784 static_cast<void*>(target_sp.get()));
785
Greg Clayton0e615682012-02-24 05:03:03 +0000786 if (target_sp)
787 {
788 Mutex::Locker api_locker (target_sp->GetAPIMutex());
789 StateType state = eStateInvalid;
Greg Claytonb09c5382013-12-13 17:20:18 +0000790 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000791 ProcessSP process_sp = target_sp->GetProcessSP();
792 if (process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000793 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000794 state = process_sp->GetState();
795
796 if (process_sp->IsAlive() && state != eStateConnected)
797 {
798 if (state == eStateAttaching)
799 error.SetErrorString ("process attach is in progress");
800 else
801 error.SetErrorString ("a process is already being debugged");
802 return sb_process;
803 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000804 }
Greg Clayton0e615682012-02-24 05:03:03 +0000805 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000806
807 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
808
Greg Clayton3e32ad62014-05-07 20:16:06 +0000809 if (!launch_info.GetExecutableFile())
810 {
811 Module *exe_module = target_sp->GetExecutableModulePointer();
812 if (exe_module)
813 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
814 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000815
816 const ArchSpec &arch_spec = target_sp->GetArchitecture();
817 if (arch_spec.IsValid())
818 launch_info.GetArchitecture () = arch_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000819
Greg Claytondc6224e2014-10-21 01:00:42 +0000820 error.SetError (target_sp->Launch (target_sp->GetDebugger().GetListener(), launch_info, NULL));
Greg Claytonb09c5382013-12-13 17:20:18 +0000821 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000822 }
823 else
824 {
825 error.SetErrorString ("SBTarget is invalid");
826 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000827
Greg Clayton0e615682012-02-24 05:03:03 +0000828 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
829 if (log)
Greg Claytonb09c5382013-12-13 17:20:18 +0000830 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000831 static_cast<void*>(target_sp.get()),
832 static_cast<void*>(sb_process.GetSP().get()));
833
Greg Clayton0e615682012-02-24 05:03:03 +0000834 return sb_process;
835}
836
837lldb::SBProcess
838SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
839{
Greg Clayton5160ce52013-03-27 23:08:40 +0000840 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000841
Greg Clayton0e615682012-02-24 05:03:03 +0000842 SBProcess sb_process;
843 ProcessSP process_sp;
844 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000845
Sean Callanan575a4542012-10-20 00:21:31 +0000846 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000847 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...",
848 static_cast<void*>(target_sp.get()));
849
Greg Clayton0e615682012-02-24 05:03:03 +0000850 if (target_sp)
851 {
852 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000853
Greg Clayton0e615682012-02-24 05:03:03 +0000854 StateType state = eStateInvalid;
855 process_sp = target_sp->GetProcessSP();
856 if (process_sp)
857 {
858 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000859
Greg Clayton0e615682012-02-24 05:03:03 +0000860 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000861 {
Greg Clayton0e615682012-02-24 05:03:03 +0000862 if (state == eStateAttaching)
863 error.SetErrorString ("process attach is in progress");
864 else
865 error.SetErrorString ("a process is already being debugged");
Sean Callanan575a4542012-10-20 00:21:31 +0000866 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000867 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000868 static_cast<void*>(target_sp.get()),
869 error.GetCString());
Greg Clayton0e615682012-02-24 05:03:03 +0000870 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000871 }
Greg Clayton0e615682012-02-24 05:03:03 +0000872 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000873
Greg Clayton0e615682012-02-24 05:03:03 +0000874 if (state != eStateConnected)
875 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
876
877 if (process_sp)
878 {
Greg Clayton0e615682012-02-24 05:03:03 +0000879 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000880 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000881 {
882 PlatformSP platform_sp = target_sp->GetPlatform();
Greg Clayton91e407e2012-09-27 00:03:39 +0000883 // See if we can pre-verify if a process exists or not
884 if (platform_sp && platform_sp->IsConnected())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000885 {
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000886 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Clayton91e407e2012-09-27 00:03:39 +0000887 ProcessInstanceInfo instance_info;
888 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
889 {
890 attach_info.SetUserID(instance_info.GetEffectiveUserID());
891 }
892 else
893 {
Daniel Malead01b2952012-11-29 21:49:15 +0000894 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000895 if (log)
896 {
897 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000898 static_cast<void*>(target_sp.get()), error.GetCString());
Sean Callanan575a4542012-10-20 00:21:31 +0000899 }
Greg Clayton91e407e2012-09-27 00:03:39 +0000900 return sb_process;
901 }
Han Ming Ongcec8c902012-02-29 00:12:56 +0000902 }
903 }
Han Ming Ongbee98392012-02-29 19:16:40 +0000904 error.SetError (process_sp->Attach (attach_info));
905 if (error.Success())
906 {
907 sb_process.SetSP (process_sp);
908 // If we are doing synchronous mode, then wait for the
909 // process to stop!
910 if (target_sp->GetDebugger().GetAsyncExecution () == false)
911 process_sp->WaitForProcessToStop (NULL);
912 }
Greg Clayton0e615682012-02-24 05:03:03 +0000913 }
914 else
915 {
916 error.SetErrorString ("unable to create lldb_private::Process");
917 }
918 }
919 else
920 {
921 error.SetErrorString ("SBTarget is invalid");
922 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000923
Sean Callanan575a4542012-10-20 00:21:31 +0000924 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000925 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000926 static_cast<void*>(target_sp.get()),
927 static_cast<void*>(process_sp.get()));
928
Greg Clayton0e615682012-02-24 05:03:03 +0000929 return sb_process;
930}
931
932
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000933#if defined(__APPLE__)
934
935lldb::SBProcess
936SBTarget::AttachToProcessWithID (SBListener &listener,
937 ::pid_t pid,
938 lldb::SBError& error)
939{
940 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
941}
942
943#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000944
945lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000946SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000947(
Greg Clayton4b045622011-02-03 21:28:34 +0000948 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000949 lldb::pid_t pid,// The process ID to attach to
950 SBError& error // An error explaining what went wrong if attach fails
951)
952{
Greg Clayton5160ce52013-03-27 23:08:40 +0000953 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000954
Greg Clayton524e60b2010-10-06 22:10:17 +0000955 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000956 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000957 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000958
959 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000960 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...",
961 static_cast<void*>(target_sp.get()), pid);
962
Greg Claytonacdbe812012-01-30 09:04:36 +0000963 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000964 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000965 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0c74e782011-06-24 03:21:43 +0000966
967 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000968 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000969 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +0000970 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000971 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000972
Greg Claytonb9556ac2012-01-30 07:41:31 +0000973 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000974 {
Greg Clayton0c74e782011-06-24 03:21:43 +0000975 if (state == eStateAttaching)
976 error.SetErrorString ("process attach is in progress");
977 else
978 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +0000979 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000980 }
Greg Clayton0c74e782011-06-24 03:21:43 +0000981 }
982
983 if (state == eStateConnected)
984 {
985 // If we are already connected, then we have already specified the
986 // listener, so if a valid listener is supplied, we need to error out
987 // to let the client know.
988 if (listener.IsValid())
989 {
990 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +0000991 return sb_process;
992 }
993 }
Greg Clayton4b045622011-02-03 21:28:34 +0000994 else
Greg Clayton0c74e782011-06-24 03:21:43 +0000995 {
996 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000997 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +0000998 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000999 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001000 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001001 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001002 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001003 sb_process.SetSP (process_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001004
Greg Clayton144f3a92011-11-15 03:53:30 +00001005 ProcessAttachInfo attach_info;
1006 attach_info.SetProcessID (pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001007
Han Ming Ong84647042012-02-25 01:07:38 +00001008 PlatformSP platform_sp = target_sp->GetPlatform();
1009 ProcessInstanceInfo instance_info;
1010 if (platform_sp->GetProcessInfo(pid, instance_info))
1011 {
1012 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ong84647042012-02-25 01:07:38 +00001013 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001014 error.SetError (process_sp->Attach (attach_info));
Greg Claytone2186ed2012-09-07 17:51:47 +00001015 if (error.Success())
1016 {
1017 // If we are doing synchronous mode, then wait for the
1018 // process to stop!
1019 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytondc6224e2014-10-21 01:00:42 +00001020 process_sp->WaitForProcessToStop (NULL);
Greg Claytone2186ed2012-09-07 17:51:47 +00001021 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001022 }
1023 else
1024 {
1025 error.SetErrorString ("unable to create lldb_private::Process");
1026 }
1027 }
1028 else
1029 {
1030 error.SetErrorString ("SBTarget is invalid");
1031 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001032
Sean Callanan575a4542012-10-20 00:21:31 +00001033 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001034 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001035 static_cast<void*>(target_sp.get()),
1036 static_cast<void*>(process_sp.get()));
Greg Clayton524e60b2010-10-06 22:10:17 +00001037 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001038}
1039
1040lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +00001041SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +00001042(
Greg Clayton4b045622011-02-03 21:28:34 +00001043 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +00001044 const char *name, // basename of process to attach to
1045 bool wait_for, // if true wait for a new instance of "name" to be launched
1046 SBError& error // An error explaining what went wrong if attach fails
1047)
1048{
Greg Clayton5160ce52013-03-27 23:08:40 +00001049 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001050
Greg Clayton524e60b2010-10-06 22:10:17 +00001051 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001052 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001053 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001054
Sean Callanan575a4542012-10-20 00:21:31 +00001055 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001056 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...",
1057 static_cast<void*>(target_sp.get()), name,
1058 wait_for ? "true" : "false");
1059
Greg Claytonacdbe812012-01-30 09:04:36 +00001060 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001061 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001062 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +00001063
Greg Clayton0c74e782011-06-24 03:21:43 +00001064 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +00001065 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +00001066 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +00001067 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001068 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001069
Greg Claytonb9556ac2012-01-30 07:41:31 +00001070 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001071 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001072 if (state == eStateAttaching)
1073 error.SetErrorString ("process attach is in progress");
1074 else
1075 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001076 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001077 }
Greg Clayton0c74e782011-06-24 03:21:43 +00001078 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001079
Greg Clayton0c74e782011-06-24 03:21:43 +00001080 if (state == eStateConnected)
1081 {
1082 // If we are already connected, then we have already specified the
1083 // listener, so if a valid listener is supplied, we need to error out
1084 // to let the client know.
1085 if (listener.IsValid())
1086 {
1087 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001088 return sb_process;
1089 }
1090 }
Greg Clayton4b045622011-02-03 21:28:34 +00001091 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001092 {
1093 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001094 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001095 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001096 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001097 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001098
Greg Claytonb9556ac2012-01-30 07:41:31 +00001099 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001100 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001101 sb_process.SetSP (process_sp);
Greg Clayton144f3a92011-11-15 03:53:30 +00001102 ProcessAttachInfo attach_info;
1103 attach_info.GetExecutableFile().SetFile(name, false);
1104 attach_info.SetWaitForLaunch(wait_for);
Greg Claytonb9556ac2012-01-30 07:41:31 +00001105 error.SetError (process_sp->Attach (attach_info));
Daniel Malead659dc12013-04-01 19:47:00 +00001106 if (error.Success())
1107 {
1108 // If we are doing synchronous mode, then wait for the
1109 // process to stop!
1110 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1111 process_sp->WaitForProcessToStop (NULL);
1112 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001113 }
1114 else
1115 {
1116 error.SetErrorString ("unable to create lldb_private::Process");
1117 }
1118 }
1119 else
1120 {
1121 error.SetErrorString ("SBTarget is invalid");
1122 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001123
Sean Callanan575a4542012-10-20 00:21:31 +00001124 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001125 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001126 static_cast<void*>(target_sp.get()),
1127 static_cast<void*>(process_sp.get()));
Greg Clayton524e60b2010-10-06 22:10:17 +00001128 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001129}
1130
James McIlree9631aae2011-03-04 00:31:13 +00001131lldb::SBProcess
1132SBTarget::ConnectRemote
1133(
1134 SBListener &listener,
1135 const char *url,
1136 const char *plugin_name,
1137 SBError& error
1138)
1139{
Greg Clayton5160ce52013-03-27 23:08:40 +00001140 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001141
James McIlree9631aae2011-03-04 00:31:13 +00001142 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001143 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001144 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001145
Sean Callanan575a4542012-10-20 00:21:31 +00001146 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001147 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...",
1148 static_cast<void*>(target_sp.get()), url, plugin_name);
1149
Greg Claytonacdbe812012-01-30 09:04:36 +00001150 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001151 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001152 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +00001153 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001154 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001155 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001156 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001157
Greg Claytonb9556ac2012-01-30 07:41:31 +00001158 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001159 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001160 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001161 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +00001162 }
1163 else
1164 {
1165 error.SetErrorString ("unable to create lldb_private::Process");
1166 }
1167 }
1168 else
1169 {
1170 error.SetErrorString ("SBTarget is invalid");
1171 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001172
Sean Callanan575a4542012-10-20 00:21:31 +00001173 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001174 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001175 static_cast<void*>(target_sp.get()),
1176 static_cast<void*>(process_sp.get()));
James McIlree9631aae2011-03-04 00:31:13 +00001177 return sb_process;
1178}
1179
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180SBFileSpec
1181SBTarget::GetExecutable ()
1182{
Caroline Ticeceb6b132010-10-26 03:11:13 +00001183
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +00001185 TargetSP target_sp(GetSP());
1186 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001188 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +00001189 if (exe_module)
1190 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001192
Greg Clayton5160ce52013-03-27 23:08:40 +00001193 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001194 if (log)
1195 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001196 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
1197 static_cast<void*>(target_sp.get()),
1198 static_cast<const void*>(exe_file_spec.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001199 }
1200
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 return exe_file_spec;
1202}
1203
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205SBTarget::operator == (const SBTarget &rhs) const
1206{
Greg Clayton66111032010-06-23 01:19:29 +00001207 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208}
1209
1210bool
1211SBTarget::operator != (const SBTarget &rhs) const
1212{
Greg Clayton66111032010-06-23 01:19:29 +00001213 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214}
1215
Greg Claytonb9556ac2012-01-30 07:41:31 +00001216lldb::TargetSP
1217SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +00001218{
1219 return m_opaque_sp;
1220}
1221
Greg Clayton66111032010-06-23 01:19:29 +00001222void
Greg Claytonb9556ac2012-01-30 07:41:31 +00001223SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +00001224{
1225 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226}
1227
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001228lldb::SBAddress
1229SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001230{
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001231 lldb::SBAddress sb_addr;
1232 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +00001233 TargetSP target_sp(GetSP());
1234 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001235 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001236 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytond5944cd2013-12-06 01:12:00 +00001237 if (target_sp->ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001238 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001239 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001240
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001241 // We have a load address that isn't in a section, just return an address
1242 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +00001243 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001244 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001245}
1246
Matthew Gardinerc928de32014-10-22 07:22:56 +00001247lldb::SBAddress
1248SBTarget::ResolveFileAddress (lldb::addr_t file_addr)
1249{
1250 lldb::SBAddress sb_addr;
1251 Address &addr = sb_addr.ref();
1252 TargetSP target_sp(GetSP());
1253 if (target_sp)
1254 {
1255 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1256 if (target_sp->ResolveFileAddress (file_addr, addr))
1257 return sb_addr;
1258 }
1259
1260 addr.SetRawAddress(file_addr);
1261 return sb_addr;
1262}
Greg Claytond5944cd2013-12-06 01:12:00 +00001263
1264lldb::SBAddress
1265SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
1266{
1267 lldb::SBAddress sb_addr;
1268 Address &addr = sb_addr.ref();
1269 TargetSP target_sp(GetSP());
1270 if (target_sp)
1271 {
1272 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1273 if (target_sp->ResolveLoadAddress (vm_addr, addr))
1274 return sb_addr;
1275 }
1276
1277 // We have a load address that isn't in a section, just return an address
1278 // with the offset filled in (the address) and the section set to NULL
1279 addr.SetRawAddress(vm_addr);
1280 return sb_addr;
1281}
1282
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001283SBSymbolContext
Greg Claytoneb023e72013-10-11 19:48:25 +00001284SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
1285 uint32_t resolve_scope)
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001286{
1287 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001288 if (addr.IsValid())
1289 {
1290 TargetSP target_sp(GetSP());
1291 if (target_sp)
1292 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1293 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001294 return sc;
1295}
1296
Matthew Gardinerc928de32014-10-22 07:22:56 +00001297size_t
1298SBTarget::ReadMemory (const SBAddress addr,
1299 void *buf,
1300 size_t size,
1301 lldb::SBError &error)
1302{
1303 SBError sb_error;
1304 size_t bytes_read = 0;
1305 TargetSP target_sp(GetSP());
1306 if (target_sp)
1307 {
1308 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1309 lldb_private::Address addr_priv(addr.GetFileAddress(), NULL);
1310 lldb_private::Error err_priv;
1311 bytes_read = target_sp->ReadMemory(addr_priv, false, buf, size, err_priv);
1312 if(err_priv.Fail())
1313 {
1314 sb_error.SetError(err_priv.GetError(), err_priv.GetType());
1315 }
1316 }
1317
1318 return bytes_read;
1319}
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001320
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001322SBTarget::BreakpointCreateByLocation (const char *file,
1323 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324{
Greg Clayton7481c202010-11-08 00:28:40 +00001325 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326}
1327
1328SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001329SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
1330 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331{
Greg Clayton5160ce52013-03-27 23:08:40 +00001332 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001333
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001335 TargetSP target_sp(GetSP());
1336 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001337 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001338 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001339
Greg Clayton1f746072012-08-29 21:13:06 +00001340 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001341 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001342 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001343 const bool hardware = false;
1344 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001345 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001346
1347 if (log)
1348 {
1349 SBStream sstr;
1350 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001351 char path[PATH_MAX];
1352 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001353 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1354 static_cast<void*>(target_sp.get()), path, line,
1355 static_cast<void*>(sb_bp.get()), sstr.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001356 }
1357
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 return sb_bp;
1359}
1360
1361SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001362SBTarget::BreakpointCreateByName (const char *symbol_name,
1363 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364{
Greg Clayton5160ce52013-03-27 23:08:40 +00001365 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001366
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001368 TargetSP target_sp(GetSP());
1369 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001371 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001372
Jim Inghama8558b62012-05-22 00:12:20 +00001373 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001374 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001375 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001376 if (module_name && module_name[0])
1377 {
Jim Ingham969795f2011-09-21 01:17:13 +00001378 FileSpecList module_spec_list;
1379 module_spec_list.Append (FileSpec (module_name, false));
Greg Claytoneb023e72013-10-11 19:48:25 +00001380 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001381 }
1382 else
1383 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001384 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385 }
1386 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001387
Caroline Ticeceb6b132010-10-26 03:11:13 +00001388 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001389 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1390 static_cast<void*>(target_sp.get()), symbol_name,
1391 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001392
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 return sb_bp;
1394}
1395
Jim Ingham87df91b2011-09-23 00:54:11 +00001396lldb::SBBreakpoint
1397SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001398 const SBFileSpecList &module_list,
1399 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001400{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001401 uint32_t name_type_mask = eFunctionNameTypeAuto;
1402 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1403}
1404
1405lldb::SBBreakpoint
1406SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001407 uint32_t name_type_mask,
1408 const SBFileSpecList &module_list,
1409 const SBFileSpecList &comp_unit_list)
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001410{
Greg Clayton5160ce52013-03-27 23:08:40 +00001411 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001412
1413 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001414 TargetSP target_sp(GetSP());
1415 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001416 {
Jim Inghama8558b62012-05-22 00:12:20 +00001417 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001418 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001419 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001420 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1421 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Greg Claytoneb023e72013-10-11 19:48:25 +00001422 comp_unit_list.get(),
1423 symbol_name,
1424 name_type_mask,
1425 skip_prologue,
1426 internal,
1427 hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001428 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001429
Jim Ingham87df91b2011-09-23 00:54:11 +00001430 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001431 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1432 static_cast<void*>(target_sp.get()), symbol_name,
1433 name_type_mask, static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001434
1435 return sb_bp;
1436}
1437
Jim Inghamfab10e82012-03-06 00:37:27 +00001438lldb::SBBreakpoint
1439SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1440 uint32_t num_names,
1441 uint32_t name_type_mask,
1442 const SBFileSpecList &module_list,
1443 const SBFileSpecList &comp_unit_list)
1444{
Greg Clayton5160ce52013-03-27 23:08:40 +00001445 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001446
1447 SBBreakpoint sb_bp;
1448 TargetSP target_sp(GetSP());
1449 if (target_sp && num_names > 0)
1450 {
1451 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001452 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001453 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001454 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001455 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1456 comp_unit_list.get(),
1457 symbol_names,
1458 num_names,
1459 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001460 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001461 internal,
1462 hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001463 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001464
Jim Inghamfab10e82012-03-06 00:37:27 +00001465 if (log)
1466 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001467 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
1468 static_cast<void*>(target_sp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001469 for (uint32_t i = 0 ; i < num_names; i++)
1470 {
1471 char sep;
1472 if (i < num_names - 1)
1473 sep = ',';
1474 else
1475 sep = '}';
1476 if (symbol_names[i] != NULL)
1477 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1478 else
1479 log->Printf ("\"<NULL>\"%c ", sep);
Jim Inghamfab10e82012-03-06 00:37:27 +00001480 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001481 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
1482 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001483 }
1484
1485 return sb_bp;
1486}
Jim Ingham87df91b2011-09-23 00:54:11 +00001487
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001488SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001489SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1490 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491{
Greg Clayton5160ce52013-03-27 23:08:40 +00001492 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001493
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001494 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001495 TargetSP target_sp(GetSP());
1496 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001498 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001500 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001501 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001502 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001503
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504 if (module_name && module_name[0])
1505 {
Jim Ingham969795f2011-09-21 01:17:13 +00001506 FileSpecList module_spec_list;
1507 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001508
Greg Claytoneb023e72013-10-11 19:48:25 +00001509 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510 }
1511 else
1512 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001513 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514 }
1515 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001516
1517 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001518 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1519 static_cast<void*>(target_sp.get()), symbol_name_regex,
1520 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001521
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001522 return sb_bp;
1523}
1524
Jim Ingham87df91b2011-09-23 00:54:11 +00001525lldb::SBBreakpoint
1526SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001527 const SBFileSpecList &module_list,
1528 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001529{
Greg Clayton5160ce52013-03-27 23:08:40 +00001530 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531
Jim Ingham87df91b2011-09-23 00:54:11 +00001532 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001533 TargetSP target_sp(GetSP());
1534 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001535 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001536 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001537 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001538 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001539 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001540 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001541
Greg Claytoneb023e72013-10-11 19:48:25 +00001542 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001543 }
1544
1545 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001546 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1547 static_cast<void*>(target_sp.get()), symbol_name_regex,
1548 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001549
1550 return sb_bp;
1551}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001552
1553SBBreakpoint
1554SBTarget::BreakpointCreateByAddress (addr_t address)
1555{
Greg Clayton5160ce52013-03-27 23:08:40 +00001556 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001557
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001558 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001559 TargetSP target_sp(GetSP());
1560 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001561 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001562 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001563 const bool hardware = false;
1564 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001565 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001566
Caroline Ticeceb6b132010-10-26 03:11:13 +00001567 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001568 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1569 static_cast<void*>(target_sp.get()),
1570 static_cast<uint64_t>(address),
1571 static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001572
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573 return sb_bp;
1574}
1575
Jim Ingham969795f2011-09-21 01:17:13 +00001576lldb::SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001577SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1578 const lldb::SBFileSpec &source_file,
1579 const char *module_name)
Jim Ingham969795f2011-09-21 01:17:13 +00001580{
Greg Clayton5160ce52013-03-27 23:08:40 +00001581 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001582
1583 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001584 TargetSP target_sp(GetSP());
1585 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001586 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001587 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001588 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001589 FileSpecList source_file_spec_list;
Greg Claytoneb023e72013-10-11 19:48:25 +00001590 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001591 source_file_spec_list.Append (source_file.ref());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001592
Jim Ingham969795f2011-09-21 01:17:13 +00001593 if (module_name && module_name[0])
1594 {
1595 FileSpecList module_spec_list;
1596 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001597
Greg Claytoneb023e72013-10-11 19:48:25 +00001598 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001599 }
1600 else
1601 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001602 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001603 }
1604 }
1605
1606 if (log)
1607 {
1608 char path[PATH_MAX];
1609 source_file->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001610 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1611 static_cast<void*>(target_sp.get()), source_regex, path,
1612 module_name, static_cast<void*>(sb_bp.get()));
Jim Ingham969795f2011-09-21 01:17:13 +00001613 }
1614
1615 return sb_bp;
1616}
1617
Jim Ingham87df91b2011-09-23 00:54:11 +00001618lldb::SBBreakpoint
1619SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001620 const SBFileSpecList &module_list,
1621 const lldb::SBFileSpecList &source_file_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001622{
Greg Clayton5160ce52013-03-27 23:08:40 +00001623 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001624
1625 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001626 TargetSP target_sp(GetSP());
1627 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001628 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001629 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001630 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001631 RegularExpression regexp(source_regex);
Greg Claytoneb023e72013-10-11 19:48:25 +00001632 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001633 }
1634
1635 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001636 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1637 static_cast<void*>(target_sp.get()), source_regex,
1638 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001639
1640 return sb_bp;
1641}
Jim Ingham969795f2011-09-21 01:17:13 +00001642
Jim Inghamfab10e82012-03-06 00:37:27 +00001643lldb::SBBreakpoint
1644SBTarget::BreakpointCreateForException (lldb::LanguageType language,
Greg Claytoneb023e72013-10-11 19:48:25 +00001645 bool catch_bp,
1646 bool throw_bp)
Jim Inghamfab10e82012-03-06 00:37:27 +00001647{
Greg Clayton5160ce52013-03-27 23:08:40 +00001648 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001649
1650 SBBreakpoint sb_bp;
1651 TargetSP target_sp(GetSP());
1652 if (target_sp)
1653 {
1654 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001655 const bool hardware = false;
1656 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001657 }
1658
1659 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001660 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1661 static_cast<void*>(target_sp.get()),
Jim Inghamfab10e82012-03-06 00:37:27 +00001662 LanguageRuntime::GetNameForLanguageType(language),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001663 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1664 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001665
1666 return sb_bp;
1667}
1668
Greg Clayton9fed0d82010-07-23 23:33:17 +00001669uint32_t
1670SBTarget::GetNumBreakpoints () const
1671{
Greg Claytonacdbe812012-01-30 09:04:36 +00001672 TargetSP target_sp(GetSP());
1673 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001674 {
1675 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001676 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001677 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001678 return 0;
1679}
1680
1681SBBreakpoint
1682SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1683{
1684 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001685 TargetSP target_sp(GetSP());
1686 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001687 {
1688 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001689 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001690 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001691 return sb_breakpoint;
1692}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001693
1694bool
1695SBTarget::BreakpointDelete (break_id_t bp_id)
1696{
Greg Clayton5160ce52013-03-27 23:08:40 +00001697 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001698
Caroline Ticeceb6b132010-10-26 03:11:13 +00001699 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001700 TargetSP target_sp(GetSP());
1701 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001702 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001703 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1704 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001705 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001706
1707 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001708 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1709 static_cast<void*>(target_sp.get()),
1710 static_cast<uint32_t>(bp_id), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001711
1712 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713}
1714
Johnny Chen5d043462011-09-26 22:40:50 +00001715SBBreakpoint
1716SBTarget::FindBreakpointByID (break_id_t bp_id)
1717{
Greg Clayton5160ce52013-03-27 23:08:40 +00001718 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001719
1720 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001721 TargetSP target_sp(GetSP());
1722 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001723 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001724 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1725 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001726 }
1727
1728 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001729 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1730 static_cast<void*>(target_sp.get()),
1731 static_cast<uint32_t>(bp_id),
1732 static_cast<void*>(sb_breakpoint.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001733
1734 return sb_breakpoint;
1735}
1736
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001737bool
1738SBTarget::EnableAllBreakpoints ()
1739{
Greg Claytonacdbe812012-01-30 09:04:36 +00001740 TargetSP target_sp(GetSP());
1741 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001742 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001743 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1744 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745 return true;
1746 }
1747 return false;
1748}
1749
1750bool
1751SBTarget::DisableAllBreakpoints ()
1752{
Greg Claytonacdbe812012-01-30 09:04:36 +00001753 TargetSP target_sp(GetSP());
1754 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001755 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001756 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1757 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001758 return true;
1759 }
1760 return false;
1761}
1762
1763bool
1764SBTarget::DeleteAllBreakpoints ()
1765{
Greg Claytonacdbe812012-01-30 09:04:36 +00001766 TargetSP target_sp(GetSP());
1767 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001769 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1770 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771 return true;
1772 }
1773 return false;
1774}
1775
Johnny Chen5d043462011-09-26 22:40:50 +00001776uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001777SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001778{
Greg Claytonacdbe812012-01-30 09:04:36 +00001779 TargetSP target_sp(GetSP());
1780 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001781 {
Johnny Chen01a67862011-10-14 00:42:25 +00001782 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001783 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001784 }
1785 return 0;
1786}
1787
Greg Clayton1b282f92011-10-13 18:08:26 +00001788SBWatchpoint
1789SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001790{
Johnny Chen01a67862011-10-14 00:42:25 +00001791 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001792 TargetSP target_sp(GetSP());
1793 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001794 {
Johnny Chen01a67862011-10-14 00:42:25 +00001795 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001796 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001797 }
Johnny Chen01a67862011-10-14 00:42:25 +00001798 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001799}
1800
1801bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001802SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001803{
Greg Clayton5160ce52013-03-27 23:08:40 +00001804 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001805
1806 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001807 TargetSP target_sp(GetSP());
1808 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001809 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001810 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001811 Mutex::Locker locker;
1812 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001813 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001814 }
1815
1816 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001817 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1818 static_cast<void*>(target_sp.get()),
1819 static_cast<uint32_t>(wp_id), result);
Johnny Chen5d043462011-09-26 22:40:50 +00001820
1821 return result;
1822}
1823
Greg Clayton1b282f92011-10-13 18:08:26 +00001824SBWatchpoint
1825SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001826{
Greg Clayton5160ce52013-03-27 23:08:40 +00001827 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001828
Greg Clayton1b282f92011-10-13 18:08:26 +00001829 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001830 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001831 TargetSP target_sp(GetSP());
1832 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001833 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001834 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001835 Mutex::Locker locker;
1836 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001837 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1838 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001839 }
1840
1841 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001842 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1843 static_cast<void*>(target_sp.get()),
1844 static_cast<uint32_t>(wp_id),
1845 static_cast<void*>(watchpoint_sp.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001846
Greg Clayton1b282f92011-10-13 18:08:26 +00001847 return sb_watchpoint;
1848}
1849
1850lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001851SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001852{
Greg Clayton5160ce52013-03-27 23:08:40 +00001853 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001854
Greg Clayton1b282f92011-10-13 18:08:26 +00001855 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001856 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001857 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001858 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001859 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001860 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001861 uint32_t watch_type = 0;
1862 if (read)
1863 watch_type |= LLDB_WATCH_TYPE_READ;
1864 if (write)
1865 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001866 if (watch_type == 0)
1867 {
1868 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1869 return sb_watchpoint;
1870 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001871
Johnny Chen7385a5a2012-05-31 22:56:36 +00001872 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001873 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001874 // This API doesn't take in a type, so we can't figure out what it is.
1875 ClangASTType *type = NULL;
1876 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001877 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001878 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001879 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001880
Greg Clayton1b282f92011-10-13 18:08:26 +00001881 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001882 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001883 static_cast<void*>(target_sp.get()), addr,
1884 static_cast<uint32_t>(size),
1885 static_cast<void*>(watchpoint_sp.get()));
1886
Greg Clayton1b282f92011-10-13 18:08:26 +00001887 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001888}
1889
1890bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001891SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001892{
Greg Claytonacdbe812012-01-30 09:04:36 +00001893 TargetSP target_sp(GetSP());
1894 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001895 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001896 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001897 Mutex::Locker locker;
1898 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001899 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001900 return true;
1901 }
1902 return false;
1903}
1904
1905bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001906SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001907{
Greg Claytonacdbe812012-01-30 09:04:36 +00001908 TargetSP target_sp(GetSP());
1909 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001910 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001911 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001912 Mutex::Locker locker;
1913 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001914 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001915 return true;
1916 }
1917 return false;
1918}
1919
Enrico Granata347c2aa2013-10-08 21:49:02 +00001920SBValue
1921SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1922{
1923 SBValue sb_value;
1924 lldb::ValueObjectSP new_value_sp;
1925 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1926 {
1927 lldb::addr_t address(addr.GetLoadAddress(*this));
1928 lldb::TypeImplSP type_impl_sp (type.GetSP());
Enrico Granatadc4db5a2013-10-29 00:28:35 +00001929 ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType(true).GetPointerType ());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001930 if (pointer_ast_type)
1931 {
1932 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001933
Enrico Granata347c2aa2013-10-08 21:49:02 +00001934 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1935 ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1936 pointer_ast_type,
1937 ConstString(name),
1938 buffer,
1939 exe_ctx.GetByteOrder(),
1940 exe_ctx.GetAddressByteSize()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001941
Enrico Granata347c2aa2013-10-08 21:49:02 +00001942 if (ptr_result_valobj_sp)
1943 {
1944 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1945 Error err;
1946 new_value_sp = ptr_result_valobj_sp->Dereference(err);
1947 if (new_value_sp)
1948 new_value_sp->SetName(ConstString(name));
1949 }
1950 }
1951 }
1952 sb_value.SetSP(new_value_sp);
1953 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1954 if (log)
1955 {
1956 if (new_value_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001957 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1958 static_cast<void*>(m_opaque_sp.get()),
1959 new_value_sp->GetName().AsCString());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001960 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001961 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1962 static_cast<void*>(m_opaque_sp.get()));
Enrico Granata347c2aa2013-10-08 21:49:02 +00001963 }
1964 return sb_value;
1965}
1966
Johnny Chen5d043462011-09-26 22:40:50 +00001967bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001968SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001969{
Greg Claytonacdbe812012-01-30 09:04:36 +00001970 TargetSP target_sp(GetSP());
1971 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001972 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001973 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001974 Mutex::Locker locker;
1975 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001976 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001977 return true;
1978 }
1979 return false;
1980}
1981
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001982
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001983lldb::SBModule
1984SBTarget::AddModule (const char *path,
1985 const char *triple,
1986 const char *uuid_cstr)
1987{
Greg Claytonb210aec2012-04-23 20:23:39 +00001988 return AddModule (path, triple, uuid_cstr, NULL);
1989}
1990
1991lldb::SBModule
1992SBTarget::AddModule (const char *path,
1993 const char *triple,
1994 const char *uuid_cstr,
1995 const char *symfile)
1996{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001997 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001998 TargetSP target_sp(GetSP());
1999 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002000 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002001 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002002 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00002003 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00002004
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002005 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00002006 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00002007
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002008 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00002009 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00002010 else
2011 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00002012
2013 if (symfile)
2014 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002015
Greg Claytonb9a01b32012-02-26 05:51:37 +00002016 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002017 }
2018 return sb_module;
2019}
2020
Greg Clayton226cce22013-07-08 22:22:41 +00002021lldb::SBModule
2022SBTarget::AddModule (const SBModuleSpec &module_spec)
2023{
2024 lldb::SBModule sb_module;
2025 TargetSP target_sp(GetSP());
2026 if (target_sp)
2027 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
2028 return sb_module;
2029}
2030
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002031bool
2032SBTarget::AddModule (lldb::SBModule &module)
2033{
Greg Claytonacdbe812012-01-30 09:04:36 +00002034 TargetSP target_sp(GetSP());
2035 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002036 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002037 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002038 return true;
2039 }
2040 return false;
2041}
2042
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002043uint32_t
2044SBTarget::GetNumModules () const
2045{
Greg Clayton5160ce52013-03-27 23:08:40 +00002046 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002047
Caroline Ticeceb6b132010-10-26 03:11:13 +00002048 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00002049 TargetSP target_sp(GetSP());
2050 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002051 {
2052 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002053 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00002054 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002055
2056 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002057 log->Printf ("SBTarget(%p)::GetNumModules () => %d",
2058 static_cast<void*>(target_sp.get()), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002059
2060 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002061}
2062
Greg Clayton48e42542010-07-30 20:12:55 +00002063void
2064SBTarget::Clear ()
2065{
Greg Clayton5160ce52013-03-27 23:08:40 +00002066 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002067
2068 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002069 log->Printf ("SBTarget(%p)::Clear ()",
2070 static_cast<void*>(m_opaque_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002071
Greg Clayton48e42542010-07-30 20:12:55 +00002072 m_opaque_sp.reset();
2073}
2074
2075
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002076SBModule
2077SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2078{
2079 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002080 TargetSP target_sp(GetSP());
2081 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00002082 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002083 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002084 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00002085 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00002086 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002087 return sb_module;
2088}
2089
Greg Clayton13d19502012-01-29 06:07:39 +00002090lldb::ByteOrder
2091SBTarget::GetByteOrder ()
2092{
Greg Claytonacdbe812012-01-30 09:04:36 +00002093 TargetSP target_sp(GetSP());
2094 if (target_sp)
2095 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00002096 return eByteOrderInvalid;
2097}
2098
2099const char *
2100SBTarget::GetTriple ()
2101{
Greg Claytonacdbe812012-01-30 09:04:36 +00002102 TargetSP target_sp(GetSP());
2103 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00002104 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002105 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00002106 // Unique the string so we don't run into ownership issues since
2107 // the const strings put the string into the string pool once and
2108 // the strings never comes out
2109 ConstString const_triple (triple.c_str());
2110 return const_triple.GetCString();
2111 }
2112 return NULL;
2113}
2114
2115uint32_t
Matthew Gardinerc928de32014-10-22 07:22:56 +00002116SBTarget::GetDataByteSize ()
2117{
2118 TargetSP target_sp(GetSP());
2119 if (target_sp)
2120 {
2121 return target_sp->GetArchitecture().GetDataByteSize() ;
2122 }
2123 return 0;
2124}
2125
2126uint32_t
2127SBTarget::GetCodeByteSize ()
2128{
2129 TargetSP target_sp(GetSP());
2130 if (target_sp)
2131 {
2132 return target_sp->GetArchitecture().GetCodeByteSize() ;
2133 }
2134 return 0;
2135}
2136
2137uint32_t
Greg Clayton13d19502012-01-29 06:07:39 +00002138SBTarget::GetAddressByteSize()
2139{
Greg Claytonacdbe812012-01-30 09:04:36 +00002140 TargetSP target_sp(GetSP());
2141 if (target_sp)
2142 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00002143 return sizeof(void*);
2144}
2145
2146
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002147SBModule
2148SBTarget::GetModuleAtIndex (uint32_t idx)
2149{
Greg Clayton5160ce52013-03-27 23:08:40 +00002150 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002151
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002152 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002153 ModuleSP module_sp;
2154 TargetSP target_sp(GetSP());
2155 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002156 {
2157 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002158 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2159 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002160 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002161
2162 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002163 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2164 static_cast<void*>(target_sp.get()), idx,
2165 static_cast<void*>(module_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002166
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002167 return sb_module;
2168}
2169
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002170bool
2171SBTarget::RemoveModule (lldb::SBModule module)
2172{
Greg Claytonacdbe812012-01-30 09:04:36 +00002173 TargetSP target_sp(GetSP());
2174 if (target_sp)
2175 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002176 return false;
2177}
2178
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002179
2180SBBroadcaster
2181SBTarget::GetBroadcaster () const
2182{
Greg Clayton5160ce52013-03-27 23:08:40 +00002183 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002184
Greg Claytonacdbe812012-01-30 09:04:36 +00002185 TargetSP target_sp(GetSP());
2186 SBBroadcaster broadcaster(target_sp.get(), false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002187
Caroline Ticeceb6b132010-10-26 03:11:13 +00002188 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002189 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2190 static_cast<void*>(target_sp.get()),
2191 static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002192
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002193 return broadcaster;
2194}
2195
Caroline Ticedde9cff2010-09-20 05:20:02 +00002196bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002197SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002198{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002199 Stream &strm = description.ref();
2200
Greg Claytonacdbe812012-01-30 09:04:36 +00002201 TargetSP target_sp(GetSP());
2202 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002203 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002204 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002205 }
2206 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002207 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002208
2209 return true;
2210}
2211
Greg Clayton5569e642012-02-06 01:44:54 +00002212lldb::SBSymbolContextList
2213SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002214{
Greg Clayton5569e642012-02-06 01:44:54 +00002215 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002216 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002217 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002218 TargetSP target_sp(GetSP());
2219 if (target_sp)
2220 {
2221 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002222 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002223 const bool append = true;
2224 target_sp->GetImages().FindFunctions (ConstString(name),
2225 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002226 symbols_ok,
2227 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002228 append,
2229 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002230 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002231 }
Greg Clayton5569e642012-02-06 01:44:54 +00002232 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002233}
2234
Carlo Kok0fd6fd42014-09-19 19:38:19 +00002235lldb::SBSymbolContextList
2236SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
2237{
2238 lldb::SBSymbolContextList sb_sc_list;
2239 if (name && name[0])
2240 {
2241 TargetSP target_sp(GetSP());
2242 if (target_sp)
2243 {
2244 std::string regexstr;
2245 switch (matchtype)
2246 {
2247 case eMatchTypeRegex:
2248 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
2249 break;
2250 case eMatchTypeStartsWith:
2251 regexstr = llvm::Regex::escape(name) + ".*";
2252 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
2253 break;
2254 default:
2255 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
2256 break;
2257 }
2258 }
2259 }
2260 return sb_sc_list;
2261}
2262
Enrico Granata6f3533f2011-07-29 19:53:35 +00002263lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002264SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002265{
Greg Claytonacdbe812012-01-30 09:04:36 +00002266 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002267 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002268 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002269 ConstString const_typename(typename_cstr);
2270 SymbolContext sc;
2271 const bool exact_match = false;
2272
2273 const ModuleList &module_list = target_sp->GetImages();
2274 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002275 for (size_t idx = 0; idx < count; idx++)
2276 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002277 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2278 if (module_sp)
2279 {
2280 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2281 if (type_sp)
2282 return SBType(type_sp);
2283 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002284 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002285
2286 // Didn't find the type in the symbols; try the Objective-C runtime
2287 // if one is installed
2288
2289 ProcessSP process_sp(target_sp->GetProcessSP());
2290
2291 if (process_sp)
2292 {
2293 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2294
2295 if (objc_language_runtime)
2296 {
2297 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2298
2299 if (objc_type_vendor)
2300 {
2301 std::vector <ClangASTType> types;
2302
2303 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2304 return SBType(types[0]);
2305 }
2306 }
2307 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002308
2309 // No matches, search for basic typename matches
2310 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2311 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002312 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002313 }
2314 return SBType();
2315}
2316
Greg Claytonb43165b2012-12-05 21:24:42 +00002317SBType
2318SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002319{
Greg Claytonacdbe812012-01-30 09:04:36 +00002320 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002321 if (target_sp)
2322 {
2323 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2324 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002325 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00002326 }
2327 return SBType();
2328}
2329
2330
2331lldb::SBTypeList
2332SBTarget::FindTypes (const char* typename_cstr)
2333{
2334 SBTypeList sb_type_list;
2335 TargetSP target_sp(GetSP());
2336 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002337 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002338 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002339 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002340 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002341 SymbolContext sc;
2342 TypeList type_list;
2343
Greg Clayton29399a22012-04-06 17:41:13 +00002344 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002345 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002346 exact_match,
2347 UINT32_MAX,
2348 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002349
Greg Claytonb43165b2012-12-05 21:24:42 +00002350 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002351 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002352 for (size_t idx = 0; idx < num_matches; idx++)
2353 {
2354 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2355 if (type_sp)
2356 sb_type_list.Append(SBType(type_sp));
2357 }
2358 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002359
2360 // Try the Objective-C runtime if one is installed
2361
2362 ProcessSP process_sp(target_sp->GetProcessSP());
2363
2364 if (process_sp)
2365 {
2366 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2367
2368 if (objc_language_runtime)
2369 {
2370 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2371
2372 if (objc_type_vendor)
2373 {
2374 std::vector <ClangASTType> types;
2375
2376 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2377 {
2378 for (ClangASTType &type : types)
2379 {
2380 sb_type_list.Append(SBType(type));
2381 }
2382 }
2383 }
2384 }
2385 }
2386
2387 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002388 {
2389 // No matches, search for basic typename matches
2390 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2391 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002392 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002393 }
2394 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002395 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002396}
2397
Greg Claytondea8cb42011-06-29 22:09:02 +00002398SBValueList
2399SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2400{
2401 SBValueList sb_value_list;
2402
Greg Claytonacdbe812012-01-30 09:04:36 +00002403 TargetSP target_sp(GetSP());
2404 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002405 {
2406 VariableList variable_list;
2407 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002408 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2409 append,
2410 max_matches,
2411 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002412
2413 if (match_count > 0)
2414 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002415 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002416 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002417 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002418 for (uint32_t i=0; i<match_count; ++i)
2419 {
2420 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2421 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002422 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002423 }
2424 }
2425 }
2426
2427 return sb_value_list;
2428}
2429
Carlo Kok0fd6fd42014-09-19 19:38:19 +00002430SBValueList
2431SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
2432{
2433 SBValueList sb_value_list;
2434
2435 TargetSP target_sp(GetSP());
2436 if (name && target_sp)
2437 {
2438 VariableList variable_list;
2439 const bool append = true;
2440
2441 std::string regexstr;
2442 uint32_t match_count;
2443 switch (matchtype)
2444 {
2445 case eMatchTypeNormal:
2446 match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
2447 append,
2448 max_matches,
2449 variable_list);
2450 break;
2451 case eMatchTypeRegex:
2452 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
2453 append,
2454 max_matches,
2455 variable_list);
2456 break;
2457 case eMatchTypeStartsWith:
2458 regexstr = llvm::Regex::escape(name) + ".*";
2459 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
2460 append,
2461 max_matches,
2462 variable_list);
2463 break;
2464 }
2465
2466
2467 if (match_count > 0)
2468 {
2469 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2470 if (exe_scope == NULL)
2471 exe_scope = target_sp.get();
2472 for (uint32_t i = 0; i<match_count; ++i)
2473 {
2474 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
2475 if (valobj_sp)
2476 sb_value_list.Append(SBValue(valobj_sp));
2477 }
2478 }
2479 }
2480
2481 return sb_value_list;
2482}
2483
2484
Enrico Granatabcd80b42013-01-16 18:53:52 +00002485lldb::SBValue
2486SBTarget::FindFirstGlobalVariable (const char* name)
2487{
2488 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2489 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2490 return sb_value_list.GetValueAtIndex(0);
2491 return SBValue();
2492}
2493
Jim Inghame37d6052011-09-13 00:29:56 +00002494SBSourceManager
2495SBTarget::GetSourceManager()
2496{
2497 SBSourceManager source_manager (*this);
2498 return source_manager;
2499}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002500
Sean Callanan50952e92011-12-14 23:49:37 +00002501lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002502SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2503{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002504 return ReadInstructions (base_addr, count, NULL);
2505}
2506
2507lldb::SBInstructionList
2508SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2509{
Greg Clayton9c766112012-03-06 22:24:44 +00002510 SBInstructionList sb_instructions;
2511
2512 TargetSP target_sp(GetSP());
2513 if (target_sp)
2514 {
2515 Address *addr_ptr = base_addr.get();
2516
2517 if (addr_ptr)
2518 {
2519 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2520 bool prefer_file_cache = false;
2521 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002522 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2523 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2524 prefer_file_cache,
2525 data.GetBytes(),
2526 data.GetByteSize(),
2527 error,
2528 &load_addr);
2529 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002530 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2531 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002532 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002533 *addr_ptr,
2534 data.GetBytes(),
2535 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002536 count,
2537 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002538 }
2539 }
2540
2541 return sb_instructions;
2542
2543}
2544
2545lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002546SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2547{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002548 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2549}
2550
2551lldb::SBInstructionList
2552SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2553{
Sean Callanan50952e92011-12-14 23:49:37 +00002554 SBInstructionList sb_instructions;
2555
Greg Claytonacdbe812012-01-30 09:04:36 +00002556 TargetSP target_sp(GetSP());
2557 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002558 {
2559 Address addr;
2560
2561 if (base_addr.get())
2562 addr = *base_addr.get();
2563
Greg Clayton3faf47c2013-03-28 23:42:53 +00002564 const bool data_from_file = true;
2565
Greg Claytonacdbe812012-01-30 09:04:36 +00002566 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002567 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002568 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002569 addr,
2570 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002571 size,
2572 UINT32_MAX,
2573 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002574 }
2575
2576 return sb_instructions;
2577}
2578
2579lldb::SBInstructionList
2580SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2581{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002582 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2583}
2584
2585lldb::SBInstructionList
2586SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2587{
2588 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002589}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002590
2591SBError
2592SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2593 lldb::addr_t section_base_addr)
2594{
2595 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002596 TargetSP target_sp(GetSP());
2597 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002598 {
2599 if (!section.IsValid())
2600 {
2601 sb_error.SetErrorStringWithFormat ("invalid section");
2602 }
2603 else
2604 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002605 SectionSP section_sp (section.GetSP());
2606 if (section_sp)
2607 {
2608 if (section_sp->IsThreadSpecific())
2609 {
2610 sb_error.SetErrorString ("thread specific sections are not yet supported");
2611 }
2612 else
2613 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002614 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002615 if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
Greg Clayton3c947372013-01-29 01:17:09 +00002616 {
2617 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002618 if (process_sp)
2619 process_sp->Flush();
2620 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002621 }
2622 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002623 }
2624 }
2625 else
2626 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002627 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002628 }
2629 return sb_error;
2630}
2631
2632SBError
2633SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2634{
2635 SBError sb_error;
2636
Greg Claytonacdbe812012-01-30 09:04:36 +00002637 TargetSP target_sp(GetSP());
2638 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002639 {
2640 if (!section.IsValid())
2641 {
2642 sb_error.SetErrorStringWithFormat ("invalid section");
2643 }
2644 else
2645 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002646 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002647 if (target_sp->SetSectionUnloaded (section.GetSP()))
Greg Clayton3c947372013-01-29 01:17:09 +00002648 {
2649 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002650 if (process_sp)
2651 process_sp->Flush();
2652 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002653 }
2654 }
2655 else
2656 {
2657 sb_error.SetErrorStringWithFormat ("invalid target");
2658 }
2659 return sb_error;
2660}
2661
2662SBError
2663SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2664{
2665 SBError sb_error;
2666
Greg Claytonacdbe812012-01-30 09:04:36 +00002667 TargetSP target_sp(GetSP());
2668 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002669 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002670 ModuleSP module_sp (module.GetSP());
2671 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002672 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002673 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +00002674 if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002675 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002676 // The load was successful, make sure that at least some sections
2677 // changed before we notify that our module was loaded.
2678 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002679 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002680 ModuleList module_list;
2681 module_list.Append(module_sp);
2682 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002683 // Flush info in the process (stack frames, etc)
2684 ProcessSP process_sp (target_sp->GetProcessSP());
2685 if (process_sp)
2686 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002687 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002688 }
2689 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002690 else
2691 {
2692 sb_error.SetErrorStringWithFormat ("invalid module");
2693 }
2694
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002695 }
2696 else
2697 {
2698 sb_error.SetErrorStringWithFormat ("invalid target");
2699 }
2700 return sb_error;
2701}
2702
2703SBError
2704SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2705{
2706 SBError sb_error;
2707
2708 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002709 TargetSP target_sp(GetSP());
2710 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002711 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002712 ModuleSP module_sp (module.GetSP());
2713 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002714 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002715 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002716 if (objfile)
2717 {
2718 SectionList *section_list = objfile->GetSectionList();
2719 if (section_list)
2720 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002721 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002722
Greg Clayton3c947372013-01-29 01:17:09 +00002723 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002724 const size_t num_sections = section_list->GetSize();
2725 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2726 {
2727 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2728 if (section_sp)
Zachary Turner40411162014-07-16 20:28:24 +00002729 changed |= target_sp->SetSectionUnloaded (section_sp);
Greg Clayton3c947372013-01-29 01:17:09 +00002730 }
2731 if (changed)
2732 {
2733 // Flush info in the process (stack frames, etc)
2734 ProcessSP process_sp (target_sp->GetProcessSP());
2735 if (process_sp)
2736 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002737 }
2738 }
2739 else
2740 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002741 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002742 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2743 }
2744 }
2745 else
2746 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002747 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002748 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2749 }
2750 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002751 else
2752 {
2753 sb_error.SetErrorStringWithFormat ("invalid module");
2754 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002755 }
2756 else
2757 {
2758 sb_error.SetErrorStringWithFormat ("invalid target");
2759 }
2760 return sb_error;
2761}
2762
2763
Greg Claytone14e1922012-12-04 02:22:16 +00002764lldb::SBSymbolContextList
2765SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2766{
2767 SBSymbolContextList sb_sc_list;
2768 if (name && name[0])
2769 {
2770 TargetSP target_sp(GetSP());
2771 if (target_sp)
2772 {
2773 bool append = true;
2774 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2775 symbol_type,
2776 *sb_sc_list,
2777 append);
2778 }
2779 }
2780 return sb_sc_list;
2781
2782}
2783
2784
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002785lldb::SBValue
2786SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2787{
Greg Clayton5160ce52013-03-27 23:08:40 +00002788 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2789 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002790 SBValue expr_result;
Jim Ingham8646d3c2014-05-05 02:47:44 +00002791 ExpressionResults exe_results = eExpressionSetupError;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002792 ValueObjectSP expr_value_sp;
2793 TargetSP target_sp(GetSP());
Jason Molendab57e4a12013-11-04 09:33:30 +00002794 StackFrame *frame = NULL;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002795 if (target_sp)
2796 {
2797 if (expr == NULL || expr[0] == '\0')
2798 {
2799 if (log)
2800 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2801 return expr_result;
2802 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002803
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002804 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2805 ExecutionContext exe_ctx (m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002806
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002807 if (log)
2808 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002809
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002810 frame = exe_ctx.GetFramePtr();
2811 Target *target = exe_ctx.GetTargetPtr();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002812
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002813 if (target)
2814 {
2815#ifdef LLDB_CONFIGURATION_DEBUG
2816 StreamString frame_description;
2817 if (frame)
2818 frame->DumpUsingSettingsFormat (&frame_description);
2819 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2820 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2821#endif
2822 exe_results = target->EvaluateExpression (expr,
2823 frame,
2824 expr_value_sp,
2825 options.ref());
2826
2827 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2828#ifdef LLDB_CONFIGURATION_DEBUG
2829 Host::SetCrashDescription (NULL);
2830#endif
2831 }
2832 else
2833 {
2834 if (log)
2835 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2836 }
2837 }
2838#ifndef LLDB_DISABLE_PYTHON
2839 if (expr_log)
2840 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002841 expr_result.GetValue(), expr_result.GetSummary());
2842
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002843 if (log)
2844 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002845 static_cast<void*>(frame), expr,
2846 static_cast<void*>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002847#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002848
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002849 return expr_result;
2850}
2851
Greg Clayton13fbb992013-02-01 00:47:49 +00002852
2853lldb::addr_t
2854SBTarget::GetStackRedZoneSize()
2855{
2856 TargetSP target_sp(GetSP());
2857 if (target_sp)
2858 {
2859 ABISP abi_sp;
2860 ProcessSP process_sp (target_sp->GetProcessSP());
2861 if (process_sp)
2862 abi_sp = process_sp->GetABI();
2863 else
2864 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2865 if (abi_sp)
2866 return abi_sp->GetRedZoneSize();
2867 }
2868 return 0;
2869}
2870