blob: 2d6e6b03a92f0a818ed5c67b502b23dcfe8d728e [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"
18#include "lldb/Core/State.h"
19#include "lldb/Core/Stream.h"
20#include "lldb/Core/StreamFile.h"
21#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/RegisterContext.h"
Greg Clayton63094e02010-06-23 01:19:29 +000023#include "lldb/Target/Target.h"
24#include "lldb/Target/Thread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025
26// Project includes
27
Eli Friedman7a62c8b2010-06-09 07:44:37 +000028#include "lldb/API/SBBroadcaster.h"
29#include "lldb/API/SBDebugger.h"
30#include "lldb/API/SBCommandReturnObject.h"
31#include "lldb/API/SBEvent.h"
32#include "lldb/API/SBThread.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000033#include "lldb/API/SBStream.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000034#include "lldb/API/SBStringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035
36using namespace lldb;
37using namespace lldb_private;
38
39
40
41SBProcess::SBProcess () :
Greg Clayton63094e02010-06-23 01:19:29 +000042 m_opaque_sp()
Chris Lattner24943d22010-06-08 16:52:24 +000043{
44}
45
46
47//----------------------------------------------------------------------
48// SBProcess constructor
49//----------------------------------------------------------------------
50
51SBProcess::SBProcess (const SBProcess& rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000052 m_opaque_sp (rhs.m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000053{
54}
55
56
57SBProcess::SBProcess (const lldb::ProcessSP &process_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000058 m_opaque_sp (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000059{
60}
61
62//----------------------------------------------------------------------
63// Destructor
64//----------------------------------------------------------------------
65SBProcess::~SBProcess()
66{
67}
68
69void
70SBProcess::SetProcess (const ProcessSP &process_sp)
71{
Greg Clayton63094e02010-06-23 01:19:29 +000072 m_opaque_sp = process_sp;
Chris Lattner24943d22010-06-08 16:52:24 +000073}
74
75void
76SBProcess::Clear ()
77{
Greg Clayton63094e02010-06-23 01:19:29 +000078 m_opaque_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +000079}
80
81
82bool
83SBProcess::IsValid() const
84{
Greg Clayton63094e02010-06-23 01:19:29 +000085 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000086}
87
88
89uint32_t
90SBProcess::GetNumThreads ()
91{
Greg Clayton63094e02010-06-23 01:19:29 +000092 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000093 {
94 const bool can_update = true;
Greg Clayton63094e02010-06-23 01:19:29 +000095 return m_opaque_sp->GetThreadList().GetSize(can_update);
Chris Lattner24943d22010-06-08 16:52:24 +000096 }
97 return 0;
98}
99
100SBThread
Jim Inghamc8332952010-08-26 21:32:51 +0000101SBProcess::GetSelectedThread () const
Chris Lattner24943d22010-06-08 16:52:24 +0000102{
103 SBThread sb_thread;
Greg Clayton63094e02010-06-23 01:19:29 +0000104 if (m_opaque_sp)
Jim Inghamc8332952010-08-26 21:32:51 +0000105 sb_thread.SetThread (m_opaque_sp->GetThreadList().GetSelectedThread());
Chris Lattner24943d22010-06-08 16:52:24 +0000106 return sb_thread;
107}
108
109SBTarget
110SBProcess::GetTarget() const
111{
112 SBTarget sb_target;
Greg Clayton63094e02010-06-23 01:19:29 +0000113 if (m_opaque_sp)
114 sb_target = m_opaque_sp->GetTarget().GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +0000115 return sb_target;
116}
117
118
119size_t
120SBProcess::PutSTDIN (const char *src, size_t src_len)
121{
Greg Clayton63094e02010-06-23 01:19:29 +0000122 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000123 {
124 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000125 return m_opaque_sp->PutSTDIN (src, src_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000126 }
127 else
128 return 0;
129}
130
131size_t
132SBProcess::GetSTDOUT (char *dst, size_t dst_len) const
133{
Greg Clayton63094e02010-06-23 01:19:29 +0000134 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000135 {
136 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000137 return m_opaque_sp->GetSTDOUT (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000138 }
139 else
140 return 0;
141}
142
143size_t
144SBProcess::GetSTDERR (char *dst, size_t dst_len) const
145{
Greg Clayton63094e02010-06-23 01:19:29 +0000146 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000147 {
148 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000149 return m_opaque_sp->GetSTDERR (dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000150 }
151 else
152 return 0;
153}
154
155void
Jim Inghamc8332952010-08-26 21:32:51 +0000156SBProcess::ReportEventState (const SBEvent &event, FILE *out) const
Chris Lattner24943d22010-06-08 16:52:24 +0000157{
158 if (out == NULL)
159 return;
160
Greg Clayton63094e02010-06-23 01:19:29 +0000161 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000162 {
163 const StateType event_state = SBProcess::GetStateFromEvent (event);
164 char message[1024];
165 int message_len = ::snprintf (message,
166 sizeof (message),
167 "Process %d %s\n",
Greg Clayton63094e02010-06-23 01:19:29 +0000168 m_opaque_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000169 SBDebugger::StateAsCString (event_state));
170
171 if (message_len > 0)
172 ::fwrite (message, 1, message_len, out);
173 }
174}
175
176void
Jim Inghamc8332952010-08-26 21:32:51 +0000177SBProcess::AppendEventStateReport (const SBEvent &event, SBCommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000178{
Greg Clayton63094e02010-06-23 01:19:29 +0000179 if (m_opaque_sp != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000180 {
181 const StateType event_state = SBProcess::GetStateFromEvent (event);
182 char message[1024];
183 ::snprintf (message,
184 sizeof (message),
185 "Process %d %s\n",
Greg Clayton63094e02010-06-23 01:19:29 +0000186 m_opaque_sp->GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +0000187 SBDebugger::StateAsCString (event_state));
188
189 result.AppendMessage (message);
190 }
191}
192
193bool
Jim Inghamc8332952010-08-26 21:32:51 +0000194SBProcess::SetSelectedThread (const SBThread &thread)
Chris Lattner24943d22010-06-08 16:52:24 +0000195{
Greg Clayton63094e02010-06-23 01:19:29 +0000196 if (m_opaque_sp != NULL)
Jim Inghamc8332952010-08-26 21:32:51 +0000197 return m_opaque_sp->GetThreadList().SetSelectedThreadByID (thread.GetThreadID());
Chris Lattner24943d22010-06-08 16:52:24 +0000198 return false;
199}
200
201bool
Jim Inghamc8332952010-08-26 21:32:51 +0000202SBProcess::SetSelectedThreadByID (uint32_t tid)
Chris Lattner24943d22010-06-08 16:52:24 +0000203{
Greg Clayton63094e02010-06-23 01:19:29 +0000204 if (m_opaque_sp != NULL)
Jim Inghamc8332952010-08-26 21:32:51 +0000205 return m_opaque_sp->GetThreadList().SetSelectedThreadByID (tid);
Chris Lattner24943d22010-06-08 16:52:24 +0000206 return false;
207}
208
209SBThread
210SBProcess::GetThreadAtIndex (size_t index)
211{
212 SBThread thread;
Greg Clayton63094e02010-06-23 01:19:29 +0000213 if (m_opaque_sp)
214 thread.SetThread (m_opaque_sp->GetThreadList().GetThreadAtIndex(index));
Chris Lattner24943d22010-06-08 16:52:24 +0000215 return thread;
216}
217
218StateType
219SBProcess::GetState ()
220{
Greg Clayton63094e02010-06-23 01:19:29 +0000221 if (m_opaque_sp != NULL)
222 return m_opaque_sp->GetState();
Chris Lattner24943d22010-06-08 16:52:24 +0000223 else
224 return eStateInvalid;
225}
226
227
228int
229SBProcess::GetExitStatus ()
230{
Greg Clayton63094e02010-06-23 01:19:29 +0000231 if (m_opaque_sp != NULL)
232 return m_opaque_sp->GetExitStatus ();
Chris Lattner24943d22010-06-08 16:52:24 +0000233 else
234 return 0;
235}
236
237const char *
238SBProcess::GetExitDescription ()
239{
Greg Clayton63094e02010-06-23 01:19:29 +0000240 if (m_opaque_sp != NULL)
241 return m_opaque_sp->GetExitDescription ();
Chris Lattner24943d22010-06-08 16:52:24 +0000242 else
243 return NULL;
244}
245
246lldb::pid_t
247SBProcess::GetProcessID ()
248{
Greg Clayton63094e02010-06-23 01:19:29 +0000249 if (m_opaque_sp)
250 return m_opaque_sp->GetID();
Chris Lattner24943d22010-06-08 16:52:24 +0000251 else
252 return LLDB_INVALID_PROCESS_ID;
253}
254
255uint32_t
256SBProcess::GetAddressByteSize () const
257{
Greg Clayton63094e02010-06-23 01:19:29 +0000258 if (m_opaque_sp)
259 return m_opaque_sp->GetAddressByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000260 else
261 return 0;
262}
263
Chris Lattner24943d22010-06-08 16:52:24 +0000264bool
265SBProcess::WaitUntilProcessHasStopped (SBCommandReturnObject &result)
266{
267 bool state_changed = false;
268
269 if (IsValid())
270 {
271 EventSP event_sp;
Greg Clayton63094e02010-06-23 01:19:29 +0000272 StateType state = m_opaque_sp->WaitForStateChangedEvents (NULL, event_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000273
274 while (StateIsStoppedState (state))
275 {
Greg Clayton63094e02010-06-23 01:19:29 +0000276 state = m_opaque_sp->WaitForStateChangedEvents (NULL, event_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000277 SBEvent event (event_sp);
Jim Inghamc8332952010-08-26 21:32:51 +0000278 AppendEventStateReport (event, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000279 state_changed = true;
280 }
281 }
282 return state_changed;
283}
284
285SBError
286SBProcess::Continue ()
287{
288 SBError sb_error;
289 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000290 sb_error.SetError(m_opaque_sp->Resume());
Chris Lattner24943d22010-06-08 16:52:24 +0000291 else
292 sb_error.SetErrorString ("SBProcess is invalid");
293
294 return sb_error;
295}
296
297
298SBError
299SBProcess::Destroy ()
300{
301 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000302 if (m_opaque_sp)
303 sb_error.SetError(m_opaque_sp->Destroy());
Chris Lattner24943d22010-06-08 16:52:24 +0000304 else
305 sb_error.SetErrorString ("SBProcess is invalid");
306
307 return sb_error;
308}
309
310
311SBError
312SBProcess::Stop ()
313{
314 SBError sb_error;
315 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000316 sb_error.SetError (m_opaque_sp->Halt());
Chris Lattner24943d22010-06-08 16:52:24 +0000317 else
318 sb_error.SetErrorString ("SBProcess is invalid");
319 return sb_error;
320}
321
322SBError
323SBProcess::Kill ()
324{
325 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000326 if (m_opaque_sp)
327 sb_error.SetError (m_opaque_sp->Destroy());
Chris Lattner24943d22010-06-08 16:52:24 +0000328 else
329 sb_error.SetErrorString ("SBProcess is invalid");
330 return sb_error;
331}
332
333
334SBError
335SBProcess::AttachByName (const char *name, bool wait_for_launch)
336{
337 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000338 if (m_opaque_sp)
339 sb_error.SetError (m_opaque_sp->Attach (name, wait_for_launch));
Chris Lattner24943d22010-06-08 16:52:24 +0000340 else
341 sb_error.SetErrorString ("SBProcess is invalid");
342 return sb_error;
343}
344
345lldb::pid_t
Caroline Ticedfc91c32010-09-15 18:29:06 +0000346SBProcess::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 +0000347{
348 Attach (attach_pid);
349 return GetProcessID();
350}
351
Caroline Ticedfc91c32010-09-15 18:29:06 +0000352
Chris Lattner24943d22010-06-08 16:52:24 +0000353SBError
354SBProcess::Attach (lldb::pid_t attach_pid)
355{
356 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000357 if (m_opaque_sp)
358 sb_error.SetError (m_opaque_sp->Attach (attach_pid));
Chris Lattner24943d22010-06-08 16:52:24 +0000359 else
360 sb_error.SetErrorString ("SBProcess is invalid");
361 return sb_error;
362}
363
364SBError
365SBProcess::Detach ()
366{
367 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000368 if (m_opaque_sp)
369 sb_error.SetError (m_opaque_sp->Detach());
Chris Lattner24943d22010-06-08 16:52:24 +0000370 else
371 sb_error.SetErrorString ("SBProcess is invalid");
372
373 return sb_error;
374}
375
376SBError
377SBProcess::Signal (int signal)
378{
379 SBError sb_error;
Greg Clayton63094e02010-06-23 01:19:29 +0000380 if (m_opaque_sp)
381 sb_error.SetError (m_opaque_sp->Signal (signal));
Chris Lattner24943d22010-06-08 16:52:24 +0000382 else
383 sb_error.SetErrorString ("SBProcess is invalid");
384 return sb_error;
385}
386
Chris Lattner24943d22010-06-08 16:52:24 +0000387SBThread
388SBProcess::GetThreadByID (tid_t sb_thread_id)
389{
390 SBThread thread;
Greg Clayton63094e02010-06-23 01:19:29 +0000391 if (m_opaque_sp)
392 thread.SetThread (m_opaque_sp->GetThreadList().FindThreadByID ((tid_t) sb_thread_id));
Chris Lattner24943d22010-06-08 16:52:24 +0000393 return thread;
394}
395
Chris Lattner24943d22010-06-08 16:52:24 +0000396StateType
397SBProcess::GetStateFromEvent (const SBEvent &event)
398{
Greg Clayton63094e02010-06-23 01:19:29 +0000399 return Process::ProcessEventData::GetStateFromEvent (event.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000400}
401
Chris Lattner24943d22010-06-08 16:52:24 +0000402bool
403SBProcess::GetRestartedFromEvent (const SBEvent &event)
404{
Greg Clayton63094e02010-06-23 01:19:29 +0000405 return Process::ProcessEventData::GetRestartedFromEvent (event.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000406}
407
408SBProcess
409SBProcess::GetProcessFromEvent (const SBEvent &event)
410{
Greg Clayton63094e02010-06-23 01:19:29 +0000411 SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
Chris Lattner24943d22010-06-08 16:52:24 +0000412 return process;
413}
414
415
416SBBroadcaster
417SBProcess::GetBroadcaster () const
418{
Greg Clayton63094e02010-06-23 01:19:29 +0000419 SBBroadcaster broadcaster(m_opaque_sp.get(), false);
Chris Lattner24943d22010-06-08 16:52:24 +0000420 return broadcaster;
421}
422
423lldb_private::Process *
424SBProcess::operator->() const
425{
Greg Clayton63094e02010-06-23 01:19:29 +0000426 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000427}
428
429size_t
430SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
431{
432 size_t bytes_read = 0;
433
434 if (IsValid())
435 {
436 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000437 bytes_read = m_opaque_sp->ReadMemory (addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000438 sb_error.SetError (error);
439 }
440 else
441 {
442 sb_error.SetErrorString ("SBProcess is invalid");
443 }
444
445 return bytes_read;
446}
447
448size_t
449SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
450{
451 size_t bytes_written = 0;
452
453 if (IsValid())
454 {
455 Error error;
Greg Clayton63094e02010-06-23 01:19:29 +0000456 bytes_written = m_opaque_sp->WriteMemory (addr, src, src_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000457 sb_error.SetError (error);
458 }
459
460 return bytes_written;
461}
462
463// Mimic shared pointer...
464lldb_private::Process *
465SBProcess::get() const
466{
Greg Clayton63094e02010-06-23 01:19:29 +0000467 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000468}
469
Caroline Tice98f930f2010-09-20 05:20:02 +0000470bool
471SBProcess::GetDescription (SBStream &description)
472{
473 if (m_opaque_sp)
474 {
475 char path[PATH_MAX];
476 GetTarget().GetExecutable().GetPath (path, sizeof(path));
477 description.Printf ("Process {pid: %d, executable %s\n", (int) GetProcessID(), path);
478 description.Printf (" instance name: %s, state: %s, thread cnt: %d}",
479 m_opaque_sp->GetInstanceName().AsCString(),
480 SBDebugger::StateAsCString (GetState()),
481 GetNumThreads());
482 }
483 else
484 description.Printf ("No value");
485
486 return true;
487}