blob: 8687cad69bf4ee48d2f6cfa8b506c06911c0f898 [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"
Jim Inghame37d6052011-09-13 00:29:56 +000022#include "lldb/API/SBSourceManager.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000023#include "lldb/API/SBProcess.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000024#include "lldb/API/SBStream.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000025#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Breakpoint/BreakpointID.h"
27#include "lldb/Breakpoint/BreakpointIDList.h"
28#include "lldb/Breakpoint/BreakpointList.h"
29#include "lldb/Breakpoint/BreakpointLocation.h"
30#include "lldb/Core/Address.h"
31#include "lldb/Core/AddressResolver.h"
32#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/ArchSpec.h"
34#include "lldb/Core/Debugger.h"
35#include "lldb/Core/Disassembler.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000036#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037#include "lldb/Core/Module.h"
38#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Core/RegularExpression.h"
40#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Core/STLUtils.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000043#include "lldb/Core/ValueObjectList.h"
44#include "lldb/Core/ValueObjectVariable.h"
45#include "lldb/Host/FileSpec.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000046#include "lldb/Host/Host.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000047#include "lldb/Interpreter/Args.h"
Greg Clayton1f746072012-08-29 21:13:06 +000048#include "lldb/Symbol/ObjectFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000049#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000050#include "lldb/Symbol/VariableList.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000051#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/Process.h"
53#include "lldb/Target/Target.h"
54#include "lldb/Target/TargetList.h"
55
56#include "lldb/Interpreter/CommandReturnObject.h"
57#include "../source/Commands/CommandObjectBreakpoint.h"
58
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
60using namespace lldb;
61using namespace lldb_private;
62
63#define DEFAULT_DISASM_BYTE_SIZE 32
64
Greg Clayton38d1f052012-02-24 20:59:25 +000065SBLaunchInfo::SBLaunchInfo (const char **argv) :
66 m_opaque_sp(new ProcessLaunchInfo())
Greg Clayton0e615682012-02-24 05:03:03 +000067{
Greg Clayton38d1f052012-02-24 20:59:25 +000068 m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
69 if (argv && argv[0])
70 m_opaque_sp->GetArguments().SetArguments(argv);
Greg Clayton0e615682012-02-24 05:03:03 +000071}
72
Greg Claytonecc7c0d2012-03-07 23:52:51 +000073SBLaunchInfo::~SBLaunchInfo()
74{
75}
76
77lldb_private::ProcessLaunchInfo &
78SBLaunchInfo::ref ()
79{
80 return *m_opaque_sp;
81}
82
83
Greg Clayton0e615682012-02-24 05:03:03 +000084uint32_t
85SBLaunchInfo::GetUserID()
86{
87 return m_opaque_sp->GetUserID();
88}
89
90uint32_t
91SBLaunchInfo::GetGroupID()
92{
93 return m_opaque_sp->GetGroupID();
94}
95
96bool
97SBLaunchInfo::UserIDIsValid ()
98{
99 return m_opaque_sp->UserIDIsValid();
100}
101
102bool
103SBLaunchInfo::GroupIDIsValid ()
104{
105 return m_opaque_sp->GroupIDIsValid();
106}
107
108void
109SBLaunchInfo::SetUserID (uint32_t uid)
110{
111 m_opaque_sp->SetUserID (uid);
112}
113
114void
115SBLaunchInfo::SetGroupID (uint32_t gid)
116{
117 m_opaque_sp->SetGroupID (gid);
118}
119
Greg Clayton0e615682012-02-24 05:03:03 +0000120uint32_t
121SBLaunchInfo::GetNumArguments ()
122{
123 return m_opaque_sp->GetArguments().GetArgumentCount();
124}
125
126const char *
127SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
128{
129 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
130}
131
132void
133SBLaunchInfo::SetArguments (const char **argv, bool append)
134{
135 if (append)
136 {
137 if (argv)
138 m_opaque_sp->GetArguments().AppendArguments(argv);
139 }
140 else
141 {
142 if (argv)
143 m_opaque_sp->GetArguments().SetArguments(argv);
144 else
145 m_opaque_sp->GetArguments().Clear();
146 }
147}
148
149uint32_t
150SBLaunchInfo::GetNumEnvironmentEntries ()
151{
152 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
153}
154
155const char *
156SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
157{
158 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
159}
160
161void
162SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
163{
164 if (append)
165 {
166 if (envp)
167 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
168 }
169 else
170 {
171 if (envp)
172 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
173 else
174 m_opaque_sp->GetEnvironmentEntries().Clear();
175 }
176}
177
178void
179SBLaunchInfo::Clear ()
180{
181 m_opaque_sp->Clear();
182}
183
184const char *
185SBLaunchInfo::GetWorkingDirectory () const
186{
187 return m_opaque_sp->GetWorkingDirectory();
188}
189
190void
191SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
192{
193 m_opaque_sp->SetWorkingDirectory(working_dir);
194}
195
196uint32_t
197SBLaunchInfo::GetLaunchFlags ()
198{
199 return m_opaque_sp->GetFlags().Get();
200}
201
202void
203SBLaunchInfo::SetLaunchFlags (uint32_t flags)
204{
205 m_opaque_sp->GetFlags().Reset(flags);
206}
207
208const char *
209SBLaunchInfo::GetProcessPluginName ()
210{
211 return m_opaque_sp->GetProcessPluginName();
212}
213
214void
215SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
216{
217 return m_opaque_sp->SetProcessPluginName (plugin_name);
218}
219
220const char *
221SBLaunchInfo::GetShell ()
222{
223 return m_opaque_sp->GetShell();
224}
225
226void
227SBLaunchInfo::SetShell (const char * path)
228{
229 m_opaque_sp->SetShell (path);
230}
231
232uint32_t
233SBLaunchInfo::GetResumeCount ()
234{
235 return m_opaque_sp->GetResumeCount();
236}
237
238void
239SBLaunchInfo::SetResumeCount (uint32_t c)
240{
241 m_opaque_sp->SetResumeCount (c);
242}
243
244bool
245SBLaunchInfo::AddCloseFileAction (int fd)
246{
247 return m_opaque_sp->AppendCloseFileAction(fd);
248}
249
250bool
251SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
252{
253 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
254}
255
256bool
257SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
258{
259 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
260}
261
262bool
263SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
264{
265 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
266}
267
268
269SBAttachInfo::SBAttachInfo () :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000270 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000271{
272}
273
274SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000275 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000276{
277 m_opaque_sp->SetProcessID (pid);
278}
279
280SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000281 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000282{
283 if (path && path[0])
284 m_opaque_sp->GetExecutableFile().SetFile(path, false);
285 m_opaque_sp->SetWaitForLaunch (wait_for);
286}
287
288SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000289 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000290{
291 *m_opaque_sp = *rhs.m_opaque_sp;
292}
293
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000294SBAttachInfo::~SBAttachInfo()
295{
296}
297
298lldb_private::ProcessAttachInfo &
299SBAttachInfo::ref ()
300{
301 return *m_opaque_sp;
302}
303
Greg Clayton0e615682012-02-24 05:03:03 +0000304SBAttachInfo &
305SBAttachInfo::operator = (const SBAttachInfo &rhs)
306{
307 if (this != &rhs)
308 *m_opaque_sp = *rhs.m_opaque_sp;
309 return *this;
310}
311
312lldb::pid_t
313SBAttachInfo::GetProcessID ()
314{
315 return m_opaque_sp->GetProcessID();
316}
317
318void
319SBAttachInfo::SetProcessID (lldb::pid_t pid)
320{
321 m_opaque_sp->SetProcessID (pid);
322}
323
324
325uint32_t
326SBAttachInfo::GetResumeCount ()
327{
328 return m_opaque_sp->GetResumeCount();
329}
330
331void
332SBAttachInfo::SetResumeCount (uint32_t c)
333{
334 m_opaque_sp->SetResumeCount (c);
335}
336
337const char *
338SBAttachInfo::GetProcessPluginName ()
339{
340 return m_opaque_sp->GetProcessPluginName();
341}
342
343void
344SBAttachInfo::SetProcessPluginName (const char *plugin_name)
345{
346 return m_opaque_sp->SetProcessPluginName (plugin_name);
347}
348
349void
350SBAttachInfo::SetExecutable (const char *path)
351{
352 if (path && path[0])
353 m_opaque_sp->GetExecutableFile().SetFile(path, false);
354 else
355 m_opaque_sp->GetExecutableFile().Clear();
356}
357
358void
359SBAttachInfo::SetExecutable (SBFileSpec exe_file)
360{
361 if (exe_file.IsValid())
362 m_opaque_sp->GetExecutableFile() = exe_file.ref();
363 else
364 m_opaque_sp->GetExecutableFile().Clear();
365}
366
367bool
368SBAttachInfo::GetWaitForLaunch ()
369{
370 return m_opaque_sp->GetWaitForLaunch();
371}
372
373void
374SBAttachInfo::SetWaitForLaunch (bool b)
375{
376 m_opaque_sp->SetWaitForLaunch (b);
377}
378
Jim Inghamcd16df92012-07-20 21:37:13 +0000379bool
380SBAttachInfo::GetIgnoreExisting ()
381{
382 return m_opaque_sp->GetIgnoreExisting();
383}
384
385void
386SBAttachInfo::SetIgnoreExisting (bool b)
387{
388 m_opaque_sp->SetIgnoreExisting (b);
389}
390
Greg Clayton0e615682012-02-24 05:03:03 +0000391uint32_t
Greg Clayton41bd8ac2012-02-24 23:56:06 +0000392SBAttachInfo::GetUserID()
393{
394 return m_opaque_sp->GetUserID();
395}
396
397uint32_t
398SBAttachInfo::GetGroupID()
399{
400 return m_opaque_sp->GetGroupID();
401}
402
403bool
404SBAttachInfo::UserIDIsValid ()
405{
406 return m_opaque_sp->UserIDIsValid();
407}
408
409bool
410SBAttachInfo::GroupIDIsValid ()
411{
412 return m_opaque_sp->GroupIDIsValid();
413}
414
415void
416SBAttachInfo::SetUserID (uint32_t uid)
417{
418 m_opaque_sp->SetUserID (uid);
419}
420
421void
422SBAttachInfo::SetGroupID (uint32_t gid)
423{
424 m_opaque_sp->SetGroupID (gid);
425}
426
427uint32_t
Greg Clayton0e615682012-02-24 05:03:03 +0000428SBAttachInfo::GetEffectiveUserID()
429{
430 return m_opaque_sp->GetEffectiveUserID();
431}
432
433uint32_t
434SBAttachInfo::GetEffectiveGroupID()
435{
436 return m_opaque_sp->GetEffectiveGroupID();
437}
438
439bool
440SBAttachInfo::EffectiveUserIDIsValid ()
441{
442 return m_opaque_sp->EffectiveUserIDIsValid();
443}
444
445bool
446SBAttachInfo::EffectiveGroupIDIsValid ()
447{
448 return m_opaque_sp->EffectiveGroupIDIsValid ();
449}
450
451void
452SBAttachInfo::SetEffectiveUserID (uint32_t uid)
453{
454 m_opaque_sp->SetEffectiveUserID(uid);
455}
456
457void
458SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
459{
460 m_opaque_sp->SetEffectiveGroupID(gid);
461}
462
463lldb::pid_t
464SBAttachInfo::GetParentProcessID ()
465{
466 return m_opaque_sp->GetParentProcessID();
467}
468
469void
470SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
471{
472 m_opaque_sp->SetParentProcessID (pid);
473}
474
475bool
476SBAttachInfo::ParentProcessIDIsValid()
477{
478 return m_opaque_sp->ParentProcessIDIsValid();
479}
480
481
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482//----------------------------------------------------------------------
483// SBTarget constructor
484//----------------------------------------------------------------------
Greg Clayton54979cd2010-12-15 05:08:08 +0000485SBTarget::SBTarget () :
486 m_opaque_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487{
488}
489
490SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton66111032010-06-23 01:19:29 +0000491 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492{
493}
494
495SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton66111032010-06-23 01:19:29 +0000496 m_opaque_sp (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497{
498}
499
Greg Claytonefabb122010-11-05 23:17:00 +0000500const SBTarget&
501SBTarget::operator = (const SBTarget& rhs)
502{
503 if (this != &rhs)
504 m_opaque_sp = rhs.m_opaque_sp;
505 return *this;
506}
507
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508//----------------------------------------------------------------------
509// Destructor
510//----------------------------------------------------------------------
511SBTarget::~SBTarget()
512{
513}
514
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000515const char *
516SBTarget::GetBroadcasterClassName ()
517{
518 return Target::GetStaticBroadcasterClass().AsCString();
519}
520
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521bool
522SBTarget::IsValid () const
523{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000524 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525}
526
527SBProcess
528SBTarget::GetProcess ()
529{
530 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000531 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000532 TargetSP target_sp(GetSP());
533 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000534 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000535 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000536 sb_process.SetSP (process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000537 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000538
Greg Clayton5160ce52013-03-27 23:08:40 +0000539 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000540 if (log)
541 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000542 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +0000543 target_sp.get(), process_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000544 }
545
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 return sb_process;
547}
548
Greg Clayton66111032010-06-23 01:19:29 +0000549SBDebugger
550SBTarget::GetDebugger () const
551{
552 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000553 TargetSP target_sp(GetSP());
554 if (target_sp)
555 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000556 return debugger;
557}
558
Jim Ingham270684d2011-03-31 00:01:24 +0000559SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000560SBTarget::LoadCore (const char *core_file)
561{
562 SBProcess sb_process;
563 TargetSP target_sp(GetSP());
564 if (target_sp)
565 {
566 FileSpec filespec(core_file, true);
567 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
568 NULL,
569 &filespec));
570 if (process_sp)
571 {
572 process_sp->LoadCore();
573 sb_process.SetSP (process_sp);
574 }
575 }
576 return sb_process;
577}
578
579SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000580SBTarget::LaunchSimple
581(
582 char const **argv,
583 char const **envp,
584 const char *working_directory
585)
586{
587 char *stdin_path = NULL;
588 char *stdout_path = NULL;
589 char *stderr_path = NULL;
590 uint32_t launch_flags = 0;
591 bool stop_at_entry = false;
592 SBError error;
593 SBListener listener = GetDebugger().GetListener();
594 return Launch (listener,
595 argv,
596 envp,
597 stdin_path,
598 stdout_path,
599 stderr_path,
600 working_directory,
601 launch_flags,
602 stop_at_entry,
603 error);
604}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000605
606SBProcess
607SBTarget::Launch
608(
Greg Clayton4b045622011-02-03 21:28:34 +0000609 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000610 char const **argv,
611 char const **envp,
612 const char *stdin_path,
613 const char *stdout_path,
614 const char *stderr_path,
615 const char *working_directory,
616 uint32_t launch_flags, // See LaunchFlags
617 bool stop_at_entry,
618 lldb::SBError& error
619)
620{
Greg Clayton5160ce52013-03-27 23:08:40 +0000621 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000622
Greg Claytonacdbe812012-01-30 09:04:36 +0000623 SBProcess sb_process;
624 ProcessSP process_sp;
625 TargetSP target_sp(GetSP());
626
Caroline Ticeceb6b132010-10-26 03:11:13 +0000627 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +0000628 {
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000629 log->Printf ("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, stop_at_entry=%i, &error (%p))...",
Greg Claytonacdbe812012-01-30 09:04:36 +0000630 target_sp.get(),
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000631 argv,
632 envp,
633 stdin_path ? stdin_path : "NULL",
634 stdout_path ? stdout_path : "NULL",
635 stderr_path ? stderr_path : "NULL",
636 working_directory ? working_directory : "NULL",
637 launch_flags,
638 stop_at_entry,
639 error.get());
Greg Clayton93aa84e2010-10-29 04:59:35 +0000640 }
Greg Claytonacdbe812012-01-30 09:04:36 +0000641
642 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000644 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000645
Greg Clayton645bf542011-01-27 01:01:10 +0000646 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
647 launch_flags |= eLaunchFlagDisableASLR;
648
Greg Clayton2289fa42011-04-30 01:09:13 +0000649 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000650 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000651 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000652 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000653 state = process_sp->GetState();
Greg Clayton2289fa42011-04-30 01:09:13 +0000654
Greg Claytonb9556ac2012-01-30 07:41:31 +0000655 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton2289fa42011-04-30 01:09:13 +0000656 {
657 if (state == eStateAttaching)
658 error.SetErrorString ("process attach is in progress");
659 else
660 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000661 return sb_process;
662 }
Greg Clayton931180e2011-01-27 06:44:37 +0000663 }
664
Greg Clayton2289fa42011-04-30 01:09:13 +0000665 if (state == eStateConnected)
666 {
667 // If we are already connected, then we have already specified the
668 // listener, so if a valid listener is supplied, we need to error out
669 // to let the client know.
670 if (listener.IsValid())
671 {
672 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000673 return sb_process;
674 }
675 }
676 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000677 {
Greg Clayton4b045622011-02-03 21:28:34 +0000678 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000679 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton4b045622011-02-03 21:28:34 +0000680 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000681 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton2289fa42011-04-30 01:09:13 +0000682 }
Greg Clayton645bf542011-01-27 01:01:10 +0000683
Greg Claytonb9556ac2012-01-30 07:41:31 +0000684 if (process_sp)
Greg Clayton2289fa42011-04-30 01:09:13 +0000685 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000686 sb_process.SetSP (process_sp);
Greg Clayton2289fa42011-04-30 01:09:13 +0000687 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
688 launch_flags |= eLaunchFlagDisableSTDIO;
689
Greg Clayton982c9762011-11-03 21:22:33 +0000690 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
691
Greg Claytonacdbe812012-01-30 09:04:36 +0000692 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Clayton982c9762011-11-03 21:22:33 +0000693 if (exe_module)
Greg Claytonad9e8282011-11-29 04:03:30 +0000694 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton982c9762011-11-03 21:22:33 +0000695 if (argv)
696 launch_info.GetArguments().AppendArguments (argv);
697 if (envp)
698 launch_info.GetEnvironmentEntries ().SetArguments (envp);
699
Greg Claytonb9556ac2012-01-30 07:41:31 +0000700 error.SetError (process_sp->Launch (launch_info));
Greg Clayton2289fa42011-04-30 01:09:13 +0000701 if (error.Success())
Greg Clayton645bf542011-01-27 01:01:10 +0000702 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000703 // We we are stopping at the entry point, we can return now!
704 if (stop_at_entry)
705 return sb_process;
Greg Clayton645bf542011-01-27 01:01:10 +0000706
Greg Clayton2289fa42011-04-30 01:09:13 +0000707 // Make sure we are stopped at the entry
Greg Claytonb9556ac2012-01-30 07:41:31 +0000708 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton2289fa42011-04-30 01:09:13 +0000709 if (state == eStateStopped)
Greg Clayton645bf542011-01-27 01:01:10 +0000710 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000711 // resume the process to skip the entry point
Greg Claytonb9556ac2012-01-30 07:41:31 +0000712 error.SetError (process_sp->Resume());
Greg Clayton2289fa42011-04-30 01:09:13 +0000713 if (error.Success())
Greg Clayton645bf542011-01-27 01:01:10 +0000714 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000715 // If we are doing synchronous mode, then wait for the
716 // process to stop yet again!
Greg Claytonacdbe812012-01-30 09:04:36 +0000717 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +0000718 process_sp->WaitForProcessToStop (NULL);
Greg Clayton645bf542011-01-27 01:01:10 +0000719 }
720 }
721 }
Greg Clayton2289fa42011-04-30 01:09:13 +0000722 }
723 else
724 {
725 error.SetErrorString ("unable to create lldb_private::Process");
Greg Clayton524e60b2010-10-06 22:10:17 +0000726 }
727 }
728 else
729 {
730 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000732
Caroline Tice20ad3c42010-10-29 21:48:37 +0000733 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000734 if (log)
735 {
Sean Callanan575a4542012-10-20 00:21:31 +0000736 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +0000737 target_sp.get(), process_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000738 }
739
Greg Clayton5d5028b2010-10-06 03:53:16 +0000740 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741}
742
Greg Clayton0e615682012-02-24 05:03:03 +0000743SBProcess
744SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
745{
Greg Clayton5160ce52013-03-27 23:08:40 +0000746 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton0e615682012-02-24 05:03:03 +0000747
748 SBProcess sb_process;
749 ProcessSP process_sp;
750 TargetSP target_sp(GetSP());
751
752 if (log)
753 {
754 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
755 }
756
757 if (target_sp)
758 {
759 Mutex::Locker api_locker (target_sp->GetAPIMutex());
760 StateType state = eStateInvalid;
761 process_sp = target_sp->GetProcessSP();
762 if (process_sp)
763 {
764 state = process_sp->GetState();
765
766 if (process_sp->IsAlive() && state != eStateConnected)
767 {
768 if (state == eStateAttaching)
769 error.SetErrorString ("process attach is in progress");
770 else
771 error.SetErrorString ("a process is already being debugged");
772 return sb_process;
773 }
774 }
775
776 if (state != eStateConnected)
777 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
778
779 if (process_sp)
780 {
781 sb_process.SetSP (process_sp);
782 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
Greg Clayton38d1f052012-02-24 20:59:25 +0000783
Greg Clayton38d1f052012-02-24 20:59:25 +0000784 Module *exe_module = target_sp->GetExecutableModulePointer();
785 if (exe_module)
Han Ming Ong04cf1ba2012-03-02 01:02:04 +0000786 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton38d1f052012-02-24 20:59:25 +0000787
788 const ArchSpec &arch_spec = target_sp->GetArchitecture();
789 if (arch_spec.IsValid())
790 launch_info.GetArchitecture () = arch_spec;
791
Greg Clayton0e615682012-02-24 05:03:03 +0000792 error.SetError (process_sp->Launch (launch_info));
Greg Clayton38d1f052012-02-24 20:59:25 +0000793 const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
Greg Clayton0e615682012-02-24 05:03:03 +0000794 if (error.Success())
795 {
Greg Clayton0e615682012-02-24 05:03:03 +0000796 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Clayton38d1f052012-02-24 20:59:25 +0000797 {
798 // If we are doing synchronous mode, then wait for the initial
799 // stop to happen, else, return and let the caller watch for
800 // the stop
801 if (synchronous_execution)
Greg Clayton23f59502012-07-17 03:23:13 +0000802 process_sp->WaitForProcessToStop (NULL);
Greg Clayton38d1f052012-02-24 20:59:25 +0000803 // We we are stopping at the entry point, we can return now!
Greg Clayton0e615682012-02-24 05:03:03 +0000804 return sb_process;
Greg Clayton38d1f052012-02-24 20:59:25 +0000805 }
Greg Clayton0e615682012-02-24 05:03:03 +0000806
807 // Make sure we are stopped at the entry
Greg Clayton23f59502012-07-17 03:23:13 +0000808 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton0e615682012-02-24 05:03:03 +0000809 if (state == eStateStopped)
810 {
811 // resume the process to skip the entry point
812 error.SetError (process_sp->Resume());
813 if (error.Success())
814 {
815 // If we are doing synchronous mode, then wait for the
816 // process to stop yet again!
Greg Clayton38d1f052012-02-24 20:59:25 +0000817 if (synchronous_execution)
Greg Clayton0e615682012-02-24 05:03:03 +0000818 process_sp->WaitForProcessToStop (NULL);
819 }
820 }
821 }
822 }
823 else
824 {
825 error.SetErrorString ("unable to create lldb_private::Process");
826 }
827 }
828 else
829 {
830 error.SetErrorString ("SBTarget is invalid");
831 }
832
833 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
834 if (log)
835 {
Sean Callanan575a4542012-10-20 00:21:31 +0000836 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Greg Clayton0e615682012-02-24 05:03:03 +0000837 target_sp.get(), process_sp.get());
838 }
839
840 return sb_process;
841}
842
843lldb::SBProcess
844SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
845{
Greg Clayton5160ce52013-03-27 23:08:40 +0000846 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000847
Greg Clayton0e615682012-02-24 05:03:03 +0000848 SBProcess sb_process;
849 ProcessSP process_sp;
850 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000851
852 if (log)
853 {
854 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...", target_sp.get());
855 }
856
Greg Clayton0e615682012-02-24 05:03:03 +0000857 if (target_sp)
858 {
859 Mutex::Locker api_locker (target_sp->GetAPIMutex());
860
861 StateType state = eStateInvalid;
862 process_sp = target_sp->GetProcessSP();
863 if (process_sp)
864 {
865 state = process_sp->GetState();
866
867 if (process_sp->IsAlive() && state != eStateConnected)
868 {
869 if (state == eStateAttaching)
870 error.SetErrorString ("process attach is in progress");
871 else
872 error.SetErrorString ("a process is already being debugged");
Sean Callanan575a4542012-10-20 00:21:31 +0000873 if (log)
874 {
875 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
876 target_sp.get(), error.GetCString());
877 }
Greg Clayton0e615682012-02-24 05:03:03 +0000878 return sb_process;
879 }
880 }
881
882 if (state != eStateConnected)
883 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
884
885 if (process_sp)
886 {
Greg Clayton0e615682012-02-24 05:03:03 +0000887 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000888 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000889 {
890 PlatformSP platform_sp = target_sp->GetPlatform();
Greg Clayton91e407e2012-09-27 00:03:39 +0000891 // See if we can pre-verify if a process exists or not
892 if (platform_sp && platform_sp->IsConnected())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000893 {
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000894 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Clayton91e407e2012-09-27 00:03:39 +0000895 ProcessInstanceInfo instance_info;
896 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
897 {
898 attach_info.SetUserID(instance_info.GetEffectiveUserID());
899 }
900 else
901 {
Daniel Malead01b2952012-11-29 21:49:15 +0000902 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000903 if (log)
904 {
905 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
906 target_sp.get(), error.GetCString());
907 }
Greg Clayton91e407e2012-09-27 00:03:39 +0000908 return sb_process;
909 }
Han Ming Ongcec8c902012-02-29 00:12:56 +0000910 }
911 }
Han Ming Ongbee98392012-02-29 19:16:40 +0000912 error.SetError (process_sp->Attach (attach_info));
913 if (error.Success())
914 {
915 sb_process.SetSP (process_sp);
916 // If we are doing synchronous mode, then wait for the
917 // process to stop!
918 if (target_sp->GetDebugger().GetAsyncExecution () == false)
919 process_sp->WaitForProcessToStop (NULL);
920 }
Greg Clayton0e615682012-02-24 05:03:03 +0000921 }
922 else
923 {
924 error.SetErrorString ("unable to create lldb_private::Process");
925 }
926 }
927 else
928 {
929 error.SetErrorString ("SBTarget is invalid");
930 }
Sean Callanan575a4542012-10-20 00:21:31 +0000931
932 if (log)
933 {
934 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
935 target_sp.get(), process_sp.get());
936 }
937
Greg Clayton0e615682012-02-24 05:03:03 +0000938 return sb_process;
939}
940
941
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000942#if defined(__APPLE__)
943
944lldb::SBProcess
945SBTarget::AttachToProcessWithID (SBListener &listener,
946 ::pid_t pid,
947 lldb::SBError& error)
948{
949 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
950}
951
952#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000953
954lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000955SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000956(
Greg Clayton4b045622011-02-03 21:28:34 +0000957 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000958 lldb::pid_t pid,// The process ID to attach to
959 SBError& error // An error explaining what went wrong if attach fails
960)
961{
Greg Clayton5160ce52013-03-27 23:08:40 +0000962 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000963
Greg Clayton524e60b2010-10-06 22:10:17 +0000964 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000965 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000966 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000967
968 if (log)
969 {
Daniel Malead01b2952012-11-29 21:49:15 +0000970 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...", target_sp.get(), pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000971 }
972
Greg Claytonacdbe812012-01-30 09:04:36 +0000973 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000974 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000975 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0c74e782011-06-24 03:21:43 +0000976
977 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000978 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000979 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +0000980 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000981 state = process_sp->GetState();
Greg Clayton0c74e782011-06-24 03:21:43 +0000982
Greg Claytonb9556ac2012-01-30 07:41:31 +0000983 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton0c74e782011-06-24 03:21:43 +0000984 {
985 if (state == eStateAttaching)
986 error.SetErrorString ("process attach is in progress");
987 else
988 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +0000989 return sb_process;
990 }
991 }
992
993 if (state == eStateConnected)
994 {
995 // If we are already connected, then we have already specified the
996 // listener, so if a valid listener is supplied, we need to error out
997 // to let the client know.
998 if (listener.IsValid())
999 {
1000 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001001 return sb_process;
1002 }
1003 }
Greg Clayton4b045622011-02-03 21:28:34 +00001004 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001005 {
1006 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001007 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001008 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001009 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001010 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001011 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001012 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001013 sb_process.SetSP (process_sp);
1014
Greg Clayton144f3a92011-11-15 03:53:30 +00001015 ProcessAttachInfo attach_info;
1016 attach_info.SetProcessID (pid);
Han Ming Ong84647042012-02-25 01:07:38 +00001017
1018 PlatformSP platform_sp = target_sp->GetPlatform();
1019 ProcessInstanceInfo instance_info;
1020 if (platform_sp->GetProcessInfo(pid, instance_info))
1021 {
1022 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ong84647042012-02-25 01:07:38 +00001023 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001024 error.SetError (process_sp->Attach (attach_info));
Greg Claytone2186ed2012-09-07 17:51:47 +00001025 if (error.Success())
1026 {
1027 // If we are doing synchronous mode, then wait for the
1028 // process to stop!
1029 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +00001030 process_sp->WaitForProcessToStop (NULL);
Greg Claytone2186ed2012-09-07 17:51:47 +00001031 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001032 }
1033 else
1034 {
1035 error.SetErrorString ("unable to create lldb_private::Process");
1036 }
1037 }
1038 else
1039 {
1040 error.SetErrorString ("SBTarget is invalid");
1041 }
Sean Callanan575a4542012-10-20 00:21:31 +00001042
1043 if (log)
1044 {
1045 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
1046 target_sp.get(), process_sp.get());
1047 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001048 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001049}
1050
1051lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +00001052SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +00001053(
Greg Clayton4b045622011-02-03 21:28:34 +00001054 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +00001055 const char *name, // basename of process to attach to
1056 bool wait_for, // if true wait for a new instance of "name" to be launched
1057 SBError& error // An error explaining what went wrong if attach fails
1058)
1059{
Greg Clayton5160ce52013-03-27 23:08:40 +00001060 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001061
Greg Clayton524e60b2010-10-06 22:10:17 +00001062 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001063 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001064 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001065
1066 if (log)
1067 {
1068 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...", target_sp.get(), name, wait_for ? "true" : "false");
1069 }
1070
Greg Claytonacdbe812012-01-30 09:04:36 +00001071 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001072 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001073 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +00001074
Greg Clayton0c74e782011-06-24 03:21:43 +00001075 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +00001076 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +00001077 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +00001078 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001079 state = process_sp->GetState();
Greg Clayton0c74e782011-06-24 03:21:43 +00001080
Greg Claytonb9556ac2012-01-30 07:41:31 +00001081 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton0c74e782011-06-24 03:21:43 +00001082 {
1083 if (state == eStateAttaching)
1084 error.SetErrorString ("process attach is in progress");
1085 else
1086 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001087 return sb_process;
1088 }
1089 }
1090
1091 if (state == eStateConnected)
1092 {
1093 // If we are already connected, then we have already specified the
1094 // listener, so if a valid listener is supplied, we need to error out
1095 // to let the client know.
1096 if (listener.IsValid())
1097 {
1098 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001099 return sb_process;
1100 }
1101 }
Greg Clayton4b045622011-02-03 21:28:34 +00001102 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001103 {
1104 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001105 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001106 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001107 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001108 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001109
Greg Claytonb9556ac2012-01-30 07:41:31 +00001110 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001111 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001112 sb_process.SetSP (process_sp);
Greg Clayton144f3a92011-11-15 03:53:30 +00001113 ProcessAttachInfo attach_info;
1114 attach_info.GetExecutableFile().SetFile(name, false);
1115 attach_info.SetWaitForLaunch(wait_for);
Greg Claytonb9556ac2012-01-30 07:41:31 +00001116 error.SetError (process_sp->Attach (attach_info));
Daniel Malead659dc12013-04-01 19:47:00 +00001117 if (error.Success())
1118 {
1119 // If we are doing synchronous mode, then wait for the
1120 // process to stop!
1121 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1122 process_sp->WaitForProcessToStop (NULL);
1123 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001124 }
1125 else
1126 {
1127 error.SetErrorString ("unable to create lldb_private::Process");
1128 }
1129 }
1130 else
1131 {
1132 error.SetErrorString ("SBTarget is invalid");
1133 }
Sean Callanan575a4542012-10-20 00:21:31 +00001134
1135 if (log)
1136 {
1137 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1138 target_sp.get(), process_sp.get());
1139 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001140 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001141}
1142
James McIlree9631aae2011-03-04 00:31:13 +00001143lldb::SBProcess
1144SBTarget::ConnectRemote
1145(
1146 SBListener &listener,
1147 const char *url,
1148 const char *plugin_name,
1149 SBError& error
1150)
1151{
Greg Clayton5160ce52013-03-27 23:08:40 +00001152 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001153
James McIlree9631aae2011-03-04 00:31:13 +00001154 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001155 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001156 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001157
1158 if (log)
1159 {
1160 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1161 }
1162
Greg Claytonacdbe812012-01-30 09:04:36 +00001163 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001164 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001165 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +00001166 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001167 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001168 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001169 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001170
1171
Greg Claytonb9556ac2012-01-30 07:41:31 +00001172 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001173 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001174 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001175 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +00001176 }
1177 else
1178 {
1179 error.SetErrorString ("unable to create lldb_private::Process");
1180 }
1181 }
1182 else
1183 {
1184 error.SetErrorString ("SBTarget is invalid");
1185 }
Sean Callanan575a4542012-10-20 00:21:31 +00001186
1187 if (log)
1188 {
1189 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1190 target_sp.get(), process_sp.get());
1191 }
James McIlree9631aae2011-03-04 00:31:13 +00001192 return sb_process;
1193}
1194
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195SBFileSpec
1196SBTarget::GetExecutable ()
1197{
Caroline Ticeceb6b132010-10-26 03:11:13 +00001198
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +00001200 TargetSP target_sp(GetSP());
1201 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001203 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +00001204 if (exe_module)
1205 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001207
Greg Clayton5160ce52013-03-27 23:08:40 +00001208 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001209 if (log)
1210 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001211 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001212 target_sp.get(), exe_file_spec.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001213 }
1214
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 return exe_file_spec;
1216}
1217
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219SBTarget::operator == (const SBTarget &rhs) const
1220{
Greg Clayton66111032010-06-23 01:19:29 +00001221 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222}
1223
1224bool
1225SBTarget::operator != (const SBTarget &rhs) const
1226{
Greg Clayton66111032010-06-23 01:19:29 +00001227 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228}
1229
Greg Claytonb9556ac2012-01-30 07:41:31 +00001230lldb::TargetSP
1231SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +00001232{
1233 return m_opaque_sp;
1234}
1235
Greg Clayton66111032010-06-23 01:19:29 +00001236void
Greg Claytonb9556ac2012-01-30 07:41:31 +00001237SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +00001238{
1239 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240}
1241
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001242lldb::SBAddress
1243SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001244{
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001245 lldb::SBAddress sb_addr;
1246 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +00001247 TargetSP target_sp(GetSP());
1248 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001249 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001250 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1251 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001252 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001253 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001254
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001255 // We have a load address that isn't in a section, just return an address
1256 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +00001257 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001258 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001259}
1260
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001261SBSymbolContext
1262SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1263{
1264 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001265 if (addr.IsValid())
1266 {
1267 TargetSP target_sp(GetSP());
1268 if (target_sp)
1269 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1270 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001271 return sc;
1272}
1273
1274
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275SBBreakpoint
1276SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1277{
Greg Clayton7481c202010-11-08 00:28:40 +00001278 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279}
1280
1281SBBreakpoint
1282SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1283{
Greg Clayton5160ce52013-03-27 23:08:40 +00001284 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001285
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001287 TargetSP target_sp(GetSP());
1288 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001289 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001290 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001291
Greg Clayton1f746072012-08-29 21:13:06 +00001292 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001293 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001294 const bool internal = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001295 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001296 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001297
1298 if (log)
1299 {
1300 SBStream sstr;
1301 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001302 char path[PATH_MAX];
1303 sb_file_spec->GetPath (path, sizeof(path));
1304 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytonacdbe812012-01-30 09:04:36 +00001305 target_sp.get(),
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001306 path,
Greg Clayton48381312010-10-30 04:51:46 +00001307 line,
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001308 sb_bp.get(),
Caroline Ticeceb6b132010-10-26 03:11:13 +00001309 sstr.GetData());
1310 }
1311
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312 return sb_bp;
1313}
1314
1315SBBreakpoint
1316SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1317{
Greg Clayton5160ce52013-03-27 23:08:40 +00001318 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001319
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001321 TargetSP target_sp(GetSP());
1322 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001323 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001324 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001325
1326 const bool internal = false;
1327 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328 if (module_name && module_name[0])
1329 {
Jim Ingham969795f2011-09-21 01:17:13 +00001330 FileSpecList module_spec_list;
1331 module_spec_list.Append (FileSpec (module_name, false));
Jim Inghama8558b62012-05-22 00:12:20 +00001332 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333 }
1334 else
1335 {
Jim Inghama8558b62012-05-22 00:12:20 +00001336 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337 }
1338 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001339
1340 if (log)
1341 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001342 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001343 target_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001344 }
1345
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346 return sb_bp;
1347}
1348
Jim Ingham87df91b2011-09-23 00:54:11 +00001349lldb::SBBreakpoint
1350SBTarget::BreakpointCreateByName (const char *symbol_name,
1351 const SBFileSpecList &module_list,
1352 const SBFileSpecList &comp_unit_list)
1353{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001354 uint32_t name_type_mask = eFunctionNameTypeAuto;
1355 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1356}
1357
1358lldb::SBBreakpoint
1359SBTarget::BreakpointCreateByName (const char *symbol_name,
1360 uint32_t name_type_mask,
1361 const SBFileSpecList &module_list,
1362 const SBFileSpecList &comp_unit_list)
1363{
Greg Clayton5160ce52013-03-27 23:08:40 +00001364 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001365
1366 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001367 TargetSP target_sp(GetSP());
1368 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001369 {
Jim Inghama8558b62012-05-22 00:12:20 +00001370 const bool internal = false;
1371 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001372 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1373 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Jim Ingham87df91b2011-09-23 00:54:11 +00001374 comp_unit_list.get(),
1375 symbol_name,
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001376 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001377 skip_prologue,
1378 internal);
Jim Ingham87df91b2011-09-23 00:54:11 +00001379 }
1380
1381 if (log)
1382 {
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001383 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001384 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001385 }
1386
1387 return sb_bp;
1388}
1389
Jim Inghamfab10e82012-03-06 00:37:27 +00001390lldb::SBBreakpoint
1391SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1392 uint32_t num_names,
1393 uint32_t name_type_mask,
1394 const SBFileSpecList &module_list,
1395 const SBFileSpecList &comp_unit_list)
1396{
Greg Clayton5160ce52013-03-27 23:08:40 +00001397 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001398
1399 SBBreakpoint sb_bp;
1400 TargetSP target_sp(GetSP());
1401 if (target_sp && num_names > 0)
1402 {
1403 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001404 const bool internal = false;
1405 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001406 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1407 comp_unit_list.get(),
1408 symbol_names,
1409 num_names,
1410 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001411 skip_prologue,
1412 internal);
Jim Inghamfab10e82012-03-06 00:37:27 +00001413 }
1414
1415 if (log)
1416 {
1417 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1418 for (uint32_t i = 0 ; i < num_names; i++)
1419 {
1420 char sep;
1421 if (i < num_names - 1)
1422 sep = ',';
1423 else
1424 sep = '}';
1425 if (symbol_names[i] != NULL)
1426 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1427 else
1428 log->Printf ("\"<NULL>\"%c ", sep);
1429
1430 }
1431 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1432 }
1433
1434 return sb_bp;
1435}
Jim Ingham87df91b2011-09-23 00:54:11 +00001436
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437SBBreakpoint
1438SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1439{
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;
1449 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001450
1451 if (module_name && module_name[0])
1452 {
Jim Ingham969795f2011-09-21 01:17:13 +00001453 FileSpecList module_spec_list;
1454 module_spec_list.Append (FileSpec (module_name, false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455
Jim Inghama8558b62012-05-22 00:12:20 +00001456 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457 }
1458 else
1459 {
Jim Inghama8558b62012-05-22 00:12:20 +00001460 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 }
1462 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001463
1464 if (log)
1465 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001466 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001467 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001468 }
1469
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,
1475 const SBFileSpecList &module_list,
1476 const SBFileSpecList &comp_unit_list)
1477{
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;
1487 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Ingham87df91b2011-09-23 00:54:11 +00001488
Jim Inghama8558b62012-05-22 00:12:20 +00001489 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
Jim Ingham87df91b2011-09-23 00:54:11 +00001490 }
1491
1492 if (log)
1493 {
1494 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001495 target_sp.get(), symbol_name_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001496 }
1497
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());
1511 *sb_bp = target_sp->CreateBreakpoint (address, false);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001512 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001513
1514 if (log)
1515 {
Daniel Malead01b2952012-11-29 21:49:15 +00001516 log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)", target_sp.get(), (uint64_t) address, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001517 }
1518
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001519 return sb_bp;
1520}
1521
Jim Ingham969795f2011-09-21 01:17:13 +00001522lldb::SBBreakpoint
1523SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1524{
Greg Clayton5160ce52013-03-27 23:08:40 +00001525 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001526
1527 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001528 TargetSP target_sp(GetSP());
1529 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001530 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001531 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001532 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001533 FileSpecList source_file_spec_list;
1534 source_file_spec_list.Append (source_file.ref());
Jim Ingham969795f2011-09-21 01:17:13 +00001535
1536 if (module_name && module_name[0])
1537 {
1538 FileSpecList module_spec_list;
1539 module_spec_list.Append (FileSpec (module_name, false));
1540
Greg Claytonacdbe812012-01-30 09:04:36 +00001541 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
Jim Ingham969795f2011-09-21 01:17:13 +00001542 }
1543 else
1544 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001545 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
Jim Ingham969795f2011-09-21 01:17:13 +00001546 }
1547 }
1548
1549 if (log)
1550 {
1551 char path[PATH_MAX];
1552 source_file->GetPath (path, sizeof(path));
1553 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001554 target_sp.get(), source_regex, path, module_name, sb_bp.get());
Jim Ingham969795f2011-09-21 01:17:13 +00001555 }
1556
1557 return sb_bp;
1558}
1559
Jim Ingham87df91b2011-09-23 00:54:11 +00001560lldb::SBBreakpoint
1561SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1562 const SBFileSpecList &module_list,
1563 const lldb::SBFileSpecList &source_file_list)
1564{
Greg Clayton5160ce52013-03-27 23:08:40 +00001565 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001566
1567 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001568 TargetSP target_sp(GetSP());
1569 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001570 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001571 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001572 RegularExpression regexp(source_regex);
Greg Claytonacdbe812012-01-30 09:04:36 +00001573 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
Jim Ingham87df91b2011-09-23 00:54:11 +00001574 }
1575
1576 if (log)
1577 {
1578 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001579 target_sp.get(), source_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001580 }
1581
1582 return sb_bp;
1583}
Jim Ingham969795f2011-09-21 01:17:13 +00001584
Jim Inghamfab10e82012-03-06 00:37:27 +00001585lldb::SBBreakpoint
1586SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1587 bool catch_bp,
1588 bool throw_bp)
1589{
Greg Clayton5160ce52013-03-27 23:08:40 +00001590 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001591
1592 SBBreakpoint sb_bp;
1593 TargetSP target_sp(GetSP());
1594 if (target_sp)
1595 {
1596 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1597 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1598 }
1599
1600 if (log)
1601 {
1602 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1603 target_sp.get(),
1604 LanguageRuntime::GetNameForLanguageType(language),
1605 catch_bp ? "on" : "off",
1606 throw_bp ? "on" : "off",
1607 sb_bp.get());
1608 }
1609
1610 return sb_bp;
1611}
1612
Greg Clayton9fed0d82010-07-23 23:33:17 +00001613uint32_t
1614SBTarget::GetNumBreakpoints () const
1615{
Greg Claytonacdbe812012-01-30 09:04:36 +00001616 TargetSP target_sp(GetSP());
1617 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001618 {
1619 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001620 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001621 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001622 return 0;
1623}
1624
1625SBBreakpoint
1626SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1627{
1628 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001629 TargetSP target_sp(GetSP());
1630 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001631 {
1632 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001633 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001634 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001635 return sb_breakpoint;
1636}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637
1638bool
1639SBTarget::BreakpointDelete (break_id_t bp_id)
1640{
Greg Clayton5160ce52013-03-27 23:08:40 +00001641 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001642
Caroline Ticeceb6b132010-10-26 03:11:13 +00001643 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001644 TargetSP target_sp(GetSP());
1645 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001646 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001647 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1648 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001649 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001650
1651 if (log)
1652 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001653 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001654 }
1655
1656 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657}
1658
Johnny Chen5d043462011-09-26 22:40:50 +00001659SBBreakpoint
1660SBTarget::FindBreakpointByID (break_id_t bp_id)
1661{
Greg Clayton5160ce52013-03-27 23:08:40 +00001662 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001663
1664 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001665 TargetSP target_sp(GetSP());
1666 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001667 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001668 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1669 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001670 }
1671
1672 if (log)
1673 {
1674 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001675 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001676 }
1677
1678 return sb_breakpoint;
1679}
1680
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001681bool
1682SBTarget::EnableAllBreakpoints ()
1683{
Greg Claytonacdbe812012-01-30 09:04:36 +00001684 TargetSP target_sp(GetSP());
1685 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001687 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1688 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689 return true;
1690 }
1691 return false;
1692}
1693
1694bool
1695SBTarget::DisableAllBreakpoints ()
1696{
Greg Claytonacdbe812012-01-30 09:04:36 +00001697 TargetSP target_sp(GetSP());
1698 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001700 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1701 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702 return true;
1703 }
1704 return false;
1705}
1706
1707bool
1708SBTarget::DeleteAllBreakpoints ()
1709{
Greg Claytonacdbe812012-01-30 09:04:36 +00001710 TargetSP target_sp(GetSP());
1711 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001712 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001713 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1714 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001715 return true;
1716 }
1717 return false;
1718}
1719
Johnny Chen5d043462011-09-26 22:40:50 +00001720uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001721SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001722{
Greg Claytonacdbe812012-01-30 09:04:36 +00001723 TargetSP target_sp(GetSP());
1724 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001725 {
Johnny Chen01a67862011-10-14 00:42:25 +00001726 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001727 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001728 }
1729 return 0;
1730}
1731
Greg Clayton1b282f92011-10-13 18:08:26 +00001732SBWatchpoint
1733SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001734{
Johnny Chen01a67862011-10-14 00:42:25 +00001735 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001736 TargetSP target_sp(GetSP());
1737 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001738 {
Johnny Chen01a67862011-10-14 00:42:25 +00001739 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001740 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001741 }
Johnny Chen01a67862011-10-14 00:42:25 +00001742 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001743}
1744
1745bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001746SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001747{
Greg Clayton5160ce52013-03-27 23:08:40 +00001748 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001749
1750 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001751 TargetSP target_sp(GetSP());
1752 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001753 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001754 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001755 Mutex::Locker locker;
1756 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001757 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001758 }
1759
1760 if (log)
1761 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001762 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
Johnny Chen5d043462011-09-26 22:40:50 +00001763 }
1764
1765 return result;
1766}
1767
Greg Clayton1b282f92011-10-13 18:08:26 +00001768SBWatchpoint
1769SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001770{
Greg Clayton5160ce52013-03-27 23:08:40 +00001771 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001772
Greg Clayton1b282f92011-10-13 18:08:26 +00001773 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001774 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001775 TargetSP target_sp(GetSP());
1776 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001777 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001778 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001779 Mutex::Locker locker;
1780 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001781 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1782 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001783 }
1784
1785 if (log)
1786 {
Johnny Chen01a67862011-10-14 00:42:25 +00001787 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001788 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001789 }
1790
Greg Clayton1b282f92011-10-13 18:08:26 +00001791 return sb_watchpoint;
1792}
1793
1794lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001795SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001796{
Greg Clayton5160ce52013-03-27 23:08:40 +00001797 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1b282f92011-10-13 18:08:26 +00001798
1799 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001800 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001801 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001802 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001803 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001804 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001805 uint32_t watch_type = 0;
1806 if (read)
1807 watch_type |= LLDB_WATCH_TYPE_READ;
1808 if (write)
1809 watch_type |= LLDB_WATCH_TYPE_WRITE;
Johnny Chen7385a5a2012-05-31 22:56:36 +00001810 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001811 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001812 // This API doesn't take in a type, so we can't figure out what it is.
1813 ClangASTType *type = NULL;
1814 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001815 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001816 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001817 }
1818
1819 if (log)
1820 {
Daniel Malead01b2952012-11-29 21:49:15 +00001821 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001822 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
Greg Clayton1b282f92011-10-13 18:08:26 +00001823 }
1824
1825 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001826}
1827
1828bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001829SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001830{
Greg Claytonacdbe812012-01-30 09:04:36 +00001831 TargetSP target_sp(GetSP());
1832 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001833 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001834 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001835 Mutex::Locker locker;
1836 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001837 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001838 return true;
1839 }
1840 return false;
1841}
1842
1843bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001844SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001845{
Greg Claytonacdbe812012-01-30 09:04:36 +00001846 TargetSP target_sp(GetSP());
1847 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001848 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001849 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001850 Mutex::Locker locker;
1851 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001852 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001853 return true;
1854 }
1855 return false;
1856}
1857
1858bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001859SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001860{
Greg Claytonacdbe812012-01-30 09:04:36 +00001861 TargetSP target_sp(GetSP());
1862 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001863 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001864 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001865 Mutex::Locker locker;
1866 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001867 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001868 return true;
1869 }
1870 return false;
1871}
1872
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001874lldb::SBModule
1875SBTarget::AddModule (const char *path,
1876 const char *triple,
1877 const char *uuid_cstr)
1878{
Greg Claytonb210aec2012-04-23 20:23:39 +00001879 return AddModule (path, triple, uuid_cstr, NULL);
1880}
1881
1882lldb::SBModule
1883SBTarget::AddModule (const char *path,
1884 const char *triple,
1885 const char *uuid_cstr,
1886 const char *symfile)
1887{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001888 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001889 TargetSP target_sp(GetSP());
1890 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001891 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001892 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001893 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001894 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001895
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001896 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001897 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001898
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001899 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001900 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Greg Claytonb210aec2012-04-23 20:23:39 +00001901
1902 if (symfile)
1903 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001904
Greg Claytonb9a01b32012-02-26 05:51:37 +00001905 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001906 }
1907 return sb_module;
1908}
1909
1910bool
1911SBTarget::AddModule (lldb::SBModule &module)
1912{
Greg Claytonacdbe812012-01-30 09:04:36 +00001913 TargetSP target_sp(GetSP());
1914 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001915 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001916 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001917 return true;
1918 }
1919 return false;
1920}
1921
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001922uint32_t
1923SBTarget::GetNumModules () const
1924{
Greg Clayton5160ce52013-03-27 23:08:40 +00001925 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001926
Caroline Ticeceb6b132010-10-26 03:11:13 +00001927 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00001928 TargetSP target_sp(GetSP());
1929 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001930 {
1931 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001932 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001933 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001934
1935 if (log)
Greg Claytonacdbe812012-01-30 09:04:36 +00001936 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001937
1938 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001939}
1940
Greg Clayton48e42542010-07-30 20:12:55 +00001941void
1942SBTarget::Clear ()
1943{
Greg Clayton5160ce52013-03-27 23:08:40 +00001944 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001945
1946 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001947 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001948
Greg Clayton48e42542010-07-30 20:12:55 +00001949 m_opaque_sp.reset();
1950}
1951
1952
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953SBModule
1954SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1955{
1956 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001957 TargetSP target_sp(GetSP());
1958 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001959 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001960 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001961 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00001962 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00001963 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001964 return sb_module;
1965}
1966
Greg Clayton13d19502012-01-29 06:07:39 +00001967lldb::ByteOrder
1968SBTarget::GetByteOrder ()
1969{
Greg Claytonacdbe812012-01-30 09:04:36 +00001970 TargetSP target_sp(GetSP());
1971 if (target_sp)
1972 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00001973 return eByteOrderInvalid;
1974}
1975
1976const char *
1977SBTarget::GetTriple ()
1978{
Greg Claytonacdbe812012-01-30 09:04:36 +00001979 TargetSP target_sp(GetSP());
1980 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00001981 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001982 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00001983 // Unique the string so we don't run into ownership issues since
1984 // the const strings put the string into the string pool once and
1985 // the strings never comes out
1986 ConstString const_triple (triple.c_str());
1987 return const_triple.GetCString();
1988 }
1989 return NULL;
1990}
1991
1992uint32_t
1993SBTarget::GetAddressByteSize()
1994{
Greg Claytonacdbe812012-01-30 09:04:36 +00001995 TargetSP target_sp(GetSP());
1996 if (target_sp)
1997 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00001998 return sizeof(void*);
1999}
2000
2001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002002SBModule
2003SBTarget::GetModuleAtIndex (uint32_t idx)
2004{
Greg Clayton5160ce52013-03-27 23:08:40 +00002005 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002006
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002007 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002008 ModuleSP module_sp;
2009 TargetSP target_sp(GetSP());
2010 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002011 {
2012 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002013 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2014 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002015 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002016
2017 if (log)
2018 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00002019 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002020 target_sp.get(), idx, module_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002021 }
2022
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002023 return sb_module;
2024}
2025
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002026bool
2027SBTarget::RemoveModule (lldb::SBModule module)
2028{
Greg Claytonacdbe812012-01-30 09:04:36 +00002029 TargetSP target_sp(GetSP());
2030 if (target_sp)
2031 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002032 return false;
2033}
2034
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035
2036SBBroadcaster
2037SBTarget::GetBroadcaster () const
2038{
Greg Clayton5160ce52013-03-27 23:08:40 +00002039 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002040
Greg Claytonacdbe812012-01-30 09:04:36 +00002041 TargetSP target_sp(GetSP());
2042 SBBroadcaster broadcaster(target_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002043
2044 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002045 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002046 target_sp.get(), broadcaster.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002047
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002048 return broadcaster;
2049}
2050
Caroline Ticedde9cff2010-09-20 05:20:02 +00002051bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002052SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002053{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002054 Stream &strm = description.ref();
2055
Greg Claytonacdbe812012-01-30 09:04:36 +00002056 TargetSP target_sp(GetSP());
2057 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002058 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002059 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002060 }
2061 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002062 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002063
2064 return true;
2065}
2066
Greg Clayton5569e642012-02-06 01:44:54 +00002067lldb::SBSymbolContextList
2068SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002069{
Greg Clayton5569e642012-02-06 01:44:54 +00002070 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002071 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002072 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002073 TargetSP target_sp(GetSP());
2074 if (target_sp)
2075 {
2076 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002077 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002078 const bool append = true;
2079 target_sp->GetImages().FindFunctions (ConstString(name),
2080 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002081 symbols_ok,
2082 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002083 append,
2084 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002085 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002086 }
Greg Clayton5569e642012-02-06 01:44:54 +00002087 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002088}
2089
Enrico Granata6f3533f2011-07-29 19:53:35 +00002090lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002091SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002092{
Greg Claytonacdbe812012-01-30 09:04:36 +00002093 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002094 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002095 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002096 ConstString const_typename(typename_cstr);
2097 SymbolContext sc;
2098 const bool exact_match = false;
2099
2100 const ModuleList &module_list = target_sp->GetImages();
2101 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002102 for (size_t idx = 0; idx < count; idx++)
2103 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002104 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2105 if (module_sp)
2106 {
2107 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2108 if (type_sp)
2109 return SBType(type_sp);
2110 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002111 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002112
2113 // Didn't find the type in the symbols; try the Objective-C runtime
2114 // if one is installed
2115
2116 ProcessSP process_sp(target_sp->GetProcessSP());
2117
2118 if (process_sp)
2119 {
2120 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2121
2122 if (objc_language_runtime)
2123 {
2124 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2125
2126 if (objc_type_vendor)
2127 {
2128 std::vector <ClangASTType> types;
2129
2130 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2131 return SBType(types[0]);
2132 }
2133 }
2134 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002135
2136 // No matches, search for basic typename matches
2137 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2138 if (clang_ast)
2139 return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002140 }
2141 return SBType();
2142}
2143
Greg Claytonb43165b2012-12-05 21:24:42 +00002144SBType
2145SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002146{
Greg Claytonacdbe812012-01-30 09:04:36 +00002147 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002148 if (target_sp)
2149 {
2150 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2151 if (clang_ast)
2152 return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), type));
2153 }
2154 return SBType();
2155}
2156
2157
2158lldb::SBTypeList
2159SBTarget::FindTypes (const char* typename_cstr)
2160{
2161 SBTypeList sb_type_list;
2162 TargetSP target_sp(GetSP());
2163 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002164 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002165 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002166 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002167 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002168 SymbolContext sc;
2169 TypeList type_list;
2170
Greg Clayton29399a22012-04-06 17:41:13 +00002171 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002172 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002173 exact_match,
2174 UINT32_MAX,
2175 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002176
Greg Claytonb43165b2012-12-05 21:24:42 +00002177 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002178 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002179 for (size_t idx = 0; idx < num_matches; idx++)
2180 {
2181 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2182 if (type_sp)
2183 sb_type_list.Append(SBType(type_sp));
2184 }
2185 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002186
2187 // Try the Objective-C runtime if one is installed
2188
2189 ProcessSP process_sp(target_sp->GetProcessSP());
2190
2191 if (process_sp)
2192 {
2193 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2194
2195 if (objc_language_runtime)
2196 {
2197 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2198
2199 if (objc_type_vendor)
2200 {
2201 std::vector <ClangASTType> types;
2202
2203 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2204 {
2205 for (ClangASTType &type : types)
2206 {
2207 sb_type_list.Append(SBType(type));
2208 }
2209 }
2210 }
2211 }
2212 }
2213
2214 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002215 {
2216 // No matches, search for basic typename matches
2217 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2218 if (clang_ast)
2219 sb_type_list.Append (SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002220 }
2221 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002222 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002223}
2224
Greg Claytondea8cb42011-06-29 22:09:02 +00002225SBValueList
2226SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2227{
2228 SBValueList sb_value_list;
2229
Greg Claytonacdbe812012-01-30 09:04:36 +00002230 TargetSP target_sp(GetSP());
2231 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002232 {
2233 VariableList variable_list;
2234 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002235 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2236 append,
2237 max_matches,
2238 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002239
2240 if (match_count > 0)
2241 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002242 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002243 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002244 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002245 for (uint32_t i=0; i<match_count; ++i)
2246 {
2247 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2248 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002249 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002250 }
2251 }
2252 }
2253
2254 return sb_value_list;
2255}
2256
Enrico Granatabcd80b42013-01-16 18:53:52 +00002257lldb::SBValue
2258SBTarget::FindFirstGlobalVariable (const char* name)
2259{
2260 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2261 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2262 return sb_value_list.GetValueAtIndex(0);
2263 return SBValue();
2264}
2265
Jim Inghame37d6052011-09-13 00:29:56 +00002266SBSourceManager
2267SBTarget::GetSourceManager()
2268{
2269 SBSourceManager source_manager (*this);
2270 return source_manager;
2271}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002272
Sean Callanan50952e92011-12-14 23:49:37 +00002273lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002274SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2275{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002276 return ReadInstructions (base_addr, count, NULL);
2277}
2278
2279lldb::SBInstructionList
2280SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2281{
Greg Clayton9c766112012-03-06 22:24:44 +00002282 SBInstructionList sb_instructions;
2283
2284 TargetSP target_sp(GetSP());
2285 if (target_sp)
2286 {
2287 Address *addr_ptr = base_addr.get();
2288
2289 if (addr_ptr)
2290 {
2291 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2292 bool prefer_file_cache = false;
2293 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002294 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2295 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2296 prefer_file_cache,
2297 data.GetBytes(),
2298 data.GetByteSize(),
2299 error,
2300 &load_addr);
2301 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002302 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2303 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002304 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002305 *addr_ptr,
2306 data.GetBytes(),
2307 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002308 count,
2309 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002310 }
2311 }
2312
2313 return sb_instructions;
2314
2315}
2316
2317lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002318SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2319{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002320 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2321}
2322
2323lldb::SBInstructionList
2324SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2325{
Sean Callanan50952e92011-12-14 23:49:37 +00002326 SBInstructionList sb_instructions;
2327
Greg Claytonacdbe812012-01-30 09:04:36 +00002328 TargetSP target_sp(GetSP());
2329 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002330 {
2331 Address addr;
2332
2333 if (base_addr.get())
2334 addr = *base_addr.get();
2335
Greg Clayton3faf47c2013-03-28 23:42:53 +00002336 const bool data_from_file = true;
2337
Greg Claytonacdbe812012-01-30 09:04:36 +00002338 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002339 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002340 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002341 addr,
2342 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002343 size,
2344 UINT32_MAX,
2345 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002346 }
2347
2348 return sb_instructions;
2349}
2350
2351lldb::SBInstructionList
2352SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2353{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002354 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2355}
2356
2357lldb::SBInstructionList
2358SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2359{
2360 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002361}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002362
2363SBError
2364SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2365 lldb::addr_t section_base_addr)
2366{
2367 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002368 TargetSP target_sp(GetSP());
2369 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002370 {
2371 if (!section.IsValid())
2372 {
2373 sb_error.SetErrorStringWithFormat ("invalid section");
2374 }
2375 else
2376 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002377 SectionSP section_sp (section.GetSP());
2378 if (section_sp)
2379 {
2380 if (section_sp->IsThreadSpecific())
2381 {
2382 sb_error.SetErrorString ("thread specific sections are not yet supported");
2383 }
2384 else
2385 {
Greg Clayton3c947372013-01-29 01:17:09 +00002386 if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2387 {
2388 // Flush info in the process (stack frames, etc)
2389 ProcessSP process_sp (target_sp->GetProcessSP());
2390 if (process_sp)
2391 process_sp->Flush();
2392 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002393 }
2394 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002395 }
2396 }
2397 else
2398 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002399 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002400 }
2401 return sb_error;
2402}
2403
2404SBError
2405SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2406{
2407 SBError sb_error;
2408
Greg Claytonacdbe812012-01-30 09:04:36 +00002409 TargetSP target_sp(GetSP());
2410 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002411 {
2412 if (!section.IsValid())
2413 {
2414 sb_error.SetErrorStringWithFormat ("invalid section");
2415 }
2416 else
2417 {
Greg Clayton3c947372013-01-29 01:17:09 +00002418 if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2419 {
2420 // Flush info in the process (stack frames, etc)
2421 ProcessSP process_sp (target_sp->GetProcessSP());
2422 if (process_sp)
2423 process_sp->Flush();
2424 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002425 }
2426 }
2427 else
2428 {
2429 sb_error.SetErrorStringWithFormat ("invalid target");
2430 }
2431 return sb_error;
2432}
2433
2434SBError
2435SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2436{
2437 SBError sb_error;
2438
Greg Claytonacdbe812012-01-30 09:04:36 +00002439 TargetSP target_sp(GetSP());
2440 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002441 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002442 ModuleSP module_sp (module.GetSP());
2443 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002444 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002445 bool changed = false;
2446 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002447 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002448 // The load was successful, make sure that at least some sections
2449 // changed before we notify that our module was loaded.
2450 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002451 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002452 ModuleList module_list;
2453 module_list.Append(module_sp);
2454 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002455 // Flush info in the process (stack frames, etc)
2456 ProcessSP process_sp (target_sp->GetProcessSP());
2457 if (process_sp)
2458 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002459 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002460 }
2461 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002462 else
2463 {
2464 sb_error.SetErrorStringWithFormat ("invalid module");
2465 }
2466
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002467 }
2468 else
2469 {
2470 sb_error.SetErrorStringWithFormat ("invalid target");
2471 }
2472 return sb_error;
2473}
2474
2475SBError
2476SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2477{
2478 SBError sb_error;
2479
2480 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002481 TargetSP target_sp(GetSP());
2482 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002483 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002484 ModuleSP module_sp (module.GetSP());
2485 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002486 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002487 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002488 if (objfile)
2489 {
2490 SectionList *section_list = objfile->GetSectionList();
2491 if (section_list)
2492 {
Greg Clayton3c947372013-01-29 01:17:09 +00002493 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002494 const size_t num_sections = section_list->GetSize();
2495 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2496 {
2497 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2498 if (section_sp)
Greg Clayton3c947372013-01-29 01:17:09 +00002499 changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2500 }
2501 if (changed)
2502 {
2503 // Flush info in the process (stack frames, etc)
2504 ProcessSP process_sp (target_sp->GetProcessSP());
2505 if (process_sp)
2506 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002507 }
2508 }
2509 else
2510 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002511 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002512 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2513 }
2514 }
2515 else
2516 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002517 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002518 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2519 }
2520 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002521 else
2522 {
2523 sb_error.SetErrorStringWithFormat ("invalid module");
2524 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002525 }
2526 else
2527 {
2528 sb_error.SetErrorStringWithFormat ("invalid target");
2529 }
2530 return sb_error;
2531}
2532
2533
Greg Claytone14e1922012-12-04 02:22:16 +00002534lldb::SBSymbolContextList
2535SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2536{
2537 SBSymbolContextList sb_sc_list;
2538 if (name && name[0])
2539 {
2540 TargetSP target_sp(GetSP());
2541 if (target_sp)
2542 {
2543 bool append = true;
2544 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2545 symbol_type,
2546 *sb_sc_list,
2547 append);
2548 }
2549 }
2550 return sb_sc_list;
2551
2552}
2553
2554
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002555lldb::SBValue
2556SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2557{
Greg Clayton5160ce52013-03-27 23:08:40 +00002558 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2559 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002560 SBValue expr_result;
2561 ExecutionResults exe_results = eExecutionSetupError;
2562 ValueObjectSP expr_value_sp;
2563 TargetSP target_sp(GetSP());
2564 StackFrame *frame = NULL;
2565 if (target_sp)
2566 {
2567 if (expr == NULL || expr[0] == '\0')
2568 {
2569 if (log)
2570 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2571 return expr_result;
2572 }
2573
2574 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2575 ExecutionContext exe_ctx (m_opaque_sp.get());
2576
2577 if (log)
2578 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2579
2580 frame = exe_ctx.GetFramePtr();
2581 Target *target = exe_ctx.GetTargetPtr();
2582
2583 if (target)
2584 {
2585#ifdef LLDB_CONFIGURATION_DEBUG
2586 StreamString frame_description;
2587 if (frame)
2588 frame->DumpUsingSettingsFormat (&frame_description);
2589 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2590 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2591#endif
2592 exe_results = target->EvaluateExpression (expr,
2593 frame,
2594 expr_value_sp,
2595 options.ref());
2596
2597 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2598#ifdef LLDB_CONFIGURATION_DEBUG
2599 Host::SetCrashDescription (NULL);
2600#endif
2601 }
2602 else
2603 {
2604 if (log)
2605 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2606 }
2607 }
2608#ifndef LLDB_DISABLE_PYTHON
2609 if (expr_log)
2610 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2611 expr_result.GetValue(),
2612 expr_result.GetSummary());
2613
2614 if (log)
2615 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2616 frame,
2617 expr,
2618 expr_value_sp.get(),
2619 exe_results);
2620#endif
2621
2622 return expr_result;
2623}
2624
Greg Clayton13fbb992013-02-01 00:47:49 +00002625
2626lldb::addr_t
2627SBTarget::GetStackRedZoneSize()
2628{
2629 TargetSP target_sp(GetSP());
2630 if (target_sp)
2631 {
2632 ABISP abi_sp;
2633 ProcessSP process_sp (target_sp->GetProcessSP());
2634 if (process_sp)
2635 abi_sp = process_sp->GetABI();
2636 else
2637 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2638 if (abi_sp)
2639 return abi_sp->GetRedZoneSize();
2640 }
2641 return 0;
2642}
2643