blob: 9b1dff035609921c7b579b9234ed92b46d05d849 [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
Eli Friedman7a62c8b2010-06-09 07:44:37 +000010#include "lldb/API/SBProcess.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
12#include "lldb/lldb-defines.h"
13#include "lldb/lldb-types.h"
14
Jim Ingham84cdc152010-06-15 19:49:27 +000015#include "lldb/Interpreter/Args.h"
Greg Clayton1a3083a2010-10-06 03:53:16 +000016#include "lldb/Core/Debugger.h"
Caroline Tice7826c882010-10-26 03:11:13 +000017#include "lldb/Core/Log.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000018#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/State.h"
20#include "lldb/Core/Stream.h"
21#include "lldb/Core/StreamFile.h"
22#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Target/RegisterContext.h"
Greg Clayton63094e02010-06-23 01:19:29 +000024#include "lldb/Target/Target.h"
25#include "lldb/Target/Thread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27// Project includes
28
Eli Friedman7a62c8b2010-06-09 07:44:37 +000029#include "lldb/API/SBBroadcaster.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000030#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton0a8dcac2012-02-24 05:03:03 +000031#include "lldb/API/SBDebugger.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000032#include "lldb/API/SBEvent.h"
Greg Clayton0a8dcac2012-02-24 05:03:03 +000033#include "lldb/API/SBFileSpec.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000034#include "lldb/API/SBThread.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000035#include "lldb/API/SBStream.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000036#include "lldb/API/SBStringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
41
Chris Lattner24943d22010-06-08 16:52:24 +000042SBProcess::SBProcess () :
Greg Claytonbcaf99a2012-07-12 20:32:19 +000043 m_opaque_wp()
Chris Lattner24943d22010-06-08 16:52:24 +000044{
45}
46
47
48//----------------------------------------------------------------------
49// SBProcess constructor
50//----------------------------------------------------------------------
51
52SBProcess::SBProcess (const SBProcess& rhs) :
Greg Claytonbcaf99a2012-07-12 20:32:19 +000053 m_opaque_wp (rhs.m_opaque_wp)
Chris Lattner24943d22010-06-08 16:52:24 +000054{
55}
56
57
58SBProcess::SBProcess (const lldb::ProcessSP &process_sp) :
Greg Claytonbcaf99a2012-07-12 20:32:19 +000059 m_opaque_wp (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000060{
61}
62
Greg Clayton538eb822010-11-05 23:17:00 +000063const SBProcess&
64SBProcess::operator = (const SBProcess& rhs)
65{
66 if (this != &rhs)
Greg Claytonbcaf99a2012-07-12 20:32:19 +000067 m_opaque_wp = rhs.m_opaque_wp;
Greg Clayton538eb822010-11-05 23:17:00 +000068 return *this;
69}
70
Chris Lattner24943d22010-06-08 16:52:24 +000071//----------------------------------------------------------------------
72// Destructor
73//----------------------------------------------------------------------
74SBProcess::~SBProcess()
75{
76}
77
Jim Ingham5a15e692012-02-16 06:50:00 +000078const char *
79SBProcess::GetBroadcasterClassName ()
80{
81 return Process::GetStaticBroadcasterClass().AsCString();
82}
83
Jim Inghamfee26ee2012-10-26 19:18:04 +000084const char *
85SBProcess::GetPluginName ()
86{
87 ProcessSP process_sp(GetSP());
88 if (process_sp)
89 {
90 return process_sp->GetPluginName();
91 }
92 return "<Unknown>";
93}
94
95const char *
96SBProcess::GetShortPluginName ()
97{
98 ProcessSP process_sp(GetSP());
99 if (process_sp)
100 {
101 return process_sp->GetShortPluginName();
102 }
103 return "<Unknown>";
104}
105
106
Greg Clayton334d33a2012-01-30 07:41:31 +0000107lldb::ProcessSP
108SBProcess::GetSP() const
109{
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000110 return m_opaque_wp.lock();
Greg Clayton334d33a2012-01-30 07:41:31 +0000111}
112
Chris Lattner24943d22010-06-08 16:52:24 +0000113void
Greg Clayton334d33a2012-01-30 07:41:31 +0000114SBProcess::SetSP (const ProcessSP &process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000115{
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000116 m_opaque_wp = process_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000117}
118
119void
120SBProcess::Clear ()
121{
Greg Claytonbcaf99a2012-07-12 20:32:19 +0000122 m_opaque_wp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000123}
124
125
126bool
127SBProcess::IsValid() const
128{
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000129 ProcessSP process_sp(m_opaque_wp.lock());
130 return ((bool) process_sp && process_sp->IsValid());
Chris Lattner24943d22010-06-08 16:52:24 +0000131}
132
James McIlree38093402011-03-04 00:31:13 +0000133bool
134SBProcess::RemoteLaunch (char const **argv,
135 char const **envp,
136 const char *stdin_path,
137 const char *stdout_path,
138 const char *stderr_path,
139 const char *working_directory,
140 uint32_t launch_flags,
141 bool stop_at_entry,
142 lldb::SBError& error)
143{
144 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
145 if (log) {
146 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 +0000147 m_opaque_wp.lock().get(),
James McIlree38093402011-03-04 00:31:13 +0000148 argv,
149 envp,
150 stdin_path ? stdin_path : "NULL",
151 stdout_path ? stdout_path : "NULL",
152 stderr_path ? stderr_path : "NULL",
153 working_directory ? working_directory : "NULL",
154 launch_flags,
155 stop_at_entry,
156 error.get());
157 }
158
Greg Clayton0416bdf2012-01-30 09:04:36 +0000159 ProcessSP process_sp(GetSP());
160 if (process_sp)
James McIlree38093402011-03-04 00:31:13 +0000161 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000162 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
163 if (process_sp->GetState() == eStateConnected)
James McIlree38093402011-03-04 00:31:13 +0000164 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000165 if (stop_at_entry)
166 launch_flags |= eLaunchFlagStopAtEntry;
167 ProcessLaunchInfo launch_info (stdin_path,
168 stdout_path,
169 stderr_path,
170 working_directory,
171 launch_flags);
172 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
173 if (exe_module)
174 launch_info.SetExecutableFile(exe_module->GetFileSpec(), true);
175 if (argv)
176 launch_info.GetArguments().AppendArguments (argv);
177 if (envp)
178 launch_info.GetEnvironmentEntries ().SetArguments (envp);
179 error.SetError (process_sp->Launch (launch_info));
James McIlree38093402011-03-04 00:31:13 +0000180 }
181 else
182 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000183 error.SetErrorString ("must be in eStateConnected to call RemoteLaunch");
James McIlree38093402011-03-04 00:31:13 +0000184 }
185 }
186 else
187 {
188 error.SetErrorString ("unable to attach pid");
189 }
190
191 if (log) {
192 SBStream sstr;
193 error.GetDescription (sstr);
Greg Clayton0416bdf2012-01-30 09:04:36 +0000194 log->Printf ("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s", process_sp.get(), error.get(), sstr.GetData());
James McIlree38093402011-03-04 00:31:13 +0000195 }
196
197 return error.Success();
198}
199
200bool
201SBProcess::RemoteAttachToProcessWithID (lldb::pid_t pid, lldb::SBError& error)
202{
Greg Clayton0416bdf2012-01-30 09:04:36 +0000203 ProcessSP process_sp(GetSP());
204 if (process_sp)
James McIlree38093402011-03-04 00:31:13 +0000205 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000206 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
207 if (process_sp->GetState() == eStateConnected)
James McIlree38093402011-03-04 00:31:13 +0000208 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000209 ProcessAttachInfo attach_info;
210 attach_info.SetProcessID (pid);
211 error.SetError (process_sp->Attach (attach_info));
James McIlree38093402011-03-04 00:31:13 +0000212 }
213 else
214 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000215 error.SetErrorString ("must be in eStateConnected to call RemoteAttachToProcessWithID");
James McIlree38093402011-03-04 00:31:13 +0000216 }
217 }
218 else
219 {
220 error.SetErrorString ("unable to attach pid");
221 }
222
223 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
224 if (log) {
225 SBStream sstr;
226 error.GetDescription (sstr);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000227 log->Printf ("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64 ") => SBError (%p): %s", process_sp.get(), pid, error.get(), sstr.GetData());
James McIlree38093402011-03-04 00:31:13 +0000228 }
229
230 return error.Success();
231}
232
Chris Lattner24943d22010-06-08 16:52:24 +0000233
234uint32_t
235SBProcess::GetNumThreads ()
236{
Greg Claytone005f2c2010-11-06 01:53:30 +0000237 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000238
Caroline Tice7826c882010-10-26 03:11:13 +0000239 uint32_t num_threads = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000240 ProcessSP process_sp(GetSP());
241 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000242 {
Greg Claytona894fe72012-04-05 16:12:35 +0000243 Process::StopLocker stop_locker;
244
245 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
Greg Clayton0416bdf2012-01-30 09:04:36 +0000246 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Clayton0416bdf2012-01-30 09:04:36 +0000247 num_threads = process_sp->GetThreadList().GetSize(can_update);
Chris Lattner24943d22010-06-08 16:52:24 +0000248 }
Caroline Tice7826c882010-10-26 03:11:13 +0000249
250 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000251 log->Printf ("SBProcess(%p)::GetNumThreads () => %d", process_sp.get(), num_threads);
Caroline Tice7826c882010-10-26 03:11:13 +0000252
253 return num_threads;
Chris Lattner24943d22010-06-08 16:52:24 +0000254}
255
256SBThread
Jim Inghamc8332952010-08-26 21:32:51 +0000257SBProcess::GetSelectedThread () const
Chris Lattner24943d22010-06-08 16:52:24 +0000258{
Greg Claytone005f2c2010-11-06 01:53:30 +0000259 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000260
Chris Lattner24943d22010-06-08 16:52:24 +0000261 SBThread sb_thread;
Greg Clayton90c52142012-01-30 02:53:15 +0000262 ThreadSP thread_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000263 ProcessSP process_sp(GetSP());
264 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000265 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000266 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
267 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Greg Clayton90c52142012-01-30 02:53:15 +0000268 sb_thread.SetThread (thread_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000269 }
Caroline Tice7826c882010-10-26 03:11:13 +0000270
271 if (log)
272 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000273 log->Printf ("SBProcess(%p)::GetSelectedThread () => SBThread(%p)", process_sp.get(), thread_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000274 }
275
Chris Lattner24943d22010-06-08 16:52:24 +0000276 return sb_thread;
277}
278
279SBTarget
280SBProcess::GetTarget() const
281{
Greg Claytone005f2c2010-11-06 01:53:30 +0000282 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000283
Chris Lattner24943d22010-06-08 16:52:24 +0000284 SBTarget sb_target;
Greg Clayton334d33a2012-01-30 07:41:31 +0000285 TargetSP target_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000286 ProcessSP process_sp(GetSP());
287 if (process_sp)
Greg Clayton334d33a2012-01-30 07:41:31 +0000288 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000289 target_sp = process_sp->GetTarget().shared_from_this();
Greg Clayton334d33a2012-01-30 07:41:31 +0000290 sb_target.SetSP (target_sp);
291 }
Caroline Tice7826c882010-10-26 03:11:13 +0000292
293 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000294 log->Printf ("SBProcess(%p)::GetTarget () => SBTarget(%p)", process_sp.get(), target_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000295
Chris Lattner24943d22010-06-08 16:52:24 +0000296 return sb_target;
297}
298
299
300size_t
301SBProcess::PutSTDIN (const char *src, size_t src_len)
302{
Greg Claytone005f2c2010-11-06 01:53:30 +0000303 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000304
Caroline Tice7826c882010-10-26 03:11:13 +0000305 size_t ret_val = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000306 ProcessSP process_sp(GetSP());
307 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000308 {
309 Error error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000310 ret_val = process_sp->PutSTDIN (src, src_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000311 }
Caroline Tice7826c882010-10-26 03:11:13 +0000312
313 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000314 log->Printf ("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%d) => %lu",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000315 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000316 src,
317 (uint32_t) src_len,
318 ret_val);
Caroline Tice7826c882010-10-26 03:11:13 +0000319
320 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000321}
322
323size_t
324SBProcess::GetSTDOUT (char *dst, size_t dst_len) const
325{
Greg Clayton49ce6822010-10-31 03:01:06 +0000326 size_t bytes_read = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000327 ProcessSP process_sp(GetSP());
328 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000329 {
330 Error error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000331 bytes_read = process_sp->GetSTDOUT (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000332 }
Caroline Tice7826c882010-10-26 03:11:13 +0000333
Greg Claytone005f2c2010-11-06 01:53:30 +0000334 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000335 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000336 log->Printf ("SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
Greg Clayton851e30e2012-09-18 18:04:04 +0000337 process_sp.get(),
338 (int) bytes_read,
339 dst,
340 (uint64_t)dst_len,
341 (uint64_t)bytes_read);
Caroline Tice7826c882010-10-26 03:11:13 +0000342
Greg Clayton49ce6822010-10-31 03:01:06 +0000343 return bytes_read;
Chris Lattner24943d22010-06-08 16:52:24 +0000344}
345
346size_t
347SBProcess::GetSTDERR (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->GetSTDERR (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)::GetSTDERR (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
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000369size_t
370SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const
371{
372 size_t bytes_read = 0;
373 ProcessSP process_sp(GetSP());
374 if (process_sp)
375 {
376 Error error;
377 bytes_read = process_sp->GetAsyncProfileData (dst, dst_len, error);
378 }
379
380 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
381 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000382 log->Printf ("SBProcess(%p)::GetProfileData (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000383 process_sp.get(),
384 (int) bytes_read,
385 dst,
386 (uint64_t)dst_len,
387 (uint64_t)bytes_read);
388
389 return bytes_read;
390}
391
Chris Lattner24943d22010-06-08 16:52:24 +0000392void
Jim Inghamc8332952010-08-26 21:32:51 +0000393SBProcess::ReportEventState (const SBEvent &event, FILE *out) const
Chris Lattner24943d22010-06-08 16:52:24 +0000394{
395 if (out == NULL)
396 return;
397
Greg Clayton0416bdf2012-01-30 09:04:36 +0000398 ProcessSP process_sp(GetSP());
399 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000400 {
401 const StateType event_state = SBProcess::GetStateFromEvent (event);
402 char message[1024];
403 int message_len = ::snprintf (message,
404 sizeof (message),
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000405 "Process %" PRIu64 " %s\n",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000406 process_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000407 SBDebugger::StateAsCString (event_state));
408
409 if (message_len > 0)
410 ::fwrite (message, 1, message_len, out);
411 }
412}
413
414void
Jim Inghamc8332952010-08-26 21:32:51 +0000415SBProcess::AppendEventStateReport (const SBEvent &event, SBCommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000416{
Greg Clayton0416bdf2012-01-30 09:04:36 +0000417 ProcessSP process_sp(GetSP());
418 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000419 {
420 const StateType event_state = SBProcess::GetStateFromEvent (event);
421 char message[1024];
422 ::snprintf (message,
423 sizeof (message),
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000424 "Process %" PRIu64 " %s\n",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000425 process_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000426 SBDebugger::StateAsCString (event_state));
427
428 result.AppendMessage (message);
429 }
430}
431
432bool
Jim Inghamc8332952010-08-26 21:32:51 +0000433SBProcess::SetSelectedThread (const SBThread &thread)
Chris Lattner24943d22010-06-08 16:52:24 +0000434{
Greg Clayton0416bdf2012-01-30 09:04:36 +0000435 ProcessSP process_sp(GetSP());
436 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000437 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000438 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
439 return process_sp->GetThreadList().SetSelectedThreadByID (thread.GetThreadID());
Greg Claytonbdcda462010-12-20 20:49:23 +0000440 }
Chris Lattner24943d22010-06-08 16:52:24 +0000441 return false;
442}
443
444bool
Greg Clayton82560f22012-10-12 23:32:11 +0000445SBProcess::SetSelectedThreadByID (lldb::tid_t tid)
446{
Greg Claytone005f2c2010-11-06 01:53:30 +0000447 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000448
Caroline Tice7826c882010-10-26 03:11:13 +0000449 bool ret_val = false;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000450 ProcessSP process_sp(GetSP());
451 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000452 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000453 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
454 ret_val = process_sp->GetThreadList().SetSelectedThreadByID (tid);
Greg Claytonbdcda462010-12-20 20:49:23 +0000455 }
Caroline Tice7826c882010-10-26 03:11:13 +0000456
457 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000458 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64 ") => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000459 process_sp.get(), tid, (ret_val ? "true" : "false"));
Caroline Tice7826c882010-10-26 03:11:13 +0000460
461 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000462}
463
Jim Inghamefbdd222012-07-13 20:18:18 +0000464bool
465SBProcess::SetSelectedThreadByIndexID (uint32_t index_id)
466{
467 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
468
469 bool ret_val = false;
470 ProcessSP process_sp(GetSP());
471 if (process_sp)
472 {
473 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
474 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID (index_id);
475 }
476
477 if (log)
478 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
479 process_sp.get(), index_id, (ret_val ? "true" : "false"));
480
481 return ret_val;
482}
483
Chris Lattner24943d22010-06-08 16:52:24 +0000484SBThread
485SBProcess::GetThreadAtIndex (size_t index)
486{
Greg Claytone005f2c2010-11-06 01:53:30 +0000487 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000488
Greg Clayton90c52142012-01-30 02:53:15 +0000489 SBThread sb_thread;
490 ThreadSP thread_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000491 ProcessSP process_sp(GetSP());
492 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000493 {
Greg Claytona894fe72012-04-05 16:12:35 +0000494 Process::StopLocker stop_locker;
495 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
Greg Clayton0416bdf2012-01-30 09:04:36 +0000496 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytona894fe72012-04-05 16:12:35 +0000497 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
Greg Clayton90c52142012-01-30 02:53:15 +0000498 sb_thread.SetThread (thread_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000499 }
Caroline Tice7826c882010-10-26 03:11:13 +0000500
501 if (log)
502 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000503 log->Printf ("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000504 process_sp.get(), (uint32_t) index, thread_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000505 }
506
Greg Clayton90c52142012-01-30 02:53:15 +0000507 return sb_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000508}
509
510StateType
511SBProcess::GetState ()
512{
Caroline Tice7826c882010-10-26 03:11:13 +0000513
Caroline Tice7826c882010-10-26 03:11:13 +0000514 StateType ret_val = eStateInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000515 ProcessSP process_sp(GetSP());
516 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000517 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000518 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
519 ret_val = process_sp->GetState();
Greg Claytonbdcda462010-12-20 20:49:23 +0000520 }
Caroline Tice7826c882010-10-26 03:11:13 +0000521
Greg Claytone005f2c2010-11-06 01:53:30 +0000522 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000523 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000524 log->Printf ("SBProcess(%p)::GetState () => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000525 process_sp.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000526 lldb_private::StateAsCString (ret_val));
Caroline Tice7826c882010-10-26 03:11:13 +0000527
528 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000529}
530
531
532int
533SBProcess::GetExitStatus ()
534{
Greg Claytona66ba462010-10-30 04:51:46 +0000535 int exit_status = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000536 ProcessSP process_sp(GetSP());
537 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000538 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000539 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
540 exit_status = process_sp->GetExitStatus ();
Greg Claytonbdcda462010-12-20 20:49:23 +0000541 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000542 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000543 if (log)
544 log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000545 process_sp.get(), exit_status, exit_status);
Greg Claytona66ba462010-10-30 04:51:46 +0000546
547 return exit_status;
Chris Lattner24943d22010-06-08 16:52:24 +0000548}
549
550const char *
551SBProcess::GetExitDescription ()
552{
Greg Claytona66ba462010-10-30 04:51:46 +0000553 const char *exit_desc = NULL;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000554 ProcessSP process_sp(GetSP());
555 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000556 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000557 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
558 exit_desc = process_sp->GetExitDescription ();
Greg Claytonbdcda462010-12-20 20:49:23 +0000559 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000560 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000561 if (log)
562 log->Printf ("SBProcess(%p)::GetExitDescription () => %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000563 process_sp.get(), exit_desc);
Greg Claytona66ba462010-10-30 04:51:46 +0000564 return exit_desc;
Chris Lattner24943d22010-06-08 16:52:24 +0000565}
566
567lldb::pid_t
568SBProcess::GetProcessID ()
569{
Caroline Tice7826c882010-10-26 03:11:13 +0000570 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000571 ProcessSP process_sp(GetSP());
572 if (process_sp)
573 ret_val = process_sp->GetID();
Caroline Tice7826c882010-10-26 03:11:13 +0000574
Greg Claytone005f2c2010-11-06 01:53:30 +0000575 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000576 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000577 log->Printf ("SBProcess(%p)::GetProcessID () => %" PRIu64, process_sp.get(), ret_val);
Caroline Tice7826c882010-10-26 03:11:13 +0000578
579 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000580}
581
Johnny Chen60a544f2011-03-01 22:56:31 +0000582ByteOrder
583SBProcess::GetByteOrder () const
584{
585 ByteOrder byteOrder = eByteOrderInvalid;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000586 ProcessSP process_sp(GetSP());
587 if (process_sp)
588 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
Johnny Chen60a544f2011-03-01 22:56:31 +0000589
590 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
591 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000592 log->Printf ("SBProcess(%p)::GetByteOrder () => %d", process_sp.get(), byteOrder);
Johnny Chen60a544f2011-03-01 22:56:31 +0000593
594 return byteOrder;
595}
596
Chris Lattner24943d22010-06-08 16:52:24 +0000597uint32_t
598SBProcess::GetAddressByteSize () const
599{
Caroline Tice7826c882010-10-26 03:11:13 +0000600 uint32_t size = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000601 ProcessSP process_sp(GetSP());
602 if (process_sp)
603 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
Caroline Tice7826c882010-10-26 03:11:13 +0000604
Greg Claytone005f2c2010-11-06 01:53:30 +0000605 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000606 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000607 log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d", process_sp.get(), size);
Caroline Tice7826c882010-10-26 03:11:13 +0000608
609 return size;
Chris Lattner24943d22010-06-08 16:52:24 +0000610}
611
Chris Lattner24943d22010-06-08 16:52:24 +0000612SBError
613SBProcess::Continue ()
614{
Greg Claytone005f2c2010-11-06 01:53:30 +0000615 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000616
Chris Lattner24943d22010-06-08 16:52:24 +0000617 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000618 ProcessSP process_sp(GetSP());
619
620 if (log)
621 log->Printf ("SBProcess(%p)::Continue ()...", process_sp.get());
622
623 if (process_sp)
Greg Clayton1a3083a2010-10-06 03:53:16 +0000624 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000625 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytonde1dd812011-06-24 03:21:43 +0000626
Greg Clayton0416bdf2012-01-30 09:04:36 +0000627 Error error (process_sp->Resume());
Greg Clayton1a3083a2010-10-06 03:53:16 +0000628 if (error.Success())
629 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000630 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution () == false)
Greg Claytona66ba462010-10-30 04:51:46 +0000631 {
632 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000633 log->Printf ("SBProcess(%p)::Continue () waiting for process to stop...", process_sp.get());
634 process_sp->WaitForProcessToStop (NULL);
Greg Claytona66ba462010-10-30 04:51:46 +0000635 }
Greg Clayton1a3083a2010-10-06 03:53:16 +0000636 }
637 sb_error.SetError(error);
638 }
Chris Lattner24943d22010-06-08 16:52:24 +0000639 else
640 sb_error.SetErrorString ("SBProcess is invalid");
641
Caroline Tice7826c882010-10-26 03:11:13 +0000642 if (log)
643 {
644 SBStream sstr;
645 sb_error.GetDescription (sstr);
Greg Clayton0416bdf2012-01-30 09:04:36 +0000646 log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s", process_sp.get(), sb_error.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000647 }
648
Chris Lattner24943d22010-06-08 16:52:24 +0000649 return sb_error;
650}
651
652
653SBError
654SBProcess::Destroy ()
655{
656 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000657 ProcessSP process_sp(GetSP());
658 if (process_sp)
Greg Clayton72e1c782011-01-22 23:43:18 +0000659 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000660 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
661 sb_error.SetError(process_sp->Destroy());
Greg Clayton72e1c782011-01-22 23:43:18 +0000662 }
Chris Lattner24943d22010-06-08 16:52:24 +0000663 else
664 sb_error.SetErrorString ("SBProcess is invalid");
665
Greg Claytone005f2c2010-11-06 01:53:30 +0000666 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000667 if (log)
668 {
669 SBStream sstr;
670 sb_error.GetDescription (sstr);
Greg Clayton72e1c782011-01-22 23:43:18 +0000671 log->Printf ("SBProcess(%p)::Destroy () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000672 process_sp.get(),
Greg Clayton72e1c782011-01-22 23:43:18 +0000673 sb_error.get(),
674 sstr.GetData());
Greg Claytona66ba462010-10-30 04:51:46 +0000675 }
676
Chris Lattner24943d22010-06-08 16:52:24 +0000677 return sb_error;
678}
679
680
681SBError
682SBProcess::Stop ()
683{
684 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000685 ProcessSP process_sp(GetSP());
686 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000687 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000688 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
689 sb_error.SetError (process_sp->Halt());
Greg Claytonbdcda462010-12-20 20:49:23 +0000690 }
Chris Lattner24943d22010-06-08 16:52:24 +0000691 else
692 sb_error.SetErrorString ("SBProcess is invalid");
Caroline Tice7826c882010-10-26 03:11:13 +0000693
Greg Claytone005f2c2010-11-06 01:53:30 +0000694 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000695 if (log)
696 {
697 SBStream sstr;
698 sb_error.GetDescription (sstr);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000699 log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000700 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000701 sb_error.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000702 sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000703 }
704
Chris Lattner24943d22010-06-08 16:52:24 +0000705 return sb_error;
706}
707
708SBError
709SBProcess::Kill ()
710{
711 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000712 ProcessSP process_sp(GetSP());
713 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000714 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000715 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
716 sb_error.SetError (process_sp->Destroy());
Greg Claytonbdcda462010-12-20 20:49:23 +0000717 }
Chris Lattner24943d22010-06-08 16:52:24 +0000718 else
719 sb_error.SetErrorString ("SBProcess is invalid");
Caroline Tice7826c882010-10-26 03:11:13 +0000720
Greg Claytone005f2c2010-11-06 01:53:30 +0000721 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000722 if (log)
723 {
724 SBStream sstr;
725 sb_error.GetDescription (sstr);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000726 log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000727 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000728 sb_error.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000729 sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +0000730 }
731
Chris Lattner24943d22010-06-08 16:52:24 +0000732 return sb_error;
733}
734
Chris Lattner24943d22010-06-08 16:52:24 +0000735SBError
736SBProcess::Detach ()
737{
738 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000739 ProcessSP process_sp(GetSP());
740 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000741 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000742 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
743 sb_error.SetError (process_sp->Detach());
Greg Claytonbdcda462010-12-20 20:49:23 +0000744 }
Chris Lattner24943d22010-06-08 16:52:24 +0000745 else
746 sb_error.SetErrorString ("SBProcess is invalid");
747
748 return sb_error;
749}
750
751SBError
Greg Claytona66ba462010-10-30 04:51:46 +0000752SBProcess::Signal (int signo)
Chris Lattner24943d22010-06-08 16:52:24 +0000753{
754 SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000755 ProcessSP process_sp(GetSP());
756 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000757 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000758 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
759 sb_error.SetError (process_sp->Signal (signo));
Greg Claytonbdcda462010-12-20 20:49:23 +0000760 }
Chris Lattner24943d22010-06-08 16:52:24 +0000761 else
762 sb_error.SetErrorString ("SBProcess is invalid");
Greg Claytone005f2c2010-11-06 01:53:30 +0000763 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000764 if (log)
765 {
766 SBStream sstr;
767 sb_error.GetDescription (sstr);
768 log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000769 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000770 signo,
771 sb_error.get(),
772 sstr.GetData());
773 }
Chris Lattner24943d22010-06-08 16:52:24 +0000774 return sb_error;
775}
776
Jim Ingham5d90ade2012-07-27 23:57:19 +0000777void
778SBProcess::SendAsyncInterrupt ()
779{
780 ProcessSP process_sp(GetSP());
781 if (process_sp)
782 {
783 process_sp->SendAsyncInterrupt ();
784 }
785}
786
Chris Lattner24943d22010-06-08 16:52:24 +0000787SBThread
Greg Claytona66ba462010-10-30 04:51:46 +0000788SBProcess::GetThreadByID (tid_t tid)
Chris Lattner24943d22010-06-08 16:52:24 +0000789{
Greg Claytona66ba462010-10-30 04:51:46 +0000790 SBThread sb_thread;
Greg Clayton90c52142012-01-30 02:53:15 +0000791 ThreadSP thread_sp;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000792 ProcessSP process_sp(GetSP());
793 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +0000794 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000795 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
Greg Claytona894fe72012-04-05 16:12:35 +0000796 Process::StopLocker stop_locker;
797 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
798 thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update);
Greg Clayton90c52142012-01-30 02:53:15 +0000799 sb_thread.SetThread (thread_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000800 }
Greg Claytona66ba462010-10-30 04:51:46 +0000801
Greg Claytone005f2c2010-11-06 01:53:30 +0000802 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000803 if (log)
804 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000805 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64 ") => SBThread (%p)",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000806 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000807 tid,
Greg Clayton90c52142012-01-30 02:53:15 +0000808 thread_sp.get());
Greg Claytona66ba462010-10-30 04:51:46 +0000809 }
810
811 return sb_thread;
Chris Lattner24943d22010-06-08 16:52:24 +0000812}
813
Jim Inghamefbdd222012-07-13 20:18:18 +0000814SBThread
815SBProcess::GetThreadByIndexID (uint32_t index_id)
816{
817 SBThread sb_thread;
818 ThreadSP thread_sp;
819 ProcessSP process_sp(GetSP());
820 if (process_sp)
821 {
822 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
823 Process::StopLocker stop_locker;
824 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
825 thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update);
826 sb_thread.SetThread (thread_sp);
827 }
828
829 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
830 if (log)
831 {
832 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
833 process_sp.get(),
834 index_id,
835 thread_sp.get());
836 }
837
838 return sb_thread;
839}
840
Chris Lattner24943d22010-06-08 16:52:24 +0000841StateType
842SBProcess::GetStateFromEvent (const SBEvent &event)
843{
Greg Claytone005f2c2010-11-06 01:53:30 +0000844 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000845
Caroline Tice7826c882010-10-26 03:11:13 +0000846 StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get());
847
848 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +0000849 log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s", event.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000850 lldb_private::StateAsCString (ret_val));
Caroline Tice7826c882010-10-26 03:11:13 +0000851
852 return ret_val;
Chris Lattner24943d22010-06-08 16:52:24 +0000853}
854
Chris Lattner24943d22010-06-08 16:52:24 +0000855bool
856SBProcess::GetRestartedFromEvent (const SBEvent &event)
857{
Greg Clayton63094e02010-06-23 01:19:29 +0000858 return Process::ProcessEventData::GetRestartedFromEvent (event.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000859}
860
861SBProcess
862SBProcess::GetProcessFromEvent (const SBEvent &event)
863{
Greg Clayton63094e02010-06-23 01:19:29 +0000864 SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
Chris Lattner24943d22010-06-08 16:52:24 +0000865 return process;
866}
867
Jim Ingham28e23862012-02-08 05:23:15 +0000868bool
869SBProcess::EventIsProcessEvent (const SBEvent &event)
870{
Jim Ingham5a15e692012-02-16 06:50:00 +0000871 return strcmp (event.GetBroadcasterClass(), SBProcess::GetBroadcasterClass()) == 0;
Jim Ingham28e23862012-02-08 05:23:15 +0000872}
Chris Lattner24943d22010-06-08 16:52:24 +0000873
874SBBroadcaster
875SBProcess::GetBroadcaster () const
876{
Greg Claytone005f2c2010-11-06 01:53:30 +0000877 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000878
Greg Clayton0416bdf2012-01-30 09:04:36 +0000879 ProcessSP process_sp(GetSP());
880
881 SBBroadcaster broadcaster(process_sp.get(), false);
Caroline Tice7826c882010-10-26 03:11:13 +0000882
883 if (log)
Greg Clayton0416bdf2012-01-30 09:04:36 +0000884 log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)", process_sp.get(),
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000885 broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000886
Chris Lattner24943d22010-06-08 16:52:24 +0000887 return broadcaster;
888}
889
Jim Ingham5a15e692012-02-16 06:50:00 +0000890const char *
891SBProcess::GetBroadcasterClass ()
892{
893 return Process::GetStaticBroadcasterClass().AsCString();
894}
895
Chris Lattner24943d22010-06-08 16:52:24 +0000896size_t
897SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
898{
Greg Claytone005f2c2010-11-06 01:53:30 +0000899 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000900
Chris Lattner24943d22010-06-08 16:52:24 +0000901 size_t bytes_read = 0;
902
Greg Clayton0416bdf2012-01-30 09:04:36 +0000903 ProcessSP process_sp(GetSP());
904
Greg Claytona66ba462010-10-30 04:51:46 +0000905 if (log)
906 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000907 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
Greg Clayton0416bdf2012-01-30 09:04:36 +0000908 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +0000909 addr,
910 dst,
Greg Clayton851e30e2012-09-18 18:04:04 +0000911 (uint64_t)dst_len,
Greg Claytona66ba462010-10-30 04:51:46 +0000912 sb_error.get());
913 }
Greg Clayton0416bdf2012-01-30 09:04:36 +0000914
915 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000916 {
Greg Claytona894fe72012-04-05 16:12:35 +0000917 Process::StopLocker stop_locker;
918 if (stop_locker.TryLock(&process_sp->GetRunLock()))
919 {
920 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
921 bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref());
922 }
923 else
924 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000925 if (log)
926 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +0000927 sb_error.SetErrorString("process is running");
928 }
Chris Lattner24943d22010-06-08 16:52:24 +0000929 }
930 else
931 {
932 sb_error.SetErrorString ("SBProcess is invalid");
933 }
934
Caroline Tice7826c882010-10-26 03:11:13 +0000935 if (log)
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000936 {
937 SBStream sstr;
938 sb_error.GetDescription (sstr);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000939 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
Greg Clayton0416bdf2012-01-30 09:04:36 +0000940 process_sp.get(),
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000941 addr,
942 dst,
Greg Clayton851e30e2012-09-18 18:04:04 +0000943 (uint64_t)dst_len,
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000944 sb_error.get(),
945 sstr.GetData(),
Greg Clayton851e30e2012-09-18 18:04:04 +0000946 (uint64_t)bytes_read);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000947 }
Caroline Tice7826c882010-10-26 03:11:13 +0000948
Chris Lattner24943d22010-06-08 16:52:24 +0000949 return bytes_read;
950}
951
952size_t
Greg Clayton4a2e3372011-12-15 03:14:23 +0000953SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error)
954{
955 size_t bytes_read = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000956 ProcessSP process_sp(GetSP());
957 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +0000958 {
Greg Claytona894fe72012-04-05 16:12:35 +0000959 Process::StopLocker stop_locker;
960 if (stop_locker.TryLock(&process_sp->GetRunLock()))
961 {
962 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
963 bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref());
964 }
965 else
966 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000967 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
968 if (log)
969 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +0000970 sb_error.SetErrorString("process is running");
971 }
Greg Clayton4a2e3372011-12-15 03:14:23 +0000972 }
973 else
974 {
975 sb_error.SetErrorString ("SBProcess is invalid");
976 }
977 return bytes_read;
978}
979
980uint64_t
981SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error)
982{
Greg Claytona894fe72012-04-05 16:12:35 +0000983 uint64_t value = 0;
Greg Clayton0416bdf2012-01-30 09:04:36 +0000984 ProcessSP process_sp(GetSP());
985 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +0000986 {
Greg Claytona894fe72012-04-05 16:12:35 +0000987 Process::StopLocker stop_locker;
988 if (stop_locker.TryLock(&process_sp->GetRunLock()))
989 {
990 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
991 value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref());
992 }
993 else
994 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +0000995 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
996 if (log)
997 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +0000998 sb_error.SetErrorString("process is running");
999 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001000 }
1001 else
1002 {
1003 sb_error.SetErrorString ("SBProcess is invalid");
1004 }
Greg Claytona894fe72012-04-05 16:12:35 +00001005 return value;
Greg Clayton4a2e3372011-12-15 03:14:23 +00001006}
1007
1008lldb::addr_t
1009SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error)
1010{
1011 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001012 ProcessSP process_sp(GetSP());
1013 if (process_sp)
Greg Clayton4a2e3372011-12-15 03:14:23 +00001014 {
Greg Claytona894fe72012-04-05 16:12:35 +00001015 Process::StopLocker stop_locker;
1016 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1017 {
1018 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1019 ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref());
1020 }
1021 else
1022 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001023 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1024 if (log)
1025 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001026 sb_error.SetErrorString("process is running");
1027 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001028 }
1029 else
1030 {
1031 sb_error.SetErrorString ("SBProcess is invalid");
1032 }
1033 return ptr;
1034}
1035
1036size_t
Chris Lattner24943d22010-06-08 16:52:24 +00001037SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
1038{
1039 size_t bytes_written = 0;
1040
Greg Claytone005f2c2010-11-06 01:53:30 +00001041 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton0416bdf2012-01-30 09:04:36 +00001042
1043 ProcessSP process_sp(GetSP());
1044
Greg Claytona66ba462010-10-30 04:51:46 +00001045 if (log)
1046 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001047 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001048 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +00001049 addr,
1050 src,
Greg Clayton851e30e2012-09-18 18:04:04 +00001051 (uint64_t)src_len,
Greg Claytona66ba462010-10-30 04:51:46 +00001052 sb_error.get());
1053 }
1054
Greg Clayton0416bdf2012-01-30 09:04:36 +00001055 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001056 {
Greg Claytona894fe72012-04-05 16:12:35 +00001057 Process::StopLocker stop_locker;
1058 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1059 {
1060 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1061 bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref());
1062 }
1063 else
1064 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001065 if (log)
1066 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001067 sb_error.SetErrorString("process is running");
1068 }
Chris Lattner24943d22010-06-08 16:52:24 +00001069 }
1070
Greg Claytona66ba462010-10-30 04:51:46 +00001071 if (log)
1072 {
1073 SBStream sstr;
1074 sb_error.GetDescription (sstr);
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001075 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
Greg Clayton0416bdf2012-01-30 09:04:36 +00001076 process_sp.get(),
Greg Claytona66ba462010-10-30 04:51:46 +00001077 addr,
1078 src,
Greg Clayton851e30e2012-09-18 18:04:04 +00001079 (uint64_t)src_len,
Greg Claytona66ba462010-10-30 04:51:46 +00001080 sb_error.get(),
1081 sstr.GetData(),
Greg Clayton851e30e2012-09-18 18:04:04 +00001082 (uint64_t)bytes_written);
Greg Claytona66ba462010-10-30 04:51:46 +00001083 }
1084
Chris Lattner24943d22010-06-08 16:52:24 +00001085 return bytes_written;
1086}
1087
Caroline Tice98f930f2010-09-20 05:20:02 +00001088bool
1089SBProcess::GetDescription (SBStream &description)
1090{
Greg Clayton96154be2011-11-13 06:57:31 +00001091 Stream &strm = description.ref();
1092
Greg Clayton0416bdf2012-01-30 09:04:36 +00001093 ProcessSP process_sp(GetSP());
1094 if (process_sp)
Caroline Tice98f930f2010-09-20 05:20:02 +00001095 {
1096 char path[PATH_MAX];
1097 GetTarget().GetExecutable().GetPath (path, sizeof(path));
Greg Clayton0416bdf2012-01-30 09:04:36 +00001098 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
Greg Clayton5c4c7462010-10-06 03:09:58 +00001099 const char *exe_name = NULL;
1100 if (exe_module)
1101 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1102
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001103 strm.Printf ("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
Greg Clayton0416bdf2012-01-30 09:04:36 +00001104 process_sp->GetID(),
Greg Clayton96154be2011-11-13 06:57:31 +00001105 lldb_private::StateAsCString (GetState()),
1106 GetNumThreads(),
1107 exe_name ? ", executable = " : "",
1108 exe_name ? exe_name : "");
Caroline Tice98f930f2010-09-20 05:20:02 +00001109 }
1110 else
Greg Clayton96154be2011-11-13 06:57:31 +00001111 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +00001112
1113 return true;
1114}
Greg Clayton0baa3942010-11-04 01:54:29 +00001115
1116uint32_t
Johnny Chen191343e2012-05-23 22:34:34 +00001117SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const
1118{
1119 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1120
1121 uint32_t num = 0;
1122 ProcessSP process_sp(GetSP());
1123 if (process_sp)
1124 {
1125 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1126 sb_error.SetError(process_sp->GetWatchpointSupportInfo (num));
Johnny Chen191343e2012-05-23 22:34:34 +00001127 if (log)
1128 log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1129 process_sp.get(), num);
1130 }
1131 else
1132 {
1133 sb_error.SetErrorString ("SBProcess is invalid");
1134 }
1135 return num;
1136}
1137
1138uint32_t
Greg Clayton0baa3942010-11-04 01:54:29 +00001139SBProcess::LoadImage (lldb::SBFileSpec &sb_image_spec, lldb::SBError &sb_error)
1140{
Greg Clayton0416bdf2012-01-30 09:04:36 +00001141 ProcessSP process_sp(GetSP());
1142 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001143 {
Greg Claytona894fe72012-04-05 16:12:35 +00001144 Process::StopLocker stop_locker;
1145 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1146 {
1147 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1148 return process_sp->LoadImage (*sb_image_spec, sb_error.ref());
1149 }
1150 else
1151 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001152 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1153 if (log)
1154 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001155 sb_error.SetErrorString("process is running");
1156 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001157 }
Greg Clayton0baa3942010-11-04 01:54:29 +00001158 return LLDB_INVALID_IMAGE_TOKEN;
1159}
1160
1161lldb::SBError
1162SBProcess::UnloadImage (uint32_t image_token)
1163{
1164 lldb::SBError sb_error;
Greg Clayton0416bdf2012-01-30 09:04:36 +00001165 ProcessSP process_sp(GetSP());
1166 if (process_sp)
Greg Claytonbdcda462010-12-20 20:49:23 +00001167 {
Greg Claytona894fe72012-04-05 16:12:35 +00001168 Process::StopLocker stop_locker;
1169 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1170 {
1171 Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1172 sb_error.SetError (process_sp->UnloadImage (image_token));
1173 }
1174 else
1175 {
Greg Clayton9f3c98e2012-04-06 02:17:47 +00001176 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1177 if (log)
1178 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running", process_sp.get());
Greg Claytona894fe72012-04-05 16:12:35 +00001179 sb_error.SetErrorString("process is running");
1180 }
Greg Claytonbdcda462010-12-20 20:49:23 +00001181 }
Greg Clayton0baa3942010-11-04 01:54:29 +00001182 else
1183 sb_error.SetErrorString("invalid process");
1184 return sb_error;
1185}