blob: 7e47fbf01877fa9622b7ac1a7dd94896cfc43eaa [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"
Greg Claytondea8cb42011-06-29 22:09:02 +000044#include "lldb/Core/ValueObjectList.h"
45#include "lldb/Core/ValueObjectVariable.h"
46#include "lldb/Host/FileSpec.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000047#include "lldb/Host/Host.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000048#include "lldb/Interpreter/Args.h"
Greg Clayton1f746072012-08-29 21:13:06 +000049#include "lldb/Symbol/ObjectFile.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000050#include "lldb/Symbol/SymbolVendor.h"
Greg Claytondea8cb42011-06-29 22:09:02 +000051#include "lldb/Symbol/VariableList.h"
Jim Inghamfab10e82012-03-06 00:37:27 +000052#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Process.h"
54#include "lldb/Target/Target.h"
55#include "lldb/Target/TargetList.h"
56
57#include "lldb/Interpreter/CommandReturnObject.h"
58#include "../source/Commands/CommandObjectBreakpoint.h"
59
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
61using namespace lldb;
62using namespace lldb_private;
63
64#define DEFAULT_DISASM_BYTE_SIZE 32
65
Greg Clayton38d1f052012-02-24 20:59:25 +000066SBLaunchInfo::SBLaunchInfo (const char **argv) :
67 m_opaque_sp(new ProcessLaunchInfo())
Greg Clayton0e615682012-02-24 05:03:03 +000068{
Greg Clayton38d1f052012-02-24 20:59:25 +000069 m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
70 if (argv && argv[0])
71 m_opaque_sp->GetArguments().SetArguments(argv);
Greg Clayton0e615682012-02-24 05:03:03 +000072}
73
Greg Claytonecc7c0d2012-03-07 23:52:51 +000074SBLaunchInfo::~SBLaunchInfo()
75{
76}
77
78lldb_private::ProcessLaunchInfo &
79SBLaunchInfo::ref ()
80{
81 return *m_opaque_sp;
82}
83
84
Greg Clayton0e615682012-02-24 05:03:03 +000085uint32_t
86SBLaunchInfo::GetUserID()
87{
88 return m_opaque_sp->GetUserID();
89}
90
91uint32_t
92SBLaunchInfo::GetGroupID()
93{
94 return m_opaque_sp->GetGroupID();
95}
96
97bool
98SBLaunchInfo::UserIDIsValid ()
99{
100 return m_opaque_sp->UserIDIsValid();
101}
102
103bool
104SBLaunchInfo::GroupIDIsValid ()
105{
106 return m_opaque_sp->GroupIDIsValid();
107}
108
109void
110SBLaunchInfo::SetUserID (uint32_t uid)
111{
112 m_opaque_sp->SetUserID (uid);
113}
114
115void
116SBLaunchInfo::SetGroupID (uint32_t gid)
117{
118 m_opaque_sp->SetGroupID (gid);
119}
120
Greg Clayton0e615682012-02-24 05:03:03 +0000121uint32_t
122SBLaunchInfo::GetNumArguments ()
123{
124 return m_opaque_sp->GetArguments().GetArgumentCount();
125}
126
127const char *
128SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
129{
130 return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
131}
132
133void
134SBLaunchInfo::SetArguments (const char **argv, bool append)
135{
136 if (append)
137 {
138 if (argv)
139 m_opaque_sp->GetArguments().AppendArguments(argv);
140 }
141 else
142 {
143 if (argv)
144 m_opaque_sp->GetArguments().SetArguments(argv);
145 else
146 m_opaque_sp->GetArguments().Clear();
147 }
148}
149
150uint32_t
151SBLaunchInfo::GetNumEnvironmentEntries ()
152{
153 return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
154}
155
156const char *
157SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
158{
159 return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
160}
161
162void
163SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
164{
165 if (append)
166 {
167 if (envp)
168 m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
169 }
170 else
171 {
172 if (envp)
173 m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
174 else
175 m_opaque_sp->GetEnvironmentEntries().Clear();
176 }
177}
178
179void
180SBLaunchInfo::Clear ()
181{
182 m_opaque_sp->Clear();
183}
184
185const char *
186SBLaunchInfo::GetWorkingDirectory () const
187{
188 return m_opaque_sp->GetWorkingDirectory();
189}
190
191void
192SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
193{
194 m_opaque_sp->SetWorkingDirectory(working_dir);
195}
196
197uint32_t
198SBLaunchInfo::GetLaunchFlags ()
199{
200 return m_opaque_sp->GetFlags().Get();
201}
202
203void
204SBLaunchInfo::SetLaunchFlags (uint32_t flags)
205{
206 m_opaque_sp->GetFlags().Reset(flags);
207}
208
209const char *
210SBLaunchInfo::GetProcessPluginName ()
211{
212 return m_opaque_sp->GetProcessPluginName();
213}
214
215void
216SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
217{
218 return m_opaque_sp->SetProcessPluginName (plugin_name);
219}
220
221const char *
222SBLaunchInfo::GetShell ()
223{
224 return m_opaque_sp->GetShell();
225}
226
227void
228SBLaunchInfo::SetShell (const char * path)
229{
230 m_opaque_sp->SetShell (path);
231}
232
233uint32_t
234SBLaunchInfo::GetResumeCount ()
235{
236 return m_opaque_sp->GetResumeCount();
237}
238
239void
240SBLaunchInfo::SetResumeCount (uint32_t c)
241{
242 m_opaque_sp->SetResumeCount (c);
243}
244
245bool
246SBLaunchInfo::AddCloseFileAction (int fd)
247{
248 return m_opaque_sp->AppendCloseFileAction(fd);
249}
250
251bool
252SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
253{
254 return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
255}
256
257bool
258SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
259{
260 return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
261}
262
263bool
264SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
265{
266 return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
267}
268
269
270SBAttachInfo::SBAttachInfo () :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000271 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000272{
273}
274
275SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000276 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000277{
278 m_opaque_sp->SetProcessID (pid);
279}
280
281SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000282 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000283{
284 if (path && path[0])
285 m_opaque_sp->GetExecutableFile().SetFile(path, false);
286 m_opaque_sp->SetWaitForLaunch (wait_for);
287}
288
289SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000290 m_opaque_sp (new ProcessAttachInfo())
Greg Clayton0e615682012-02-24 05:03:03 +0000291{
292 *m_opaque_sp = *rhs.m_opaque_sp;
293}
294
Greg Claytonecc7c0d2012-03-07 23:52:51 +0000295SBAttachInfo::~SBAttachInfo()
296{
297}
298
299lldb_private::ProcessAttachInfo &
300SBAttachInfo::ref ()
301{
302 return *m_opaque_sp;
303}
304
Greg Clayton0e615682012-02-24 05:03:03 +0000305SBAttachInfo &
306SBAttachInfo::operator = (const SBAttachInfo &rhs)
307{
308 if (this != &rhs)
309 *m_opaque_sp = *rhs.m_opaque_sp;
310 return *this;
311}
312
313lldb::pid_t
314SBAttachInfo::GetProcessID ()
315{
316 return m_opaque_sp->GetProcessID();
317}
318
319void
320SBAttachInfo::SetProcessID (lldb::pid_t pid)
321{
322 m_opaque_sp->SetProcessID (pid);
323}
324
325
326uint32_t
327SBAttachInfo::GetResumeCount ()
328{
329 return m_opaque_sp->GetResumeCount();
330}
331
332void
333SBAttachInfo::SetResumeCount (uint32_t c)
334{
335 m_opaque_sp->SetResumeCount (c);
336}
337
338const char *
339SBAttachInfo::GetProcessPluginName ()
340{
341 return m_opaque_sp->GetProcessPluginName();
342}
343
344void
345SBAttachInfo::SetProcessPluginName (const char *plugin_name)
346{
347 return m_opaque_sp->SetProcessPluginName (plugin_name);
348}
349
350void
351SBAttachInfo::SetExecutable (const char *path)
352{
353 if (path && path[0])
354 m_opaque_sp->GetExecutableFile().SetFile(path, false);
355 else
356 m_opaque_sp->GetExecutableFile().Clear();
357}
358
359void
360SBAttachInfo::SetExecutable (SBFileSpec exe_file)
361{
362 if (exe_file.IsValid())
363 m_opaque_sp->GetExecutableFile() = exe_file.ref();
364 else
365 m_opaque_sp->GetExecutableFile().Clear();
366}
367
368bool
369SBAttachInfo::GetWaitForLaunch ()
370{
371 return m_opaque_sp->GetWaitForLaunch();
372}
373
374void
375SBAttachInfo::SetWaitForLaunch (bool b)
376{
377 m_opaque_sp->SetWaitForLaunch (b);
378}
379
Jim Inghamcd16df92012-07-20 21:37:13 +0000380bool
381SBAttachInfo::GetIgnoreExisting ()
382{
383 return m_opaque_sp->GetIgnoreExisting();
384}
385
386void
387SBAttachInfo::SetIgnoreExisting (bool b)
388{
389 m_opaque_sp->SetIgnoreExisting (b);
390}
391
Greg Clayton0e615682012-02-24 05:03:03 +0000392uint32_t
Greg Clayton41bd8ac2012-02-24 23:56:06 +0000393SBAttachInfo::GetUserID()
394{
395 return m_opaque_sp->GetUserID();
396}
397
398uint32_t
399SBAttachInfo::GetGroupID()
400{
401 return m_opaque_sp->GetGroupID();
402}
403
404bool
405SBAttachInfo::UserIDIsValid ()
406{
407 return m_opaque_sp->UserIDIsValid();
408}
409
410bool
411SBAttachInfo::GroupIDIsValid ()
412{
413 return m_opaque_sp->GroupIDIsValid();
414}
415
416void
417SBAttachInfo::SetUserID (uint32_t uid)
418{
419 m_opaque_sp->SetUserID (uid);
420}
421
422void
423SBAttachInfo::SetGroupID (uint32_t gid)
424{
425 m_opaque_sp->SetGroupID (gid);
426}
427
428uint32_t
Greg Clayton0e615682012-02-24 05:03:03 +0000429SBAttachInfo::GetEffectiveUserID()
430{
431 return m_opaque_sp->GetEffectiveUserID();
432}
433
434uint32_t
435SBAttachInfo::GetEffectiveGroupID()
436{
437 return m_opaque_sp->GetEffectiveGroupID();
438}
439
440bool
441SBAttachInfo::EffectiveUserIDIsValid ()
442{
443 return m_opaque_sp->EffectiveUserIDIsValid();
444}
445
446bool
447SBAttachInfo::EffectiveGroupIDIsValid ()
448{
449 return m_opaque_sp->EffectiveGroupIDIsValid ();
450}
451
452void
453SBAttachInfo::SetEffectiveUserID (uint32_t uid)
454{
455 m_opaque_sp->SetEffectiveUserID(uid);
456}
457
458void
459SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
460{
461 m_opaque_sp->SetEffectiveGroupID(gid);
462}
463
464lldb::pid_t
465SBAttachInfo::GetParentProcessID ()
466{
467 return m_opaque_sp->GetParentProcessID();
468}
469
470void
471SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
472{
473 m_opaque_sp->SetParentProcessID (pid);
474}
475
476bool
477SBAttachInfo::ParentProcessIDIsValid()
478{
479 return m_opaque_sp->ParentProcessIDIsValid();
480}
481
482
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483//----------------------------------------------------------------------
484// SBTarget constructor
485//----------------------------------------------------------------------
Greg Clayton54979cd2010-12-15 05:08:08 +0000486SBTarget::SBTarget () :
487 m_opaque_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488{
489}
490
491SBTarget::SBTarget (const SBTarget& rhs) :
Greg Clayton66111032010-06-23 01:19:29 +0000492 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493{
494}
495
496SBTarget::SBTarget(const TargetSP& target_sp) :
Greg Clayton66111032010-06-23 01:19:29 +0000497 m_opaque_sp (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498{
499}
500
Greg Claytonefabb122010-11-05 23:17:00 +0000501const SBTarget&
502SBTarget::operator = (const SBTarget& rhs)
503{
504 if (this != &rhs)
505 m_opaque_sp = rhs.m_opaque_sp;
506 return *this;
507}
508
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509//----------------------------------------------------------------------
510// Destructor
511//----------------------------------------------------------------------
512SBTarget::~SBTarget()
513{
514}
515
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000516const char *
517SBTarget::GetBroadcasterClassName ()
518{
519 return Target::GetStaticBroadcasterClass().AsCString();
520}
521
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522bool
523SBTarget::IsValid () const
524{
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000525 return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526}
527
528SBProcess
529SBTarget::GetProcess ()
530{
531 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000532 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000533 TargetSP target_sp(GetSP());
534 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +0000535 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000536 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000537 sb_process.SetSP (process_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +0000538 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000539
Greg Clayton5160ce52013-03-27 23:08:40 +0000540 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000541 if (log)
542 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000543 log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +0000544 target_sp.get(), process_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000545 }
546
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547 return sb_process;
548}
549
Greg Clayton66111032010-06-23 01:19:29 +0000550SBDebugger
551SBTarget::GetDebugger () const
552{
553 SBDebugger debugger;
Greg Claytonacdbe812012-01-30 09:04:36 +0000554 TargetSP target_sp(GetSP());
555 if (target_sp)
556 debugger.reset (target_sp->GetDebugger().shared_from_this());
Greg Clayton66111032010-06-23 01:19:29 +0000557 return debugger;
558}
559
Jim Ingham270684d2011-03-31 00:01:24 +0000560SBProcess
Greg Clayton4d8ad552013-03-25 22:40:51 +0000561SBTarget::LoadCore (const char *core_file)
562{
563 SBProcess sb_process;
564 TargetSP target_sp(GetSP());
565 if (target_sp)
566 {
567 FileSpec filespec(core_file, true);
568 ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
569 NULL,
570 &filespec));
571 if (process_sp)
572 {
573 process_sp->LoadCore();
574 sb_process.SetSP (process_sp);
575 }
576 }
577 return sb_process;
578}
579
580SBProcess
Jim Ingham270684d2011-03-31 00:01:24 +0000581SBTarget::LaunchSimple
582(
583 char const **argv,
584 char const **envp,
585 const char *working_directory
586)
587{
588 char *stdin_path = NULL;
589 char *stdout_path = NULL;
590 char *stderr_path = NULL;
591 uint32_t launch_flags = 0;
592 bool stop_at_entry = false;
593 SBError error;
594 SBListener listener = GetDebugger().GetListener();
595 return Launch (listener,
596 argv,
597 envp,
598 stdin_path,
599 stdout_path,
600 stderr_path,
601 working_directory,
602 launch_flags,
603 stop_at_entry,
604 error);
605}
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000606
607SBProcess
608SBTarget::Launch
609(
Greg Clayton4b045622011-02-03 21:28:34 +0000610 SBListener &listener,
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000611 char const **argv,
612 char const **envp,
613 const char *stdin_path,
614 const char *stdout_path,
615 const char *stderr_path,
616 const char *working_directory,
617 uint32_t launch_flags, // See LaunchFlags
618 bool stop_at_entry,
619 lldb::SBError& error
620)
621{
Greg Clayton5160ce52013-03-27 23:08:40 +0000622 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000623
Greg Claytonacdbe812012-01-30 09:04:36 +0000624 SBProcess sb_process;
625 ProcessSP process_sp;
626 TargetSP target_sp(GetSP());
627
Caroline Ticeceb6b132010-10-26 03:11:13 +0000628 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +0000629 {
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000630 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 +0000631 target_sp.get(),
Greg Claytonbd82a5d2011-01-23 05:56:20 +0000632 argv,
633 envp,
634 stdin_path ? stdin_path : "NULL",
635 stdout_path ? stdout_path : "NULL",
636 stderr_path ? stderr_path : "NULL",
637 working_directory ? working_directory : "NULL",
638 launch_flags,
639 stop_at_entry,
640 error.get());
Greg Clayton93aa84e2010-10-29 04:59:35 +0000641 }
Greg Claytonacdbe812012-01-30 09:04:36 +0000642
643 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000645 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton5d5028b2010-10-06 03:53:16 +0000646
Greg Clayton645bf542011-01-27 01:01:10 +0000647 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
648 launch_flags |= eLaunchFlagDisableASLR;
649
Greg Clayton2289fa42011-04-30 01:09:13 +0000650 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000651 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000652 if (process_sp)
Greg Clayton931180e2011-01-27 06:44:37 +0000653 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000654 state = process_sp->GetState();
Greg Clayton2289fa42011-04-30 01:09:13 +0000655
Greg Claytonb9556ac2012-01-30 07:41:31 +0000656 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton2289fa42011-04-30 01:09:13 +0000657 {
658 if (state == eStateAttaching)
659 error.SetErrorString ("process attach is in progress");
660 else
661 error.SetErrorString ("a process is already being debugged");
Greg Clayton2289fa42011-04-30 01:09:13 +0000662 return sb_process;
663 }
Greg Clayton931180e2011-01-27 06:44:37 +0000664 }
665
Greg Clayton2289fa42011-04-30 01:09:13 +0000666 if (state == eStateConnected)
667 {
668 // If we are already connected, then we have already specified the
669 // listener, so if a valid listener is supplied, we need to error out
670 // to let the client know.
671 if (listener.IsValid())
672 {
673 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton2289fa42011-04-30 01:09:13 +0000674 return sb_process;
675 }
676 }
677 else
Greg Clayton524e60b2010-10-06 22:10:17 +0000678 {
Greg Clayton4b045622011-02-03 21:28:34 +0000679 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000680 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton4b045622011-02-03 21:28:34 +0000681 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000682 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton2289fa42011-04-30 01:09:13 +0000683 }
Greg Clayton645bf542011-01-27 01:01:10 +0000684
Greg Claytonb9556ac2012-01-30 07:41:31 +0000685 if (process_sp)
Greg Clayton2289fa42011-04-30 01:09:13 +0000686 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000687 sb_process.SetSP (process_sp);
Greg Clayton2289fa42011-04-30 01:09:13 +0000688 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
689 launch_flags |= eLaunchFlagDisableSTDIO;
690
Greg Clayton982c9762011-11-03 21:22:33 +0000691 ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
692
Greg Claytonacdbe812012-01-30 09:04:36 +0000693 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Clayton982c9762011-11-03 21:22:33 +0000694 if (exe_module)
Greg Claytonad9e8282011-11-29 04:03:30 +0000695 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton982c9762011-11-03 21:22:33 +0000696 if (argv)
697 launch_info.GetArguments().AppendArguments (argv);
698 if (envp)
699 launch_info.GetEnvironmentEntries ().SetArguments (envp);
700
Greg Claytonb9556ac2012-01-30 07:41:31 +0000701 error.SetError (process_sp->Launch (launch_info));
Greg Clayton2289fa42011-04-30 01:09:13 +0000702 if (error.Success())
Greg Clayton645bf542011-01-27 01:01:10 +0000703 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000704 // We we are stopping at the entry point, we can return now!
705 if (stop_at_entry)
706 return sb_process;
Greg Clayton645bf542011-01-27 01:01:10 +0000707
Greg Clayton2289fa42011-04-30 01:09:13 +0000708 // Make sure we are stopped at the entry
Greg Claytonb9556ac2012-01-30 07:41:31 +0000709 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton2289fa42011-04-30 01:09:13 +0000710 if (state == eStateStopped)
Greg Clayton645bf542011-01-27 01:01:10 +0000711 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000712 // resume the process to skip the entry point
Greg Claytonb9556ac2012-01-30 07:41:31 +0000713 error.SetError (process_sp->Resume());
Greg Clayton2289fa42011-04-30 01:09:13 +0000714 if (error.Success())
Greg Clayton645bf542011-01-27 01:01:10 +0000715 {
Greg Clayton2289fa42011-04-30 01:09:13 +0000716 // If we are doing synchronous mode, then wait for the
717 // process to stop yet again!
Greg Claytonacdbe812012-01-30 09:04:36 +0000718 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +0000719 process_sp->WaitForProcessToStop (NULL);
Greg Clayton645bf542011-01-27 01:01:10 +0000720 }
721 }
722 }
Greg Clayton2289fa42011-04-30 01:09:13 +0000723 }
724 else
725 {
726 error.SetErrorString ("unable to create lldb_private::Process");
Greg Clayton524e60b2010-10-06 22:10:17 +0000727 }
728 }
729 else
730 {
731 error.SetErrorString ("SBTarget is invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000733
Caroline Tice20ad3c42010-10-29 21:48:37 +0000734 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000735 if (log)
736 {
Sean Callanan575a4542012-10-20 00:21:31 +0000737 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +0000738 target_sp.get(), process_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000739 }
740
Greg Clayton5d5028b2010-10-06 03:53:16 +0000741 return sb_process;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742}
743
Greg Clayton0e615682012-02-24 05:03:03 +0000744SBProcess
745SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
746{
Greg Clayton5160ce52013-03-27 23:08:40 +0000747 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton0e615682012-02-24 05:03:03 +0000748
749 SBProcess sb_process;
750 ProcessSP process_sp;
751 TargetSP target_sp(GetSP());
752
753 if (log)
754 {
755 log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
756 }
757
758 if (target_sp)
759 {
760 Mutex::Locker api_locker (target_sp->GetAPIMutex());
761 StateType state = eStateInvalid;
762 process_sp = target_sp->GetProcessSP();
763 if (process_sp)
764 {
765 state = process_sp->GetState();
766
767 if (process_sp->IsAlive() && state != eStateConnected)
768 {
769 if (state == eStateAttaching)
770 error.SetErrorString ("process attach is in progress");
771 else
772 error.SetErrorString ("a process is already being debugged");
773 return sb_process;
774 }
775 }
776
777 if (state != eStateConnected)
778 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
779
780 if (process_sp)
781 {
782 sb_process.SetSP (process_sp);
783 lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
Greg Clayton38d1f052012-02-24 20:59:25 +0000784
Greg Clayton38d1f052012-02-24 20:59:25 +0000785 Module *exe_module = target_sp->GetExecutableModulePointer();
786 if (exe_module)
Han Ming Ong04cf1ba2012-03-02 01:02:04 +0000787 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
Greg Clayton38d1f052012-02-24 20:59:25 +0000788
789 const ArchSpec &arch_spec = target_sp->GetArchitecture();
790 if (arch_spec.IsValid())
791 launch_info.GetArchitecture () = arch_spec;
792
Greg Clayton0e615682012-02-24 05:03:03 +0000793 error.SetError (process_sp->Launch (launch_info));
Greg Clayton38d1f052012-02-24 20:59:25 +0000794 const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
Greg Clayton0e615682012-02-24 05:03:03 +0000795 if (error.Success())
796 {
Greg Clayton0e615682012-02-24 05:03:03 +0000797 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Clayton38d1f052012-02-24 20:59:25 +0000798 {
799 // If we are doing synchronous mode, then wait for the initial
800 // stop to happen, else, return and let the caller watch for
801 // the stop
802 if (synchronous_execution)
Greg Clayton23f59502012-07-17 03:23:13 +0000803 process_sp->WaitForProcessToStop (NULL);
Greg Clayton38d1f052012-02-24 20:59:25 +0000804 // We we are stopping at the entry point, we can return now!
Greg Clayton0e615682012-02-24 05:03:03 +0000805 return sb_process;
Greg Clayton38d1f052012-02-24 20:59:25 +0000806 }
Greg Clayton0e615682012-02-24 05:03:03 +0000807
808 // Make sure we are stopped at the entry
Greg Clayton23f59502012-07-17 03:23:13 +0000809 StateType state = process_sp->WaitForProcessToStop (NULL);
Greg Clayton0e615682012-02-24 05:03:03 +0000810 if (state == eStateStopped)
811 {
812 // resume the process to skip the entry point
813 error.SetError (process_sp->Resume());
814 if (error.Success())
815 {
816 // If we are doing synchronous mode, then wait for the
817 // process to stop yet again!
Greg Clayton38d1f052012-02-24 20:59:25 +0000818 if (synchronous_execution)
Greg Clayton0e615682012-02-24 05:03:03 +0000819 process_sp->WaitForProcessToStop (NULL);
820 }
821 }
822 }
823 }
824 else
825 {
826 error.SetErrorString ("unable to create lldb_private::Process");
827 }
828 }
829 else
830 {
831 error.SetErrorString ("SBTarget is invalid");
832 }
833
834 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
835 if (log)
836 {
Sean Callanan575a4542012-10-20 00:21:31 +0000837 log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
Greg Clayton0e615682012-02-24 05:03:03 +0000838 target_sp.get(), process_sp.get());
839 }
840
841 return sb_process;
842}
843
844lldb::SBProcess
845SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
846{
Greg Clayton5160ce52013-03-27 23:08:40 +0000847 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000848
Greg Clayton0e615682012-02-24 05:03:03 +0000849 SBProcess sb_process;
850 ProcessSP process_sp;
851 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000852
853 if (log)
854 {
855 log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...", target_sp.get());
856 }
857
Greg Clayton0e615682012-02-24 05:03:03 +0000858 if (target_sp)
859 {
860 Mutex::Locker api_locker (target_sp->GetAPIMutex());
861
862 StateType state = eStateInvalid;
863 process_sp = target_sp->GetProcessSP();
864 if (process_sp)
865 {
866 state = process_sp->GetState();
867
868 if (process_sp->IsAlive() && state != eStateConnected)
869 {
870 if (state == eStateAttaching)
871 error.SetErrorString ("process attach is in progress");
872 else
873 error.SetErrorString ("a process is already being debugged");
Sean Callanan575a4542012-10-20 00:21:31 +0000874 if (log)
875 {
876 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
877 target_sp.get(), error.GetCString());
878 }
Greg Clayton0e615682012-02-24 05:03:03 +0000879 return sb_process;
880 }
881 }
882
883 if (state != eStateConnected)
884 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
885
886 if (process_sp)
887 {
Greg Clayton0e615682012-02-24 05:03:03 +0000888 ProcessAttachInfo &attach_info = sb_attach_info.ref();
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000889 if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000890 {
891 PlatformSP platform_sp = target_sp->GetPlatform();
Greg Clayton91e407e2012-09-27 00:03:39 +0000892 // See if we can pre-verify if a process exists or not
893 if (platform_sp && platform_sp->IsConnected())
Han Ming Ongcec8c902012-02-29 00:12:56 +0000894 {
Han Ming Ongfbd8de82013-03-25 20:11:18 +0000895 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Clayton91e407e2012-09-27 00:03:39 +0000896 ProcessInstanceInfo instance_info;
897 if (platform_sp->GetProcessInfo(attach_pid, instance_info))
898 {
899 attach_info.SetUserID(instance_info.GetEffectiveUserID());
900 }
901 else
902 {
Daniel Malead01b2952012-11-29 21:49:15 +0000903 error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000904 if (log)
905 {
906 log->Printf ("SBTarget(%p)::Attach (...) => error %s",
907 target_sp.get(), error.GetCString());
908 }
Greg Clayton91e407e2012-09-27 00:03:39 +0000909 return sb_process;
910 }
Han Ming Ongcec8c902012-02-29 00:12:56 +0000911 }
912 }
Han Ming Ongbee98392012-02-29 19:16:40 +0000913 error.SetError (process_sp->Attach (attach_info));
914 if (error.Success())
915 {
916 sb_process.SetSP (process_sp);
917 // If we are doing synchronous mode, then wait for the
918 // process to stop!
919 if (target_sp->GetDebugger().GetAsyncExecution () == false)
920 process_sp->WaitForProcessToStop (NULL);
921 }
Greg Clayton0e615682012-02-24 05:03:03 +0000922 }
923 else
924 {
925 error.SetErrorString ("unable to create lldb_private::Process");
926 }
927 }
928 else
929 {
930 error.SetErrorString ("SBTarget is invalid");
931 }
Sean Callanan575a4542012-10-20 00:21:31 +0000932
933 if (log)
934 {
935 log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
936 target_sp.get(), process_sp.get());
937 }
938
Greg Clayton0e615682012-02-24 05:03:03 +0000939 return sb_process;
940}
941
942
Greg Clayton1ba6cfd2011-12-02 02:10:57 +0000943#if defined(__APPLE__)
944
945lldb::SBProcess
946SBTarget::AttachToProcessWithID (SBListener &listener,
947 ::pid_t pid,
948 lldb::SBError& error)
949{
950 return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
951}
952
953#endif // #if defined(__APPLE__)
Greg Clayton524e60b2010-10-06 22:10:17 +0000954
955lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +0000956SBTarget::AttachToProcessWithID
Greg Clayton524e60b2010-10-06 22:10:17 +0000957(
Greg Clayton4b045622011-02-03 21:28:34 +0000958 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +0000959 lldb::pid_t pid,// The process ID to attach to
960 SBError& error // An error explaining what went wrong if attach fails
961)
962{
Greg Clayton5160ce52013-03-27 23:08:40 +0000963 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +0000964
Greg Clayton524e60b2010-10-06 22:10:17 +0000965 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000966 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +0000967 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +0000968
969 if (log)
970 {
Daniel Malead01b2952012-11-29 21:49:15 +0000971 log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...", target_sp.get(), pid);
Sean Callanan575a4542012-10-20 00:21:31 +0000972 }
973
Greg Claytonacdbe812012-01-30 09:04:36 +0000974 if (target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +0000975 {
Greg Claytonacdbe812012-01-30 09:04:36 +0000976 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton0c74e782011-06-24 03:21:43 +0000977
978 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +0000979 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +0000980 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +0000981 {
Greg Claytonb9556ac2012-01-30 07:41:31 +0000982 state = process_sp->GetState();
Greg Clayton0c74e782011-06-24 03:21:43 +0000983
Greg Claytonb9556ac2012-01-30 07:41:31 +0000984 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton0c74e782011-06-24 03:21:43 +0000985 {
986 if (state == eStateAttaching)
987 error.SetErrorString ("process attach is in progress");
988 else
989 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +0000990 return sb_process;
991 }
992 }
993
994 if (state == eStateConnected)
995 {
996 // If we are already connected, then we have already specified the
997 // listener, so if a valid listener is supplied, we need to error out
998 // to let the client know.
999 if (listener.IsValid())
1000 {
1001 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001002 return sb_process;
1003 }
1004 }
Greg Clayton4b045622011-02-03 21:28:34 +00001005 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001006 {
1007 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001008 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001009 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001010 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001011 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001012 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001013 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001014 sb_process.SetSP (process_sp);
1015
Greg Clayton144f3a92011-11-15 03:53:30 +00001016 ProcessAttachInfo attach_info;
1017 attach_info.SetProcessID (pid);
Han Ming Ong84647042012-02-25 01:07:38 +00001018
1019 PlatformSP platform_sp = target_sp->GetPlatform();
1020 ProcessInstanceInfo instance_info;
1021 if (platform_sp->GetProcessInfo(pid, instance_info))
1022 {
1023 attach_info.SetUserID(instance_info.GetEffectiveUserID());
Han Ming Ong84647042012-02-25 01:07:38 +00001024 }
Greg Claytonb9556ac2012-01-30 07:41:31 +00001025 error.SetError (process_sp->Attach (attach_info));
Greg Claytone2186ed2012-09-07 17:51:47 +00001026 if (error.Success())
1027 {
1028 // If we are doing synchronous mode, then wait for the
1029 // process to stop!
1030 if (target_sp->GetDebugger().GetAsyncExecution () == false)
Greg Claytonb9556ac2012-01-30 07:41:31 +00001031 process_sp->WaitForProcessToStop (NULL);
Greg Claytone2186ed2012-09-07 17:51:47 +00001032 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001033 }
1034 else
1035 {
1036 error.SetErrorString ("unable to create lldb_private::Process");
1037 }
1038 }
1039 else
1040 {
1041 error.SetErrorString ("SBTarget is invalid");
1042 }
Sean Callanan575a4542012-10-20 00:21:31 +00001043
1044 if (log)
1045 {
1046 log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
1047 target_sp.get(), process_sp.get());
1048 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001049 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001050}
1051
1052lldb::SBProcess
Greg Clayton05faeb72010-10-07 04:19:01 +00001053SBTarget::AttachToProcessWithName
Greg Clayton524e60b2010-10-06 22:10:17 +00001054(
Greg Clayton4b045622011-02-03 21:28:34 +00001055 SBListener &listener,
Greg Clayton524e60b2010-10-06 22:10:17 +00001056 const char *name, // basename of process to attach to
1057 bool wait_for, // if true wait for a new instance of "name" to be launched
1058 SBError& error // An error explaining what went wrong if attach fails
1059)
1060{
Greg Clayton5160ce52013-03-27 23:08:40 +00001061 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001062
Greg Clayton524e60b2010-10-06 22:10:17 +00001063 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001064 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001065 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001066
1067 if (log)
1068 {
1069 log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...", target_sp.get(), name, wait_for ? "true" : "false");
1070 }
1071
Greg Claytonacdbe812012-01-30 09:04:36 +00001072 if (name && target_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001073 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001074 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Claytonaf67cec2010-12-20 20:49:23 +00001075
Greg Clayton0c74e782011-06-24 03:21:43 +00001076 StateType state = eStateInvalid;
Greg Claytonacdbe812012-01-30 09:04:36 +00001077 process_sp = target_sp->GetProcessSP();
Greg Claytonb9556ac2012-01-30 07:41:31 +00001078 if (process_sp)
Greg Clayton0c74e782011-06-24 03:21:43 +00001079 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001080 state = process_sp->GetState();
Greg Clayton0c74e782011-06-24 03:21:43 +00001081
Greg Claytonb9556ac2012-01-30 07:41:31 +00001082 if (process_sp->IsAlive() && state != eStateConnected)
Greg Clayton0c74e782011-06-24 03:21:43 +00001083 {
1084 if (state == eStateAttaching)
1085 error.SetErrorString ("process attach is in progress");
1086 else
1087 error.SetErrorString ("a process is already being debugged");
Greg Clayton0c74e782011-06-24 03:21:43 +00001088 return sb_process;
1089 }
1090 }
1091
1092 if (state == eStateConnected)
1093 {
1094 // If we are already connected, then we have already specified the
1095 // listener, so if a valid listener is supplied, we need to error out
1096 // to let the client know.
1097 if (listener.IsValid())
1098 {
1099 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
Greg Clayton0c74e782011-06-24 03:21:43 +00001100 return sb_process;
1101 }
1102 }
Greg Clayton4b045622011-02-03 21:28:34 +00001103 else
Greg Clayton0c74e782011-06-24 03:21:43 +00001104 {
1105 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001106 process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001107 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001108 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
Greg Clayton0c74e782011-06-24 03:21:43 +00001109 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001110
Greg Claytonb9556ac2012-01-30 07:41:31 +00001111 if (process_sp)
Greg Clayton524e60b2010-10-06 22:10:17 +00001112 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001113 sb_process.SetSP (process_sp);
Greg Clayton144f3a92011-11-15 03:53:30 +00001114 ProcessAttachInfo attach_info;
1115 attach_info.GetExecutableFile().SetFile(name, false);
1116 attach_info.SetWaitForLaunch(wait_for);
Greg Claytonb9556ac2012-01-30 07:41:31 +00001117 error.SetError (process_sp->Attach (attach_info));
Daniel Malead659dc12013-04-01 19:47:00 +00001118 if (error.Success())
1119 {
1120 // If we are doing synchronous mode, then wait for the
1121 // process to stop!
1122 if (target_sp->GetDebugger().GetAsyncExecution () == false)
1123 process_sp->WaitForProcessToStop (NULL);
1124 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001125 }
1126 else
1127 {
1128 error.SetErrorString ("unable to create lldb_private::Process");
1129 }
1130 }
1131 else
1132 {
1133 error.SetErrorString ("SBTarget is invalid");
1134 }
Sean Callanan575a4542012-10-20 00:21:31 +00001135
1136 if (log)
1137 {
1138 log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1139 target_sp.get(), process_sp.get());
1140 }
Greg Clayton524e60b2010-10-06 22:10:17 +00001141 return sb_process;
Greg Clayton524e60b2010-10-06 22:10:17 +00001142}
1143
James McIlree9631aae2011-03-04 00:31:13 +00001144lldb::SBProcess
1145SBTarget::ConnectRemote
1146(
1147 SBListener &listener,
1148 const char *url,
1149 const char *plugin_name,
1150 SBError& error
1151)
1152{
Greg Clayton5160ce52013-03-27 23:08:40 +00001153 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Sean Callanan575a4542012-10-20 00:21:31 +00001154
James McIlree9631aae2011-03-04 00:31:13 +00001155 SBProcess sb_process;
Greg Claytonb9556ac2012-01-30 07:41:31 +00001156 ProcessSP process_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001157 TargetSP target_sp(GetSP());
Sean Callanan575a4542012-10-20 00:21:31 +00001158
1159 if (log)
1160 {
1161 log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1162 }
1163
Greg Claytonacdbe812012-01-30 09:04:36 +00001164 if (target_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001165 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001166 Mutex::Locker api_locker (target_sp->GetAPIMutex());
James McIlree9631aae2011-03-04 00:31:13 +00001167 if (listener.IsValid())
Greg Claytonc3776bf2012-02-09 06:16:32 +00001168 process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001169 else
Greg Claytonc3776bf2012-02-09 06:16:32 +00001170 process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
James McIlree9631aae2011-03-04 00:31:13 +00001171
1172
Greg Claytonb9556ac2012-01-30 07:41:31 +00001173 if (process_sp)
James McIlree9631aae2011-03-04 00:31:13 +00001174 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00001175 sb_process.SetSP (process_sp);
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00001176 error.SetError (process_sp->ConnectRemote (NULL, url));
James McIlree9631aae2011-03-04 00:31:13 +00001177 }
1178 else
1179 {
1180 error.SetErrorString ("unable to create lldb_private::Process");
1181 }
1182 }
1183 else
1184 {
1185 error.SetErrorString ("SBTarget is invalid");
1186 }
Sean Callanan575a4542012-10-20 00:21:31 +00001187
1188 if (log)
1189 {
1190 log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1191 target_sp.get(), process_sp.get());
1192 }
James McIlree9631aae2011-03-04 00:31:13 +00001193 return sb_process;
1194}
1195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196SBFileSpec
1197SBTarget::GetExecutable ()
1198{
Caroline Ticeceb6b132010-10-26 03:11:13 +00001199
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 SBFileSpec exe_file_spec;
Greg Claytonacdbe812012-01-30 09:04:36 +00001201 TargetSP target_sp(GetSP());
1202 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001204 Module *exe_module = target_sp->GetExecutableModulePointer();
Greg Claytonaa149cb2011-08-11 02:48:45 +00001205 if (exe_module)
1206 exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001208
Greg Clayton5160ce52013-03-27 23:08:40 +00001209 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001210 if (log)
1211 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001212 log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001213 target_sp.get(), exe_file_spec.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001214 }
1215
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216 return exe_file_spec;
1217}
1218
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220SBTarget::operator == (const SBTarget &rhs) const
1221{
Greg Clayton66111032010-06-23 01:19:29 +00001222 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223}
1224
1225bool
1226SBTarget::operator != (const SBTarget &rhs) const
1227{
Greg Clayton66111032010-06-23 01:19:29 +00001228 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229}
1230
Greg Claytonb9556ac2012-01-30 07:41:31 +00001231lldb::TargetSP
1232SBTarget::GetSP () const
Greg Clayton9a377662011-10-01 02:59:24 +00001233{
1234 return m_opaque_sp;
1235}
1236
Greg Clayton66111032010-06-23 01:19:29 +00001237void
Greg Claytonb9556ac2012-01-30 07:41:31 +00001238SBTarget::SetSP (const lldb::TargetSP& target_sp)
Greg Clayton66111032010-06-23 01:19:29 +00001239{
1240 m_opaque_sp = target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241}
1242
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001243lldb::SBAddress
1244SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001245{
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001246 lldb::SBAddress sb_addr;
1247 Address &addr = sb_addr.ref();
Greg Claytonacdbe812012-01-30 09:04:36 +00001248 TargetSP target_sp(GetSP());
1249 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001250 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001251 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1252 if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001253 return sb_addr;
Greg Claytonaf67cec2010-12-20 20:49:23 +00001254 }
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001255
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001256 // We have a load address that isn't in a section, just return an address
1257 // with the offset filled in (the address) and the section set to NULL
Greg Claytone72dfb32012-02-24 01:59:29 +00001258 addr.SetRawAddress(vm_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +00001259 return sb_addr;
Greg Claytonac2eb9b2010-12-12 19:25:26 +00001260}
1261
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001262SBSymbolContext
1263SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1264{
1265 SBSymbolContext sc;
Greg Claytonacdbe812012-01-30 09:04:36 +00001266 if (addr.IsValid())
1267 {
1268 TargetSP target_sp(GetSP());
1269 if (target_sp)
1270 target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1271 }
Greg Clayton5f2a4f92011-03-02 21:34:46 +00001272 return sc;
1273}
1274
1275
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001276SBBreakpoint
1277SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1278{
Greg Clayton7481c202010-11-08 00:28:40 +00001279 return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280}
1281
1282SBBreakpoint
1283SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1284{
Greg Clayton5160ce52013-03-27 23:08:40 +00001285 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001286
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001288 TargetSP target_sp(GetSP());
1289 if (target_sp && line != 0)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001290 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001291 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001292
Greg Clayton1f746072012-08-29 21:13:06 +00001293 const LazyBool check_inlines = eLazyBoolCalculate;
Jim Inghama8558b62012-05-22 00:12:20 +00001294 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Clayton1f746072012-08-29 21:13:06 +00001295 const bool internal = false;
Jim Inghama8558b62012-05-22 00:12:20 +00001296 *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001297 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001298
1299 if (log)
1300 {
1301 SBStream sstr;
1302 sb_bp.GetDescription (sstr);
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001303 char path[PATH_MAX];
1304 sb_file_spec->GetPath (path, sizeof(path));
1305 log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
Greg Claytonacdbe812012-01-30 09:04:36 +00001306 target_sp.get(),
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001307 path,
Greg Clayton48381312010-10-30 04:51:46 +00001308 line,
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001309 sb_bp.get(),
Caroline Ticeceb6b132010-10-26 03:11:13 +00001310 sstr.GetData());
1311 }
1312
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313 return sb_bp;
1314}
1315
1316SBBreakpoint
1317SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1318{
Greg Clayton5160ce52013-03-27 23:08:40 +00001319 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001320
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001322 TargetSP target_sp(GetSP());
1323 if (target_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001325 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001326
1327 const bool internal = false;
1328 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329 if (module_name && module_name[0])
1330 {
Jim Ingham969795f2011-09-21 01:17:13 +00001331 FileSpecList module_spec_list;
1332 module_spec_list.Append (FileSpec (module_name, false));
Jim Inghama8558b62012-05-22 00:12:20 +00001333 *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 }
1335 else
1336 {
Jim Inghama8558b62012-05-22 00:12:20 +00001337 *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338 }
1339 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001340
1341 if (log)
1342 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001343 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001344 target_sp.get(), symbol_name, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001345 }
1346
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347 return sb_bp;
1348}
1349
Jim Ingham87df91b2011-09-23 00:54:11 +00001350lldb::SBBreakpoint
1351SBTarget::BreakpointCreateByName (const char *symbol_name,
1352 const SBFileSpecList &module_list,
1353 const SBFileSpecList &comp_unit_list)
1354{
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001355 uint32_t name_type_mask = eFunctionNameTypeAuto;
1356 return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1357}
1358
1359lldb::SBBreakpoint
1360SBTarget::BreakpointCreateByName (const char *symbol_name,
1361 uint32_t name_type_mask,
1362 const SBFileSpecList &module_list,
1363 const SBFileSpecList &comp_unit_list)
1364{
Greg Clayton5160ce52013-03-27 23:08:40 +00001365 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001366
1367 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001368 TargetSP target_sp(GetSP());
1369 if (target_sp && symbol_name && symbol_name[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001370 {
Jim Inghama8558b62012-05-22 00:12:20 +00001371 const bool internal = false;
1372 const LazyBool skip_prologue = eLazyBoolCalculate;
Greg Claytonacdbe812012-01-30 09:04:36 +00001373 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1374 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
Jim Ingham87df91b2011-09-23 00:54:11 +00001375 comp_unit_list.get(),
1376 symbol_name,
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001377 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001378 skip_prologue,
1379 internal);
Jim Ingham87df91b2011-09-23 00:54:11 +00001380 }
1381
1382 if (log)
1383 {
Jim Ingham2dd7f7f2011-10-11 01:18:55 +00001384 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001385 target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001386 }
1387
1388 return sb_bp;
1389}
1390
Jim Inghamfab10e82012-03-06 00:37:27 +00001391lldb::SBBreakpoint
1392SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1393 uint32_t num_names,
1394 uint32_t name_type_mask,
1395 const SBFileSpecList &module_list,
1396 const SBFileSpecList &comp_unit_list)
1397{
Greg Clayton5160ce52013-03-27 23:08:40 +00001398 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001399
1400 SBBreakpoint sb_bp;
1401 TargetSP target_sp(GetSP());
1402 if (target_sp && num_names > 0)
1403 {
1404 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Inghama8558b62012-05-22 00:12:20 +00001405 const bool internal = false;
1406 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Inghamfab10e82012-03-06 00:37:27 +00001407 *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1408 comp_unit_list.get(),
1409 symbol_names,
1410 num_names,
1411 name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +00001412 skip_prologue,
1413 internal);
Jim Inghamfab10e82012-03-06 00:37:27 +00001414 }
1415
1416 if (log)
1417 {
1418 log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1419 for (uint32_t i = 0 ; i < num_names; i++)
1420 {
1421 char sep;
1422 if (i < num_names - 1)
1423 sep = ',';
1424 else
1425 sep = '}';
1426 if (symbol_names[i] != NULL)
1427 log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1428 else
1429 log->Printf ("\"<NULL>\"%c ", sep);
1430
1431 }
1432 log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1433 }
1434
1435 return sb_bp;
1436}
Jim Ingham87df91b2011-09-23 00:54:11 +00001437
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438SBBreakpoint
1439SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1440{
Greg Clayton5160ce52013-03-27 23:08:40 +00001441 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001442
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001443 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001444 TargetSP target_sp(GetSP());
1445 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001447 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001448 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001449 const bool internal = false;
1450 const LazyBool skip_prologue = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451
1452 if (module_name && module_name[0])
1453 {
Jim Ingham969795f2011-09-21 01:17:13 +00001454 FileSpecList module_spec_list;
1455 module_spec_list.Append (FileSpec (module_name, false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456
Jim Inghama8558b62012-05-22 00:12:20 +00001457 *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 }
1459 else
1460 {
Jim Inghama8558b62012-05-22 00:12:20 +00001461 *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001462 }
1463 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001464
1465 if (log)
1466 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001467 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001468 target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001469 }
1470
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 return sb_bp;
1472}
1473
Jim Ingham87df91b2011-09-23 00:54:11 +00001474lldb::SBBreakpoint
1475SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1476 const SBFileSpecList &module_list,
1477 const SBFileSpecList &comp_unit_list)
1478{
Greg Clayton5160ce52013-03-27 23:08:40 +00001479 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480
Jim Ingham87df91b2011-09-23 00:54:11 +00001481 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001482 TargetSP target_sp(GetSP());
1483 if (target_sp && symbol_name_regex && symbol_name_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001484 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001485 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001486 RegularExpression regexp(symbol_name_regex);
Jim Inghama8558b62012-05-22 00:12:20 +00001487 const bool internal = false;
1488 const LazyBool skip_prologue = eLazyBoolCalculate;
Jim Ingham87df91b2011-09-23 00:54:11 +00001489
Jim Inghama8558b62012-05-22 00:12:20 +00001490 *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal);
Jim Ingham87df91b2011-09-23 00:54:11 +00001491 }
1492
1493 if (log)
1494 {
1495 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001496 target_sp.get(), symbol_name_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001497 }
1498
1499 return sb_bp;
1500}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501
1502SBBreakpoint
1503SBTarget::BreakpointCreateByAddress (addr_t address)
1504{
Greg Clayton5160ce52013-03-27 23:08:40 +00001505 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001506
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001507 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001508 TargetSP target_sp(GetSP());
1509 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001510 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001511 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1512 *sb_bp = target_sp->CreateBreakpoint (address, false);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001513 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001514
1515 if (log)
1516 {
Daniel Malead01b2952012-11-29 21:49:15 +00001517 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 +00001518 }
1519
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 return sb_bp;
1521}
1522
Jim Ingham969795f2011-09-21 01:17:13 +00001523lldb::SBBreakpoint
1524SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1525{
Greg Clayton5160ce52013-03-27 23:08:40 +00001526 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham969795f2011-09-21 01:17:13 +00001527
1528 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001529 TargetSP target_sp(GetSP());
1530 if (target_sp && source_regex && source_regex[0])
Jim Ingham969795f2011-09-21 01:17:13 +00001531 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001532 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham969795f2011-09-21 01:17:13 +00001533 RegularExpression regexp(source_regex);
Jim Ingham87df91b2011-09-23 00:54:11 +00001534 FileSpecList source_file_spec_list;
1535 source_file_spec_list.Append (source_file.ref());
Jim Ingham969795f2011-09-21 01:17:13 +00001536
1537 if (module_name && module_name[0])
1538 {
1539 FileSpecList module_spec_list;
1540 module_spec_list.Append (FileSpec (module_name, false));
1541
Greg Claytonacdbe812012-01-30 09:04:36 +00001542 *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
Jim Ingham969795f2011-09-21 01:17:13 +00001543 }
1544 else
1545 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001546 *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
Jim Ingham969795f2011-09-21 01:17:13 +00001547 }
1548 }
1549
1550 if (log)
1551 {
1552 char path[PATH_MAX];
1553 source_file->GetPath (path, sizeof(path));
1554 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001555 target_sp.get(), source_regex, path, module_name, sb_bp.get());
Jim Ingham969795f2011-09-21 01:17:13 +00001556 }
1557
1558 return sb_bp;
1559}
1560
Jim Ingham87df91b2011-09-23 00:54:11 +00001561lldb::SBBreakpoint
1562SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1563 const SBFileSpecList &module_list,
1564 const lldb::SBFileSpecList &source_file_list)
1565{
Greg Clayton5160ce52013-03-27 23:08:40 +00001566 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham87df91b2011-09-23 00:54:11 +00001567
1568 SBBreakpoint sb_bp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001569 TargetSP target_sp(GetSP());
1570 if (target_sp && source_regex && source_regex[0])
Jim Ingham87df91b2011-09-23 00:54:11 +00001571 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001572 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Jim Ingham87df91b2011-09-23 00:54:11 +00001573 RegularExpression regexp(source_regex);
Greg Claytonacdbe812012-01-30 09:04:36 +00001574 *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
Jim Ingham87df91b2011-09-23 00:54:11 +00001575 }
1576
1577 if (log)
1578 {
1579 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001580 target_sp.get(), source_regex, sb_bp.get());
Jim Ingham87df91b2011-09-23 00:54:11 +00001581 }
1582
1583 return sb_bp;
1584}
Jim Ingham969795f2011-09-21 01:17:13 +00001585
Jim Inghamfab10e82012-03-06 00:37:27 +00001586lldb::SBBreakpoint
1587SBTarget::BreakpointCreateForException (lldb::LanguageType language,
1588 bool catch_bp,
1589 bool throw_bp)
1590{
Greg Clayton5160ce52013-03-27 23:08:40 +00001591 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Inghamfab10e82012-03-06 00:37:27 +00001592
1593 SBBreakpoint sb_bp;
1594 TargetSP target_sp(GetSP());
1595 if (target_sp)
1596 {
1597 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1598 *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp);
1599 }
1600
1601 if (log)
1602 {
1603 log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1604 target_sp.get(),
1605 LanguageRuntime::GetNameForLanguageType(language),
1606 catch_bp ? "on" : "off",
1607 throw_bp ? "on" : "off",
1608 sb_bp.get());
1609 }
1610
1611 return sb_bp;
1612}
1613
Greg Clayton9fed0d82010-07-23 23:33:17 +00001614uint32_t
1615SBTarget::GetNumBreakpoints () const
1616{
Greg Claytonacdbe812012-01-30 09:04:36 +00001617 TargetSP target_sp(GetSP());
1618 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001619 {
1620 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001621 return target_sp->GetBreakpointList().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001622 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001623 return 0;
1624}
1625
1626SBBreakpoint
1627SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1628{
1629 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001630 TargetSP target_sp(GetSP());
1631 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001632 {
1633 // The breakpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001634 *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001635 }
Greg Clayton9fed0d82010-07-23 23:33:17 +00001636 return sb_breakpoint;
1637}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638
1639bool
1640SBTarget::BreakpointDelete (break_id_t bp_id)
1641{
Greg Clayton5160ce52013-03-27 23:08:40 +00001642 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001643
Caroline Ticeceb6b132010-10-26 03:11:13 +00001644 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001645 TargetSP target_sp(GetSP());
1646 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001647 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001648 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1649 result = target_sp->RemoveBreakpointByID (bp_id);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001650 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001651
1652 if (log)
1653 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001654 log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001655 }
1656
1657 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658}
1659
Johnny Chen5d043462011-09-26 22:40:50 +00001660SBBreakpoint
1661SBTarget::FindBreakpointByID (break_id_t bp_id)
1662{
Greg Clayton5160ce52013-03-27 23:08:40 +00001663 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001664
1665 SBBreakpoint sb_breakpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001666 TargetSP target_sp(GetSP());
1667 if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001668 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001669 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1670 *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001671 }
1672
1673 if (log)
1674 {
1675 log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00001676 target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001677 }
1678
1679 return sb_breakpoint;
1680}
1681
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682bool
1683SBTarget::EnableAllBreakpoints ()
1684{
Greg Claytonacdbe812012-01-30 09:04:36 +00001685 TargetSP target_sp(GetSP());
1686 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001688 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1689 target_sp->EnableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690 return true;
1691 }
1692 return false;
1693}
1694
1695bool
1696SBTarget::DisableAllBreakpoints ()
1697{
Greg Claytonacdbe812012-01-30 09:04:36 +00001698 TargetSP target_sp(GetSP());
1699 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001701 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1702 target_sp->DisableAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703 return true;
1704 }
1705 return false;
1706}
1707
1708bool
1709SBTarget::DeleteAllBreakpoints ()
1710{
Greg Claytonacdbe812012-01-30 09:04:36 +00001711 TargetSP target_sp(GetSP());
1712 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001714 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1715 target_sp->RemoveAllBreakpoints ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001716 return true;
1717 }
1718 return false;
1719}
1720
Johnny Chen5d043462011-09-26 22:40:50 +00001721uint32_t
Greg Clayton1b282f92011-10-13 18:08:26 +00001722SBTarget::GetNumWatchpoints () const
Johnny Chen5d043462011-09-26 22:40:50 +00001723{
Greg Claytonacdbe812012-01-30 09:04:36 +00001724 TargetSP target_sp(GetSP());
1725 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001726 {
Johnny Chen01a67862011-10-14 00:42:25 +00001727 // The watchpoint list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001728 return target_sp->GetWatchpointList().GetSize();
Johnny Chen5d043462011-09-26 22:40:50 +00001729 }
1730 return 0;
1731}
1732
Greg Clayton1b282f92011-10-13 18:08:26 +00001733SBWatchpoint
1734SBTarget::GetWatchpointAtIndex (uint32_t idx) const
Johnny Chen9d954d82011-09-27 20:29:45 +00001735{
Johnny Chen01a67862011-10-14 00:42:25 +00001736 SBWatchpoint sb_watchpoint;
Greg Claytonacdbe812012-01-30 09:04:36 +00001737 TargetSP target_sp(GetSP());
1738 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001739 {
Johnny Chen01a67862011-10-14 00:42:25 +00001740 // The watchpoint list is thread safe, no need to lock
Greg Clayton81e871e2012-02-04 02:27:34 +00001741 sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
Johnny Chen5d043462011-09-26 22:40:50 +00001742 }
Johnny Chen01a67862011-10-14 00:42:25 +00001743 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001744}
1745
1746bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001747SBTarget::DeleteWatchpoint (watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001748{
Greg Clayton5160ce52013-03-27 23:08:40 +00001749 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001750
1751 bool result = false;
Greg Claytonacdbe812012-01-30 09:04:36 +00001752 TargetSP target_sp(GetSP());
1753 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001754 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001755 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001756 Mutex::Locker locker;
1757 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001758 result = target_sp->RemoveWatchpointByID (wp_id);
Johnny Chen5d043462011-09-26 22:40:50 +00001759 }
1760
1761 if (log)
1762 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001763 log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
Johnny Chen5d043462011-09-26 22:40:50 +00001764 }
1765
1766 return result;
1767}
1768
Greg Clayton1b282f92011-10-13 18:08:26 +00001769SBWatchpoint
1770SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
Johnny Chen5d043462011-09-26 22:40:50 +00001771{
Greg Clayton5160ce52013-03-27 23:08:40 +00001772 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +00001773
Greg Clayton1b282f92011-10-13 18:08:26 +00001774 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001775 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001776 TargetSP target_sp(GetSP());
1777 if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
Johnny Chen5d043462011-09-26 22:40:50 +00001778 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001779 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001780 Mutex::Locker locker;
1781 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Clayton81e871e2012-02-04 02:27:34 +00001782 watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1783 sb_watchpoint.SetSP (watchpoint_sp);
Johnny Chen5d043462011-09-26 22:40:50 +00001784 }
1785
1786 if (log)
1787 {
Johnny Chen01a67862011-10-14 00:42:25 +00001788 log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001789 target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
Johnny Chen5d043462011-09-26 22:40:50 +00001790 }
1791
Greg Clayton1b282f92011-10-13 18:08:26 +00001792 return sb_watchpoint;
1793}
1794
1795lldb::SBWatchpoint
Johnny Chenb90827e2012-06-04 23:19:54 +00001796SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
Greg Clayton1b282f92011-10-13 18:08:26 +00001797{
Greg Clayton5160ce52013-03-27 23:08:40 +00001798 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton1b282f92011-10-13 18:08:26 +00001799
1800 SBWatchpoint sb_watchpoint;
Greg Clayton81e871e2012-02-04 02:27:34 +00001801 lldb::WatchpointSP watchpoint_sp;
Greg Claytonacdbe812012-01-30 09:04:36 +00001802 TargetSP target_sp(GetSP());
Greg Clayton81e871e2012-02-04 02:27:34 +00001803 if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
Greg Clayton1b282f92011-10-13 18:08:26 +00001804 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001805 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton81e871e2012-02-04 02:27:34 +00001806 uint32_t watch_type = 0;
1807 if (read)
1808 watch_type |= LLDB_WATCH_TYPE_READ;
1809 if (write)
1810 watch_type |= LLDB_WATCH_TYPE_WRITE;
Jim Inghamc6462312013-06-18 21:52:48 +00001811 if (watch_type == 0)
1812 {
1813 error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1814 return sb_watchpoint;
1815 }
1816
Johnny Chen7385a5a2012-05-31 22:56:36 +00001817 // Target::CreateWatchpoint() is thread safe.
Johnny Chenb90827e2012-06-04 23:19:54 +00001818 Error cw_error;
Jim Inghama7dfb662012-10-23 07:20:06 +00001819 // This API doesn't take in a type, so we can't figure out what it is.
1820 ClangASTType *type = NULL;
1821 watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
Johnny Chenb90827e2012-06-04 23:19:54 +00001822 error.SetError(cw_error);
Greg Clayton81e871e2012-02-04 02:27:34 +00001823 sb_watchpoint.SetSP (watchpoint_sp);
Greg Clayton1b282f92011-10-13 18:08:26 +00001824 }
1825
1826 if (log)
1827 {
Daniel Malead01b2952012-11-29 21:49:15 +00001828 log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
Greg Clayton81e871e2012-02-04 02:27:34 +00001829 target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
Greg Clayton1b282f92011-10-13 18:08:26 +00001830 }
1831
1832 return sb_watchpoint;
Johnny Chen5d043462011-09-26 22:40:50 +00001833}
1834
1835bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001836SBTarget::EnableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001837{
Greg Claytonacdbe812012-01-30 09:04:36 +00001838 TargetSP target_sp(GetSP());
1839 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001840 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001841 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001842 Mutex::Locker locker;
1843 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001844 target_sp->EnableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001845 return true;
1846 }
1847 return false;
1848}
1849
1850bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001851SBTarget::DisableAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001852{
Greg Claytonacdbe812012-01-30 09:04:36 +00001853 TargetSP target_sp(GetSP());
1854 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001855 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001856 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001857 Mutex::Locker locker;
1858 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001859 target_sp->DisableAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001860 return true;
1861 }
1862 return false;
1863}
1864
1865bool
Greg Clayton1b282f92011-10-13 18:08:26 +00001866SBTarget::DeleteAllWatchpoints ()
Johnny Chen5d043462011-09-26 22:40:50 +00001867{
Greg Claytonacdbe812012-01-30 09:04:36 +00001868 TargetSP target_sp(GetSP());
1869 if (target_sp)
Johnny Chen5d043462011-09-26 22:40:50 +00001870 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001871 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Johnny Chen7385a5a2012-05-31 22:56:36 +00001872 Mutex::Locker locker;
1873 target_sp->GetWatchpointList().GetListMutex(locker);
Greg Claytonacdbe812012-01-30 09:04:36 +00001874 target_sp->RemoveAllWatchpoints ();
Johnny Chen5d043462011-09-26 22:40:50 +00001875 return true;
1876 }
1877 return false;
1878}
1879
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001880
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001881lldb::SBModule
1882SBTarget::AddModule (const char *path,
1883 const char *triple,
1884 const char *uuid_cstr)
1885{
Greg Claytonb210aec2012-04-23 20:23:39 +00001886 return AddModule (path, triple, uuid_cstr, NULL);
1887}
1888
1889lldb::SBModule
1890SBTarget::AddModule (const char *path,
1891 const char *triple,
1892 const char *uuid_cstr,
1893 const char *symfile)
1894{
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001895 lldb::SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001896 TargetSP target_sp(GetSP());
1897 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001898 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001899 ModuleSpec module_spec;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001900 if (path)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001901 module_spec.GetFileSpec().SetFile(path, false);
Greg Claytonb210aec2012-04-23 20:23:39 +00001902
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001903 if (uuid_cstr)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00001904 module_spec.GetUUID().SetFromCString(uuid_cstr);
Greg Claytonb210aec2012-04-23 20:23:39 +00001905
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001906 if (triple)
Greg Claytonb9a01b32012-02-26 05:51:37 +00001907 module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
Greg Claytonb210aec2012-04-23 20:23:39 +00001908
1909 if (symfile)
1910 module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001911
Greg Claytonb9a01b32012-02-26 05:51:37 +00001912 sb_module.SetSP(target_sp->GetSharedModule (module_spec));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001913 }
1914 return sb_module;
1915}
1916
Greg Clayton226cce22013-07-08 22:22:41 +00001917lldb::SBModule
1918SBTarget::AddModule (const SBModuleSpec &module_spec)
1919{
1920 lldb::SBModule sb_module;
1921 TargetSP target_sp(GetSP());
1922 if (target_sp)
1923 sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1924 return sb_module;
1925}
1926
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001927bool
1928SBTarget::AddModule (lldb::SBModule &module)
1929{
Greg Claytonacdbe812012-01-30 09:04:36 +00001930 TargetSP target_sp(GetSP());
1931 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001932 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001933 target_sp->GetImages().AppendIfNeeded (module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001934 return true;
1935 }
1936 return false;
1937}
1938
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001939uint32_t
1940SBTarget::GetNumModules () const
1941{
Greg Clayton5160ce52013-03-27 23:08:40 +00001942 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001943
Caroline Ticeceb6b132010-10-26 03:11:13 +00001944 uint32_t num = 0;
Greg Claytonacdbe812012-01-30 09:04:36 +00001945 TargetSP target_sp(GetSP());
1946 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00001947 {
1948 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00001949 num = target_sp->GetImages().GetSize();
Greg Claytonaf67cec2010-12-20 20:49:23 +00001950 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00001951
1952 if (log)
Greg Claytonacdbe812012-01-30 09:04:36 +00001953 log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
Caroline Ticeceb6b132010-10-26 03:11:13 +00001954
1955 return num;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001956}
1957
Greg Clayton48e42542010-07-30 20:12:55 +00001958void
1959SBTarget::Clear ()
1960{
Greg Clayton5160ce52013-03-27 23:08:40 +00001961 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00001962
1963 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001964 log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00001965
Greg Clayton48e42542010-07-30 20:12:55 +00001966 m_opaque_sp.reset();
1967}
1968
1969
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001970SBModule
1971SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1972{
1973 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00001974 TargetSP target_sp(GetSP());
1975 if (target_sp && sb_file_spec.IsValid())
Greg Claytonaf67cec2010-12-20 20:49:23 +00001976 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001977 ModuleSpec module_spec(*sb_file_spec);
Greg Claytonaf67cec2010-12-20 20:49:23 +00001978 // The module list is thread safe, no need to lock
Greg Claytonb9a01b32012-02-26 05:51:37 +00001979 sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
Greg Claytonaf67cec2010-12-20 20:49:23 +00001980 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981 return sb_module;
1982}
1983
Greg Clayton13d19502012-01-29 06:07:39 +00001984lldb::ByteOrder
1985SBTarget::GetByteOrder ()
1986{
Greg Claytonacdbe812012-01-30 09:04:36 +00001987 TargetSP target_sp(GetSP());
1988 if (target_sp)
1989 return target_sp->GetArchitecture().GetByteOrder();
Greg Clayton13d19502012-01-29 06:07:39 +00001990 return eByteOrderInvalid;
1991}
1992
1993const char *
1994SBTarget::GetTriple ()
1995{
Greg Claytonacdbe812012-01-30 09:04:36 +00001996 TargetSP target_sp(GetSP());
1997 if (target_sp)
Greg Clayton13d19502012-01-29 06:07:39 +00001998 {
Greg Claytonacdbe812012-01-30 09:04:36 +00001999 std::string triple (target_sp->GetArchitecture().GetTriple().str());
Greg Clayton13d19502012-01-29 06:07:39 +00002000 // Unique the string so we don't run into ownership issues since
2001 // the const strings put the string into the string pool once and
2002 // the strings never comes out
2003 ConstString const_triple (triple.c_str());
2004 return const_triple.GetCString();
2005 }
2006 return NULL;
2007}
2008
2009uint32_t
2010SBTarget::GetAddressByteSize()
2011{
Greg Claytonacdbe812012-01-30 09:04:36 +00002012 TargetSP target_sp(GetSP());
2013 if (target_sp)
2014 return target_sp->GetArchitecture().GetAddressByteSize();
Greg Clayton13d19502012-01-29 06:07:39 +00002015 return sizeof(void*);
2016}
2017
2018
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019SBModule
2020SBTarget::GetModuleAtIndex (uint32_t idx)
2021{
Greg Clayton5160ce52013-03-27 23:08:40 +00002022 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002023
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002024 SBModule sb_module;
Greg Claytonacdbe812012-01-30 09:04:36 +00002025 ModuleSP module_sp;
2026 TargetSP target_sp(GetSP());
2027 if (target_sp)
Greg Claytonaf67cec2010-12-20 20:49:23 +00002028 {
2029 // The module list is thread safe, no need to lock
Greg Claytonacdbe812012-01-30 09:04:36 +00002030 module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2031 sb_module.SetSP (module_sp);
Greg Claytonaf67cec2010-12-20 20:49:23 +00002032 }
Caroline Ticeceb6b132010-10-26 03:11:13 +00002033
2034 if (log)
2035 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +00002036 log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002037 target_sp.get(), idx, module_sp.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002038 }
2039
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002040 return sb_module;
2041}
2042
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002043bool
2044SBTarget::RemoveModule (lldb::SBModule module)
2045{
Greg Claytonacdbe812012-01-30 09:04:36 +00002046 TargetSP target_sp(GetSP());
2047 if (target_sp)
2048 return target_sp->GetImages().Remove(module.GetSP());
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002049 return false;
2050}
2051
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002052
2053SBBroadcaster
2054SBTarget::GetBroadcaster () const
2055{
Greg Clayton5160ce52013-03-27 23:08:40 +00002056 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +00002057
Greg Claytonacdbe812012-01-30 09:04:36 +00002058 TargetSP target_sp(GetSP());
2059 SBBroadcaster broadcaster(target_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002060
2061 if (log)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002062 log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
Greg Claytonacdbe812012-01-30 09:04:36 +00002063 target_sp.get(), broadcaster.get());
Caroline Ticeceb6b132010-10-26 03:11:13 +00002064
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002065 return broadcaster;
2066}
2067
Caroline Ticedde9cff2010-09-20 05:20:02 +00002068bool
Caroline Ticeceb6b132010-10-26 03:11:13 +00002069SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
Caroline Ticedde9cff2010-09-20 05:20:02 +00002070{
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002071 Stream &strm = description.ref();
2072
Greg Claytonacdbe812012-01-30 09:04:36 +00002073 TargetSP target_sp(GetSP());
2074 if (target_sp)
Caroline Tice201a8852010-09-20 16:21:41 +00002075 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002076 target_sp->Dump (&strm, description_level);
Caroline Ticeceb6b132010-10-26 03:11:13 +00002077 }
2078 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00002079 strm.PutCString ("No value");
Caroline Ticeceb6b132010-10-26 03:11:13 +00002080
2081 return true;
2082}
2083
Greg Clayton5569e642012-02-06 01:44:54 +00002084lldb::SBSymbolContextList
2085SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
Greg Claytonfe356d32011-06-21 01:34:41 +00002086{
Greg Clayton5569e642012-02-06 01:44:54 +00002087 lldb::SBSymbolContextList sb_sc_list;
Greg Claytonacdbe812012-01-30 09:04:36 +00002088 if (name && name[0])
Greg Claytonfe356d32011-06-21 01:34:41 +00002089 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002090 TargetSP target_sp(GetSP());
2091 if (target_sp)
2092 {
2093 const bool symbols_ok = true;
Sean Callanan9df05fb2012-02-10 22:52:19 +00002094 const bool inlines_ok = true;
Greg Clayton5569e642012-02-06 01:44:54 +00002095 const bool append = true;
2096 target_sp->GetImages().FindFunctions (ConstString(name),
2097 name_type_mask,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002098 symbols_ok,
2099 inlines_ok,
Greg Clayton5569e642012-02-06 01:44:54 +00002100 append,
2101 *sb_sc_list);
Greg Claytonacdbe812012-01-30 09:04:36 +00002102 }
Greg Claytonfe356d32011-06-21 01:34:41 +00002103 }
Greg Clayton5569e642012-02-06 01:44:54 +00002104 return sb_sc_list;
Greg Claytonfe356d32011-06-21 01:34:41 +00002105}
2106
Enrico Granata6f3533f2011-07-29 19:53:35 +00002107lldb::SBType
Greg Claytonb43165b2012-12-05 21:24:42 +00002108SBTarget::FindFirstType (const char* typename_cstr)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002109{
Greg Claytonacdbe812012-01-30 09:04:36 +00002110 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002111 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002112 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002113 ConstString const_typename(typename_cstr);
2114 SymbolContext sc;
2115 const bool exact_match = false;
2116
2117 const ModuleList &module_list = target_sp->GetImages();
2118 size_t count = module_list.GetSize();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002119 for (size_t idx = 0; idx < count; idx++)
2120 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002121 ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2122 if (module_sp)
2123 {
2124 TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2125 if (type_sp)
2126 return SBType(type_sp);
2127 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00002128 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002129
2130 // Didn't find the type in the symbols; try the Objective-C runtime
2131 // if one is installed
2132
2133 ProcessSP process_sp(target_sp->GetProcessSP());
2134
2135 if (process_sp)
2136 {
2137 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2138
2139 if (objc_language_runtime)
2140 {
2141 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2142
2143 if (objc_type_vendor)
2144 {
2145 std::vector <ClangASTType> types;
2146
2147 if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2148 return SBType(types[0]);
2149 }
2150 }
2151 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002152
2153 // No matches, search for basic typename matches
2154 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2155 if (clang_ast)
2156 return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002157 }
2158 return SBType();
2159}
2160
Greg Claytonb43165b2012-12-05 21:24:42 +00002161SBType
2162SBTarget::GetBasicType(lldb::BasicType type)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002163{
Greg Claytonacdbe812012-01-30 09:04:36 +00002164 TargetSP target_sp(GetSP());
Greg Claytonb43165b2012-12-05 21:24:42 +00002165 if (target_sp)
2166 {
2167 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2168 if (clang_ast)
2169 return SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), type));
2170 }
2171 return SBType();
2172}
2173
2174
2175lldb::SBTypeList
2176SBTarget::FindTypes (const char* typename_cstr)
2177{
2178 SBTypeList sb_type_list;
2179 TargetSP target_sp(GetSP());
2180 if (typename_cstr && typename_cstr[0] && target_sp)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002181 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002182 ModuleList& images = target_sp->GetImages();
Greg Claytonb43165b2012-12-05 21:24:42 +00002183 ConstString const_typename(typename_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00002184 bool exact_match = false;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002185 SymbolContext sc;
2186 TypeList type_list;
2187
Greg Clayton29399a22012-04-06 17:41:13 +00002188 uint32_t num_matches = images.FindTypes (sc,
Greg Claytonb43165b2012-12-05 21:24:42 +00002189 const_typename,
Greg Clayton84db9102012-03-26 23:03:23 +00002190 exact_match,
2191 UINT32_MAX,
2192 type_list);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002193
Greg Claytonb43165b2012-12-05 21:24:42 +00002194 if (num_matches > 0)
Enrico Granata6f3533f2011-07-29 19:53:35 +00002195 {
Greg Claytonb43165b2012-12-05 21:24:42 +00002196 for (size_t idx = 0; idx < num_matches; idx++)
2197 {
2198 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2199 if (type_sp)
2200 sb_type_list.Append(SBType(type_sp));
2201 }
2202 }
Sean Callanan7be70e82012-12-19 23:05:01 +00002203
2204 // Try the Objective-C runtime if one is installed
2205
2206 ProcessSP process_sp(target_sp->GetProcessSP());
2207
2208 if (process_sp)
2209 {
2210 ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2211
2212 if (objc_language_runtime)
2213 {
2214 TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2215
2216 if (objc_type_vendor)
2217 {
2218 std::vector <ClangASTType> types;
2219
2220 if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2221 {
2222 for (ClangASTType &type : types)
2223 {
2224 sb_type_list.Append(SBType(type));
2225 }
2226 }
2227 }
2228 }
2229 }
2230
2231 if (sb_type_list.GetSize() == 0)
Greg Claytonb43165b2012-12-05 21:24:42 +00002232 {
2233 // No matches, search for basic typename matches
2234 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2235 if (clang_ast)
2236 sb_type_list.Append (SBType (ClangASTType::GetBasicType (clang_ast->getASTContext(), const_typename)));
Enrico Granata6f3533f2011-07-29 19:53:35 +00002237 }
2238 }
Greg Claytonb43165b2012-12-05 21:24:42 +00002239 return sb_type_list;
Enrico Granata6f3533f2011-07-29 19:53:35 +00002240}
2241
Greg Claytondea8cb42011-06-29 22:09:02 +00002242SBValueList
2243SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2244{
2245 SBValueList sb_value_list;
2246
Greg Claytonacdbe812012-01-30 09:04:36 +00002247 TargetSP target_sp(GetSP());
2248 if (name && target_sp)
Greg Claytondea8cb42011-06-29 22:09:02 +00002249 {
2250 VariableList variable_list;
2251 const bool append = true;
Greg Claytonacdbe812012-01-30 09:04:36 +00002252 const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2253 append,
2254 max_matches,
2255 variable_list);
Greg Claytondea8cb42011-06-29 22:09:02 +00002256
2257 if (match_count > 0)
2258 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002259 ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002260 if (exe_scope == NULL)
Greg Claytonacdbe812012-01-30 09:04:36 +00002261 exe_scope = target_sp.get();
Greg Claytondea8cb42011-06-29 22:09:02 +00002262 for (uint32_t i=0; i<match_count; ++i)
2263 {
2264 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2265 if (valobj_sp)
Enrico Granata85425d72013-02-07 18:23:56 +00002266 sb_value_list.Append(SBValue(valobj_sp));
Greg Claytondea8cb42011-06-29 22:09:02 +00002267 }
2268 }
2269 }
2270
2271 return sb_value_list;
2272}
2273
Enrico Granatabcd80b42013-01-16 18:53:52 +00002274lldb::SBValue
2275SBTarget::FindFirstGlobalVariable (const char* name)
2276{
2277 SBValueList sb_value_list(FindGlobalVariables(name, 1));
2278 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2279 return sb_value_list.GetValueAtIndex(0);
2280 return SBValue();
2281}
2282
Jim Inghame37d6052011-09-13 00:29:56 +00002283SBSourceManager
2284SBTarget::GetSourceManager()
2285{
2286 SBSourceManager source_manager (*this);
2287 return source_manager;
2288}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002289
Sean Callanan50952e92011-12-14 23:49:37 +00002290lldb::SBInstructionList
Greg Clayton9c766112012-03-06 22:24:44 +00002291SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2292{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002293 return ReadInstructions (base_addr, count, NULL);
2294}
2295
2296lldb::SBInstructionList
2297SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2298{
Greg Clayton9c766112012-03-06 22:24:44 +00002299 SBInstructionList sb_instructions;
2300
2301 TargetSP target_sp(GetSP());
2302 if (target_sp)
2303 {
2304 Address *addr_ptr = base_addr.get();
2305
2306 if (addr_ptr)
2307 {
2308 DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2309 bool prefer_file_cache = false;
2310 lldb_private::Error error;
Greg Clayton3faf47c2013-03-28 23:42:53 +00002311 lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2312 const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2313 prefer_file_cache,
2314 data.GetBytes(),
2315 data.GetByteSize(),
2316 error,
2317 &load_addr);
2318 const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
Greg Clayton9c766112012-03-06 22:24:44 +00002319 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2320 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002321 flavor_string,
Greg Clayton9c766112012-03-06 22:24:44 +00002322 *addr_ptr,
2323 data.GetBytes(),
2324 bytes_read,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002325 count,
2326 data_from_file));
Greg Clayton9c766112012-03-06 22:24:44 +00002327 }
2328 }
2329
2330 return sb_instructions;
2331
2332}
2333
2334lldb::SBInstructionList
Sean Callanan50952e92011-12-14 23:49:37 +00002335SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2336{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002337 return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2338}
2339
2340lldb::SBInstructionList
2341SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2342{
Sean Callanan50952e92011-12-14 23:49:37 +00002343 SBInstructionList sb_instructions;
2344
Greg Claytonacdbe812012-01-30 09:04:36 +00002345 TargetSP target_sp(GetSP());
2346 if (target_sp)
Sean Callanan50952e92011-12-14 23:49:37 +00002347 {
2348 Address addr;
2349
2350 if (base_addr.get())
2351 addr = *base_addr.get();
2352
Greg Clayton3faf47c2013-03-28 23:42:53 +00002353 const bool data_from_file = true;
2354
Greg Claytonacdbe812012-01-30 09:04:36 +00002355 sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
Sean Callanan50952e92011-12-14 23:49:37 +00002356 NULL,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002357 flavor_string,
Sean Callanan50952e92011-12-14 23:49:37 +00002358 addr,
2359 buf,
Greg Clayton3faf47c2013-03-28 23:42:53 +00002360 size,
2361 UINT32_MAX,
2362 data_from_file));
Sean Callanan50952e92011-12-14 23:49:37 +00002363 }
2364
2365 return sb_instructions;
2366}
2367
2368lldb::SBInstructionList
2369SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2370{
Jim Ingham0f063ba2013-03-02 00:26:47 +00002371 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2372}
2373
2374lldb::SBInstructionList
2375SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2376{
2377 return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
Sean Callanan50952e92011-12-14 23:49:37 +00002378}
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002379
2380SBError
2381SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2382 lldb::addr_t section_base_addr)
2383{
2384 SBError sb_error;
Greg Claytonacdbe812012-01-30 09:04:36 +00002385 TargetSP target_sp(GetSP());
2386 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002387 {
2388 if (!section.IsValid())
2389 {
2390 sb_error.SetErrorStringWithFormat ("invalid section");
2391 }
2392 else
2393 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002394 SectionSP section_sp (section.GetSP());
2395 if (section_sp)
2396 {
2397 if (section_sp->IsThreadSpecific())
2398 {
2399 sb_error.SetErrorString ("thread specific sections are not yet supported");
2400 }
2401 else
2402 {
Greg Clayton3c947372013-01-29 01:17:09 +00002403 if (target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp, section_base_addr))
2404 {
2405 // Flush info in the process (stack frames, etc)
2406 ProcessSP process_sp (target_sp->GetProcessSP());
2407 if (process_sp)
2408 process_sp->Flush();
2409 }
Greg Clayton741f3f92012-03-27 21:10:07 +00002410 }
2411 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002412 }
2413 }
2414 else
2415 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002416 sb_error.SetErrorString ("invalid target");
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002417 }
2418 return sb_error;
2419}
2420
2421SBError
2422SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2423{
2424 SBError sb_error;
2425
Greg Claytonacdbe812012-01-30 09:04:36 +00002426 TargetSP target_sp(GetSP());
2427 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002428 {
2429 if (!section.IsValid())
2430 {
2431 sb_error.SetErrorStringWithFormat ("invalid section");
2432 }
2433 else
2434 {
Greg Clayton3c947372013-01-29 01:17:09 +00002435 if (target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP()))
2436 {
2437 // Flush info in the process (stack frames, etc)
2438 ProcessSP process_sp (target_sp->GetProcessSP());
2439 if (process_sp)
2440 process_sp->Flush();
2441 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002442 }
2443 }
2444 else
2445 {
2446 sb_error.SetErrorStringWithFormat ("invalid target");
2447 }
2448 return sb_error;
2449}
2450
2451SBError
2452SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2453{
2454 SBError sb_error;
2455
Greg Claytonacdbe812012-01-30 09:04:36 +00002456 TargetSP target_sp(GetSP());
2457 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002458 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002459 ModuleSP module_sp (module.GetSP());
2460 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002461 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002462 bool changed = false;
2463 if (module_sp->SetLoadAddress (*target_sp, slide_offset, changed))
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002464 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002465 // The load was successful, make sure that at least some sections
2466 // changed before we notify that our module was loaded.
2467 if (changed)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002468 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002469 ModuleList module_list;
2470 module_list.Append(module_sp);
2471 target_sp->ModulesDidLoad (module_list);
Greg Clayton3c947372013-01-29 01:17:09 +00002472 // Flush info in the process (stack frames, etc)
2473 ProcessSP process_sp (target_sp->GetProcessSP());
2474 if (process_sp)
2475 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002476 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002477 }
2478 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002479 else
2480 {
2481 sb_error.SetErrorStringWithFormat ("invalid module");
2482 }
2483
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002484 }
2485 else
2486 {
2487 sb_error.SetErrorStringWithFormat ("invalid target");
2488 }
2489 return sb_error;
2490}
2491
2492SBError
2493SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2494{
2495 SBError sb_error;
2496
2497 char path[PATH_MAX];
Greg Claytonacdbe812012-01-30 09:04:36 +00002498 TargetSP target_sp(GetSP());
2499 if (target_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002500 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002501 ModuleSP module_sp (module.GetSP());
2502 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002503 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002504 ObjectFile *objfile = module_sp->GetObjectFile();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002505 if (objfile)
2506 {
2507 SectionList *section_list = objfile->GetSectionList();
2508 if (section_list)
2509 {
Greg Clayton3c947372013-01-29 01:17:09 +00002510 bool changed = false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002511 const size_t num_sections = section_list->GetSize();
2512 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2513 {
2514 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2515 if (section_sp)
Greg Clayton3c947372013-01-29 01:17:09 +00002516 changed |= target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp) > 0;
2517 }
2518 if (changed)
2519 {
2520 // Flush info in the process (stack frames, etc)
2521 ProcessSP process_sp (target_sp->GetProcessSP());
2522 if (process_sp)
2523 process_sp->Flush();
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002524 }
2525 }
2526 else
2527 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002528 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002529 sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2530 }
2531 }
2532 else
2533 {
Greg Claytonacdbe812012-01-30 09:04:36 +00002534 module_sp->GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002535 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2536 }
2537 }
Greg Claytonacdbe812012-01-30 09:04:36 +00002538 else
2539 {
2540 sb_error.SetErrorStringWithFormat ("invalid module");
2541 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +00002542 }
2543 else
2544 {
2545 sb_error.SetErrorStringWithFormat ("invalid target");
2546 }
2547 return sb_error;
2548}
2549
2550
Greg Claytone14e1922012-12-04 02:22:16 +00002551lldb::SBSymbolContextList
2552SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2553{
2554 SBSymbolContextList sb_sc_list;
2555 if (name && name[0])
2556 {
2557 TargetSP target_sp(GetSP());
2558 if (target_sp)
2559 {
2560 bool append = true;
2561 target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2562 symbol_type,
2563 *sb_sc_list,
2564 append);
2565 }
2566 }
2567 return sb_sc_list;
2568
2569}
2570
2571
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002572lldb::SBValue
2573SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2574{
Greg Clayton5160ce52013-03-27 23:08:40 +00002575 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2576 Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002577 SBValue expr_result;
2578 ExecutionResults exe_results = eExecutionSetupError;
2579 ValueObjectSP expr_value_sp;
2580 TargetSP target_sp(GetSP());
2581 StackFrame *frame = NULL;
2582 if (target_sp)
2583 {
2584 if (expr == NULL || expr[0] == '\0')
2585 {
2586 if (log)
2587 log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2588 return expr_result;
2589 }
2590
2591 Mutex::Locker api_locker (target_sp->GetAPIMutex());
2592 ExecutionContext exe_ctx (m_opaque_sp.get());
2593
2594 if (log)
2595 log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2596
2597 frame = exe_ctx.GetFramePtr();
2598 Target *target = exe_ctx.GetTargetPtr();
2599
2600 if (target)
2601 {
2602#ifdef LLDB_CONFIGURATION_DEBUG
2603 StreamString frame_description;
2604 if (frame)
2605 frame->DumpUsingSettingsFormat (&frame_description);
2606 Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2607 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2608#endif
2609 exe_results = target->EvaluateExpression (expr,
2610 frame,
2611 expr_value_sp,
2612 options.ref());
2613
2614 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2615#ifdef LLDB_CONFIGURATION_DEBUG
2616 Host::SetCrashDescription (NULL);
2617#endif
2618 }
2619 else
2620 {
2621 if (log)
2622 log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2623 }
2624 }
2625#ifndef LLDB_DISABLE_PYTHON
2626 if (expr_log)
2627 expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2628 expr_result.GetValue(),
2629 expr_result.GetSummary());
2630
2631 if (log)
2632 log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2633 frame,
2634 expr,
2635 expr_value_sp.get(),
2636 exe_results);
2637#endif
2638
2639 return expr_result;
2640}
2641
Greg Clayton13fbb992013-02-01 00:47:49 +00002642
2643lldb::addr_t
2644SBTarget::GetStackRedZoneSize()
2645{
2646 TargetSP target_sp(GetSP());
2647 if (target_sp)
2648 {
2649 ABISP abi_sp;
2650 ProcessSP process_sp (target_sp->GetProcessSP());
2651 if (process_sp)
2652 abi_sp = process_sp->GetABI();
2653 else
2654 abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2655 if (abi_sp)
2656 return abi_sp->GetRedZoneSize();
2657 }
2658 return 0;
2659}
2660