blob: cff6e4e2de36cd332a33593238ed5b1192adaf5d [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
Greg Claytoneb023e72013-10-11 19:48:25 +00001264SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
1265 uint32_t resolve_scope)
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001266{
1267 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001268 if (addr.IsValid())
1269 {
1270 TargetSP target_sp(GetSP());
1271 if (target_sp)
1272 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1273 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001274 return sc;
1275}
1276
1277
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001278SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001279SBTarget::BreakpointCreateByLocation (const char *file,
1280 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281{
Greg Clayton7481c202010-11-08 00:28:40 +00001282 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283}
1284
1285SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001286SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
1287 uint32_t line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288{
Greg Clayton5160ce52013-03-27 23:08:40 +00001289 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001290
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001292 TargetSP target_sp(GetSP());
1293 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001294 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001295 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001296
Greg Clayton1f746072012-08-29 21:13:06 +00001297 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001298 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001299 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001300 const bool hardware = false;
1301 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001302 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001303
1304 if (log)
1305 {
1306 SBStream sstr;
1307 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001308 char path[PATH_MAX];
1309 sb_file_spec->GetPath (path, sizeof(path));
1310 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytonacdbe812012-01-30 09:04:36 +00001311 target_sp.get(),
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001312 path,
Greg Clayton48381312010-10-30 04:51:46 +00001313 line,
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001314 sb_bp.get(),
Caroline Ticeceb6b132010-10-26 03:11:13 +00001315 sstr.GetData());
1316 }
1317
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 return sb_bp;
1319}
1320
1321SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001322SBTarget::BreakpointCreateByName (const char *symbol_name,
1323 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324{
Greg Clayton5160ce52013-03-27 23:08:40 +00001325 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001326
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001328 TargetSP target_sp(GetSP());
1329 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001331 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001332
1333 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001334 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001335 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336 if (module_name && module_name[0])
1337 {
Jim Ingham969795f2011-09-21 01:17:13 +00001338 FileSpecList module_spec_list;
1339 module_spec_list.Append (FileSpec (module_name, false));
Greg Claytoneb023e72013-10-11 19:48:25 +00001340 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341 }
1342 else
1343 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001344 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345 }
1346 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001347
1348 if (log)
1349 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001350 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001351 target_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001352 }
1353
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 return sb_bp;
1355}
1356
Jim Ingham87df91b2011-09-23 00:54:11 +00001357lldb::SBBreakpoint
1358SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001359 const SBFileSpecList &module_list,
1360 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001361{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001362 uint32_t name_type_mask = eFunctionNameTypeAuto;
1363 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1364}
1365
1366lldb::SBBreakpoint
1367SBTarget::BreakpointCreateByName (const char *symbol_name,
Greg Claytoneb023e72013-10-11 19:48:25 +00001368 uint32_t name_type_mask,
1369 const SBFileSpecList &module_list,
1370 const SBFileSpecList &comp_unit_list)
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001371{
Greg Clayton5160ce52013-03-27 23:08:40 +00001372 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001373
1374 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001375 TargetSP target_sp(GetSP());
1376 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001377 {
Jim Inghama8558b62012-05-22 00:12:20 +00001378 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001379 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001380 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001381 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1382 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Greg Claytoneb023e72013-10-11 19:48:25 +00001383 comp_unit_list.get(),
1384 symbol_name,
1385 name_type_mask,
1386 skip_prologue,
1387 internal,
1388 hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001389 }
1390
1391 if (log)
1392 {
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001393 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001394 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001395 }
1396
1397 return sb_bp;
1398}
1399
Jim Inghamfab10e82012-03-06 00:37:27 +00001400lldb::SBBreakpoint
1401SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1402 uint32_t num_names,
1403 uint32_t name_type_mask,
1404 const SBFileSpecList &module_list,
1405 const SBFileSpecList &comp_unit_list)
1406{
Greg Clayton5160ce52013-03-27 23:08:40 +00001407 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001408
1409 SBBreakpoint sb_bp;
1410 TargetSP target_sp(GetSP());
1411 if (target_sp && num_names > 0)
1412 {
1413 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001414 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001415 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001416 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001417 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1418 comp_unit_list.get(),
1419 symbol_names,
1420 num_names,
1421 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001422 skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +00001423 internal,
1424 hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001425 }
1426
1427 if (log)
1428 {
1429 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1430 for (uint32_t i = 0 ; i < num_names; i++)
1431 {
1432 char sep;
1433 if (i < num_names - 1)
1434 sep = ',';
1435 else
1436 sep = '}';
1437 if (symbol_names[i] != NULL)
1438 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1439 else
1440 log->Printf ("\"<NULL>\"%c ", sep);
1441
1442 }
1443 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1444 }
1445
1446 return sb_bp;
1447}
Jim Ingham87df91b2011-09-23 00:54:11 +00001448
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001449SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001450SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1451 const char *module_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452{
Greg Clayton5160ce52013-03-27 23:08:40 +00001453 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001454
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001456 TargetSP target_sp(GetSP());
1457 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001459 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001461 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001462 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001463 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464
1465 if (module_name && module_name[0])
1466 {
Jim Ingham969795f2011-09-21 01:17:13 +00001467 FileSpecList module_spec_list;
1468 module_spec_list.Append (FileSpec (module_name, false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001469
Greg Claytoneb023e72013-10-11 19:48:25 +00001470 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 }
1472 else
1473 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001474 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475 }
1476 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001477
1478 if (log)
1479 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001480 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001481 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001482 }
1483
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 return sb_bp;
1485}
1486
Jim Ingham87df91b2011-09-23 00:54:11 +00001487lldb::SBBreakpoint
1488SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001489 const SBFileSpecList &module_list,
1490 const SBFileSpecList &comp_unit_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001491{
Greg Clayton5160ce52013-03-27 23:08:40 +00001492 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001493
Jim Ingham87df91b2011-09-23 00:54:11 +00001494 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001495 TargetSP target_sp(GetSP());
1496 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001497 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001498 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001499 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001500 const bool internal = false;
Greg Claytoneb023e72013-10-11 19:48:25 +00001501 const bool hardware = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001502 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Ingham87df91b2011-09-23 00:54:11 +00001503
Greg Claytoneb023e72013-10-11 19:48:25 +00001504 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001505 }
1506
1507 if (log)
1508 {
1509 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001510 target_sp.get(), symbol_name_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001511 }
1512
1513 return sb_bp;
1514}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515
1516SBBreakpoint
1517SBTarget::BreakpointCreateByAddress (addr_t address)
1518{
Greg Clayton5160ce52013-03-27 23:08:40 +00001519 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001520
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001522 TargetSP target_sp(GetSP());
1523 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001524 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001525 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001526 const bool hardware = false;
1527 *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001528 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001529
1530 if (log)
1531 {
Daniel Malead01b2952012-11-29 21:49:15 +00001532 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 +00001533 }
1534
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535 return sb_bp;
1536}
1537
Jim Ingham969795f2011-09-21 01:17:13 +00001538lldb::SBBreakpoint
Greg Claytoneb023e72013-10-11 19:48:25 +00001539SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1540 const lldb::SBFileSpec &source_file,
1541 const char *module_name)
Jim Ingham969795f2011-09-21 01:17:13 +00001542{
Greg Clayton5160ce52013-03-27 23:08:40 +00001543 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001544
1545 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001546 TargetSP target_sp(GetSP());
1547 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001548 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001549 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001550 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001551 FileSpecList source_file_spec_list;
Greg Claytoneb023e72013-10-11 19:48:25 +00001552 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001553 source_file_spec_list.Append (source_file.ref());
Jim Ingham969795f2011-09-21 01:17:13 +00001554
1555 if (module_name && module_name[0])
1556 {
1557 FileSpecList module_spec_list;
1558 module_spec_list.Append (FileSpec (module_name, false));
1559
Greg Claytoneb023e72013-10-11 19:48:25 +00001560 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001561 }
1562 else
1563 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001564 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
Jim Ingham969795f2011-09-21 01:17:13 +00001565 }
1566 }
1567
1568 if (log)
1569 {
1570 char path[PATH_MAX];
1571 source_file->GetPath (path, sizeof(path));
1572 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001573 target_sp.get(), source_regex, path, module_name, sb_bp.get());
Jim Ingham969795f2011-09-21 01:17:13 +00001574 }
1575
1576 return sb_bp;
1577}
1578
Jim Ingham87df91b2011-09-23 00:54:11 +00001579lldb::SBBreakpoint
1580SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +00001581 const SBFileSpecList &module_list,
1582 const lldb::SBFileSpecList &source_file_list)
Jim Ingham87df91b2011-09-23 00:54:11 +00001583{
Greg Clayton5160ce52013-03-27 23:08:40 +00001584 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001585
1586 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001587 TargetSP target_sp(GetSP());
1588 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001589 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001590 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001591 const bool hardware = false;
Jim Ingham87df91b2011-09-23 00:54:11 +00001592 RegularExpression regexp(source_regex);
Greg Claytoneb023e72013-10-11 19:48:25 +00001593 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
Jim Ingham87df91b2011-09-23 00:54:11 +00001594 }
1595
1596 if (log)
1597 {
1598 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001599 target_sp.get(), source_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001600 }
1601
1602 return sb_bp;
1603}
Jim Ingham969795f2011-09-21 01:17:13 +00001604
Jim Inghamfab10e82012-03-06 00:37:27 +00001605lldb::SBBreakpoint
1606SBTarget::BreakpointCreateForException (lldb::LanguageType language,
Greg Claytoneb023e72013-10-11 19:48:25 +00001607 bool catch_bp,
1608 bool throw_bp)
Jim Inghamfab10e82012-03-06 00:37:27 +00001609{
Greg Clayton5160ce52013-03-27 23:08:40 +00001610 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001611
1612 SBBreakpoint sb_bp;
1613 TargetSP target_sp(GetSP());
1614 if (target_sp)
1615 {
1616 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytoneb023e72013-10-11 19:48:25 +00001617 const bool hardware = false;
1618 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
Jim Inghamfab10e82012-03-06 00:37:27 +00001619 }
1620
1621 if (log)
1622 {
1623 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1624 target_sp.get(),
1625 LanguageRuntime::GetNameForLanguageType(language),
1626 catch_bp ? "on" : "off",
1627 throw_bp ? "on" : "off",
1628 sb_bp.get());
1629 }
1630
1631 return sb_bp;
1632}
1633
Greg Clayton9fed0d82010-07-23 23:33:17 +00001634uint32_t
1635SBTarget::GetNumBreakpoints () const
1636{
Greg Claytonacdbe812012-01-30 09:04:36 +00001637 TargetSP target_sp(GetSP());
1638 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001639 {
1640 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001641 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001642 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001643 return 0;
1644}
1645
1646SBBreakpoint
1647SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1648{
1649 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001650 TargetSP target_sp(GetSP());
1651 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001652 {
1653 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001654 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001655 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001656 return sb_breakpoint;
1657}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658
1659bool
1660SBTarget::BreakpointDelete (break_id_t bp_id)
1661{
Greg Clayton5160ce52013-03-27 23:08:40 +00001662 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001663
Caroline Ticeceb6b132010-10-26 03:11:13 +00001664 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001665 TargetSP target_sp(GetSP());
1666 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001667 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001668 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1669 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001670 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001671
1672 if (log)
1673 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001674 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001675 }
1676
1677 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678}
1679
Johnny Chen5d043462011-09-26 22:40:50 +00001680SBBreakpoint
1681SBTarget::FindBreakpointByID (break_id_t bp_id)
1682{
Greg Clayton5160ce52013-03-27 23:08:40 +00001683 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001684
1685 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001686 TargetSP target_sp(GetSP());
1687 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001688 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001689 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1690 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001691 }
1692
1693 if (log)
1694 {
1695 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001696 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001697 }
1698
1699 return sb_breakpoint;
1700}
1701
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702bool
1703SBTarget::EnableAllBreakpoints ()
1704{
Greg Claytonacdbe812012-01-30 09:04:36 +00001705 TargetSP target_sp(GetSP());
1706 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001708 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1709 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001710 return true;
1711 }
1712 return false;
1713}
1714
1715bool
1716SBTarget::DisableAllBreakpoints ()
1717{
Greg Claytonacdbe812012-01-30 09:04:36 +00001718 TargetSP target_sp(GetSP());
1719 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001721 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1722 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723 return true;
1724 }
1725 return false;
1726}
1727
1728bool
1729SBTarget::DeleteAllBreakpoints ()
1730{
Greg Claytonacdbe812012-01-30 09:04:36 +00001731 TargetSP target_sp(GetSP());
1732 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001734 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1735 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 return true;
1737 }
1738 return false;
1739}
1740
Johnny Chen5d043462011-09-26 22:40:50 +00001741uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001742SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001743{
Greg Claytonacdbe812012-01-30 09:04:36 +00001744 TargetSP target_sp(GetSP());
1745 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001746 {
Johnny Chen01a67862011-10-14 00:42:25 +00001747 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001748 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001749 }
1750 return 0;
1751}
1752
Greg Clayton1b282f92011-10-13 18:08:26 +00001753SBWatchpoint
1754SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001755{
Johnny Chen01a67862011-10-14 00:42:25 +00001756 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001757 TargetSP target_sp(GetSP());
1758 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001759 {
Johnny Chen01a67862011-10-14 00:42:25 +00001760 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001761 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001762 }
Johnny Chen01a67862011-10-14 00:42:25 +00001763 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001764}
1765
1766bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001767SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001768{
Greg Clayton5160ce52013-03-27 23:08:40 +00001769 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001770
1771 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001772 TargetSP target_sp(GetSP());
1773 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001774 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001775 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001776 Mutex::Locker locker;
1777 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001778 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001779 }
1780
1781 if (log)
1782 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001783 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
Johnny Chen5d043462011-09-26 22:40:50 +00001784 }
1785
1786 return result;
1787}
1788
Greg Clayton1b282f92011-10-13 18:08:26 +00001789SBWatchpoint
1790SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001791{
Greg Clayton5160ce52013-03-27 23:08:40 +00001792 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001793
Greg Clayton1b282f92011-10-13 18:08:26 +00001794 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001795 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001796 TargetSP target_sp(GetSP());
1797 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001798 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001799 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001800 Mutex::Locker locker;
1801 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001802 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1803 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001804 }
1805
1806 if (log)
1807 {
Johnny Chen01a67862011-10-14 00:42:25 +00001808 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001809 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001810 }
1811
Greg Clayton1b282f92011-10-13 18:08:26 +00001812 return sb_watchpoint;
1813}
1814
1815lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001816SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001817{
Greg Clayton5160ce52013-03-27 23:08:40 +00001818 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1b282f92011-10-13 18:08:26 +00001819
1820 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001821 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001822 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001823 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001824 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001825 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001826 uint32_t watch_type = 0;
1827 if (read)
1828 watch_type |= LLDB_WATCH_TYPE_READ;
1829 if (write)
1830 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001831 if (watch_type == 0)
1832 {
1833 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1834 return sb_watchpoint;
1835 }
1836
Johnny Chen7385a5a2012-05-31 22:56:36 +00001837 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001838 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001839 // This API doesn't take in a type, so we can't figure out what it is.
1840 ClangASTType *type = NULL;
1841 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001842 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001843 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001844 }
1845
1846 if (log)
1847 {
Daniel Malead01b2952012-11-29 21:49:15 +00001848 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001849 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
Greg Clayton1b282f92011-10-13 18:08:26 +00001850 }
1851
1852 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001853}
1854
1855bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001856SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001857{
Greg Claytonacdbe812012-01-30 09:04:36 +00001858 TargetSP target_sp(GetSP());
1859 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001860 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001861 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001862 Mutex::Locker locker;
1863 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001864 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001865 return true;
1866 }
1867 return false;
1868}
1869
1870bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001871SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001872{
Greg Claytonacdbe812012-01-30 09:04:36 +00001873 TargetSP target_sp(GetSP());
1874 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001875 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001876 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001877 Mutex::Locker locker;
1878 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001879 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001880 return true;
1881 }
1882 return false;
1883}
1884
Enrico Granata347c2aa2013-10-08 21:49:02 +00001885SBValue
1886SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1887{
1888 SBValue sb_value;
1889 lldb::ValueObjectSP new_value_sp;
1890 if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1891 {
1892 lldb::addr_t address(addr.GetLoadAddress(*this));
1893 lldb::TypeImplSP type_impl_sp (type.GetSP());
Enrico Granatadc4db5a2013-10-29 00:28:35 +00001894 ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType(true).GetPointerType ());
Enrico Granata347c2aa2013-10-08 21:49:02 +00001895 if (pointer_ast_type)
1896 {
1897 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
1898
1899 ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1900 ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1901 pointer_ast_type,
1902 ConstString(name),
1903 buffer,
1904 exe_ctx.GetByteOrder(),
1905 exe_ctx.GetAddressByteSize()));
1906
1907 if (ptr_result_valobj_sp)
1908 {
1909 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1910 Error err;
1911 new_value_sp = ptr_result_valobj_sp->Dereference(err);
1912 if (new_value_sp)
1913 new_value_sp->SetName(ConstString(name));
1914 }
1915 }
1916 }
1917 sb_value.SetSP(new_value_sp);
1918 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1919 if (log)
1920 {
1921 if (new_value_sp)
1922 log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"", m_opaque_sp.get(), new_value_sp->GetName().AsCString());
1923 else
1924 log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL", m_opaque_sp.get());
1925 }
1926 return sb_value;
1927}
1928
Johnny Chen5d043462011-09-26 22:40:50 +00001929bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001930SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001931{
Greg Claytonacdbe812012-01-30 09:04:36 +00001932 TargetSP target_sp(GetSP());
1933 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001934 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001935 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001936 Mutex::Locker locker;
1937 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001938 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001939 return true;
1940 }
1941 return false;
1942}
1943
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001944
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001945lldb::SBModule
1946SBTarget::AddModule (const char *path,
1947 const char *triple,
1948 const char *uuid_cstr)
1949{
Greg Claytonb210aec2012-04-23 20:23:39 +00001950 return AddModule (path, triple, uuid_cstr, NULL);
1951}
1952
1953lldb::SBModule
1954SBTarget::AddModule (const char *path,
1955 const char *triple,
1956 const char *uuid_cstr,
1957 const char *symfile)
1958{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001959 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001960 TargetSP target_sp(GetSP());
1961 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001962 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001963 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001964 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001965 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001966
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001967 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001968 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001969
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001970 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001971 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Jason Molendab019cd92013-09-11 21:25:46 +00001972 else
1973 module_spec.GetArchitecture() = target_sp->GetArchitecture();
Greg Claytonb210aec2012-04-23 20:23:39 +00001974
1975 if (symfile)
1976 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001977
Greg Claytonb9a01b32012-02-26 05:51:37 +00001978 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001979 }
1980 return sb_module;
1981}
1982
Greg Clayton226cce22013-07-08 22:22:41 +00001983lldb::SBModule
1984SBTarget::AddModule (const SBModuleSpec &module_spec)
1985{
1986 lldb::SBModule sb_module;
1987 TargetSP target_sp(GetSP());
1988 if (target_sp)
1989 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1990 return sb_module;
1991}
1992
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001993bool
1994SBTarget::AddModule (lldb::SBModule &module)
1995{
Greg Claytonacdbe812012-01-30 09:04:36 +00001996 TargetSP target_sp(GetSP());
1997 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001998 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001999 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002000 return true;
2001 }
2002 return false;
2003}
2004
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002005uint32_t
2006SBTarget::GetNumModules () const
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
Caroline Ticeceb6b132010-10-26 03:11:13 +00002010 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00002011 TargetSP target_sp(GetSP());
2012 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002013 {
2014 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002015 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00002016 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002017
2018 if (log)
Greg Claytonacdbe812012-01-30 09:04:36 +00002019 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002020
2021 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002022}
2023
Greg Clayton48e42542010-07-30 20:12:55 +00002024void
2025SBTarget::Clear ()
2026{
Greg Clayton5160ce52013-03-27 23:08:40 +00002027 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002028
2029 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002030 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002031
Greg Clayton48e42542010-07-30 20:12:55 +00002032 m_opaque_sp.reset();
2033}
2034
2035
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002036SBModule
2037SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2038{
2039 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002040 TargetSP target_sp(GetSP());
2041 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00002042 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002043 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002044 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00002045 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00002046 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047 return sb_module;
2048}
2049
Greg Clayton13d19502012-01-29 06:07:39 +00002050lldb::ByteOrder
2051SBTarget::GetByteOrder ()
2052{
Greg Claytonacdbe812012-01-30 09:04:36 +00002053 TargetSP target_sp(GetSP());
2054 if (target_sp)
2055 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00002056 return eByteOrderInvalid;
2057}
2058
2059const char *
2060SBTarget::GetTriple ()
2061{
Greg Claytonacdbe812012-01-30 09:04:36 +00002062 TargetSP target_sp(GetSP());
2063 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00002064 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002065 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00002066 // Unique the string so we don't run into ownership issues since
2067 // the const strings put the string into the string pool once and
2068 // the strings never comes out
2069 ConstString const_triple (triple.c_str());
2070 return const_triple.GetCString();
2071 }
2072 return NULL;
2073}
2074
2075uint32_t
2076SBTarget::GetAddressByteSize()
2077{
Greg Claytonacdbe812012-01-30 09:04:36 +00002078 TargetSP target_sp(GetSP());
2079 if (target_sp)
2080 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00002081 return sizeof(void*);
2082}
2083
2084
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002085SBModule
2086SBTarget::GetModuleAtIndex (uint32_t idx)
2087{
Greg Clayton5160ce52013-03-27 23:08:40 +00002088 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002089
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002090 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002091 ModuleSP module_sp;
2092 TargetSP target_sp(GetSP());
2093 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002094 {
2095 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002096 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2097 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002098 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002099
2100 if (log)
2101 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00002102 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002103 target_sp.get(), idx, module_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002104 }
2105
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002106 return sb_module;
2107}
2108
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002109bool
2110SBTarget::RemoveModule (lldb::SBModule module)
2111{
Greg Claytonacdbe812012-01-30 09:04:36 +00002112 TargetSP target_sp(GetSP());
2113 if (target_sp)
2114 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002115 return false;
2116}
2117
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118
2119SBBroadcaster
2120SBTarget::GetBroadcaster () const
2121{
Greg Clayton5160ce52013-03-27 23:08:40 +00002122 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002123
Greg Claytonacdbe812012-01-30 09:04:36 +00002124 TargetSP target_sp(GetSP());
2125 SBBroadcaster broadcaster(target_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002126
2127 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002128 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002129 target_sp.get(), broadcaster.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131 return broadcaster;
2132}
2133
Caroline Ticedde9cff2010-09-20 05:20:02 +00002134bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002135SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002136{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002137 Stream &strm = description.ref();
2138
Greg Claytonacdbe812012-01-30 09:04:36 +00002139 TargetSP target_sp(GetSP());
2140 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002141 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002142 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002143 }
2144 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002145 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002146
2147 return true;
2148}
2149
Greg Clayton5569e642012-02-06 01:44:54 +00002150lldb::SBSymbolContextList
2151SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002152{
Greg Clayton5569e642012-02-06 01:44:54 +00002153 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002154 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002155 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002156 TargetSP target_sp(GetSP());
2157 if (target_sp)
2158 {
2159 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002160 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002161 const bool append = true;
2162 target_sp->GetImages().FindFunctions (ConstString(name),
2163 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002164 symbols_ok,
2165 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002166 append,
2167 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002168 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002169 }
Greg Clayton5569e642012-02-06 01:44:54 +00002170 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002171}
2172
Enrico Granata6f3533f2011-07-29 19:53:35 +00002173lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002174SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002175{
Greg Claytonacdbe812012-01-30 09:04:36 +00002176 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002177 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002178 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002179 ConstString const_typename(typename_cstr);
2180 SymbolContext sc;
2181 const bool exact_match = false;
2182
2183 const ModuleList &module_list = target_sp->GetImages();
2184 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002185 for (size_t idx = 0; idx < count; idx++)
2186 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002187 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2188 if (module_sp)
2189 {
2190 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2191 if (type_sp)
2192 return SBType(type_sp);
2193 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002194 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002195
2196 // Didn't find the type in the symbols; try the Objective-C runtime
2197 // if one is installed
2198
2199 ProcessSP process_sp(target_sp->GetProcessSP());
2200
2201 if (process_sp)
2202 {
2203 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2204
2205 if (objc_language_runtime)
2206 {
2207 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2208
2209 if (objc_type_vendor)
2210 {
2211 std::vector <ClangASTType> types;
2212
2213 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2214 return SBType(types[0]);
2215 }
2216 }
2217 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002218
2219 // No matches, search for basic typename matches
2220 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2221 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002222 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002223 }
2224 return SBType();
2225}
2226
Greg Claytonb43165b2012-12-05 21:24:42 +00002227SBType
2228SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002229{
Greg Claytonacdbe812012-01-30 09:04:36 +00002230 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002231 if (target_sp)
2232 {
2233 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2234 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002235 return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
Greg Claytonb43165b2012-12-05 21:24:42 +00002236 }
2237 return SBType();
2238}
2239
2240
2241lldb::SBTypeList
2242SBTarget::FindTypes (const char* typename_cstr)
2243{
2244 SBTypeList sb_type_list;
2245 TargetSP target_sp(GetSP());
2246 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002247 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002248 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002249 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002250 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002251 SymbolContext sc;
2252 TypeList type_list;
2253
Greg Clayton29399a22012-04-06 17:41:13 +00002254 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002255 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002256 exact_match,
2257 UINT32_MAX,
2258 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002259
Greg Claytonb43165b2012-12-05 21:24:42 +00002260 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002261 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002262 for (size_t idx = 0; idx < num_matches; idx++)
2263 {
2264 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2265 if (type_sp)
2266 sb_type_list.Append(SBType(type_sp));
2267 }
2268 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002269
2270 // Try the Objective-C runtime if one is installed
2271
2272 ProcessSP process_sp(target_sp->GetProcessSP());
2273
2274 if (process_sp)
2275 {
2276 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2277
2278 if (objc_language_runtime)
2279 {
2280 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2281
2282 if (objc_type_vendor)
2283 {
2284 std::vector <ClangASTType> types;
2285
2286 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2287 {
2288 for (ClangASTType &type : types)
2289 {
2290 sb_type_list.Append(SBType(type));
2291 }
2292 }
2293 }
2294 }
2295 }
2296
2297 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002298 {
2299 // No matches, search for basic typename matches
2300 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2301 if (clang_ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00002302 sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002303 }
2304 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002305 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002306}
2307
Greg Claytondea8cb42011-06-29 22:09:02 +00002308SBValueList
2309SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2310{
2311 SBValueList sb_value_list;
2312
Greg Claytonacdbe812012-01-30 09:04:36 +00002313 TargetSP target_sp(GetSP());
2314 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002315 {
2316 VariableList variable_list;
2317 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002318 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2319 append,
2320 max_matches,
2321 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002322
2323 if (match_count > 0)
2324 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002325 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002326 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002327 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002328 for (uint32_t i=0; i<match_count; ++i)
2329 {
2330 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2331 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002332 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002333 }
2334 }
2335 }
2336
2337 return sb_value_list;
2338}
2339
Enrico Granatabcd80b42013-01-16 18:53:52 +00002340lldb::SBValue
2341SBTarget::FindFirstGlobalVariable (const char* name)
2342{
2343 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2344 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2345 return sb_value_list.GetValueAtIndex(0);
2346 return SBValue();
2347}
2348
Jim Inghame37d6052011-09-13 00:29:56 +00002349SBSourceManager
2350SBTarget::GetSourceManager()
2351{
2352 SBSourceManager source_manager (*this);
2353 return source_manager;
2354}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002355
Sean Callanan50952e92011-12-14 23:49:37 +00002356lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002357SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2358{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002359 return ReadInstructions (base_addr, count, NULL);
2360}
2361
2362lldb::SBInstructionList
2363SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2364{
Greg Clayton9c766112012-03-06 22:24:44 +00002365 SBInstructionList sb_instructions;
2366
2367 TargetSP target_sp(GetSP());
2368 if (target_sp)
2369 {
2370 Address *addr_ptr = base_addr.get();
2371
2372 if (addr_ptr)
2373 {
2374 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2375 bool prefer_file_cache = false;
2376 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002377 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2378 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2379 prefer_file_cache,
2380 data.GetBytes(),
2381 data.GetByteSize(),
2382 error,
2383 &load_addr);
2384 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002385 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2386 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002387 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002388 *addr_ptr,
2389 data.GetBytes(),
2390 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002391 count,
2392 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002393 }
2394 }
2395
2396 return sb_instructions;
2397
2398}
2399
2400lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002401SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2402{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002403 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2404}
2405
2406lldb::SBInstructionList
2407SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2408{
Sean Callanan50952e92011-12-14 23:49:37 +00002409 SBInstructionList sb_instructions;
2410
Greg Claytonacdbe812012-01-30 09:04:36 +00002411 TargetSP target_sp(GetSP());
2412 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002413 {
2414 Address addr;
2415
2416 if (base_addr.get())
2417 addr = *base_addr.get();
2418
Greg Clayton3faf47c2013-03-28 23:42:53 +00002419 const bool data_from_file = true;
2420
Greg Claytonacdbe812012-01-30 09:04:36 +00002421 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002422 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002423 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002424 addr,
2425 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002426 size,
2427 UINT32_MAX,
2428 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002429 }
2430
2431 return sb_instructions;
2432}
2433
2434lldb::SBInstructionList
2435SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2436{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002437 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2438}
2439
2440lldb::SBInstructionList
2441SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2442{
2443 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002444}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002445
2446SBError
2447SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2448 lldb::addr_t section_base_addr)
2449{
2450 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002451 TargetSP target_sp(GetSP());
2452 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002453 {
2454 if (!section.IsValid())
2455 {
2456 sb_error.SetErrorStringWithFormat ("invalid section");
2457 }
2458 else
2459 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002460 SectionSP section_sp (section.GetSP());
2461 if (section_sp)
2462 {
2463 if (section_sp->IsThreadSpecific())
2464 {
2465 sb_error.SetErrorString ("thread specific sections are not yet supported");
2466 }
2467 else
2468 {
Greg Clayton3c947372013-01-29 01:17:09 +00002469 if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2470 {
2471 // Flush info in the process (stack frames, etc)
2472 ProcessSP process_sp (target_sp->GetProcessSP());
2473 if (process_sp)
2474 process_sp->Flush();
2475 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002476 }
2477 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002478 }
2479 }
2480 else
2481 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002482 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002483 }
2484 return sb_error;
2485}
2486
2487SBError
2488SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2489{
2490 SBError sb_error;
2491
Greg Claytonacdbe812012-01-30 09:04:36 +00002492 TargetSP target_sp(GetSP());
2493 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002494 {
2495 if (!section.IsValid())
2496 {
2497 sb_error.SetErrorStringWithFormat ("invalid section");
2498 }
2499 else
2500 {
Greg Clayton3c947372013-01-29 01:17:09 +00002501 if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2502 {
2503 // Flush info in the process (stack frames, etc)
2504 ProcessSP process_sp (target_sp->GetProcessSP());
2505 if (process_sp)
2506 process_sp->Flush();
2507 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002508 }
2509 }
2510 else
2511 {
2512 sb_error.SetErrorStringWithFormat ("invalid target");
2513 }
2514 return sb_error;
2515}
2516
2517SBError
2518SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2519{
2520 SBError sb_error;
2521
Greg Claytonacdbe812012-01-30 09:04:36 +00002522 TargetSP target_sp(GetSP());
2523 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002524 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002525 ModuleSP module_sp (module.GetSP());
2526 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002527 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002528 bool changed = false;
2529 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002530 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002531 // The load was successful, make sure that at least some sections
2532 // changed before we notify that our module was loaded.
2533 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002534 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002535 ModuleList module_list;
2536 module_list.Append(module_sp);
2537 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002538 // Flush info in the process (stack frames, etc)
2539 ProcessSP process_sp (target_sp->GetProcessSP());
2540 if (process_sp)
2541 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002542 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002543 }
2544 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002545 else
2546 {
2547 sb_error.SetErrorStringWithFormat ("invalid module");
2548 }
2549
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002550 }
2551 else
2552 {
2553 sb_error.SetErrorStringWithFormat ("invalid target");
2554 }
2555 return sb_error;
2556}
2557
2558SBError
2559SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2560{
2561 SBError sb_error;
2562
2563 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002564 TargetSP target_sp(GetSP());
2565 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002566 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002567 ModuleSP module_sp (module.GetSP());
2568 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002569 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002570 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002571 if (objfile)
2572 {
2573 SectionList *section_list = objfile->GetSectionList();
2574 if (section_list)
2575 {
Greg Clayton3c947372013-01-29 01:17:09 +00002576 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002577 const size_t num_sections = section_list->GetSize();
2578 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2579 {
2580 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2581 if (section_sp)
Greg Clayton3c947372013-01-29 01:17:09 +00002582 changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2583 }
2584 if (changed)
2585 {
2586 // Flush info in the process (stack frames, etc)
2587 ProcessSP process_sp (target_sp->GetProcessSP());
2588 if (process_sp)
2589 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002590 }
2591 }
2592 else
2593 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002594 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002595 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2596 }
2597 }
2598 else
2599 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002600 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002601 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2602 }
2603 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002604 else
2605 {
2606 sb_error.SetErrorStringWithFormat ("invalid module");
2607 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002608 }
2609 else
2610 {
2611 sb_error.SetErrorStringWithFormat ("invalid target");
2612 }
2613 return sb_error;
2614}
2615
2616
Greg Claytone14e1922012-12-04 02:22:16 +00002617lldb::SBSymbolContextList
2618SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2619{
2620 SBSymbolContextList sb_sc_list;
2621 if (name && name[0])
2622 {
2623 TargetSP target_sp(GetSP());
2624 if (target_sp)
2625 {
2626 bool append = true;
2627 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2628 symbol_type,
2629 *sb_sc_list,
2630 append);
2631 }
2632 }
2633 return sb_sc_list;
2634
2635}
2636
2637
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002638lldb::SBValue
2639SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2640{
Greg Clayton5160ce52013-03-27 23:08:40 +00002641 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2642 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002643 SBValue expr_result;
2644 ExecutionResults exe_results = eExecutionSetupError;
2645 ValueObjectSP expr_value_sp;
2646 TargetSP target_sp(GetSP());
Jason Molendab57e4a12013-11-04 09:33:30 +00002647 StackFrame *frame = NULL;
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002648 if (target_sp)
2649 {
2650 if (expr == NULL || expr[0] == '\0')
2651 {
2652 if (log)
2653 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2654 return expr_result;
2655 }
2656
2657 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2658 ExecutionContext exe_ctx (m_opaque_sp.get());
2659
2660 if (log)
2661 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2662
2663 frame = exe_ctx.GetFramePtr();
2664 Target *target = exe_ctx.GetTargetPtr();
2665
2666 if (target)
2667 {
2668#ifdef LLDB_CONFIGURATION_DEBUG
2669 StreamString frame_description;
2670 if (frame)
2671 frame->DumpUsingSettingsFormat (&frame_description);
2672 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2673 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2674#endif
2675 exe_results = target->EvaluateExpression (expr,
2676 frame,
2677 expr_value_sp,
2678 options.ref());
2679
2680 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2681#ifdef LLDB_CONFIGURATION_DEBUG
2682 Host::SetCrashDescription (NULL);
2683#endif
2684 }
2685 else
2686 {
2687 if (log)
2688 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2689 }
2690 }
2691#ifndef LLDB_DISABLE_PYTHON
2692 if (expr_log)
2693 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2694 expr_result.GetValue(),
2695 expr_result.GetSummary());
2696
2697 if (log)
2698 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2699 frame,
2700 expr,
2701 expr_value_sp.get(),
2702 exe_results);
2703#endif
2704
2705 return expr_result;
2706}
2707
Greg Clayton13fbb992013-02-01 00:47:49 +00002708
2709lldb::addr_t
2710SBTarget::GetStackRedZoneSize()
2711{
2712 TargetSP target_sp(GetSP());
2713 if (target_sp)
2714 {
2715 ABISP abi_sp;
2716 ProcessSP process_sp (target_sp->GetProcessSP());
2717 if (process_sp)
2718 abi_sp = process_sp->GetABI();
2719 else
2720 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2721 if (abi_sp)
2722 return abi_sp->GetRedZoneSize();
2723 }
2724 return 0;
2725}
2726