blob: fb88036dd6e4cb4cb6b7e3528737f93914fe8623 [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
Greg Clayton66111032010-06-23 01:19:29 +0000590SBDebugger
591SBTarget::GetDebugger () const
592{
593 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000594 TargetSP target_sp(GetSP());
595 if (target_sp)
596 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000597 return debugger;
598}
599
Jim Ingham270684d2011-03-31 00:01:24 +0000600SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000601SBTarget::LoadCore (const char *core_file)
602{
603 SBProcess sb_process;
604 TargetSP target_sp(GetSP());
605 if (target_sp)
606 {
607 FileSpec filespec(core_file, true);
608 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
609 NULL,
610 &filespec));
611 if (process_sp)
612 {
613 process_sp->LoadCore();
614 sb_process.SetSP (process_sp);
615 }
616 }
617 return sb_process;
618}
619
620SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000621SBTarget::LaunchSimple
622(
623 char const **argv,
624 char const **envp,
625 const char *working_directory
626)
627{
628 char *stdin_path = NULL;
629 char *stdout_path = NULL;
630 char *stderr_path = NULL;
631 uint32_t launch_flags = 0;
632 bool stop_at_entry = false;
633 SBError error;
634 SBListener listener = GetDebugger().GetListener();
635 return Launch (listener,
636 argv,
637 envp,
638 stdin_path,
639 stdout_path,
640 stderr_path,
641 working_directory,
642 launch_flags,
643 stop_at_entry,
644 error);
645}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000646
Greg Claytonfbb76342013-11-20 21:07:01 +0000647SBError
648SBTarget::Install()
649{
650 SBError sb_error;
651 TargetSP target_sp(GetSP());
652 if (target_sp)
653 {
654 Mutex::Locker api_locker (target_sp->GetAPIMutex());
655 sb_error.ref() = target_sp->Install(NULL);
656 }
657 return sb_error;
658}
659
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000660SBProcess
661SBTarget::Launch
662(
Greg Clayton4b045622011-02-03 21:28:34 +0000663 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000664 char const **argv,
665 char const **envp,
666 const char *stdin_path,
667 const char *stdout_path,
668 const char *stderr_path,
669 const char *working_directory,
670 uint32_t launch_flags, // See LaunchFlags
671 bool stop_at_entry,
672 lldb::SBError& error
673)
674{
Greg Clayton5160ce52013-03-27 23:08:40 +0000675 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000676
Greg Claytonacdbe812012-01-30 09:04:36 +0000677 SBProcess sb_process;
678 ProcessSP process_sp;
679 TargetSP target_sp(GetSP());
680
Caroline Ticeceb6b132010-10-26 03:11:13 +0000681 if (log)
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000682 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 +0000683 static_cast<void*>(target_sp.get()),
684 static_cast<void*>(argv), static_cast<void*>(envp),
685 stdin_path ? stdin_path : "NULL",
686 stdout_path ? stdout_path : "NULL",
687 stderr_path ? stderr_path : "NULL",
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000688 working_directory ? working_directory : "NULL",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000689 launch_flags, stop_at_entry,
690 static_cast<void*>(error.get()));
Greg Claytonacdbe812012-01-30 09:04:36 +0000691
692 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000694 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000695
Greg Clayton645bf542011-01-27 01:01:10 +0000696 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
697 launch_flags |= eLaunchFlagDisableASLR;
698
Greg Clayton2289fa42011-04-30 01:09:13 +0000699 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000700 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000701 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000702 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000703 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000704
Greg Claytonb9556ac2012-01-30 07:41:31 +0000705 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000706 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000707 if (state == eStateAttaching)
708 error.SetErrorString ("process attach is in progress");
709 else
710 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000711 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000712 }
Greg Clayton931180e2011-01-27 06:44:37 +0000713 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000714
Greg Clayton2289fa42011-04-30 01:09:13 +0000715 if (state == eStateConnected)
716 {
717 // If we are already connected, then we have already specified the
718 // listener, so if a valid listener is supplied, we need to error out
719 // to let the client know.
720 if (listener.IsValid())
721 {
722 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000723 return sb_process;
724 }
725 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000726
727 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
728 launch_flags |= eLaunchFlagDisableSTDIO;
729
730 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000731
Greg Claytonb09c5382013-12-13 17:20:18 +0000732 Module *exe_module = target_sp->GetExecutableModulePointer();
733 if (exe_module)
734 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
735 if (argv)
736 launch_info.GetArguments().AppendArguments (argv);
737 if (envp)
738 launch_info.GetEnvironmentEntries ().SetArguments (envp);
739
740 if (listener.IsValid())
Greg Claytondc6224e2014-10-21 01:00:42 +0000741 error.SetError (target_sp->Launch(listener.ref(), launch_info, NULL));
Greg Clayton2289fa42011-04-30 01:09:13 +0000742 else
Greg Claytondc6224e2014-10-21 01:00:42 +0000743 error.SetError (target_sp->Launch(target_sp->GetDebugger().GetListener(), launch_info, NULL));
Greg Clayton645bf542011-01-27 01:01:10 +0000744
Greg Claytonb09c5382013-12-13 17:20:18 +0000745 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton524e60b2010-10-06 22:10:17 +0000746 }
747 else
748 {
749 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000751
Caroline Tice20ad3c42010-10-29 21:48:37 +0000752 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000753 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000754 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
755 static_cast<void*>(target_sp.get()),
756 static_cast<void*>(sb_process.GetSP().get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000757
Greg Clayton5d5028b2010-10-06 03:53:16 +0000758 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759}
760
Greg Clayton0e615682012-02-24 05:03:03 +0000761SBProcess
762SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
763{
Greg Clayton5160ce52013-03-27 23:08:40 +0000764 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000765
Greg Clayton0e615682012-02-24 05:03:03 +0000766 SBProcess sb_process;
Greg Clayton0e615682012-02-24 05:03:03 +0000767 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000768
Greg Clayton0e615682012-02-24 05:03:03 +0000769 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000770 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...",
771 static_cast<void*>(target_sp.get()));
772
Greg Clayton0e615682012-02-24 05:03:03 +0000773 if (target_sp)
774 {
775 Mutex::Locker api_locker (target_sp->GetAPIMutex());
776 StateType state = eStateInvalid;
Greg Claytonb09c5382013-12-13 17:20:18 +0000777 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000778 ProcessSP process_sp = target_sp->GetProcessSP();
779 if (process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000780 {
Greg Clayton3e32ad62014-05-07 20:16:06 +0000781 state = process_sp->GetState();
782
783 if (process_sp->IsAlive() && state != eStateConnected)
784 {
785 if (state == eStateAttaching)
786 error.SetErrorString ("process attach is in progress");
787 else
788 error.SetErrorString ("a process is already being debugged");
789 return sb_process;
790 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000791 }
Greg Clayton0e615682012-02-24 05:03:03 +0000792 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000793
794 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
795
Greg Clayton3e32ad62014-05-07 20:16:06 +0000796 if (!launch_info.GetExecutableFile())
797 {
798 Module *exe_module = target_sp->GetExecutableModulePointer();
799 if (exe_module)
800 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
801 }
Greg Claytonb09c5382013-12-13 17:20:18 +0000802
803 const ArchSpec &arch_spec = target_sp->GetArchitecture();
804 if (arch_spec.IsValid())
805 launch_info.GetArchitecture () = arch_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000806
Greg Claytondc6224e2014-10-21 01:00:42 +0000807 error.SetError (target_sp->Launch (target_sp->GetDebugger().GetListener(), launch_info, NULL));
Greg Claytonb09c5382013-12-13 17:20:18 +0000808 sb_process.SetSP(target_sp->GetProcessSP());
Greg Clayton0e615682012-02-24 05:03:03 +0000809 }
810 else
811 {
812 error.SetErrorString ("SBTarget is invalid");
813 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000814
Greg Clayton0e615682012-02-24 05:03:03 +0000815 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
816 if (log)
Greg Claytonb09c5382013-12-13 17:20:18 +0000817 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000818 static_cast<void*>(target_sp.get()),
819 static_cast<void*>(sb_process.GetSP().get()));
820
Greg Clayton0e615682012-02-24 05:03:03 +0000821 return sb_process;
822}
823
824lldb::SBProcess
825SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
826{
Greg Clayton5160ce52013-03-27 23:08:40 +0000827 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000828
Greg Clayton0e615682012-02-24 05:03:03 +0000829 SBProcess sb_process;
830 ProcessSP process_sp;
831 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000832
Sean Callanan575a4542012-10-20 00:21:31 +0000833 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000834 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...",
835 static_cast<void*>(target_sp.get()));
836
Greg Clayton0e615682012-02-24 05:03:03 +0000837 if (target_sp)
838 {
839 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000840
Greg Clayton0e615682012-02-24 05:03:03 +0000841 StateType state = eStateInvalid;
842 process_sp = target_sp->GetProcessSP();
843 if (process_sp)
844 {
845 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000846
Greg Clayton0e615682012-02-24 05:03:03 +0000847 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000848 {
Greg Clayton0e615682012-02-24 05:03:03 +0000849 if (state == eStateAttaching)
850 error.SetErrorString ("process attach is in progress");
851 else
852 error.SetErrorString ("a process is already being debugged");
Sean Callanan575a4542012-10-20 00:21:31 +0000853 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000854 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000855 static_cast<void*>(target_sp.get()),
856 error.GetCString());
Greg Clayton0e615682012-02-24 05:03:03 +0000857 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000858 }
Greg Clayton0e615682012-02-24 05:03:03 +0000859 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000860
Greg Clayton0e615682012-02-24 05:03:03 +0000861 if (state != eStateConnected)
862 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
863
864 if (process_sp)
865 {
Greg Clayton0e615682012-02-24 05:03:03 +0000866 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000867 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000868 {
869 PlatformSP platform_sp = target_sp->GetPlatform();
Greg Clayton91e407e2012-09-27 00:03:39 +0000870 // See if we can pre-verify if a process exists or not
871 if (platform_sp && platform_sp->IsConnected())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000872 {
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000873 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Clayton91e407e2012-09-27 00:03:39 +0000874 ProcessInstanceInfo instance_info;
875 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
876 {
877 attach_info.SetUserID(instance_info.GetEffectiveUserID());
878 }
879 else
880 {
Daniel Malead01b2952012-11-29 21:49:15 +0000881 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000882 if (log)
883 {
884 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000885 static_cast<void*>(target_sp.get()), error.GetCString());
Sean Callanan575a4542012-10-20 00:21:31 +0000886 }
Greg Clayton91e407e2012-09-27 00:03:39 +0000887 return sb_process;
888 }
Han Ming Ongcec8c902012-02-29 00:12:56 +0000889 }
890 }
Han Ming Ongbee98392012-02-29 19:16:40 +0000891 error.SetError (process_sp->Attach (attach_info));
892 if (error.Success())
893 {
894 sb_process.SetSP (process_sp);
895 // If we are doing synchronous mode, then wait for the
896 // process to stop!
897 if (target_sp->GetDebugger().GetAsyncExecution () == false)
898 process_sp->WaitForProcessToStop (NULL);
899 }
Greg Clayton0e615682012-02-24 05:03:03 +0000900 }
901 else
902 {
903 error.SetErrorString ("unable to create lldb_private::Process");
904 }
905 }
906 else
907 {
908 error.SetErrorString ("SBTarget is invalid");
909 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000910
Sean Callanan575a4542012-10-20 00:21:31 +0000911 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +0000912 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000913 static_cast<void*>(target_sp.get()),
914 static_cast<void*>(process_sp.get()));
915
Greg Clayton0e615682012-02-24 05:03:03 +0000916 return sb_process;
917}
918
919
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000920#if defined(__APPLE__)
921
922lldb::SBProcess
923SBTarget::AttachToProcessWithID (SBListener &listener,
924 ::pid_t pid,
925 lldb::SBError& error)
926{
927 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
928}
929
930#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000931
932lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000933SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000934(
Greg Clayton4b045622011-02-03 21:28:34 +0000935 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000936 lldb::pid_t pid,// The process ID to attach to
937 SBError& error // An error explaining what went wrong if attach fails
938)
939{
Greg Clayton5160ce52013-03-27 23:08:40 +0000940 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000941
Greg Clayton524e60b2010-10-06 22:10:17 +0000942 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000943 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000944 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000945
946 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000947 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...",
948 static_cast<void*>(target_sp.get()), pid);
949
Greg Claytonacdbe812012-01-30 09:04:36 +0000950 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000951 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000952 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0c74e782011-06-24 03:21:43 +0000953
954 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000955 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000956 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +0000957 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000958 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000959
Greg Claytonb9556ac2012-01-30 07:41:31 +0000960 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000961 {
Greg Clayton0c74e782011-06-24 03:21:43 +0000962 if (state == eStateAttaching)
963 error.SetErrorString ("process attach is in progress");
964 else
965 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +0000966 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000967 }
Greg Clayton0c74e782011-06-24 03:21:43 +0000968 }
969
970 if (state == eStateConnected)
971 {
972 // If we are already connected, then we have already specified the
973 // listener, so if a valid listener is supplied, we need to error out
974 // to let the client know.
975 if (listener.IsValid())
976 {
977 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +0000978 return sb_process;
979 }
980 }
Greg Clayton4b045622011-02-03 21:28:34 +0000981 else
Greg Clayton0c74e782011-06-24 03:21:43 +0000982 {
983 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000984 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +0000985 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000986 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +0000987 }
Greg Claytonb9556ac2012-01-30 07:41:31 +0000988 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000989 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000990 sb_process.SetSP (process_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000991
Greg Clayton144f3a92011-11-15 03:53:30 +0000992 ProcessAttachInfo attach_info;
993 attach_info.SetProcessID (pid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000994
Han Ming Ong84647042012-02-25 01:07:38 +0000995 PlatformSP platform_sp = target_sp->GetPlatform();
996 ProcessInstanceInfo instance_info;
997 if (platform_sp->GetProcessInfo(pid, instance_info))
998 {
999 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ong84647042012-02-25 01:07:38 +00001000 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001001 error.SetError (process_sp->Attach (attach_info));
Greg Claytone2186ed2012-09-07 17:51:47 +00001002 if (error.Success())
1003 {
1004 // If we are doing synchronous mode, then wait for the
1005 // process to stop!
1006 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytondc6224e2014-10-21 01:00:42 +00001007 process_sp->WaitForProcessToStop (NULL);
Greg Claytone2186ed2012-09-07 17:51:47 +00001008 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001009 }
1010 else
1011 {
1012 error.SetErrorString ("unable to create lldb_private::Process");
1013 }
1014 }
1015 else
1016 {
1017 error.SetErrorString ("SBTarget is invalid");
1018 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001019
Sean Callanan575a4542012-10-20 00:21:31 +00001020 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001021 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001022 static_cast<void*>(target_sp.get()),
1023 static_cast<void*>(process_sp.get()));
Greg Clayton524e60b2010-10-06 22:10:17 +00001024 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001025}
1026
1027lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +00001028SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +00001029(
Greg Clayton4b045622011-02-03 21:28:34 +00001030 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +00001031 const char *name, // basename of process to attach to
1032 bool wait_for, // if true wait for a new instance of "name" to be launched
1033 SBError& error // An error explaining what went wrong if attach fails
1034)
1035{
Greg Clayton5160ce52013-03-27 23:08:40 +00001036 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001037
Greg Clayton524e60b2010-10-06 22:10:17 +00001038 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001039 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001040 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001041
Sean Callanan575a4542012-10-20 00:21:31 +00001042 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001043 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...",
1044 static_cast<void*>(target_sp.get()), name,
1045 wait_for ? "true" : "false");
1046
Greg Claytonacdbe812012-01-30 09:04:36 +00001047 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001048 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001049 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +00001050
Greg Clayton0c74e782011-06-24 03:21:43 +00001051 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +00001052 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +00001053 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +00001054 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001055 state = process_sp->GetState();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001056
Greg Claytonb9556ac2012-01-30 07:41:31 +00001057 if (process_sp->IsAlive() && state != eStateConnected)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001058 {
Greg Clayton0c74e782011-06-24 03:21:43 +00001059 if (state == eStateAttaching)
1060 error.SetErrorString ("process attach is in progress");
1061 else
1062 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001063 return sb_process;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001064 }
Greg Clayton0c74e782011-06-24 03:21:43 +00001065 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001066
Greg Clayton0c74e782011-06-24 03:21:43 +00001067 if (state == eStateConnected)
1068 {
1069 // If we are already connected, then we have already specified the
1070 // listener, so if a valid listener is supplied, we need to error out
1071 // to let the client know.
1072 if (listener.IsValid())
1073 {
1074 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001075 return sb_process;
1076 }
1077 }
Greg Clayton4b045622011-02-03 21:28:34 +00001078 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001079 {
1080 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001081 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001082 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001083 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001084 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001085
Greg Claytonb9556ac2012-01-30 07:41:31 +00001086 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001087 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001088 sb_process.SetSP (process_sp);
Greg Clayton144f3a92011-11-15 03:53:30 +00001089 ProcessAttachInfo attach_info;
1090 attach_info.GetExecutableFile().SetFile(name, false);
1091 attach_info.SetWaitForLaunch(wait_for);
Greg Claytonb9556ac2012-01-30 07:41:31 +00001092 error.SetError (process_sp->Attach (attach_info));
Daniel Malead659dc12013-04-01 19:47:00 +00001093 if (error.Success())
1094 {
1095 // If we are doing synchronous mode, then wait for the
1096 // process to stop!
1097 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1098 process_sp->WaitForProcessToStop (NULL);
1099 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001100 }
1101 else
1102 {
1103 error.SetErrorString ("unable to create lldb_private::Process");
1104 }
1105 }
1106 else
1107 {
1108 error.SetErrorString ("SBTarget is invalid");
1109 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001110
Sean Callanan575a4542012-10-20 00:21:31 +00001111 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001112 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001113 static_cast<void*>(target_sp.get()),
1114 static_cast<void*>(process_sp.get()));
Greg Clayton524e60b2010-10-06 22:10:17 +00001115 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001116}
1117
James McIlree9631aae2011-03-04 00:31:13 +00001118lldb::SBProcess
1119SBTarget::ConnectRemote
1120(
1121 SBListener &listener,
1122 const char *url,
1123 const char *plugin_name,
1124 SBError& error
1125)
1126{
Greg Clayton5160ce52013-03-27 23:08:40 +00001127 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001128
James McIlree9631aae2011-03-04 00:31:13 +00001129 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001130 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001131 TargetSP target_sp(GetSP());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001132
Sean Callanan575a4542012-10-20 00:21:31 +00001133 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001134 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...",
1135 static_cast<void*>(target_sp.get()), url, plugin_name);
1136
Greg Claytonacdbe812012-01-30 09:04:36 +00001137 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001138 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001139 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +00001140 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001141 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001142 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001143 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001144
Greg Claytonb9556ac2012-01-30 07:41:31 +00001145 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001146 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001147 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001148 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +00001149 }
1150 else
1151 {
1152 error.SetErrorString ("unable to create lldb_private::Process");
1153 }
1154 }
1155 else
1156 {
1157 error.SetErrorString ("SBTarget is invalid");
1158 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001159
Sean Callanan575a4542012-10-20 00:21:31 +00001160 if (log)
Sean Callanan575a4542012-10-20 00:21:31 +00001161 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001162 static_cast<void*>(target_sp.get()),
1163 static_cast<void*>(process_sp.get()));
James McIlree9631aae2011-03-04 00:31:13 +00001164 return sb_process;
1165}
1166
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167SBFileSpec
1168SBTarget::GetExecutable ()
1169{
Caroline Ticeceb6b132010-10-26 03:11:13 +00001170
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +00001172 TargetSP target_sp(GetSP());
1173 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001175 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +00001176 if (exe_module)
1177 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001179
Greg Clayton5160ce52013-03-27 23:08:40 +00001180 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001181 if (log)
1182 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001183 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
1184 static_cast<void*>(target_sp.get()),
1185 static_cast<const void*>(exe_file_spec.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001186 }
1187
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 return exe_file_spec;
1189}
1190
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192SBTarget::operator == (const SBTarget &rhs) const
1193{
Greg Clayton66111032010-06-23 01:19:29 +00001194 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195}
1196
1197bool
1198SBTarget::operator != (const SBTarget &rhs) const
1199{
Greg Clayton66111032010-06-23 01:19:29 +00001200 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201}
1202
Greg Claytonb9556ac2012-01-30 07:41:31 +00001203lldb::TargetSP
1204SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +00001205{
1206 return m_opaque_sp;
1207}
1208
Greg Clayton66111032010-06-23 01:19:29 +00001209void
Greg Claytonb9556ac2012-01-30 07:41:31 +00001210SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +00001211{
1212 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213}
1214
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001215lldb::SBAddress
1216SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001217{
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001218 lldb::SBAddress sb_addr;
1219 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +00001220 TargetSP target_sp(GetSP());
1221 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001222 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001223 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytond5944cd2013-12-06 01:12:00 +00001224 if (target_sp->ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001225 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001226 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001227
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001228 // We have a load address that isn't in a section, just return an address
1229 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +00001230 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001231 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001232}
1233
Greg Claytond5944cd2013-12-06 01:12:00 +00001234
1235lldb::SBAddress
1236SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
1237{
1238 lldb::SBAddress sb_addr;
1239 Address &addr = sb_addr.ref();
1240 TargetSP target_sp(GetSP());
1241 if (target_sp)
1242 {
1243 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1244 if (target_sp->ResolveLoadAddress (vm_addr, addr))
1245 return sb_addr;
1246 }
1247
1248 // We have a load address that isn't in a section, just return an address
1249 // with the offset filled in (the address) and the section set to NULL
1250 addr.SetRawAddress(vm_addr);
1251 return sb_addr;
1252}
1253
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001254SBSymbolContext
Greg Claytoneb023e72013-10-11 19:48:25 +00001255SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
1256 uint32_t resolve_scope)
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001257{
1258 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001259 if (addr.IsValid())
1260 {
1261 TargetSP target_sp(GetSP());
1262 if (target_sp)
1263 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1264 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001265 return sc;
1266}
1267
1268
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001270SBTarget::BreakpointCreateByLocation (const char *file,
1271 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272{
Greg Clayton7481c202010-11-08 00:28:40 +00001273 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274}
1275
1276SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001277SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
1278 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279{
Greg Clayton5160ce52013-03-27 23:08:40 +00001280 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001281
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001283 TargetSP target_sp(GetSP());
1284 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001285 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001286 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001287
Greg Clayton1f746072012-08-29 21:13:06 +00001288 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001289 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001290 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001291 const bool hardware = false;
1292 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001293 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001294
1295 if (log)
1296 {
1297 SBStream sstr;
1298 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001299 char path[PATH_MAX];
1300 sb_file_spec->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001301 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1302 static_cast<void*>(target_sp.get()), path, line,
1303 static_cast<void*>(sb_bp.get()), sstr.GetData());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001304 }
1305
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306 return sb_bp;
1307}
1308
1309SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001310SBTarget::BreakpointCreateByName (const char *symbol_name,
1311 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312{
Greg Clayton5160ce52013-03-27 23:08:40 +00001313 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001314
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001316 TargetSP target_sp(GetSP());
1317 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001319 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001320
Jim Inghama8558b62012-05-22 00:12:20 +00001321 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001322 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001323 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324 if (module_name && module_name[0])
1325 {
Jim Ingham969795f2011-09-21 01:17:13 +00001326 FileSpecList module_spec_list;
1327 module_spec_list.Append (FileSpec (module_name, false));
Greg Claytoneb023e72013-10-11 19:48:25 +00001328 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329 }
1330 else
1331 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001332 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333 }
1334 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001335
Caroline Ticeceb6b132010-10-26 03:11:13 +00001336 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001337 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1338 static_cast<void*>(target_sp.get()), symbol_name,
1339 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001340
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341 return sb_bp;
1342}
1343
Jim Ingham87df91b2011-09-23 00:54:11 +00001344lldb::SBBreakpoint
1345SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001346 const SBFileSpecList &module_list,
1347 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001348{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001349 uint32_t name_type_mask = eFunctionNameTypeAuto;
1350 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1351}
1352
1353lldb::SBBreakpoint
1354SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001355 uint32_t name_type_mask,
1356 const SBFileSpecList &module_list,
1357 const SBFileSpecList &comp_unit_list)
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001358{
Greg Clayton5160ce52013-03-27 23:08:40 +00001359 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001360
1361 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001362 TargetSP target_sp(GetSP());
1363 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001364 {
Jim Inghama8558b62012-05-22 00:12:20 +00001365 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001366 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001367 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001368 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1369 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Greg Claytoneb023e72013-10-11 19:48:25 +00001370 comp_unit_list.get(),
1371 symbol_name,
1372 name_type_mask,
1373 skip_prologue,
1374 internal,
1375 hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001376 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001377
Jim Ingham87df91b2011-09-23 00:54:11 +00001378 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001379 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1380 static_cast<void*>(target_sp.get()), symbol_name,
1381 name_type_mask, static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001382
1383 return sb_bp;
1384}
1385
Jim Inghamfab10e82012-03-06 00:37:27 +00001386lldb::SBBreakpoint
1387SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1388 uint32_t num_names,
1389 uint32_t name_type_mask,
1390 const SBFileSpecList &module_list,
1391 const SBFileSpecList &comp_unit_list)
1392{
Greg Clayton5160ce52013-03-27 23:08:40 +00001393 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001394
1395 SBBreakpoint sb_bp;
1396 TargetSP target_sp(GetSP());
1397 if (target_sp && num_names > 0)
1398 {
1399 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001400 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001401 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001402 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001403 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1404 comp_unit_list.get(),
1405 symbol_names,
1406 num_names,
1407 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001408 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001409 internal,
1410 hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001411 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001412
Jim Inghamfab10e82012-03-06 00:37:27 +00001413 if (log)
1414 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001415 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={",
1416 static_cast<void*>(target_sp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001417 for (uint32_t i = 0 ; i < num_names; i++)
1418 {
1419 char sep;
1420 if (i < num_names - 1)
1421 sep = ',';
1422 else
1423 sep = '}';
1424 if (symbol_names[i] != NULL)
1425 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1426 else
1427 log->Printf ("\"<NULL>\"%c ", sep);
Jim Inghamfab10e82012-03-06 00:37:27 +00001428 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001429 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
1430 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001431 }
1432
1433 return sb_bp;
1434}
Jim Ingham87df91b2011-09-23 00:54:11 +00001435
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001437SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1438 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439{
Greg Clayton5160ce52013-03-27 23:08:40 +00001440 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001441
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001443 TargetSP target_sp(GetSP());
1444 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001446 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001448 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001449 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001450 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001451
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452 if (module_name && module_name[0])
1453 {
Jim Ingham969795f2011-09-21 01:17:13 +00001454 FileSpecList module_spec_list;
1455 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001456
Greg Claytoneb023e72013-10-11 19:48:25 +00001457 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 }
1459 else
1460 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001461 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001462 }
1463 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001464
1465 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001466 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1467 static_cast<void*>(target_sp.get()), symbol_name_regex,
1468 module_name, static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001469
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470 return sb_bp;
1471}
1472
Jim Ingham87df91b2011-09-23 00:54:11 +00001473lldb::SBBreakpoint
1474SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001475 const SBFileSpecList &module_list,
1476 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001477{
Greg Clayton5160ce52013-03-27 23:08:40 +00001478 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001479
Jim Ingham87df91b2011-09-23 00:54:11 +00001480 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001481 TargetSP target_sp(GetSP());
1482 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001483 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001484 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001485 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001486 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001487 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001488 const LazyBool skip_prologue = eLazyBoolCalculate;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001489
Greg Claytoneb023e72013-10-11 19:48:25 +00001490 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001491 }
1492
1493 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001494 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1495 static_cast<void*>(target_sp.get()), symbol_name_regex,
1496 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001497
1498 return sb_bp;
1499}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500
1501SBBreakpoint
1502SBTarget::BreakpointCreateByAddress (addr_t address)
1503{
Greg Clayton5160ce52013-03-27 23:08:40 +00001504 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001505
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001507 TargetSP target_sp(GetSP());
1508 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001509 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001510 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001511 const bool hardware = false;
1512 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001513 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001514
Caroline Ticeceb6b132010-10-26 03:11:13 +00001515 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001516 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)",
1517 static_cast<void*>(target_sp.get()),
1518 static_cast<uint64_t>(address),
1519 static_cast<void*>(sb_bp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001520
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521 return sb_bp;
1522}
1523
Jim Ingham969795f2011-09-21 01:17:13 +00001524lldb::SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001525SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1526 const lldb::SBFileSpec &source_file,
1527 const char *module_name)
Jim Ingham969795f2011-09-21 01:17:13 +00001528{
Greg Clayton5160ce52013-03-27 23:08:40 +00001529 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001530
1531 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001532 TargetSP target_sp(GetSP());
1533 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001534 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001535 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001536 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001537 FileSpecList source_file_spec_list;
Greg Claytoneb023e72013-10-11 19:48:25 +00001538 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001539 source_file_spec_list.Append (source_file.ref());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001540
Jim Ingham969795f2011-09-21 01:17:13 +00001541 if (module_name && module_name[0])
1542 {
1543 FileSpecList module_spec_list;
1544 module_spec_list.Append (FileSpec (module_name, false));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001545
Greg Claytoneb023e72013-10-11 19:48:25 +00001546 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001547 }
1548 else
1549 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001550 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001551 }
1552 }
1553
1554 if (log)
1555 {
1556 char path[PATH_MAX];
1557 source_file->GetPath (path, sizeof(path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001558 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1559 static_cast<void*>(target_sp.get()), source_regex, path,
1560 module_name, static_cast<void*>(sb_bp.get()));
Jim Ingham969795f2011-09-21 01:17:13 +00001561 }
1562
1563 return sb_bp;
1564}
1565
Jim Ingham87df91b2011-09-23 00:54:11 +00001566lldb::SBBreakpoint
1567SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001568 const SBFileSpecList &module_list,
1569 const lldb::SBFileSpecList &source_file_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001570{
Greg Clayton5160ce52013-03-27 23:08:40 +00001571 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001572
1573 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001574 TargetSP target_sp(GetSP());
1575 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001576 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001577 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001578 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001579 RegularExpression regexp(source_regex);
Greg Claytoneb023e72013-10-11 19:48:25 +00001580 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001581 }
1582
1583 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001584 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1585 static_cast<void*>(target_sp.get()), source_regex,
1586 static_cast<void*>(sb_bp.get()));
Jim Ingham87df91b2011-09-23 00:54:11 +00001587
1588 return sb_bp;
1589}
Jim Ingham969795f2011-09-21 01:17:13 +00001590
Jim Inghamfab10e82012-03-06 00:37:27 +00001591lldb::SBBreakpoint
1592SBTarget::BreakpointCreateForException (lldb::LanguageType language,
Greg Claytoneb023e72013-10-11 19:48:25 +00001593 bool catch_bp,
1594 bool throw_bp)
Jim Inghamfab10e82012-03-06 00:37:27 +00001595{
Greg Clayton5160ce52013-03-27 23:08:40 +00001596 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001597
1598 SBBreakpoint sb_bp;
1599 TargetSP target_sp(GetSP());
1600 if (target_sp)
1601 {
1602 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001603 const bool hardware = false;
1604 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001605 }
1606
1607 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001608 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1609 static_cast<void*>(target_sp.get()),
Jim Inghamfab10e82012-03-06 00:37:27 +00001610 LanguageRuntime::GetNameForLanguageType(language),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001611 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1612 static_cast<void*>(sb_bp.get()));
Jim Inghamfab10e82012-03-06 00:37:27 +00001613
1614 return sb_bp;
1615}
1616
Greg Clayton9fed0d82010-07-23 23:33:17 +00001617uint32_t
1618SBTarget::GetNumBreakpoints () const
1619{
Greg Claytonacdbe812012-01-30 09:04:36 +00001620 TargetSP target_sp(GetSP());
1621 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001622 {
1623 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001624 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001625 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001626 return 0;
1627}
1628
1629SBBreakpoint
1630SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1631{
1632 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001633 TargetSP target_sp(GetSP());
1634 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001635 {
1636 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001637 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001638 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001639 return sb_breakpoint;
1640}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001641
1642bool
1643SBTarget::BreakpointDelete (break_id_t bp_id)
1644{
Greg Clayton5160ce52013-03-27 23:08:40 +00001645 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001646
Caroline Ticeceb6b132010-10-26 03:11:13 +00001647 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001648 TargetSP target_sp(GetSP());
1649 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001650 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001651 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1652 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001653 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001654
1655 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001656 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1657 static_cast<void*>(target_sp.get()),
1658 static_cast<uint32_t>(bp_id), result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001659
1660 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001661}
1662
Johnny Chen5d043462011-09-26 22:40:50 +00001663SBBreakpoint
1664SBTarget::FindBreakpointByID (break_id_t bp_id)
1665{
Greg Clayton5160ce52013-03-27 23:08:40 +00001666 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001667
1668 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001669 TargetSP target_sp(GetSP());
1670 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001671 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001672 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1673 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001674 }
1675
1676 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001677 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1678 static_cast<void*>(target_sp.get()),
1679 static_cast<uint32_t>(bp_id),
1680 static_cast<void*>(sb_breakpoint.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001681
1682 return sb_breakpoint;
1683}
1684
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685bool
1686SBTarget::EnableAllBreakpoints ()
1687{
Greg Claytonacdbe812012-01-30 09:04:36 +00001688 TargetSP target_sp(GetSP());
1689 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001691 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1692 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001693 return true;
1694 }
1695 return false;
1696}
1697
1698bool
1699SBTarget::DisableAllBreakpoints ()
1700{
Greg Claytonacdbe812012-01-30 09:04:36 +00001701 TargetSP target_sp(GetSP());
1702 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001704 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1705 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001706 return true;
1707 }
1708 return false;
1709}
1710
1711bool
1712SBTarget::DeleteAllBreakpoints ()
1713{
Greg Claytonacdbe812012-01-30 09:04:36 +00001714 TargetSP target_sp(GetSP());
1715 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001716 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001717 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1718 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001719 return true;
1720 }
1721 return false;
1722}
1723
Johnny Chen5d043462011-09-26 22:40:50 +00001724uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001725SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001726{
Greg Claytonacdbe812012-01-30 09:04:36 +00001727 TargetSP target_sp(GetSP());
1728 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001729 {
Johnny Chen01a67862011-10-14 00:42:25 +00001730 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001731 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001732 }
1733 return 0;
1734}
1735
Greg Clayton1b282f92011-10-13 18:08:26 +00001736SBWatchpoint
1737SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001738{
Johnny Chen01a67862011-10-14 00:42:25 +00001739 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001740 TargetSP target_sp(GetSP());
1741 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001742 {
Johnny Chen01a67862011-10-14 00:42:25 +00001743 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001744 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001745 }
Johnny Chen01a67862011-10-14 00:42:25 +00001746 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001747}
1748
1749bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001750SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001751{
Greg Clayton5160ce52013-03-27 23:08:40 +00001752 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001753
1754 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001755 TargetSP target_sp(GetSP());
1756 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001757 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001758 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001759 Mutex::Locker locker;
1760 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001761 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001762 }
1763
1764 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001765 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1766 static_cast<void*>(target_sp.get()),
1767 static_cast<uint32_t>(wp_id), result);
Johnny Chen5d043462011-09-26 22:40:50 +00001768
1769 return result;
1770}
1771
Greg Clayton1b282f92011-10-13 18:08:26 +00001772SBWatchpoint
1773SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001774{
Greg Clayton5160ce52013-03-27 23:08:40 +00001775 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001776
Greg Clayton1b282f92011-10-13 18:08:26 +00001777 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001778 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001779 TargetSP target_sp(GetSP());
1780 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001781 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001782 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001783 Mutex::Locker locker;
1784 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001785 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1786 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001787 }
1788
1789 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001790 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1791 static_cast<void*>(target_sp.get()),
1792 static_cast<uint32_t>(wp_id),
1793 static_cast<void*>(watchpoint_sp.get()));
Johnny Chen5d043462011-09-26 22:40:50 +00001794
Greg Clayton1b282f92011-10-13 18:08:26 +00001795 return sb_watchpoint;
1796}
1797
1798lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001799SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001800{
Greg Clayton5160ce52013-03-27 23:08:40 +00001801 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001802
Greg Clayton1b282f92011-10-13 18:08:26 +00001803 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001804 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001805 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001806 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001807 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001808 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001809 uint32_t watch_type = 0;
1810 if (read)
1811 watch_type |= LLDB_WATCH_TYPE_READ;
1812 if (write)
1813 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001814 if (watch_type == 0)
1815 {
1816 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1817 return sb_watchpoint;
1818 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001819
Johnny Chen7385a5a2012-05-31 22:56:36 +00001820 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001821 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001822 // This API doesn't take in a type, so we can't figure out what it is.
1823 ClangASTType *type = NULL;
1824 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001825 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001826 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001827 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001828
Greg Clayton1b282f92011-10-13 18:08:26 +00001829 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001830 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001831 static_cast<void*>(target_sp.get()), addr,
1832 static_cast<uint32_t>(size),
1833 static_cast<void*>(watchpoint_sp.get()));
1834
Greg Clayton1b282f92011-10-13 18:08:26 +00001835 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001836}
1837
1838bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001839SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001840{
Greg Claytonacdbe812012-01-30 09:04:36 +00001841 TargetSP target_sp(GetSP());
1842 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001843 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001844 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001845 Mutex::Locker locker;
1846 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001847 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001848 return true;
1849 }
1850 return false;
1851}
1852
1853bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001854SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001855{
Greg Claytonacdbe812012-01-30 09:04:36 +00001856 TargetSP target_sp(GetSP());
1857 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001858 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001859 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001860 Mutex::Locker locker;
1861 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001862 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001863 return true;
1864 }
1865 return false;
1866}
1867
Enrico Granata347c2aa2013-10-08 21:49:02 +00001868SBValue
1869SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1870{
1871 SBValue sb_value;
1872 lldb::ValueObjectSP new_value_sp;
1873 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1874 {
1875 lldb::addr_t address(addr.GetLoadAddress(*this));
1876 lldb::TypeImplSP type_impl_sp (type.GetSP());
Enrico Granatadc4db5a2013-10-29 00:28:35 +00001877 ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType(true).GetPointerType ());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001878 if (pointer_ast_type)
1879 {
1880 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001881
Enrico Granata347c2aa2013-10-08 21:49:02 +00001882 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1883 ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1884 pointer_ast_type,
1885 ConstString(name),
1886 buffer,
1887 exe_ctx.GetByteOrder(),
1888 exe_ctx.GetAddressByteSize()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001889
Enrico Granata347c2aa2013-10-08 21:49:02 +00001890 if (ptr_result_valobj_sp)
1891 {
1892 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1893 Error err;
1894 new_value_sp = ptr_result_valobj_sp->Dereference(err);
1895 if (new_value_sp)
1896 new_value_sp->SetName(ConstString(name));
1897 }
1898 }
1899 }
1900 sb_value.SetSP(new_value_sp);
1901 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1902 if (log)
1903 {
1904 if (new_value_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001905 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1906 static_cast<void*>(m_opaque_sp.get()),
1907 new_value_sp->GetName().AsCString());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001908 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001909 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL",
1910 static_cast<void*>(m_opaque_sp.get()));
Enrico Granata347c2aa2013-10-08 21:49:02 +00001911 }
1912 return sb_value;
1913}
1914
Johnny Chen5d043462011-09-26 22:40:50 +00001915bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001916SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001917{
Greg Claytonacdbe812012-01-30 09:04:36 +00001918 TargetSP target_sp(GetSP());
1919 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001920 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001921 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001922 Mutex::Locker locker;
1923 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001924 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001925 return true;
1926 }
1927 return false;
1928}
1929
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001931lldb::SBModule
1932SBTarget::AddModule (const char *path,
1933 const char *triple,
1934 const char *uuid_cstr)
1935{
Greg Claytonb210aec2012-04-23 20:23:39 +00001936 return AddModule (path, triple, uuid_cstr, NULL);
1937}
1938
1939lldb::SBModule
1940SBTarget::AddModule (const char *path,
1941 const char *triple,
1942 const char *uuid_cstr,
1943 const char *symfile)
1944{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001945 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001946 TargetSP target_sp(GetSP());
1947 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001948 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001949 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001950 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001951 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001952
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001953 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001954 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001955
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001956 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001957 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00001958 else
1959 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00001960
1961 if (symfile)
1962 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001963
Greg Claytonb9a01b32012-02-26 05:51:37 +00001964 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001965 }
1966 return sb_module;
1967}
1968
Greg Clayton226cce22013-07-08 22:22:41 +00001969lldb::SBModule
1970SBTarget::AddModule (const SBModuleSpec &module_spec)
1971{
1972 lldb::SBModule sb_module;
1973 TargetSP target_sp(GetSP());
1974 if (target_sp)
1975 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1976 return sb_module;
1977}
1978
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001979bool
1980SBTarget::AddModule (lldb::SBModule &module)
1981{
Greg Claytonacdbe812012-01-30 09:04:36 +00001982 TargetSP target_sp(GetSP());
1983 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001984 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001985 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001986 return true;
1987 }
1988 return false;
1989}
1990
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001991uint32_t
1992SBTarget::GetNumModules () const
1993{
Greg Clayton5160ce52013-03-27 23:08:40 +00001994 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001995
Caroline Ticeceb6b132010-10-26 03:11:13 +00001996 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00001997 TargetSP target_sp(GetSP());
1998 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001999 {
2000 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002001 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00002002 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002003
2004 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002005 log->Printf ("SBTarget(%p)::GetNumModules () => %d",
2006 static_cast<void*>(target_sp.get()), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002007
2008 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002009}
2010
Greg Clayton48e42542010-07-30 20:12:55 +00002011void
2012SBTarget::Clear ()
2013{
Greg Clayton5160ce52013-03-27 23:08:40 +00002014 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002015
2016 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002017 log->Printf ("SBTarget(%p)::Clear ()",
2018 static_cast<void*>(m_opaque_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002019
Greg Clayton48e42542010-07-30 20:12:55 +00002020 m_opaque_sp.reset();
2021}
2022
2023
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002024SBModule
2025SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2026{
2027 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002028 TargetSP target_sp(GetSP());
2029 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00002030 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002031 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002032 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00002033 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00002034 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035 return sb_module;
2036}
2037
Greg Clayton13d19502012-01-29 06:07:39 +00002038lldb::ByteOrder
2039SBTarget::GetByteOrder ()
2040{
Greg Claytonacdbe812012-01-30 09:04:36 +00002041 TargetSP target_sp(GetSP());
2042 if (target_sp)
2043 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00002044 return eByteOrderInvalid;
2045}
2046
2047const char *
2048SBTarget::GetTriple ()
2049{
Greg Claytonacdbe812012-01-30 09:04:36 +00002050 TargetSP target_sp(GetSP());
2051 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00002052 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002053 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00002054 // Unique the string so we don't run into ownership issues since
2055 // the const strings put the string into the string pool once and
2056 // the strings never comes out
2057 ConstString const_triple (triple.c_str());
2058 return const_triple.GetCString();
2059 }
2060 return NULL;
2061}
2062
2063uint32_t
2064SBTarget::GetAddressByteSize()
2065{
Greg Claytonacdbe812012-01-30 09:04:36 +00002066 TargetSP target_sp(GetSP());
2067 if (target_sp)
2068 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00002069 return sizeof(void*);
2070}
2071
2072
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073SBModule
2074SBTarget::GetModuleAtIndex (uint32_t idx)
2075{
Greg Clayton5160ce52013-03-27 23:08:40 +00002076 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002077
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002078 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002079 ModuleSP module_sp;
2080 TargetSP target_sp(GetSP());
2081 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002082 {
2083 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002084 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2085 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002086 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002087
2088 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002089 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2090 static_cast<void*>(target_sp.get()), idx,
2091 static_cast<void*>(module_sp.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002092
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002093 return sb_module;
2094}
2095
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002096bool
2097SBTarget::RemoveModule (lldb::SBModule module)
2098{
Greg Claytonacdbe812012-01-30 09:04:36 +00002099 TargetSP target_sp(GetSP());
2100 if (target_sp)
2101 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002102 return false;
2103}
2104
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002105
2106SBBroadcaster
2107SBTarget::GetBroadcaster () const
2108{
Greg Clayton5160ce52013-03-27 23:08:40 +00002109 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002110
Greg Claytonacdbe812012-01-30 09:04:36 +00002111 TargetSP target_sp(GetSP());
2112 SBBroadcaster broadcaster(target_sp.get(), false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002113
Caroline Ticeceb6b132010-10-26 03:11:13 +00002114 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002115 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2116 static_cast<void*>(target_sp.get()),
2117 static_cast<void*>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002118
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002119 return broadcaster;
2120}
2121
Caroline Ticedde9cff2010-09-20 05:20:02 +00002122bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002123SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002124{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002125 Stream &strm = description.ref();
2126
Greg Claytonacdbe812012-01-30 09:04:36 +00002127 TargetSP target_sp(GetSP());
2128 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002129 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002130 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002131 }
2132 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002133 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002134
2135 return true;
2136}
2137
Greg Clayton5569e642012-02-06 01:44:54 +00002138lldb::SBSymbolContextList
2139SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002140{
Greg Clayton5569e642012-02-06 01:44:54 +00002141 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002142 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002143 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002144 TargetSP target_sp(GetSP());
2145 if (target_sp)
2146 {
2147 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002148 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002149 const bool append = true;
2150 target_sp->GetImages().FindFunctions (ConstString(name),
2151 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002152 symbols_ok,
2153 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002154 append,
2155 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002156 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002157 }
Greg Clayton5569e642012-02-06 01:44:54 +00002158 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002159}
2160
Carlo Kok0fd6fd42014-09-19 19:38:19 +00002161lldb::SBSymbolContextList
2162SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype)
2163{
2164 lldb::SBSymbolContextList sb_sc_list;
2165 if (name && name[0])
2166 {
2167 TargetSP target_sp(GetSP());
2168 if (target_sp)
2169 {
2170 std::string regexstr;
2171 switch (matchtype)
2172 {
2173 case eMatchTypeRegex:
2174 target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list);
2175 break;
2176 case eMatchTypeStartsWith:
2177 regexstr = llvm::Regex::escape(name) + ".*";
2178 target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list);
2179 break;
2180 default:
2181 target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list);
2182 break;
2183 }
2184 }
2185 }
2186 return sb_sc_list;
2187}
2188
Enrico Granata6f3533f2011-07-29 19:53:35 +00002189lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002190SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002191{
Greg Claytonacdbe812012-01-30 09:04:36 +00002192 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002193 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002194 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002195 ConstString const_typename(typename_cstr);
2196 SymbolContext sc;
2197 const bool exact_match = false;
2198
2199 const ModuleList &module_list = target_sp->GetImages();
2200 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002201 for (size_t idx = 0; idx < count; idx++)
2202 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002203 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2204 if (module_sp)
2205 {
2206 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2207 if (type_sp)
2208 return SBType(type_sp);
2209 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002210 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002211
2212 // Didn't find the type in the symbols; try the Objective-C runtime
2213 // if one is installed
2214
2215 ProcessSP process_sp(target_sp->GetProcessSP());
2216
2217 if (process_sp)
2218 {
2219 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2220
2221 if (objc_language_runtime)
2222 {
2223 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2224
2225 if (objc_type_vendor)
2226 {
2227 std::vector <ClangASTType> types;
2228
2229 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2230 return SBType(types[0]);
2231 }
2232 }
2233 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002234
2235 // No matches, search for basic typename matches
2236 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2237 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002238 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002239 }
2240 return SBType();
2241}
2242
Greg Claytonb43165b2012-12-05 21:24:42 +00002243SBType
2244SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002245{
Greg Claytonacdbe812012-01-30 09:04:36 +00002246 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002247 if (target_sp)
2248 {
2249 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2250 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002251 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00002252 }
2253 return SBType();
2254}
2255
2256
2257lldb::SBTypeList
2258SBTarget::FindTypes (const char* typename_cstr)
2259{
2260 SBTypeList sb_type_list;
2261 TargetSP target_sp(GetSP());
2262 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002263 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002264 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002265 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002266 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002267 SymbolContext sc;
2268 TypeList type_list;
2269
Greg Clayton29399a22012-04-06 17:41:13 +00002270 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002271 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002272 exact_match,
2273 UINT32_MAX,
2274 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002275
Greg Claytonb43165b2012-12-05 21:24:42 +00002276 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002277 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002278 for (size_t idx = 0; idx < num_matches; idx++)
2279 {
2280 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2281 if (type_sp)
2282 sb_type_list.Append(SBType(type_sp));
2283 }
2284 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002285
2286 // Try the Objective-C runtime if one is installed
2287
2288 ProcessSP process_sp(target_sp->GetProcessSP());
2289
2290 if (process_sp)
2291 {
2292 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2293
2294 if (objc_language_runtime)
2295 {
2296 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2297
2298 if (objc_type_vendor)
2299 {
2300 std::vector <ClangASTType> types;
2301
2302 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2303 {
2304 for (ClangASTType &type : types)
2305 {
2306 sb_type_list.Append(SBType(type));
2307 }
2308 }
2309 }
2310 }
2311 }
2312
2313 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002314 {
2315 // No matches, search for basic typename matches
2316 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2317 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002318 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002319 }
2320 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002321 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002322}
2323
Greg Claytondea8cb42011-06-29 22:09:02 +00002324SBValueList
2325SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2326{
2327 SBValueList sb_value_list;
2328
Greg Claytonacdbe812012-01-30 09:04:36 +00002329 TargetSP target_sp(GetSP());
2330 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002331 {
2332 VariableList variable_list;
2333 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002334 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2335 append,
2336 max_matches,
2337 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002338
2339 if (match_count > 0)
2340 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002341 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002342 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002343 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002344 for (uint32_t i=0; i<match_count; ++i)
2345 {
2346 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2347 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002348 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002349 }
2350 }
2351 }
2352
2353 return sb_value_list;
2354}
2355
Carlo Kok0fd6fd42014-09-19 19:38:19 +00002356SBValueList
2357SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype)
2358{
2359 SBValueList sb_value_list;
2360
2361 TargetSP target_sp(GetSP());
2362 if (name && target_sp)
2363 {
2364 VariableList variable_list;
2365 const bool append = true;
2366
2367 std::string regexstr;
2368 uint32_t match_count;
2369 switch (matchtype)
2370 {
2371 case eMatchTypeNormal:
2372 match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name),
2373 append,
2374 max_matches,
2375 variable_list);
2376 break;
2377 case eMatchTypeRegex:
2378 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name),
2379 append,
2380 max_matches,
2381 variable_list);
2382 break;
2383 case eMatchTypeStartsWith:
2384 regexstr = llvm::Regex::escape(name) + ".*";
2385 match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()),
2386 append,
2387 max_matches,
2388 variable_list);
2389 break;
2390 }
2391
2392
2393 if (match_count > 0)
2394 {
2395 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2396 if (exe_scope == NULL)
2397 exe_scope = target_sp.get();
2398 for (uint32_t i = 0; i<match_count; ++i)
2399 {
2400 lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(exe_scope, variable_list.GetVariableAtIndex(i)));
2401 if (valobj_sp)
2402 sb_value_list.Append(SBValue(valobj_sp));
2403 }
2404 }
2405 }
2406
2407 return sb_value_list;
2408}
2409
2410
Enrico Granatabcd80b42013-01-16 18:53:52 +00002411lldb::SBValue
2412SBTarget::FindFirstGlobalVariable (const char* name)
2413{
2414 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2415 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2416 return sb_value_list.GetValueAtIndex(0);
2417 return SBValue();
2418}
2419
Jim Inghame37d6052011-09-13 00:29:56 +00002420SBSourceManager
2421SBTarget::GetSourceManager()
2422{
2423 SBSourceManager source_manager (*this);
2424 return source_manager;
2425}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002426
Sean Callanan50952e92011-12-14 23:49:37 +00002427lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002428SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2429{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002430 return ReadInstructions (base_addr, count, NULL);
2431}
2432
2433lldb::SBInstructionList
2434SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2435{
Greg Clayton9c766112012-03-06 22:24:44 +00002436 SBInstructionList sb_instructions;
2437
2438 TargetSP target_sp(GetSP());
2439 if (target_sp)
2440 {
2441 Address *addr_ptr = base_addr.get();
2442
2443 if (addr_ptr)
2444 {
2445 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2446 bool prefer_file_cache = false;
2447 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002448 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2449 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2450 prefer_file_cache,
2451 data.GetBytes(),
2452 data.GetByteSize(),
2453 error,
2454 &load_addr);
2455 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002456 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2457 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002458 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002459 *addr_ptr,
2460 data.GetBytes(),
2461 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002462 count,
2463 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002464 }
2465 }
2466
2467 return sb_instructions;
2468
2469}
2470
2471lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002472SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2473{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002474 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2475}
2476
2477lldb::SBInstructionList
2478SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2479{
Sean Callanan50952e92011-12-14 23:49:37 +00002480 SBInstructionList sb_instructions;
2481
Greg Claytonacdbe812012-01-30 09:04:36 +00002482 TargetSP target_sp(GetSP());
2483 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002484 {
2485 Address addr;
2486
2487 if (base_addr.get())
2488 addr = *base_addr.get();
2489
Greg Clayton3faf47c2013-03-28 23:42:53 +00002490 const bool data_from_file = true;
2491
Greg Claytonacdbe812012-01-30 09:04:36 +00002492 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002493 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002494 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002495 addr,
2496 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002497 size,
2498 UINT32_MAX,
2499 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002500 }
2501
2502 return sb_instructions;
2503}
2504
2505lldb::SBInstructionList
2506SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2507{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002508 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2509}
2510
2511lldb::SBInstructionList
2512SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2513{
2514 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002515}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002516
2517SBError
2518SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2519 lldb::addr_t section_base_addr)
2520{
2521 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002522 TargetSP target_sp(GetSP());
2523 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002524 {
2525 if (!section.IsValid())
2526 {
2527 sb_error.SetErrorStringWithFormat ("invalid section");
2528 }
2529 else
2530 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002531 SectionSP section_sp (section.GetSP());
2532 if (section_sp)
2533 {
2534 if (section_sp->IsThreadSpecific())
2535 {
2536 sb_error.SetErrorString ("thread specific sections are not yet supported");
2537 }
2538 else
2539 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002540 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002541 if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
Greg Clayton3c947372013-01-29 01:17:09 +00002542 {
2543 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002544 if (process_sp)
2545 process_sp->Flush();
2546 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002547 }
2548 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002549 }
2550 }
2551 else
2552 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002553 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002554 }
2555 return sb_error;
2556}
2557
2558SBError
2559SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2560{
2561 SBError sb_error;
2562
Greg Claytonacdbe812012-01-30 09:04:36 +00002563 TargetSP target_sp(GetSP());
2564 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002565 {
2566 if (!section.IsValid())
2567 {
2568 sb_error.SetErrorStringWithFormat ("invalid section");
2569 }
2570 else
2571 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002572 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002573 if (target_sp->SetSectionUnloaded (section.GetSP()))
Greg Clayton3c947372013-01-29 01:17:09 +00002574 {
2575 // Flush info in the process (stack frames, etc)
Greg Clayton3c947372013-01-29 01:17:09 +00002576 if (process_sp)
2577 process_sp->Flush();
2578 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002579 }
2580 }
2581 else
2582 {
2583 sb_error.SetErrorStringWithFormat ("invalid target");
2584 }
2585 return sb_error;
2586}
2587
2588SBError
2589SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2590{
2591 SBError sb_error;
2592
Greg Claytonacdbe812012-01-30 09:04:36 +00002593 TargetSP target_sp(GetSP());
2594 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002595 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002596 ModuleSP module_sp (module.GetSP());
2597 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002598 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002599 bool changed = false;
Greg Clayton751caf62014-02-07 22:54:47 +00002600 if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002601 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002602 // The load was successful, make sure that at least some sections
2603 // changed before we notify that our module was loaded.
2604 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002605 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002606 ModuleList module_list;
2607 module_list.Append(module_sp);
2608 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002609 // Flush info in the process (stack frames, etc)
2610 ProcessSP process_sp (target_sp->GetProcessSP());
2611 if (process_sp)
2612 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002613 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002614 }
2615 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002616 else
2617 {
2618 sb_error.SetErrorStringWithFormat ("invalid module");
2619 }
2620
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002621 }
2622 else
2623 {
2624 sb_error.SetErrorStringWithFormat ("invalid target");
2625 }
2626 return sb_error;
2627}
2628
2629SBError
2630SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2631{
2632 SBError sb_error;
2633
2634 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002635 TargetSP target_sp(GetSP());
2636 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002637 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002638 ModuleSP module_sp (module.GetSP());
2639 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002640 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002641 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002642 if (objfile)
2643 {
2644 SectionList *section_list = objfile->GetSectionList();
2645 if (section_list)
2646 {
Greg Claytond5944cd2013-12-06 01:12:00 +00002647 ProcessSP process_sp (target_sp->GetProcessSP());
Greg Claytond5944cd2013-12-06 01:12:00 +00002648
Greg Clayton3c947372013-01-29 01:17:09 +00002649 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002650 const size_t num_sections = section_list->GetSize();
2651 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2652 {
2653 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2654 if (section_sp)
Zachary Turner40411162014-07-16 20:28:24 +00002655 changed |= target_sp->SetSectionUnloaded (section_sp);
Greg Clayton3c947372013-01-29 01:17:09 +00002656 }
2657 if (changed)
2658 {
2659 // Flush info in the process (stack frames, etc)
2660 ProcessSP process_sp (target_sp->GetProcessSP());
2661 if (process_sp)
2662 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002663 }
2664 }
2665 else
2666 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002667 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002668 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2669 }
2670 }
2671 else
2672 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002673 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002674 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2675 }
2676 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002677 else
2678 {
2679 sb_error.SetErrorStringWithFormat ("invalid module");
2680 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002681 }
2682 else
2683 {
2684 sb_error.SetErrorStringWithFormat ("invalid target");
2685 }
2686 return sb_error;
2687}
2688
2689
Greg Claytone14e1922012-12-04 02:22:16 +00002690lldb::SBSymbolContextList
2691SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2692{
2693 SBSymbolContextList sb_sc_list;
2694 if (name && name[0])
2695 {
2696 TargetSP target_sp(GetSP());
2697 if (target_sp)
2698 {
2699 bool append = true;
2700 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2701 symbol_type,
2702 *sb_sc_list,
2703 append);
2704 }
2705 }
2706 return sb_sc_list;
2707
2708}
2709
2710
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002711lldb::SBValue
2712SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2713{
Greg Clayton5160ce52013-03-27 23:08:40 +00002714 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2715 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002716 SBValue expr_result;
Jim Ingham8646d3c2014-05-05 02:47:44 +00002717 ExpressionResults exe_results = eExpressionSetupError;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002718 ValueObjectSP expr_value_sp;
2719 TargetSP target_sp(GetSP());
Jason Molendab57e4a12013-11-04 09:33:30 +00002720 StackFrame *frame = NULL;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002721 if (target_sp)
2722 {
2723 if (expr == NULL || expr[0] == '\0')
2724 {
2725 if (log)
2726 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2727 return expr_result;
2728 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002729
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002730 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2731 ExecutionContext exe_ctx (m_opaque_sp.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002732
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002733 if (log)
2734 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002735
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002736 frame = exe_ctx.GetFramePtr();
2737 Target *target = exe_ctx.GetTargetPtr();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002738
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002739 if (target)
2740 {
2741#ifdef LLDB_CONFIGURATION_DEBUG
2742 StreamString frame_description;
2743 if (frame)
2744 frame->DumpUsingSettingsFormat (&frame_description);
2745 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2746 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2747#endif
2748 exe_results = target->EvaluateExpression (expr,
2749 frame,
2750 expr_value_sp,
2751 options.ref());
2752
2753 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2754#ifdef LLDB_CONFIGURATION_DEBUG
2755 Host::SetCrashDescription (NULL);
2756#endif
2757 }
2758 else
2759 {
2760 if (log)
2761 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2762 }
2763 }
2764#ifndef LLDB_DISABLE_PYTHON
2765 if (expr_log)
2766 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002767 expr_result.GetValue(), expr_result.GetSummary());
2768
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002769 if (log)
2770 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002771 static_cast<void*>(frame), expr,
2772 static_cast<void*>(expr_value_sp.get()), exe_results);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002773#endif
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002774
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002775 return expr_result;
2776}
2777
Greg Clayton13fbb992013-02-01 00:47:49 +00002778
2779lldb::addr_t
2780SBTarget::GetStackRedZoneSize()
2781{
2782 TargetSP target_sp(GetSP());
2783 if (target_sp)
2784 {
2785 ABISP abi_sp;
2786 ProcessSP process_sp (target_sp->GetProcessSP());
2787 if (process_sp)
2788 abi_sp = process_sp->GetABI();
2789 else
2790 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2791 if (abi_sp)
2792 return abi_sp->GetRedZoneSize();
2793 }
2794 return 0;
2795}
2796