blob: f7d6bca55f06a6f4c0d657c674f31fe14aff654d [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));
Johnny Chena8079022011-06-17 19:21:30 +00001117 // If we are doing synchronous mode, then wait for the
1118 // process to stop!
Greg Claytonacdbe812012-01-30 09:04:36 +00001119 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +00001120 process_sp->WaitForProcessToStop (NULL);
Greg Clayton524e60b2010-10-06 22:10:17 +00001121 }
1122 else
1123 {
1124 error.SetErrorString ("unable to create lldb_private::Process");
1125 }
1126 }
1127 else
1128 {
1129 error.SetErrorString ("SBTarget is invalid");
1130 }
Sean Callanan575a4542012-10-20 00:21:31 +00001131
1132 if (log)
1133 {
1134 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1135 target_sp.get(), process_sp.get());
1136 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001137 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001138}
1139
James McIlree9631aae2011-03-04 00:31:13 +00001140lldb::SBProcess
1141SBTarget::ConnectRemote
1142(
1143 SBListener &listener,
1144 const char *url,
1145 const char *plugin_name,
1146 SBError& error
1147)
1148{
Greg Clayton5160ce52013-03-27 23:08:40 +00001149 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001150
James McIlree9631aae2011-03-04 00:31:13 +00001151 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001152 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001153 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001154
1155 if (log)
1156 {
1157 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1158 }
1159
Greg Claytonacdbe812012-01-30 09:04:36 +00001160 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001161 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001162 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +00001163 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001164 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001165 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001166 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001167
1168
Greg Claytonb9556ac2012-01-30 07:41:31 +00001169 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001170 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001171 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001172 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +00001173 }
1174 else
1175 {
1176 error.SetErrorString ("unable to create lldb_private::Process");
1177 }
1178 }
1179 else
1180 {
1181 error.SetErrorString ("SBTarget is invalid");
1182 }
Sean Callanan575a4542012-10-20 00:21:31 +00001183
1184 if (log)
1185 {
1186 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1187 target_sp.get(), process_sp.get());
1188 }
James McIlree9631aae2011-03-04 00:31:13 +00001189 return sb_process;
1190}
1191
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192SBFileSpec
1193SBTarget::GetExecutable ()
1194{
Caroline Ticeceb6b132010-10-26 03:11:13 +00001195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +00001197 TargetSP target_sp(GetSP());
1198 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001200 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +00001201 if (exe_module)
1202 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001204
Greg Clayton5160ce52013-03-27 23:08:40 +00001205 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001206 if (log)
1207 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001208 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001209 target_sp.get(), exe_file_spec.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001210 }
1211
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212 return exe_file_spec;
1213}
1214
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216SBTarget::operator == (const SBTarget &rhs) const
1217{
Greg Clayton66111032010-06-23 01:19:29 +00001218 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219}
1220
1221bool
1222SBTarget::operator != (const SBTarget &rhs) const
1223{
Greg Clayton66111032010-06-23 01:19:29 +00001224 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225}
1226
Greg Claytonb9556ac2012-01-30 07:41:31 +00001227lldb::TargetSP
1228SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +00001229{
1230 return m_opaque_sp;
1231}
1232
Greg Clayton66111032010-06-23 01:19:29 +00001233void
Greg Claytonb9556ac2012-01-30 07:41:31 +00001234SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +00001235{
1236 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237}
1238
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001239lldb::SBAddress
1240SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001241{
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001242 lldb::SBAddress sb_addr;
1243 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +00001244 TargetSP target_sp(GetSP());
1245 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001246 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001247 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1248 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001249 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001250 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001251
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001252 // We have a load address that isn't in a section, just return an address
1253 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +00001254 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001255 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001256}
1257
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001258SBSymbolContext
1259SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1260{
1261 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001262 if (addr.IsValid())
1263 {
1264 TargetSP target_sp(GetSP());
1265 if (target_sp)
1266 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1267 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001268 return sc;
1269}
1270
1271
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272SBBreakpoint
1273SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1274{
Greg Clayton7481c202010-11-08 00:28:40 +00001275 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001276}
1277
1278SBBreakpoint
1279SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1280{
Greg Clayton5160ce52013-03-27 23:08:40 +00001281 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001282
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001284 TargetSP target_sp(GetSP());
1285 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001286 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001287 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001288
Greg Clayton1f746072012-08-29 21:13:06 +00001289 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001290 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001291 const bool internal = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001292 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001293 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001294
1295 if (log)
1296 {
1297 SBStream sstr;
1298 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001299 char path[PATH_MAX];
1300 sb_file_spec->GetPath (path, sizeof(path));
1301 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytonacdbe812012-01-30 09:04:36 +00001302 target_sp.get(),
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001303 path,
Greg Clayton48381312010-10-30 04:51:46 +00001304 line,
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001305 sb_bp.get(),
Caroline Ticeceb6b132010-10-26 03:11:13 +00001306 sstr.GetData());
1307 }
1308
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001309 return sb_bp;
1310}
1311
1312SBBreakpoint
1313SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1314{
Greg Clayton5160ce52013-03-27 23:08:40 +00001315 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001316
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001318 TargetSP target_sp(GetSP());
1319 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001321 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001322
1323 const bool internal = false;
1324 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001325 if (module_name && module_name[0])
1326 {
Jim Ingham969795f2011-09-21 01:17:13 +00001327 FileSpecList module_spec_list;
1328 module_spec_list.Append (FileSpec (module_name, false));
Jim Inghama8558b62012-05-22 00:12:20 +00001329 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330 }
1331 else
1332 {
Jim Inghama8558b62012-05-22 00:12:20 +00001333 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 }
1335 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001336
1337 if (log)
1338 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001339 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001340 target_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001341 }
1342
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343 return sb_bp;
1344}
1345
Jim Ingham87df91b2011-09-23 00:54:11 +00001346lldb::SBBreakpoint
1347SBTarget::BreakpointCreateByName (const char *symbol_name,
1348 const SBFileSpecList &module_list,
1349 const SBFileSpecList &comp_unit_list)
1350{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001351 uint32_t name_type_mask = eFunctionNameTypeAuto;
1352 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1353}
1354
1355lldb::SBBreakpoint
1356SBTarget::BreakpointCreateByName (const char *symbol_name,
1357 uint32_t name_type_mask,
1358 const SBFileSpecList &module_list,
1359 const SBFileSpecList &comp_unit_list)
1360{
Greg Clayton5160ce52013-03-27 23:08:40 +00001361 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001362
1363 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001364 TargetSP target_sp(GetSP());
1365 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001366 {
Jim Inghama8558b62012-05-22 00:12:20 +00001367 const bool internal = false;
1368 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001369 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1370 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Jim Ingham87df91b2011-09-23 00:54:11 +00001371 comp_unit_list.get(),
1372 symbol_name,
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001373 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001374 skip_prologue,
1375 internal);
Jim Ingham87df91b2011-09-23 00:54:11 +00001376 }
1377
1378 if (log)
1379 {
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001380 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001381 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001382 }
1383
1384 return sb_bp;
1385}
1386
Jim Inghamfab10e82012-03-06 00:37:27 +00001387lldb::SBBreakpoint
1388SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1389 uint32_t num_names,
1390 uint32_t name_type_mask,
1391 const SBFileSpecList &module_list,
1392 const SBFileSpecList &comp_unit_list)
1393{
Greg Clayton5160ce52013-03-27 23:08:40 +00001394 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001395
1396 SBBreakpoint sb_bp;
1397 TargetSP target_sp(GetSP());
1398 if (target_sp && num_names > 0)
1399 {
1400 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001401 const bool internal = false;
1402 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001403 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1404 comp_unit_list.get(),
1405 symbol_names,
1406 num_names,
1407 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001408 skip_prologue,
1409 internal);
Jim Inghamfab10e82012-03-06 00:37:27 +00001410 }
1411
1412 if (log)
1413 {
1414 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1415 for (uint32_t i = 0 ; i < num_names; i++)
1416 {
1417 char sep;
1418 if (i < num_names - 1)
1419 sep = ',';
1420 else
1421 sep = '}';
1422 if (symbol_names[i] != NULL)
1423 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1424 else
1425 log->Printf ("\"<NULL>\"%c ", sep);
1426
1427 }
1428 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1429 }
1430
1431 return sb_bp;
1432}
Jim Ingham87df91b2011-09-23 00:54:11 +00001433
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001434SBBreakpoint
1435SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1436{
Greg Clayton5160ce52013-03-27 23:08:40 +00001437 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001438
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001440 TargetSP target_sp(GetSP());
1441 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001443 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001444 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001445 const bool internal = false;
1446 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447
1448 if (module_name && module_name[0])
1449 {
Jim Ingham969795f2011-09-21 01:17:13 +00001450 FileSpecList module_spec_list;
1451 module_spec_list.Append (FileSpec (module_name, false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452
Jim Inghama8558b62012-05-22 00:12:20 +00001453 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454 }
1455 else
1456 {
Jim Inghama8558b62012-05-22 00:12:20 +00001457 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 }
1459 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001460
1461 if (log)
1462 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001463 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001464 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001465 }
1466
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467 return sb_bp;
1468}
1469
Jim Ingham87df91b2011-09-23 00:54:11 +00001470lldb::SBBreakpoint
1471SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1472 const SBFileSpecList &module_list,
1473 const SBFileSpecList &comp_unit_list)
1474{
Greg Clayton5160ce52013-03-27 23:08:40 +00001475 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476
Jim Ingham87df91b2011-09-23 00:54:11 +00001477 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001478 TargetSP target_sp(GetSP());
1479 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001480 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001481 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001482 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001483 const bool internal = false;
1484 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Ingham87df91b2011-09-23 00:54:11 +00001485
Jim Inghama8558b62012-05-22 00:12:20 +00001486 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
Jim Ingham87df91b2011-09-23 00:54:11 +00001487 }
1488
1489 if (log)
1490 {
1491 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001492 target_sp.get(), symbol_name_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001493 }
1494
1495 return sb_bp;
1496}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497
1498SBBreakpoint
1499SBTarget::BreakpointCreateByAddress (addr_t address)
1500{
Greg Clayton5160ce52013-03-27 23:08:40 +00001501 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001502
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001504 TargetSP target_sp(GetSP());
1505 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001506 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001507 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1508 *sb_bp = target_sp->CreateBreakpoint (address, false);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001509 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001510
1511 if (log)
1512 {
Daniel Malead01b2952012-11-29 21:49:15 +00001513 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 +00001514 }
1515
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001516 return sb_bp;
1517}
1518
Jim Ingham969795f2011-09-21 01:17:13 +00001519lldb::SBBreakpoint
1520SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1521{
Greg Clayton5160ce52013-03-27 23:08:40 +00001522 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001523
1524 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001525 TargetSP target_sp(GetSP());
1526 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001527 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001528 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001529 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001530 FileSpecList source_file_spec_list;
1531 source_file_spec_list.Append (source_file.ref());
Jim Ingham969795f2011-09-21 01:17:13 +00001532
1533 if (module_name && module_name[0])
1534 {
1535 FileSpecList module_spec_list;
1536 module_spec_list.Append (FileSpec (module_name, false));
1537
Greg Claytonacdbe812012-01-30 09:04:36 +00001538 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
Jim Ingham969795f2011-09-21 01:17:13 +00001539 }
1540 else
1541 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001542 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
Jim Ingham969795f2011-09-21 01:17:13 +00001543 }
1544 }
1545
1546 if (log)
1547 {
1548 char path[PATH_MAX];
1549 source_file->GetPath (path, sizeof(path));
1550 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001551 target_sp.get(), source_regex, path, module_name, sb_bp.get());
Jim Ingham969795f2011-09-21 01:17:13 +00001552 }
1553
1554 return sb_bp;
1555}
1556
Jim Ingham87df91b2011-09-23 00:54:11 +00001557lldb::SBBreakpoint
1558SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1559 const SBFileSpecList &module_list,
1560 const lldb::SBFileSpecList &source_file_list)
1561{
Greg Clayton5160ce52013-03-27 23:08:40 +00001562 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001563
1564 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001565 TargetSP target_sp(GetSP());
1566 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001567 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001568 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001569 RegularExpression regexp(source_regex);
Greg Claytonacdbe812012-01-30 09:04:36 +00001570 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
Jim Ingham87df91b2011-09-23 00:54:11 +00001571 }
1572
1573 if (log)
1574 {
1575 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001576 target_sp.get(), source_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001577 }
1578
1579 return sb_bp;
1580}
Jim Ingham969795f2011-09-21 01:17:13 +00001581
Jim Inghamfab10e82012-03-06 00:37:27 +00001582lldb::SBBreakpoint
1583SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1584 bool catch_bp,
1585 bool throw_bp)
1586{
Greg Clayton5160ce52013-03-27 23:08:40 +00001587 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001588
1589 SBBreakpoint sb_bp;
1590 TargetSP target_sp(GetSP());
1591 if (target_sp)
1592 {
1593 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1594 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1595 }
1596
1597 if (log)
1598 {
1599 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1600 target_sp.get(),
1601 LanguageRuntime::GetNameForLanguageType(language),
1602 catch_bp ? "on" : "off",
1603 throw_bp ? "on" : "off",
1604 sb_bp.get());
1605 }
1606
1607 return sb_bp;
1608}
1609
Greg Clayton9fed0d82010-07-23 23:33:17 +00001610uint32_t
1611SBTarget::GetNumBreakpoints () const
1612{
Greg Claytonacdbe812012-01-30 09:04:36 +00001613 TargetSP target_sp(GetSP());
1614 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001615 {
1616 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001617 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001618 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001619 return 0;
1620}
1621
1622SBBreakpoint
1623SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1624{
1625 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001626 TargetSP target_sp(GetSP());
1627 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001628 {
1629 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001630 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001631 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001632 return sb_breakpoint;
1633}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634
1635bool
1636SBTarget::BreakpointDelete (break_id_t bp_id)
1637{
Greg Clayton5160ce52013-03-27 23:08:40 +00001638 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001639
Caroline Ticeceb6b132010-10-26 03:11:13 +00001640 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001641 TargetSP target_sp(GetSP());
1642 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001643 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001644 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1645 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001646 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001647
1648 if (log)
1649 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001650 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001651 }
1652
1653 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654}
1655
Johnny Chen5d043462011-09-26 22:40:50 +00001656SBBreakpoint
1657SBTarget::FindBreakpointByID (break_id_t bp_id)
1658{
Greg Clayton5160ce52013-03-27 23:08:40 +00001659 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001660
1661 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001662 TargetSP target_sp(GetSP());
1663 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001664 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001665 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1666 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001667 }
1668
1669 if (log)
1670 {
1671 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001672 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001673 }
1674
1675 return sb_breakpoint;
1676}
1677
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678bool
1679SBTarget::EnableAllBreakpoints ()
1680{
Greg Claytonacdbe812012-01-30 09:04:36 +00001681 TargetSP target_sp(GetSP());
1682 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001684 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1685 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686 return true;
1687 }
1688 return false;
1689}
1690
1691bool
1692SBTarget::DisableAllBreakpoints ()
1693{
Greg Claytonacdbe812012-01-30 09:04:36 +00001694 TargetSP target_sp(GetSP());
1695 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001696 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001697 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1698 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 return true;
1700 }
1701 return false;
1702}
1703
1704bool
1705SBTarget::DeleteAllBreakpoints ()
1706{
Greg Claytonacdbe812012-01-30 09:04:36 +00001707 TargetSP target_sp(GetSP());
1708 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001709 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001710 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1711 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001712 return true;
1713 }
1714 return false;
1715}
1716
Johnny Chen5d043462011-09-26 22:40:50 +00001717uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001718SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001719{
Greg Claytonacdbe812012-01-30 09:04:36 +00001720 TargetSP target_sp(GetSP());
1721 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001722 {
Johnny Chen01a67862011-10-14 00:42:25 +00001723 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001724 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001725 }
1726 return 0;
1727}
1728
Greg Clayton1b282f92011-10-13 18:08:26 +00001729SBWatchpoint
1730SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001731{
Johnny Chen01a67862011-10-14 00:42:25 +00001732 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001733 TargetSP target_sp(GetSP());
1734 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001735 {
Johnny Chen01a67862011-10-14 00:42:25 +00001736 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001737 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001738 }
Johnny Chen01a67862011-10-14 00:42:25 +00001739 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001740}
1741
1742bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001743SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001744{
Greg Clayton5160ce52013-03-27 23:08:40 +00001745 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001746
1747 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001748 TargetSP target_sp(GetSP());
1749 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001750 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001751 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001752 Mutex::Locker locker;
1753 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001754 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001755 }
1756
1757 if (log)
1758 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001759 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
Johnny Chen5d043462011-09-26 22:40:50 +00001760 }
1761
1762 return result;
1763}
1764
Greg Clayton1b282f92011-10-13 18:08:26 +00001765SBWatchpoint
1766SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001767{
Greg Clayton5160ce52013-03-27 23:08:40 +00001768 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001769
Greg Clayton1b282f92011-10-13 18:08:26 +00001770 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001771 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001772 TargetSP target_sp(GetSP());
1773 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001774 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001775 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001776 Mutex::Locker locker;
1777 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001778 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1779 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001780 }
1781
1782 if (log)
1783 {
Johnny Chen01a67862011-10-14 00:42:25 +00001784 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001785 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001786 }
1787
Greg Clayton1b282f92011-10-13 18:08:26 +00001788 return sb_watchpoint;
1789}
1790
1791lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001792SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001793{
Greg Clayton5160ce52013-03-27 23:08:40 +00001794 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1b282f92011-10-13 18:08:26 +00001795
1796 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001797 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001798 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001799 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001800 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001801 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001802 uint32_t watch_type = 0;
1803 if (read)
1804 watch_type |= LLDB_WATCH_TYPE_READ;
1805 if (write)
1806 watch_type |= LLDB_WATCH_TYPE_WRITE;
Johnny Chen7385a5a2012-05-31 22:56:36 +00001807 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001808 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001809 // This API doesn't take in a type, so we can't figure out what it is.
1810 ClangASTType *type = NULL;
1811 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001812 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001813 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001814 }
1815
1816 if (log)
1817 {
Daniel Malead01b2952012-11-29 21:49:15 +00001818 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001819 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
Greg Clayton1b282f92011-10-13 18:08:26 +00001820 }
1821
1822 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001823}
1824
1825bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001826SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001827{
Greg Claytonacdbe812012-01-30 09:04:36 +00001828 TargetSP target_sp(GetSP());
1829 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001830 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001831 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001832 Mutex::Locker locker;
1833 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001834 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001835 return true;
1836 }
1837 return false;
1838}
1839
1840bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001841SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001842{
Greg Claytonacdbe812012-01-30 09:04:36 +00001843 TargetSP target_sp(GetSP());
1844 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001845 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001846 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001847 Mutex::Locker locker;
1848 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001849 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001850 return true;
1851 }
1852 return false;
1853}
1854
1855bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001856SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001857{
Greg Claytonacdbe812012-01-30 09:04:36 +00001858 TargetSP target_sp(GetSP());
1859 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001860 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001861 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001862 Mutex::Locker locker;
1863 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001864 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001865 return true;
1866 }
1867 return false;
1868}
1869
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001870
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001871lldb::SBModule
1872SBTarget::AddModule (const char *path,
1873 const char *triple,
1874 const char *uuid_cstr)
1875{
Greg Claytonb210aec2012-04-23 20:23:39 +00001876 return AddModule (path, triple, uuid_cstr, NULL);
1877}
1878
1879lldb::SBModule
1880SBTarget::AddModule (const char *path,
1881 const char *triple,
1882 const char *uuid_cstr,
1883 const char *symfile)
1884{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001885 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001886 TargetSP target_sp(GetSP());
1887 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001888 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001889 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001890 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001891 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001892
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001893 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001894 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001895
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001896 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001897 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Greg Claytonb210aec2012-04-23 20:23:39 +00001898
1899 if (symfile)
1900 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001901
Greg Claytonb9a01b32012-02-26 05:51:37 +00001902 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001903 }
1904 return sb_module;
1905}
1906
1907bool
1908SBTarget::AddModule (lldb::SBModule &module)
1909{
Greg Claytonacdbe812012-01-30 09:04:36 +00001910 TargetSP target_sp(GetSP());
1911 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001912 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001913 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001914 return true;
1915 }
1916 return false;
1917}
1918
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001919uint32_t
1920SBTarget::GetNumModules () const
1921{
Greg Clayton5160ce52013-03-27 23:08:40 +00001922 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001923
Caroline Ticeceb6b132010-10-26 03:11:13 +00001924 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00001925 TargetSP target_sp(GetSP());
1926 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001927 {
1928 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001929 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001930 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001931
1932 if (log)
Greg Claytonacdbe812012-01-30 09:04:36 +00001933 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001934
1935 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936}
1937
Greg Clayton48e42542010-07-30 20:12:55 +00001938void
1939SBTarget::Clear ()
1940{
Greg Clayton5160ce52013-03-27 23:08:40 +00001941 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001942
1943 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001944 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001945
Greg Clayton48e42542010-07-30 20:12:55 +00001946 m_opaque_sp.reset();
1947}
1948
1949
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001950SBModule
1951SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1952{
1953 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001954 TargetSP target_sp(GetSP());
1955 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001956 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001957 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001958 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00001959 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00001960 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001961 return sb_module;
1962}
1963
Greg Clayton13d19502012-01-29 06:07:39 +00001964lldb::ByteOrder
1965SBTarget::GetByteOrder ()
1966{
Greg Claytonacdbe812012-01-30 09:04:36 +00001967 TargetSP target_sp(GetSP());
1968 if (target_sp)
1969 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00001970 return eByteOrderInvalid;
1971}
1972
1973const char *
1974SBTarget::GetTriple ()
1975{
Greg Claytonacdbe812012-01-30 09:04:36 +00001976 TargetSP target_sp(GetSP());
1977 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00001978 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001979 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00001980 // Unique the string so we don't run into ownership issues since
1981 // the const strings put the string into the string pool once and
1982 // the strings never comes out
1983 ConstString const_triple (triple.c_str());
1984 return const_triple.GetCString();
1985 }
1986 return NULL;
1987}
1988
1989uint32_t
1990SBTarget::GetAddressByteSize()
1991{
Greg Claytonacdbe812012-01-30 09:04:36 +00001992 TargetSP target_sp(GetSP());
1993 if (target_sp)
1994 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00001995 return sizeof(void*);
1996}
1997
1998
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001999SBModule
2000SBTarget::GetModuleAtIndex (uint32_t idx)
2001{
Greg Clayton5160ce52013-03-27 23:08:40 +00002002 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002003
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002005 ModuleSP module_sp;
2006 TargetSP target_sp(GetSP());
2007 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002008 {
2009 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002010 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2011 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002012 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002013
2014 if (log)
2015 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00002016 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002017 target_sp.get(), idx, module_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002018 }
2019
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002020 return sb_module;
2021}
2022
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002023bool
2024SBTarget::RemoveModule (lldb::SBModule module)
2025{
Greg Claytonacdbe812012-01-30 09:04:36 +00002026 TargetSP target_sp(GetSP());
2027 if (target_sp)
2028 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002029 return false;
2030}
2031
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002032
2033SBBroadcaster
2034SBTarget::GetBroadcaster () const
2035{
Greg Clayton5160ce52013-03-27 23:08:40 +00002036 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002037
Greg Claytonacdbe812012-01-30 09:04:36 +00002038 TargetSP target_sp(GetSP());
2039 SBBroadcaster broadcaster(target_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002040
2041 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002042 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002043 target_sp.get(), broadcaster.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002045 return broadcaster;
2046}
2047
Caroline Ticedde9cff2010-09-20 05:20:02 +00002048bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002049SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002050{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002051 Stream &strm = description.ref();
2052
Greg Claytonacdbe812012-01-30 09:04:36 +00002053 TargetSP target_sp(GetSP());
2054 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002055 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002056 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002057 }
2058 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002059 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002060
2061 return true;
2062}
2063
Greg Clayton5569e642012-02-06 01:44:54 +00002064lldb::SBSymbolContextList
2065SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002066{
Greg Clayton5569e642012-02-06 01:44:54 +00002067 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002068 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002069 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002070 TargetSP target_sp(GetSP());
2071 if (target_sp)
2072 {
2073 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002074 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002075 const bool append = true;
2076 target_sp->GetImages().FindFunctions (ConstString(name),
2077 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002078 symbols_ok,
2079 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002080 append,
2081 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002082 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002083 }
Greg Clayton5569e642012-02-06 01:44:54 +00002084 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002085}
2086
Enrico Granata6f3533f2011-07-29 19:53:35 +00002087lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002088SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002089{
Greg Claytonacdbe812012-01-30 09:04:36 +00002090 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002091 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002092 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002093 ConstString const_typename(typename_cstr);
2094 SymbolContext sc;
2095 const bool exact_match = false;
2096
2097 const ModuleList &module_list = target_sp->GetImages();
2098 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002099 for (size_t idx = 0; idx < count; idx++)
2100 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002101 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2102 if (module_sp)
2103 {
2104 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2105 if (type_sp)
2106 return SBType(type_sp);
2107 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002108 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002109
2110 // Didn't find the type in the symbols; try the Objective-C runtime
2111 // if one is installed
2112
2113 ProcessSP process_sp(target_sp->GetProcessSP());
2114
2115 if (process_sp)
2116 {
2117 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2118
2119 if (objc_language_runtime)
2120 {
2121 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2122
2123 if (objc_type_vendor)
2124 {
2125 std::vector <ClangASTType> types;
2126
2127 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2128 return SBType(types[0]);
2129 }
2130 }
2131 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002132
2133 // No matches, search for basic typename matches
2134 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2135 if (clang_ast)
2136 return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002137 }
2138 return SBType();
2139}
2140
Greg Claytonb43165b2012-12-05 21:24:42 +00002141SBType
2142SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002143{
Greg Claytonacdbe812012-01-30 09:04:36 +00002144 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002145 if (target_sp)
2146 {
2147 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2148 if (clang_ast)
2149 return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), type));
2150 }
2151 return SBType();
2152}
2153
2154
2155lldb::SBTypeList
2156SBTarget::FindTypes (const char* typename_cstr)
2157{
2158 SBTypeList sb_type_list;
2159 TargetSP target_sp(GetSP());
2160 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002161 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002162 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002163 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002164 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002165 SymbolContext sc;
2166 TypeList type_list;
2167
Greg Clayton29399a22012-04-06 17:41:13 +00002168 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002169 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002170 exact_match,
2171 UINT32_MAX,
2172 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002173
Greg Claytonb43165b2012-12-05 21:24:42 +00002174 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002175 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002176 for (size_t idx = 0; idx < num_matches; idx++)
2177 {
2178 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2179 if (type_sp)
2180 sb_type_list.Append(SBType(type_sp));
2181 }
2182 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002183
2184 // Try the Objective-C runtime if one is installed
2185
2186 ProcessSP process_sp(target_sp->GetProcessSP());
2187
2188 if (process_sp)
2189 {
2190 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2191
2192 if (objc_language_runtime)
2193 {
2194 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2195
2196 if (objc_type_vendor)
2197 {
2198 std::vector <ClangASTType> types;
2199
2200 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2201 {
2202 for (ClangASTType &type : types)
2203 {
2204 sb_type_list.Append(SBType(type));
2205 }
2206 }
2207 }
2208 }
2209 }
2210
2211 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002212 {
2213 // No matches, search for basic typename matches
2214 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2215 if (clang_ast)
2216 sb_type_list.Append (SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002217 }
2218 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002219 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002220}
2221
Greg Claytondea8cb42011-06-29 22:09:02 +00002222SBValueList
2223SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2224{
2225 SBValueList sb_value_list;
2226
Greg Claytonacdbe812012-01-30 09:04:36 +00002227 TargetSP target_sp(GetSP());
2228 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002229 {
2230 VariableList variable_list;
2231 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002232 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2233 append,
2234 max_matches,
2235 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002236
2237 if (match_count > 0)
2238 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002239 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002240 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002241 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002242 for (uint32_t i=0; i<match_count; ++i)
2243 {
2244 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2245 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002246 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002247 }
2248 }
2249 }
2250
2251 return sb_value_list;
2252}
2253
Enrico Granatabcd80b42013-01-16 18:53:52 +00002254lldb::SBValue
2255SBTarget::FindFirstGlobalVariable (const char* name)
2256{
2257 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2258 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2259 return sb_value_list.GetValueAtIndex(0);
2260 return SBValue();
2261}
2262
Jim Inghame37d6052011-09-13 00:29:56 +00002263SBSourceManager
2264SBTarget::GetSourceManager()
2265{
2266 SBSourceManager source_manager (*this);
2267 return source_manager;
2268}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002269
Sean Callanan50952e92011-12-14 23:49:37 +00002270lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002271SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2272{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002273 return ReadInstructions (base_addr, count, NULL);
2274}
2275
2276lldb::SBInstructionList
2277SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2278{
Greg Clayton9c766112012-03-06 22:24:44 +00002279 SBInstructionList sb_instructions;
2280
2281 TargetSP target_sp(GetSP());
2282 if (target_sp)
2283 {
2284 Address *addr_ptr = base_addr.get();
2285
2286 if (addr_ptr)
2287 {
2288 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2289 bool prefer_file_cache = false;
2290 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002291 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2292 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2293 prefer_file_cache,
2294 data.GetBytes(),
2295 data.GetByteSize(),
2296 error,
2297 &load_addr);
2298 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002299 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2300 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002301 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002302 *addr_ptr,
2303 data.GetBytes(),
2304 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002305 count,
2306 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002307 }
2308 }
2309
2310 return sb_instructions;
2311
2312}
2313
2314lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002315SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2316{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002317 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2318}
2319
2320lldb::SBInstructionList
2321SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2322{
Sean Callanan50952e92011-12-14 23:49:37 +00002323 SBInstructionList sb_instructions;
2324
Greg Claytonacdbe812012-01-30 09:04:36 +00002325 TargetSP target_sp(GetSP());
2326 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002327 {
2328 Address addr;
2329
2330 if (base_addr.get())
2331 addr = *base_addr.get();
2332
Greg Clayton3faf47c2013-03-28 23:42:53 +00002333 const bool data_from_file = true;
2334
Greg Claytonacdbe812012-01-30 09:04:36 +00002335 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002336 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002337 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002338 addr,
2339 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002340 size,
2341 UINT32_MAX,
2342 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002343 }
2344
2345 return sb_instructions;
2346}
2347
2348lldb::SBInstructionList
2349SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2350{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002351 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2352}
2353
2354lldb::SBInstructionList
2355SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2356{
2357 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002358}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002359
2360SBError
2361SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2362 lldb::addr_t section_base_addr)
2363{
2364 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002365 TargetSP target_sp(GetSP());
2366 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002367 {
2368 if (!section.IsValid())
2369 {
2370 sb_error.SetErrorStringWithFormat ("invalid section");
2371 }
2372 else
2373 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002374 SectionSP section_sp (section.GetSP());
2375 if (section_sp)
2376 {
2377 if (section_sp->IsThreadSpecific())
2378 {
2379 sb_error.SetErrorString ("thread specific sections are not yet supported");
2380 }
2381 else
2382 {
Greg Clayton3c947372013-01-29 01:17:09 +00002383 if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2384 {
2385 // Flush info in the process (stack frames, etc)
2386 ProcessSP process_sp (target_sp->GetProcessSP());
2387 if (process_sp)
2388 process_sp->Flush();
2389 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002390 }
2391 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002392 }
2393 }
2394 else
2395 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002396 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002397 }
2398 return sb_error;
2399}
2400
2401SBError
2402SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2403{
2404 SBError sb_error;
2405
Greg Claytonacdbe812012-01-30 09:04:36 +00002406 TargetSP target_sp(GetSP());
2407 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002408 {
2409 if (!section.IsValid())
2410 {
2411 sb_error.SetErrorStringWithFormat ("invalid section");
2412 }
2413 else
2414 {
Greg Clayton3c947372013-01-29 01:17:09 +00002415 if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2416 {
2417 // Flush info in the process (stack frames, etc)
2418 ProcessSP process_sp (target_sp->GetProcessSP());
2419 if (process_sp)
2420 process_sp->Flush();
2421 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002422 }
2423 }
2424 else
2425 {
2426 sb_error.SetErrorStringWithFormat ("invalid target");
2427 }
2428 return sb_error;
2429}
2430
2431SBError
2432SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2433{
2434 SBError sb_error;
2435
Greg Claytonacdbe812012-01-30 09:04:36 +00002436 TargetSP target_sp(GetSP());
2437 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002438 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002439 ModuleSP module_sp (module.GetSP());
2440 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002441 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002442 bool changed = false;
2443 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002444 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002445 // The load was successful, make sure that at least some sections
2446 // changed before we notify that our module was loaded.
2447 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002448 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002449 ModuleList module_list;
2450 module_list.Append(module_sp);
2451 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002452 // Flush info in the process (stack frames, etc)
2453 ProcessSP process_sp (target_sp->GetProcessSP());
2454 if (process_sp)
2455 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002456 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002457 }
2458 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002459 else
2460 {
2461 sb_error.SetErrorStringWithFormat ("invalid module");
2462 }
2463
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002464 }
2465 else
2466 {
2467 sb_error.SetErrorStringWithFormat ("invalid target");
2468 }
2469 return sb_error;
2470}
2471
2472SBError
2473SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2474{
2475 SBError sb_error;
2476
2477 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002478 TargetSP target_sp(GetSP());
2479 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002480 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002481 ModuleSP module_sp (module.GetSP());
2482 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002483 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002484 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002485 if (objfile)
2486 {
2487 SectionList *section_list = objfile->GetSectionList();
2488 if (section_list)
2489 {
Greg Clayton3c947372013-01-29 01:17:09 +00002490 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002491 const size_t num_sections = section_list->GetSize();
2492 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2493 {
2494 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2495 if (section_sp)
Greg Clayton3c947372013-01-29 01:17:09 +00002496 changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2497 }
2498 if (changed)
2499 {
2500 // Flush info in the process (stack frames, etc)
2501 ProcessSP process_sp (target_sp->GetProcessSP());
2502 if (process_sp)
2503 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002504 }
2505 }
2506 else
2507 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002508 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002509 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2510 }
2511 }
2512 else
2513 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002514 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002515 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2516 }
2517 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002518 else
2519 {
2520 sb_error.SetErrorStringWithFormat ("invalid module");
2521 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002522 }
2523 else
2524 {
2525 sb_error.SetErrorStringWithFormat ("invalid target");
2526 }
2527 return sb_error;
2528}
2529
2530
Greg Claytone14e1922012-12-04 02:22:16 +00002531lldb::SBSymbolContextList
2532SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2533{
2534 SBSymbolContextList sb_sc_list;
2535 if (name && name[0])
2536 {
2537 TargetSP target_sp(GetSP());
2538 if (target_sp)
2539 {
2540 bool append = true;
2541 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2542 symbol_type,
2543 *sb_sc_list,
2544 append);
2545 }
2546 }
2547 return sb_sc_list;
2548
2549}
2550
2551
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002552lldb::SBValue
2553SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2554{
Greg Clayton5160ce52013-03-27 23:08:40 +00002555 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2556 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002557 SBValue expr_result;
2558 ExecutionResults exe_results = eExecutionSetupError;
2559 ValueObjectSP expr_value_sp;
2560 TargetSP target_sp(GetSP());
2561 StackFrame *frame = NULL;
2562 if (target_sp)
2563 {
2564 if (expr == NULL || expr[0] == '\0')
2565 {
2566 if (log)
2567 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2568 return expr_result;
2569 }
2570
2571 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2572 ExecutionContext exe_ctx (m_opaque_sp.get());
2573
2574 if (log)
2575 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2576
2577 frame = exe_ctx.GetFramePtr();
2578 Target *target = exe_ctx.GetTargetPtr();
2579
2580 if (target)
2581 {
2582#ifdef LLDB_CONFIGURATION_DEBUG
2583 StreamString frame_description;
2584 if (frame)
2585 frame->DumpUsingSettingsFormat (&frame_description);
2586 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2587 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2588#endif
2589 exe_results = target->EvaluateExpression (expr,
2590 frame,
2591 expr_value_sp,
2592 options.ref());
2593
2594 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2595#ifdef LLDB_CONFIGURATION_DEBUG
2596 Host::SetCrashDescription (NULL);
2597#endif
2598 }
2599 else
2600 {
2601 if (log)
2602 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2603 }
2604 }
2605#ifndef LLDB_DISABLE_PYTHON
2606 if (expr_log)
2607 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2608 expr_result.GetValue(),
2609 expr_result.GetSummary());
2610
2611 if (log)
2612 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2613 frame,
2614 expr,
2615 expr_value_sp.get(),
2616 exe_results);
2617#endif
2618
2619 return expr_result;
2620}
2621
Greg Clayton13fbb992013-02-01 00:47:49 +00002622
2623lldb::addr_t
2624SBTarget::GetStackRedZoneSize()
2625{
2626 TargetSP target_sp(GetSP());
2627 if (target_sp)
2628 {
2629 ABISP abi_sp;
2630 ProcessSP process_sp (target_sp->GetProcessSP());
2631 if (process_sp)
2632 abi_sp = process_sp->GetABI();
2633 else
2634 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2635 if (abi_sp)
2636 return abi_sp->GetRedZoneSize();
2637 }
2638 return 0;
2639}
2640