blob: 6c128b7325f89ee1c5a7175d264d45aaab414db9 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBProcess.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 Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Eli Friedman7a62c8b2010-06-09 07:44:37 +000012#include "lldb/API/SBProcess.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013
14#include "lldb/lldb-defines.h"
15#include "lldb/lldb-types.h"
16
Jim Ingham84cdc152010-06-15 19:49:27 +000017#include "lldb/Interpreter/Args.h"
Greg Clayton1a3083a2010-10-06 03:53:16 +000018#include "lldb/Core/Debugger.h"
Caroline Tice7826c882010-10-26 03:11:13 +000019#include "lldb/Core/Log.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000020#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/State.h"
22#include "lldb/Core/Stream.h"
23#include "lldb/Core/StreamFile.h"
24#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/RegisterContext.h"
Greg Clayton63094e02010-06-23 01:19:29 +000026#include "lldb/Target/Target.h"
27#include "lldb/Target/Thread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028
29// Project includes
30
Eli Friedman7a62c8b2010-06-09 07:44:37 +000031#include "lldb/API/SBBroadcaster.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000032#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton0a8dcac2012-02-24 05:03:03 +000033#include "lldb/API/SBDebugger.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000034#include "lldb/API/SBEvent.h"
Greg Clayton0a8dcac2012-02-24 05:03:03 +000035#include "lldb/API/SBFileSpec.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000036#include "lldb/API/SBThread.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000037#include "lldb/API/SBStream.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000038#include "lldb/API/SBStringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
43
Chris Lattner24943d22010-06-08 16:52:24 +000044SBProcess::SBProcess () :
Greg Claytonbcaf99a2012-07-12 20:32:19 +000045 m_opaque_wp()
Chris Lattner24943d22010-06-08 16:52:24 +000046{
47}
48
49
50//----------------------------------------------------------------------
51// SBProcess constructor
52//----------------------------------------------------------------------
53
54SBProcess::SBProcess (const SBProcess& rhs) :
Greg Claytonbcaf99a2012-07-12 20:32:19 +000055 m_opaque_wp (rhs.m_opaque_wp)
Chris Lattner24943d22010-06-08 16:52:24 +000056{
57}
58
59
60SBProcess::SBProcess (const lldb::ProcessSP &process_sp) :
Greg Claytonbcaf99a2012-07-12 20:32:19 +000061 m_opaque_wp (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000062{
63}
64
Greg Clayton538eb822010-11-05 23:17:00 +000065const SBProcess&
66SBProcess::operator = (const SBProcess& rhs)
67{
68 if (this != &rhs)
Greg Claytonbcaf99a2012-07-12 20:32:19 +000069 m_opaque_wp = rhs.m_opaque_wp;
Greg Clayton538eb822010-11-05 23:17:00 +000070 return *this;
71}
72
Chris Lattner24943d22010-06-08 16:52:24 +000073//----------------------------------------------------------------------
74// Destructor
75//----------------------------------------------------------------------
76SBProcess::~SBProcess()
77{
78}
79
Jim Ingham5a15e692012-02-16 06:50:00 +000080const char *
81SBProcess::GetBroadcasterClassName ()
82{
83 return Process::GetStaticBroadcasterClass().AsCString();
84}
85
Jim Inghamfee26ee2012-10-26 19:18:04 +000086const char *
87SBProcess::GetPluginName ()
88{
89 ProcessSP process_sp(GetSP());
90 if (process_sp)
91 {
92 return process_sp->GetPluginName();
93 }
94 return "<Unknown>";
95}
96
97const char *
98SBProcess::GetShortPluginName ()
99{
100 ProcessSP process_sp(GetSP());
101 if (process_sp)
102 {
103 return process_sp->GetShortPluginName();
104 }
105 return "<Unknown>";
106}
107
108
Greg Clayton334d33a2012-01-30 07:41:31 +0000109lldb::ProcessSP
110SBProcess::GetSP() const
111{
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000112 return m_opaque_wp.lock();
Greg Clayton334d33a2012-01-30 07:41:31 +0000113}
114
Chris Lattner24943d22010-06-08 16:52:24 +0000115void
Greg Clayton334d33a2012-01-30 07:41:31 +0000116SBProcess::SetSP (const ProcessSP &process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000117{
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000118 m_opaque_wp = process_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000119}
120
121void
122SBProcess::Clear ()
123{
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000124 m_opaque_wp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000125}
126
127
128bool
129SBProcess::IsValid() const
130{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000131 ProcessSP process_sp(m_opaque_wp.lock());
132 return ((bool) process_sp && process_sp->IsValid());
Chris Lattner24943d22010-06-08 16:52:24 +0000133}
134
James McIlree38093402011-03-04 00:31:13 +0000135bool
136SBProcess::RemoteLaunch (char const **argv,
137 char const **envp,
138 const char *stdin_path,
139 const char *stdout_path,
140 const char *stderr_path,
141 const char *working_directory,
142 uint32_t launch_flags,
143 bool stop_at_entry,
144 lldb::SBError& error)
145{
146 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
147 if (log) {
148 log->Printf ("SBProcess(%p)::RemoteLaunch (argv=%p, envp=%p, stdin=%s, stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, stop_at_entry=%i, &error (%p))...",
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000149 m_opaque_wp.lock().get(),
James McIlree38093402011-03-04 00:31:13 +0000150 argv,
151 envp,
152 stdin_path ? stdin_path : "NULL",
153 stdout_path ? stdout_path : "NULL",
154 stderr_path ? stderr_path : "NULL",
155 working_directory ? working_directory : "NULL",
156 launch_flags,
157 stop_at_entry,
158 error.get());
159 }
160
Greg Clayton0416bdf2012-01-30 09:04:36 +0000161 ProcessSP process_sp(GetSP());
162 if (process_sp)
James McIlree38093402011-03-04 00:31:13 +0000163 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000164 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
165 if (process_sp->GetState() == eStateConnected)
James McIlree38093402011-03-04 00:31:13 +0000166 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000167 if (stop_at_entry)
168 launch_flags |= eLaunchFlagStopAtEntry;
169 ProcessLaunchInfo launch_info (stdin_path,
170 stdout_path,
171 stderr_path,
172 working_directory,
173 launch_flags);
174 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
175 if (exe_module)
176 launch_info.SetExecutableFile(exe_module->GetFileSpec(), true);
177 if (argv)
178 launch_info.GetArguments().AppendArguments (argv);
179 if (envp)
180 launch_info.GetEnvironmentEntries ().SetArguments (envp);
181 error.SetError (process_sp->Launch (launch_info));
James McIlree38093402011-03-04 00:31:13 +0000182 }
183 else
184 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000185 error.SetErrorString ("must be in eStateConnected to call RemoteLaunch");
James McIlree38093402011-03-04 00:31:13 +0000186 }
187 }
188 else
189 {
190 error.SetErrorString ("unable to attach pid");
191 }
192
193 if (log) {
194 SBStream sstr;
195 error.GetDescription (sstr);
Greg Clayton0416bdf2012-01-30 09:04:36 +0000196 log->Printf ("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s", process_sp.get(), error.get(), sstr.GetData());
James McIlree38093402011-03-04 00:31:13 +0000197 }
198
199 return error.Success();
200}
201
202bool
203SBProcess::RemoteAttachToProcessWithID (lldb::pid_t pid, lldb::SBError& error)
204{
Greg Clayton0416bdf2012-01-30 09:04:36 +0000205 ProcessSP process_sp(GetSP());
206 if (process_sp)
James McIlree38093402011-03-04 00:31:13 +0000207 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000208 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
209 if (process_sp->GetState() == eStateConnected)
James McIlree38093402011-03-04 00:31:13 +0000210 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000211 ProcessAttachInfo attach_info;
212 attach_info.SetProcessID (pid);
213 error.SetError (process_sp->Attach (attach_info));
James McIlree38093402011-03-04 00:31:13 +0000214 }
215 else
216 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000217 error.SetErrorString ("must be in eStateConnected to call RemoteAttachToProcessWithID");
James McIlree38093402011-03-04 00:31:13 +0000218 }
219 }
220 else
221 {
222 error.SetErrorString ("unable to attach pid");
223 }
224
225 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
226 if (log) {
227 SBStream sstr;
228 error.GetDescription (sstr);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000229 log->Printf ("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64 ") => SBError (%p): %s", process_sp.get(), pid, error.get(), sstr.GetData());
James McIlree38093402011-03-04 00:31:13 +0000230 }
231
232 return error.Success();
233}
234
Chris Lattner24943d22010-06-08 16:52:24 +0000235
236uint32_t
237SBProcess::GetNumThreads ()
238{
Greg Claytone005f2c2010-11-06 01:53:30 +0000239 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000240
Caroline Tice7826c882010-10-26 03:11:13 +0000241 uint32_t num_threads = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000242 ProcessSP process_sp(GetSP());
243 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000244 {
Greg Claytona894fe72012-04-05 16:12:35 +0000245 Process::StopLocker stop_locker;
246
247 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
Greg Clayton0416bdf2012-01-30 09:04:36 +0000248 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Clayton0416bdf2012-01-30 09:04:36 +0000249 num_threads = process_sp->GetThreadList().GetSize(can_update);
Chris Lattner24943d22010-06-08 16:52:24 +0000250 }
Caroline Tice7826c882010-10-26 03:11:13 +0000251
252 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000253 log->Printf ("SBProcess(%p)::GetNumThreads () => %d", process_sp.get(), num_threads);
Caroline Tice7826c882010-10-26 03:11:13 +0000254
255 return num_threads;
Chris Lattner24943d22010-06-08 16:52:24 +0000256}
257
258SBThread
Jim Inghamc8332952010-08-26 21:32:51 +0000259SBProcess::GetSelectedThread () const
Chris Lattner24943d22010-06-08 16:52:24 +0000260{
Greg Claytone005f2c2010-11-06 01:53:30 +0000261 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000262
Chris Lattner24943d22010-06-08 16:52:24 +0000263 SBThread sb_thread;
Greg Clayton90c52142012-01-30 02:53:15 +0000264 ThreadSP thread_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000265 ProcessSP process_sp(GetSP());
266 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000267 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000268 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
269 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Greg Clayton90c52142012-01-30 02:53:15 +0000270 sb_thread.SetThread (thread_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000271 }
Caroline Tice7826c882010-10-26 03:11:13 +0000272
273 if (log)
274 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000275 log->Printf ("SBProcess(%p)::GetSelectedThread () => SBThread(%p)", process_sp.get(), thread_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000276 }
277
Chris Lattner24943d22010-06-08 16:52:24 +0000278 return sb_thread;
279}
280
281SBTarget
282SBProcess::GetTarget() const
283{
Greg Claytone005f2c2010-11-06 01:53:30 +0000284 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000285
Chris Lattner24943d22010-06-08 16:52:24 +0000286 SBTarget sb_target;
Greg Clayton334d33a2012-01-30 07:41:31 +0000287 TargetSP target_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000288 ProcessSP process_sp(GetSP());
289 if (process_sp)
Greg Clayton334d33a2012-01-30 07:41:31 +0000290 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000291 target_sp = process_sp->GetTarget().shared_from_this();
Greg Clayton334d33a2012-01-30 07:41:31 +0000292 sb_target.SetSP (target_sp);
293 }
Caroline Tice7826c882010-10-26 03:11:13 +0000294
295 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000296 log->Printf ("SBProcess(%p)::GetTarget () => SBTarget(%p)", process_sp.get(), target_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000297
Chris Lattner24943d22010-06-08 16:52:24 +0000298 return sb_target;
299}
300
301
302size_t
303SBProcess::PutSTDIN (const char *src, size_t src_len)
304{
Greg Claytone005f2c2010-11-06 01:53:30 +0000305 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000306
Caroline Tice7826c882010-10-26 03:11:13 +0000307 size_t ret_val = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000308 ProcessSP process_sp(GetSP());
309 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000310 {
311 Error error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000312 ret_val = process_sp->PutSTDIN (src, src_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000313 }
Caroline Tice7826c882010-10-26 03:11:13 +0000314
315 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000316 log->Printf ("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%d) => %lu",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000317 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000318 src,
319 (uint32_t) src_len,
320 ret_val);
Caroline Tice7826c882010-10-26 03:11:13 +0000321
322 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000323}
324
325size_t
326SBProcess::GetSTDOUT (char *dst, size_t dst_len) const
327{
Greg Clayton49ce6822010-10-31 03:01:06 +0000328 size_t bytes_read = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000329 ProcessSP process_sp(GetSP());
330 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000331 {
332 Error error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000333 bytes_read = process_sp->GetSTDOUT (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000334 }
Caroline Tice7826c882010-10-26 03:11:13 +0000335
Greg Claytone005f2c2010-11-06 01:53:30 +0000336 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000337 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000338 log->Printf ("SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
Greg Clayton851e30e2012-09-18 18:04:04 +0000339 process_sp.get(),
340 (int) bytes_read,
341 dst,
342 (uint64_t)dst_len,
343 (uint64_t)bytes_read);
Caroline Tice7826c882010-10-26 03:11:13 +0000344
Greg Clayton49ce6822010-10-31 03:01:06 +0000345 return bytes_read;
Chris Lattner24943d22010-06-08 16:52:24 +0000346}
347
348size_t
349SBProcess::GetSTDERR (char *dst, size_t dst_len) const
350{
Greg Clayton49ce6822010-10-31 03:01:06 +0000351 size_t bytes_read = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000352 ProcessSP process_sp(GetSP());
353 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000354 {
355 Error error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000356 bytes_read = process_sp->GetSTDERR (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000357 }
Caroline Tice7826c882010-10-26 03:11:13 +0000358
Greg Claytone005f2c2010-11-06 01:53:30 +0000359 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000360 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000361 log->Printf ("SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
Greg Clayton851e30e2012-09-18 18:04:04 +0000362 process_sp.get(),
363 (int) bytes_read,
364 dst,
365 (uint64_t)dst_len,
366 (uint64_t)bytes_read);
Caroline Tice7826c882010-10-26 03:11:13 +0000367
Greg Clayton49ce6822010-10-31 03:01:06 +0000368 return bytes_read;
Chris Lattner24943d22010-06-08 16:52:24 +0000369}
370
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000371size_t
372SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const
373{
374 size_t bytes_read = 0;
375 ProcessSP process_sp(GetSP());
376 if (process_sp)
377 {
378 Error error;
379 bytes_read = process_sp->GetAsyncProfileData (dst, dst_len, error);
380 }
381
382 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
383 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000384 log->Printf ("SBProcess(%p)::GetProfileData (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000385 process_sp.get(),
386 (int) bytes_read,
387 dst,
388 (uint64_t)dst_len,
389 (uint64_t)bytes_read);
390
391 return bytes_read;
392}
393
Chris Lattner24943d22010-06-08 16:52:24 +0000394void
Jim Inghamc8332952010-08-26 21:32:51 +0000395SBProcess::ReportEventState (const SBEvent &event, FILE *out) const
Chris Lattner24943d22010-06-08 16:52:24 +0000396{
397 if (out == NULL)
398 return;
399
Greg Clayton0416bdf2012-01-30 09:04:36 +0000400 ProcessSP process_sp(GetSP());
401 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000402 {
403 const StateType event_state = SBProcess::GetStateFromEvent (event);
404 char message[1024];
405 int message_len = ::snprintf (message,
406 sizeof (message),
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000407 "Process %" PRIu64 " %s\n",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000408 process_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000409 SBDebugger::StateAsCString (event_state));
410
411 if (message_len > 0)
412 ::fwrite (message, 1, message_len, out);
413 }
414}
415
416void
Jim Inghamc8332952010-08-26 21:32:51 +0000417SBProcess::AppendEventStateReport (const SBEvent &event, SBCommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000418{
Greg Clayton0416bdf2012-01-30 09:04:36 +0000419 ProcessSP process_sp(GetSP());
420 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000421 {
422 const StateType event_state = SBProcess::GetStateFromEvent (event);
423 char message[1024];
424 ::snprintf (message,
425 sizeof (message),
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000426 "Process %" PRIu64 " %s\n",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000427 process_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000428 SBDebugger::StateAsCString (event_state));
429
430 result.AppendMessage (message);
431 }
432}
433
434bool
Jim Inghamc8332952010-08-26 21:32:51 +0000435SBProcess::SetSelectedThread (const SBThread &thread)
Chris Lattner24943d22010-06-08 16:52:24 +0000436{
Greg Clayton0416bdf2012-01-30 09:04:36 +0000437 ProcessSP process_sp(GetSP());
438 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000439 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000440 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
441 return process_sp->GetThreadList().SetSelectedThreadByID (thread.GetThreadID());
Greg Claytonbdcda462010-12-20 20:49:23 +0000442 }
Chris Lattner24943d22010-06-08 16:52:24 +0000443 return false;
444}
445
446bool
Greg Clayton82560f22012-10-12 23:32:11 +0000447SBProcess::SetSelectedThreadByID (lldb::tid_t tid)
448{
Greg Claytone005f2c2010-11-06 01:53:30 +0000449 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000450
Caroline Tice7826c882010-10-26 03:11:13 +0000451 bool ret_val = false;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000452 ProcessSP process_sp(GetSP());
453 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000454 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000455 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
456 ret_val = process_sp->GetThreadList().SetSelectedThreadByID (tid);
Greg Claytonbdcda462010-12-20 20:49:23 +0000457 }
Caroline Tice7826c882010-10-26 03:11:13 +0000458
459 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000460 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64 ") => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000461 process_sp.get(), tid, (ret_val ? "true" : "false"));
Caroline Tice7826c882010-10-26 03:11:13 +0000462
463 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000464}
465
Jim Inghamefbdd222012-07-13 20:18:18 +0000466bool
467SBProcess::SetSelectedThreadByIndexID (uint32_t index_id)
468{
469 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
470
471 bool ret_val = false;
472 ProcessSP process_sp(GetSP());
473 if (process_sp)
474 {
475 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
476 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID (index_id);
477 }
478
479 if (log)
480 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
481 process_sp.get(), index_id, (ret_val ? "true" : "false"));
482
483 return ret_val;
484}
485
Chris Lattner24943d22010-06-08 16:52:24 +0000486SBThread
487SBProcess::GetThreadAtIndex (size_t index)
488{
Greg Claytone005f2c2010-11-06 01:53:30 +0000489 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000490
Greg Clayton90c52142012-01-30 02:53:15 +0000491 SBThread sb_thread;
492 ThreadSP thread_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000493 ProcessSP process_sp(GetSP());
494 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000495 {
Greg Claytona894fe72012-04-05 16:12:35 +0000496 Process::StopLocker stop_locker;
497 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
Greg Clayton0416bdf2012-01-30 09:04:36 +0000498 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytona894fe72012-04-05 16:12:35 +0000499 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
Greg Clayton90c52142012-01-30 02:53:15 +0000500 sb_thread.SetThread (thread_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000501 }
Caroline Tice7826c882010-10-26 03:11:13 +0000502
503 if (log)
504 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000505 log->Printf ("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000506 process_sp.get(), (uint32_t) index, thread_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000507 }
508
Greg Clayton90c52142012-01-30 02:53:15 +0000509 return sb_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000510}
511
Jim Ingham0e3b98e2013-01-08 23:22:42 +0000512uint32_t
513SBProcess::GetStopID(bool include_expression_stops)
514{
515 ProcessSP process_sp(GetSP());
516 if (process_sp)
517 {
518 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
519 if (include_expression_stops)
520 return process_sp->GetStopID();
521 else
522 return process_sp->GetLastNaturalStopID();
523 }
524 return 0;
525}
526
Chris Lattner24943d22010-06-08 16:52:24 +0000527StateType
528SBProcess::GetState ()
529{
Caroline Tice7826c882010-10-26 03:11:13 +0000530
Caroline Tice7826c882010-10-26 03:11:13 +0000531 StateType ret_val = eStateInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000532 ProcessSP process_sp(GetSP());
533 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000534 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000535 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
536 ret_val = process_sp->GetState();
Greg Claytonbdcda462010-12-20 20:49:23 +0000537 }
Caroline Tice7826c882010-10-26 03:11:13 +0000538
Greg Claytone005f2c2010-11-06 01:53:30 +0000539 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000540 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000541 log->Printf ("SBProcess(%p)::GetState () => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000542 process_sp.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000543 lldb_private::StateAsCString (ret_val));
Caroline Tice7826c882010-10-26 03:11:13 +0000544
545 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000546}
547
548
549int
550SBProcess::GetExitStatus ()
551{
Greg Claytona66ba462010-10-30 04:51:46 +0000552 int exit_status = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000553 ProcessSP process_sp(GetSP());
554 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000555 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000556 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
557 exit_status = process_sp->GetExitStatus ();
Greg Claytonbdcda462010-12-20 20:49:23 +0000558 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000559 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000560 if (log)
561 log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000562 process_sp.get(), exit_status, exit_status);
Greg Claytona66ba462010-10-30 04:51:46 +0000563
564 return exit_status;
Chris Lattner24943d22010-06-08 16:52:24 +0000565}
566
567const char *
568SBProcess::GetExitDescription ()
569{
Greg Claytona66ba462010-10-30 04:51:46 +0000570 const char *exit_desc = NULL;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000571 ProcessSP process_sp(GetSP());
572 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000573 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000574 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
575 exit_desc = process_sp->GetExitDescription ();
Greg Claytonbdcda462010-12-20 20:49:23 +0000576 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000577 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000578 if (log)
579 log->Printf ("SBProcess(%p)::GetExitDescription () => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000580 process_sp.get(), exit_desc);
Greg Claytona66ba462010-10-30 04:51:46 +0000581 return exit_desc;
Chris Lattner24943d22010-06-08 16:52:24 +0000582}
583
584lldb::pid_t
585SBProcess::GetProcessID ()
586{
Caroline Tice7826c882010-10-26 03:11:13 +0000587 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000588 ProcessSP process_sp(GetSP());
589 if (process_sp)
590 ret_val = process_sp->GetID();
Caroline Tice7826c882010-10-26 03:11:13 +0000591
Greg Claytone005f2c2010-11-06 01:53:30 +0000592 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000593 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000594 log->Printf ("SBProcess(%p)::GetProcessID () => %" PRIu64, process_sp.get(), ret_val);
Caroline Tice7826c882010-10-26 03:11:13 +0000595
596 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000597}
598
Greg Clayton64742742013-01-16 17:29:04 +0000599uint32_t
600SBProcess::GetUniqueID()
601{
602 uint32_t ret_val = 0;
603 ProcessSP process_sp(GetSP());
604 if (process_sp)
605 ret_val = process_sp->GetUniqueID();
606 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
607 if (log)
608 log->Printf ("SBProcess(%p)::GetUniqueID () => %" PRIu32, process_sp.get(), ret_val);
609 return ret_val;
610}
611
Johnny Chen60a544f2011-03-01 22:56:31 +0000612ByteOrder
613SBProcess::GetByteOrder () const
614{
615 ByteOrder byteOrder = eByteOrderInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000616 ProcessSP process_sp(GetSP());
617 if (process_sp)
618 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
Johnny Chen60a544f2011-03-01 22:56:31 +0000619
620 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
621 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000622 log->Printf ("SBProcess(%p)::GetByteOrder () => %d", process_sp.get(), byteOrder);
Johnny Chen60a544f2011-03-01 22:56:31 +0000623
624 return byteOrder;
625}
626
Chris Lattner24943d22010-06-08 16:52:24 +0000627uint32_t
628SBProcess::GetAddressByteSize () const
629{
Caroline Tice7826c882010-10-26 03:11:13 +0000630 uint32_t size = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000631 ProcessSP process_sp(GetSP());
632 if (process_sp)
633 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
Caroline Tice7826c882010-10-26 03:11:13 +0000634
Greg Claytone005f2c2010-11-06 01:53:30 +0000635 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000636 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000637 log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d", process_sp.get(), size);
Caroline Tice7826c882010-10-26 03:11:13 +0000638
639 return size;
Chris Lattner24943d22010-06-08 16:52:24 +0000640}
641
Chris Lattner24943d22010-06-08 16:52:24 +0000642SBError
643SBProcess::Continue ()
644{
Greg Claytone005f2c2010-11-06 01:53:30 +0000645 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000646
Chris Lattner24943d22010-06-08 16:52:24 +0000647 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000648 ProcessSP process_sp(GetSP());
649
650 if (log)
651 log->Printf ("SBProcess(%p)::Continue ()...", process_sp.get());
652
653 if (process_sp)
Greg Clayton1a3083a2010-10-06 03:53:16 +0000654 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000655 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytonde1dd812011-06-24 03:21:43 +0000656
Greg Clayton0416bdf2012-01-30 09:04:36 +0000657 Error error (process_sp->Resume());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000658 if (error.Success())
659 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000660 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution () == false)
Greg Claytona66ba462010-10-30 04:51:46 +0000661 {
662 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000663 log->Printf ("SBProcess(%p)::Continue () waiting for process to stop...", process_sp.get());
664 process_sp->WaitForProcessToStop (NULL);
Greg Claytona66ba462010-10-30 04:51:46 +0000665 }
Greg Clayton1a3083a2010-10-06 03:53:16 +0000666 }
667 sb_error.SetError(error);
668 }
Chris Lattner24943d22010-06-08 16:52:24 +0000669 else
670 sb_error.SetErrorString ("SBProcess is invalid");
671
Caroline Tice7826c882010-10-26 03:11:13 +0000672 if (log)
673 {
674 SBStream sstr;
675 sb_error.GetDescription (sstr);
Greg Clayton0416bdf2012-01-30 09:04:36 +0000676 log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s", process_sp.get(), sb_error.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000677 }
678
Chris Lattner24943d22010-06-08 16:52:24 +0000679 return sb_error;
680}
681
682
683SBError
684SBProcess::Destroy ()
685{
686 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000687 ProcessSP process_sp(GetSP());
688 if (process_sp)
Greg Clayton72e1c782011-01-22 23:43:18 +0000689 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000690 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
691 sb_error.SetError(process_sp->Destroy());
Greg Clayton72e1c782011-01-22 23:43:18 +0000692 }
Chris Lattner24943d22010-06-08 16:52:24 +0000693 else
694 sb_error.SetErrorString ("SBProcess is invalid");
695
Greg Claytone005f2c2010-11-06 01:53:30 +0000696 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000697 if (log)
698 {
699 SBStream sstr;
700 sb_error.GetDescription (sstr);
Greg Clayton72e1c782011-01-22 23:43:18 +0000701 log->Printf ("SBProcess(%p)::Destroy () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000702 process_sp.get(),
Greg Clayton72e1c782011-01-22 23:43:18 +0000703 sb_error.get(),
704 sstr.GetData());
Greg Claytona66ba462010-10-30 04:51:46 +0000705 }
706
Chris Lattner24943d22010-06-08 16:52:24 +0000707 return sb_error;
708}
709
710
711SBError
712SBProcess::Stop ()
713{
714 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000715 ProcessSP process_sp(GetSP());
716 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000717 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000718 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
719 sb_error.SetError (process_sp->Halt());
Greg Claytonbdcda462010-12-20 20:49:23 +0000720 }
Chris Lattner24943d22010-06-08 16:52:24 +0000721 else
722 sb_error.SetErrorString ("SBProcess is invalid");
Caroline Tice7826c882010-10-26 03:11:13 +0000723
Greg Claytone005f2c2010-11-06 01:53:30 +0000724 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000725 if (log)
726 {
727 SBStream sstr;
728 sb_error.GetDescription (sstr);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000729 log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000730 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000731 sb_error.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000732 sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000733 }
734
Chris Lattner24943d22010-06-08 16:52:24 +0000735 return sb_error;
736}
737
738SBError
739SBProcess::Kill ()
740{
741 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000742 ProcessSP process_sp(GetSP());
743 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000744 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000745 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
746 sb_error.SetError (process_sp->Destroy());
Greg Claytonbdcda462010-12-20 20:49:23 +0000747 }
Chris Lattner24943d22010-06-08 16:52:24 +0000748 else
749 sb_error.SetErrorString ("SBProcess is invalid");
Caroline Tice7826c882010-10-26 03:11:13 +0000750
Greg Claytone005f2c2010-11-06 01:53:30 +0000751 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000752 if (log)
753 {
754 SBStream sstr;
755 sb_error.GetDescription (sstr);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000756 log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000757 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000758 sb_error.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000759 sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000760 }
761
Chris Lattner24943d22010-06-08 16:52:24 +0000762 return sb_error;
763}
764
Chris Lattner24943d22010-06-08 16:52:24 +0000765SBError
766SBProcess::Detach ()
767{
768 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000769 ProcessSP process_sp(GetSP());
770 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000771 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000772 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
773 sb_error.SetError (process_sp->Detach());
Greg Claytonbdcda462010-12-20 20:49:23 +0000774 }
Chris Lattner24943d22010-06-08 16:52:24 +0000775 else
776 sb_error.SetErrorString ("SBProcess is invalid");
777
778 return sb_error;
779}
780
781SBError
Greg Claytona66ba462010-10-30 04:51:46 +0000782SBProcess::Signal (int signo)
Chris Lattner24943d22010-06-08 16:52:24 +0000783{
784 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000785 ProcessSP process_sp(GetSP());
786 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000787 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000788 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
789 sb_error.SetError (process_sp->Signal (signo));
Greg Claytonbdcda462010-12-20 20:49:23 +0000790 }
Chris Lattner24943d22010-06-08 16:52:24 +0000791 else
792 sb_error.SetErrorString ("SBProcess is invalid");
Greg Claytone005f2c2010-11-06 01:53:30 +0000793 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000794 if (log)
795 {
796 SBStream sstr;
797 sb_error.GetDescription (sstr);
798 log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000799 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000800 signo,
801 sb_error.get(),
802 sstr.GetData());
803 }
Chris Lattner24943d22010-06-08 16:52:24 +0000804 return sb_error;
805}
806
Jim Ingham5d90ade2012-07-27 23:57:19 +0000807void
808SBProcess::SendAsyncInterrupt ()
809{
810 ProcessSP process_sp(GetSP());
811 if (process_sp)
812 {
813 process_sp->SendAsyncInterrupt ();
814 }
815}
816
Chris Lattner24943d22010-06-08 16:52:24 +0000817SBThread
Greg Claytona66ba462010-10-30 04:51:46 +0000818SBProcess::GetThreadByID (tid_t tid)
Chris Lattner24943d22010-06-08 16:52:24 +0000819{
Greg Claytona66ba462010-10-30 04:51:46 +0000820 SBThread sb_thread;
Greg Clayton90c52142012-01-30 02:53:15 +0000821 ThreadSP thread_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000822 ProcessSP process_sp(GetSP());
823 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000824 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000825 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytona894fe72012-04-05 16:12:35 +0000826 Process::StopLocker stop_locker;
827 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
828 thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update);
Greg Clayton90c52142012-01-30 02:53:15 +0000829 sb_thread.SetThread (thread_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000830 }
Greg Claytona66ba462010-10-30 04:51:46 +0000831
Greg Claytone005f2c2010-11-06 01:53:30 +0000832 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000833 if (log)
834 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000835 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64 ") => SBThread (%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000836 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000837 tid,
Greg Clayton90c52142012-01-30 02:53:15 +0000838 thread_sp.get());
Greg Claytona66ba462010-10-30 04:51:46 +0000839 }
840
841 return sb_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000842}
843
Jim Inghamefbdd222012-07-13 20:18:18 +0000844SBThread
845SBProcess::GetThreadByIndexID (uint32_t index_id)
846{
847 SBThread sb_thread;
848 ThreadSP thread_sp;
849 ProcessSP process_sp(GetSP());
850 if (process_sp)
851 {
852 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
853 Process::StopLocker stop_locker;
854 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
855 thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update);
856 sb_thread.SetThread (thread_sp);
857 }
858
859 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
860 if (log)
861 {
862 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
863 process_sp.get(),
864 index_id,
865 thread_sp.get());
866 }
867
868 return sb_thread;
869}
870
Chris Lattner24943d22010-06-08 16:52:24 +0000871StateType
872SBProcess::GetStateFromEvent (const SBEvent &event)
873{
Greg Claytone005f2c2010-11-06 01:53:30 +0000874 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000875
Caroline Tice7826c882010-10-26 03:11:13 +0000876 StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get());
877
878 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000879 log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s", event.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000880 lldb_private::StateAsCString (ret_val));
Caroline Tice7826c882010-10-26 03:11:13 +0000881
882 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000883}
884
Chris Lattner24943d22010-06-08 16:52:24 +0000885bool
886SBProcess::GetRestartedFromEvent (const SBEvent &event)
887{
Greg Clayton63094e02010-06-23 01:19:29 +0000888 return Process::ProcessEventData::GetRestartedFromEvent (event.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000889}
890
891SBProcess
892SBProcess::GetProcessFromEvent (const SBEvent &event)
893{
Greg Clayton63094e02010-06-23 01:19:29 +0000894 SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
Chris Lattner24943d22010-06-08 16:52:24 +0000895 return process;
896}
897
Jim Ingham28e23862012-02-08 05:23:15 +0000898bool
899SBProcess::EventIsProcessEvent (const SBEvent &event)
900{
Jim Ingham5a15e692012-02-16 06:50:00 +0000901 return strcmp (event.GetBroadcasterClass(), SBProcess::GetBroadcasterClass()) == 0;
Jim Ingham28e23862012-02-08 05:23:15 +0000902}
Chris Lattner24943d22010-06-08 16:52:24 +0000903
904SBBroadcaster
905SBProcess::GetBroadcaster () const
906{
Greg Claytone005f2c2010-11-06 01:53:30 +0000907 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000908
Greg Clayton0416bdf2012-01-30 09:04:36 +0000909 ProcessSP process_sp(GetSP());
910
911 SBBroadcaster broadcaster(process_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +0000912
913 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000914 log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)", process_sp.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000915 broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000916
Chris Lattner24943d22010-06-08 16:52:24 +0000917 return broadcaster;
918}
919
Jim Ingham5a15e692012-02-16 06:50:00 +0000920const char *
921SBProcess::GetBroadcasterClass ()
922{
923 return Process::GetStaticBroadcasterClass().AsCString();
924}
925
Chris Lattner24943d22010-06-08 16:52:24 +0000926size_t
927SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
928{
Greg Claytone005f2c2010-11-06 01:53:30 +0000929 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000930
Chris Lattner24943d22010-06-08 16:52:24 +0000931 size_t bytes_read = 0;
932
Greg Clayton0416bdf2012-01-30 09:04:36 +0000933 ProcessSP process_sp(GetSP());
934
Greg Claytona66ba462010-10-30 04:51:46 +0000935 if (log)
936 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000937 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000938 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000939 addr,
940 dst,
Greg Clayton851e30e2012-09-18 18:04:04 +0000941 (uint64_t)dst_len,
Greg Claytona66ba462010-10-30 04:51:46 +0000942 sb_error.get());
943 }
Greg Clayton0416bdf2012-01-30 09:04:36 +0000944
945 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000946 {
Greg Claytona894fe72012-04-05 16:12:35 +0000947 Process::StopLocker stop_locker;
948 if (stop_locker.TryLock(&process_sp->GetRunLock()))
949 {
950 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
951 bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref());
952 }
953 else
954 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000955 if (log)
956 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +0000957 sb_error.SetErrorString("process is running");
958 }
Chris Lattner24943d22010-06-08 16:52:24 +0000959 }
960 else
961 {
962 sb_error.SetErrorString ("SBProcess is invalid");
963 }
964
Caroline Tice7826c882010-10-26 03:11:13 +0000965 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000966 {
967 SBStream sstr;
968 sb_error.GetDescription (sstr);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000969 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
Greg Clayton0416bdf2012-01-30 09:04:36 +0000970 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000971 addr,
972 dst,
Greg Clayton851e30e2012-09-18 18:04:04 +0000973 (uint64_t)dst_len,
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000974 sb_error.get(),
975 sstr.GetData(),
Greg Clayton851e30e2012-09-18 18:04:04 +0000976 (uint64_t)bytes_read);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000977 }
Caroline Tice7826c882010-10-26 03:11:13 +0000978
Chris Lattner24943d22010-06-08 16:52:24 +0000979 return bytes_read;
980}
981
982size_t
Greg Clayton4a2e3372011-12-15 03:14:23 +0000983SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error)
984{
985 size_t bytes_read = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000986 ProcessSP process_sp(GetSP());
987 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +0000988 {
Greg Claytona894fe72012-04-05 16:12:35 +0000989 Process::StopLocker stop_locker;
990 if (stop_locker.TryLock(&process_sp->GetRunLock()))
991 {
992 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
993 bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref());
994 }
995 else
996 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000997 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
998 if (log)
999 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001000 sb_error.SetErrorString("process is running");
1001 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001002 }
1003 else
1004 {
1005 sb_error.SetErrorString ("SBProcess is invalid");
1006 }
1007 return bytes_read;
1008}
1009
1010uint64_t
1011SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error)
1012{
Greg Claytona894fe72012-04-05 16:12:35 +00001013 uint64_t value = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001014 ProcessSP process_sp(GetSP());
1015 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +00001016 {
Greg Claytona894fe72012-04-05 16:12:35 +00001017 Process::StopLocker stop_locker;
1018 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1019 {
1020 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1021 value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref());
1022 }
1023 else
1024 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001025 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1026 if (log)
1027 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001028 sb_error.SetErrorString("process is running");
1029 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001030 }
1031 else
1032 {
1033 sb_error.SetErrorString ("SBProcess is invalid");
1034 }
Greg Claytona894fe72012-04-05 16:12:35 +00001035 return value;
Greg Clayton4a2e3372011-12-15 03:14:23 +00001036}
1037
1038lldb::addr_t
1039SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error)
1040{
1041 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001042 ProcessSP process_sp(GetSP());
1043 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +00001044 {
Greg Claytona894fe72012-04-05 16:12:35 +00001045 Process::StopLocker stop_locker;
1046 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1047 {
1048 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1049 ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref());
1050 }
1051 else
1052 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001053 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1054 if (log)
1055 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001056 sb_error.SetErrorString("process is running");
1057 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001058 }
1059 else
1060 {
1061 sb_error.SetErrorString ("SBProcess is invalid");
1062 }
1063 return ptr;
1064}
1065
1066size_t
Chris Lattner24943d22010-06-08 16:52:24 +00001067SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
1068{
1069 size_t bytes_written = 0;
1070
Greg Claytone005f2c2010-11-06 01:53:30 +00001071 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton0416bdf2012-01-30 09:04:36 +00001072
1073 ProcessSP process_sp(GetSP());
1074
Greg Claytona66ba462010-10-30 04:51:46 +00001075 if (log)
1076 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001077 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001078 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +00001079 addr,
1080 src,
Greg Clayton851e30e2012-09-18 18:04:04 +00001081 (uint64_t)src_len,
Greg Claytona66ba462010-10-30 04:51:46 +00001082 sb_error.get());
1083 }
1084
Greg Clayton0416bdf2012-01-30 09:04:36 +00001085 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001086 {
Greg Claytona894fe72012-04-05 16:12:35 +00001087 Process::StopLocker stop_locker;
1088 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1089 {
1090 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1091 bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref());
1092 }
1093 else
1094 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001095 if (log)
1096 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001097 sb_error.SetErrorString("process is running");
1098 }
Chris Lattner24943d22010-06-08 16:52:24 +00001099 }
1100
Greg Claytona66ba462010-10-30 04:51:46 +00001101 if (log)
1102 {
1103 SBStream sstr;
1104 sb_error.GetDescription (sstr);
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001105 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
Greg Clayton0416bdf2012-01-30 09:04:36 +00001106 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +00001107 addr,
1108 src,
Greg Clayton851e30e2012-09-18 18:04:04 +00001109 (uint64_t)src_len,
Greg Claytona66ba462010-10-30 04:51:46 +00001110 sb_error.get(),
1111 sstr.GetData(),
Greg Clayton851e30e2012-09-18 18:04:04 +00001112 (uint64_t)bytes_written);
Greg Claytona66ba462010-10-30 04:51:46 +00001113 }
1114
Chris Lattner24943d22010-06-08 16:52:24 +00001115 return bytes_written;
1116}
1117
Caroline Tice98f930f2010-09-20 05:20:02 +00001118bool
1119SBProcess::GetDescription (SBStream &description)
1120{
Greg Clayton96154be2011-11-13 06:57:31 +00001121 Stream &strm = description.ref();
1122
Greg Clayton0416bdf2012-01-30 09:04:36 +00001123 ProcessSP process_sp(GetSP());
1124 if (process_sp)
Caroline Tice98f930f2010-09-20 05:20:02 +00001125 {
1126 char path[PATH_MAX];
1127 GetTarget().GetExecutable().GetPath (path, sizeof(path));
Greg Clayton0416bdf2012-01-30 09:04:36 +00001128 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
Greg Clayton5c4c7462010-10-06 03:09:58 +00001129 const char *exe_name = NULL;
1130 if (exe_module)
1131 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1132
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001133 strm.Printf ("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001134 process_sp->GetID(),
Greg Clayton96154be2011-11-13 06:57:31 +00001135 lldb_private::StateAsCString (GetState()),
1136 GetNumThreads(),
1137 exe_name ? ", executable = " : "",
1138 exe_name ? exe_name : "");
Caroline Tice98f930f2010-09-20 05:20:02 +00001139 }
1140 else
Greg Clayton96154be2011-11-13 06:57:31 +00001141 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +00001142
1143 return true;
1144}
Greg Clayton0baa3942010-11-04 01:54:29 +00001145
1146uint32_t
Johnny Chen191343e2012-05-23 22:34:34 +00001147SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const
1148{
1149 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1150
1151 uint32_t num = 0;
1152 ProcessSP process_sp(GetSP());
1153 if (process_sp)
1154 {
1155 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1156 sb_error.SetError(process_sp->GetWatchpointSupportInfo (num));
Johnny Chen191343e2012-05-23 22:34:34 +00001157 if (log)
1158 log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1159 process_sp.get(), num);
1160 }
1161 else
1162 {
1163 sb_error.SetErrorString ("SBProcess is invalid");
1164 }
1165 return num;
1166}
1167
1168uint32_t
Greg Clayton0baa3942010-11-04 01:54:29 +00001169SBProcess::LoadImage (lldb::SBFileSpec &sb_image_spec, lldb::SBError &sb_error)
1170{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001171 ProcessSP process_sp(GetSP());
1172 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001173 {
Greg Claytona894fe72012-04-05 16:12:35 +00001174 Process::StopLocker stop_locker;
1175 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1176 {
1177 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1178 return process_sp->LoadImage (*sb_image_spec, sb_error.ref());
1179 }
1180 else
1181 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001182 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1183 if (log)
1184 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001185 sb_error.SetErrorString("process is running");
1186 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001187 }
Greg Clayton0baa3942010-11-04 01:54:29 +00001188 return LLDB_INVALID_IMAGE_TOKEN;
1189}
1190
1191lldb::SBError
1192SBProcess::UnloadImage (uint32_t image_token)
1193{
1194 lldb::SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001195 ProcessSP process_sp(GetSP());
1196 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001197 {
Greg Claytona894fe72012-04-05 16:12:35 +00001198 Process::StopLocker stop_locker;
1199 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1200 {
1201 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1202 sb_error.SetError (process_sp->UnloadImage (image_token));
1203 }
1204 else
1205 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001206 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1207 if (log)
1208 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001209 sb_error.SetErrorString("process is running");
1210 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001211 }
Greg Clayton0baa3942010-11-04 01:54:29 +00001212 else
1213 sb_error.SetErrorString("invalid process");
1214 return sb_error;
1215}