blob: dfd799c2eb26e01d25b2d3e600ef2062c59eb914 [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
Greg Clayton52ebc0a2013-01-18 23:41:08 +0000281SBThread
282SBProcess::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context)
283{
284 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
285
286 SBThread sb_thread;
287 ThreadSP thread_sp;
288 ProcessSP process_sp(GetSP());
289 if (process_sp)
290 {
291 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
292 thread_sp = process_sp->CreateOSPluginThread(tid, context);
293 sb_thread.SetThread (thread_sp);
294 }
295
296 if (log)
297 log->Printf ("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64 ", context=0x%" PRIx64 ") => SBThread(%p)", process_sp.get(), tid, context, thread_sp.get());
298
299 return sb_thread;
300}
301
Chris Lattner24943d22010-06-08 16:52:24 +0000302SBTarget
303SBProcess::GetTarget() const
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
Chris Lattner24943d22010-06-08 16:52:24 +0000307 SBTarget sb_target;
Greg Clayton334d33a2012-01-30 07:41:31 +0000308 TargetSP target_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000309 ProcessSP process_sp(GetSP());
310 if (process_sp)
Greg Clayton334d33a2012-01-30 07:41:31 +0000311 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000312 target_sp = process_sp->GetTarget().shared_from_this();
Greg Clayton334d33a2012-01-30 07:41:31 +0000313 sb_target.SetSP (target_sp);
314 }
Caroline Tice7826c882010-10-26 03:11:13 +0000315
316 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000317 log->Printf ("SBProcess(%p)::GetTarget () => SBTarget(%p)", process_sp.get(), target_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000318
Chris Lattner24943d22010-06-08 16:52:24 +0000319 return sb_target;
320}
321
322
323size_t
324SBProcess::PutSTDIN (const char *src, size_t src_len)
325{
Greg Claytone005f2c2010-11-06 01:53:30 +0000326 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000327
Caroline Tice7826c882010-10-26 03:11:13 +0000328 size_t ret_val = 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 ret_val = process_sp->PutSTDIN (src, src_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000334 }
Caroline Tice7826c882010-10-26 03:11:13 +0000335
336 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000337 log->Printf ("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%d) => %lu",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000338 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000339 src,
340 (uint32_t) src_len,
341 ret_val);
Caroline Tice7826c882010-10-26 03:11:13 +0000342
343 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000344}
345
346size_t
347SBProcess::GetSTDOUT (char *dst, size_t dst_len) const
348{
Greg Clayton49ce6822010-10-31 03:01:06 +0000349 size_t bytes_read = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000350 ProcessSP process_sp(GetSP());
351 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000352 {
353 Error error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000354 bytes_read = process_sp->GetSTDOUT (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000355 }
Caroline Tice7826c882010-10-26 03:11:13 +0000356
Greg Claytone005f2c2010-11-06 01:53:30 +0000357 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000358 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000359 log->Printf ("SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
Greg Clayton851e30e2012-09-18 18:04:04 +0000360 process_sp.get(),
361 (int) bytes_read,
362 dst,
363 (uint64_t)dst_len,
364 (uint64_t)bytes_read);
Caroline Tice7826c882010-10-26 03:11:13 +0000365
Greg Clayton49ce6822010-10-31 03:01:06 +0000366 return bytes_read;
Chris Lattner24943d22010-06-08 16:52:24 +0000367}
368
369size_t
370SBProcess::GetSTDERR (char *dst, size_t dst_len) const
371{
Greg Clayton49ce6822010-10-31 03:01:06 +0000372 size_t bytes_read = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000373 ProcessSP process_sp(GetSP());
374 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000375 {
376 Error error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000377 bytes_read = process_sp->GetSTDERR (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
Caroline Tice7826c882010-10-26 03:11:13 +0000379
Greg Claytone005f2c2010-11-06 01:53:30 +0000380 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000381 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000382 log->Printf ("SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
Greg Clayton851e30e2012-09-18 18:04:04 +0000383 process_sp.get(),
384 (int) bytes_read,
385 dst,
386 (uint64_t)dst_len,
387 (uint64_t)bytes_read);
Caroline Tice7826c882010-10-26 03:11:13 +0000388
Greg Clayton49ce6822010-10-31 03:01:06 +0000389 return bytes_read;
Chris Lattner24943d22010-06-08 16:52:24 +0000390}
391
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000392size_t
393SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const
394{
395 size_t bytes_read = 0;
396 ProcessSP process_sp(GetSP());
397 if (process_sp)
398 {
399 Error error;
400 bytes_read = process_sp->GetAsyncProfileData (dst, dst_len, error);
401 }
402
403 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
404 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000405 log->Printf ("SBProcess(%p)::GetProfileData (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000406 process_sp.get(),
407 (int) bytes_read,
408 dst,
409 (uint64_t)dst_len,
410 (uint64_t)bytes_read);
411
412 return bytes_read;
413}
414
Chris Lattner24943d22010-06-08 16:52:24 +0000415void
Jim Inghamc8332952010-08-26 21:32:51 +0000416SBProcess::ReportEventState (const SBEvent &event, FILE *out) const
Chris Lattner24943d22010-06-08 16:52:24 +0000417{
418 if (out == NULL)
419 return;
420
Greg Clayton0416bdf2012-01-30 09:04:36 +0000421 ProcessSP process_sp(GetSP());
422 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000423 {
424 const StateType event_state = SBProcess::GetStateFromEvent (event);
425 char message[1024];
426 int message_len = ::snprintf (message,
427 sizeof (message),
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000428 "Process %" PRIu64 " %s\n",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000429 process_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000430 SBDebugger::StateAsCString (event_state));
431
432 if (message_len > 0)
433 ::fwrite (message, 1, message_len, out);
434 }
435}
436
437void
Jim Inghamc8332952010-08-26 21:32:51 +0000438SBProcess::AppendEventStateReport (const SBEvent &event, SBCommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000439{
Greg Clayton0416bdf2012-01-30 09:04:36 +0000440 ProcessSP process_sp(GetSP());
441 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000442 {
443 const StateType event_state = SBProcess::GetStateFromEvent (event);
444 char message[1024];
445 ::snprintf (message,
446 sizeof (message),
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000447 "Process %" PRIu64 " %s\n",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000448 process_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000449 SBDebugger::StateAsCString (event_state));
450
451 result.AppendMessage (message);
452 }
453}
454
455bool
Jim Inghamc8332952010-08-26 21:32:51 +0000456SBProcess::SetSelectedThread (const SBThread &thread)
Chris Lattner24943d22010-06-08 16:52:24 +0000457{
Greg Clayton0416bdf2012-01-30 09:04:36 +0000458 ProcessSP process_sp(GetSP());
459 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000460 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000461 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
462 return process_sp->GetThreadList().SetSelectedThreadByID (thread.GetThreadID());
Greg Claytonbdcda462010-12-20 20:49:23 +0000463 }
Chris Lattner24943d22010-06-08 16:52:24 +0000464 return false;
465}
466
467bool
Greg Clayton82560f22012-10-12 23:32:11 +0000468SBProcess::SetSelectedThreadByID (lldb::tid_t tid)
469{
Greg Claytone005f2c2010-11-06 01:53:30 +0000470 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000471
Caroline Tice7826c882010-10-26 03:11:13 +0000472 bool ret_val = false;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000473 ProcessSP process_sp(GetSP());
474 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000475 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000476 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
477 ret_val = process_sp->GetThreadList().SetSelectedThreadByID (tid);
Greg Claytonbdcda462010-12-20 20:49:23 +0000478 }
Caroline Tice7826c882010-10-26 03:11:13 +0000479
480 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000481 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64 ") => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000482 process_sp.get(), tid, (ret_val ? "true" : "false"));
Caroline Tice7826c882010-10-26 03:11:13 +0000483
484 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000485}
486
Jim Inghamefbdd222012-07-13 20:18:18 +0000487bool
488SBProcess::SetSelectedThreadByIndexID (uint32_t index_id)
489{
490 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
491
492 bool ret_val = false;
493 ProcessSP process_sp(GetSP());
494 if (process_sp)
495 {
496 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
497 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID (index_id);
498 }
499
500 if (log)
501 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
502 process_sp.get(), index_id, (ret_val ? "true" : "false"));
503
504 return ret_val;
505}
506
Chris Lattner24943d22010-06-08 16:52:24 +0000507SBThread
508SBProcess::GetThreadAtIndex (size_t index)
509{
Greg Claytone005f2c2010-11-06 01:53:30 +0000510 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000511
Greg Clayton90c52142012-01-30 02:53:15 +0000512 SBThread sb_thread;
513 ThreadSP thread_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000514 ProcessSP process_sp(GetSP());
515 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000516 {
Greg Claytona894fe72012-04-05 16:12:35 +0000517 Process::StopLocker stop_locker;
518 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
Greg Clayton0416bdf2012-01-30 09:04:36 +0000519 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytona894fe72012-04-05 16:12:35 +0000520 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
Greg Clayton90c52142012-01-30 02:53:15 +0000521 sb_thread.SetThread (thread_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000522 }
Caroline Tice7826c882010-10-26 03:11:13 +0000523
524 if (log)
525 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000526 log->Printf ("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000527 process_sp.get(), (uint32_t) index, thread_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000528 }
529
Greg Clayton90c52142012-01-30 02:53:15 +0000530 return sb_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000531}
532
Jim Ingham0e3b98e2013-01-08 23:22:42 +0000533uint32_t
534SBProcess::GetStopID(bool include_expression_stops)
535{
536 ProcessSP process_sp(GetSP());
537 if (process_sp)
538 {
539 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
540 if (include_expression_stops)
541 return process_sp->GetStopID();
542 else
543 return process_sp->GetLastNaturalStopID();
544 }
545 return 0;
546}
547
Chris Lattner24943d22010-06-08 16:52:24 +0000548StateType
549SBProcess::GetState ()
550{
Caroline Tice7826c882010-10-26 03:11:13 +0000551
Caroline Tice7826c882010-10-26 03:11:13 +0000552 StateType ret_val = eStateInvalid;
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 ret_val = process_sp->GetState();
Greg Claytonbdcda462010-12-20 20:49:23 +0000558 }
Caroline Tice7826c882010-10-26 03:11:13 +0000559
Greg Claytone005f2c2010-11-06 01:53:30 +0000560 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000561 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000562 log->Printf ("SBProcess(%p)::GetState () => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000563 process_sp.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000564 lldb_private::StateAsCString (ret_val));
Caroline Tice7826c882010-10-26 03:11:13 +0000565
566 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000567}
568
569
570int
571SBProcess::GetExitStatus ()
572{
Greg Claytona66ba462010-10-30 04:51:46 +0000573 int exit_status = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000574 ProcessSP process_sp(GetSP());
575 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000576 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000577 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
578 exit_status = process_sp->GetExitStatus ();
Greg Claytonbdcda462010-12-20 20:49:23 +0000579 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000580 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000581 if (log)
582 log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000583 process_sp.get(), exit_status, exit_status);
Greg Claytona66ba462010-10-30 04:51:46 +0000584
585 return exit_status;
Chris Lattner24943d22010-06-08 16:52:24 +0000586}
587
588const char *
589SBProcess::GetExitDescription ()
590{
Greg Claytona66ba462010-10-30 04:51:46 +0000591 const char *exit_desc = NULL;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000592 ProcessSP process_sp(GetSP());
593 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000594 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000595 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
596 exit_desc = process_sp->GetExitDescription ();
Greg Claytonbdcda462010-12-20 20:49:23 +0000597 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000598 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000599 if (log)
600 log->Printf ("SBProcess(%p)::GetExitDescription () => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000601 process_sp.get(), exit_desc);
Greg Claytona66ba462010-10-30 04:51:46 +0000602 return exit_desc;
Chris Lattner24943d22010-06-08 16:52:24 +0000603}
604
605lldb::pid_t
606SBProcess::GetProcessID ()
607{
Caroline Tice7826c882010-10-26 03:11:13 +0000608 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000609 ProcessSP process_sp(GetSP());
610 if (process_sp)
611 ret_val = process_sp->GetID();
Caroline Tice7826c882010-10-26 03:11:13 +0000612
Greg Claytone005f2c2010-11-06 01:53:30 +0000613 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000614 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000615 log->Printf ("SBProcess(%p)::GetProcessID () => %" PRIu64, process_sp.get(), ret_val);
Caroline Tice7826c882010-10-26 03:11:13 +0000616
617 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000618}
619
Greg Clayton64742742013-01-16 17:29:04 +0000620uint32_t
621SBProcess::GetUniqueID()
622{
623 uint32_t ret_val = 0;
624 ProcessSP process_sp(GetSP());
625 if (process_sp)
626 ret_val = process_sp->GetUniqueID();
627 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
628 if (log)
629 log->Printf ("SBProcess(%p)::GetUniqueID () => %" PRIu32, process_sp.get(), ret_val);
630 return ret_val;
631}
632
Johnny Chen60a544f2011-03-01 22:56:31 +0000633ByteOrder
634SBProcess::GetByteOrder () const
635{
636 ByteOrder byteOrder = eByteOrderInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000637 ProcessSP process_sp(GetSP());
638 if (process_sp)
639 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
Johnny Chen60a544f2011-03-01 22:56:31 +0000640
641 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
642 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000643 log->Printf ("SBProcess(%p)::GetByteOrder () => %d", process_sp.get(), byteOrder);
Johnny Chen60a544f2011-03-01 22:56:31 +0000644
645 return byteOrder;
646}
647
Chris Lattner24943d22010-06-08 16:52:24 +0000648uint32_t
649SBProcess::GetAddressByteSize () const
650{
Caroline Tice7826c882010-10-26 03:11:13 +0000651 uint32_t size = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000652 ProcessSP process_sp(GetSP());
653 if (process_sp)
654 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
Caroline Tice7826c882010-10-26 03:11:13 +0000655
Greg Claytone005f2c2010-11-06 01:53:30 +0000656 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000657 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000658 log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d", process_sp.get(), size);
Caroline Tice7826c882010-10-26 03:11:13 +0000659
660 return size;
Chris Lattner24943d22010-06-08 16:52:24 +0000661}
662
Chris Lattner24943d22010-06-08 16:52:24 +0000663SBError
664SBProcess::Continue ()
665{
Greg Claytone005f2c2010-11-06 01:53:30 +0000666 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000667
Chris Lattner24943d22010-06-08 16:52:24 +0000668 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000669 ProcessSP process_sp(GetSP());
670
671 if (log)
672 log->Printf ("SBProcess(%p)::Continue ()...", process_sp.get());
673
674 if (process_sp)
Greg Clayton1a3083a2010-10-06 03:53:16 +0000675 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000676 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytonde1dd812011-06-24 03:21:43 +0000677
Greg Clayton0416bdf2012-01-30 09:04:36 +0000678 Error error (process_sp->Resume());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000679 if (error.Success())
680 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000681 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution () == false)
Greg Claytona66ba462010-10-30 04:51:46 +0000682 {
683 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000684 log->Printf ("SBProcess(%p)::Continue () waiting for process to stop...", process_sp.get());
685 process_sp->WaitForProcessToStop (NULL);
Greg Claytona66ba462010-10-30 04:51:46 +0000686 }
Greg Clayton1a3083a2010-10-06 03:53:16 +0000687 }
688 sb_error.SetError(error);
689 }
Chris Lattner24943d22010-06-08 16:52:24 +0000690 else
691 sb_error.SetErrorString ("SBProcess is invalid");
692
Caroline Tice7826c882010-10-26 03:11:13 +0000693 if (log)
694 {
695 SBStream sstr;
696 sb_error.GetDescription (sstr);
Greg Clayton0416bdf2012-01-30 09:04:36 +0000697 log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s", process_sp.get(), sb_error.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000698 }
699
Chris Lattner24943d22010-06-08 16:52:24 +0000700 return sb_error;
701}
702
703
704SBError
705SBProcess::Destroy ()
706{
707 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000708 ProcessSP process_sp(GetSP());
709 if (process_sp)
Greg Clayton72e1c782011-01-22 23:43:18 +0000710 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000711 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
712 sb_error.SetError(process_sp->Destroy());
Greg Clayton72e1c782011-01-22 23:43:18 +0000713 }
Chris Lattner24943d22010-06-08 16:52:24 +0000714 else
715 sb_error.SetErrorString ("SBProcess is invalid");
716
Greg Claytone005f2c2010-11-06 01:53:30 +0000717 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000718 if (log)
719 {
720 SBStream sstr;
721 sb_error.GetDescription (sstr);
Greg Clayton72e1c782011-01-22 23:43:18 +0000722 log->Printf ("SBProcess(%p)::Destroy () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000723 process_sp.get(),
Greg Clayton72e1c782011-01-22 23:43:18 +0000724 sb_error.get(),
725 sstr.GetData());
Greg Claytona66ba462010-10-30 04:51:46 +0000726 }
727
Chris Lattner24943d22010-06-08 16:52:24 +0000728 return sb_error;
729}
730
731
732SBError
733SBProcess::Stop ()
734{
735 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000736 ProcessSP process_sp(GetSP());
737 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000738 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000739 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
740 sb_error.SetError (process_sp->Halt());
Greg Claytonbdcda462010-12-20 20:49:23 +0000741 }
Chris Lattner24943d22010-06-08 16:52:24 +0000742 else
743 sb_error.SetErrorString ("SBProcess is invalid");
Caroline Tice7826c882010-10-26 03:11:13 +0000744
Greg Claytone005f2c2010-11-06 01:53:30 +0000745 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000746 if (log)
747 {
748 SBStream sstr;
749 sb_error.GetDescription (sstr);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000750 log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000751 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000752 sb_error.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000753 sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000754 }
755
Chris Lattner24943d22010-06-08 16:52:24 +0000756 return sb_error;
757}
758
759SBError
760SBProcess::Kill ()
761{
762 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000763 ProcessSP process_sp(GetSP());
764 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000765 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000766 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
767 sb_error.SetError (process_sp->Destroy());
Greg Claytonbdcda462010-12-20 20:49:23 +0000768 }
Chris Lattner24943d22010-06-08 16:52:24 +0000769 else
770 sb_error.SetErrorString ("SBProcess is invalid");
Caroline Tice7826c882010-10-26 03:11:13 +0000771
Greg Claytone005f2c2010-11-06 01:53:30 +0000772 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000773 if (log)
774 {
775 SBStream sstr;
776 sb_error.GetDescription (sstr);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000777 log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000778 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000779 sb_error.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000780 sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000781 }
782
Chris Lattner24943d22010-06-08 16:52:24 +0000783 return sb_error;
784}
785
Chris Lattner24943d22010-06-08 16:52:24 +0000786SBError
787SBProcess::Detach ()
788{
789 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000790 ProcessSP process_sp(GetSP());
791 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000792 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000793 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
794 sb_error.SetError (process_sp->Detach());
Greg Claytonbdcda462010-12-20 20:49:23 +0000795 }
Chris Lattner24943d22010-06-08 16:52:24 +0000796 else
797 sb_error.SetErrorString ("SBProcess is invalid");
798
799 return sb_error;
800}
801
802SBError
Greg Claytona66ba462010-10-30 04:51:46 +0000803SBProcess::Signal (int signo)
Chris Lattner24943d22010-06-08 16:52:24 +0000804{
805 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000806 ProcessSP process_sp(GetSP());
807 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000808 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000809 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
810 sb_error.SetError (process_sp->Signal (signo));
Greg Claytonbdcda462010-12-20 20:49:23 +0000811 }
Chris Lattner24943d22010-06-08 16:52:24 +0000812 else
813 sb_error.SetErrorString ("SBProcess is invalid");
Greg Claytone005f2c2010-11-06 01:53:30 +0000814 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000815 if (log)
816 {
817 SBStream sstr;
818 sb_error.GetDescription (sstr);
819 log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000820 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000821 signo,
822 sb_error.get(),
823 sstr.GetData());
824 }
Chris Lattner24943d22010-06-08 16:52:24 +0000825 return sb_error;
826}
827
Jim Ingham5d90ade2012-07-27 23:57:19 +0000828void
829SBProcess::SendAsyncInterrupt ()
830{
831 ProcessSP process_sp(GetSP());
832 if (process_sp)
833 {
834 process_sp->SendAsyncInterrupt ();
835 }
836}
837
Chris Lattner24943d22010-06-08 16:52:24 +0000838SBThread
Greg Claytona66ba462010-10-30 04:51:46 +0000839SBProcess::GetThreadByID (tid_t tid)
Chris Lattner24943d22010-06-08 16:52:24 +0000840{
Greg Claytona66ba462010-10-30 04:51:46 +0000841 SBThread sb_thread;
Greg Clayton90c52142012-01-30 02:53:15 +0000842 ThreadSP thread_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000843 ProcessSP process_sp(GetSP());
844 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000845 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000846 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytona894fe72012-04-05 16:12:35 +0000847 Process::StopLocker stop_locker;
848 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
849 thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update);
Greg Clayton90c52142012-01-30 02:53:15 +0000850 sb_thread.SetThread (thread_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000851 }
Greg Claytona66ba462010-10-30 04:51:46 +0000852
Greg Claytone005f2c2010-11-06 01:53:30 +0000853 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000854 if (log)
855 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000856 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64 ") => SBThread (%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000857 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000858 tid,
Greg Clayton90c52142012-01-30 02:53:15 +0000859 thread_sp.get());
Greg Claytona66ba462010-10-30 04:51:46 +0000860 }
861
862 return sb_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000863}
864
Jim Inghamefbdd222012-07-13 20:18:18 +0000865SBThread
866SBProcess::GetThreadByIndexID (uint32_t index_id)
867{
868 SBThread sb_thread;
869 ThreadSP thread_sp;
870 ProcessSP process_sp(GetSP());
871 if (process_sp)
872 {
873 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
874 Process::StopLocker stop_locker;
875 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
876 thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update);
877 sb_thread.SetThread (thread_sp);
878 }
879
880 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
881 if (log)
882 {
883 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
884 process_sp.get(),
885 index_id,
886 thread_sp.get());
887 }
888
889 return sb_thread;
890}
891
Chris Lattner24943d22010-06-08 16:52:24 +0000892StateType
893SBProcess::GetStateFromEvent (const SBEvent &event)
894{
Greg Claytone005f2c2010-11-06 01:53:30 +0000895 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000896
Caroline Tice7826c882010-10-26 03:11:13 +0000897 StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get());
898
899 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000900 log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s", event.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000901 lldb_private::StateAsCString (ret_val));
Caroline Tice7826c882010-10-26 03:11:13 +0000902
903 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000904}
905
Chris Lattner24943d22010-06-08 16:52:24 +0000906bool
907SBProcess::GetRestartedFromEvent (const SBEvent &event)
908{
Greg Clayton63094e02010-06-23 01:19:29 +0000909 return Process::ProcessEventData::GetRestartedFromEvent (event.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000910}
911
912SBProcess
913SBProcess::GetProcessFromEvent (const SBEvent &event)
914{
Greg Clayton63094e02010-06-23 01:19:29 +0000915 SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
Chris Lattner24943d22010-06-08 16:52:24 +0000916 return process;
917}
918
Jim Ingham28e23862012-02-08 05:23:15 +0000919bool
920SBProcess::EventIsProcessEvent (const SBEvent &event)
921{
Jim Ingham5a15e692012-02-16 06:50:00 +0000922 return strcmp (event.GetBroadcasterClass(), SBProcess::GetBroadcasterClass()) == 0;
Jim Ingham28e23862012-02-08 05:23:15 +0000923}
Chris Lattner24943d22010-06-08 16:52:24 +0000924
925SBBroadcaster
926SBProcess::GetBroadcaster () const
927{
Greg Claytone005f2c2010-11-06 01:53:30 +0000928 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000929
Greg Clayton0416bdf2012-01-30 09:04:36 +0000930 ProcessSP process_sp(GetSP());
931
932 SBBroadcaster broadcaster(process_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +0000933
934 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000935 log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)", process_sp.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000936 broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000937
Chris Lattner24943d22010-06-08 16:52:24 +0000938 return broadcaster;
939}
940
Jim Ingham5a15e692012-02-16 06:50:00 +0000941const char *
942SBProcess::GetBroadcasterClass ()
943{
944 return Process::GetStaticBroadcasterClass().AsCString();
945}
946
Chris Lattner24943d22010-06-08 16:52:24 +0000947size_t
948SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
949{
Greg Claytone005f2c2010-11-06 01:53:30 +0000950 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000951
Chris Lattner24943d22010-06-08 16:52:24 +0000952 size_t bytes_read = 0;
953
Greg Clayton0416bdf2012-01-30 09:04:36 +0000954 ProcessSP process_sp(GetSP());
955
Greg Claytona66ba462010-10-30 04:51:46 +0000956 if (log)
957 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000958 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000959 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000960 addr,
961 dst,
Greg Clayton851e30e2012-09-18 18:04:04 +0000962 (uint64_t)dst_len,
Greg Claytona66ba462010-10-30 04:51:46 +0000963 sb_error.get());
964 }
Greg Clayton0416bdf2012-01-30 09:04:36 +0000965
966 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000967 {
Greg Claytona894fe72012-04-05 16:12:35 +0000968 Process::StopLocker stop_locker;
969 if (stop_locker.TryLock(&process_sp->GetRunLock()))
970 {
971 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
972 bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref());
973 }
974 else
975 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000976 if (log)
977 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +0000978 sb_error.SetErrorString("process is running");
979 }
Chris Lattner24943d22010-06-08 16:52:24 +0000980 }
981 else
982 {
983 sb_error.SetErrorString ("SBProcess is invalid");
984 }
985
Caroline Tice7826c882010-10-26 03:11:13 +0000986 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000987 {
988 SBStream sstr;
989 sb_error.GetDescription (sstr);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000990 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
Greg Clayton0416bdf2012-01-30 09:04:36 +0000991 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000992 addr,
993 dst,
Greg Clayton851e30e2012-09-18 18:04:04 +0000994 (uint64_t)dst_len,
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000995 sb_error.get(),
996 sstr.GetData(),
Greg Clayton851e30e2012-09-18 18:04:04 +0000997 (uint64_t)bytes_read);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000998 }
Caroline Tice7826c882010-10-26 03:11:13 +0000999
Chris Lattner24943d22010-06-08 16:52:24 +00001000 return bytes_read;
1001}
1002
1003size_t
Greg Clayton4a2e3372011-12-15 03:14:23 +00001004SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error)
1005{
1006 size_t bytes_read = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001007 ProcessSP process_sp(GetSP());
1008 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +00001009 {
Greg Claytona894fe72012-04-05 16:12:35 +00001010 Process::StopLocker stop_locker;
1011 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1012 {
1013 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1014 bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref());
1015 }
1016 else
1017 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001018 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1019 if (log)
1020 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001021 sb_error.SetErrorString("process is running");
1022 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001023 }
1024 else
1025 {
1026 sb_error.SetErrorString ("SBProcess is invalid");
1027 }
1028 return bytes_read;
1029}
1030
1031uint64_t
1032SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error)
1033{
Greg Claytona894fe72012-04-05 16:12:35 +00001034 uint64_t value = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001035 ProcessSP process_sp(GetSP());
1036 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +00001037 {
Greg Claytona894fe72012-04-05 16:12:35 +00001038 Process::StopLocker stop_locker;
1039 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1040 {
1041 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1042 value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref());
1043 }
1044 else
1045 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001046 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1047 if (log)
1048 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001049 sb_error.SetErrorString("process is running");
1050 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001051 }
1052 else
1053 {
1054 sb_error.SetErrorString ("SBProcess is invalid");
1055 }
Greg Claytona894fe72012-04-05 16:12:35 +00001056 return value;
Greg Clayton4a2e3372011-12-15 03:14:23 +00001057}
1058
1059lldb::addr_t
1060SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error)
1061{
1062 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001063 ProcessSP process_sp(GetSP());
1064 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +00001065 {
Greg Claytona894fe72012-04-05 16:12:35 +00001066 Process::StopLocker stop_locker;
1067 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1068 {
1069 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1070 ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref());
1071 }
1072 else
1073 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001074 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1075 if (log)
1076 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001077 sb_error.SetErrorString("process is running");
1078 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001079 }
1080 else
1081 {
1082 sb_error.SetErrorString ("SBProcess is invalid");
1083 }
1084 return ptr;
1085}
1086
1087size_t
Chris Lattner24943d22010-06-08 16:52:24 +00001088SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
1089{
1090 size_t bytes_written = 0;
1091
Greg Claytone005f2c2010-11-06 01:53:30 +00001092 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton0416bdf2012-01-30 09:04:36 +00001093
1094 ProcessSP process_sp(GetSP());
1095
Greg Claytona66ba462010-10-30 04:51:46 +00001096 if (log)
1097 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001098 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001099 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +00001100 addr,
1101 src,
Greg Clayton851e30e2012-09-18 18:04:04 +00001102 (uint64_t)src_len,
Greg Claytona66ba462010-10-30 04:51:46 +00001103 sb_error.get());
1104 }
1105
Greg Clayton0416bdf2012-01-30 09:04:36 +00001106 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001107 {
Greg Claytona894fe72012-04-05 16:12:35 +00001108 Process::StopLocker stop_locker;
1109 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1110 {
1111 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1112 bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref());
1113 }
1114 else
1115 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001116 if (log)
1117 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001118 sb_error.SetErrorString("process is running");
1119 }
Chris Lattner24943d22010-06-08 16:52:24 +00001120 }
1121
Greg Claytona66ba462010-10-30 04:51:46 +00001122 if (log)
1123 {
1124 SBStream sstr;
1125 sb_error.GetDescription (sstr);
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001126 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
Greg Clayton0416bdf2012-01-30 09:04:36 +00001127 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +00001128 addr,
1129 src,
Greg Clayton851e30e2012-09-18 18:04:04 +00001130 (uint64_t)src_len,
Greg Claytona66ba462010-10-30 04:51:46 +00001131 sb_error.get(),
1132 sstr.GetData(),
Greg Clayton851e30e2012-09-18 18:04:04 +00001133 (uint64_t)bytes_written);
Greg Claytona66ba462010-10-30 04:51:46 +00001134 }
1135
Chris Lattner24943d22010-06-08 16:52:24 +00001136 return bytes_written;
1137}
1138
Caroline Tice98f930f2010-09-20 05:20:02 +00001139bool
1140SBProcess::GetDescription (SBStream &description)
1141{
Greg Clayton96154be2011-11-13 06:57:31 +00001142 Stream &strm = description.ref();
1143
Greg Clayton0416bdf2012-01-30 09:04:36 +00001144 ProcessSP process_sp(GetSP());
1145 if (process_sp)
Caroline Tice98f930f2010-09-20 05:20:02 +00001146 {
1147 char path[PATH_MAX];
1148 GetTarget().GetExecutable().GetPath (path, sizeof(path));
Greg Clayton0416bdf2012-01-30 09:04:36 +00001149 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
Greg Clayton5c4c7462010-10-06 03:09:58 +00001150 const char *exe_name = NULL;
1151 if (exe_module)
1152 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1153
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001154 strm.Printf ("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001155 process_sp->GetID(),
Greg Clayton96154be2011-11-13 06:57:31 +00001156 lldb_private::StateAsCString (GetState()),
1157 GetNumThreads(),
1158 exe_name ? ", executable = " : "",
1159 exe_name ? exe_name : "");
Caroline Tice98f930f2010-09-20 05:20:02 +00001160 }
1161 else
Greg Clayton96154be2011-11-13 06:57:31 +00001162 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +00001163
1164 return true;
1165}
Greg Clayton0baa3942010-11-04 01:54:29 +00001166
1167uint32_t
Johnny Chen191343e2012-05-23 22:34:34 +00001168SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const
1169{
1170 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1171
1172 uint32_t num = 0;
1173 ProcessSP process_sp(GetSP());
1174 if (process_sp)
1175 {
1176 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1177 sb_error.SetError(process_sp->GetWatchpointSupportInfo (num));
Johnny Chen191343e2012-05-23 22:34:34 +00001178 if (log)
1179 log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1180 process_sp.get(), num);
1181 }
1182 else
1183 {
1184 sb_error.SetErrorString ("SBProcess is invalid");
1185 }
1186 return num;
1187}
1188
1189uint32_t
Greg Clayton0baa3942010-11-04 01:54:29 +00001190SBProcess::LoadImage (lldb::SBFileSpec &sb_image_spec, lldb::SBError &sb_error)
1191{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001192 ProcessSP process_sp(GetSP());
1193 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001194 {
Greg Claytona894fe72012-04-05 16:12:35 +00001195 Process::StopLocker stop_locker;
1196 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1197 {
1198 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1199 return process_sp->LoadImage (*sb_image_spec, sb_error.ref());
1200 }
1201 else
1202 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001203 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1204 if (log)
1205 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001206 sb_error.SetErrorString("process is running");
1207 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001208 }
Greg Clayton0baa3942010-11-04 01:54:29 +00001209 return LLDB_INVALID_IMAGE_TOKEN;
1210}
1211
1212lldb::SBError
1213SBProcess::UnloadImage (uint32_t image_token)
1214{
1215 lldb::SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001216 ProcessSP process_sp(GetSP());
1217 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001218 {
Greg Claytona894fe72012-04-05 16:12:35 +00001219 Process::StopLocker stop_locker;
1220 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1221 {
1222 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1223 sb_error.SetError (process_sp->UnloadImage (image_token));
1224 }
1225 else
1226 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001227 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1228 if (log)
1229 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001230 sb_error.SetErrorString("process is running");
1231 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001232 }
Greg Clayton0baa3942010-11-04 01:54:29 +00001233 else
1234 sb_error.SetErrorString("invalid process");
1235 return sb_error;
1236}