blob: 0a758f6d5e28130369089eebff8d9291904d3563 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Process.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
10#include "lldb/Target/Process.h"
11
12#include "lldb/lldb-private-log.h"
13
14#include "lldb/Breakpoint/StoppointCallbackContext.h"
15#include "lldb/Breakpoint/BreakpointLocation.h"
16#include "lldb/Core/Event.h"
Caroline Tice861efb32010-11-16 05:07:41 +000017#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/Debugger.h"
Caroline Tice861efb32010-11-16 05:07:41 +000019#include "lldb/Core/InputReader.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Log.h"
21#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/State.h"
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000023#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Host/Host.h"
25#include "lldb/Target/ABI.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000026#include "lldb/Target/DynamicLoader.h"
Jim Ingham642036f2010-09-23 02:01:19 +000027#include "lldb/Target/LanguageRuntime.h"
28#include "lldb/Target/CPPLanguageRuntime.h"
29#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000031#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032#include "lldb/Target/Target.h"
33#include "lldb/Target/TargetList.h"
34#include "lldb/Target/Thread.h"
35#include "lldb/Target/ThreadPlan.h"
36
37using namespace lldb;
38using namespace lldb_private;
39
40Process*
41Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener)
42{
43 ProcessCreateInstance create_callback = NULL;
44 if (plugin_name)
45 {
46 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name);
47 if (create_callback)
48 {
49 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
50 if (debugger_ap->CanDebug(target))
51 return debugger_ap.release();
52 }
53 }
54 else
55 {
Greg Clayton54e7afa2010-07-09 20:39:50 +000056 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +000057 {
Greg Clayton54e7afa2010-07-09 20:39:50 +000058 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
59 if (debugger_ap->CanDebug(target))
60 return debugger_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +000061 }
62 }
63 return NULL;
64}
65
66
67//----------------------------------------------------------------------
68// Process constructor
69//----------------------------------------------------------------------
70Process::Process(Target &target, Listener &listener) :
71 UserID (LLDB_INVALID_PROCESS_ID),
Greg Clayton49ce6822010-10-31 03:01:06 +000072 Broadcaster ("lldb.process"),
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000073 ProcessInstanceSettings (*(Process::GetSettingsController().get())),
Chris Lattner24943d22010-06-08 16:52:24 +000074 m_target (target),
Chris Lattner24943d22010-06-08 16:52:24 +000075 m_public_state (eStateUnloaded),
76 m_private_state (eStateUnloaded),
77 m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"),
78 m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"),
79 m_private_state_listener ("lldb.process.internal_state_listener"),
80 m_private_state_control_wait(),
81 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
82 m_stop_id (0),
83 m_thread_index_id (0),
84 m_exit_status (-1),
85 m_exit_string (),
86 m_thread_list (this),
87 m_notifications (),
Greg Clayton20d338f2010-11-18 05:57:03 +000088 m_image_tokens (),
89 m_listener (listener),
90 m_breakpoint_site_list (),
91 m_persistent_vars (),
92 m_dynamic_checkers_ap (),
Caroline Tice861efb32010-11-16 05:07:41 +000093 m_unix_signals (),
Greg Clayton20d338f2010-11-18 05:57:03 +000094 m_target_triple (),
95 m_byte_order (eByteOrderHost),
96 m_addr_byte_size (0),
97 m_abi_sp (),
Caroline Tice861efb32010-11-16 05:07:41 +000098 m_process_input_reader (),
99 m_stdio_communication ("lldb.process.stdio"),
Greg Clayton20d338f2010-11-18 05:57:03 +0000100 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Caroline Tice861efb32010-11-16 05:07:41 +0000101 m_stdout_data ()
Chris Lattner24943d22010-06-08 16:52:24 +0000102{
Caroline Tice1ebef442010-09-27 00:30:10 +0000103 UpdateInstanceName();
104
Greg Claytone005f2c2010-11-06 01:53:30 +0000105 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000106 if (log)
107 log->Printf ("%p Process::Process()", this);
108
Greg Clayton49ce6822010-10-31 03:01:06 +0000109 SetEventName (eBroadcastBitStateChanged, "state-changed");
110 SetEventName (eBroadcastBitInterrupt, "interrupt");
111 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
112 SetEventName (eBroadcastBitSTDERR, "stderr-available");
113
Chris Lattner24943d22010-06-08 16:52:24 +0000114 listener.StartListeningForEvents (this,
115 eBroadcastBitStateChanged |
116 eBroadcastBitInterrupt |
117 eBroadcastBitSTDOUT |
118 eBroadcastBitSTDERR);
119
120 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
121 eBroadcastBitStateChanged);
122
123 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
124 eBroadcastInternalStateControlStop |
125 eBroadcastInternalStateControlPause |
126 eBroadcastInternalStateControlResume);
127}
128
129//----------------------------------------------------------------------
130// Destructor
131//----------------------------------------------------------------------
132Process::~Process()
133{
Greg Claytone005f2c2010-11-06 01:53:30 +0000134 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000135 if (log)
136 log->Printf ("%p Process::~Process()", this);
137 StopPrivateStateThread();
138}
139
140void
141Process::Finalize()
142{
143 // Do any cleanup needed prior to being destructed... Subclasses
144 // that override this method should call this superclass method as well.
145}
146
147void
148Process::RegisterNotificationCallbacks (const Notifications& callbacks)
149{
150 m_notifications.push_back(callbacks);
151 if (callbacks.initialize != NULL)
152 callbacks.initialize (callbacks.baton, this);
153}
154
155bool
156Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
157{
158 std::vector<Notifications>::iterator pos, end = m_notifications.end();
159 for (pos = m_notifications.begin(); pos != end; ++pos)
160 {
161 if (pos->baton == callbacks.baton &&
162 pos->initialize == callbacks.initialize &&
163 pos->process_state_changed == callbacks.process_state_changed)
164 {
165 m_notifications.erase(pos);
166 return true;
167 }
168 }
169 return false;
170}
171
172void
173Process::SynchronouslyNotifyStateChanged (StateType state)
174{
175 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
176 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
177 {
178 if (notification_pos->process_state_changed)
179 notification_pos->process_state_changed (notification_pos->baton, this, state);
180 }
181}
182
183// FIXME: We need to do some work on events before the general Listener sees them.
184// For instance if we are continuing from a breakpoint, we need to ensure that we do
185// the little "insert real insn, step & stop" trick. But we can't do that when the
186// event is delivered by the broadcaster - since that is done on the thread that is
187// waiting for new events, so if we needed more than one event for our handling, we would
188// stall. So instead we do it when we fetch the event off of the queue.
189//
190
191StateType
192Process::GetNextEvent (EventSP &event_sp)
193{
194 StateType state = eStateInvalid;
195
196 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
197 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
198
199 return state;
200}
201
202
203StateType
204Process::WaitForProcessToStop (const TimeValue *timeout)
205{
206 StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded };
207 return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType));
208}
209
210
211StateType
212Process::WaitForState
213(
214 const TimeValue *timeout,
215 const StateType *match_states, const uint32_t num_match_states
216)
217{
218 EventSP event_sp;
219 uint32_t i;
Greg Claytond8c62532010-10-07 04:19:01 +0000220 StateType state = GetState();
Chris Lattner24943d22010-06-08 16:52:24 +0000221 while (state != eStateInvalid)
222 {
Greg Claytond8c62532010-10-07 04:19:01 +0000223 // If we are exited or detached, we won't ever get back to any
224 // other valid state...
225 if (state == eStateDetached || state == eStateExited)
226 return state;
227
Chris Lattner24943d22010-06-08 16:52:24 +0000228 state = WaitForStateChangedEvents (timeout, event_sp);
229
230 for (i=0; i<num_match_states; ++i)
231 {
232 if (match_states[i] == state)
233 return state;
234 }
235 }
236 return state;
237}
238
Jim Ingham63e24d72010-10-11 23:53:14 +0000239bool
240Process::HijackProcessEvents (Listener *listener)
241{
242 if (listener != NULL)
243 {
244 return HijackBroadcaster(listener, eBroadcastBitStateChanged);
245 }
246 else
247 return false;
248}
249
250void
251Process::RestoreProcessEvents ()
252{
253 RestoreBroadcaster();
254}
255
Chris Lattner24943d22010-06-08 16:52:24 +0000256StateType
257Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
258{
Greg Claytone005f2c2010-11-06 01:53:30 +0000259 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000260
261 if (log)
262 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
263
264 StateType state = eStateInvalid;
Greg Clayton36f63a92010-10-19 03:25:40 +0000265 if (m_listener.WaitForEventForBroadcasterWithType (timeout,
266 this,
267 eBroadcastBitStateChanged,
268 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +0000269 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
270
Caroline Tice926060e2010-10-29 21:48:37 +0000271 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Chris Lattner24943d22010-06-08 16:52:24 +0000272 if (log)
273 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
274 __FUNCTION__,
275 timeout,
276 StateAsCString(state));
277 return state;
278}
279
280Event *
281Process::PeekAtStateChangedEvents ()
282{
Greg Claytone005f2c2010-11-06 01:53:30 +0000283 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000284
285 if (log)
286 log->Printf ("Process::%s...", __FUNCTION__);
287
288 Event *event_ptr;
Greg Clayton36f63a92010-10-19 03:25:40 +0000289 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
290 eBroadcastBitStateChanged);
Caroline Tice926060e2010-10-29 21:48:37 +0000291 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Chris Lattner24943d22010-06-08 16:52:24 +0000292 if (log)
293 {
294 if (event_ptr)
295 {
296 log->Printf ("Process::%s (event_ptr) => %s",
297 __FUNCTION__,
298 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
299 }
300 else
301 {
302 log->Printf ("Process::%s no events found",
303 __FUNCTION__);
304 }
305 }
306 return event_ptr;
307}
308
309StateType
310Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
311{
Greg Claytone005f2c2010-11-06 01:53:30 +0000312 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000313
314 if (log)
315 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
316
317 StateType state = eStateInvalid;
318 if (m_private_state_listener.WaitForEventForBroadcasterWithType(timeout,
319 &m_private_state_broadcaster,
320 eBroadcastBitStateChanged,
321 event_sp))
322 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
323
324 // This is a bit of a hack, but when we wait here we could very well return
325 // to the command-line, and that could disable the log, which would render the
326 // log we got above invalid.
327 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
328 if (log)
329 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
330 return state;
331}
332
333bool
334Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
335{
Greg Claytone005f2c2010-11-06 01:53:30 +0000336 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000337
338 if (log)
339 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
340
341 if (control_only)
342 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
343 else
344 return m_private_state_listener.WaitForEvent(timeout, event_sp);
345}
346
347bool
348Process::IsRunning () const
349{
350 return StateIsRunningState (m_public_state.GetValue());
351}
352
353int
354Process::GetExitStatus ()
355{
356 if (m_public_state.GetValue() == eStateExited)
357 return m_exit_status;
358 return -1;
359}
360
361const char *
362Process::GetExitDescription ()
363{
364 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
365 return m_exit_string.c_str();
366 return NULL;
367}
368
369void
370Process::SetExitStatus (int status, const char *cstr)
371{
372 m_exit_status = status;
373 if (cstr)
374 m_exit_string = cstr;
375 else
376 m_exit_string.clear();
377
378 SetPrivateState (eStateExited);
379}
380
381// This static callback can be used to watch for local child processes on
382// the current host. The the child process exits, the process will be
383// found in the global target list (we want to be completely sure that the
384// lldb_private::Process doesn't go away before we can deliver the signal.
385bool
386Process::SetProcessExitStatus
387(
388 void *callback_baton,
389 lldb::pid_t pid,
390 int signo, // Zero for no signal
391 int exit_status // Exit value of process if signal is zero
392)
393{
394 if (signo == 0 || exit_status)
395 {
Greg Clayton63094e02010-06-23 01:19:29 +0000396 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner24943d22010-06-08 16:52:24 +0000397 if (target_sp)
398 {
399 ProcessSP process_sp (target_sp->GetProcessSP());
400 if (process_sp)
401 {
402 const char *signal_cstr = NULL;
403 if (signo)
404 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
405
406 process_sp->SetExitStatus (exit_status, signal_cstr);
407 }
408 }
409 return true;
410 }
411 return false;
412}
413
414
415uint32_t
416Process::GetNextThreadIndexID ()
417{
418 return ++m_thread_index_id;
419}
420
421StateType
422Process::GetState()
423{
424 // If any other threads access this we will need a mutex for it
425 return m_public_state.GetValue ();
426}
427
428void
429Process::SetPublicState (StateType new_state)
430{
Greg Claytone005f2c2010-11-06 01:53:30 +0000431 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE));
Chris Lattner24943d22010-06-08 16:52:24 +0000432 if (log)
433 log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state));
434 m_public_state.SetValue (new_state);
435}
436
437StateType
438Process::GetPrivateState ()
439{
440 return m_private_state.GetValue();
441}
442
443void
444Process::SetPrivateState (StateType new_state)
445{
Greg Claytone005f2c2010-11-06 01:53:30 +0000446 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE));
Chris Lattner24943d22010-06-08 16:52:24 +0000447 bool state_changed = false;
448
449 if (log)
450 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
451
452 Mutex::Locker locker(m_private_state.GetMutex());
453
454 const StateType old_state = m_private_state.GetValueNoLock ();
455 state_changed = old_state != new_state;
456 if (state_changed)
457 {
458 m_private_state.SetValueNoLock (new_state);
459 if (StateIsStoppedState(new_state))
460 {
461 m_stop_id++;
462 if (log)
463 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id);
464 }
465 // Use our target to get a shared pointer to ourselves...
466 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state));
467 }
468 else
469 {
470 if (log)
471 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state));
472 }
473}
474
475
476uint32_t
477Process::GetStopID() const
478{
479 return m_stop_id;
480}
481
482addr_t
483Process::GetImageInfoAddress()
484{
485 return LLDB_INVALID_ADDRESS;
486}
487
Greg Clayton0baa3942010-11-04 01:54:29 +0000488//----------------------------------------------------------------------
489// LoadImage
490//
491// This function provides a default implementation that works for most
492// unix variants. Any Process subclasses that need to do shared library
493// loading differently should override LoadImage and UnloadImage and
494// do what is needed.
495//----------------------------------------------------------------------
496uint32_t
497Process::LoadImage (const FileSpec &image_spec, Error &error)
498{
499 DynamicLoader *loader = GetDynamicLoader();
500 if (loader)
501 {
502 error = loader->CanLoadImage();
503 if (error.Fail())
504 return LLDB_INVALID_IMAGE_TOKEN;
505 }
506
507 if (error.Success())
508 {
509 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
510 if (thread_sp == NULL)
511 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
512
513 if (thread_sp)
514 {
515 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
516
517 if (frame_sp)
518 {
519 ExecutionContext exe_ctx;
520 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +0000521 bool unwind_on_error = true;
Greg Clayton0baa3942010-11-04 01:54:29 +0000522 StreamString expr;
523 char path[PATH_MAX];
524 image_spec.GetPath(path, sizeof(path));
525 expr.Printf("dlopen (\"%s\", 2)", path);
526 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Inghamea9d4262010-11-05 19:25:48 +0000527 lldb::ValueObjectSP result_valobj_sp (ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, expr.GetData(), prefix));
Greg Clayton0baa3942010-11-04 01:54:29 +0000528 if (result_valobj_sp->GetError().Success())
529 {
530 Scalar scalar;
531 if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar))
532 {
533 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
534 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
535 {
536 uint32_t image_token = m_image_tokens.size();
537 m_image_tokens.push_back (image_ptr);
538 return image_token;
539 }
540 }
541 }
542 }
543 }
544 }
545 return LLDB_INVALID_IMAGE_TOKEN;
546}
547
548//----------------------------------------------------------------------
549// UnloadImage
550//
551// This function provides a default implementation that works for most
552// unix variants. Any Process subclasses that need to do shared library
553// loading differently should override LoadImage and UnloadImage and
554// do what is needed.
555//----------------------------------------------------------------------
556Error
557Process::UnloadImage (uint32_t image_token)
558{
559 Error error;
560 if (image_token < m_image_tokens.size())
561 {
562 const addr_t image_addr = m_image_tokens[image_token];
563 if (image_addr == LLDB_INVALID_ADDRESS)
564 {
565 error.SetErrorString("image already unloaded");
566 }
567 else
568 {
569 DynamicLoader *loader = GetDynamicLoader();
570 if (loader)
571 error = loader->CanLoadImage();
572
573 if (error.Success())
574 {
575 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
576 if (thread_sp == NULL)
577 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
578
579 if (thread_sp)
580 {
581 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
582
583 if (frame_sp)
584 {
585 ExecutionContext exe_ctx;
586 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +0000587 bool unwind_on_error = true;
Greg Clayton0baa3942010-11-04 01:54:29 +0000588 StreamString expr;
589 expr.Printf("dlclose ((void *)0x%llx)", image_addr);
590 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Inghamea9d4262010-11-05 19:25:48 +0000591 lldb::ValueObjectSP result_valobj_sp (ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, expr.GetData(), prefix));
Greg Clayton0baa3942010-11-04 01:54:29 +0000592 if (result_valobj_sp->GetError().Success())
593 {
594 Scalar scalar;
595 if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar))
596 {
597 if (scalar.UInt(1))
598 {
599 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
600 }
601 else
602 {
603 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
604 }
605 }
606 }
607 else
608 {
609 error = result_valobj_sp->GetError();
610 }
611 }
612 }
613 }
614 }
615 }
616 else
617 {
618 error.SetErrorString("invalid image token");
619 }
620 return error;
621}
622
Chris Lattner24943d22010-06-08 16:52:24 +0000623DynamicLoader *
624Process::GetDynamicLoader()
625{
626 return NULL;
627}
628
629const ABI *
630Process::GetABI()
631{
632 ConstString& triple = m_target_triple;
633
634 if (triple.IsEmpty())
635 return NULL;
636
637 if (m_abi_sp.get() == NULL)
638 {
639 m_abi_sp.reset(ABI::FindPlugin(triple));
640 }
641
642 return m_abi_sp.get();
643}
644
Jim Ingham642036f2010-09-23 02:01:19 +0000645LanguageRuntime *
646Process::GetLanguageRuntime(lldb::LanguageType language)
647{
648 LanguageRuntimeCollection::iterator pos;
649 pos = m_language_runtimes.find (language);
650 if (pos == m_language_runtimes.end())
651 {
652 lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language));
653
654 m_language_runtimes[language]
655 = runtime;
656 return runtime.get();
657 }
658 else
659 return (*pos).second.get();
660}
661
662CPPLanguageRuntime *
663Process::GetCPPLanguageRuntime ()
664{
665 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus);
666 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
667 return static_cast<CPPLanguageRuntime *> (runtime);
668 return NULL;
669}
670
671ObjCLanguageRuntime *
672Process::GetObjCLanguageRuntime ()
673{
674 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC);
675 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
676 return static_cast<ObjCLanguageRuntime *> (runtime);
677 return NULL;
678}
679
Chris Lattner24943d22010-06-08 16:52:24 +0000680BreakpointSiteList &
681Process::GetBreakpointSiteList()
682{
683 return m_breakpoint_site_list;
684}
685
686const BreakpointSiteList &
687Process::GetBreakpointSiteList() const
688{
689 return m_breakpoint_site_list;
690}
691
692
693void
694Process::DisableAllBreakpointSites ()
695{
696 m_breakpoint_site_list.SetEnabledForAll (false);
697}
698
699Error
700Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
701{
702 Error error (DisableBreakpointSiteByID (break_id));
703
704 if (error.Success())
705 m_breakpoint_site_list.Remove(break_id);
706
707 return error;
708}
709
710Error
711Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
712{
713 Error error;
714 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
715 if (bp_site_sp)
716 {
717 if (bp_site_sp->IsEnabled())
718 error = DisableBreakpoint (bp_site_sp.get());
719 }
720 else
721 {
722 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
723 }
724
725 return error;
726}
727
728Error
729Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
730{
731 Error error;
732 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
733 if (bp_site_sp)
734 {
735 if (!bp_site_sp->IsEnabled())
736 error = EnableBreakpoint (bp_site_sp.get());
737 }
738 else
739 {
740 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
741 }
742 return error;
743}
744
Stephen Wilson3fd1f362010-07-17 00:56:13 +0000745lldb::break_id_t
Chris Lattner24943d22010-06-08 16:52:24 +0000746Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware)
747{
Greg Claytoneea26402010-09-14 23:36:40 +0000748 const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +0000749 if (load_addr != LLDB_INVALID_ADDRESS)
750 {
751 BreakpointSiteSP bp_site_sp;
752
753 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
754 // create a new breakpoint site and add it.
755
756 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
757
758 if (bp_site_sp)
759 {
760 bp_site_sp->AddOwner (owner);
761 owner->SetBreakpointSite (bp_site_sp);
762 return bp_site_sp->GetID();
763 }
764 else
765 {
766 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware));
767 if (bp_site_sp)
768 {
769 if (EnableBreakpoint (bp_site_sp.get()).Success())
770 {
771 owner->SetBreakpointSite (bp_site_sp);
772 return m_breakpoint_site_list.Add (bp_site_sp);
773 }
774 }
775 }
776 }
777 // We failed to enable the breakpoint
778 return LLDB_INVALID_BREAK_ID;
779
780}
781
782void
783Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
784{
785 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
786 if (num_owners == 0)
787 {
788 DisableBreakpoint(bp_site_sp.get());
789 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
790 }
791}
792
793
794size_t
795Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
796{
797 size_t bytes_removed = 0;
798 addr_t intersect_addr;
799 size_t intersect_size;
800 size_t opcode_offset;
801 size_t idx;
802 BreakpointSiteSP bp;
803
804 for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx)
805 {
806 if (bp->GetType() == BreakpointSite::eSoftware)
807 {
808 if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
809 {
810 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
811 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
812 assert(opcode_offset + intersect_size <= bp->GetByteSize());
813 size_t buf_offset = intersect_addr - bp_addr;
814 ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
815 }
816 }
817 }
818 return bytes_removed;
819}
820
821
822Error
823Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
824{
825 Error error;
826 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +0000827 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000828 const addr_t bp_addr = bp_site->GetLoadAddress();
829 if (log)
830 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr);
831 if (bp_site->IsEnabled())
832 {
833 if (log)
834 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
835 return error;
836 }
837
838 if (bp_addr == LLDB_INVALID_ADDRESS)
839 {
840 error.SetErrorString("BreakpointSite contains an invalid load address.");
841 return error;
842 }
843 // Ask the lldb::Process subclass to fill in the correct software breakpoint
844 // trap for the breakpoint site
845 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
846
847 if (bp_opcode_size == 0)
848 {
849 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr);
850 }
851 else
852 {
853 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
854
855 if (bp_opcode_bytes == NULL)
856 {
857 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
858 return error;
859 }
860
861 // Save the original opcode by reading it
862 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
863 {
864 // Write a software breakpoint in place of the original opcode
865 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
866 {
867 uint8_t verify_bp_opcode_bytes[64];
868 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
869 {
870 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
871 {
872 bp_site->SetEnabled(true);
873 bp_site->SetType (BreakpointSite::eSoftware);
874 if (log)
875 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS",
876 bp_site->GetID(),
877 (uint64_t)bp_addr);
878 }
879 else
880 error.SetErrorString("Failed to verify the breakpoint trap in memory.");
881 }
882 else
883 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
884 }
885 else
886 error.SetErrorString("Unable to write breakpoint trap to memory.");
887 }
888 else
889 error.SetErrorString("Unable to read memory at breakpoint address.");
890 }
891 if (log)
892 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
893 bp_site->GetID(),
894 (uint64_t)bp_addr,
895 error.AsCString());
896 return error;
897}
898
899Error
900Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
901{
902 Error error;
903 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +0000904 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000905 addr_t bp_addr = bp_site->GetLoadAddress();
906 lldb::user_id_t breakID = bp_site->GetID();
907 if (log)
908 log->Printf ("ProcessMacOSX::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr);
909
910 if (bp_site->IsHardware())
911 {
912 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
913 }
914 else if (bp_site->IsEnabled())
915 {
916 const size_t break_op_size = bp_site->GetByteSize();
917 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
918 if (break_op_size > 0)
919 {
920 // Clear a software breakoint instruction
Greg Clayton54e7afa2010-07-09 20:39:50 +0000921 uint8_t curr_break_op[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +0000922 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner24943d22010-06-08 16:52:24 +0000923 bool break_op_found = false;
924
925 // Read the breakpoint opcode
926 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
927 {
928 bool verify = false;
929 // Make sure we have the a breakpoint opcode exists at this address
930 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
931 {
932 break_op_found = true;
933 // We found a valid breakpoint opcode at this address, now restore
934 // the saved opcode.
935 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
936 {
937 verify = true;
938 }
939 else
940 error.SetErrorString("Memory write failed when restoring original opcode.");
941 }
942 else
943 {
944 error.SetErrorString("Original breakpoint trap is no longer in memory.");
945 // Set verify to true and so we can check if the original opcode has already been restored
946 verify = true;
947 }
948
949 if (verify)
950 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000951 uint8_t verify_opcode[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +0000952 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner24943d22010-06-08 16:52:24 +0000953 // Verify that our original opcode made it back to the inferior
954 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
955 {
956 // compare the memory we just read with the original opcode
957 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
958 {
959 // SUCCESS
960 bp_site->SetEnabled(false);
961 if (log)
962 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
963 return error;
964 }
965 else
966 {
967 if (break_op_found)
968 error.SetErrorString("Failed to restore original opcode.");
969 }
970 }
971 else
972 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
973 }
974 }
975 else
976 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
977 }
978 }
979 else
980 {
981 if (log)
982 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
983 return error;
984 }
985
986 if (log)
987 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
988 bp_site->GetID(),
989 (uint64_t)bp_addr,
990 error.AsCString());
991 return error;
992
993}
994
995
996size_t
997Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
998{
999 if (buf == NULL || size == 0)
1000 return 0;
1001
1002 size_t bytes_read = 0;
1003 uint8_t *bytes = (uint8_t *)buf;
1004
1005 while (bytes_read < size)
1006 {
1007 const size_t curr_size = size - bytes_read;
1008 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
1009 bytes + bytes_read,
1010 curr_size,
1011 error);
1012 bytes_read += curr_bytes_read;
1013 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
1014 break;
1015 }
1016
1017 // Replace any software breakpoint opcodes that fall into this range back
1018 // into "buf" before we return
1019 if (bytes_read > 0)
1020 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
1021 return bytes_read;
1022}
1023
1024size_t
1025Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
1026{
1027 size_t bytes_written = 0;
1028 const uint8_t *bytes = (const uint8_t *)buf;
1029
1030 while (bytes_written < size)
1031 {
1032 const size_t curr_size = size - bytes_written;
1033 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
1034 bytes + bytes_written,
1035 curr_size,
1036 error);
1037 bytes_written += curr_bytes_written;
1038 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
1039 break;
1040 }
1041 return bytes_written;
1042}
1043
1044size_t
1045Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1046{
1047 if (buf == NULL || size == 0)
1048 return 0;
1049 // We need to write any data that would go where any current software traps
1050 // (enabled software breakpoints) any software traps (breakpoints) that we
1051 // may have placed in our tasks memory.
1052
1053 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
1054 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
1055
1056 if (iter == end || iter->second->GetLoadAddress() > addr + size)
1057 return DoWriteMemory(addr, buf, size, error);
1058
1059 BreakpointSiteList::collection::const_iterator pos;
1060 size_t bytes_written = 0;
Greg Clayton54e7afa2010-07-09 20:39:50 +00001061 addr_t intersect_addr = 0;
1062 size_t intersect_size = 0;
1063 size_t opcode_offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001064 const uint8_t *ubuf = (const uint8_t *)buf;
1065
1066 for (pos = iter; pos != end; ++pos)
1067 {
1068 BreakpointSiteSP bp;
1069 bp = pos->second;
1070
1071 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
1072 assert(addr <= intersect_addr && intersect_addr < addr + size);
1073 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
1074 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1075
1076 // Check for bytes before this breakpoint
1077 const addr_t curr_addr = addr + bytes_written;
1078 if (intersect_addr > curr_addr)
1079 {
1080 // There are some bytes before this breakpoint that we need to
1081 // just write to memory
1082 size_t curr_size = intersect_addr - curr_addr;
1083 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
1084 ubuf + bytes_written,
1085 curr_size,
1086 error);
1087 bytes_written += curr_bytes_written;
1088 if (curr_bytes_written != curr_size)
1089 {
1090 // We weren't able to write all of the requested bytes, we
1091 // are done looping and will return the number of bytes that
1092 // we have written so far.
1093 break;
1094 }
1095 }
1096
1097 // Now write any bytes that would cover up any software breakpoints
1098 // directly into the breakpoint opcode buffer
1099 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
1100 bytes_written += intersect_size;
1101 }
1102
1103 // Write any remaining bytes after the last breakpoint if we have any left
1104 if (bytes_written < size)
1105 bytes_written += WriteMemoryPrivate (addr + bytes_written,
1106 ubuf + bytes_written,
1107 size - bytes_written,
1108 error);
1109
1110 return bytes_written;
1111}
1112
1113addr_t
1114Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
1115{
1116 // Fixme: we should track the blocks we've allocated, and clean them up...
1117 // We could even do our own allocator here if that ends up being more efficient.
1118 return DoAllocateMemory (size, permissions, error);
1119}
1120
1121Error
1122Process::DeallocateMemory (addr_t ptr)
1123{
1124 return DoDeallocateMemory (ptr);
1125}
1126
1127
1128Error
1129Process::EnableWatchpoint (WatchpointLocation *watchpoint)
1130{
1131 Error error;
1132 error.SetErrorString("watchpoints are not supported");
1133 return error;
1134}
1135
1136Error
1137Process::DisableWatchpoint (WatchpointLocation *watchpoint)
1138{
1139 Error error;
1140 error.SetErrorString("watchpoints are not supported");
1141 return error;
1142}
1143
1144StateType
1145Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
1146{
1147 StateType state;
1148 // Now wait for the process to launch and return control to us, and then
1149 // call DidLaunch:
1150 while (1)
1151 {
1152 // FIXME: Might want to put a timeout in here:
1153 state = WaitForStateChangedEventsPrivate (NULL, event_sp);
1154 if (state == eStateStopped || state == eStateCrashed || state == eStateExited)
1155 break;
1156 else
1157 HandlePrivateEvent (event_sp);
1158 }
1159 return state;
1160}
1161
1162Error
1163Process::Launch
1164(
1165 char const *argv[],
1166 char const *envp[],
Greg Clayton452bf612010-08-31 18:35:14 +00001167 uint32_t launch_flags,
Chris Lattner24943d22010-06-08 16:52:24 +00001168 const char *stdin_path,
1169 const char *stdout_path,
1170 const char *stderr_path
1171)
1172{
1173 Error error;
1174 m_target_triple.Clear();
1175 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001176 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001177
1178 Module *exe_module = m_target.GetExecutableModule().get();
1179 if (exe_module)
1180 {
1181 char exec_file_path[PATH_MAX];
1182 exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path));
1183 if (exe_module->GetFileSpec().Exists())
1184 {
1185 error = WillLaunch (exe_module);
1186 if (error.Success())
1187 {
Greg Claytond8c62532010-10-07 04:19:01 +00001188 SetPublicState (eStateLaunching);
Chris Lattner24943d22010-06-08 16:52:24 +00001189 // The args coming in should not contain the application name, the
1190 // lldb_private::Process class will add this in case the executable
1191 // gets resolved to a different file than was given on the command
1192 // line (like when an applicaiton bundle is specified and will
1193 // resolve to the contained exectuable file, or the file given was
1194 // a symlink or other file system link that resolves to a different
1195 // file).
1196
1197 // Get the resolved exectuable path
1198
1199 // Make a new argument vector
1200 std::vector<const char *> exec_path_plus_argv;
1201 // Append the resolved executable path
1202 exec_path_plus_argv.push_back (exec_file_path);
1203
1204 // Push all args if there are any
1205 if (argv)
1206 {
1207 for (int i = 0; argv[i]; ++i)
1208 exec_path_plus_argv.push_back(argv[i]);
1209 }
1210
1211 // Push a NULL to terminate the args.
1212 exec_path_plus_argv.push_back(NULL);
1213
1214 // Now launch using these arguments.
Greg Clayton53d68e72010-07-20 22:52:08 +00001215 error = DoLaunch (exe_module,
1216 exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(),
1217 envp,
Greg Clayton452bf612010-08-31 18:35:14 +00001218 launch_flags,
Greg Clayton53d68e72010-07-20 22:52:08 +00001219 stdin_path,
1220 stdout_path,
1221 stderr_path);
Chris Lattner24943d22010-06-08 16:52:24 +00001222
1223 if (error.Fail())
1224 {
1225 if (GetID() != LLDB_INVALID_PROCESS_ID)
1226 {
1227 SetID (LLDB_INVALID_PROCESS_ID);
1228 const char *error_string = error.AsCString();
1229 if (error_string == NULL)
1230 error_string = "launch failed";
1231 SetExitStatus (-1, error_string);
1232 }
1233 }
1234 else
1235 {
1236 EventSP event_sp;
1237 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1238
1239 if (state == eStateStopped || state == eStateCrashed)
1240 {
1241 DidLaunch ();
1242
1243 // This delays passing the stopped event to listeners till DidLaunch gets
1244 // a chance to complete...
1245 HandlePrivateEvent (event_sp);
1246 StartPrivateStateThread ();
1247 }
1248 else if (state == eStateExited)
1249 {
1250 // We exited while trying to launch somehow. Don't call DidLaunch as that's
1251 // not likely to work, and return an invalid pid.
1252 HandlePrivateEvent (event_sp);
1253 }
1254 }
1255 }
1256 }
1257 else
1258 {
1259 error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path);
1260 }
1261 }
1262 return error;
1263}
1264
1265Error
1266Process::CompleteAttach ()
1267{
1268 Error error;
Greg Claytonc1d37752010-10-18 01:45:30 +00001269
1270 if (GetID() == LLDB_INVALID_PROCESS_ID)
1271 {
1272 error.SetErrorString("no process");
1273 }
1274
Chris Lattner24943d22010-06-08 16:52:24 +00001275 EventSP event_sp;
1276 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1277 if (state == eStateStopped || state == eStateCrashed)
1278 {
1279 DidAttach ();
Jim Ingham7508e732010-08-09 23:31:02 +00001280 // Figure out which one is the executable, and set that in our target:
1281 ModuleList &modules = GetTarget().GetImages();
1282
1283 size_t num_modules = modules.GetSize();
1284 for (int i = 0; i < num_modules; i++)
1285 {
1286 ModuleSP module_sp = modules.GetModuleAtIndex(i);
1287 if (module_sp->IsExecutable())
1288 {
1289 ModuleSP exec_module = GetTarget().GetExecutableModule();
1290 if (!exec_module || exec_module != module_sp)
1291 {
1292
1293 GetTarget().SetExecutableModule (module_sp, false);
1294 }
1295 break;
1296 }
1297 }
Chris Lattner24943d22010-06-08 16:52:24 +00001298
1299 // This delays passing the stopped event to listeners till DidLaunch gets
1300 // a chance to complete...
1301 HandlePrivateEvent(event_sp);
1302 StartPrivateStateThread();
1303 }
1304 else
1305 {
1306 // We exited while trying to launch somehow. Don't call DidLaunch as that's
1307 // not likely to work, and return an invalid pid.
1308 if (state == eStateExited)
1309 HandlePrivateEvent (event_sp);
1310 error.SetErrorStringWithFormat("invalid state after attach: %s",
1311 lldb_private::StateAsCString(state));
1312 }
1313 return error;
1314}
1315
1316Error
1317Process::Attach (lldb::pid_t attach_pid)
1318{
1319
1320 m_target_triple.Clear();
1321 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001322 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001323
Jim Ingham7508e732010-08-09 23:31:02 +00001324 // Find the process and its architecture. Make sure it matches the architecture
1325 // of the current Target, and if not adjust it.
1326
1327 ArchSpec attach_spec = GetArchSpecForExistingProcess (attach_pid);
1328 if (attach_spec != GetTarget().GetArchitecture())
1329 {
1330 // Set the architecture on the target.
1331 GetTarget().SetArchitecture(attach_spec);
1332 }
1333
Greg Clayton54e7afa2010-07-09 20:39:50 +00001334 Error error (WillAttachToProcessWithID(attach_pid));
Chris Lattner24943d22010-06-08 16:52:24 +00001335 if (error.Success())
1336 {
Greg Claytond8c62532010-10-07 04:19:01 +00001337 SetPublicState (eStateAttaching);
1338
Greg Clayton54e7afa2010-07-09 20:39:50 +00001339 error = DoAttachToProcessWithID (attach_pid);
Chris Lattner24943d22010-06-08 16:52:24 +00001340 if (error.Success())
1341 {
1342 error = CompleteAttach();
1343 }
1344 else
1345 {
1346 if (GetID() != LLDB_INVALID_PROCESS_ID)
1347 {
1348 SetID (LLDB_INVALID_PROCESS_ID);
1349 const char *error_string = error.AsCString();
1350 if (error_string == NULL)
1351 error_string = "attach failed";
1352
1353 SetExitStatus(-1, error_string);
1354 }
1355 }
1356 }
1357 return error;
1358}
1359
1360Error
1361Process::Attach (const char *process_name, bool wait_for_launch)
1362{
1363 m_target_triple.Clear();
1364 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001365 m_process_input_reader.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001366
1367 // Find the process and its architecture. Make sure it matches the architecture
1368 // of the current Target, and if not adjust it.
1369
Jim Inghamea294182010-08-17 21:54:19 +00001370 if (!wait_for_launch)
Jim Ingham7508e732010-08-09 23:31:02 +00001371 {
Jim Inghamea294182010-08-17 21:54:19 +00001372 ArchSpec attach_spec = GetArchSpecForExistingProcess (process_name);
Greg Claytonc1d37752010-10-18 01:45:30 +00001373 if (attach_spec.IsValid() && attach_spec != GetTarget().GetArchitecture())
Jim Inghamea294182010-08-17 21:54:19 +00001374 {
1375 // Set the architecture on the target.
1376 GetTarget().SetArchitecture(attach_spec);
1377 }
Jim Ingham7508e732010-08-09 23:31:02 +00001378 }
Jim Inghamea294182010-08-17 21:54:19 +00001379
Greg Clayton54e7afa2010-07-09 20:39:50 +00001380 Error error (WillAttachToProcessWithName(process_name, wait_for_launch));
Chris Lattner24943d22010-06-08 16:52:24 +00001381 if (error.Success())
1382 {
Greg Claytond8c62532010-10-07 04:19:01 +00001383 SetPublicState (eStateAttaching);
Greg Clayton54e7afa2010-07-09 20:39:50 +00001384 error = DoAttachToProcessWithName (process_name, wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +00001385 if (error.Fail())
1386 {
1387 if (GetID() != LLDB_INVALID_PROCESS_ID)
1388 {
1389 SetID (LLDB_INVALID_PROCESS_ID);
1390 const char *error_string = error.AsCString();
1391 if (error_string == NULL)
1392 error_string = "attach failed";
1393
1394 SetExitStatus(-1, error_string);
1395 }
1396 }
1397 else
1398 {
1399 error = CompleteAttach();
1400 }
1401 }
1402 return error;
1403}
1404
1405Error
1406Process::Resume ()
1407{
Greg Claytone005f2c2010-11-06 01:53:30 +00001408 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001409 if (log)
1410 log->Printf("Process::Resume() m_stop_id = %u", m_stop_id);
1411
1412 Error error (WillResume());
1413 // Tell the process it is about to resume before the thread list
1414 if (error.Success())
1415 {
1416 // Now let the thread list know we are about to resume to it
1417 // can let all of our threads know that they are about to be
1418 // resumed. Threads will each be called with
1419 // Thread::WillResume(StateType) where StateType contains the state
1420 // that they are supposed to have when the process is resumed
1421 // (suspended/running/stepping). Threads should also check
1422 // their resume signal in lldb::Thread::GetResumeSignal()
1423 // to see if they are suppoed to start back up with a signal.
1424 if (m_thread_list.WillResume())
1425 {
1426 error = DoResume();
1427 if (error.Success())
1428 {
1429 DidResume();
1430 m_thread_list.DidResume();
1431 }
1432 }
1433 else
1434 {
1435 error.SetErrorStringWithFormat("thread list returned flase after WillResume");
1436 }
1437 }
1438 return error;
1439}
1440
1441Error
1442Process::Halt ()
1443{
1444 Error error (WillHalt());
1445
1446 if (error.Success())
1447 {
Greg Clayton20d338f2010-11-18 05:57:03 +00001448
1449 bool caused_stop = false;
1450 EventSP event_sp;
1451
1452 // Pause our private state thread so we can ensure no one else eats
1453 // the stop event out from under us.
1454 PausePrivateStateThread();
1455
1456 // Ask the process subclass to actually halt our process
Jim Ingham3ae449a2010-11-17 02:32:00 +00001457 error = DoHalt(caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +00001458 if (error.Success())
Jim Ingham3ae449a2010-11-17 02:32:00 +00001459 {
Greg Clayton20d338f2010-11-18 05:57:03 +00001460 // If "caused_stop" is true, then DoHalt stopped the process. If
1461 // "caused_stop" is false, the process was already stopped.
1462 // If the DoHalt caused the process to stop, then we want to catch
1463 // this event and set the interrupted bool to true before we pass
1464 // this along so clients know that the process was interrupted by
1465 // a halt command.
Jim Ingham3ae449a2010-11-17 02:32:00 +00001466 if (caused_stop)
1467 {
Greg Clayton20d338f2010-11-18 05:57:03 +00001468 // Wait for 2 seconds for the process to stop.
1469 TimeValue timeout_time;
1470 timeout_time = TimeValue::Now();
1471 timeout_time.OffsetWithSeconds(2);
1472 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, event_sp);
1473
1474 if (state == eStateInvalid)
1475 {
1476 // We timeout out and didn't get a stop event...
1477 error.SetErrorString ("Halt timed out.");
1478 }
1479 else
1480 {
1481 // Since we are eating the event, we need to update our state
1482 // otherwise the process state will not match reality...
1483 SetPublicState(state);
1484
1485 if (StateIsStoppedState (state))
1486 {
1487 // We caused the process to interrupt itself, so mark this
1488 // as such in the stop event so clients can tell an interrupted
1489 // process from a natural stop
1490 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
1491 }
1492 else
1493 {
1494 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1495 if (log)
1496 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
1497 error.SetErrorString ("Did not get stopped event after halt.");
1498 }
1499 }
Jim Ingham3ae449a2010-11-17 02:32:00 +00001500 }
Greg Clayton20d338f2010-11-18 05:57:03 +00001501 DidHalt();
1502
Jim Ingham3ae449a2010-11-17 02:32:00 +00001503 }
Greg Clayton20d338f2010-11-18 05:57:03 +00001504 // Resume our private state thread before we post the event (if any)
1505 ResumePrivateStateThread();
1506
1507 // Post any event we might have consumed. If all goes well, we will have
1508 // stopped the process, intercepted the event and set the interrupted
1509 // bool in the event.
1510 if (event_sp)
1511 BroadcastEvent(event_sp);
1512
Chris Lattner24943d22010-06-08 16:52:24 +00001513 }
1514 return error;
1515}
1516
1517Error
1518Process::Detach ()
1519{
1520 Error error (WillDetach());
1521
1522 if (error.Success())
1523 {
1524 DisableAllBreakpointSites();
1525 error = DoDetach();
1526 if (error.Success())
1527 {
1528 DidDetach();
1529 StopPrivateStateThread();
1530 }
1531 }
1532 return error;
1533}
1534
1535Error
1536Process::Destroy ()
1537{
1538 Error error (WillDestroy());
1539 if (error.Success())
1540 {
1541 DisableAllBreakpointSites();
1542 error = DoDestroy();
1543 if (error.Success())
1544 {
1545 DidDestroy();
1546 StopPrivateStateThread();
1547 }
Caroline Tice861efb32010-11-16 05:07:41 +00001548 m_stdio_communication.StopReadThread();
1549 m_stdio_communication.Disconnect();
1550 if (m_process_input_reader && m_process_input_reader->IsActive())
1551 m_target.GetDebugger().PopInputReader (m_process_input_reader);
1552 if (m_process_input_reader)
1553 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001554 }
1555 return error;
1556}
1557
1558Error
1559Process::Signal (int signal)
1560{
1561 Error error (WillSignal());
1562 if (error.Success())
1563 {
1564 error = DoSignal(signal);
1565 if (error.Success())
1566 DidSignal();
1567 }
1568 return error;
1569}
1570
1571UnixSignals &
1572Process::GetUnixSignals ()
1573{
1574 return m_unix_signals;
1575}
1576
1577Target &
1578Process::GetTarget ()
1579{
1580 return m_target;
1581}
1582
1583const Target &
1584Process::GetTarget () const
1585{
1586 return m_target;
1587}
1588
1589uint32_t
1590Process::GetAddressByteSize()
1591{
Greg Clayton20d338f2010-11-18 05:57:03 +00001592 if (m_addr_byte_size == 0)
1593 return m_target.GetArchitecture().GetAddressByteSize();
1594 return m_addr_byte_size;
Chris Lattner24943d22010-06-08 16:52:24 +00001595}
1596
1597bool
1598Process::ShouldBroadcastEvent (Event *event_ptr)
1599{
1600 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
1601 bool return_value = true;
Greg Claytone005f2c2010-11-06 01:53:30 +00001602 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001603
1604 switch (state)
1605 {
1606 case eStateAttaching:
1607 case eStateLaunching:
1608 case eStateDetached:
1609 case eStateExited:
1610 case eStateUnloaded:
1611 // These events indicate changes in the state of the debugging session, always report them.
1612 return_value = true;
1613 break;
1614 case eStateInvalid:
1615 // We stopped for no apparent reason, don't report it.
1616 return_value = false;
1617 break;
1618 case eStateRunning:
1619 case eStateStepping:
1620 // If we've started the target running, we handle the cases where we
1621 // are already running and where there is a transition from stopped to
1622 // running differently.
1623 // running -> running: Automatically suppress extra running events
1624 // stopped -> running: Report except when there is one or more no votes
1625 // and no yes votes.
1626 SynchronouslyNotifyStateChanged (state);
1627 switch (m_public_state.GetValue())
1628 {
1629 case eStateRunning:
1630 case eStateStepping:
1631 // We always suppress multiple runnings with no PUBLIC stop in between.
1632 return_value = false;
1633 break;
1634 default:
1635 // TODO: make this work correctly. For now always report
1636 // run if we aren't running so we don't miss any runnning
1637 // events. If I run the lldb/test/thread/a.out file and
1638 // break at main.cpp:58, run and hit the breakpoints on
1639 // multiple threads, then somehow during the stepping over
1640 // of all breakpoints no run gets reported.
1641 return_value = true;
1642
1643 // This is a transition from stop to run.
1644 switch (m_thread_list.ShouldReportRun (event_ptr))
1645 {
1646 case eVoteYes:
1647 case eVoteNoOpinion:
1648 return_value = true;
1649 break;
1650 case eVoteNo:
1651 return_value = false;
1652 break;
1653 }
1654 break;
1655 }
1656 break;
1657 case eStateStopped:
1658 case eStateCrashed:
1659 case eStateSuspended:
1660 {
1661 // We've stopped. First see if we're going to restart the target.
1662 // If we are going to stop, then we always broadcast the event.
1663 // If we aren't going to stop, let the thread plans decide if we're going to report this event.
Jim Ingham5a47e8b2010-06-19 04:45:32 +00001664 // If no thread has an opinion, we don't report it.
Jim Ingham3ae449a2010-11-17 02:32:00 +00001665 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner24943d22010-06-08 16:52:24 +00001666 {
Greg Clayton20d338f2010-11-18 05:57:03 +00001667 if (log)
1668 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", event_ptr, StateAsCString(state));
Jim Ingham3ae449a2010-11-17 02:32:00 +00001669 return true;
1670 }
1671 else
1672 {
Chris Lattner24943d22010-06-08 16:52:24 +00001673 RefreshStateAfterStop ();
1674
1675 if (m_thread_list.ShouldStop (event_ptr) == false)
1676 {
1677 switch (m_thread_list.ShouldReportStop (event_ptr))
1678 {
1679 case eVoteYes:
1680 Process::ProcessEventData::SetRestartedInEvent (event_ptr, true);
Johnny Chen028784b2010-10-14 00:54:32 +00001681 // Intentional fall-through here.
Chris Lattner24943d22010-06-08 16:52:24 +00001682 case eVoteNoOpinion:
Chris Lattner24943d22010-06-08 16:52:24 +00001683 case eVoteNo:
1684 return_value = false;
1685 break;
1686 }
1687
1688 if (log)
Jim Ingham3ae449a2010-11-17 02:32:00 +00001689 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state));
Chris Lattner24943d22010-06-08 16:52:24 +00001690 Resume ();
1691 }
1692 else
1693 {
1694 return_value = true;
1695 SynchronouslyNotifyStateChanged (state);
1696 }
1697 }
1698 }
1699 }
1700
1701 if (log)
1702 log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO");
1703 return return_value;
1704}
1705
1706//------------------------------------------------------------------
1707// Thread Queries
1708//------------------------------------------------------------------
1709
1710ThreadList &
1711Process::GetThreadList ()
1712{
1713 return m_thread_list;
1714}
1715
1716const ThreadList &
1717Process::GetThreadList () const
1718{
1719 return m_thread_list;
1720}
1721
1722
1723bool
1724Process::StartPrivateStateThread ()
1725{
Greg Claytone005f2c2010-11-06 01:53:30 +00001726 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001727
1728 if (log)
1729 log->Printf ("Process::%s ( )", __FUNCTION__);
1730
1731 // Create a thread that watches our internal state and controls which
1732 // events make it to clients (into the DCProcess event queue).
1733 m_private_state_thread = Host::ThreadCreate ("<lldb.process.internal-state>", Process::PrivateStateThread, this, NULL);
1734 return m_private_state_thread != LLDB_INVALID_HOST_THREAD;
1735}
1736
1737void
1738Process::PausePrivateStateThread ()
1739{
1740 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
1741}
1742
1743void
1744Process::ResumePrivateStateThread ()
1745{
1746 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
1747}
1748
1749void
1750Process::StopPrivateStateThread ()
1751{
1752 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
1753}
1754
1755void
1756Process::ControlPrivateStateThread (uint32_t signal)
1757{
Greg Claytone005f2c2010-11-06 01:53:30 +00001758 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001759
1760 assert (signal == eBroadcastInternalStateControlStop ||
1761 signal == eBroadcastInternalStateControlPause ||
1762 signal == eBroadcastInternalStateControlResume);
1763
1764 if (log)
1765 log->Printf ("Process::%s ( ) - signal: %d", __FUNCTION__, signal);
1766
1767 // Signal the private state thread
1768 if (m_private_state_thread != LLDB_INVALID_HOST_THREAD)
1769 {
1770 TimeValue timeout_time;
1771 bool timed_out;
1772
1773 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
1774
1775 timeout_time = TimeValue::Now();
1776 timeout_time.OffsetWithSeconds(2);
1777 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
1778 m_private_state_control_wait.SetValue (false, eBroadcastNever);
1779
1780 if (signal == eBroadcastInternalStateControlStop)
1781 {
1782 if (timed_out)
1783 Host::ThreadCancel (m_private_state_thread, NULL);
1784
1785 thread_result_t result = NULL;
1786 Host::ThreadJoin (m_private_state_thread, &result, NULL);
Greg Claytonc607d862010-07-22 18:34:21 +00001787 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00001788 }
1789 }
1790}
1791
1792void
1793Process::HandlePrivateEvent (EventSP &event_sp)
1794{
Greg Claytone005f2c2010-11-06 01:53:30 +00001795 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001796 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1797 // See if we should broadcast this state to external clients?
1798 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
1799 if (log)
1800 log->Printf ("Process::%s (arg = %p, pid = %i) got event '%s' broadcast = %s", __FUNCTION__, this, GetID(), StateAsCString(internal_state), should_broadcast ? "yes" : "no");
1801
1802 if (should_broadcast)
1803 {
1804 if (log)
1805 {
1806 log->Printf ("\tChanging public state from: %s to %s", StateAsCString(GetState ()), StateAsCString (internal_state));
1807 }
Caroline Tice861efb32010-11-16 05:07:41 +00001808 if (StateIsRunningState (internal_state))
1809 PushProcessInputReader ();
1810 else
1811 PopProcessInputReader ();
Chris Lattner24943d22010-06-08 16:52:24 +00001812 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
1813 BroadcastEvent (event_sp);
1814 }
1815 else
1816 {
1817 if (log)
1818 {
1819 log->Printf ("\tNot changing public state with event: %s", StateAsCString (internal_state));
1820 }
1821 }
1822}
1823
1824void *
1825Process::PrivateStateThread (void *arg)
1826{
1827 Process *proc = static_cast<Process*> (arg);
1828 void *result = proc->RunPrivateStateThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00001829 return result;
1830}
1831
1832void *
1833Process::RunPrivateStateThread ()
1834{
1835 bool control_only = false;
1836 m_private_state_control_wait.SetValue (false, eBroadcastNever);
1837
Greg Claytone005f2c2010-11-06 01:53:30 +00001838 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001839 if (log)
1840 log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID());
1841
1842 bool exit_now = false;
1843 while (!exit_now)
1844 {
1845 EventSP event_sp;
1846 WaitForEventsPrivate (NULL, event_sp, control_only);
1847 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
1848 {
1849 switch (event_sp->GetType())
1850 {
1851 case eBroadcastInternalStateControlStop:
1852 exit_now = true;
1853 continue; // Go to next loop iteration so we exit without
1854 break; // doing any internal state managment below
1855
1856 case eBroadcastInternalStateControlPause:
1857 control_only = true;
1858 break;
1859
1860 case eBroadcastInternalStateControlResume:
1861 control_only = false;
1862 break;
1863 }
Jim Ingham3ae449a2010-11-17 02:32:00 +00001864
1865 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
1866 if (log)
1867 log->Printf ("Process::%s (arg = %p, pid = %i) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType());
1868
Chris Lattner24943d22010-06-08 16:52:24 +00001869 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham3ae449a2010-11-17 02:32:00 +00001870 continue;
Chris Lattner24943d22010-06-08 16:52:24 +00001871 }
1872
1873
1874 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1875
1876 if (internal_state != eStateInvalid)
1877 {
1878 HandlePrivateEvent (event_sp);
1879 }
1880
Greg Clayton3b2c41c2010-10-18 04:14:23 +00001881 if (internal_state == eStateInvalid ||
1882 internal_state == eStateExited ||
1883 internal_state == eStateDetached )
Jim Ingham3ae449a2010-11-17 02:32:00 +00001884 {
1885 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
1886 if (log)
1887 log->Printf ("Process::%s (arg = %p, pid = %i) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state));
1888
Chris Lattner24943d22010-06-08 16:52:24 +00001889 break;
Jim Ingham3ae449a2010-11-17 02:32:00 +00001890 }
Chris Lattner24943d22010-06-08 16:52:24 +00001891 }
1892
Caroline Tice926060e2010-10-29 21:48:37 +00001893 // Verify log is still enabled before attempting to write to it...
1894 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
Chris Lattner24943d22010-06-08 16:52:24 +00001895 if (log)
1896 log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID());
1897
Greg Clayton8b4c16e2010-08-19 21:50:06 +00001898 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00001899 return NULL;
1900}
1901
Chris Lattner24943d22010-06-08 16:52:24 +00001902//------------------------------------------------------------------
1903// Process Event Data
1904//------------------------------------------------------------------
1905
1906Process::ProcessEventData::ProcessEventData () :
1907 EventData (),
1908 m_process_sp (),
1909 m_state (eStateInvalid),
Greg Clayton54e7afa2010-07-09 20:39:50 +00001910 m_restarted (false),
Jim Ingham3ae449a2010-11-17 02:32:00 +00001911 m_update_state (false),
1912 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00001913{
1914}
1915
1916Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
1917 EventData (),
1918 m_process_sp (process_sp),
1919 m_state (state),
Greg Clayton54e7afa2010-07-09 20:39:50 +00001920 m_restarted (false),
Jim Ingham3ae449a2010-11-17 02:32:00 +00001921 m_update_state (false),
1922 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00001923{
1924}
1925
1926Process::ProcessEventData::~ProcessEventData()
1927{
1928}
1929
1930const ConstString &
1931Process::ProcessEventData::GetFlavorString ()
1932{
1933 static ConstString g_flavor ("Process::ProcessEventData");
1934 return g_flavor;
1935}
1936
1937const ConstString &
1938Process::ProcessEventData::GetFlavor () const
1939{
1940 return ProcessEventData::GetFlavorString ();
1941}
1942
Chris Lattner24943d22010-06-08 16:52:24 +00001943void
1944Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
1945{
1946 // This function gets called twice for each event, once when the event gets pulled
1947 // off of the private process event queue, and once when it gets pulled off of
1948 // the public event queue. m_update_state is used to distinguish these
1949 // two cases; it is false when we're just pulling it off for private handling,
1950 // and we don't want to do the breakpoint command handling then.
1951
1952 if (!m_update_state)
1953 return;
1954
1955 m_process_sp->SetPublicState (m_state);
1956
1957 // If we're stopped and haven't restarted, then do the breakpoint commands here:
1958 if (m_state == eStateStopped && ! m_restarted)
1959 {
1960 int num_threads = m_process_sp->GetThreadList().GetSize();
1961 int idx;
Greg Clayton643ee732010-08-04 01:40:35 +00001962
Chris Lattner24943d22010-06-08 16:52:24 +00001963 for (idx = 0; idx < num_threads; ++idx)
1964 {
1965 lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx);
1966
Jim Ingham6297a3a2010-10-20 00:39:53 +00001967 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
1968 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001969 {
Jim Ingham6297a3a2010-10-20 00:39:53 +00001970 stop_info_sp->PerformAction(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +00001971 }
1972 }
Greg Clayton643ee732010-08-04 01:40:35 +00001973
Jim Ingham6fb8baa2010-08-10 00:59:59 +00001974 // The stop action might restart the target. If it does, then we want to mark that in the
1975 // event so that whoever is receiving it will know to wait for the running event and reflect
1976 // that state appropriately.
1977
Chris Lattner24943d22010-06-08 16:52:24 +00001978 if (m_process_sp->GetPrivateState() == eStateRunning)
1979 SetRestarted(true);
1980 }
1981}
1982
1983void
1984Process::ProcessEventData::Dump (Stream *s) const
1985{
1986 if (m_process_sp)
1987 s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID());
1988
1989 s->Printf("state = %s", StateAsCString(GetState()));;
1990}
1991
1992const Process::ProcessEventData *
1993Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
1994{
1995 if (event_ptr)
1996 {
1997 const EventData *event_data = event_ptr->GetData();
1998 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
1999 return static_cast <const ProcessEventData *> (event_ptr->GetData());
2000 }
2001 return NULL;
2002}
2003
2004ProcessSP
2005Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
2006{
2007 ProcessSP process_sp;
2008 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2009 if (data)
2010 process_sp = data->GetProcessSP();
2011 return process_sp;
2012}
2013
2014StateType
2015Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
2016{
2017 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2018 if (data == NULL)
2019 return eStateInvalid;
2020 else
2021 return data->GetState();
2022}
2023
2024bool
2025Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
2026{
2027 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2028 if (data == NULL)
2029 return false;
2030 else
2031 return data->GetRestarted();
2032}
2033
2034void
2035Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
2036{
2037 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2038 if (data != NULL)
2039 data->SetRestarted(new_value);
2040}
2041
2042bool
Jim Ingham3ae449a2010-11-17 02:32:00 +00002043Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
2044{
2045 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2046 if (data == NULL)
2047 return false;
2048 else
2049 return data->GetInterrupted ();
2050}
2051
2052void
2053Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
2054{
2055 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2056 if (data != NULL)
2057 data->SetInterrupted(new_value);
2058}
2059
2060bool
Chris Lattner24943d22010-06-08 16:52:24 +00002061Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
2062{
2063 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2064 if (data)
2065 {
2066 data->SetUpdateStateOnRemoval();
2067 return true;
2068 }
2069 return false;
2070}
2071
Chris Lattner24943d22010-06-08 16:52:24 +00002072Target *
2073Process::CalculateTarget ()
2074{
2075 return &m_target;
2076}
2077
2078Process *
2079Process::CalculateProcess ()
2080{
2081 return this;
2082}
2083
2084Thread *
2085Process::CalculateThread ()
2086{
2087 return NULL;
2088}
2089
2090StackFrame *
2091Process::CalculateStackFrame ()
2092{
2093 return NULL;
2094}
2095
2096void
Greg Claytona830adb2010-10-04 01:05:56 +00002097Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00002098{
2099 exe_ctx.target = &m_target;
2100 exe_ctx.process = this;
2101 exe_ctx.thread = NULL;
2102 exe_ctx.frame = NULL;
2103}
2104
2105lldb::ProcessSP
2106Process::GetSP ()
2107{
2108 return GetTarget().GetProcessSP();
2109}
2110
Sean Callanana48fe162010-08-11 03:57:18 +00002111ClangPersistentVariables &
2112Process::GetPersistentVariables()
2113{
2114 return m_persistent_vars;
2115}
2116
Jim Ingham7508e732010-08-09 23:31:02 +00002117uint32_t
2118Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2119{
2120 return 0;
2121}
2122
2123ArchSpec
2124Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
2125{
2126 return Host::GetArchSpecForExistingProcess (pid);
2127}
2128
2129ArchSpec
2130Process::GetArchSpecForExistingProcess (const char *process_name)
2131{
2132 return Host::GetArchSpecForExistingProcess (process_name);
2133}
2134
Caroline Tice861efb32010-11-16 05:07:41 +00002135void
2136Process::AppendSTDOUT (const char * s, size_t len)
2137{
Greg Clayton20d338f2010-11-18 05:57:03 +00002138 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Tice861efb32010-11-16 05:07:41 +00002139 m_stdout_data.append (s, len);
2140
2141 BroadcastEventIfUnique (eBroadcastBitSTDOUT);
2142}
2143
2144void
2145Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
2146{
2147 Process *process = (Process *) baton;
2148 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
2149}
2150
2151size_t
2152Process::ProcessInputReaderCallback (void *baton,
2153 InputReader &reader,
2154 lldb::InputReaderAction notification,
2155 const char *bytes,
2156 size_t bytes_len)
2157{
2158 Process *process = (Process *) baton;
2159
2160 switch (notification)
2161 {
2162 case eInputReaderActivate:
2163 break;
2164
2165 case eInputReaderDeactivate:
2166 break;
2167
2168 case eInputReaderReactivate:
2169 break;
2170
2171 case eInputReaderGotToken:
2172 {
2173 Error error;
2174 process->PutSTDIN (bytes, bytes_len, error);
2175 }
2176 break;
2177
2178 case eInputReaderDone:
2179 break;
2180
2181 }
2182
2183 return bytes_len;
2184}
2185
2186void
2187Process::ResetProcessInputReader ()
2188{
2189 m_process_input_reader.reset();
2190}
2191
2192void
2193Process::SetUpProcessInputReader (int file_descriptor)
2194{
2195 // First set up the Read Thread for reading/handling process I/O
2196
2197 std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true));
2198
2199 if (conn_ap.get())
2200 {
2201 m_stdio_communication.SetConnection (conn_ap.release());
2202 if (m_stdio_communication.IsConnected())
2203 {
2204 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
2205 m_stdio_communication.StartReadThread();
2206
2207 // Now read thread is set up, set up input reader.
2208
2209 if (!m_process_input_reader.get())
2210 {
2211 m_process_input_reader.reset (new InputReader(m_target.GetDebugger()));
2212 Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback,
2213 this,
2214 eInputReaderGranularityByte,
2215 NULL,
2216 NULL,
2217 false));
2218
2219 if (err.Fail())
2220 m_process_input_reader.reset();
2221 }
2222 }
2223 }
2224}
2225
2226void
2227Process::PushProcessInputReader ()
2228{
2229 if (m_process_input_reader && !m_process_input_reader->IsActive())
2230 m_target.GetDebugger().PushInputReader (m_process_input_reader);
2231}
2232
2233void
2234Process::PopProcessInputReader ()
2235{
2236 if (m_process_input_reader && m_process_input_reader->IsActive())
2237 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2238}
2239
Greg Clayton990de7b2010-11-18 23:32:35 +00002240
2241void
2242Process::Initialize ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002243{
Greg Clayton990de7b2010-11-18 23:32:35 +00002244 UserSettingsControllerSP &usc = GetSettingsController();
2245 usc.reset (new SettingsController);
2246 UserSettingsController::InitializeSettingsController (usc,
2247 SettingsController::global_settings_table,
2248 SettingsController::instance_settings_table);
2249}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002250
Greg Clayton990de7b2010-11-18 23:32:35 +00002251void
2252Process::Terminate ()
2253{
2254 UserSettingsControllerSP &usc = GetSettingsController();
2255 UserSettingsController::FinalizeSettingsController (usc);
2256 usc.reset();
2257}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002258
Greg Clayton990de7b2010-11-18 23:32:35 +00002259UserSettingsControllerSP &
2260Process::GetSettingsController ()
2261{
2262 static UserSettingsControllerSP g_settings_controller;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002263 return g_settings_controller;
2264}
2265
Caroline Tice1ebef442010-09-27 00:30:10 +00002266void
2267Process::UpdateInstanceName ()
2268{
2269 ModuleSP module_sp = GetTarget().GetExecutableModule();
2270 if (module_sp)
2271 {
2272 StreamString sstr;
2273 sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString());
2274
2275 Process::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
2276 sstr.GetData());
2277 }
2278}
2279
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002280//--------------------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00002281// class Process::SettingsController
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002282//--------------------------------------------------------------
2283
Greg Claytond0a5a232010-09-19 02:33:57 +00002284Process::SettingsController::SettingsController () :
Caroline Tice5bc8c972010-09-20 20:44:43 +00002285 UserSettingsController ("process", Target::GetSettingsController())
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002286{
Caroline Tice004afcb2010-09-08 17:48:55 +00002287 m_default_settings.reset (new ProcessInstanceSettings (*this, false,
2288 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002289}
2290
Greg Claytond0a5a232010-09-19 02:33:57 +00002291Process::SettingsController::~SettingsController ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002292{
2293}
2294
2295lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +00002296Process::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002297{
Caroline Tice004afcb2010-09-08 17:48:55 +00002298 ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*(Process::GetSettingsController().get()),
2299 false, instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002300 lldb::InstanceSettingsSP new_settings_sp (new_settings);
2301 return new_settings_sp;
2302}
2303
2304//--------------------------------------------------------------
2305// class ProcessInstanceSettings
2306//--------------------------------------------------------------
2307
Caroline Tice004afcb2010-09-08 17:48:55 +00002308ProcessInstanceSettings::ProcessInstanceSettings (UserSettingsController &owner, bool live_instance,
2309 const char *name) :
Caroline Tice75b11a32010-09-16 19:05:55 +00002310 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance),
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002311 m_run_args (),
2312 m_env_vars (),
2313 m_input_path (),
2314 m_output_path (),
2315 m_error_path (),
2316 m_plugin (),
2317 m_disable_aslr (true)
2318{
Caroline Tice396704b2010-09-09 18:26:37 +00002319 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2320 // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers.
2321 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
Caroline Tice75b11a32010-09-16 19:05:55 +00002322 // This is true for CreateInstanceName() too.
2323
2324 if (GetInstanceName () == InstanceSettings::InvalidName())
2325 {
2326 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2327 m_owner.RegisterInstanceSettings (this);
2328 }
Caroline Tice396704b2010-09-09 18:26:37 +00002329
2330 if (live_instance)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002331 {
2332 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2333 CopyInstanceSettings (pending_settings,false);
Caroline Tice396704b2010-09-09 18:26:37 +00002334 //m_owner.RemovePendingSettings (m_instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002335 }
2336}
2337
2338ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) :
2339 InstanceSettings (*(Process::GetSettingsController().get()), CreateInstanceName().AsCString()),
2340 m_run_args (rhs.m_run_args),
2341 m_env_vars (rhs.m_env_vars),
2342 m_input_path (rhs.m_input_path),
2343 m_output_path (rhs.m_output_path),
2344 m_error_path (rhs.m_error_path),
2345 m_plugin (rhs.m_plugin),
2346 m_disable_aslr (rhs.m_disable_aslr)
2347{
2348 if (m_instance_name != InstanceSettings::GetDefaultName())
2349 {
2350 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2351 CopyInstanceSettings (pending_settings,false);
2352 m_owner.RemovePendingSettings (m_instance_name);
2353 }
2354}
2355
2356ProcessInstanceSettings::~ProcessInstanceSettings ()
2357{
2358}
2359
2360ProcessInstanceSettings&
2361ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs)
2362{
2363 if (this != &rhs)
2364 {
2365 m_run_args = rhs.m_run_args;
2366 m_env_vars = rhs.m_env_vars;
2367 m_input_path = rhs.m_input_path;
2368 m_output_path = rhs.m_output_path;
2369 m_error_path = rhs.m_error_path;
2370 m_plugin = rhs.m_plugin;
2371 m_disable_aslr = rhs.m_disable_aslr;
2372 }
2373
2374 return *this;
2375}
2376
2377
2378void
2379ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2380 const char *index_value,
2381 const char *value,
2382 const ConstString &instance_name,
2383 const SettingEntry &entry,
2384 lldb::VarSetOperationType op,
2385 Error &err,
2386 bool pending)
2387{
2388 if (var_name == RunArgsVarName())
2389 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2390 else if (var_name == EnvVarsVarName())
2391 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2392 else if (var_name == InputPathVarName())
2393 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2394 else if (var_name == OutputPathVarName())
2395 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2396 else if (var_name == ErrorPathVarName())
2397 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2398 else if (var_name == PluginVarName())
2399 UserSettingsController::UpdateEnumVariable (entry.enum_values, (int *) &m_plugin, value, err);
2400 else if (var_name == DisableASLRVarName())
2401 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err);
2402}
2403
2404void
2405ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
2406 bool pending)
2407{
2408 if (new_settings.get() == NULL)
2409 return;
2410
2411 ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get();
2412
2413 m_run_args = new_process_settings->m_run_args;
2414 m_env_vars = new_process_settings->m_env_vars;
2415 m_input_path = new_process_settings->m_input_path;
2416 m_output_path = new_process_settings->m_output_path;
2417 m_error_path = new_process_settings->m_error_path;
2418 m_plugin = new_process_settings->m_plugin;
2419 m_disable_aslr = new_process_settings->m_disable_aslr;
2420}
2421
Caroline Ticebcb5b452010-09-20 21:37:42 +00002422bool
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002423ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2424 const ConstString &var_name,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002425 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002426 Error *err)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002427{
2428 if (var_name == RunArgsVarName())
2429 {
2430 if (m_run_args.GetArgumentCount() > 0)
Greg Claytonc14069e2010-09-14 03:47:41 +00002431 {
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002432 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2433 value.AppendString (m_run_args.GetArgumentAtIndex (i));
Greg Claytonc14069e2010-09-14 03:47:41 +00002434 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002435 }
2436 else if (var_name == EnvVarsVarName())
2437 {
2438 if (m_env_vars.size() > 0)
2439 {
2440 std::map<std::string, std::string>::iterator pos;
2441 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2442 {
2443 StreamString value_str;
2444 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2445 value.AppendString (value_str.GetData());
2446 }
2447 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002448 }
2449 else if (var_name == InputPathVarName())
2450 {
2451 value.AppendString (m_input_path.c_str());
2452 }
2453 else if (var_name == OutputPathVarName())
2454 {
2455 value.AppendString (m_output_path.c_str());
2456 }
2457 else if (var_name == ErrorPathVarName())
2458 {
2459 value.AppendString (m_error_path.c_str());
2460 }
2461 else if (var_name == PluginVarName())
2462 {
2463 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, (int) m_plugin));
2464 }
2465 else if (var_name == DisableASLRVarName())
2466 {
2467 if (m_disable_aslr)
2468 value.AppendString ("true");
2469 else
2470 value.AppendString ("false");
2471 }
2472 else
Caroline Ticebcb5b452010-09-20 21:37:42 +00002473 {
2474 if (err)
2475 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2476 return false;
2477 }
2478 return true;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002479}
2480
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002481const ConstString
2482ProcessInstanceSettings::CreateInstanceName ()
2483{
2484 static int instance_count = 1;
2485 StreamString sstr;
2486
2487 sstr.Printf ("process_%d", instance_count);
2488 ++instance_count;
2489
2490 const ConstString ret_val (sstr.GetData());
2491 return ret_val;
2492}
2493
2494const ConstString &
2495ProcessInstanceSettings::RunArgsVarName ()
2496{
2497 static ConstString run_args_var_name ("run-args");
2498
2499 return run_args_var_name;
2500}
2501
2502const ConstString &
2503ProcessInstanceSettings::EnvVarsVarName ()
2504{
2505 static ConstString env_vars_var_name ("env-vars");
2506
2507 return env_vars_var_name;
2508}
2509
2510const ConstString &
2511ProcessInstanceSettings::InputPathVarName ()
2512{
2513 static ConstString input_path_var_name ("input-path");
2514
2515 return input_path_var_name;
2516}
2517
2518const ConstString &
2519ProcessInstanceSettings::OutputPathVarName ()
2520{
Caroline Tice87097232010-09-07 18:35:40 +00002521 static ConstString output_path_var_name ("output-path");
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002522
2523 return output_path_var_name;
2524}
2525
2526const ConstString &
2527ProcessInstanceSettings::ErrorPathVarName ()
2528{
Caroline Tice87097232010-09-07 18:35:40 +00002529 static ConstString error_path_var_name ("error-path");
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002530
2531 return error_path_var_name;
2532}
2533
2534const ConstString &
2535ProcessInstanceSettings::PluginVarName ()
2536{
2537 static ConstString plugin_var_name ("plugin");
2538
2539 return plugin_var_name;
2540}
2541
2542
2543const ConstString &
2544ProcessInstanceSettings::DisableASLRVarName ()
2545{
2546 static ConstString disable_aslr_var_name ("disable-aslr");
2547
2548 return disable_aslr_var_name;
2549}
2550
2551
2552//--------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00002553// SettingsController Variable Tables
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002554//--------------------------------------------------
2555
2556SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00002557Process::SettingsController::global_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002558{
2559 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
2560 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2561};
2562
2563
2564lldb::OptionEnumValueElement
Greg Claytond0a5a232010-09-19 02:33:57 +00002565Process::SettingsController::g_plugins[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002566{
Caroline Ticef2c330d2010-09-09 18:01:59 +00002567 { eMacosx, "process.macosx", "Use the native MacOSX debugger plugin" },
2568 { eRemoteDebugger, "process.gdb-remote" , "Use the GDB Remote protocol based debugger plugin" },
2569 { 0, NULL, NULL }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002570};
2571
2572SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00002573Process::SettingsController::instance_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002574{
2575 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
2576 { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2577 { "env-vars", eSetVarTypeDictionary, NULL, NULL, false, false, "A list of all the environment variables to be passed to the executable's environment, and their values." },
2578 { "input-path", eSetVarTypeString, "/dev/stdin", NULL, false, false, "The file/path to be used by the executable program for reading its input." },
2579 { "output-path", eSetVarTypeString, "/dev/stdout", NULL, false, false, "The file/path to be used by the executable program for writing its output." },
2580 { "error-path", eSetVarTypeString, "/dev/stderr", NULL, false, false, "The file/path to be used by the executable program for writings its error messages." },
2581 { "plugin", eSetVarTypeEnum, NULL , g_plugins, false, false, "The plugin to be used to run the process." },
Jim Ingham745ac7a2010-11-11 19:26:09 +00002582 { "disable-aslr", eSetVarTypeBoolean, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002583 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2584};
2585
2586
Jim Ingham7508e732010-08-09 23:31:02 +00002587