blob: a288833e0f0ba0459012aac6c07ff1fc69a8ff43 [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"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/DataBufferHeap.h"
17#include "lldb/Core/DataExtractor.h"
Greg Clayton1a3083a2010-10-06 03:53:16 +000018#include "lldb/Core/Debugger.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"
30#include "lldb/API/SBDebugger.h"
31#include "lldb/API/SBCommandReturnObject.h"
32#include "lldb/API/SBEvent.h"
33#include "lldb/API/SBThread.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000034#include "lldb/API/SBStream.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000035#include "lldb/API/SBStringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
40
41
42SBProcess::SBProcess () :
Greg Clayton63094e02010-06-23 01:19:29 +000043 m_opaque_sp()
Chris Lattner24943d22010-06-08 16:52:24 +000044{
45}
46
47
48//----------------------------------------------------------------------
49// SBProcess constructor
50//----------------------------------------------------------------------
51
52SBProcess::SBProcess (const SBProcess& rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000053 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000054{
55}
56
57
58SBProcess::SBProcess (const lldb::ProcessSP &process_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000059 m_opaque_sp (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000060{
61}
62
63//----------------------------------------------------------------------
64// Destructor
65//----------------------------------------------------------------------
66SBProcess::~SBProcess()
67{
68}
69
70void
71SBProcess::SetProcess (const ProcessSP &process_sp)
72{
Greg Clayton63094e02010-06-23 01:19:29 +000073 m_opaque_sp = process_sp;
Chris Lattner24943d22010-06-08 16:52:24 +000074}
75
76void
77SBProcess::Clear ()
78{
Greg Clayton63094e02010-06-23 01:19:29 +000079 m_opaque_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +000080}
81
82
83bool
84SBProcess::IsValid() const
85{
Greg Clayton63094e02010-06-23 01:19:29 +000086 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000087}
88
89
90uint32_t
91SBProcess::GetNumThreads ()
92{
Greg Clayton63094e02010-06-23 01:19:29 +000093 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000094 {
95 const bool can_update = true;
Greg Clayton63094e02010-06-23 01:19:29 +000096 return m_opaque_sp->GetThreadList().GetSize(can_update);
Chris Lattner24943d22010-06-08 16:52:24 +000097 }
98 return 0;
99}
100
101SBThread
Jim Inghamc8332952010-08-26 21:32:51 +0000102SBProcess::GetSelectedThread () const
Chris Lattner24943d22010-06-08 16:52:24 +0000103{
104 SBThread sb_thread;
Greg Clayton63094e02010-06-23 01:19:29 +0000105 if (m_opaque_sp)
Jim Inghamc8332952010-08-26 21:32:51 +0000106 sb_thread.SetThread (m_opaque_sp->GetThreadList().GetSelectedThread());
Chris Lattner24943d22010-06-08 16:52:24 +0000107 return sb_thread;
108}
109
110SBTarget
111SBProcess::GetTarget() const
112{
113 SBTarget sb_target;
Greg Clayton63094e02010-06-23 01:19:29 +0000114 if (m_opaque_sp)
115 sb_target = m_opaque_sp->GetTarget().GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +0000116 return sb_target;
117}
118
119
120size_t
121SBProcess::PutSTDIN (const char *src, size_t src_len)
122{
Greg Clayton63094e02010-06-23 01:19:29 +0000123 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000124 {
125 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000126 return m_opaque_sp->PutSTDIN (src, src_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000127 }
128 else
129 return 0;
130}
131
132size_t
133SBProcess::GetSTDOUT (char *dst, size_t dst_len) const
134{
Greg Clayton63094e02010-06-23 01:19:29 +0000135 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000136 {
137 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000138 return m_opaque_sp->GetSTDOUT (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000139 }
140 else
141 return 0;
142}
143
144size_t
145SBProcess::GetSTDERR (char *dst, size_t dst_len) const
146{
Greg Clayton63094e02010-06-23 01:19:29 +0000147 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000148 {
149 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000150 return m_opaque_sp->GetSTDERR (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000151 }
152 else
153 return 0;
154}
155
156void
Jim Inghamc8332952010-08-26 21:32:51 +0000157SBProcess::ReportEventState (const SBEvent &event, FILE *out) const
Chris Lattner24943d22010-06-08 16:52:24 +0000158{
159 if (out == NULL)
160 return;
161
Greg Clayton63094e02010-06-23 01:19:29 +0000162 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000163 {
164 const StateType event_state = SBProcess::GetStateFromEvent (event);
165 char message[1024];
166 int message_len = ::snprintf (message,
167 sizeof (message),
168 "Process %d %s\n",
Greg Clayton63094e02010-06-23 01:19:29 +0000169 m_opaque_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000170 SBDebugger::StateAsCString (event_state));
171
172 if (message_len > 0)
173 ::fwrite (message, 1, message_len, out);
174 }
175}
176
177void
Jim Inghamc8332952010-08-26 21:32:51 +0000178SBProcess::AppendEventStateReport (const SBEvent &event, SBCommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000179{
Greg Clayton63094e02010-06-23 01:19:29 +0000180 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000181 {
182 const StateType event_state = SBProcess::GetStateFromEvent (event);
183 char message[1024];
184 ::snprintf (message,
185 sizeof (message),
186 "Process %d %s\n",
Greg Clayton63094e02010-06-23 01:19:29 +0000187 m_opaque_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000188 SBDebugger::StateAsCString (event_state));
189
190 result.AppendMessage (message);
191 }
192}
193
194bool
Jim Inghamc8332952010-08-26 21:32:51 +0000195SBProcess::SetSelectedThread (const SBThread &thread)
Chris Lattner24943d22010-06-08 16:52:24 +0000196{
Greg Clayton63094e02010-06-23 01:19:29 +0000197 if (m_opaque_sp != NULL)
Jim Inghamc8332952010-08-26 21:32:51 +0000198 return m_opaque_sp->GetThreadList().SetSelectedThreadByID (thread.GetThreadID());
Chris Lattner24943d22010-06-08 16:52:24 +0000199 return false;
200}
201
202bool
Jim Inghamc8332952010-08-26 21:32:51 +0000203SBProcess::SetSelectedThreadByID (uint32_t tid)
Chris Lattner24943d22010-06-08 16:52:24 +0000204{
Greg Clayton63094e02010-06-23 01:19:29 +0000205 if (m_opaque_sp != NULL)
Jim Inghamc8332952010-08-26 21:32:51 +0000206 return m_opaque_sp->GetThreadList().SetSelectedThreadByID (tid);
Chris Lattner24943d22010-06-08 16:52:24 +0000207 return false;
208}
209
210SBThread
211SBProcess::GetThreadAtIndex (size_t index)
212{
213 SBThread thread;
Greg Clayton63094e02010-06-23 01:19:29 +0000214 if (m_opaque_sp)
215 thread.SetThread (m_opaque_sp->GetThreadList().GetThreadAtIndex(index));
Chris Lattner24943d22010-06-08 16:52:24 +0000216 return thread;
217}
218
219StateType
220SBProcess::GetState ()
221{
Greg Clayton63094e02010-06-23 01:19:29 +0000222 if (m_opaque_sp != NULL)
223 return m_opaque_sp->GetState();
Chris Lattner24943d22010-06-08 16:52:24 +0000224 else
225 return eStateInvalid;
226}
227
228
229int
230SBProcess::GetExitStatus ()
231{
Greg Clayton63094e02010-06-23 01:19:29 +0000232 if (m_opaque_sp != NULL)
233 return m_opaque_sp->GetExitStatus ();
Chris Lattner24943d22010-06-08 16:52:24 +0000234 else
235 return 0;
236}
237
238const char *
239SBProcess::GetExitDescription ()
240{
Greg Clayton63094e02010-06-23 01:19:29 +0000241 if (m_opaque_sp != NULL)
242 return m_opaque_sp->GetExitDescription ();
Chris Lattner24943d22010-06-08 16:52:24 +0000243 else
244 return NULL;
245}
246
247lldb::pid_t
248SBProcess::GetProcessID ()
249{
Greg Clayton63094e02010-06-23 01:19:29 +0000250 if (m_opaque_sp)
251 return m_opaque_sp->GetID();
Chris Lattner24943d22010-06-08 16:52:24 +0000252 else
253 return LLDB_INVALID_PROCESS_ID;
254}
255
256uint32_t
257SBProcess::GetAddressByteSize () const
258{
Greg Clayton63094e02010-06-23 01:19:29 +0000259 if (m_opaque_sp)
260 return m_opaque_sp->GetAddressByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000261 else
262 return 0;
263}
264
Chris Lattner24943d22010-06-08 16:52:24 +0000265bool
266SBProcess::WaitUntilProcessHasStopped (SBCommandReturnObject &result)
267{
268 bool state_changed = false;
269
270 if (IsValid())
271 {
272 EventSP event_sp;
Greg Clayton63094e02010-06-23 01:19:29 +0000273 StateType state = m_opaque_sp->WaitForStateChangedEvents (NULL, event_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000274
275 while (StateIsStoppedState (state))
276 {
Greg Clayton63094e02010-06-23 01:19:29 +0000277 state = m_opaque_sp->WaitForStateChangedEvents (NULL, event_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000278 SBEvent event (event_sp);
Jim Inghamc8332952010-08-26 21:32:51 +0000279 AppendEventStateReport (event, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000280 state_changed = true;
281 }
282 }
283 return state_changed;
284}
285
286SBError
287SBProcess::Continue ()
288{
289 SBError sb_error;
290 if (IsValid())
Greg Clayton1a3083a2010-10-06 03:53:16 +0000291 {
292 Error error (m_opaque_sp->Resume());
293 if (error.Success())
294 {
295 if (m_opaque_sp->GetTarget().GetDebugger().GetAsyncExecution () == false)
296 m_opaque_sp->WaitForProcessToStop (NULL);
297 }
298 sb_error.SetError(error);
299 }
Chris Lattner24943d22010-06-08 16:52:24 +0000300 else
301 sb_error.SetErrorString ("SBProcess is invalid");
302
303 return sb_error;
304}
305
306
307SBError
308SBProcess::Destroy ()
309{
310 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000311 if (m_opaque_sp)
312 sb_error.SetError(m_opaque_sp->Destroy());
Chris Lattner24943d22010-06-08 16:52:24 +0000313 else
314 sb_error.SetErrorString ("SBProcess is invalid");
315
316 return sb_error;
317}
318
319
320SBError
321SBProcess::Stop ()
322{
323 SBError sb_error;
324 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000325 sb_error.SetError (m_opaque_sp->Halt());
Chris Lattner24943d22010-06-08 16:52:24 +0000326 else
327 sb_error.SetErrorString ("SBProcess is invalid");
328 return sb_error;
329}
330
331SBError
332SBProcess::Kill ()
333{
334 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000335 if (m_opaque_sp)
336 sb_error.SetError (m_opaque_sp->Destroy());
Chris Lattner24943d22010-06-08 16:52:24 +0000337 else
338 sb_error.SetErrorString ("SBProcess is invalid");
339 return sb_error;
340}
341
342
343SBError
344SBProcess::AttachByName (const char *name, bool wait_for_launch)
345{
346 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000347 if (m_opaque_sp)
348 sb_error.SetError (m_opaque_sp->Attach (name, wait_for_launch));
Chris Lattner24943d22010-06-08 16:52:24 +0000349 else
350 sb_error.SetErrorString ("SBProcess is invalid");
351 return sb_error;
352}
353
354lldb::pid_t
Caroline Ticedfc91c32010-09-15 18:29:06 +0000355SBProcess::AttachByPID (lldb::pid_t attach_pid) // DEPRECATED: will be removed in a few builds in favor of SBError AttachByPID(pid_t)
Chris Lattner24943d22010-06-08 16:52:24 +0000356{
357 Attach (attach_pid);
358 return GetProcessID();
359}
360
Caroline Ticedfc91c32010-09-15 18:29:06 +0000361
Chris Lattner24943d22010-06-08 16:52:24 +0000362SBError
363SBProcess::Attach (lldb::pid_t attach_pid)
364{
365 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000366 if (m_opaque_sp)
367 sb_error.SetError (m_opaque_sp->Attach (attach_pid));
Chris Lattner24943d22010-06-08 16:52:24 +0000368 else
369 sb_error.SetErrorString ("SBProcess is invalid");
370 return sb_error;
371}
372
373SBError
374SBProcess::Detach ()
375{
376 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000377 if (m_opaque_sp)
378 sb_error.SetError (m_opaque_sp->Detach());
Chris Lattner24943d22010-06-08 16:52:24 +0000379 else
380 sb_error.SetErrorString ("SBProcess is invalid");
381
382 return sb_error;
383}
384
385SBError
386SBProcess::Signal (int signal)
387{
388 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000389 if (m_opaque_sp)
390 sb_error.SetError (m_opaque_sp->Signal (signal));
Chris Lattner24943d22010-06-08 16:52:24 +0000391 else
392 sb_error.SetErrorString ("SBProcess is invalid");
393 return sb_error;
394}
395
Chris Lattner24943d22010-06-08 16:52:24 +0000396SBThread
397SBProcess::GetThreadByID (tid_t sb_thread_id)
398{
399 SBThread thread;
Greg Clayton63094e02010-06-23 01:19:29 +0000400 if (m_opaque_sp)
401 thread.SetThread (m_opaque_sp->GetThreadList().FindThreadByID ((tid_t) sb_thread_id));
Chris Lattner24943d22010-06-08 16:52:24 +0000402 return thread;
403}
404
Chris Lattner24943d22010-06-08 16:52:24 +0000405StateType
406SBProcess::GetStateFromEvent (const SBEvent &event)
407{
Greg Clayton63094e02010-06-23 01:19:29 +0000408 return Process::ProcessEventData::GetStateFromEvent (event.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000409}
410
Chris Lattner24943d22010-06-08 16:52:24 +0000411bool
412SBProcess::GetRestartedFromEvent (const SBEvent &event)
413{
Greg Clayton63094e02010-06-23 01:19:29 +0000414 return Process::ProcessEventData::GetRestartedFromEvent (event.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000415}
416
417SBProcess
418SBProcess::GetProcessFromEvent (const SBEvent &event)
419{
Greg Clayton63094e02010-06-23 01:19:29 +0000420 SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
Chris Lattner24943d22010-06-08 16:52:24 +0000421 return process;
422}
423
424
425SBBroadcaster
426SBProcess::GetBroadcaster () const
427{
Greg Clayton63094e02010-06-23 01:19:29 +0000428 SBBroadcaster broadcaster(m_opaque_sp.get(), false);
Chris Lattner24943d22010-06-08 16:52:24 +0000429 return broadcaster;
430}
431
432lldb_private::Process *
433SBProcess::operator->() const
434{
Greg Clayton63094e02010-06-23 01:19:29 +0000435 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000436}
437
438size_t
439SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
440{
441 size_t bytes_read = 0;
442
443 if (IsValid())
444 {
445 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000446 bytes_read = m_opaque_sp->ReadMemory (addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000447 sb_error.SetError (error);
448 }
449 else
450 {
451 sb_error.SetErrorString ("SBProcess is invalid");
452 }
453
454 return bytes_read;
455}
456
457size_t
458SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
459{
460 size_t bytes_written = 0;
461
462 if (IsValid())
463 {
464 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000465 bytes_written = m_opaque_sp->WriteMemory (addr, src, src_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000466 sb_error.SetError (error);
467 }
468
469 return bytes_written;
470}
471
472// Mimic shared pointer...
473lldb_private::Process *
474SBProcess::get() const
475{
Greg Clayton63094e02010-06-23 01:19:29 +0000476 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000477}
478
Caroline Tice98f930f2010-09-20 05:20:02 +0000479bool
480SBProcess::GetDescription (SBStream &description)
481{
482 if (m_opaque_sp)
483 {
484 char path[PATH_MAX];
485 GetTarget().GetExecutable().GetPath (path, sizeof(path));
Greg Clayton5c4c7462010-10-06 03:09:58 +0000486 Module *exe_module = m_opaque_sp->GetTarget().GetExecutableModule ().get();
487 const char *exe_name = NULL;
488 if (exe_module)
489 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
490
491 description.Printf ("Process {pid: %d, state: %s, threads: %d%s%s}",
492 m_opaque_sp->GetID(),
Caroline Tice98f930f2010-09-20 05:20:02 +0000493 SBDebugger::StateAsCString (GetState()),
Greg Clayton5c4c7462010-10-06 03:09:58 +0000494 GetNumThreads(),
495 exe_name ? ", executable: " : "",
496 exe_name ? exe_name : "");
Caroline Tice98f930f2010-09-20 05:20:02 +0000497 }
498 else
499 description.Printf ("No value");
500
501 return true;
502}