blob: c8bc2171436d6b97862cee467bc7796e05da5408 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Eli Friedman4c5de692010-06-09 07:44:37 +000012#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
Greg Claytone0d378b2011-03-24 21:19:54 +000014#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Greg Claytondea8cb42011-06-29 22:09:02 +000016#include "lldb/API/SBDebugger.h"
17#include "lldb/API/SBBreakpoint.h"
Greg Clayton4b63a5c2013-01-04 18:10:18 +000018#include "lldb/API/SBExpressionOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/API/SBFileSpec.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000020#include "lldb/API/SBListener.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/API/SBModule.h"
Greg Clayton226cce22013-07-08 22:22:41 +000022#include "lldb/API/SBModuleSpec.h"
Jim Inghame37d6052011-09-13 00:29:56 +000023#include "lldb/API/SBSourceManager.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000024#include "lldb/API/SBProcess.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000025#include "lldb/API/SBStream.h"
Greg Claytonfe356d32011-06-21 01:34:41 +000026#include "lldb/API/SBSymbolContextList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Breakpoint/BreakpointID.h"
28#include "lldb/Breakpoint/BreakpointIDList.h"
29#include "lldb/Breakpoint/BreakpointList.h"
30#include "lldb/Breakpoint/BreakpointLocation.h"
31#include "lldb/Core/Address.h"
32#include "lldb/Core/AddressResolver.h"
33#include "lldb/Core/AddressResolverName.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/ArchSpec.h"
35#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000037#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000038#include "lldb/Core/Module.h"
39#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Core/RegularExpression.h"
41#include "lldb/Core/SearchFilter.h"
Greg Clayton1f746072012-08-29 21:13:06 +000042#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Core/STLUtils.h"
Enrico Granata347c2aa2013-10-08 21:49:02 +000044#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000045#include "lldb/Core/ValueObjectList.h"
46#include "lldb/Core/ValueObjectVariable.h"
47#include "lldb/Host/FileSpec.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000048#include "lldb/Host/Host.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000049#include "lldb/Interpreter/Args.h"
Greg Clayton1f746072012-08-29 21:13:06 +000050#include "lldb/Symbol/ObjectFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000051#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000052#include "lldb/Symbol/VariableList.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000053#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "lldb/Target/Process.h"
55#include "lldb/Target/Target.h"
56#include "lldb/Target/TargetList.h"
57
58#include "lldb/Interpreter/CommandReturnObject.h"
59#include "../source/Commands/CommandObjectBreakpoint.h"
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
62using namespace lldb;
63using namespace lldb_private;
64
65#define DEFAULT_DISASM_BYTE_SIZE 32
66
Greg Clayton38d1f052012-02-24 20:59:25 +000067SBLaunchInfo::SBLaunchInfo (const char **argv) :
68 m_opaque_sp(new ProcessLaunchInfo())
Greg Clayton0e615682012-02-24 05:03:03 +000069{
Greg Clayton38d1f052012-02-24 20:59:25 +000070 m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
71 if (argv && argv[0])
72 m_opaque_sp->GetArguments().SetArguments(argv);
Greg Clayton0e615682012-02-24 05:03:03 +000073}
74
Greg Claytonecc7c0d2012-03-07 23:52:51 +000075SBLaunchInfo::~SBLaunchInfo()
76{
77}
78
79lldb_private::ProcessLaunchInfo &
80SBLaunchInfo::ref ()
81{
82 return *m_opaque_sp;
83}
84
85
Greg Clayton0e615682012-02-24 05:03:03 +000086uint32_t
87SBLaunchInfo::GetUserID()
88{
89 return m_opaque_sp->GetUserID();
90}
91
92uint32_t
93SBLaunchInfo::GetGroupID()
94{
95 return m_opaque_sp->GetGroupID();
96}
97
98bool
99SBLaunchInfo::UserIDIsValid ()
100{
101 return m_opaque_sp->UserIDIsValid();
102}
103
104bool
105SBLaunchInfo::GroupIDIsValid ()
106{
107 return m_opaque_sp->GroupIDIsValid();
108}
109
110void
111SBLaunchInfo::SetUserID (uint32_t uid)
112{
113 m_opaque_sp->SetUserID (uid);
114}
115
116void
117SBLaunchInfo::SetGroupID (uint32_t gid)
118{
119 m_opaque_sp->SetGroupID (gid);
120}
121
Greg Clayton0e615682012-02-24 05:03:03 +0000122uint32_t
123SBLaunchInfo::GetNumArguments ()
124{
125 return m_opaque_sp->GetArguments().GetArgumentCount();
126}
127
128const char *
129SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
130{
131 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
132}
133
134void
135SBLaunchInfo::SetArguments (const char **argv, bool append)
136{
137 if (append)
138 {
139 if (argv)
140 m_opaque_sp->GetArguments().AppendArguments(argv);
141 }
142 else
143 {
144 if (argv)
145 m_opaque_sp->GetArguments().SetArguments(argv);
146 else
147 m_opaque_sp->GetArguments().Clear();
148 }
149}
150
151uint32_t
152SBLaunchInfo::GetNumEnvironmentEntries ()
153{
154 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
155}
156
157const char *
158SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
159{
160 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
161}
162
163void
164SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
165{
166 if (append)
167 {
168 if (envp)
169 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
170 }
171 else
172 {
173 if (envp)
174 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
175 else
176 m_opaque_sp->GetEnvironmentEntries().Clear();
177 }
178}
179
180void
181SBLaunchInfo::Clear ()
182{
183 m_opaque_sp->Clear();
184}
185
186const char *
187SBLaunchInfo::GetWorkingDirectory () const
188{
189 return m_opaque_sp->GetWorkingDirectory();
190}
191
192void
193SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
194{
195 m_opaque_sp->SetWorkingDirectory(working_dir);
196}
197
198uint32_t
199SBLaunchInfo::GetLaunchFlags ()
200{
201 return m_opaque_sp->GetFlags().Get();
202}
203
204void
205SBLaunchInfo::SetLaunchFlags (uint32_t flags)
206{
207 m_opaque_sp->GetFlags().Reset(flags);
208}
209
210const char *
211SBLaunchInfo::GetProcessPluginName ()
212{
213 return m_opaque_sp->GetProcessPluginName();
214}
215
216void
217SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
218{
219 return m_opaque_sp->SetProcessPluginName (plugin_name);
220}
221
222const char *
223SBLaunchInfo::GetShell ()
224{
225 return m_opaque_sp->GetShell();
226}
227
228void
229SBLaunchInfo::SetShell (const char * path)
230{
231 m_opaque_sp->SetShell (path);
232}
233
234uint32_t
235SBLaunchInfo::GetResumeCount ()
236{
237 return m_opaque_sp->GetResumeCount();
238}
239
240void
241SBLaunchInfo::SetResumeCount (uint32_t c)
242{
243 m_opaque_sp->SetResumeCount (c);
244}
245
246bool
247SBLaunchInfo::AddCloseFileAction (int fd)
248{
249 return m_opaque_sp->AppendCloseFileAction(fd);
250}
251
252bool
253SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
254{
255 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
256}
257
258bool
259SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
260{
261 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
262}
263
264bool
265SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
266{
267 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
268}
269
270
271SBAttachInfo::SBAttachInfo () :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000272 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000273{
274}
275
276SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000277 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000278{
279 m_opaque_sp->SetProcessID (pid);
280}
281
282SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000283 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000284{
285 if (path && path[0])
286 m_opaque_sp->GetExecutableFile().SetFile(path, false);
287 m_opaque_sp->SetWaitForLaunch (wait_for);
288}
289
290SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000291 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000292{
293 *m_opaque_sp = *rhs.m_opaque_sp;
294}
295
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000296SBAttachInfo::~SBAttachInfo()
297{
298}
299
300lldb_private::ProcessAttachInfo &
301SBAttachInfo::ref ()
302{
303 return *m_opaque_sp;
304}
305
Greg Clayton0e615682012-02-24 05:03:03 +0000306SBAttachInfo &
307SBAttachInfo::operator = (const SBAttachInfo &rhs)
308{
309 if (this != &rhs)
310 *m_opaque_sp = *rhs.m_opaque_sp;
311 return *this;
312}
313
314lldb::pid_t
315SBAttachInfo::GetProcessID ()
316{
317 return m_opaque_sp->GetProcessID();
318}
319
320void
321SBAttachInfo::SetProcessID (lldb::pid_t pid)
322{
323 m_opaque_sp->SetProcessID (pid);
324}
325
326
327uint32_t
328SBAttachInfo::GetResumeCount ()
329{
330 return m_opaque_sp->GetResumeCount();
331}
332
333void
334SBAttachInfo::SetResumeCount (uint32_t c)
335{
336 m_opaque_sp->SetResumeCount (c);
337}
338
339const char *
340SBAttachInfo::GetProcessPluginName ()
341{
342 return m_opaque_sp->GetProcessPluginName();
343}
344
345void
346SBAttachInfo::SetProcessPluginName (const char *plugin_name)
347{
348 return m_opaque_sp->SetProcessPluginName (plugin_name);
349}
350
351void
352SBAttachInfo::SetExecutable (const char *path)
353{
354 if (path && path[0])
355 m_opaque_sp->GetExecutableFile().SetFile(path, false);
356 else
357 m_opaque_sp->GetExecutableFile().Clear();
358}
359
360void
361SBAttachInfo::SetExecutable (SBFileSpec exe_file)
362{
363 if (exe_file.IsValid())
364 m_opaque_sp->GetExecutableFile() = exe_file.ref();
365 else
366 m_opaque_sp->GetExecutableFile().Clear();
367}
368
369bool
370SBAttachInfo::GetWaitForLaunch ()
371{
372 return m_opaque_sp->GetWaitForLaunch();
373}
374
375void
376SBAttachInfo::SetWaitForLaunch (bool b)
377{
378 m_opaque_sp->SetWaitForLaunch (b);
379}
380
Jim Inghamcd16df92012-07-20 21:37:13 +0000381bool
382SBAttachInfo::GetIgnoreExisting ()
383{
384 return m_opaque_sp->GetIgnoreExisting();
385}
386
387void
388SBAttachInfo::SetIgnoreExisting (bool b)
389{
390 m_opaque_sp->SetIgnoreExisting (b);
391}
392
Greg Clayton0e615682012-02-24 05:03:03 +0000393uint32_t
Greg Clayton41bd8ac2012-02-24 23:56:06 +0000394SBAttachInfo::GetUserID()
395{
396 return m_opaque_sp->GetUserID();
397}
398
399uint32_t
400SBAttachInfo::GetGroupID()
401{
402 return m_opaque_sp->GetGroupID();
403}
404
405bool
406SBAttachInfo::UserIDIsValid ()
407{
408 return m_opaque_sp->UserIDIsValid();
409}
410
411bool
412SBAttachInfo::GroupIDIsValid ()
413{
414 return m_opaque_sp->GroupIDIsValid();
415}
416
417void
418SBAttachInfo::SetUserID (uint32_t uid)
419{
420 m_opaque_sp->SetUserID (uid);
421}
422
423void
424SBAttachInfo::SetGroupID (uint32_t gid)
425{
426 m_opaque_sp->SetGroupID (gid);
427}
428
429uint32_t
Greg Clayton0e615682012-02-24 05:03:03 +0000430SBAttachInfo::GetEffectiveUserID()
431{
432 return m_opaque_sp->GetEffectiveUserID();
433}
434
435uint32_t
436SBAttachInfo::GetEffectiveGroupID()
437{
438 return m_opaque_sp->GetEffectiveGroupID();
439}
440
441bool
442SBAttachInfo::EffectiveUserIDIsValid ()
443{
444 return m_opaque_sp->EffectiveUserIDIsValid();
445}
446
447bool
448SBAttachInfo::EffectiveGroupIDIsValid ()
449{
450 return m_opaque_sp->EffectiveGroupIDIsValid ();
451}
452
453void
454SBAttachInfo::SetEffectiveUserID (uint32_t uid)
455{
456 m_opaque_sp->SetEffectiveUserID(uid);
457}
458
459void
460SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
461{
462 m_opaque_sp->SetEffectiveGroupID(gid);
463}
464
465lldb::pid_t
466SBAttachInfo::GetParentProcessID ()
467{
468 return m_opaque_sp->GetParentProcessID();
469}
470
471void
472SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
473{
474 m_opaque_sp->SetParentProcessID (pid);
475}
476
477bool
478SBAttachInfo::ParentProcessIDIsValid()
479{
480 return m_opaque_sp->ParentProcessIDIsValid();
481}
482
483
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484//----------------------------------------------------------------------
485// SBTarget constructor
486//----------------------------------------------------------------------
Greg Clayton54979cd2010-12-15 05:08:08 +0000487SBTarget::SBTarget () :
488 m_opaque_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489{
490}
491
492SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton66111032010-06-23 01:19:29 +0000493 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494{
495}
496
497SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton66111032010-06-23 01:19:29 +0000498 m_opaque_sp (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499{
500}
501
Greg Claytonefabb122010-11-05 23:17:00 +0000502const SBTarget&
503SBTarget::operator = (const SBTarget& rhs)
504{
505 if (this != &rhs)
506 m_opaque_sp = rhs.m_opaque_sp;
507 return *this;
508}
509
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510//----------------------------------------------------------------------
511// Destructor
512//----------------------------------------------------------------------
513SBTarget::~SBTarget()
514{
515}
516
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000517const char *
518SBTarget::GetBroadcasterClassName ()
519{
520 return Target::GetStaticBroadcasterClass().AsCString();
521}
522
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523bool
524SBTarget::IsValid () const
525{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000526 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527}
528
529SBProcess
530SBTarget::GetProcess ()
531{
532 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000533 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000534 TargetSP target_sp(GetSP());
535 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000536 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000537 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000538 sb_process.SetSP (process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000539 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000540
Greg Clayton5160ce52013-03-27 23:08:40 +0000541 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000542 if (log)
543 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000544 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +0000545 target_sp.get(), process_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000546 }
547
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548 return sb_process;
549}
550
Greg Clayton66111032010-06-23 01:19:29 +0000551SBDebugger
552SBTarget::GetDebugger () const
553{
554 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000555 TargetSP target_sp(GetSP());
556 if (target_sp)
557 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000558 return debugger;
559}
560
Jim Ingham270684d2011-03-31 00:01:24 +0000561SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000562SBTarget::LoadCore (const char *core_file)
563{
564 SBProcess sb_process;
565 TargetSP target_sp(GetSP());
566 if (target_sp)
567 {
568 FileSpec filespec(core_file, true);
569 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
570 NULL,
571 &filespec));
572 if (process_sp)
573 {
574 process_sp->LoadCore();
575 sb_process.SetSP (process_sp);
576 }
577 }
578 return sb_process;
579}
580
581SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000582SBTarget::LaunchSimple
583(
584 char const **argv,
585 char const **envp,
586 const char *working_directory
587)
588{
589 char *stdin_path = NULL;
590 char *stdout_path = NULL;
591 char *stderr_path = NULL;
592 uint32_t launch_flags = 0;
593 bool stop_at_entry = false;
594 SBError error;
595 SBListener listener = GetDebugger().GetListener();
596 return Launch (listener,
597 argv,
598 envp,
599 stdin_path,
600 stdout_path,
601 stderr_path,
602 working_directory,
603 launch_flags,
604 stop_at_entry,
605 error);
606}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000607
Greg Claytonfbb76342013-11-20 21:07:01 +0000608SBError
609SBTarget::Install()
610{
611 SBError sb_error;
612 TargetSP target_sp(GetSP());
613 if (target_sp)
614 {
615 Mutex::Locker api_locker (target_sp->GetAPIMutex());
616 sb_error.ref() = target_sp->Install(NULL);
617 }
618 return sb_error;
619}
620
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000621SBProcess
622SBTarget::Launch
623(
Greg Clayton4b045622011-02-03 21:28:34 +0000624 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000625 char const **argv,
626 char const **envp,
627 const char *stdin_path,
628 const char *stdout_path,
629 const char *stderr_path,
630 const char *working_directory,
631 uint32_t launch_flags, // See LaunchFlags
632 bool stop_at_entry,
633 lldb::SBError& error
634)
635{
Greg Clayton5160ce52013-03-27 23:08:40 +0000636 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000637
Greg Claytonacdbe812012-01-30 09:04:36 +0000638 SBProcess sb_process;
639 ProcessSP process_sp;
640 TargetSP target_sp(GetSP());
641
Caroline Ticeceb6b132010-10-26 03:11:13 +0000642 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +0000643 {
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000644 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 +0000645 target_sp.get(),
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000646 argv,
647 envp,
648 stdin_path ? stdin_path : "NULL",
649 stdout_path ? stdout_path : "NULL",
650 stderr_path ? stderr_path : "NULL",
651 working_directory ? working_directory : "NULL",
652 launch_flags,
653 stop_at_entry,
654 error.get());
Greg Clayton93aa84e2010-10-29 04:59:35 +0000655 }
Greg Claytonacdbe812012-01-30 09:04:36 +0000656
657 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000659 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000660
Greg Clayton645bf542011-01-27 01:01:10 +0000661 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
662 launch_flags |= eLaunchFlagDisableASLR;
663
Greg Clayton2289fa42011-04-30 01:09:13 +0000664 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000665 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000666 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000667 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000668 state = process_sp->GetState();
Greg Clayton2289fa42011-04-30 01:09:13 +0000669
Greg Claytonb9556ac2012-01-30 07:41:31 +0000670 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton2289fa42011-04-30 01:09:13 +0000671 {
672 if (state == eStateAttaching)
673 error.SetErrorString ("process attach is in progress");
674 else
675 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000676 return sb_process;
677 }
Greg Clayton931180e2011-01-27 06:44:37 +0000678 }
679
Greg Clayton2289fa42011-04-30 01:09:13 +0000680 if (state == eStateConnected)
681 {
682 // If we are already connected, then we have already specified the
683 // listener, so if a valid listener is supplied, we need to error out
684 // to let the client know.
685 if (listener.IsValid())
686 {
687 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000688 return sb_process;
689 }
690 }
691 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000692 {
Greg Clayton4b045622011-02-03 21:28:34 +0000693 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000694 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton4b045622011-02-03 21:28:34 +0000695 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000696 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton2289fa42011-04-30 01:09:13 +0000697 }
Greg Clayton645bf542011-01-27 01:01:10 +0000698
Greg Claytonb9556ac2012-01-30 07:41:31 +0000699 if (process_sp)
Greg Clayton2289fa42011-04-30 01:09:13 +0000700 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000701 sb_process.SetSP (process_sp);
Greg Clayton2289fa42011-04-30 01:09:13 +0000702 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
703 launch_flags |= eLaunchFlagDisableSTDIO;
704
Greg Clayton982c9762011-11-03 21:22:33 +0000705 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
706
Greg Claytonacdbe812012-01-30 09:04:36 +0000707 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Clayton982c9762011-11-03 21:22:33 +0000708 if (exe_module)
Greg Claytonad9e8282011-11-29 04:03:30 +0000709 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton982c9762011-11-03 21:22:33 +0000710 if (argv)
711 launch_info.GetArguments().AppendArguments (argv);
712 if (envp)
713 launch_info.GetEnvironmentEntries ().SetArguments (envp);
714
Greg Claytonb9556ac2012-01-30 07:41:31 +0000715 error.SetError (process_sp->Launch (launch_info));
Greg Clayton2289fa42011-04-30 01:09:13 +0000716 if (error.Success())
Greg Clayton645bf542011-01-27 01:01:10 +0000717 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000718 // We we are stopping at the entry point, we can return now!
719 if (stop_at_entry)
720 return sb_process;
Greg Clayton645bf542011-01-27 01:01:10 +0000721
Greg Clayton2289fa42011-04-30 01:09:13 +0000722 // Make sure we are stopped at the entry
Greg Claytonb9556ac2012-01-30 07:41:31 +0000723 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton2289fa42011-04-30 01:09:13 +0000724 if (state == eStateStopped)
Greg Clayton645bf542011-01-27 01:01:10 +0000725 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000726 // resume the process to skip the entry point
Greg Claytonb9556ac2012-01-30 07:41:31 +0000727 error.SetError (process_sp->Resume());
Greg Clayton2289fa42011-04-30 01:09:13 +0000728 if (error.Success())
Greg Clayton645bf542011-01-27 01:01:10 +0000729 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000730 // If we are doing synchronous mode, then wait for the
731 // process to stop yet again!
Greg Claytonacdbe812012-01-30 09:04:36 +0000732 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +0000733 process_sp->WaitForProcessToStop (NULL);
Greg Clayton645bf542011-01-27 01:01:10 +0000734 }
735 }
736 }
Greg Clayton2289fa42011-04-30 01:09:13 +0000737 }
738 else
739 {
740 error.SetErrorString ("unable to create lldb_private::Process");
Greg Clayton524e60b2010-10-06 22:10:17 +0000741 }
742 }
743 else
744 {
745 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000747
Caroline Tice20ad3c42010-10-29 21:48:37 +0000748 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000749 if (log)
750 {
Sean Callanan575a4542012-10-20 00:21:31 +0000751 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +0000752 target_sp.get(), process_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000753 }
754
Greg Clayton5d5028b2010-10-06 03:53:16 +0000755 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756}
757
Greg Clayton0e615682012-02-24 05:03:03 +0000758SBProcess
759SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
760{
Greg Clayton5160ce52013-03-27 23:08:40 +0000761 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton0e615682012-02-24 05:03:03 +0000762
763 SBProcess sb_process;
764 ProcessSP process_sp;
765 TargetSP target_sp(GetSP());
766
767 if (log)
768 {
769 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
770 }
771
772 if (target_sp)
773 {
774 Mutex::Locker api_locker (target_sp->GetAPIMutex());
775 StateType state = eStateInvalid;
776 process_sp = target_sp->GetProcessSP();
777 if (process_sp)
778 {
779 state = process_sp->GetState();
780
781 if (process_sp->IsAlive() && state != eStateConnected)
782 {
783 if (state == eStateAttaching)
784 error.SetErrorString ("process attach is in progress");
785 else
786 error.SetErrorString ("a process is already being debugged");
787 return sb_process;
788 }
789 }
790
791 if (state != eStateConnected)
792 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
793
794 if (process_sp)
795 {
796 sb_process.SetSP (process_sp);
797 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
Greg Clayton38d1f052012-02-24 20:59:25 +0000798
Greg Clayton38d1f052012-02-24 20:59:25 +0000799 Module *exe_module = target_sp->GetExecutableModulePointer();
800 if (exe_module)
Han Ming Ong04cf1ba2012-03-02 01:02:04 +0000801 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton38d1f052012-02-24 20:59:25 +0000802
803 const ArchSpec &arch_spec = target_sp->GetArchitecture();
804 if (arch_spec.IsValid())
805 launch_info.GetArchitecture () = arch_spec;
806
Greg Clayton0e615682012-02-24 05:03:03 +0000807 error.SetError (process_sp->Launch (launch_info));
Greg Clayton38d1f052012-02-24 20:59:25 +0000808 const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
Greg Clayton0e615682012-02-24 05:03:03 +0000809 if (error.Success())
810 {
Greg Clayton0e615682012-02-24 05:03:03 +0000811 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Clayton38d1f052012-02-24 20:59:25 +0000812 {
813 // If we are doing synchronous mode, then wait for the initial
814 // stop to happen, else, return and let the caller watch for
815 // the stop
816 if (synchronous_execution)
Greg Clayton23f59502012-07-17 03:23:13 +0000817 process_sp->WaitForProcessToStop (NULL);
Greg Clayton38d1f052012-02-24 20:59:25 +0000818 // We we are stopping at the entry point, we can return now!
Greg Clayton0e615682012-02-24 05:03:03 +0000819 return sb_process;
Greg Clayton38d1f052012-02-24 20:59:25 +0000820 }
Greg Clayton0e615682012-02-24 05:03:03 +0000821
822 // Make sure we are stopped at the entry
Greg Clayton23f59502012-07-17 03:23:13 +0000823 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton0e615682012-02-24 05:03:03 +0000824 if (state == eStateStopped)
825 {
826 // resume the process to skip the entry point
827 error.SetError (process_sp->Resume());
828 if (error.Success())
829 {
830 // If we are doing synchronous mode, then wait for the
831 // process to stop yet again!
Greg Clayton38d1f052012-02-24 20:59:25 +0000832 if (synchronous_execution)
Greg Clayton0e615682012-02-24 05:03:03 +0000833 process_sp->WaitForProcessToStop (NULL);
834 }
835 }
836 }
837 }
838 else
839 {
840 error.SetErrorString ("unable to create lldb_private::Process");
841 }
842 }
843 else
844 {
845 error.SetErrorString ("SBTarget is invalid");
846 }
847
848 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
849 if (log)
850 {
Sean Callanan575a4542012-10-20 00:21:31 +0000851 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Greg Clayton0e615682012-02-24 05:03:03 +0000852 target_sp.get(), process_sp.get());
853 }
854
855 return sb_process;
856}
857
858lldb::SBProcess
859SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
860{
Greg Clayton5160ce52013-03-27 23:08:40 +0000861 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000862
Greg Clayton0e615682012-02-24 05:03:03 +0000863 SBProcess sb_process;
864 ProcessSP process_sp;
865 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000866
867 if (log)
868 {
869 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...", target_sp.get());
870 }
871
Greg Clayton0e615682012-02-24 05:03:03 +0000872 if (target_sp)
873 {
874 Mutex::Locker api_locker (target_sp->GetAPIMutex());
875
876 StateType state = eStateInvalid;
877 process_sp = target_sp->GetProcessSP();
878 if (process_sp)
879 {
880 state = process_sp->GetState();
881
882 if (process_sp->IsAlive() && state != eStateConnected)
883 {
884 if (state == eStateAttaching)
885 error.SetErrorString ("process attach is in progress");
886 else
887 error.SetErrorString ("a process is already being debugged");
Sean Callanan575a4542012-10-20 00:21:31 +0000888 if (log)
889 {
890 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
891 target_sp.get(), error.GetCString());
892 }
Greg Clayton0e615682012-02-24 05:03:03 +0000893 return sb_process;
894 }
895 }
896
897 if (state != eStateConnected)
898 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
899
900 if (process_sp)
901 {
Greg Clayton0e615682012-02-24 05:03:03 +0000902 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000903 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000904 {
905 PlatformSP platform_sp = target_sp->GetPlatform();
Greg Clayton91e407e2012-09-27 00:03:39 +0000906 // See if we can pre-verify if a process exists or not
907 if (platform_sp && platform_sp->IsConnected())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000908 {
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000909 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Clayton91e407e2012-09-27 00:03:39 +0000910 ProcessInstanceInfo instance_info;
911 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
912 {
913 attach_info.SetUserID(instance_info.GetEffectiveUserID());
914 }
915 else
916 {
Daniel Malead01b2952012-11-29 21:49:15 +0000917 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000918 if (log)
919 {
920 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
921 target_sp.get(), error.GetCString());
922 }
Greg Clayton91e407e2012-09-27 00:03:39 +0000923 return sb_process;
924 }
Han Ming Ongcec8c902012-02-29 00:12:56 +0000925 }
926 }
Han Ming Ongbee98392012-02-29 19:16:40 +0000927 error.SetError (process_sp->Attach (attach_info));
928 if (error.Success())
929 {
930 sb_process.SetSP (process_sp);
931 // If we are doing synchronous mode, then wait for the
932 // process to stop!
933 if (target_sp->GetDebugger().GetAsyncExecution () == false)
934 process_sp->WaitForProcessToStop (NULL);
935 }
Greg Clayton0e615682012-02-24 05:03:03 +0000936 }
937 else
938 {
939 error.SetErrorString ("unable to create lldb_private::Process");
940 }
941 }
942 else
943 {
944 error.SetErrorString ("SBTarget is invalid");
945 }
Sean Callanan575a4542012-10-20 00:21:31 +0000946
947 if (log)
948 {
949 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
950 target_sp.get(), process_sp.get());
951 }
952
Greg Clayton0e615682012-02-24 05:03:03 +0000953 return sb_process;
954}
955
956
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000957#if defined(__APPLE__)
958
959lldb::SBProcess
960SBTarget::AttachToProcessWithID (SBListener &listener,
961 ::pid_t pid,
962 lldb::SBError& error)
963{
964 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
965}
966
967#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000968
969lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000970SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000971(
Greg Clayton4b045622011-02-03 21:28:34 +0000972 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000973 lldb::pid_t pid,// The process ID to attach to
974 SBError& error // An error explaining what went wrong if attach fails
975)
976{
Greg Clayton5160ce52013-03-27 23:08:40 +0000977 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000978
Greg Clayton524e60b2010-10-06 22:10:17 +0000979 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000980 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000981 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000982
983 if (log)
984 {
Daniel Malead01b2952012-11-29 21:49:15 +0000985 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...", target_sp.get(), pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000986 }
987
Greg Claytonacdbe812012-01-30 09:04:36 +0000988 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000989 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000990 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0c74e782011-06-24 03:21:43 +0000991
992 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000993 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000994 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +0000995 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000996 state = process_sp->GetState();
Greg Clayton0c74e782011-06-24 03:21:43 +0000997
Greg Claytonb9556ac2012-01-30 07:41:31 +0000998 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton0c74e782011-06-24 03:21:43 +0000999 {
1000 if (state == eStateAttaching)
1001 error.SetErrorString ("process attach is in progress");
1002 else
1003 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001004 return sb_process;
1005 }
1006 }
1007
1008 if (state == eStateConnected)
1009 {
1010 // If we are already connected, then we have already specified the
1011 // listener, so if a valid listener is supplied, we need to error out
1012 // to let the client know.
1013 if (listener.IsValid())
1014 {
1015 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001016 return sb_process;
1017 }
1018 }
Greg Clayton4b045622011-02-03 21:28:34 +00001019 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001020 {
1021 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001022 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001023 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001024 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001025 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001026 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001027 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001028 sb_process.SetSP (process_sp);
1029
Greg Clayton144f3a92011-11-15 03:53:30 +00001030 ProcessAttachInfo attach_info;
1031 attach_info.SetProcessID (pid);
Han Ming Ong84647042012-02-25 01:07:38 +00001032
1033 PlatformSP platform_sp = target_sp->GetPlatform();
1034 ProcessInstanceInfo instance_info;
1035 if (platform_sp->GetProcessInfo(pid, instance_info))
1036 {
1037 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ong84647042012-02-25 01:07:38 +00001038 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001039 error.SetError (process_sp->Attach (attach_info));
Greg Claytone2186ed2012-09-07 17:51:47 +00001040 if (error.Success())
1041 {
1042 // If we are doing synchronous mode, then wait for the
1043 // process to stop!
1044 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +00001045 process_sp->WaitForProcessToStop (NULL);
Greg Claytone2186ed2012-09-07 17:51:47 +00001046 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001047 }
1048 else
1049 {
1050 error.SetErrorString ("unable to create lldb_private::Process");
1051 }
1052 }
1053 else
1054 {
1055 error.SetErrorString ("SBTarget is invalid");
1056 }
Sean Callanan575a4542012-10-20 00:21:31 +00001057
1058 if (log)
1059 {
1060 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
1061 target_sp.get(), process_sp.get());
1062 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001063 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001064}
1065
1066lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +00001067SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +00001068(
Greg Clayton4b045622011-02-03 21:28:34 +00001069 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +00001070 const char *name, // basename of process to attach to
1071 bool wait_for, // if true wait for a new instance of "name" to be launched
1072 SBError& error // An error explaining what went wrong if attach fails
1073)
1074{
Greg Clayton5160ce52013-03-27 23:08:40 +00001075 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001076
Greg Clayton524e60b2010-10-06 22:10:17 +00001077 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001078 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001079 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001080
1081 if (log)
1082 {
1083 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...", target_sp.get(), name, wait_for ? "true" : "false");
1084 }
1085
Greg Claytonacdbe812012-01-30 09:04:36 +00001086 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001087 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001088 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +00001089
Greg Clayton0c74e782011-06-24 03:21:43 +00001090 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +00001091 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +00001092 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +00001093 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001094 state = process_sp->GetState();
Greg Clayton0c74e782011-06-24 03:21:43 +00001095
Greg Claytonb9556ac2012-01-30 07:41:31 +00001096 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton0c74e782011-06-24 03:21:43 +00001097 {
1098 if (state == eStateAttaching)
1099 error.SetErrorString ("process attach is in progress");
1100 else
1101 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001102 return sb_process;
1103 }
1104 }
1105
1106 if (state == eStateConnected)
1107 {
1108 // If we are already connected, then we have already specified the
1109 // listener, so if a valid listener is supplied, we need to error out
1110 // to let the client know.
1111 if (listener.IsValid())
1112 {
1113 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001114 return sb_process;
1115 }
1116 }
Greg Clayton4b045622011-02-03 21:28:34 +00001117 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001118 {
1119 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001120 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001121 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001122 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001123 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001124
Greg Claytonb9556ac2012-01-30 07:41:31 +00001125 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001126 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001127 sb_process.SetSP (process_sp);
Greg Clayton144f3a92011-11-15 03:53:30 +00001128 ProcessAttachInfo attach_info;
1129 attach_info.GetExecutableFile().SetFile(name, false);
1130 attach_info.SetWaitForLaunch(wait_for);
Greg Claytonb9556ac2012-01-30 07:41:31 +00001131 error.SetError (process_sp->Attach (attach_info));
Daniel Malead659dc12013-04-01 19:47:00 +00001132 if (error.Success())
1133 {
1134 // If we are doing synchronous mode, then wait for the
1135 // process to stop!
1136 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1137 process_sp->WaitForProcessToStop (NULL);
1138 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001139 }
1140 else
1141 {
1142 error.SetErrorString ("unable to create lldb_private::Process");
1143 }
1144 }
1145 else
1146 {
1147 error.SetErrorString ("SBTarget is invalid");
1148 }
Sean Callanan575a4542012-10-20 00:21:31 +00001149
1150 if (log)
1151 {
1152 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1153 target_sp.get(), process_sp.get());
1154 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001155 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001156}
1157
James McIlree9631aae2011-03-04 00:31:13 +00001158lldb::SBProcess
1159SBTarget::ConnectRemote
1160(
1161 SBListener &listener,
1162 const char *url,
1163 const char *plugin_name,
1164 SBError& error
1165)
1166{
Greg Clayton5160ce52013-03-27 23:08:40 +00001167 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001168
James McIlree9631aae2011-03-04 00:31:13 +00001169 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001170 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001171 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001172
1173 if (log)
1174 {
1175 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1176 }
1177
Greg Claytonacdbe812012-01-30 09:04:36 +00001178 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001179 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001180 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +00001181 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001182 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001183 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001184 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001185
1186
Greg Claytonb9556ac2012-01-30 07:41:31 +00001187 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001188 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001189 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001190 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +00001191 }
1192 else
1193 {
1194 error.SetErrorString ("unable to create lldb_private::Process");
1195 }
1196 }
1197 else
1198 {
1199 error.SetErrorString ("SBTarget is invalid");
1200 }
Sean Callanan575a4542012-10-20 00:21:31 +00001201
1202 if (log)
1203 {
1204 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1205 target_sp.get(), process_sp.get());
1206 }
James McIlree9631aae2011-03-04 00:31:13 +00001207 return sb_process;
1208}
1209
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210SBFileSpec
1211SBTarget::GetExecutable ()
1212{
Caroline Ticeceb6b132010-10-26 03:11:13 +00001213
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +00001215 TargetSP target_sp(GetSP());
1216 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001218 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +00001219 if (exe_module)
1220 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001222
Greg Clayton5160ce52013-03-27 23:08:40 +00001223 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001224 if (log)
1225 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001226 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001227 target_sp.get(), exe_file_spec.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001228 }
1229
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230 return exe_file_spec;
1231}
1232
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234SBTarget::operator == (const SBTarget &rhs) const
1235{
Greg Clayton66111032010-06-23 01:19:29 +00001236 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237}
1238
1239bool
1240SBTarget::operator != (const SBTarget &rhs) const
1241{
Greg Clayton66111032010-06-23 01:19:29 +00001242 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243}
1244
Greg Claytonb9556ac2012-01-30 07:41:31 +00001245lldb::TargetSP
1246SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +00001247{
1248 return m_opaque_sp;
1249}
1250
Greg Clayton66111032010-06-23 01:19:29 +00001251void
Greg Claytonb9556ac2012-01-30 07:41:31 +00001252SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +00001253{
1254 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255}
1256
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001257lldb::SBAddress
1258SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001259{
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001260 lldb::SBAddress sb_addr;
1261 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +00001262 TargetSP target_sp(GetSP());
1263 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001264 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001265 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1266 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001267 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001268 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001269
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001270 // We have a load address that isn't in a section, just return an address
1271 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +00001272 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001273 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001274}
1275
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001276SBSymbolContext
Greg Claytoneb023e72013-10-11 19:48:25 +00001277SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
1278 uint32_t resolve_scope)
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001279{
1280 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001281 if (addr.IsValid())
1282 {
1283 TargetSP target_sp(GetSP());
1284 if (target_sp)
1285 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1286 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001287 return sc;
1288}
1289
1290
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001292SBTarget::BreakpointCreateByLocation (const char *file,
1293 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294{
Greg Clayton7481c202010-11-08 00:28:40 +00001295 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296}
1297
1298SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001299SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
1300 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001301{
Greg Clayton5160ce52013-03-27 23:08:40 +00001302 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001303
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001305 TargetSP target_sp(GetSP());
1306 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001307 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001308 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001309
Greg Clayton1f746072012-08-29 21:13:06 +00001310 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001311 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001312 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001313 const bool hardware = false;
1314 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001315 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001316
1317 if (log)
1318 {
1319 SBStream sstr;
1320 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001321 char path[PATH_MAX];
1322 sb_file_spec->GetPath (path, sizeof(path));
1323 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytonacdbe812012-01-30 09:04:36 +00001324 target_sp.get(),
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001325 path,
Greg Clayton48381312010-10-30 04:51:46 +00001326 line,
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001327 sb_bp.get(),
Caroline Ticeceb6b132010-10-26 03:11:13 +00001328 sstr.GetData());
1329 }
1330
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331 return sb_bp;
1332}
1333
1334SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001335SBTarget::BreakpointCreateByName (const char *symbol_name,
1336 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337{
Greg Clayton5160ce52013-03-27 23:08:40 +00001338 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001339
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001341 TargetSP target_sp(GetSP());
1342 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001344 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001345
1346 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001347 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001348 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349 if (module_name && module_name[0])
1350 {
Jim Ingham969795f2011-09-21 01:17:13 +00001351 FileSpecList module_spec_list;
1352 module_spec_list.Append (FileSpec (module_name, false));
Greg Claytoneb023e72013-10-11 19:48:25 +00001353 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 }
1355 else
1356 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001357 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 }
1359 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001360
1361 if (log)
1362 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001363 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001364 target_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001365 }
1366
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367 return sb_bp;
1368}
1369
Jim Ingham87df91b2011-09-23 00:54:11 +00001370lldb::SBBreakpoint
1371SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001372 const SBFileSpecList &module_list,
1373 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001374{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001375 uint32_t name_type_mask = eFunctionNameTypeAuto;
1376 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1377}
1378
1379lldb::SBBreakpoint
1380SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001381 uint32_t name_type_mask,
1382 const SBFileSpecList &module_list,
1383 const SBFileSpecList &comp_unit_list)
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001384{
Greg Clayton5160ce52013-03-27 23:08:40 +00001385 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001386
1387 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001388 TargetSP target_sp(GetSP());
1389 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001390 {
Jim Inghama8558b62012-05-22 00:12:20 +00001391 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001392 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001393 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001394 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1395 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Greg Claytoneb023e72013-10-11 19:48:25 +00001396 comp_unit_list.get(),
1397 symbol_name,
1398 name_type_mask,
1399 skip_prologue,
1400 internal,
1401 hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001402 }
1403
1404 if (log)
1405 {
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001406 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001407 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001408 }
1409
1410 return sb_bp;
1411}
1412
Jim Inghamfab10e82012-03-06 00:37:27 +00001413lldb::SBBreakpoint
1414SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1415 uint32_t num_names,
1416 uint32_t name_type_mask,
1417 const SBFileSpecList &module_list,
1418 const SBFileSpecList &comp_unit_list)
1419{
Greg Clayton5160ce52013-03-27 23:08:40 +00001420 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001421
1422 SBBreakpoint sb_bp;
1423 TargetSP target_sp(GetSP());
1424 if (target_sp && num_names > 0)
1425 {
1426 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001427 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001428 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001429 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001430 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1431 comp_unit_list.get(),
1432 symbol_names,
1433 num_names,
1434 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001435 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001436 internal,
1437 hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001438 }
1439
1440 if (log)
1441 {
1442 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1443 for (uint32_t i = 0 ; i < num_names; i++)
1444 {
1445 char sep;
1446 if (i < num_names - 1)
1447 sep = ',';
1448 else
1449 sep = '}';
1450 if (symbol_names[i] != NULL)
1451 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1452 else
1453 log->Printf ("\"<NULL>\"%c ", sep);
1454
1455 }
1456 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1457 }
1458
1459 return sb_bp;
1460}
Jim Ingham87df91b2011-09-23 00:54:11 +00001461
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001462SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001463SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1464 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465{
Greg Clayton5160ce52013-03-27 23:08:40 +00001466 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001467
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001469 TargetSP target_sp(GetSP());
1470 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001472 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001473 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001474 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001475 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001476 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001477
1478 if (module_name && module_name[0])
1479 {
Jim Ingham969795f2011-09-21 01:17:13 +00001480 FileSpecList module_spec_list;
1481 module_spec_list.Append (FileSpec (module_name, false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001482
Greg Claytoneb023e72013-10-11 19:48:25 +00001483 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 }
1485 else
1486 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001487 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001488 }
1489 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001490
1491 if (log)
1492 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001493 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001494 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001495 }
1496
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497 return sb_bp;
1498}
1499
Jim Ingham87df91b2011-09-23 00:54:11 +00001500lldb::SBBreakpoint
1501SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001502 const SBFileSpecList &module_list,
1503 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001504{
Greg Clayton5160ce52013-03-27 23:08:40 +00001505 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506
Jim Ingham87df91b2011-09-23 00:54:11 +00001507 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001508 TargetSP target_sp(GetSP());
1509 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001510 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001511 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001512 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001513 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001514 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001515 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Ingham87df91b2011-09-23 00:54:11 +00001516
Greg Claytoneb023e72013-10-11 19:48:25 +00001517 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001518 }
1519
1520 if (log)
1521 {
1522 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001523 target_sp.get(), symbol_name_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001524 }
1525
1526 return sb_bp;
1527}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528
1529SBBreakpoint
1530SBTarget::BreakpointCreateByAddress (addr_t address)
1531{
Greg Clayton5160ce52013-03-27 23:08:40 +00001532 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001533
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001534 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001535 TargetSP target_sp(GetSP());
1536 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001537 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001538 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001539 const bool hardware = false;
1540 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001541 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001542
1543 if (log)
1544 {
Daniel Malead01b2952012-11-29 21:49:15 +00001545 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 +00001546 }
1547
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001548 return sb_bp;
1549}
1550
Jim Ingham969795f2011-09-21 01:17:13 +00001551lldb::SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001552SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1553 const lldb::SBFileSpec &source_file,
1554 const char *module_name)
Jim Ingham969795f2011-09-21 01:17:13 +00001555{
Greg Clayton5160ce52013-03-27 23:08:40 +00001556 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001557
1558 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001559 TargetSP target_sp(GetSP());
1560 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001561 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001562 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001563 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001564 FileSpecList source_file_spec_list;
Greg Claytoneb023e72013-10-11 19:48:25 +00001565 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001566 source_file_spec_list.Append (source_file.ref());
Jim Ingham969795f2011-09-21 01:17:13 +00001567
1568 if (module_name && module_name[0])
1569 {
1570 FileSpecList module_spec_list;
1571 module_spec_list.Append (FileSpec (module_name, false));
1572
Greg Claytoneb023e72013-10-11 19:48:25 +00001573 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001574 }
1575 else
1576 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001577 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001578 }
1579 }
1580
1581 if (log)
1582 {
1583 char path[PATH_MAX];
1584 source_file->GetPath (path, sizeof(path));
1585 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001586 target_sp.get(), source_regex, path, module_name, sb_bp.get());
Jim Ingham969795f2011-09-21 01:17:13 +00001587 }
1588
1589 return sb_bp;
1590}
1591
Jim Ingham87df91b2011-09-23 00:54:11 +00001592lldb::SBBreakpoint
1593SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001594 const SBFileSpecList &module_list,
1595 const lldb::SBFileSpecList &source_file_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001596{
Greg Clayton5160ce52013-03-27 23:08:40 +00001597 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001598
1599 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001600 TargetSP target_sp(GetSP());
1601 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001602 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001603 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001604 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001605 RegularExpression regexp(source_regex);
Greg Claytoneb023e72013-10-11 19:48:25 +00001606 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001607 }
1608
1609 if (log)
1610 {
1611 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001612 target_sp.get(), source_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001613 }
1614
1615 return sb_bp;
1616}
Jim Ingham969795f2011-09-21 01:17:13 +00001617
Jim Inghamfab10e82012-03-06 00:37:27 +00001618lldb::SBBreakpoint
1619SBTarget::BreakpointCreateForException (lldb::LanguageType language,
Greg Claytoneb023e72013-10-11 19:48:25 +00001620 bool catch_bp,
1621 bool throw_bp)
Jim Inghamfab10e82012-03-06 00:37:27 +00001622{
Greg Clayton5160ce52013-03-27 23:08:40 +00001623 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001624
1625 SBBreakpoint sb_bp;
1626 TargetSP target_sp(GetSP());
1627 if (target_sp)
1628 {
1629 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001630 const bool hardware = false;
1631 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001632 }
1633
1634 if (log)
1635 {
1636 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1637 target_sp.get(),
1638 LanguageRuntime::GetNameForLanguageType(language),
1639 catch_bp ? "on" : "off",
1640 throw_bp ? "on" : "off",
1641 sb_bp.get());
1642 }
1643
1644 return sb_bp;
1645}
1646
Greg Clayton9fed0d82010-07-23 23:33:17 +00001647uint32_t
1648SBTarget::GetNumBreakpoints () const
1649{
Greg Claytonacdbe812012-01-30 09:04:36 +00001650 TargetSP target_sp(GetSP());
1651 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001652 {
1653 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001654 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001655 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001656 return 0;
1657}
1658
1659SBBreakpoint
1660SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1661{
1662 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001663 TargetSP target_sp(GetSP());
1664 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001665 {
1666 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001667 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001668 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001669 return sb_breakpoint;
1670}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671
1672bool
1673SBTarget::BreakpointDelete (break_id_t bp_id)
1674{
Greg Clayton5160ce52013-03-27 23:08:40 +00001675 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001676
Caroline Ticeceb6b132010-10-26 03:11:13 +00001677 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001678 TargetSP target_sp(GetSP());
1679 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001680 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001681 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1682 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001683 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001684
1685 if (log)
1686 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001687 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001688 }
1689
1690 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691}
1692
Johnny Chen5d043462011-09-26 22:40:50 +00001693SBBreakpoint
1694SBTarget::FindBreakpointByID (break_id_t bp_id)
1695{
Greg Clayton5160ce52013-03-27 23:08:40 +00001696 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001697
1698 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001699 TargetSP target_sp(GetSP());
1700 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001701 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001702 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1703 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001704 }
1705
1706 if (log)
1707 {
1708 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001709 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001710 }
1711
1712 return sb_breakpoint;
1713}
1714
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001715bool
1716SBTarget::EnableAllBreakpoints ()
1717{
Greg Claytonacdbe812012-01-30 09:04:36 +00001718 TargetSP target_sp(GetSP());
1719 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001721 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1722 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723 return true;
1724 }
1725 return false;
1726}
1727
1728bool
1729SBTarget::DisableAllBreakpoints ()
1730{
Greg Claytonacdbe812012-01-30 09:04:36 +00001731 TargetSP target_sp(GetSP());
1732 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001734 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1735 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 return true;
1737 }
1738 return false;
1739}
1740
1741bool
1742SBTarget::DeleteAllBreakpoints ()
1743{
Greg Claytonacdbe812012-01-30 09:04:36 +00001744 TargetSP target_sp(GetSP());
1745 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001747 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1748 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749 return true;
1750 }
1751 return false;
1752}
1753
Johnny Chen5d043462011-09-26 22:40:50 +00001754uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001755SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001756{
Greg Claytonacdbe812012-01-30 09:04:36 +00001757 TargetSP target_sp(GetSP());
1758 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001759 {
Johnny Chen01a67862011-10-14 00:42:25 +00001760 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001761 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001762 }
1763 return 0;
1764}
1765
Greg Clayton1b282f92011-10-13 18:08:26 +00001766SBWatchpoint
1767SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001768{
Johnny Chen01a67862011-10-14 00:42:25 +00001769 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001770 TargetSP target_sp(GetSP());
1771 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001772 {
Johnny Chen01a67862011-10-14 00:42:25 +00001773 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001774 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001775 }
Johnny Chen01a67862011-10-14 00:42:25 +00001776 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001777}
1778
1779bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001780SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001781{
Greg Clayton5160ce52013-03-27 23:08:40 +00001782 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001783
1784 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001785 TargetSP target_sp(GetSP());
1786 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001787 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001788 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001789 Mutex::Locker locker;
1790 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001791 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001792 }
1793
1794 if (log)
1795 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001796 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
Johnny Chen5d043462011-09-26 22:40:50 +00001797 }
1798
1799 return result;
1800}
1801
Greg Clayton1b282f92011-10-13 18:08:26 +00001802SBWatchpoint
1803SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001804{
Greg Clayton5160ce52013-03-27 23:08:40 +00001805 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001806
Greg Clayton1b282f92011-10-13 18:08:26 +00001807 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001808 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001809 TargetSP target_sp(GetSP());
1810 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001811 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001812 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001813 Mutex::Locker locker;
1814 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001815 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1816 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001817 }
1818
1819 if (log)
1820 {
Johnny Chen01a67862011-10-14 00:42:25 +00001821 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001822 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001823 }
1824
Greg Clayton1b282f92011-10-13 18:08:26 +00001825 return sb_watchpoint;
1826}
1827
1828lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001829SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001830{
Greg Clayton5160ce52013-03-27 23:08:40 +00001831 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1b282f92011-10-13 18:08:26 +00001832
1833 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001834 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001835 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001836 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001837 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001838 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001839 uint32_t watch_type = 0;
1840 if (read)
1841 watch_type |= LLDB_WATCH_TYPE_READ;
1842 if (write)
1843 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001844 if (watch_type == 0)
1845 {
1846 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1847 return sb_watchpoint;
1848 }
1849
Johnny Chen7385a5a2012-05-31 22:56:36 +00001850 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001851 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001852 // This API doesn't take in a type, so we can't figure out what it is.
1853 ClangASTType *type = NULL;
1854 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001855 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001856 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001857 }
1858
1859 if (log)
1860 {
Daniel Malead01b2952012-11-29 21:49:15 +00001861 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001862 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
Greg Clayton1b282f92011-10-13 18:08:26 +00001863 }
1864
1865 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001866}
1867
1868bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001869SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001870{
Greg Claytonacdbe812012-01-30 09:04:36 +00001871 TargetSP target_sp(GetSP());
1872 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001873 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001874 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001875 Mutex::Locker locker;
1876 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001877 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001878 return true;
1879 }
1880 return false;
1881}
1882
1883bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001884SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001885{
Greg Claytonacdbe812012-01-30 09:04:36 +00001886 TargetSP target_sp(GetSP());
1887 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001888 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001889 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001890 Mutex::Locker locker;
1891 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001892 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001893 return true;
1894 }
1895 return false;
1896}
1897
Enrico Granata347c2aa2013-10-08 21:49:02 +00001898SBValue
1899SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1900{
1901 SBValue sb_value;
1902 lldb::ValueObjectSP new_value_sp;
1903 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1904 {
1905 lldb::addr_t address(addr.GetLoadAddress(*this));
1906 lldb::TypeImplSP type_impl_sp (type.GetSP());
Enrico Granatadc4db5a2013-10-29 00:28:35 +00001907 ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType(true).GetPointerType ());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001908 if (pointer_ast_type)
1909 {
1910 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
1911
1912 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1913 ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1914 pointer_ast_type,
1915 ConstString(name),
1916 buffer,
1917 exe_ctx.GetByteOrder(),
1918 exe_ctx.GetAddressByteSize()));
1919
1920 if (ptr_result_valobj_sp)
1921 {
1922 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1923 Error err;
1924 new_value_sp = ptr_result_valobj_sp->Dereference(err);
1925 if (new_value_sp)
1926 new_value_sp->SetName(ConstString(name));
1927 }
1928 }
1929 }
1930 sb_value.SetSP(new_value_sp);
1931 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1932 if (log)
1933 {
1934 if (new_value_sp)
1935 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"", m_opaque_sp.get(), new_value_sp->GetName().AsCString());
1936 else
1937 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL", m_opaque_sp.get());
1938 }
1939 return sb_value;
1940}
1941
Johnny Chen5d043462011-09-26 22:40:50 +00001942bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001943SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001944{
Greg Claytonacdbe812012-01-30 09:04:36 +00001945 TargetSP target_sp(GetSP());
1946 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001947 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001948 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001949 Mutex::Locker locker;
1950 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001951 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001952 return true;
1953 }
1954 return false;
1955}
1956
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001957
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001958lldb::SBModule
1959SBTarget::AddModule (const char *path,
1960 const char *triple,
1961 const char *uuid_cstr)
1962{
Greg Claytonb210aec2012-04-23 20:23:39 +00001963 return AddModule (path, triple, uuid_cstr, NULL);
1964}
1965
1966lldb::SBModule
1967SBTarget::AddModule (const char *path,
1968 const char *triple,
1969 const char *uuid_cstr,
1970 const char *symfile)
1971{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001972 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001973 TargetSP target_sp(GetSP());
1974 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001975 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001976 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001977 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001978 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001979
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001980 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001981 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001982
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001983 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001984 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00001985 else
1986 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00001987
1988 if (symfile)
1989 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001990
Greg Claytonb9a01b32012-02-26 05:51:37 +00001991 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001992 }
1993 return sb_module;
1994}
1995
Greg Clayton226cce22013-07-08 22:22:41 +00001996lldb::SBModule
1997SBTarget::AddModule (const SBModuleSpec &module_spec)
1998{
1999 lldb::SBModule sb_module;
2000 TargetSP target_sp(GetSP());
2001 if (target_sp)
2002 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
2003 return sb_module;
2004}
2005
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002006bool
2007SBTarget::AddModule (lldb::SBModule &module)
2008{
Greg Claytonacdbe812012-01-30 09:04:36 +00002009 TargetSP target_sp(GetSP());
2010 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002011 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002012 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002013 return true;
2014 }
2015 return false;
2016}
2017
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002018uint32_t
2019SBTarget::GetNumModules () const
2020{
Greg Clayton5160ce52013-03-27 23:08:40 +00002021 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002022
Caroline Ticeceb6b132010-10-26 03:11:13 +00002023 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00002024 TargetSP target_sp(GetSP());
2025 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002026 {
2027 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002028 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00002029 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002030
2031 if (log)
Greg Claytonacdbe812012-01-30 09:04:36 +00002032 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002033
2034 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035}
2036
Greg Clayton48e42542010-07-30 20:12:55 +00002037void
2038SBTarget::Clear ()
2039{
Greg Clayton5160ce52013-03-27 23:08:40 +00002040 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002041
2042 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002043 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002044
Greg Clayton48e42542010-07-30 20:12:55 +00002045 m_opaque_sp.reset();
2046}
2047
2048
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002049SBModule
2050SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2051{
2052 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002053 TargetSP target_sp(GetSP());
2054 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00002055 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002056 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002057 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00002058 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00002059 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002060 return sb_module;
2061}
2062
Greg Clayton13d19502012-01-29 06:07:39 +00002063lldb::ByteOrder
2064SBTarget::GetByteOrder ()
2065{
Greg Claytonacdbe812012-01-30 09:04:36 +00002066 TargetSP target_sp(GetSP());
2067 if (target_sp)
2068 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00002069 return eByteOrderInvalid;
2070}
2071
2072const char *
2073SBTarget::GetTriple ()
2074{
Greg Claytonacdbe812012-01-30 09:04:36 +00002075 TargetSP target_sp(GetSP());
2076 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00002077 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002078 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00002079 // Unique the string so we don't run into ownership issues since
2080 // the const strings put the string into the string pool once and
2081 // the strings never comes out
2082 ConstString const_triple (triple.c_str());
2083 return const_triple.GetCString();
2084 }
2085 return NULL;
2086}
2087
2088uint32_t
2089SBTarget::GetAddressByteSize()
2090{
Greg Claytonacdbe812012-01-30 09:04:36 +00002091 TargetSP target_sp(GetSP());
2092 if (target_sp)
2093 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00002094 return sizeof(void*);
2095}
2096
2097
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002098SBModule
2099SBTarget::GetModuleAtIndex (uint32_t idx)
2100{
Greg Clayton5160ce52013-03-27 23:08:40 +00002101 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002102
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002103 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002104 ModuleSP module_sp;
2105 TargetSP target_sp(GetSP());
2106 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002107 {
2108 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002109 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2110 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002111 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002112
2113 if (log)
2114 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00002115 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002116 target_sp.get(), idx, module_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002117 }
2118
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002119 return sb_module;
2120}
2121
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002122bool
2123SBTarget::RemoveModule (lldb::SBModule module)
2124{
Greg Claytonacdbe812012-01-30 09:04:36 +00002125 TargetSP target_sp(GetSP());
2126 if (target_sp)
2127 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002128 return false;
2129}
2130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131
2132SBBroadcaster
2133SBTarget::GetBroadcaster () const
2134{
Greg Clayton5160ce52013-03-27 23:08:40 +00002135 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002136
Greg Claytonacdbe812012-01-30 09:04:36 +00002137 TargetSP target_sp(GetSP());
2138 SBBroadcaster broadcaster(target_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002139
2140 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002141 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002142 target_sp.get(), broadcaster.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002143
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002144 return broadcaster;
2145}
2146
Caroline Ticedde9cff2010-09-20 05:20:02 +00002147bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002148SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002149{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002150 Stream &strm = description.ref();
2151
Greg Claytonacdbe812012-01-30 09:04:36 +00002152 TargetSP target_sp(GetSP());
2153 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002154 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002155 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002156 }
2157 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002158 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002159
2160 return true;
2161}
2162
Greg Clayton5569e642012-02-06 01:44:54 +00002163lldb::SBSymbolContextList
2164SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002165{
Greg Clayton5569e642012-02-06 01:44:54 +00002166 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002167 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002168 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002169 TargetSP target_sp(GetSP());
2170 if (target_sp)
2171 {
2172 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002173 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002174 const bool append = true;
2175 target_sp->GetImages().FindFunctions (ConstString(name),
2176 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002177 symbols_ok,
2178 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002179 append,
2180 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002181 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002182 }
Greg Clayton5569e642012-02-06 01:44:54 +00002183 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002184}
2185
Enrico Granata6f3533f2011-07-29 19:53:35 +00002186lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002187SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002188{
Greg Claytonacdbe812012-01-30 09:04:36 +00002189 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002190 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002191 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002192 ConstString const_typename(typename_cstr);
2193 SymbolContext sc;
2194 const bool exact_match = false;
2195
2196 const ModuleList &module_list = target_sp->GetImages();
2197 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002198 for (size_t idx = 0; idx < count; idx++)
2199 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002200 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2201 if (module_sp)
2202 {
2203 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2204 if (type_sp)
2205 return SBType(type_sp);
2206 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002207 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002208
2209 // Didn't find the type in the symbols; try the Objective-C runtime
2210 // if one is installed
2211
2212 ProcessSP process_sp(target_sp->GetProcessSP());
2213
2214 if (process_sp)
2215 {
2216 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2217
2218 if (objc_language_runtime)
2219 {
2220 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2221
2222 if (objc_type_vendor)
2223 {
2224 std::vector <ClangASTType> types;
2225
2226 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2227 return SBType(types[0]);
2228 }
2229 }
2230 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002231
2232 // No matches, search for basic typename matches
2233 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2234 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002235 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002236 }
2237 return SBType();
2238}
2239
Greg Claytonb43165b2012-12-05 21:24:42 +00002240SBType
2241SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002242{
Greg Claytonacdbe812012-01-30 09:04:36 +00002243 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002244 if (target_sp)
2245 {
2246 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2247 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002248 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00002249 }
2250 return SBType();
2251}
2252
2253
2254lldb::SBTypeList
2255SBTarget::FindTypes (const char* typename_cstr)
2256{
2257 SBTypeList sb_type_list;
2258 TargetSP target_sp(GetSP());
2259 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002260 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002261 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002262 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002263 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002264 SymbolContext sc;
2265 TypeList type_list;
2266
Greg Clayton29399a22012-04-06 17:41:13 +00002267 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002268 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002269 exact_match,
2270 UINT32_MAX,
2271 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002272
Greg Claytonb43165b2012-12-05 21:24:42 +00002273 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002274 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002275 for (size_t idx = 0; idx < num_matches; idx++)
2276 {
2277 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2278 if (type_sp)
2279 sb_type_list.Append(SBType(type_sp));
2280 }
2281 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002282
2283 // Try the Objective-C runtime if one is installed
2284
2285 ProcessSP process_sp(target_sp->GetProcessSP());
2286
2287 if (process_sp)
2288 {
2289 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2290
2291 if (objc_language_runtime)
2292 {
2293 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2294
2295 if (objc_type_vendor)
2296 {
2297 std::vector <ClangASTType> types;
2298
2299 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2300 {
2301 for (ClangASTType &type : types)
2302 {
2303 sb_type_list.Append(SBType(type));
2304 }
2305 }
2306 }
2307 }
2308 }
2309
2310 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002311 {
2312 // No matches, search for basic typename matches
2313 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2314 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002315 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002316 }
2317 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002318 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002319}
2320
Greg Claytondea8cb42011-06-29 22:09:02 +00002321SBValueList
2322SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2323{
2324 SBValueList sb_value_list;
2325
Greg Claytonacdbe812012-01-30 09:04:36 +00002326 TargetSP target_sp(GetSP());
2327 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002328 {
2329 VariableList variable_list;
2330 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002331 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2332 append,
2333 max_matches,
2334 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002335
2336 if (match_count > 0)
2337 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002338 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002339 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002340 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002341 for (uint32_t i=0; i<match_count; ++i)
2342 {
2343 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2344 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002345 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002346 }
2347 }
2348 }
2349
2350 return sb_value_list;
2351}
2352
Enrico Granatabcd80b42013-01-16 18:53:52 +00002353lldb::SBValue
2354SBTarget::FindFirstGlobalVariable (const char* name)
2355{
2356 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2357 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2358 return sb_value_list.GetValueAtIndex(0);
2359 return SBValue();
2360}
2361
Jim Inghame37d6052011-09-13 00:29:56 +00002362SBSourceManager
2363SBTarget::GetSourceManager()
2364{
2365 SBSourceManager source_manager (*this);
2366 return source_manager;
2367}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002368
Sean Callanan50952e92011-12-14 23:49:37 +00002369lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002370SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2371{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002372 return ReadInstructions (base_addr, count, NULL);
2373}
2374
2375lldb::SBInstructionList
2376SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2377{
Greg Clayton9c766112012-03-06 22:24:44 +00002378 SBInstructionList sb_instructions;
2379
2380 TargetSP target_sp(GetSP());
2381 if (target_sp)
2382 {
2383 Address *addr_ptr = base_addr.get();
2384
2385 if (addr_ptr)
2386 {
2387 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2388 bool prefer_file_cache = false;
2389 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002390 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2391 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2392 prefer_file_cache,
2393 data.GetBytes(),
2394 data.GetByteSize(),
2395 error,
2396 &load_addr);
2397 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002398 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2399 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002400 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002401 *addr_ptr,
2402 data.GetBytes(),
2403 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002404 count,
2405 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002406 }
2407 }
2408
2409 return sb_instructions;
2410
2411}
2412
2413lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002414SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2415{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002416 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2417}
2418
2419lldb::SBInstructionList
2420SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2421{
Sean Callanan50952e92011-12-14 23:49:37 +00002422 SBInstructionList sb_instructions;
2423
Greg Claytonacdbe812012-01-30 09:04:36 +00002424 TargetSP target_sp(GetSP());
2425 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002426 {
2427 Address addr;
2428
2429 if (base_addr.get())
2430 addr = *base_addr.get();
2431
Greg Clayton3faf47c2013-03-28 23:42:53 +00002432 const bool data_from_file = true;
2433
Greg Claytonacdbe812012-01-30 09:04:36 +00002434 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002435 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002436 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002437 addr,
2438 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002439 size,
2440 UINT32_MAX,
2441 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002442 }
2443
2444 return sb_instructions;
2445}
2446
2447lldb::SBInstructionList
2448SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2449{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002450 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2451}
2452
2453lldb::SBInstructionList
2454SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2455{
2456 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002457}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002458
2459SBError
2460SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2461 lldb::addr_t section_base_addr)
2462{
2463 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002464 TargetSP target_sp(GetSP());
2465 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002466 {
2467 if (!section.IsValid())
2468 {
2469 sb_error.SetErrorStringWithFormat ("invalid section");
2470 }
2471 else
2472 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002473 SectionSP section_sp (section.GetSP());
2474 if (section_sp)
2475 {
2476 if (section_sp->IsThreadSpecific())
2477 {
2478 sb_error.SetErrorString ("thread specific sections are not yet supported");
2479 }
2480 else
2481 {
Greg Clayton3c947372013-01-29 01:17:09 +00002482 if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2483 {
2484 // Flush info in the process (stack frames, etc)
2485 ProcessSP process_sp (target_sp->GetProcessSP());
2486 if (process_sp)
2487 process_sp->Flush();
2488 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002489 }
2490 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002491 }
2492 }
2493 else
2494 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002495 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002496 }
2497 return sb_error;
2498}
2499
2500SBError
2501SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2502{
2503 SBError sb_error;
2504
Greg Claytonacdbe812012-01-30 09:04:36 +00002505 TargetSP target_sp(GetSP());
2506 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002507 {
2508 if (!section.IsValid())
2509 {
2510 sb_error.SetErrorStringWithFormat ("invalid section");
2511 }
2512 else
2513 {
Greg Clayton3c947372013-01-29 01:17:09 +00002514 if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2515 {
2516 // Flush info in the process (stack frames, etc)
2517 ProcessSP process_sp (target_sp->GetProcessSP());
2518 if (process_sp)
2519 process_sp->Flush();
2520 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002521 }
2522 }
2523 else
2524 {
2525 sb_error.SetErrorStringWithFormat ("invalid target");
2526 }
2527 return sb_error;
2528}
2529
2530SBError
2531SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2532{
2533 SBError sb_error;
2534
Greg Claytonacdbe812012-01-30 09:04:36 +00002535 TargetSP target_sp(GetSP());
2536 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002537 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002538 ModuleSP module_sp (module.GetSP());
2539 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002540 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002541 bool changed = false;
2542 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002543 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002544 // The load was successful, make sure that at least some sections
2545 // changed before we notify that our module was loaded.
2546 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002547 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002548 ModuleList module_list;
2549 module_list.Append(module_sp);
2550 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002551 // Flush info in the process (stack frames, etc)
2552 ProcessSP process_sp (target_sp->GetProcessSP());
2553 if (process_sp)
2554 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002555 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002556 }
2557 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002558 else
2559 {
2560 sb_error.SetErrorStringWithFormat ("invalid module");
2561 }
2562
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002563 }
2564 else
2565 {
2566 sb_error.SetErrorStringWithFormat ("invalid target");
2567 }
2568 return sb_error;
2569}
2570
2571SBError
2572SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2573{
2574 SBError sb_error;
2575
2576 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002577 TargetSP target_sp(GetSP());
2578 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002579 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002580 ModuleSP module_sp (module.GetSP());
2581 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002582 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002583 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002584 if (objfile)
2585 {
2586 SectionList *section_list = objfile->GetSectionList();
2587 if (section_list)
2588 {
Greg Clayton3c947372013-01-29 01:17:09 +00002589 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002590 const size_t num_sections = section_list->GetSize();
2591 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2592 {
2593 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2594 if (section_sp)
Greg Clayton3c947372013-01-29 01:17:09 +00002595 changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2596 }
2597 if (changed)
2598 {
2599 // Flush info in the process (stack frames, etc)
2600 ProcessSP process_sp (target_sp->GetProcessSP());
2601 if (process_sp)
2602 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002603 }
2604 }
2605 else
2606 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002607 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002608 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2609 }
2610 }
2611 else
2612 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002613 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002614 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2615 }
2616 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002617 else
2618 {
2619 sb_error.SetErrorStringWithFormat ("invalid module");
2620 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002621 }
2622 else
2623 {
2624 sb_error.SetErrorStringWithFormat ("invalid target");
2625 }
2626 return sb_error;
2627}
2628
2629
Greg Claytone14e1922012-12-04 02:22:16 +00002630lldb::SBSymbolContextList
2631SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2632{
2633 SBSymbolContextList sb_sc_list;
2634 if (name && name[0])
2635 {
2636 TargetSP target_sp(GetSP());
2637 if (target_sp)
2638 {
2639 bool append = true;
2640 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2641 symbol_type,
2642 *sb_sc_list,
2643 append);
2644 }
2645 }
2646 return sb_sc_list;
2647
2648}
2649
2650
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002651lldb::SBValue
2652SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2653{
Greg Clayton5160ce52013-03-27 23:08:40 +00002654 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2655 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002656 SBValue expr_result;
2657 ExecutionResults exe_results = eExecutionSetupError;
2658 ValueObjectSP expr_value_sp;
2659 TargetSP target_sp(GetSP());
Jason Molendab57e4a12013-11-04 09:33:30 +00002660 StackFrame *frame = NULL;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002661 if (target_sp)
2662 {
2663 if (expr == NULL || expr[0] == '\0')
2664 {
2665 if (log)
2666 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2667 return expr_result;
2668 }
2669
2670 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2671 ExecutionContext exe_ctx (m_opaque_sp.get());
2672
2673 if (log)
2674 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2675
2676 frame = exe_ctx.GetFramePtr();
2677 Target *target = exe_ctx.GetTargetPtr();
2678
2679 if (target)
2680 {
2681#ifdef LLDB_CONFIGURATION_DEBUG
2682 StreamString frame_description;
2683 if (frame)
2684 frame->DumpUsingSettingsFormat (&frame_description);
2685 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2686 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2687#endif
2688 exe_results = target->EvaluateExpression (expr,
2689 frame,
2690 expr_value_sp,
2691 options.ref());
2692
2693 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2694#ifdef LLDB_CONFIGURATION_DEBUG
2695 Host::SetCrashDescription (NULL);
2696#endif
2697 }
2698 else
2699 {
2700 if (log)
2701 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2702 }
2703 }
2704#ifndef LLDB_DISABLE_PYTHON
2705 if (expr_log)
2706 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2707 expr_result.GetValue(),
2708 expr_result.GetSummary());
2709
2710 if (log)
2711 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2712 frame,
2713 expr,
2714 expr_value_sp.get(),
2715 exe_results);
2716#endif
2717
2718 return expr_result;
2719}
2720
Greg Clayton13fbb992013-02-01 00:47:49 +00002721
2722lldb::addr_t
2723SBTarget::GetStackRedZoneSize()
2724{
2725 TargetSP target_sp(GetSP());
2726 if (target_sp)
2727 {
2728 ABISP abi_sp;
2729 ProcessSP process_sp (target_sp->GetProcessSP());
2730 if (process_sp)
2731 abi_sp = process_sp->GetABI();
2732 else
2733 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2734 if (abi_sp)
2735 return abi_sp->GetRedZoneSize();
2736 }
2737 return 0;
2738}
2739