blob: c86b74cc77f93b6a01ae48626bb7d9c827776b1c [file] [log] [blame]
Chris Lattner30fdc8d2010-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"
17#include "lldb/Core/Debugger.h"
18#include "lldb/Core/Log.h"
19#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/State.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000021#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Host/Host.h"
23#include "lldb/Target/ABI.h"
24#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000025#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/Target.h"
27#include "lldb/Target/TargetList.h"
28#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlan.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34Process*
35Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener)
36{
37 ProcessCreateInstance create_callback = NULL;
38 if (plugin_name)
39 {
40 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name);
41 if (create_callback)
42 {
43 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
44 if (debugger_ap->CanDebug(target))
45 return debugger_ap.release();
46 }
47 }
48 else
49 {
Greg Claytonc982c762010-07-09 20:39:50 +000050 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 {
Greg Claytonc982c762010-07-09 20:39:50 +000052 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
53 if (debugger_ap->CanDebug(target))
54 return debugger_ap.release();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055 }
56 }
57 return NULL;
58}
59
60
61//----------------------------------------------------------------------
62// Process constructor
63//----------------------------------------------------------------------
64Process::Process(Target &target, Listener &listener) :
65 UserID (LLDB_INVALID_PROCESS_ID),
66 Broadcaster ("Process"),
Caroline Tice3df9a8d2010-09-04 00:03:46 +000067 ProcessInstanceSettings (*(Process::GetSettingsController().get())),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068 m_target (target),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 m_public_state (eStateUnloaded),
70 m_private_state (eStateUnloaded),
71 m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"),
72 m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"),
73 m_private_state_listener ("lldb.process.internal_state_listener"),
74 m_private_state_control_wait(),
75 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
76 m_stop_id (0),
77 m_thread_index_id (0),
78 m_exit_status (-1),
79 m_exit_string (),
80 m_thread_list (this),
81 m_notifications (),
82 m_listener(listener),
83 m_unix_signals (),
Sean Callanan2235f322010-08-11 03:57:18 +000084 m_objc_object_printer(*this),
85 m_persistent_vars()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086{
87 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
88 if (log)
89 log->Printf ("%p Process::Process()", this);
90
91 listener.StartListeningForEvents (this,
92 eBroadcastBitStateChanged |
93 eBroadcastBitInterrupt |
94 eBroadcastBitSTDOUT |
95 eBroadcastBitSTDERR);
96
97 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
98 eBroadcastBitStateChanged);
99
100 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
101 eBroadcastInternalStateControlStop |
102 eBroadcastInternalStateControlPause |
103 eBroadcastInternalStateControlResume);
104}
105
106//----------------------------------------------------------------------
107// Destructor
108//----------------------------------------------------------------------
109Process::~Process()
110{
111 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
112 if (log)
113 log->Printf ("%p Process::~Process()", this);
114 StopPrivateStateThread();
115}
116
117void
118Process::Finalize()
119{
120 // Do any cleanup needed prior to being destructed... Subclasses
121 // that override this method should call this superclass method as well.
122}
123
124void
125Process::RegisterNotificationCallbacks (const Notifications& callbacks)
126{
127 m_notifications.push_back(callbacks);
128 if (callbacks.initialize != NULL)
129 callbacks.initialize (callbacks.baton, this);
130}
131
132bool
133Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
134{
135 std::vector<Notifications>::iterator pos, end = m_notifications.end();
136 for (pos = m_notifications.begin(); pos != end; ++pos)
137 {
138 if (pos->baton == callbacks.baton &&
139 pos->initialize == callbacks.initialize &&
140 pos->process_state_changed == callbacks.process_state_changed)
141 {
142 m_notifications.erase(pos);
143 return true;
144 }
145 }
146 return false;
147}
148
149void
150Process::SynchronouslyNotifyStateChanged (StateType state)
151{
152 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
153 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
154 {
155 if (notification_pos->process_state_changed)
156 notification_pos->process_state_changed (notification_pos->baton, this, state);
157 }
158}
159
160// FIXME: We need to do some work on events before the general Listener sees them.
161// For instance if we are continuing from a breakpoint, we need to ensure that we do
162// the little "insert real insn, step & stop" trick. But we can't do that when the
163// event is delivered by the broadcaster - since that is done on the thread that is
164// waiting for new events, so if we needed more than one event for our handling, we would
165// stall. So instead we do it when we fetch the event off of the queue.
166//
167
168StateType
169Process::GetNextEvent (EventSP &event_sp)
170{
171 StateType state = eStateInvalid;
172
173 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
174 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
175
176 return state;
177}
178
179
180StateType
181Process::WaitForProcessToStop (const TimeValue *timeout)
182{
183 StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded };
184 return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType));
185}
186
187
188StateType
189Process::WaitForState
190(
191 const TimeValue *timeout,
192 const StateType *match_states, const uint32_t num_match_states
193)
194{
195 EventSP event_sp;
196 uint32_t i;
197 StateType state = eStateUnloaded;
198 while (state != eStateInvalid)
199 {
200 state = WaitForStateChangedEvents (timeout, event_sp);
201
202 for (i=0; i<num_match_states; ++i)
203 {
204 if (match_states[i] == state)
205 return state;
206 }
207 }
208 return state;
209}
210
211StateType
212Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
213{
214 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
215
216 if (log)
217 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
218
219 StateType state = eStateInvalid;
220 if (m_listener.WaitForEventForBroadcasterWithType(timeout,
221 this,
222 eBroadcastBitStateChanged,
223 event_sp))
224 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
225
226 if (log)
227 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
228 __FUNCTION__,
229 timeout,
230 StateAsCString(state));
231 return state;
232}
233
234Event *
235Process::PeekAtStateChangedEvents ()
236{
237 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
238
239 if (log)
240 log->Printf ("Process::%s...", __FUNCTION__);
241
242 Event *event_ptr;
243 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType(this,
244 eBroadcastBitStateChanged);
245 if (log)
246 {
247 if (event_ptr)
248 {
249 log->Printf ("Process::%s (event_ptr) => %s",
250 __FUNCTION__,
251 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
252 }
253 else
254 {
255 log->Printf ("Process::%s no events found",
256 __FUNCTION__);
257 }
258 }
259 return event_ptr;
260}
261
262StateType
263Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
264{
265 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
266
267 if (log)
268 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
269
270 StateType state = eStateInvalid;
271 if (m_private_state_listener.WaitForEventForBroadcasterWithType(timeout,
272 &m_private_state_broadcaster,
273 eBroadcastBitStateChanged,
274 event_sp))
275 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
276
277 // This is a bit of a hack, but when we wait here we could very well return
278 // to the command-line, and that could disable the log, which would render the
279 // log we got above invalid.
280 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
281 if (log)
282 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
283 return state;
284}
285
286bool
287Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
288{
289 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
290
291 if (log)
292 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
293
294 if (control_only)
295 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
296 else
297 return m_private_state_listener.WaitForEvent(timeout, event_sp);
298}
299
300bool
301Process::IsRunning () const
302{
303 return StateIsRunningState (m_public_state.GetValue());
304}
305
306int
307Process::GetExitStatus ()
308{
309 if (m_public_state.GetValue() == eStateExited)
310 return m_exit_status;
311 return -1;
312}
313
314const char *
315Process::GetExitDescription ()
316{
317 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
318 return m_exit_string.c_str();
319 return NULL;
320}
321
322void
323Process::SetExitStatus (int status, const char *cstr)
324{
325 m_exit_status = status;
326 if (cstr)
327 m_exit_string = cstr;
328 else
329 m_exit_string.clear();
330
331 SetPrivateState (eStateExited);
332}
333
334// This static callback can be used to watch for local child processes on
335// the current host. The the child process exits, the process will be
336// found in the global target list (we want to be completely sure that the
337// lldb_private::Process doesn't go away before we can deliver the signal.
338bool
339Process::SetProcessExitStatus
340(
341 void *callback_baton,
342 lldb::pid_t pid,
343 int signo, // Zero for no signal
344 int exit_status // Exit value of process if signal is zero
345)
346{
347 if (signo == 0 || exit_status)
348 {
Greg Clayton66111032010-06-23 01:19:29 +0000349 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 if (target_sp)
351 {
352 ProcessSP process_sp (target_sp->GetProcessSP());
353 if (process_sp)
354 {
355 const char *signal_cstr = NULL;
356 if (signo)
357 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
358
359 process_sp->SetExitStatus (exit_status, signal_cstr);
360 }
361 }
362 return true;
363 }
364 return false;
365}
366
367
368uint32_t
369Process::GetNextThreadIndexID ()
370{
371 return ++m_thread_index_id;
372}
373
374StateType
375Process::GetState()
376{
377 // If any other threads access this we will need a mutex for it
378 return m_public_state.GetValue ();
379}
380
381void
382Process::SetPublicState (StateType new_state)
383{
384 Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE);
385 if (log)
386 log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state));
387 m_public_state.SetValue (new_state);
388}
389
390StateType
391Process::GetPrivateState ()
392{
393 return m_private_state.GetValue();
394}
395
396void
397Process::SetPrivateState (StateType new_state)
398{
399 Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE);
400 bool state_changed = false;
401
402 if (log)
403 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
404
405 Mutex::Locker locker(m_private_state.GetMutex());
406
407 const StateType old_state = m_private_state.GetValueNoLock ();
408 state_changed = old_state != new_state;
409 if (state_changed)
410 {
411 m_private_state.SetValueNoLock (new_state);
412 if (StateIsStoppedState(new_state))
413 {
414 m_stop_id++;
415 if (log)
416 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id);
417 }
418 // Use our target to get a shared pointer to ourselves...
419 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state));
420 }
421 else
422 {
423 if (log)
424 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state));
425 }
426}
427
428
429uint32_t
430Process::GetStopID() const
431{
432 return m_stop_id;
433}
434
435addr_t
436Process::GetImageInfoAddress()
437{
438 return LLDB_INVALID_ADDRESS;
439}
440
441DynamicLoader *
442Process::GetDynamicLoader()
443{
444 return NULL;
445}
446
447const ABI *
448Process::GetABI()
449{
450 ConstString& triple = m_target_triple;
451
452 if (triple.IsEmpty())
453 return NULL;
454
455 if (m_abi_sp.get() == NULL)
456 {
457 m_abi_sp.reset(ABI::FindPlugin(triple));
458 }
459
460 return m_abi_sp.get();
461}
462
463BreakpointSiteList &
464Process::GetBreakpointSiteList()
465{
466 return m_breakpoint_site_list;
467}
468
469const BreakpointSiteList &
470Process::GetBreakpointSiteList() const
471{
472 return m_breakpoint_site_list;
473}
474
475
476void
477Process::DisableAllBreakpointSites ()
478{
479 m_breakpoint_site_list.SetEnabledForAll (false);
480}
481
482Error
483Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
484{
485 Error error (DisableBreakpointSiteByID (break_id));
486
487 if (error.Success())
488 m_breakpoint_site_list.Remove(break_id);
489
490 return error;
491}
492
493Error
494Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
495{
496 Error error;
497 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
498 if (bp_site_sp)
499 {
500 if (bp_site_sp->IsEnabled())
501 error = DisableBreakpoint (bp_site_sp.get());
502 }
503 else
504 {
505 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
506 }
507
508 return error;
509}
510
511Error
512Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
513{
514 Error error;
515 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
516 if (bp_site_sp)
517 {
518 if (!bp_site_sp->IsEnabled())
519 error = EnableBreakpoint (bp_site_sp.get());
520 }
521 else
522 {
523 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
524 }
525 return error;
526}
527
Stephen Wilson50bd94f2010-07-17 00:56:13 +0000528lldb::break_id_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware)
530{
Greg Claytonf5e56de2010-09-14 23:36:40 +0000531 const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 if (load_addr != LLDB_INVALID_ADDRESS)
533 {
534 BreakpointSiteSP bp_site_sp;
535
536 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
537 // create a new breakpoint site and add it.
538
539 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
540
541 if (bp_site_sp)
542 {
543 bp_site_sp->AddOwner (owner);
544 owner->SetBreakpointSite (bp_site_sp);
545 return bp_site_sp->GetID();
546 }
547 else
548 {
549 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware));
550 if (bp_site_sp)
551 {
552 if (EnableBreakpoint (bp_site_sp.get()).Success())
553 {
554 owner->SetBreakpointSite (bp_site_sp);
555 return m_breakpoint_site_list.Add (bp_site_sp);
556 }
557 }
558 }
559 }
560 // We failed to enable the breakpoint
561 return LLDB_INVALID_BREAK_ID;
562
563}
564
565void
566Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
567{
568 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
569 if (num_owners == 0)
570 {
571 DisableBreakpoint(bp_site_sp.get());
572 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
573 }
574}
575
576
577size_t
578Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
579{
580 size_t bytes_removed = 0;
581 addr_t intersect_addr;
582 size_t intersect_size;
583 size_t opcode_offset;
584 size_t idx;
585 BreakpointSiteSP bp;
586
587 for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx)
588 {
589 if (bp->GetType() == BreakpointSite::eSoftware)
590 {
591 if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
592 {
593 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
594 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
595 assert(opcode_offset + intersect_size <= bp->GetByteSize());
596 size_t buf_offset = intersect_addr - bp_addr;
597 ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
598 }
599 }
600 }
601 return bytes_removed;
602}
603
604
605Error
606Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
607{
608 Error error;
609 assert (bp_site != NULL);
610 Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
611 const addr_t bp_addr = bp_site->GetLoadAddress();
612 if (log)
613 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr);
614 if (bp_site->IsEnabled())
615 {
616 if (log)
617 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
618 return error;
619 }
620
621 if (bp_addr == LLDB_INVALID_ADDRESS)
622 {
623 error.SetErrorString("BreakpointSite contains an invalid load address.");
624 return error;
625 }
626 // Ask the lldb::Process subclass to fill in the correct software breakpoint
627 // trap for the breakpoint site
628 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
629
630 if (bp_opcode_size == 0)
631 {
632 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr);
633 }
634 else
635 {
636 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
637
638 if (bp_opcode_bytes == NULL)
639 {
640 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
641 return error;
642 }
643
644 // Save the original opcode by reading it
645 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
646 {
647 // Write a software breakpoint in place of the original opcode
648 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
649 {
650 uint8_t verify_bp_opcode_bytes[64];
651 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
652 {
653 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
654 {
655 bp_site->SetEnabled(true);
656 bp_site->SetType (BreakpointSite::eSoftware);
657 if (log)
658 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS",
659 bp_site->GetID(),
660 (uint64_t)bp_addr);
661 }
662 else
663 error.SetErrorString("Failed to verify the breakpoint trap in memory.");
664 }
665 else
666 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
667 }
668 else
669 error.SetErrorString("Unable to write breakpoint trap to memory.");
670 }
671 else
672 error.SetErrorString("Unable to read memory at breakpoint address.");
673 }
674 if (log)
675 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
676 bp_site->GetID(),
677 (uint64_t)bp_addr,
678 error.AsCString());
679 return error;
680}
681
682Error
683Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
684{
685 Error error;
686 assert (bp_site != NULL);
687 Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
688 addr_t bp_addr = bp_site->GetLoadAddress();
689 lldb::user_id_t breakID = bp_site->GetID();
690 if (log)
691 log->Printf ("ProcessMacOSX::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr);
692
693 if (bp_site->IsHardware())
694 {
695 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
696 }
697 else if (bp_site->IsEnabled())
698 {
699 const size_t break_op_size = bp_site->GetByteSize();
700 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
701 if (break_op_size > 0)
702 {
703 // Clear a software breakoint instruction
Greg Claytonc982c762010-07-09 20:39:50 +0000704 uint8_t curr_break_op[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +0000705 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 bool break_op_found = false;
707
708 // Read the breakpoint opcode
709 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
710 {
711 bool verify = false;
712 // Make sure we have the a breakpoint opcode exists at this address
713 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
714 {
715 break_op_found = true;
716 // We found a valid breakpoint opcode at this address, now restore
717 // the saved opcode.
718 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
719 {
720 verify = true;
721 }
722 else
723 error.SetErrorString("Memory write failed when restoring original opcode.");
724 }
725 else
726 {
727 error.SetErrorString("Original breakpoint trap is no longer in memory.");
728 // Set verify to true and so we can check if the original opcode has already been restored
729 verify = true;
730 }
731
732 if (verify)
733 {
Greg Claytonc982c762010-07-09 20:39:50 +0000734 uint8_t verify_opcode[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +0000735 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 // Verify that our original opcode made it back to the inferior
737 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
738 {
739 // compare the memory we just read with the original opcode
740 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
741 {
742 // SUCCESS
743 bp_site->SetEnabled(false);
744 if (log)
745 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
746 return error;
747 }
748 else
749 {
750 if (break_op_found)
751 error.SetErrorString("Failed to restore original opcode.");
752 }
753 }
754 else
755 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
756 }
757 }
758 else
759 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
760 }
761 }
762 else
763 {
764 if (log)
765 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
766 return error;
767 }
768
769 if (log)
770 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
771 bp_site->GetID(),
772 (uint64_t)bp_addr,
773 error.AsCString());
774 return error;
775
776}
777
778
779size_t
780Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
781{
782 if (buf == NULL || size == 0)
783 return 0;
784
785 size_t bytes_read = 0;
786 uint8_t *bytes = (uint8_t *)buf;
787
788 while (bytes_read < size)
789 {
790 const size_t curr_size = size - bytes_read;
791 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
792 bytes + bytes_read,
793 curr_size,
794 error);
795 bytes_read += curr_bytes_read;
796 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
797 break;
798 }
799
800 // Replace any software breakpoint opcodes that fall into this range back
801 // into "buf" before we return
802 if (bytes_read > 0)
803 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
804 return bytes_read;
805}
806
807size_t
808Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
809{
810 size_t bytes_written = 0;
811 const uint8_t *bytes = (const uint8_t *)buf;
812
813 while (bytes_written < size)
814 {
815 const size_t curr_size = size - bytes_written;
816 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
817 bytes + bytes_written,
818 curr_size,
819 error);
820 bytes_written += curr_bytes_written;
821 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
822 break;
823 }
824 return bytes_written;
825}
826
827size_t
828Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
829{
830 if (buf == NULL || size == 0)
831 return 0;
832 // We need to write any data that would go where any current software traps
833 // (enabled software breakpoints) any software traps (breakpoints) that we
834 // may have placed in our tasks memory.
835
836 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
837 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
838
839 if (iter == end || iter->second->GetLoadAddress() > addr + size)
840 return DoWriteMemory(addr, buf, size, error);
841
842 BreakpointSiteList::collection::const_iterator pos;
843 size_t bytes_written = 0;
Greg Claytonc982c762010-07-09 20:39:50 +0000844 addr_t intersect_addr = 0;
845 size_t intersect_size = 0;
846 size_t opcode_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 const uint8_t *ubuf = (const uint8_t *)buf;
848
849 for (pos = iter; pos != end; ++pos)
850 {
851 BreakpointSiteSP bp;
852 bp = pos->second;
853
854 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
855 assert(addr <= intersect_addr && intersect_addr < addr + size);
856 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
857 assert(opcode_offset + intersect_size <= bp->GetByteSize());
858
859 // Check for bytes before this breakpoint
860 const addr_t curr_addr = addr + bytes_written;
861 if (intersect_addr > curr_addr)
862 {
863 // There are some bytes before this breakpoint that we need to
864 // just write to memory
865 size_t curr_size = intersect_addr - curr_addr;
866 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
867 ubuf + bytes_written,
868 curr_size,
869 error);
870 bytes_written += curr_bytes_written;
871 if (curr_bytes_written != curr_size)
872 {
873 // We weren't able to write all of the requested bytes, we
874 // are done looping and will return the number of bytes that
875 // we have written so far.
876 break;
877 }
878 }
879
880 // Now write any bytes that would cover up any software breakpoints
881 // directly into the breakpoint opcode buffer
882 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
883 bytes_written += intersect_size;
884 }
885
886 // Write any remaining bytes after the last breakpoint if we have any left
887 if (bytes_written < size)
888 bytes_written += WriteMemoryPrivate (addr + bytes_written,
889 ubuf + bytes_written,
890 size - bytes_written,
891 error);
892
893 return bytes_written;
894}
895
896addr_t
897Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
898{
899 // Fixme: we should track the blocks we've allocated, and clean them up...
900 // We could even do our own allocator here if that ends up being more efficient.
901 return DoAllocateMemory (size, permissions, error);
902}
903
904Error
905Process::DeallocateMemory (addr_t ptr)
906{
907 return DoDeallocateMemory (ptr);
908}
909
910
911Error
912Process::EnableWatchpoint (WatchpointLocation *watchpoint)
913{
914 Error error;
915 error.SetErrorString("watchpoints are not supported");
916 return error;
917}
918
919Error
920Process::DisableWatchpoint (WatchpointLocation *watchpoint)
921{
922 Error error;
923 error.SetErrorString("watchpoints are not supported");
924 return error;
925}
926
927StateType
928Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
929{
930 StateType state;
931 // Now wait for the process to launch and return control to us, and then
932 // call DidLaunch:
933 while (1)
934 {
935 // FIXME: Might want to put a timeout in here:
936 state = WaitForStateChangedEventsPrivate (NULL, event_sp);
937 if (state == eStateStopped || state == eStateCrashed || state == eStateExited)
938 break;
939 else
940 HandlePrivateEvent (event_sp);
941 }
942 return state;
943}
944
945Error
946Process::Launch
947(
948 char const *argv[],
949 char const *envp[],
Greg Claytonf681b942010-08-31 18:35:14 +0000950 uint32_t launch_flags,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951 const char *stdin_path,
952 const char *stdout_path,
953 const char *stderr_path
954)
955{
956 Error error;
957 m_target_triple.Clear();
958 m_abi_sp.reset();
959
960 Module *exe_module = m_target.GetExecutableModule().get();
961 if (exe_module)
962 {
963 char exec_file_path[PATH_MAX];
964 exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path));
965 if (exe_module->GetFileSpec().Exists())
966 {
967 error = WillLaunch (exe_module);
968 if (error.Success())
969 {
970 // The args coming in should not contain the application name, the
971 // lldb_private::Process class will add this in case the executable
972 // gets resolved to a different file than was given on the command
973 // line (like when an applicaiton bundle is specified and will
974 // resolve to the contained exectuable file, or the file given was
975 // a symlink or other file system link that resolves to a different
976 // file).
977
978 // Get the resolved exectuable path
979
980 // Make a new argument vector
981 std::vector<const char *> exec_path_plus_argv;
982 // Append the resolved executable path
983 exec_path_plus_argv.push_back (exec_file_path);
984
985 // Push all args if there are any
986 if (argv)
987 {
988 for (int i = 0; argv[i]; ++i)
989 exec_path_plus_argv.push_back(argv[i]);
990 }
991
992 // Push a NULL to terminate the args.
993 exec_path_plus_argv.push_back(NULL);
994
995 // Now launch using these arguments.
Greg Clayton471b31c2010-07-20 22:52:08 +0000996 error = DoLaunch (exe_module,
997 exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(),
998 envp,
Greg Claytonf681b942010-08-31 18:35:14 +0000999 launch_flags,
Greg Clayton471b31c2010-07-20 22:52:08 +00001000 stdin_path,
1001 stdout_path,
1002 stderr_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003
1004 if (error.Fail())
1005 {
1006 if (GetID() != LLDB_INVALID_PROCESS_ID)
1007 {
1008 SetID (LLDB_INVALID_PROCESS_ID);
1009 const char *error_string = error.AsCString();
1010 if (error_string == NULL)
1011 error_string = "launch failed";
1012 SetExitStatus (-1, error_string);
1013 }
1014 }
1015 else
1016 {
1017 EventSP event_sp;
1018 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1019
1020 if (state == eStateStopped || state == eStateCrashed)
1021 {
1022 DidLaunch ();
1023
1024 // This delays passing the stopped event to listeners till DidLaunch gets
1025 // a chance to complete...
1026 HandlePrivateEvent (event_sp);
1027 StartPrivateStateThread ();
1028 }
1029 else if (state == eStateExited)
1030 {
1031 // We exited while trying to launch somehow. Don't call DidLaunch as that's
1032 // not likely to work, and return an invalid pid.
1033 HandlePrivateEvent (event_sp);
1034 }
1035 }
1036 }
1037 }
1038 else
1039 {
1040 error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path);
1041 }
1042 }
1043 return error;
1044}
1045
1046Error
1047Process::CompleteAttach ()
1048{
1049 Error error;
1050 EventSP event_sp;
1051 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1052 if (state == eStateStopped || state == eStateCrashed)
1053 {
1054 DidAttach ();
Jim Ingham5aee1622010-08-09 23:31:02 +00001055 // Figure out which one is the executable, and set that in our target:
1056 ModuleList &modules = GetTarget().GetImages();
1057
1058 size_t num_modules = modules.GetSize();
1059 for (int i = 0; i < num_modules; i++)
1060 {
1061 ModuleSP module_sp = modules.GetModuleAtIndex(i);
1062 if (module_sp->IsExecutable())
1063 {
1064 ModuleSP exec_module = GetTarget().GetExecutableModule();
1065 if (!exec_module || exec_module != module_sp)
1066 {
1067
1068 GetTarget().SetExecutableModule (module_sp, false);
1069 }
1070 break;
1071 }
1072 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073
1074 // This delays passing the stopped event to listeners till DidLaunch gets
1075 // a chance to complete...
1076 HandlePrivateEvent(event_sp);
1077 StartPrivateStateThread();
1078 }
1079 else
1080 {
1081 // We exited while trying to launch somehow. Don't call DidLaunch as that's
1082 // not likely to work, and return an invalid pid.
1083 if (state == eStateExited)
1084 HandlePrivateEvent (event_sp);
1085 error.SetErrorStringWithFormat("invalid state after attach: %s",
1086 lldb_private::StateAsCString(state));
1087 }
1088 return error;
1089}
1090
1091Error
1092Process::Attach (lldb::pid_t attach_pid)
1093{
1094
1095 m_target_triple.Clear();
1096 m_abi_sp.reset();
1097
Jim Ingham5aee1622010-08-09 23:31:02 +00001098 // Find the process and its architecture. Make sure it matches the architecture
1099 // of the current Target, and if not adjust it.
1100
1101 ArchSpec attach_spec = GetArchSpecForExistingProcess (attach_pid);
1102 if (attach_spec != GetTarget().GetArchitecture())
1103 {
1104 // Set the architecture on the target.
1105 GetTarget().SetArchitecture(attach_spec);
1106 }
1107
Greg Claytonc982c762010-07-09 20:39:50 +00001108 Error error (WillAttachToProcessWithID(attach_pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 if (error.Success())
1110 {
Greg Claytonc982c762010-07-09 20:39:50 +00001111 error = DoAttachToProcessWithID (attach_pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112 if (error.Success())
1113 {
1114 error = CompleteAttach();
1115 }
1116 else
1117 {
1118 if (GetID() != LLDB_INVALID_PROCESS_ID)
1119 {
1120 SetID (LLDB_INVALID_PROCESS_ID);
1121 const char *error_string = error.AsCString();
1122 if (error_string == NULL)
1123 error_string = "attach failed";
1124
1125 SetExitStatus(-1, error_string);
1126 }
1127 }
1128 }
1129 return error;
1130}
1131
1132Error
1133Process::Attach (const char *process_name, bool wait_for_launch)
1134{
1135 m_target_triple.Clear();
1136 m_abi_sp.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00001137
1138 // Find the process and its architecture. Make sure it matches the architecture
1139 // of the current Target, and if not adjust it.
1140
Jim Ingham2ecb7422010-08-17 21:54:19 +00001141 if (!wait_for_launch)
Jim Ingham5aee1622010-08-09 23:31:02 +00001142 {
Jim Ingham2ecb7422010-08-17 21:54:19 +00001143 ArchSpec attach_spec = GetArchSpecForExistingProcess (process_name);
1144 if (attach_spec != GetTarget().GetArchitecture())
1145 {
1146 // Set the architecture on the target.
1147 GetTarget().SetArchitecture(attach_spec);
1148 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001149 }
Jim Ingham2ecb7422010-08-17 21:54:19 +00001150
Greg Claytonc982c762010-07-09 20:39:50 +00001151 Error error (WillAttachToProcessWithName(process_name, wait_for_launch));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152 if (error.Success())
1153 {
1154 StartPrivateStateThread();
Greg Claytonc982c762010-07-09 20:39:50 +00001155 error = DoAttachToProcessWithName (process_name, wait_for_launch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 if (error.Fail())
1157 {
1158 if (GetID() != LLDB_INVALID_PROCESS_ID)
1159 {
1160 SetID (LLDB_INVALID_PROCESS_ID);
1161 const char *error_string = error.AsCString();
1162 if (error_string == NULL)
1163 error_string = "attach failed";
1164
1165 SetExitStatus(-1, error_string);
1166 }
1167 }
1168 else
1169 {
1170 error = CompleteAttach();
1171 }
1172 }
1173 return error;
1174}
1175
1176Error
1177Process::Resume ()
1178{
1179 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
1180 if (log)
1181 log->Printf("Process::Resume() m_stop_id = %u", m_stop_id);
1182
1183 Error error (WillResume());
1184 // Tell the process it is about to resume before the thread list
1185 if (error.Success())
1186 {
1187 // Now let the thread list know we are about to resume to it
1188 // can let all of our threads know that they are about to be
1189 // resumed. Threads will each be called with
1190 // Thread::WillResume(StateType) where StateType contains the state
1191 // that they are supposed to have when the process is resumed
1192 // (suspended/running/stepping). Threads should also check
1193 // their resume signal in lldb::Thread::GetResumeSignal()
1194 // to see if they are suppoed to start back up with a signal.
1195 if (m_thread_list.WillResume())
1196 {
1197 error = DoResume();
1198 if (error.Success())
1199 {
1200 DidResume();
1201 m_thread_list.DidResume();
1202 }
1203 }
1204 else
1205 {
1206 error.SetErrorStringWithFormat("thread list returned flase after WillResume");
1207 }
1208 }
1209 return error;
1210}
1211
1212Error
1213Process::Halt ()
1214{
1215 Error error (WillHalt());
1216
1217 if (error.Success())
1218 {
1219 error = DoHalt();
1220 if (error.Success())
1221 DidHalt();
1222 }
1223 return error;
1224}
1225
1226Error
1227Process::Detach ()
1228{
1229 Error error (WillDetach());
1230
1231 if (error.Success())
1232 {
1233 DisableAllBreakpointSites();
1234 error = DoDetach();
1235 if (error.Success())
1236 {
1237 DidDetach();
1238 StopPrivateStateThread();
1239 }
1240 }
1241 return error;
1242}
1243
1244Error
1245Process::Destroy ()
1246{
1247 Error error (WillDestroy());
1248 if (error.Success())
1249 {
1250 DisableAllBreakpointSites();
1251 error = DoDestroy();
1252 if (error.Success())
1253 {
1254 DidDestroy();
1255 StopPrivateStateThread();
1256 }
1257 }
1258 return error;
1259}
1260
1261Error
1262Process::Signal (int signal)
1263{
1264 Error error (WillSignal());
1265 if (error.Success())
1266 {
1267 error = DoSignal(signal);
1268 if (error.Success())
1269 DidSignal();
1270 }
1271 return error;
1272}
1273
1274UnixSignals &
1275Process::GetUnixSignals ()
1276{
1277 return m_unix_signals;
1278}
1279
1280Target &
1281Process::GetTarget ()
1282{
1283 return m_target;
1284}
1285
1286const Target &
1287Process::GetTarget () const
1288{
1289 return m_target;
1290}
1291
1292uint32_t
1293Process::GetAddressByteSize()
1294{
1295 return m_target.GetArchitecture().GetAddressByteSize();
1296}
1297
1298bool
1299Process::ShouldBroadcastEvent (Event *event_ptr)
1300{
1301 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
1302 bool return_value = true;
1303 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS);
1304
1305 switch (state)
1306 {
1307 case eStateAttaching:
1308 case eStateLaunching:
1309 case eStateDetached:
1310 case eStateExited:
1311 case eStateUnloaded:
1312 // These events indicate changes in the state of the debugging session, always report them.
1313 return_value = true;
1314 break;
1315 case eStateInvalid:
1316 // We stopped for no apparent reason, don't report it.
1317 return_value = false;
1318 break;
1319 case eStateRunning:
1320 case eStateStepping:
1321 // If we've started the target running, we handle the cases where we
1322 // are already running and where there is a transition from stopped to
1323 // running differently.
1324 // running -> running: Automatically suppress extra running events
1325 // stopped -> running: Report except when there is one or more no votes
1326 // and no yes votes.
1327 SynchronouslyNotifyStateChanged (state);
1328 switch (m_public_state.GetValue())
1329 {
1330 case eStateRunning:
1331 case eStateStepping:
1332 // We always suppress multiple runnings with no PUBLIC stop in between.
1333 return_value = false;
1334 break;
1335 default:
1336 // TODO: make this work correctly. For now always report
1337 // run if we aren't running so we don't miss any runnning
1338 // events. If I run the lldb/test/thread/a.out file and
1339 // break at main.cpp:58, run and hit the breakpoints on
1340 // multiple threads, then somehow during the stepping over
1341 // of all breakpoints no run gets reported.
1342 return_value = true;
1343
1344 // This is a transition from stop to run.
1345 switch (m_thread_list.ShouldReportRun (event_ptr))
1346 {
1347 case eVoteYes:
1348 case eVoteNoOpinion:
1349 return_value = true;
1350 break;
1351 case eVoteNo:
1352 return_value = false;
1353 break;
1354 }
1355 break;
1356 }
1357 break;
1358 case eStateStopped:
1359 case eStateCrashed:
1360 case eStateSuspended:
1361 {
1362 // We've stopped. First see if we're going to restart the target.
1363 // If we are going to stop, then we always broadcast the event.
1364 // If we aren't going to stop, let the thread plans decide if we're going to report this event.
Jim Inghamb01e7422010-06-19 04:45:32 +00001365 // If no thread has an opinion, we don't report it.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001366 if (state != eStateInvalid)
1367 {
1368
1369 RefreshStateAfterStop ();
1370
1371 if (m_thread_list.ShouldStop (event_ptr) == false)
1372 {
1373 switch (m_thread_list.ShouldReportStop (event_ptr))
1374 {
1375 case eVoteYes:
1376 Process::ProcessEventData::SetRestartedInEvent (event_ptr, true);
1377 case eVoteNoOpinion:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378 case eVoteNo:
1379 return_value = false;
1380 break;
1381 }
1382
1383 if (log)
1384 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process", event_ptr, StateAsCString(state));
1385 Resume ();
1386 }
1387 else
1388 {
1389 return_value = true;
1390 SynchronouslyNotifyStateChanged (state);
1391 }
1392 }
1393 }
1394 }
1395
1396 if (log)
1397 log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO");
1398 return return_value;
1399}
1400
1401//------------------------------------------------------------------
1402// Thread Queries
1403//------------------------------------------------------------------
1404
1405ThreadList &
1406Process::GetThreadList ()
1407{
1408 return m_thread_list;
1409}
1410
1411const ThreadList &
1412Process::GetThreadList () const
1413{
1414 return m_thread_list;
1415}
1416
1417
1418bool
1419Process::StartPrivateStateThread ()
1420{
1421 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS);
1422
1423 if (log)
1424 log->Printf ("Process::%s ( )", __FUNCTION__);
1425
1426 // Create a thread that watches our internal state and controls which
1427 // events make it to clients (into the DCProcess event queue).
1428 m_private_state_thread = Host::ThreadCreate ("<lldb.process.internal-state>", Process::PrivateStateThread, this, NULL);
1429 return m_private_state_thread != LLDB_INVALID_HOST_THREAD;
1430}
1431
1432void
1433Process::PausePrivateStateThread ()
1434{
1435 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
1436}
1437
1438void
1439Process::ResumePrivateStateThread ()
1440{
1441 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
1442}
1443
1444void
1445Process::StopPrivateStateThread ()
1446{
1447 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
1448}
1449
1450void
1451Process::ControlPrivateStateThread (uint32_t signal)
1452{
1453 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS);
1454
1455 assert (signal == eBroadcastInternalStateControlStop ||
1456 signal == eBroadcastInternalStateControlPause ||
1457 signal == eBroadcastInternalStateControlResume);
1458
1459 if (log)
1460 log->Printf ("Process::%s ( ) - signal: %d", __FUNCTION__, signal);
1461
1462 // Signal the private state thread
1463 if (m_private_state_thread != LLDB_INVALID_HOST_THREAD)
1464 {
1465 TimeValue timeout_time;
1466 bool timed_out;
1467
1468 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
1469
1470 timeout_time = TimeValue::Now();
1471 timeout_time.OffsetWithSeconds(2);
1472 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
1473 m_private_state_control_wait.SetValue (false, eBroadcastNever);
1474
1475 if (signal == eBroadcastInternalStateControlStop)
1476 {
1477 if (timed_out)
1478 Host::ThreadCancel (m_private_state_thread, NULL);
1479
1480 thread_result_t result = NULL;
1481 Host::ThreadJoin (m_private_state_thread, &result, NULL);
Greg Clayton49182ed2010-07-22 18:34:21 +00001482 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483 }
1484 }
1485}
1486
1487void
1488Process::HandlePrivateEvent (EventSP &event_sp)
1489{
1490 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
1491 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1492 // See if we should broadcast this state to external clients?
1493 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
1494 if (log)
1495 log->Printf ("Process::%s (arg = %p, pid = %i) got event '%s' broadcast = %s", __FUNCTION__, this, GetID(), StateAsCString(internal_state), should_broadcast ? "yes" : "no");
1496
1497 if (should_broadcast)
1498 {
1499 if (log)
1500 {
1501 log->Printf ("\tChanging public state from: %s to %s", StateAsCString(GetState ()), StateAsCString (internal_state));
1502 }
1503 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
1504 BroadcastEvent (event_sp);
1505 }
1506 else
1507 {
1508 if (log)
1509 {
1510 log->Printf ("\tNot changing public state with event: %s", StateAsCString (internal_state));
1511 }
1512 }
1513}
1514
1515void *
1516Process::PrivateStateThread (void *arg)
1517{
1518 Process *proc = static_cast<Process*> (arg);
1519 void *result = proc->RunPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 return result;
1521}
1522
1523void *
1524Process::RunPrivateStateThread ()
1525{
1526 bool control_only = false;
1527 m_private_state_control_wait.SetValue (false, eBroadcastNever);
1528
1529 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
1530 if (log)
1531 log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID());
1532
1533 bool exit_now = false;
1534 while (!exit_now)
1535 {
1536 EventSP event_sp;
1537 WaitForEventsPrivate (NULL, event_sp, control_only);
1538 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
1539 {
1540 switch (event_sp->GetType())
1541 {
1542 case eBroadcastInternalStateControlStop:
1543 exit_now = true;
1544 continue; // Go to next loop iteration so we exit without
1545 break; // doing any internal state managment below
1546
1547 case eBroadcastInternalStateControlPause:
1548 control_only = true;
1549 break;
1550
1551 case eBroadcastInternalStateControlResume:
1552 control_only = false;
1553 break;
1554 }
1555 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
1556 }
1557
1558
1559 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1560
1561 if (internal_state != eStateInvalid)
1562 {
1563 HandlePrivateEvent (event_sp);
1564 }
1565
1566 if (internal_state == eStateInvalid || internal_state == eStateExited)
1567 break;
1568 }
1569
1570 if (log)
1571 log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID());
1572
Greg Claytonbe77e3b2010-08-19 21:50:06 +00001573 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001574 return NULL;
1575}
1576
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577//------------------------------------------------------------------
1578// Process Event Data
1579//------------------------------------------------------------------
1580
1581Process::ProcessEventData::ProcessEventData () :
1582 EventData (),
1583 m_process_sp (),
1584 m_state (eStateInvalid),
Greg Claytonc982c762010-07-09 20:39:50 +00001585 m_restarted (false),
1586 m_update_state (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001587{
1588}
1589
1590Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
1591 EventData (),
1592 m_process_sp (process_sp),
1593 m_state (state),
Greg Claytonc982c762010-07-09 20:39:50 +00001594 m_restarted (false),
1595 m_update_state (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001596{
1597}
1598
1599Process::ProcessEventData::~ProcessEventData()
1600{
1601}
1602
1603const ConstString &
1604Process::ProcessEventData::GetFlavorString ()
1605{
1606 static ConstString g_flavor ("Process::ProcessEventData");
1607 return g_flavor;
1608}
1609
1610const ConstString &
1611Process::ProcessEventData::GetFlavor () const
1612{
1613 return ProcessEventData::GetFlavorString ();
1614}
1615
1616const ProcessSP &
1617Process::ProcessEventData::GetProcessSP () const
1618{
1619 return m_process_sp;
1620}
1621
1622StateType
1623Process::ProcessEventData::GetState () const
1624{
1625 return m_state;
1626}
1627
1628bool
1629Process::ProcessEventData::GetRestarted () const
1630{
1631 return m_restarted;
1632}
1633
1634void
1635Process::ProcessEventData::SetRestarted (bool new_value)
1636{
1637 m_restarted = new_value;
1638}
1639
1640void
1641Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
1642{
1643 // This function gets called twice for each event, once when the event gets pulled
1644 // off of the private process event queue, and once when it gets pulled off of
1645 // the public event queue. m_update_state is used to distinguish these
1646 // two cases; it is false when we're just pulling it off for private handling,
1647 // and we don't want to do the breakpoint command handling then.
1648
1649 if (!m_update_state)
1650 return;
1651
1652 m_process_sp->SetPublicState (m_state);
1653
1654 // If we're stopped and haven't restarted, then do the breakpoint commands here:
1655 if (m_state == eStateStopped && ! m_restarted)
1656 {
1657 int num_threads = m_process_sp->GetThreadList().GetSize();
1658 int idx;
Greg Claytonf4b47e12010-08-04 01:40:35 +00001659
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001660 for (idx = 0; idx < num_threads; ++idx)
1661 {
1662 lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx);
1663
Greg Claytonf4b47e12010-08-04 01:40:35 +00001664 StopInfo *stop_info = thread_sp->GetStopInfo ();
1665 if (stop_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001666 {
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00001667 stop_info->PerformAction(event_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001668 }
1669 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001670
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00001671 // The stop action might restart the target. If it does, then we want to mark that in the
1672 // event so that whoever is receiving it will know to wait for the running event and reflect
1673 // that state appropriately.
1674
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675 if (m_process_sp->GetPrivateState() == eStateRunning)
1676 SetRestarted(true);
1677 }
1678}
1679
1680void
1681Process::ProcessEventData::Dump (Stream *s) const
1682{
1683 if (m_process_sp)
1684 s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID());
1685
1686 s->Printf("state = %s", StateAsCString(GetState()));;
1687}
1688
1689const Process::ProcessEventData *
1690Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
1691{
1692 if (event_ptr)
1693 {
1694 const EventData *event_data = event_ptr->GetData();
1695 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
1696 return static_cast <const ProcessEventData *> (event_ptr->GetData());
1697 }
1698 return NULL;
1699}
1700
1701ProcessSP
1702Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
1703{
1704 ProcessSP process_sp;
1705 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
1706 if (data)
1707 process_sp = data->GetProcessSP();
1708 return process_sp;
1709}
1710
1711StateType
1712Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
1713{
1714 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
1715 if (data == NULL)
1716 return eStateInvalid;
1717 else
1718 return data->GetState();
1719}
1720
1721bool
1722Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
1723{
1724 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
1725 if (data == NULL)
1726 return false;
1727 else
1728 return data->GetRestarted();
1729}
1730
1731void
1732Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
1733{
1734 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
1735 if (data != NULL)
1736 data->SetRestarted(new_value);
1737}
1738
1739bool
1740Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
1741{
1742 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
1743 if (data)
1744 {
1745 data->SetUpdateStateOnRemoval();
1746 return true;
1747 }
1748 return false;
1749}
1750
1751void
1752Process::ProcessEventData::SetUpdateStateOnRemoval()
1753{
1754 m_update_state = true;
1755}
1756
1757Target *
1758Process::CalculateTarget ()
1759{
1760 return &m_target;
1761}
1762
1763Process *
1764Process::CalculateProcess ()
1765{
1766 return this;
1767}
1768
1769Thread *
1770Process::CalculateThread ()
1771{
1772 return NULL;
1773}
1774
1775StackFrame *
1776Process::CalculateStackFrame ()
1777{
1778 return NULL;
1779}
1780
1781void
1782Process::Calculate (ExecutionContext &exe_ctx)
1783{
1784 exe_ctx.target = &m_target;
1785 exe_ctx.process = this;
1786 exe_ctx.thread = NULL;
1787 exe_ctx.frame = NULL;
1788}
1789
1790lldb::ProcessSP
1791Process::GetSP ()
1792{
1793 return GetTarget().GetProcessSP();
1794}
1795
Sean Callanan2235f322010-08-11 03:57:18 +00001796ClangPersistentVariables &
1797Process::GetPersistentVariables()
1798{
1799 return m_persistent_vars;
1800}
1801
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802ObjCObjectPrinter &
1803Process::GetObjCObjectPrinter()
1804{
1805 return m_objc_object_printer;
1806}
1807
Jim Ingham5aee1622010-08-09 23:31:02 +00001808uint32_t
1809Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
1810{
1811 return 0;
1812}
1813
1814ArchSpec
1815Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
1816{
1817 return Host::GetArchSpecForExistingProcess (pid);
1818}
1819
1820ArchSpec
1821Process::GetArchSpecForExistingProcess (const char *process_name)
1822{
1823 return Host::GetArchSpecForExistingProcess (process_name);
1824}
1825
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001826lldb::UserSettingsControllerSP
1827Process::GetSettingsController (bool finish)
1828{
1829 static UserSettingsControllerSP g_settings_controller (new ProcessSettingsController);
1830 static bool initialized = false;
1831
1832 if (!initialized)
1833 {
Jim Ingham95852752010-09-07 20:27:09 +00001834 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
1835 Process::ProcessSettingsController::global_settings_table,
1836 Process::ProcessSettingsController::instance_settings_table);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001837 }
1838
1839 if (finish)
1840 {
Jim Ingham95852752010-09-07 20:27:09 +00001841 UserSettingsController::FinalizeSettingsController (g_settings_controller);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001842 g_settings_controller.reset();
Jim Ingham95852752010-09-07 20:27:09 +00001843 initialized = false;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001844 }
1845
1846 return g_settings_controller;
1847}
1848
1849//--------------------------------------------------------------
1850// class Process::ProcessSettingsController
1851//--------------------------------------------------------------
1852
1853Process::ProcessSettingsController::ProcessSettingsController () :
1854 UserSettingsController ("process", Debugger::GetSettingsController())
1855{
Caroline Tice91123da2010-09-08 17:48:55 +00001856 m_default_settings.reset (new ProcessInstanceSettings (*this, false,
1857 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001858}
1859
1860Process::ProcessSettingsController::~ProcessSettingsController ()
1861{
1862}
1863
1864lldb::InstanceSettingsSP
Caroline Tice91123da2010-09-08 17:48:55 +00001865Process::ProcessSettingsController::CreateNewInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001866{
Caroline Tice91123da2010-09-08 17:48:55 +00001867 ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*(Process::GetSettingsController().get()),
1868 false, instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001869 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1870 return new_settings_sp;
1871}
1872
1873//--------------------------------------------------------------
1874// class ProcessInstanceSettings
1875//--------------------------------------------------------------
1876
Caroline Tice91123da2010-09-08 17:48:55 +00001877ProcessInstanceSettings::ProcessInstanceSettings (UserSettingsController &owner, bool live_instance,
1878 const char *name) :
Caroline Ticef20e8232010-09-09 18:26:37 +00001879 InstanceSettings (owner, (name == NULL ? CreateInstanceName().AsCString() : name), live_instance),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001880 m_run_args (),
1881 m_env_vars (),
1882 m_input_path (),
1883 m_output_path (),
1884 m_error_path (),
1885 m_plugin (),
1886 m_disable_aslr (true)
1887{
Caroline Ticef20e8232010-09-09 18:26:37 +00001888 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1889 // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers.
1890 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1891
1892 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001893 {
1894 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1895 CopyInstanceSettings (pending_settings,false);
Caroline Ticef20e8232010-09-09 18:26:37 +00001896 //m_owner.RemovePendingSettings (m_instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001897 }
1898}
1899
1900ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) :
1901 InstanceSettings (*(Process::GetSettingsController().get()), CreateInstanceName().AsCString()),
1902 m_run_args (rhs.m_run_args),
1903 m_env_vars (rhs.m_env_vars),
1904 m_input_path (rhs.m_input_path),
1905 m_output_path (rhs.m_output_path),
1906 m_error_path (rhs.m_error_path),
1907 m_plugin (rhs.m_plugin),
1908 m_disable_aslr (rhs.m_disable_aslr)
1909{
1910 if (m_instance_name != InstanceSettings::GetDefaultName())
1911 {
1912 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1913 CopyInstanceSettings (pending_settings,false);
1914 m_owner.RemovePendingSettings (m_instance_name);
1915 }
1916}
1917
1918ProcessInstanceSettings::~ProcessInstanceSettings ()
1919{
1920}
1921
1922ProcessInstanceSettings&
1923ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs)
1924{
1925 if (this != &rhs)
1926 {
1927 m_run_args = rhs.m_run_args;
1928 m_env_vars = rhs.m_env_vars;
1929 m_input_path = rhs.m_input_path;
1930 m_output_path = rhs.m_output_path;
1931 m_error_path = rhs.m_error_path;
1932 m_plugin = rhs.m_plugin;
1933 m_disable_aslr = rhs.m_disable_aslr;
1934 }
1935
1936 return *this;
1937}
1938
1939
1940void
1941ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1942 const char *index_value,
1943 const char *value,
1944 const ConstString &instance_name,
1945 const SettingEntry &entry,
1946 lldb::VarSetOperationType op,
1947 Error &err,
1948 bool pending)
1949{
1950 if (var_name == RunArgsVarName())
1951 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
1952 else if (var_name == EnvVarsVarName())
1953 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
1954 else if (var_name == InputPathVarName())
1955 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
1956 else if (var_name == OutputPathVarName())
1957 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
1958 else if (var_name == ErrorPathVarName())
1959 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
1960 else if (var_name == PluginVarName())
1961 UserSettingsController::UpdateEnumVariable (entry.enum_values, (int *) &m_plugin, value, err);
1962 else if (var_name == DisableASLRVarName())
1963 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err);
1964}
1965
1966void
1967ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1968 bool pending)
1969{
1970 if (new_settings.get() == NULL)
1971 return;
1972
1973 ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get();
1974
1975 m_run_args = new_process_settings->m_run_args;
1976 m_env_vars = new_process_settings->m_env_vars;
1977 m_input_path = new_process_settings->m_input_path;
1978 m_output_path = new_process_settings->m_output_path;
1979 m_error_path = new_process_settings->m_error_path;
1980 m_plugin = new_process_settings->m_plugin;
1981 m_disable_aslr = new_process_settings->m_disable_aslr;
1982}
1983
1984void
1985Process::ProcessSettingsController::UpdateGlobalVariable (const ConstString &var_name,
1986 const char *index_value,
1987 const char *value,
1988 const SettingEntry &entry,
1989 lldb::VarSetOperationType op,
1990 Error&err)
1991{
1992 // Currently 'process' does not have any global settings.
1993}
1994
1995
1996void
1997ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1998 const ConstString &var_name,
1999 StringList &value)
2000{
2001 if (var_name == RunArgsVarName())
2002 {
2003 if (m_run_args.GetArgumentCount() > 0)
Greg Claytona52c1552010-09-14 03:47:41 +00002004 {
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002005 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2006 value.AppendString (m_run_args.GetArgumentAtIndex (i));
Greg Claytona52c1552010-09-14 03:47:41 +00002007 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002008 }
2009 else if (var_name == EnvVarsVarName())
2010 {
2011 if (m_env_vars.size() > 0)
2012 {
2013 std::map<std::string, std::string>::iterator pos;
2014 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2015 {
2016 StreamString value_str;
2017 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2018 value.AppendString (value_str.GetData());
2019 }
2020 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002021 }
2022 else if (var_name == InputPathVarName())
2023 {
2024 value.AppendString (m_input_path.c_str());
2025 }
2026 else if (var_name == OutputPathVarName())
2027 {
2028 value.AppendString (m_output_path.c_str());
2029 }
2030 else if (var_name == ErrorPathVarName())
2031 {
2032 value.AppendString (m_error_path.c_str());
2033 }
2034 else if (var_name == PluginVarName())
2035 {
2036 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, (int) m_plugin));
2037 }
2038 else if (var_name == DisableASLRVarName())
2039 {
2040 if (m_disable_aslr)
2041 value.AppendString ("true");
2042 else
2043 value.AppendString ("false");
2044 }
2045 else
2046 value.AppendString ("unrecognized variable name");
2047}
2048
2049void
2050Process::ProcessSettingsController::GetGlobalSettingsValue (const ConstString &var_name,
2051 StringList &value)
2052{
2053 // Currently 'process' does not have any global settings.
2054}
2055
2056const ConstString
2057ProcessInstanceSettings::CreateInstanceName ()
2058{
2059 static int instance_count = 1;
2060 StreamString sstr;
2061
2062 sstr.Printf ("process_%d", instance_count);
2063 ++instance_count;
2064
2065 const ConstString ret_val (sstr.GetData());
2066 return ret_val;
2067}
2068
2069const ConstString &
2070ProcessInstanceSettings::RunArgsVarName ()
2071{
2072 static ConstString run_args_var_name ("run-args");
2073
2074 return run_args_var_name;
2075}
2076
2077const ConstString &
2078ProcessInstanceSettings::EnvVarsVarName ()
2079{
2080 static ConstString env_vars_var_name ("env-vars");
2081
2082 return env_vars_var_name;
2083}
2084
2085const ConstString &
2086ProcessInstanceSettings::InputPathVarName ()
2087{
2088 static ConstString input_path_var_name ("input-path");
2089
2090 return input_path_var_name;
2091}
2092
2093const ConstString &
2094ProcessInstanceSettings::OutputPathVarName ()
2095{
Caroline Tice49e27372010-09-07 18:35:40 +00002096 static ConstString output_path_var_name ("output-path");
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002097
2098 return output_path_var_name;
2099}
2100
2101const ConstString &
2102ProcessInstanceSettings::ErrorPathVarName ()
2103{
Caroline Tice49e27372010-09-07 18:35:40 +00002104 static ConstString error_path_var_name ("error-path");
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002105
2106 return error_path_var_name;
2107}
2108
2109const ConstString &
2110ProcessInstanceSettings::PluginVarName ()
2111{
2112 static ConstString plugin_var_name ("plugin");
2113
2114 return plugin_var_name;
2115}
2116
2117
2118const ConstString &
2119ProcessInstanceSettings::DisableASLRVarName ()
2120{
2121 static ConstString disable_aslr_var_name ("disable-aslr");
2122
2123 return disable_aslr_var_name;
2124}
2125
2126
2127//--------------------------------------------------
2128// ProcessSettingsController Variable Tables
2129//--------------------------------------------------
2130
2131SettingEntry
2132Process::ProcessSettingsController::global_settings_table[] =
2133{
2134 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
2135 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2136};
2137
2138
2139lldb::OptionEnumValueElement
2140Process::ProcessSettingsController::g_plugins[] =
2141{
Caroline Tice5c9fdfa2010-09-09 18:01:59 +00002142 { eMacosx, "process.macosx", "Use the native MacOSX debugger plugin" },
2143 { eRemoteDebugger, "process.gdb-remote" , "Use the GDB Remote protocol based debugger plugin" },
2144 { 0, NULL, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002145};
2146
2147SettingEntry
2148Process::ProcessSettingsController::instance_settings_table[] =
2149{
2150 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
2151 { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2152 { "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." },
2153 { "input-path", eSetVarTypeString, "/dev/stdin", NULL, false, false, "The file/path to be used by the executable program for reading its input." },
2154 { "output-path", eSetVarTypeString, "/dev/stdout", NULL, false, false, "The file/path to be used by the executable program for writing its output." },
2155 { "error-path", eSetVarTypeString, "/dev/stderr", NULL, false, false, "The file/path to be used by the executable program for writings its error messages." },
2156 { "plugin", eSetVarTypeEnum, NULL , g_plugins, false, false, "The plugin to be used to run the process." },
2157 { "disable-aslr", eSetVarTypeBool, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2158 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2159};
2160
2161
Jim Ingham5aee1622010-08-09 23:31:02 +00002162