blob: b9947d985a9810aa65f2d43805f52df99109557c [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
608SBProcess
609SBTarget::Launch
610(
Greg Clayton4b045622011-02-03 21:28:34 +0000611 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000612 char const **argv,
613 char const **envp,
614 const char *stdin_path,
615 const char *stdout_path,
616 const char *stderr_path,
617 const char *working_directory,
618 uint32_t launch_flags, // See LaunchFlags
619 bool stop_at_entry,
620 lldb::SBError& error
621)
622{
Greg Clayton5160ce52013-03-27 23:08:40 +0000623 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000624
Greg Claytonacdbe812012-01-30 09:04:36 +0000625 SBProcess sb_process;
626 ProcessSP process_sp;
627 TargetSP target_sp(GetSP());
628
Caroline Ticeceb6b132010-10-26 03:11:13 +0000629 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +0000630 {
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000631 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 +0000632 target_sp.get(),
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000633 argv,
634 envp,
635 stdin_path ? stdin_path : "NULL",
636 stdout_path ? stdout_path : "NULL",
637 stderr_path ? stderr_path : "NULL",
638 working_directory ? working_directory : "NULL",
639 launch_flags,
640 stop_at_entry,
641 error.get());
Greg Clayton93aa84e2010-10-29 04:59:35 +0000642 }
Greg Claytonacdbe812012-01-30 09:04:36 +0000643
644 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000646 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000647
Greg Clayton645bf542011-01-27 01:01:10 +0000648 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
649 launch_flags |= eLaunchFlagDisableASLR;
650
Greg Clayton2289fa42011-04-30 01:09:13 +0000651 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000652 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000653 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000654 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000655 state = process_sp->GetState();
Greg Clayton2289fa42011-04-30 01:09:13 +0000656
Greg Claytonb9556ac2012-01-30 07:41:31 +0000657 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton2289fa42011-04-30 01:09:13 +0000658 {
659 if (state == eStateAttaching)
660 error.SetErrorString ("process attach is in progress");
661 else
662 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000663 return sb_process;
664 }
Greg Clayton931180e2011-01-27 06:44:37 +0000665 }
666
Greg Clayton2289fa42011-04-30 01:09:13 +0000667 if (state == eStateConnected)
668 {
669 // If we are already connected, then we have already specified the
670 // listener, so if a valid listener is supplied, we need to error out
671 // to let the client know.
672 if (listener.IsValid())
673 {
674 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000675 return sb_process;
676 }
677 }
678 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000679 {
Greg Clayton4b045622011-02-03 21:28:34 +0000680 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000681 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton4b045622011-02-03 21:28:34 +0000682 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000683 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton2289fa42011-04-30 01:09:13 +0000684 }
Greg Clayton645bf542011-01-27 01:01:10 +0000685
Greg Claytonb9556ac2012-01-30 07:41:31 +0000686 if (process_sp)
Greg Clayton2289fa42011-04-30 01:09:13 +0000687 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000688 sb_process.SetSP (process_sp);
Greg Clayton2289fa42011-04-30 01:09:13 +0000689 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
690 launch_flags |= eLaunchFlagDisableSTDIO;
691
Greg Clayton982c9762011-11-03 21:22:33 +0000692 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
693
Greg Claytonacdbe812012-01-30 09:04:36 +0000694 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Clayton982c9762011-11-03 21:22:33 +0000695 if (exe_module)
Greg Claytonad9e8282011-11-29 04:03:30 +0000696 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton982c9762011-11-03 21:22:33 +0000697 if (argv)
698 launch_info.GetArguments().AppendArguments (argv);
699 if (envp)
700 launch_info.GetEnvironmentEntries ().SetArguments (envp);
701
Greg Claytonb9556ac2012-01-30 07:41:31 +0000702 error.SetError (process_sp->Launch (launch_info));
Greg Clayton2289fa42011-04-30 01:09:13 +0000703 if (error.Success())
Greg Clayton645bf542011-01-27 01:01:10 +0000704 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000705 // We we are stopping at the entry point, we can return now!
706 if (stop_at_entry)
707 return sb_process;
Greg Clayton645bf542011-01-27 01:01:10 +0000708
Greg Clayton2289fa42011-04-30 01:09:13 +0000709 // Make sure we are stopped at the entry
Greg Claytonb9556ac2012-01-30 07:41:31 +0000710 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton2289fa42011-04-30 01:09:13 +0000711 if (state == eStateStopped)
Greg Clayton645bf542011-01-27 01:01:10 +0000712 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000713 // resume the process to skip the entry point
Greg Claytonb9556ac2012-01-30 07:41:31 +0000714 error.SetError (process_sp->Resume());
Greg Clayton2289fa42011-04-30 01:09:13 +0000715 if (error.Success())
Greg Clayton645bf542011-01-27 01:01:10 +0000716 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000717 // If we are doing synchronous mode, then wait for the
718 // process to stop yet again!
Greg Claytonacdbe812012-01-30 09:04:36 +0000719 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +0000720 process_sp->WaitForProcessToStop (NULL);
Greg Clayton645bf542011-01-27 01:01:10 +0000721 }
722 }
723 }
Greg Clayton2289fa42011-04-30 01:09:13 +0000724 }
725 else
726 {
727 error.SetErrorString ("unable to create lldb_private::Process");
Greg Clayton524e60b2010-10-06 22:10:17 +0000728 }
729 }
730 else
731 {
732 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000734
Caroline Tice20ad3c42010-10-29 21:48:37 +0000735 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000736 if (log)
737 {
Sean Callanan575a4542012-10-20 00:21:31 +0000738 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +0000739 target_sp.get(), process_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000740 }
741
Greg Clayton5d5028b2010-10-06 03:53:16 +0000742 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743}
744
Greg Clayton0e615682012-02-24 05:03:03 +0000745SBProcess
746SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
747{
Greg Clayton5160ce52013-03-27 23:08:40 +0000748 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton0e615682012-02-24 05:03:03 +0000749
750 SBProcess sb_process;
751 ProcessSP process_sp;
752 TargetSP target_sp(GetSP());
753
754 if (log)
755 {
756 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
757 }
758
759 if (target_sp)
760 {
761 Mutex::Locker api_locker (target_sp->GetAPIMutex());
762 StateType state = eStateInvalid;
763 process_sp = target_sp->GetProcessSP();
764 if (process_sp)
765 {
766 state = process_sp->GetState();
767
768 if (process_sp->IsAlive() && state != eStateConnected)
769 {
770 if (state == eStateAttaching)
771 error.SetErrorString ("process attach is in progress");
772 else
773 error.SetErrorString ("a process is already being debugged");
774 return sb_process;
775 }
776 }
777
778 if (state != eStateConnected)
779 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
780
781 if (process_sp)
782 {
783 sb_process.SetSP (process_sp);
784 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
Greg Clayton38d1f052012-02-24 20:59:25 +0000785
Greg Clayton38d1f052012-02-24 20:59:25 +0000786 Module *exe_module = target_sp->GetExecutableModulePointer();
787 if (exe_module)
Han Ming Ong04cf1ba2012-03-02 01:02:04 +0000788 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton38d1f052012-02-24 20:59:25 +0000789
790 const ArchSpec &arch_spec = target_sp->GetArchitecture();
791 if (arch_spec.IsValid())
792 launch_info.GetArchitecture () = arch_spec;
793
Greg Clayton0e615682012-02-24 05:03:03 +0000794 error.SetError (process_sp->Launch (launch_info));
Greg Clayton38d1f052012-02-24 20:59:25 +0000795 const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
Greg Clayton0e615682012-02-24 05:03:03 +0000796 if (error.Success())
797 {
Greg Clayton0e615682012-02-24 05:03:03 +0000798 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Clayton38d1f052012-02-24 20:59:25 +0000799 {
800 // If we are doing synchronous mode, then wait for the initial
801 // stop to happen, else, return and let the caller watch for
802 // the stop
803 if (synchronous_execution)
Greg Clayton23f59502012-07-17 03:23:13 +0000804 process_sp->WaitForProcessToStop (NULL);
Greg Clayton38d1f052012-02-24 20:59:25 +0000805 // We we are stopping at the entry point, we can return now!
Greg Clayton0e615682012-02-24 05:03:03 +0000806 return sb_process;
Greg Clayton38d1f052012-02-24 20:59:25 +0000807 }
Greg Clayton0e615682012-02-24 05:03:03 +0000808
809 // Make sure we are stopped at the entry
Greg Clayton23f59502012-07-17 03:23:13 +0000810 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton0e615682012-02-24 05:03:03 +0000811 if (state == eStateStopped)
812 {
813 // resume the process to skip the entry point
814 error.SetError (process_sp->Resume());
815 if (error.Success())
816 {
817 // If we are doing synchronous mode, then wait for the
818 // process to stop yet again!
Greg Clayton38d1f052012-02-24 20:59:25 +0000819 if (synchronous_execution)
Greg Clayton0e615682012-02-24 05:03:03 +0000820 process_sp->WaitForProcessToStop (NULL);
821 }
822 }
823 }
824 }
825 else
826 {
827 error.SetErrorString ("unable to create lldb_private::Process");
828 }
829 }
830 else
831 {
832 error.SetErrorString ("SBTarget is invalid");
833 }
834
835 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
836 if (log)
837 {
Sean Callanan575a4542012-10-20 00:21:31 +0000838 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Greg Clayton0e615682012-02-24 05:03:03 +0000839 target_sp.get(), process_sp.get());
840 }
841
842 return sb_process;
843}
844
845lldb::SBProcess
846SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
847{
Greg Clayton5160ce52013-03-27 23:08:40 +0000848 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000849
Greg Clayton0e615682012-02-24 05:03:03 +0000850 SBProcess sb_process;
851 ProcessSP process_sp;
852 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000853
854 if (log)
855 {
856 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...", target_sp.get());
857 }
858
Greg Clayton0e615682012-02-24 05:03:03 +0000859 if (target_sp)
860 {
861 Mutex::Locker api_locker (target_sp->GetAPIMutex());
862
863 StateType state = eStateInvalid;
864 process_sp = target_sp->GetProcessSP();
865 if (process_sp)
866 {
867 state = process_sp->GetState();
868
869 if (process_sp->IsAlive() && state != eStateConnected)
870 {
871 if (state == eStateAttaching)
872 error.SetErrorString ("process attach is in progress");
873 else
874 error.SetErrorString ("a process is already being debugged");
Sean Callanan575a4542012-10-20 00:21:31 +0000875 if (log)
876 {
877 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
878 target_sp.get(), error.GetCString());
879 }
Greg Clayton0e615682012-02-24 05:03:03 +0000880 return sb_process;
881 }
882 }
883
884 if (state != eStateConnected)
885 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
886
887 if (process_sp)
888 {
Greg Clayton0e615682012-02-24 05:03:03 +0000889 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000890 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000891 {
892 PlatformSP platform_sp = target_sp->GetPlatform();
Greg Clayton91e407e2012-09-27 00:03:39 +0000893 // See if we can pre-verify if a process exists or not
894 if (platform_sp && platform_sp->IsConnected())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000895 {
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000896 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Clayton91e407e2012-09-27 00:03:39 +0000897 ProcessInstanceInfo instance_info;
898 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
899 {
900 attach_info.SetUserID(instance_info.GetEffectiveUserID());
901 }
902 else
903 {
Daniel Malead01b2952012-11-29 21:49:15 +0000904 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000905 if (log)
906 {
907 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
908 target_sp.get(), error.GetCString());
909 }
Greg Clayton91e407e2012-09-27 00:03:39 +0000910 return sb_process;
911 }
Han Ming Ongcec8c902012-02-29 00:12:56 +0000912 }
913 }
Han Ming Ongbee98392012-02-29 19:16:40 +0000914 error.SetError (process_sp->Attach (attach_info));
915 if (error.Success())
916 {
917 sb_process.SetSP (process_sp);
918 // If we are doing synchronous mode, then wait for the
919 // process to stop!
920 if (target_sp->GetDebugger().GetAsyncExecution () == false)
921 process_sp->WaitForProcessToStop (NULL);
922 }
Greg Clayton0e615682012-02-24 05:03:03 +0000923 }
924 else
925 {
926 error.SetErrorString ("unable to create lldb_private::Process");
927 }
928 }
929 else
930 {
931 error.SetErrorString ("SBTarget is invalid");
932 }
Sean Callanan575a4542012-10-20 00:21:31 +0000933
934 if (log)
935 {
936 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
937 target_sp.get(), process_sp.get());
938 }
939
Greg Clayton0e615682012-02-24 05:03:03 +0000940 return sb_process;
941}
942
943
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000944#if defined(__APPLE__)
945
946lldb::SBProcess
947SBTarget::AttachToProcessWithID (SBListener &listener,
948 ::pid_t pid,
949 lldb::SBError& error)
950{
951 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
952}
953
954#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000955
956lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000957SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000958(
Greg Clayton4b045622011-02-03 21:28:34 +0000959 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000960 lldb::pid_t pid,// The process ID to attach to
961 SBError& error // An error explaining what went wrong if attach fails
962)
963{
Greg Clayton5160ce52013-03-27 23:08:40 +0000964 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000965
Greg Clayton524e60b2010-10-06 22:10:17 +0000966 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000967 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000968 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000969
970 if (log)
971 {
Daniel Malead01b2952012-11-29 21:49:15 +0000972 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...", target_sp.get(), pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000973 }
974
Greg Claytonacdbe812012-01-30 09:04:36 +0000975 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000976 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000977 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0c74e782011-06-24 03:21:43 +0000978
979 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000980 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000981 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +0000982 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000983 state = process_sp->GetState();
Greg Clayton0c74e782011-06-24 03:21:43 +0000984
Greg Claytonb9556ac2012-01-30 07:41:31 +0000985 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton0c74e782011-06-24 03:21:43 +0000986 {
987 if (state == eStateAttaching)
988 error.SetErrorString ("process attach is in progress");
989 else
990 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +0000991 return sb_process;
992 }
993 }
994
995 if (state == eStateConnected)
996 {
997 // If we are already connected, then we have already specified the
998 // listener, so if a valid listener is supplied, we need to error out
999 // to let the client know.
1000 if (listener.IsValid())
1001 {
1002 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001003 return sb_process;
1004 }
1005 }
Greg Clayton4b045622011-02-03 21:28:34 +00001006 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001007 {
1008 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001009 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001010 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001011 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001012 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001013 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001014 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001015 sb_process.SetSP (process_sp);
1016
Greg Clayton144f3a92011-11-15 03:53:30 +00001017 ProcessAttachInfo attach_info;
1018 attach_info.SetProcessID (pid);
Han Ming Ong84647042012-02-25 01:07:38 +00001019
1020 PlatformSP platform_sp = target_sp->GetPlatform();
1021 ProcessInstanceInfo instance_info;
1022 if (platform_sp->GetProcessInfo(pid, instance_info))
1023 {
1024 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ong84647042012-02-25 01:07:38 +00001025 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001026 error.SetError (process_sp->Attach (attach_info));
Greg Claytone2186ed2012-09-07 17:51:47 +00001027 if (error.Success())
1028 {
1029 // If we are doing synchronous mode, then wait for the
1030 // process to stop!
1031 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +00001032 process_sp->WaitForProcessToStop (NULL);
Greg Claytone2186ed2012-09-07 17:51:47 +00001033 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001034 }
1035 else
1036 {
1037 error.SetErrorString ("unable to create lldb_private::Process");
1038 }
1039 }
1040 else
1041 {
1042 error.SetErrorString ("SBTarget is invalid");
1043 }
Sean Callanan575a4542012-10-20 00:21:31 +00001044
1045 if (log)
1046 {
1047 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
1048 target_sp.get(), process_sp.get());
1049 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001050 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001051}
1052
1053lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +00001054SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +00001055(
Greg Clayton4b045622011-02-03 21:28:34 +00001056 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +00001057 const char *name, // basename of process to attach to
1058 bool wait_for, // if true wait for a new instance of "name" to be launched
1059 SBError& error // An error explaining what went wrong if attach fails
1060)
1061{
Greg Clayton5160ce52013-03-27 23:08:40 +00001062 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001063
Greg Clayton524e60b2010-10-06 22:10:17 +00001064 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001065 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001066 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001067
1068 if (log)
1069 {
1070 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...", target_sp.get(), name, wait_for ? "true" : "false");
1071 }
1072
Greg Claytonacdbe812012-01-30 09:04:36 +00001073 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001074 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001075 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +00001076
Greg Clayton0c74e782011-06-24 03:21:43 +00001077 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +00001078 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +00001079 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +00001080 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001081 state = process_sp->GetState();
Greg Clayton0c74e782011-06-24 03:21:43 +00001082
Greg Claytonb9556ac2012-01-30 07:41:31 +00001083 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton0c74e782011-06-24 03:21:43 +00001084 {
1085 if (state == eStateAttaching)
1086 error.SetErrorString ("process attach is in progress");
1087 else
1088 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001089 return sb_process;
1090 }
1091 }
1092
1093 if (state == eStateConnected)
1094 {
1095 // If we are already connected, then we have already specified the
1096 // listener, so if a valid listener is supplied, we need to error out
1097 // to let the client know.
1098 if (listener.IsValid())
1099 {
1100 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001101 return sb_process;
1102 }
1103 }
Greg Clayton4b045622011-02-03 21:28:34 +00001104 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001105 {
1106 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001107 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001108 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001109 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001110 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001111
Greg Claytonb9556ac2012-01-30 07:41:31 +00001112 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001113 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001114 sb_process.SetSP (process_sp);
Greg Clayton144f3a92011-11-15 03:53:30 +00001115 ProcessAttachInfo attach_info;
1116 attach_info.GetExecutableFile().SetFile(name, false);
1117 attach_info.SetWaitForLaunch(wait_for);
Greg Claytonb9556ac2012-01-30 07:41:31 +00001118 error.SetError (process_sp->Attach (attach_info));
Daniel Malead659dc12013-04-01 19:47:00 +00001119 if (error.Success())
1120 {
1121 // If we are doing synchronous mode, then wait for the
1122 // process to stop!
1123 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1124 process_sp->WaitForProcessToStop (NULL);
1125 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001126 }
1127 else
1128 {
1129 error.SetErrorString ("unable to create lldb_private::Process");
1130 }
1131 }
1132 else
1133 {
1134 error.SetErrorString ("SBTarget is invalid");
1135 }
Sean Callanan575a4542012-10-20 00:21:31 +00001136
1137 if (log)
1138 {
1139 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1140 target_sp.get(), process_sp.get());
1141 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001142 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001143}
1144
James McIlree9631aae2011-03-04 00:31:13 +00001145lldb::SBProcess
1146SBTarget::ConnectRemote
1147(
1148 SBListener &listener,
1149 const char *url,
1150 const char *plugin_name,
1151 SBError& error
1152)
1153{
Greg Clayton5160ce52013-03-27 23:08:40 +00001154 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001155
James McIlree9631aae2011-03-04 00:31:13 +00001156 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001157 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001158 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001159
1160 if (log)
1161 {
1162 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1163 }
1164
Greg Claytonacdbe812012-01-30 09:04:36 +00001165 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001166 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001167 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +00001168 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001169 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001170 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001171 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001172
1173
Greg Claytonb9556ac2012-01-30 07:41:31 +00001174 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001175 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001176 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001177 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +00001178 }
1179 else
1180 {
1181 error.SetErrorString ("unable to create lldb_private::Process");
1182 }
1183 }
1184 else
1185 {
1186 error.SetErrorString ("SBTarget is invalid");
1187 }
Sean Callanan575a4542012-10-20 00:21:31 +00001188
1189 if (log)
1190 {
1191 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1192 target_sp.get(), process_sp.get());
1193 }
James McIlree9631aae2011-03-04 00:31:13 +00001194 return sb_process;
1195}
1196
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197SBFileSpec
1198SBTarget::GetExecutable ()
1199{
Caroline Ticeceb6b132010-10-26 03:11:13 +00001200
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +00001202 TargetSP target_sp(GetSP());
1203 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001205 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +00001206 if (exe_module)
1207 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001209
Greg Clayton5160ce52013-03-27 23:08:40 +00001210 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001211 if (log)
1212 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001213 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001214 target_sp.get(), exe_file_spec.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001215 }
1216
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 return exe_file_spec;
1218}
1219
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221SBTarget::operator == (const SBTarget &rhs) const
1222{
Greg Clayton66111032010-06-23 01:19:29 +00001223 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224}
1225
1226bool
1227SBTarget::operator != (const SBTarget &rhs) const
1228{
Greg Clayton66111032010-06-23 01:19:29 +00001229 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230}
1231
Greg Claytonb9556ac2012-01-30 07:41:31 +00001232lldb::TargetSP
1233SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +00001234{
1235 return m_opaque_sp;
1236}
1237
Greg Clayton66111032010-06-23 01:19:29 +00001238void
Greg Claytonb9556ac2012-01-30 07:41:31 +00001239SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +00001240{
1241 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242}
1243
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001244lldb::SBAddress
1245SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001246{
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001247 lldb::SBAddress sb_addr;
1248 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +00001249 TargetSP target_sp(GetSP());
1250 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001251 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001252 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1253 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001254 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001255 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001256
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001257 // We have a load address that isn't in a section, just return an address
1258 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +00001259 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001260 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001261}
1262
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001263SBSymbolContext
1264SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1265{
1266 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001267 if (addr.IsValid())
1268 {
1269 TargetSP target_sp(GetSP());
1270 if (target_sp)
1271 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1272 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001273 return sc;
1274}
1275
1276
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277SBBreakpoint
1278SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1279{
Greg Clayton7481c202010-11-08 00:28:40 +00001280 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281}
1282
1283SBBreakpoint
1284SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1285{
Greg Clayton5160ce52013-03-27 23:08:40 +00001286 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001287
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001289 TargetSP target_sp(GetSP());
1290 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001291 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001292 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001293
Greg Clayton1f746072012-08-29 21:13:06 +00001294 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001295 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001296 const bool internal = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001297 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001298 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001299
1300 if (log)
1301 {
1302 SBStream sstr;
1303 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001304 char path[PATH_MAX];
1305 sb_file_spec->GetPath (path, sizeof(path));
1306 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytonacdbe812012-01-30 09:04:36 +00001307 target_sp.get(),
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001308 path,
Greg Clayton48381312010-10-30 04:51:46 +00001309 line,
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001310 sb_bp.get(),
Caroline Ticeceb6b132010-10-26 03:11:13 +00001311 sstr.GetData());
1312 }
1313
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314 return sb_bp;
1315}
1316
1317SBBreakpoint
1318SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1319{
Greg Clayton5160ce52013-03-27 23:08:40 +00001320 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001321
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001323 TargetSP target_sp(GetSP());
1324 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001325 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001326 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001327
1328 const bool internal = false;
1329 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330 if (module_name && module_name[0])
1331 {
Jim Ingham969795f2011-09-21 01:17:13 +00001332 FileSpecList module_spec_list;
1333 module_spec_list.Append (FileSpec (module_name, false));
Jim Inghama8558b62012-05-22 00:12:20 +00001334 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001335 }
1336 else
1337 {
Jim Inghama8558b62012-05-22 00:12:20 +00001338 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339 }
1340 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001341
1342 if (log)
1343 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001344 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001345 target_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001346 }
1347
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348 return sb_bp;
1349}
1350
Jim Ingham87df91b2011-09-23 00:54:11 +00001351lldb::SBBreakpoint
1352SBTarget::BreakpointCreateByName (const char *symbol_name,
1353 const SBFileSpecList &module_list,
1354 const SBFileSpecList &comp_unit_list)
1355{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001356 uint32_t name_type_mask = eFunctionNameTypeAuto;
1357 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1358}
1359
1360lldb::SBBreakpoint
1361SBTarget::BreakpointCreateByName (const char *symbol_name,
1362 uint32_t name_type_mask,
1363 const SBFileSpecList &module_list,
1364 const SBFileSpecList &comp_unit_list)
1365{
Greg Clayton5160ce52013-03-27 23:08:40 +00001366 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001367
1368 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001369 TargetSP target_sp(GetSP());
1370 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001371 {
Jim Inghama8558b62012-05-22 00:12:20 +00001372 const bool internal = false;
1373 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001374 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1375 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Jim Ingham87df91b2011-09-23 00:54:11 +00001376 comp_unit_list.get(),
1377 symbol_name,
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001378 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001379 skip_prologue,
1380 internal);
Jim Ingham87df91b2011-09-23 00:54:11 +00001381 }
1382
1383 if (log)
1384 {
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001385 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001386 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001387 }
1388
1389 return sb_bp;
1390}
1391
Jim Inghamfab10e82012-03-06 00:37:27 +00001392lldb::SBBreakpoint
1393SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1394 uint32_t num_names,
1395 uint32_t name_type_mask,
1396 const SBFileSpecList &module_list,
1397 const SBFileSpecList &comp_unit_list)
1398{
Greg Clayton5160ce52013-03-27 23:08:40 +00001399 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001400
1401 SBBreakpoint sb_bp;
1402 TargetSP target_sp(GetSP());
1403 if (target_sp && num_names > 0)
1404 {
1405 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001406 const bool internal = false;
1407 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001408 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1409 comp_unit_list.get(),
1410 symbol_names,
1411 num_names,
1412 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001413 skip_prologue,
1414 internal);
Jim Inghamfab10e82012-03-06 00:37:27 +00001415 }
1416
1417 if (log)
1418 {
1419 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1420 for (uint32_t i = 0 ; i < num_names; i++)
1421 {
1422 char sep;
1423 if (i < num_names - 1)
1424 sep = ',';
1425 else
1426 sep = '}';
1427 if (symbol_names[i] != NULL)
1428 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1429 else
1430 log->Printf ("\"<NULL>\"%c ", sep);
1431
1432 }
1433 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1434 }
1435
1436 return sb_bp;
1437}
Jim Ingham87df91b2011-09-23 00:54:11 +00001438
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439SBBreakpoint
1440SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1441{
Greg Clayton5160ce52013-03-27 23:08:40 +00001442 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001443
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001444 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001445 TargetSP target_sp(GetSP());
1446 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001448 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001449 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001450 const bool internal = false;
1451 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452
1453 if (module_name && module_name[0])
1454 {
Jim Ingham969795f2011-09-21 01:17:13 +00001455 FileSpecList module_spec_list;
1456 module_spec_list.Append (FileSpec (module_name, false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457
Jim Inghama8558b62012-05-22 00:12:20 +00001458 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459 }
1460 else
1461 {
Jim Inghama8558b62012-05-22 00:12:20 +00001462 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463 }
1464 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001465
1466 if (log)
1467 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001468 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001469 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001470 }
1471
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 return sb_bp;
1473}
1474
Jim Ingham87df91b2011-09-23 00:54:11 +00001475lldb::SBBreakpoint
1476SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1477 const SBFileSpecList &module_list,
1478 const SBFileSpecList &comp_unit_list)
1479{
Greg Clayton5160ce52013-03-27 23:08:40 +00001480 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481
Jim Ingham87df91b2011-09-23 00:54:11 +00001482 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001483 TargetSP target_sp(GetSP());
1484 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001485 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001486 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001487 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001488 const bool internal = false;
1489 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Ingham87df91b2011-09-23 00:54:11 +00001490
Jim Inghama8558b62012-05-22 00:12:20 +00001491 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
Jim Ingham87df91b2011-09-23 00:54:11 +00001492 }
1493
1494 if (log)
1495 {
1496 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001497 target_sp.get(), symbol_name_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001498 }
1499
1500 return sb_bp;
1501}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502
1503SBBreakpoint
1504SBTarget::BreakpointCreateByAddress (addr_t address)
1505{
Greg Clayton5160ce52013-03-27 23:08:40 +00001506 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001507
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001509 TargetSP target_sp(GetSP());
1510 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001511 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001512 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1513 *sb_bp = target_sp->CreateBreakpoint (address, false);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001514 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001515
1516 if (log)
1517 {
Daniel Malead01b2952012-11-29 21:49:15 +00001518 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 +00001519 }
1520
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521 return sb_bp;
1522}
1523
Jim Ingham969795f2011-09-21 01:17:13 +00001524lldb::SBBreakpoint
1525SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1526{
Greg Clayton5160ce52013-03-27 23:08:40 +00001527 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001528
1529 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001530 TargetSP target_sp(GetSP());
1531 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001532 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001533 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001534 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001535 FileSpecList source_file_spec_list;
1536 source_file_spec_list.Append (source_file.ref());
Jim Ingham969795f2011-09-21 01:17:13 +00001537
1538 if (module_name && module_name[0])
1539 {
1540 FileSpecList module_spec_list;
1541 module_spec_list.Append (FileSpec (module_name, false));
1542
Greg Claytonacdbe812012-01-30 09:04:36 +00001543 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
Jim Ingham969795f2011-09-21 01:17:13 +00001544 }
1545 else
1546 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001547 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
Jim Ingham969795f2011-09-21 01:17:13 +00001548 }
1549 }
1550
1551 if (log)
1552 {
1553 char path[PATH_MAX];
1554 source_file->GetPath (path, sizeof(path));
1555 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001556 target_sp.get(), source_regex, path, module_name, sb_bp.get());
Jim Ingham969795f2011-09-21 01:17:13 +00001557 }
1558
1559 return sb_bp;
1560}
1561
Jim Ingham87df91b2011-09-23 00:54:11 +00001562lldb::SBBreakpoint
1563SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1564 const SBFileSpecList &module_list,
1565 const lldb::SBFileSpecList &source_file_list)
1566{
Greg Clayton5160ce52013-03-27 23:08:40 +00001567 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001568
1569 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001570 TargetSP target_sp(GetSP());
1571 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001572 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001573 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001574 RegularExpression regexp(source_regex);
Greg Claytonacdbe812012-01-30 09:04:36 +00001575 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
Jim Ingham87df91b2011-09-23 00:54:11 +00001576 }
1577
1578 if (log)
1579 {
1580 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001581 target_sp.get(), source_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001582 }
1583
1584 return sb_bp;
1585}
Jim Ingham969795f2011-09-21 01:17:13 +00001586
Jim Inghamfab10e82012-03-06 00:37:27 +00001587lldb::SBBreakpoint
1588SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1589 bool catch_bp,
1590 bool throw_bp)
1591{
Greg Clayton5160ce52013-03-27 23:08:40 +00001592 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001593
1594 SBBreakpoint sb_bp;
1595 TargetSP target_sp(GetSP());
1596 if (target_sp)
1597 {
1598 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1599 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1600 }
1601
1602 if (log)
1603 {
1604 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1605 target_sp.get(),
1606 LanguageRuntime::GetNameForLanguageType(language),
1607 catch_bp ? "on" : "off",
1608 throw_bp ? "on" : "off",
1609 sb_bp.get());
1610 }
1611
1612 return sb_bp;
1613}
1614
Greg Clayton9fed0d82010-07-23 23:33:17 +00001615uint32_t
1616SBTarget::GetNumBreakpoints () const
1617{
Greg Claytonacdbe812012-01-30 09:04:36 +00001618 TargetSP target_sp(GetSP());
1619 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001620 {
1621 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001622 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001623 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001624 return 0;
1625}
1626
1627SBBreakpoint
1628SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1629{
1630 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001631 TargetSP target_sp(GetSP());
1632 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001633 {
1634 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001635 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001636 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001637 return sb_breakpoint;
1638}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639
1640bool
1641SBTarget::BreakpointDelete (break_id_t bp_id)
1642{
Greg Clayton5160ce52013-03-27 23:08:40 +00001643 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001644
Caroline Ticeceb6b132010-10-26 03:11:13 +00001645 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001646 TargetSP target_sp(GetSP());
1647 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001648 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001649 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1650 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001651 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001652
1653 if (log)
1654 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001655 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001656 }
1657
1658 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001659}
1660
Johnny Chen5d043462011-09-26 22:40:50 +00001661SBBreakpoint
1662SBTarget::FindBreakpointByID (break_id_t bp_id)
1663{
Greg Clayton5160ce52013-03-27 23:08:40 +00001664 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001665
1666 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001667 TargetSP target_sp(GetSP());
1668 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001669 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001670 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1671 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001672 }
1673
1674 if (log)
1675 {
1676 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001677 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001678 }
1679
1680 return sb_breakpoint;
1681}
1682
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683bool
1684SBTarget::EnableAllBreakpoints ()
1685{
Greg Claytonacdbe812012-01-30 09:04:36 +00001686 TargetSP target_sp(GetSP());
1687 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001689 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1690 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691 return true;
1692 }
1693 return false;
1694}
1695
1696bool
1697SBTarget::DisableAllBreakpoints ()
1698{
Greg Claytonacdbe812012-01-30 09:04:36 +00001699 TargetSP target_sp(GetSP());
1700 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001702 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1703 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704 return true;
1705 }
1706 return false;
1707}
1708
1709bool
1710SBTarget::DeleteAllBreakpoints ()
1711{
Greg Claytonacdbe812012-01-30 09:04:36 +00001712 TargetSP target_sp(GetSP());
1713 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001714 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001715 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1716 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001717 return true;
1718 }
1719 return false;
1720}
1721
Johnny Chen5d043462011-09-26 22:40:50 +00001722uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001723SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001724{
Greg Claytonacdbe812012-01-30 09:04:36 +00001725 TargetSP target_sp(GetSP());
1726 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001727 {
Johnny Chen01a67862011-10-14 00:42:25 +00001728 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001729 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001730 }
1731 return 0;
1732}
1733
Greg Clayton1b282f92011-10-13 18:08:26 +00001734SBWatchpoint
1735SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001736{
Johnny Chen01a67862011-10-14 00:42:25 +00001737 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001738 TargetSP target_sp(GetSP());
1739 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001740 {
Johnny Chen01a67862011-10-14 00:42:25 +00001741 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001742 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001743 }
Johnny Chen01a67862011-10-14 00:42:25 +00001744 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001745}
1746
1747bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001748SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001749{
Greg Clayton5160ce52013-03-27 23:08:40 +00001750 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001751
1752 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001753 TargetSP target_sp(GetSP());
1754 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001755 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001756 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001757 Mutex::Locker locker;
1758 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001759 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001760 }
1761
1762 if (log)
1763 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001764 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
Johnny Chen5d043462011-09-26 22:40:50 +00001765 }
1766
1767 return result;
1768}
1769
Greg Clayton1b282f92011-10-13 18:08:26 +00001770SBWatchpoint
1771SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001772{
Greg Clayton5160ce52013-03-27 23:08:40 +00001773 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001774
Greg Clayton1b282f92011-10-13 18:08:26 +00001775 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001776 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001777 TargetSP target_sp(GetSP());
1778 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001779 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001780 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001781 Mutex::Locker locker;
1782 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001783 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1784 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001785 }
1786
1787 if (log)
1788 {
Johnny Chen01a67862011-10-14 00:42:25 +00001789 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001790 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001791 }
1792
Greg Clayton1b282f92011-10-13 18:08:26 +00001793 return sb_watchpoint;
1794}
1795
1796lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001797SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001798{
Greg Clayton5160ce52013-03-27 23:08:40 +00001799 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1b282f92011-10-13 18:08:26 +00001800
1801 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001802 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001803 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001804 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001805 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001806 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001807 uint32_t watch_type = 0;
1808 if (read)
1809 watch_type |= LLDB_WATCH_TYPE_READ;
1810 if (write)
1811 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001812 if (watch_type == 0)
1813 {
1814 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1815 return sb_watchpoint;
1816 }
1817
Johnny Chen7385a5a2012-05-31 22:56:36 +00001818 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001819 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001820 // This API doesn't take in a type, so we can't figure out what it is.
1821 ClangASTType *type = NULL;
1822 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001823 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001824 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001825 }
1826
1827 if (log)
1828 {
Daniel Malead01b2952012-11-29 21:49:15 +00001829 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001830 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
Greg Clayton1b282f92011-10-13 18:08:26 +00001831 }
1832
1833 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001834}
1835
1836bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001837SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001838{
Greg Claytonacdbe812012-01-30 09:04:36 +00001839 TargetSP target_sp(GetSP());
1840 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001841 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001842 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001843 Mutex::Locker locker;
1844 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001845 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001846 return true;
1847 }
1848 return false;
1849}
1850
1851bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001852SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001853{
Greg Claytonacdbe812012-01-30 09:04:36 +00001854 TargetSP target_sp(GetSP());
1855 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001856 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001857 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001858 Mutex::Locker locker;
1859 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001860 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001861 return true;
1862 }
1863 return false;
1864}
1865
Enrico Granata347c2aa2013-10-08 21:49:02 +00001866SBValue
1867SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1868{
1869 SBValue sb_value;
1870 lldb::ValueObjectSP new_value_sp;
1871 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1872 {
1873 lldb::addr_t address(addr.GetLoadAddress(*this));
1874 lldb::TypeImplSP type_impl_sp (type.GetSP());
1875 ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType().GetPointerType ());
1876 if (pointer_ast_type)
1877 {
1878 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
1879
1880 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1881 ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1882 pointer_ast_type,
1883 ConstString(name),
1884 buffer,
1885 exe_ctx.GetByteOrder(),
1886 exe_ctx.GetAddressByteSize()));
1887
1888 if (ptr_result_valobj_sp)
1889 {
1890 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1891 Error err;
1892 new_value_sp = ptr_result_valobj_sp->Dereference(err);
1893 if (new_value_sp)
1894 new_value_sp->SetName(ConstString(name));
1895 }
1896 }
1897 }
1898 sb_value.SetSP(new_value_sp);
1899 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1900 if (log)
1901 {
1902 if (new_value_sp)
1903 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"", m_opaque_sp.get(), new_value_sp->GetName().AsCString());
1904 else
1905 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL", m_opaque_sp.get());
1906 }
1907 return sb_value;
1908}
1909
Johnny Chen5d043462011-09-26 22:40:50 +00001910bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001911SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001912{
Greg Claytonacdbe812012-01-30 09:04:36 +00001913 TargetSP target_sp(GetSP());
1914 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001915 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001916 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001917 Mutex::Locker locker;
1918 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001919 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001920 return true;
1921 }
1922 return false;
1923}
1924
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001925
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001926lldb::SBModule
1927SBTarget::AddModule (const char *path,
1928 const char *triple,
1929 const char *uuid_cstr)
1930{
Greg Claytonb210aec2012-04-23 20:23:39 +00001931 return AddModule (path, triple, uuid_cstr, NULL);
1932}
1933
1934lldb::SBModule
1935SBTarget::AddModule (const char *path,
1936 const char *triple,
1937 const char *uuid_cstr,
1938 const char *symfile)
1939{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001940 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001941 TargetSP target_sp(GetSP());
1942 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001943 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001944 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001945 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001946 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001947
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001948 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001949 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001950
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001951 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001952 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00001953 else
1954 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00001955
1956 if (symfile)
1957 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001958
Greg Claytonb9a01b32012-02-26 05:51:37 +00001959 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001960 }
1961 return sb_module;
1962}
1963
Greg Clayton226cce22013-07-08 22:22:41 +00001964lldb::SBModule
1965SBTarget::AddModule (const SBModuleSpec &module_spec)
1966{
1967 lldb::SBModule sb_module;
1968 TargetSP target_sp(GetSP());
1969 if (target_sp)
1970 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1971 return sb_module;
1972}
1973
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001974bool
1975SBTarget::AddModule (lldb::SBModule &module)
1976{
Greg Claytonacdbe812012-01-30 09:04:36 +00001977 TargetSP target_sp(GetSP());
1978 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001979 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001980 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001981 return true;
1982 }
1983 return false;
1984}
1985
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986uint32_t
1987SBTarget::GetNumModules () const
1988{
Greg Clayton5160ce52013-03-27 23:08:40 +00001989 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001990
Caroline Ticeceb6b132010-10-26 03:11:13 +00001991 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00001992 TargetSP target_sp(GetSP());
1993 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001994 {
1995 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001996 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001997 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001998
1999 if (log)
Greg Claytonacdbe812012-01-30 09:04:36 +00002000 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002001
2002 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002003}
2004
Greg Clayton48e42542010-07-30 20:12:55 +00002005void
2006SBTarget::Clear ()
2007{
Greg Clayton5160ce52013-03-27 23:08:40 +00002008 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002009
2010 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002011 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002012
Greg Clayton48e42542010-07-30 20:12:55 +00002013 m_opaque_sp.reset();
2014}
2015
2016
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002017SBModule
2018SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2019{
2020 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002021 TargetSP target_sp(GetSP());
2022 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00002023 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002024 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002025 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00002026 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00002027 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028 return sb_module;
2029}
2030
Greg Clayton13d19502012-01-29 06:07:39 +00002031lldb::ByteOrder
2032SBTarget::GetByteOrder ()
2033{
Greg Claytonacdbe812012-01-30 09:04:36 +00002034 TargetSP target_sp(GetSP());
2035 if (target_sp)
2036 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00002037 return eByteOrderInvalid;
2038}
2039
2040const char *
2041SBTarget::GetTriple ()
2042{
Greg Claytonacdbe812012-01-30 09:04:36 +00002043 TargetSP target_sp(GetSP());
2044 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00002045 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002046 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00002047 // Unique the string so we don't run into ownership issues since
2048 // the const strings put the string into the string pool once and
2049 // the strings never comes out
2050 ConstString const_triple (triple.c_str());
2051 return const_triple.GetCString();
2052 }
2053 return NULL;
2054}
2055
2056uint32_t
2057SBTarget::GetAddressByteSize()
2058{
Greg Claytonacdbe812012-01-30 09:04:36 +00002059 TargetSP target_sp(GetSP());
2060 if (target_sp)
2061 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00002062 return sizeof(void*);
2063}
2064
2065
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002066SBModule
2067SBTarget::GetModuleAtIndex (uint32_t idx)
2068{
Greg Clayton5160ce52013-03-27 23:08:40 +00002069 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002070
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002071 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002072 ModuleSP module_sp;
2073 TargetSP target_sp(GetSP());
2074 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002075 {
2076 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002077 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2078 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002079 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002080
2081 if (log)
2082 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00002083 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002084 target_sp.get(), idx, module_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002085 }
2086
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002087 return sb_module;
2088}
2089
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002090bool
2091SBTarget::RemoveModule (lldb::SBModule module)
2092{
Greg Claytonacdbe812012-01-30 09:04:36 +00002093 TargetSP target_sp(GetSP());
2094 if (target_sp)
2095 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002096 return false;
2097}
2098
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002099
2100SBBroadcaster
2101SBTarget::GetBroadcaster () const
2102{
Greg Clayton5160ce52013-03-27 23:08:40 +00002103 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002104
Greg Claytonacdbe812012-01-30 09:04:36 +00002105 TargetSP target_sp(GetSP());
2106 SBBroadcaster broadcaster(target_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002107
2108 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002109 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002110 target_sp.get(), broadcaster.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002111
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002112 return broadcaster;
2113}
2114
Caroline Ticedde9cff2010-09-20 05:20:02 +00002115bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002116SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002117{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002118 Stream &strm = description.ref();
2119
Greg Claytonacdbe812012-01-30 09:04:36 +00002120 TargetSP target_sp(GetSP());
2121 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002122 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002123 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002124 }
2125 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002126 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002127
2128 return true;
2129}
2130
Greg Clayton5569e642012-02-06 01:44:54 +00002131lldb::SBSymbolContextList
2132SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002133{
Greg Clayton5569e642012-02-06 01:44:54 +00002134 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002135 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002136 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002137 TargetSP target_sp(GetSP());
2138 if (target_sp)
2139 {
2140 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002141 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002142 const bool append = true;
2143 target_sp->GetImages().FindFunctions (ConstString(name),
2144 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002145 symbols_ok,
2146 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002147 append,
2148 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002149 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002150 }
Greg Clayton5569e642012-02-06 01:44:54 +00002151 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002152}
2153
Enrico Granata6f3533f2011-07-29 19:53:35 +00002154lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002155SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002156{
Greg Claytonacdbe812012-01-30 09:04:36 +00002157 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002158 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002159 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002160 ConstString const_typename(typename_cstr);
2161 SymbolContext sc;
2162 const bool exact_match = false;
2163
2164 const ModuleList &module_list = target_sp->GetImages();
2165 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002166 for (size_t idx = 0; idx < count; idx++)
2167 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002168 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2169 if (module_sp)
2170 {
2171 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2172 if (type_sp)
2173 return SBType(type_sp);
2174 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002175 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002176
2177 // Didn't find the type in the symbols; try the Objective-C runtime
2178 // if one is installed
2179
2180 ProcessSP process_sp(target_sp->GetProcessSP());
2181
2182 if (process_sp)
2183 {
2184 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2185
2186 if (objc_language_runtime)
2187 {
2188 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2189
2190 if (objc_type_vendor)
2191 {
2192 std::vector <ClangASTType> types;
2193
2194 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2195 return SBType(types[0]);
2196 }
2197 }
2198 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002199
2200 // No matches, search for basic typename matches
2201 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2202 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002203 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002204 }
2205 return SBType();
2206}
2207
Greg Claytonb43165b2012-12-05 21:24:42 +00002208SBType
2209SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002210{
Greg Claytonacdbe812012-01-30 09:04:36 +00002211 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002212 if (target_sp)
2213 {
2214 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2215 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002216 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00002217 }
2218 return SBType();
2219}
2220
2221
2222lldb::SBTypeList
2223SBTarget::FindTypes (const char* typename_cstr)
2224{
2225 SBTypeList sb_type_list;
2226 TargetSP target_sp(GetSP());
2227 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002228 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002229 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002230 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002231 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002232 SymbolContext sc;
2233 TypeList type_list;
2234
Greg Clayton29399a22012-04-06 17:41:13 +00002235 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002236 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002237 exact_match,
2238 UINT32_MAX,
2239 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002240
Greg Claytonb43165b2012-12-05 21:24:42 +00002241 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002242 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002243 for (size_t idx = 0; idx < num_matches; idx++)
2244 {
2245 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2246 if (type_sp)
2247 sb_type_list.Append(SBType(type_sp));
2248 }
2249 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002250
2251 // Try the Objective-C runtime if one is installed
2252
2253 ProcessSP process_sp(target_sp->GetProcessSP());
2254
2255 if (process_sp)
2256 {
2257 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2258
2259 if (objc_language_runtime)
2260 {
2261 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2262
2263 if (objc_type_vendor)
2264 {
2265 std::vector <ClangASTType> types;
2266
2267 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2268 {
2269 for (ClangASTType &type : types)
2270 {
2271 sb_type_list.Append(SBType(type));
2272 }
2273 }
2274 }
2275 }
2276 }
2277
2278 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002279 {
2280 // No matches, search for basic typename matches
2281 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2282 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002283 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002284 }
2285 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002286 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002287}
2288
Greg Claytondea8cb42011-06-29 22:09:02 +00002289SBValueList
2290SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2291{
2292 SBValueList sb_value_list;
2293
Greg Claytonacdbe812012-01-30 09:04:36 +00002294 TargetSP target_sp(GetSP());
2295 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002296 {
2297 VariableList variable_list;
2298 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002299 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2300 append,
2301 max_matches,
2302 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002303
2304 if (match_count > 0)
2305 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002306 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002307 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002308 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002309 for (uint32_t i=0; i<match_count; ++i)
2310 {
2311 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2312 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002313 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002314 }
2315 }
2316 }
2317
2318 return sb_value_list;
2319}
2320
Enrico Granatabcd80b42013-01-16 18:53:52 +00002321lldb::SBValue
2322SBTarget::FindFirstGlobalVariable (const char* name)
2323{
2324 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2325 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2326 return sb_value_list.GetValueAtIndex(0);
2327 return SBValue();
2328}
2329
Jim Inghame37d6052011-09-13 00:29:56 +00002330SBSourceManager
2331SBTarget::GetSourceManager()
2332{
2333 SBSourceManager source_manager (*this);
2334 return source_manager;
2335}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002336
Sean Callanan50952e92011-12-14 23:49:37 +00002337lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002338SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2339{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002340 return ReadInstructions (base_addr, count, NULL);
2341}
2342
2343lldb::SBInstructionList
2344SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2345{
Greg Clayton9c766112012-03-06 22:24:44 +00002346 SBInstructionList sb_instructions;
2347
2348 TargetSP target_sp(GetSP());
2349 if (target_sp)
2350 {
2351 Address *addr_ptr = base_addr.get();
2352
2353 if (addr_ptr)
2354 {
2355 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2356 bool prefer_file_cache = false;
2357 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002358 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2359 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2360 prefer_file_cache,
2361 data.GetBytes(),
2362 data.GetByteSize(),
2363 error,
2364 &load_addr);
2365 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002366 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2367 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002368 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002369 *addr_ptr,
2370 data.GetBytes(),
2371 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002372 count,
2373 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002374 }
2375 }
2376
2377 return sb_instructions;
2378
2379}
2380
2381lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002382SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2383{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002384 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2385}
2386
2387lldb::SBInstructionList
2388SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2389{
Sean Callanan50952e92011-12-14 23:49:37 +00002390 SBInstructionList sb_instructions;
2391
Greg Claytonacdbe812012-01-30 09:04:36 +00002392 TargetSP target_sp(GetSP());
2393 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002394 {
2395 Address addr;
2396
2397 if (base_addr.get())
2398 addr = *base_addr.get();
2399
Greg Clayton3faf47c2013-03-28 23:42:53 +00002400 const bool data_from_file = true;
2401
Greg Claytonacdbe812012-01-30 09:04:36 +00002402 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002403 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002404 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002405 addr,
2406 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002407 size,
2408 UINT32_MAX,
2409 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002410 }
2411
2412 return sb_instructions;
2413}
2414
2415lldb::SBInstructionList
2416SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2417{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002418 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2419}
2420
2421lldb::SBInstructionList
2422SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2423{
2424 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002425}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002426
2427SBError
2428SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2429 lldb::addr_t section_base_addr)
2430{
2431 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002432 TargetSP target_sp(GetSP());
2433 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002434 {
2435 if (!section.IsValid())
2436 {
2437 sb_error.SetErrorStringWithFormat ("invalid section");
2438 }
2439 else
2440 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002441 SectionSP section_sp (section.GetSP());
2442 if (section_sp)
2443 {
2444 if (section_sp->IsThreadSpecific())
2445 {
2446 sb_error.SetErrorString ("thread specific sections are not yet supported");
2447 }
2448 else
2449 {
Greg Clayton3c947372013-01-29 01:17:09 +00002450 if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2451 {
2452 // Flush info in the process (stack frames, etc)
2453 ProcessSP process_sp (target_sp->GetProcessSP());
2454 if (process_sp)
2455 process_sp->Flush();
2456 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002457 }
2458 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002459 }
2460 }
2461 else
2462 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002463 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002464 }
2465 return sb_error;
2466}
2467
2468SBError
2469SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2470{
2471 SBError sb_error;
2472
Greg Claytonacdbe812012-01-30 09:04:36 +00002473 TargetSP target_sp(GetSP());
2474 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002475 {
2476 if (!section.IsValid())
2477 {
2478 sb_error.SetErrorStringWithFormat ("invalid section");
2479 }
2480 else
2481 {
Greg Clayton3c947372013-01-29 01:17:09 +00002482 if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
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 Claytoncac9c5f2011-09-24 00:52:29 +00002489 }
2490 }
2491 else
2492 {
2493 sb_error.SetErrorStringWithFormat ("invalid target");
2494 }
2495 return sb_error;
2496}
2497
2498SBError
2499SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2500{
2501 SBError sb_error;
2502
Greg Claytonacdbe812012-01-30 09:04:36 +00002503 TargetSP target_sp(GetSP());
2504 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002505 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002506 ModuleSP module_sp (module.GetSP());
2507 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002508 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002509 bool changed = false;
2510 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002511 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002512 // The load was successful, make sure that at least some sections
2513 // changed before we notify that our module was loaded.
2514 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002515 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002516 ModuleList module_list;
2517 module_list.Append(module_sp);
2518 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002519 // Flush info in the process (stack frames, etc)
2520 ProcessSP process_sp (target_sp->GetProcessSP());
2521 if (process_sp)
2522 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002523 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002524 }
2525 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002526 else
2527 {
2528 sb_error.SetErrorStringWithFormat ("invalid module");
2529 }
2530
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002531 }
2532 else
2533 {
2534 sb_error.SetErrorStringWithFormat ("invalid target");
2535 }
2536 return sb_error;
2537}
2538
2539SBError
2540SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2541{
2542 SBError sb_error;
2543
2544 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002545 TargetSP target_sp(GetSP());
2546 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002547 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002548 ModuleSP module_sp (module.GetSP());
2549 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002550 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002551 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002552 if (objfile)
2553 {
2554 SectionList *section_list = objfile->GetSectionList();
2555 if (section_list)
2556 {
Greg Clayton3c947372013-01-29 01:17:09 +00002557 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002558 const size_t num_sections = section_list->GetSize();
2559 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2560 {
2561 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2562 if (section_sp)
Greg Clayton3c947372013-01-29 01:17:09 +00002563 changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2564 }
2565 if (changed)
2566 {
2567 // Flush info in the process (stack frames, etc)
2568 ProcessSP process_sp (target_sp->GetProcessSP());
2569 if (process_sp)
2570 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002571 }
2572 }
2573 else
2574 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002575 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002576 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2577 }
2578 }
2579 else
2580 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002581 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002582 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2583 }
2584 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002585 else
2586 {
2587 sb_error.SetErrorStringWithFormat ("invalid module");
2588 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002589 }
2590 else
2591 {
2592 sb_error.SetErrorStringWithFormat ("invalid target");
2593 }
2594 return sb_error;
2595}
2596
2597
Greg Claytone14e1922012-12-04 02:22:16 +00002598lldb::SBSymbolContextList
2599SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2600{
2601 SBSymbolContextList sb_sc_list;
2602 if (name && name[0])
2603 {
2604 TargetSP target_sp(GetSP());
2605 if (target_sp)
2606 {
2607 bool append = true;
2608 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2609 symbol_type,
2610 *sb_sc_list,
2611 append);
2612 }
2613 }
2614 return sb_sc_list;
2615
2616}
2617
2618
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002619lldb::SBValue
2620SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2621{
Greg Clayton5160ce52013-03-27 23:08:40 +00002622 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2623 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002624 SBValue expr_result;
2625 ExecutionResults exe_results = eExecutionSetupError;
2626 ValueObjectSP expr_value_sp;
2627 TargetSP target_sp(GetSP());
2628 StackFrame *frame = NULL;
2629 if (target_sp)
2630 {
2631 if (expr == NULL || expr[0] == '\0')
2632 {
2633 if (log)
2634 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2635 return expr_result;
2636 }
2637
2638 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2639 ExecutionContext exe_ctx (m_opaque_sp.get());
2640
2641 if (log)
2642 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2643
2644 frame = exe_ctx.GetFramePtr();
2645 Target *target = exe_ctx.GetTargetPtr();
2646
2647 if (target)
2648 {
2649#ifdef LLDB_CONFIGURATION_DEBUG
2650 StreamString frame_description;
2651 if (frame)
2652 frame->DumpUsingSettingsFormat (&frame_description);
2653 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2654 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2655#endif
2656 exe_results = target->EvaluateExpression (expr,
2657 frame,
2658 expr_value_sp,
2659 options.ref());
2660
2661 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2662#ifdef LLDB_CONFIGURATION_DEBUG
2663 Host::SetCrashDescription (NULL);
2664#endif
2665 }
2666 else
2667 {
2668 if (log)
2669 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2670 }
2671 }
2672#ifndef LLDB_DISABLE_PYTHON
2673 if (expr_log)
2674 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2675 expr_result.GetValue(),
2676 expr_result.GetSummary());
2677
2678 if (log)
2679 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2680 frame,
2681 expr,
2682 expr_value_sp.get(),
2683 exe_results);
2684#endif
2685
2686 return expr_result;
2687}
2688
Greg Clayton13fbb992013-02-01 00:47:49 +00002689
2690lldb::addr_t
2691SBTarget::GetStackRedZoneSize()
2692{
2693 TargetSP target_sp(GetSP());
2694 if (target_sp)
2695 {
2696 ABISP abi_sp;
2697 ProcessSP process_sp (target_sp->GetProcessSP());
2698 if (process_sp)
2699 abi_sp = process_sp->GetABI();
2700 else
2701 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2702 if (abi_sp)
2703 return abi_sp->GetRedZoneSize();
2704 }
2705 return 0;
2706}
2707