blob: 9b5718a0adf08dd63dbef798479fee34ae939c39 [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
Greg Claytonfd119992011-01-07 06:08:19 +000040
41//----------------------------------------------------------------------
42// MemoryCache constructor
43//----------------------------------------------------------------------
44Process::MemoryCache::MemoryCache() :
45 m_cache_line_byte_size (512),
46 m_cache_mutex (Mutex::eMutexTypeRecursive),
47 m_cache ()
48{
49}
50
51//----------------------------------------------------------------------
52// Destructor
53//----------------------------------------------------------------------
54Process::MemoryCache::~MemoryCache()
55{
56}
57
58void
59Process::MemoryCache::Clear()
60{
61 Mutex::Locker locker (m_cache_mutex);
62 m_cache.clear();
63}
64
65void
66Process::MemoryCache::Flush (addr_t addr, size_t size)
67{
68 if (size == 0)
69 return;
70
71 const uint32_t cache_line_byte_size = m_cache_line_byte_size;
72 const addr_t end_addr = (addr + size - 1);
73 const addr_t flush_start_addr = addr - (addr % cache_line_byte_size);
74 const addr_t flush_end_addr = end_addr - (end_addr % cache_line_byte_size);
75
76 Mutex::Locker locker (m_cache_mutex);
77 if (m_cache.empty())
78 return;
79
80 assert ((flush_start_addr % cache_line_byte_size) == 0);
81
82 for (addr_t curr_addr = flush_start_addr; curr_addr <= flush_end_addr; curr_addr += cache_line_byte_size)
83 {
84 collection::iterator pos = m_cache.find (curr_addr);
85 if (pos != m_cache.end())
86 m_cache.erase(pos);
87 }
88}
89
90size_t
91Process::MemoryCache::Read
92(
93 Process *process,
94 addr_t addr,
95 void *dst,
96 size_t dst_len,
97 Error &error
98)
99{
100 size_t bytes_left = dst_len;
101 if (dst && bytes_left > 0)
102 {
103 const uint32_t cache_line_byte_size = m_cache_line_byte_size;
104 uint8_t *dst_buf = (uint8_t *)dst;
105 addr_t curr_addr = addr - (addr % cache_line_byte_size);
106 addr_t cache_offset = addr - curr_addr;
107 Mutex::Locker locker (m_cache_mutex);
108
109 while (bytes_left > 0)
110 {
111 collection::const_iterator pos = m_cache.find (curr_addr);
112 collection::const_iterator end = m_cache.end ();
113
114 if (pos != end)
115 {
116 size_t curr_read_size = cache_line_byte_size - cache_offset;
117 if (curr_read_size > bytes_left)
118 curr_read_size = bytes_left;
119
120 memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes() + cache_offset, curr_read_size);
121
122 bytes_left -= curr_read_size;
123 curr_addr += curr_read_size + cache_offset;
124 cache_offset = 0;
125
126 if (bytes_left > 0)
127 {
128 // Get sequential cache page hits
129 for (++pos; (pos != end) && (bytes_left > 0); ++pos)
130 {
131 assert ((curr_addr % cache_line_byte_size) == 0);
132
133 if (pos->first != curr_addr)
134 break;
135
136 curr_read_size = pos->second->GetByteSize();
137 if (curr_read_size > bytes_left)
138 curr_read_size = bytes_left;
139
140 memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes(), curr_read_size);
141
142 bytes_left -= curr_read_size;
143 curr_addr += curr_read_size;
144
145 // We have a cache page that succeeded to read some bytes
146 // but not an entire page. If this happens, we must cap
147 // off how much data we are able to read...
148 if (pos->second->GetByteSize() != cache_line_byte_size)
149 return dst_len - bytes_left;
150 }
151 }
152 }
153
154 // We need to read from the process
155
156 if (bytes_left > 0)
157 {
158 assert ((curr_addr % cache_line_byte_size) == 0);
159 std::auto_ptr<DataBufferHeap> data_buffer_heap_ap(new DataBufferHeap (cache_line_byte_size, 0));
160 size_t process_bytes_read = process->ReadMemoryFromInferior (curr_addr,
161 data_buffer_heap_ap->GetBytes(),
162 data_buffer_heap_ap->GetByteSize(),
163 error);
164 if (process_bytes_read == 0)
165 return dst_len - bytes_left;
166
167 if (process_bytes_read != cache_line_byte_size)
168 data_buffer_heap_ap->SetByteSize (process_bytes_read);
169 m_cache[curr_addr] = DataBufferSP (data_buffer_heap_ap.release());
170 // We have read data and put it into the cache, continue through the
171 // loop again to get the data out of the cache...
172 }
173 }
174 }
175
176 return dst_len - bytes_left;
177}
178
Chris Lattner24943d22010-06-08 16:52:24 +0000179Process*
180Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener)
181{
182 ProcessCreateInstance create_callback = NULL;
183 if (plugin_name)
184 {
185 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name);
186 if (create_callback)
187 {
188 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
189 if (debugger_ap->CanDebug(target))
190 return debugger_ap.release();
191 }
192 }
193 else
194 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000195 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000196 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000197 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
198 if (debugger_ap->CanDebug(target))
199 return debugger_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000200 }
201 }
202 return NULL;
203}
204
205
206//----------------------------------------------------------------------
207// Process constructor
208//----------------------------------------------------------------------
209Process::Process(Target &target, Listener &listener) :
210 UserID (LLDB_INVALID_PROCESS_ID),
Greg Clayton49ce6822010-10-31 03:01:06 +0000211 Broadcaster ("lldb.process"),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000212 ProcessInstanceSettings (*GetSettingsController()),
Chris Lattner24943d22010-06-08 16:52:24 +0000213 m_target (target),
Chris Lattner24943d22010-06-08 16:52:24 +0000214 m_public_state (eStateUnloaded),
215 m_private_state (eStateUnloaded),
216 m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"),
217 m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"),
218 m_private_state_listener ("lldb.process.internal_state_listener"),
219 m_private_state_control_wait(),
220 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
221 m_stop_id (0),
222 m_thread_index_id (0),
223 m_exit_status (-1),
224 m_exit_string (),
225 m_thread_list (this),
226 m_notifications (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000227 m_image_tokens (),
228 m_listener (listener),
229 m_breakpoint_site_list (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000230 m_dynamic_checkers_ap (),
Caroline Tice861efb32010-11-16 05:07:41 +0000231 m_unix_signals (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000232 m_target_triple (),
Greg Claytoncd548032011-02-01 01:31:41 +0000233 m_byte_order (lldb::endian::InlHostByteOrder()),
Greg Clayton20d338f2010-11-18 05:57:03 +0000234 m_addr_byte_size (0),
235 m_abi_sp (),
Caroline Tice861efb32010-11-16 05:07:41 +0000236 m_process_input_reader (),
Greg Claytona875b642011-01-09 21:07:35 +0000237 m_stdio_communication ("process.stdio"),
Greg Clayton20d338f2010-11-18 05:57:03 +0000238 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonfd119992011-01-07 06:08:19 +0000239 m_stdout_data (),
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000240 m_memory_cache (),
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000241 m_next_event_action_ap()
Chris Lattner24943d22010-06-08 16:52:24 +0000242{
Caroline Tice1ebef442010-09-27 00:30:10 +0000243 UpdateInstanceName();
244
Greg Claytone005f2c2010-11-06 01:53:30 +0000245 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000246 if (log)
247 log->Printf ("%p Process::Process()", this);
248
Greg Clayton49ce6822010-10-31 03:01:06 +0000249 SetEventName (eBroadcastBitStateChanged, "state-changed");
250 SetEventName (eBroadcastBitInterrupt, "interrupt");
251 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
252 SetEventName (eBroadcastBitSTDERR, "stderr-available");
253
Chris Lattner24943d22010-06-08 16:52:24 +0000254 listener.StartListeningForEvents (this,
255 eBroadcastBitStateChanged |
256 eBroadcastBitInterrupt |
257 eBroadcastBitSTDOUT |
258 eBroadcastBitSTDERR);
259
260 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
261 eBroadcastBitStateChanged);
262
263 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
264 eBroadcastInternalStateControlStop |
265 eBroadcastInternalStateControlPause |
266 eBroadcastInternalStateControlResume);
267}
268
269//----------------------------------------------------------------------
270// Destructor
271//----------------------------------------------------------------------
272Process::~Process()
273{
Greg Claytone005f2c2010-11-06 01:53:30 +0000274 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000275 if (log)
276 log->Printf ("%p Process::~Process()", this);
277 StopPrivateStateThread();
278}
279
280void
281Process::Finalize()
282{
283 // Do any cleanup needed prior to being destructed... Subclasses
284 // that override this method should call this superclass method as well.
285}
286
287void
288Process::RegisterNotificationCallbacks (const Notifications& callbacks)
289{
290 m_notifications.push_back(callbacks);
291 if (callbacks.initialize != NULL)
292 callbacks.initialize (callbacks.baton, this);
293}
294
295bool
296Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
297{
298 std::vector<Notifications>::iterator pos, end = m_notifications.end();
299 for (pos = m_notifications.begin(); pos != end; ++pos)
300 {
301 if (pos->baton == callbacks.baton &&
302 pos->initialize == callbacks.initialize &&
303 pos->process_state_changed == callbacks.process_state_changed)
304 {
305 m_notifications.erase(pos);
306 return true;
307 }
308 }
309 return false;
310}
311
312void
313Process::SynchronouslyNotifyStateChanged (StateType state)
314{
315 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
316 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
317 {
318 if (notification_pos->process_state_changed)
319 notification_pos->process_state_changed (notification_pos->baton, this, state);
320 }
321}
322
323// FIXME: We need to do some work on events before the general Listener sees them.
324// For instance if we are continuing from a breakpoint, we need to ensure that we do
325// the little "insert real insn, step & stop" trick. But we can't do that when the
326// event is delivered by the broadcaster - since that is done on the thread that is
327// waiting for new events, so if we needed more than one event for our handling, we would
328// stall. So instead we do it when we fetch the event off of the queue.
329//
330
331StateType
332Process::GetNextEvent (EventSP &event_sp)
333{
334 StateType state = eStateInvalid;
335
336 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
337 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
338
339 return state;
340}
341
342
343StateType
344Process::WaitForProcessToStop (const TimeValue *timeout)
345{
346 StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded };
347 return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType));
348}
349
350
351StateType
352Process::WaitForState
353(
354 const TimeValue *timeout,
355 const StateType *match_states, const uint32_t num_match_states
356)
357{
358 EventSP event_sp;
359 uint32_t i;
Greg Claytond8c62532010-10-07 04:19:01 +0000360 StateType state = GetState();
Chris Lattner24943d22010-06-08 16:52:24 +0000361 while (state != eStateInvalid)
362 {
Greg Claytond8c62532010-10-07 04:19:01 +0000363 // If we are exited or detached, we won't ever get back to any
364 // other valid state...
365 if (state == eStateDetached || state == eStateExited)
366 return state;
367
Chris Lattner24943d22010-06-08 16:52:24 +0000368 state = WaitForStateChangedEvents (timeout, event_sp);
369
370 for (i=0; i<num_match_states; ++i)
371 {
372 if (match_states[i] == state)
373 return state;
374 }
375 }
376 return state;
377}
378
Jim Ingham63e24d72010-10-11 23:53:14 +0000379bool
380Process::HijackProcessEvents (Listener *listener)
381{
382 if (listener != NULL)
383 {
384 return HijackBroadcaster(listener, eBroadcastBitStateChanged);
385 }
386 else
387 return false;
388}
389
390void
391Process::RestoreProcessEvents ()
392{
393 RestoreBroadcaster();
394}
395
Chris Lattner24943d22010-06-08 16:52:24 +0000396StateType
397Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
398{
Greg Claytone005f2c2010-11-06 01:53:30 +0000399 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000400
401 if (log)
402 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
403
404 StateType state = eStateInvalid;
Greg Clayton36f63a92010-10-19 03:25:40 +0000405 if (m_listener.WaitForEventForBroadcasterWithType (timeout,
406 this,
407 eBroadcastBitStateChanged,
408 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +0000409 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
410
411 if (log)
412 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
413 __FUNCTION__,
414 timeout,
415 StateAsCString(state));
416 return state;
417}
418
419Event *
420Process::PeekAtStateChangedEvents ()
421{
Greg Claytone005f2c2010-11-06 01:53:30 +0000422 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000423
424 if (log)
425 log->Printf ("Process::%s...", __FUNCTION__);
426
427 Event *event_ptr;
Greg Clayton36f63a92010-10-19 03:25:40 +0000428 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
429 eBroadcastBitStateChanged);
Chris Lattner24943d22010-06-08 16:52:24 +0000430 if (log)
431 {
432 if (event_ptr)
433 {
434 log->Printf ("Process::%s (event_ptr) => %s",
435 __FUNCTION__,
436 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
437 }
438 else
439 {
440 log->Printf ("Process::%s no events found",
441 __FUNCTION__);
442 }
443 }
444 return event_ptr;
445}
446
447StateType
448Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
449{
Greg Claytone005f2c2010-11-06 01:53:30 +0000450 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000451
452 if (log)
453 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
454
455 StateType state = eStateInvalid;
Greg Clayton72e1c782011-01-22 23:43:18 +0000456 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
457 &m_private_state_broadcaster,
458 eBroadcastBitStateChanged,
459 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +0000460 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
461
462 // This is a bit of a hack, but when we wait here we could very well return
463 // to the command-line, and that could disable the log, which would render the
464 // log we got above invalid.
Chris Lattner24943d22010-06-08 16:52:24 +0000465 if (log)
Greg Clayton72e1c782011-01-22 23:43:18 +0000466 {
467 if (state == eStateInvalid)
468 log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout);
469 else
470 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
471 }
Chris Lattner24943d22010-06-08 16:52:24 +0000472 return state;
473}
474
475bool
476Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
477{
Greg Claytone005f2c2010-11-06 01:53:30 +0000478 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000479
480 if (log)
481 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
482
483 if (control_only)
484 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
485 else
486 return m_private_state_listener.WaitForEvent(timeout, event_sp);
487}
488
489bool
490Process::IsRunning () const
491{
492 return StateIsRunningState (m_public_state.GetValue());
493}
494
495int
496Process::GetExitStatus ()
497{
498 if (m_public_state.GetValue() == eStateExited)
499 return m_exit_status;
500 return -1;
501}
502
Greg Clayton638351a2010-12-04 00:10:17 +0000503
504void
505Process::ProcessInstanceSettings::GetHostEnvironmentIfNeeded ()
506{
507 if (m_inherit_host_env && !m_got_host_env)
508 {
509 m_got_host_env = true;
510 StringList host_env;
511 const size_t host_env_count = Host::GetEnvironment (host_env);
512 for (size_t idx=0; idx<host_env_count; idx++)
513 {
514 const char *env_entry = host_env.GetStringAtIndex (idx);
515 if (env_entry)
516 {
Greg Clayton1f3dd642010-12-15 20:52:40 +0000517 const char *equal_pos = ::strchr(env_entry, '=');
Greg Clayton638351a2010-12-04 00:10:17 +0000518 if (equal_pos)
519 {
520 std::string key (env_entry, equal_pos - env_entry);
521 std::string value (equal_pos + 1);
522 if (m_env_vars.find (key) == m_env_vars.end())
523 m_env_vars[key] = value;
524 }
525 }
526 }
527 }
528}
529
530
531size_t
532Process::ProcessInstanceSettings::GetEnvironmentAsArgs (Args &env)
533{
534 GetHostEnvironmentIfNeeded ();
535
536 dictionary::const_iterator pos, end = m_env_vars.end();
537 for (pos = m_env_vars.begin(); pos != end; ++pos)
538 {
539 std::string env_var_equal_value (pos->first);
540 env_var_equal_value.append(1, '=');
541 env_var_equal_value.append (pos->second);
542 env.AppendArgument (env_var_equal_value.c_str());
543 }
544 return env.GetArgumentCount();
545}
546
547
Chris Lattner24943d22010-06-08 16:52:24 +0000548const char *
549Process::GetExitDescription ()
550{
551 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
552 return m_exit_string.c_str();
553 return NULL;
554}
555
Greg Clayton72e1c782011-01-22 23:43:18 +0000556bool
Chris Lattner24943d22010-06-08 16:52:24 +0000557Process::SetExitStatus (int status, const char *cstr)
558{
Greg Clayton68ca8232011-01-25 02:58:48 +0000559 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
560 if (log)
561 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
562 status, status,
563 cstr ? "\"" : "",
564 cstr ? cstr : "NULL",
565 cstr ? "\"" : "");
566
Greg Clayton72e1c782011-01-22 23:43:18 +0000567 // We were already in the exited state
568 if (m_private_state.GetValue() == eStateExited)
Greg Clayton68ca8232011-01-25 02:58:48 +0000569 {
Greg Clayton644ddfb2011-01-26 23:47:29 +0000570 if (log)
571 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton72e1c782011-01-22 23:43:18 +0000572 return false;
Greg Clayton68ca8232011-01-25 02:58:48 +0000573 }
Greg Clayton72e1c782011-01-22 23:43:18 +0000574
575 m_exit_status = status;
576 if (cstr)
577 m_exit_string = cstr;
578 else
579 m_exit_string.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000580
Greg Clayton72e1c782011-01-22 23:43:18 +0000581 DidExit ();
Greg Clayton58e844b2010-12-08 05:08:21 +0000582
Greg Clayton72e1c782011-01-22 23:43:18 +0000583 SetPrivateState (eStateExited);
584 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000585}
586
587// This static callback can be used to watch for local child processes on
588// the current host. The the child process exits, the process will be
589// found in the global target list (we want to be completely sure that the
590// lldb_private::Process doesn't go away before we can deliver the signal.
591bool
592Process::SetProcessExitStatus
593(
594 void *callback_baton,
595 lldb::pid_t pid,
596 int signo, // Zero for no signal
597 int exit_status // Exit value of process if signal is zero
598)
599{
600 if (signo == 0 || exit_status)
601 {
Greg Clayton63094e02010-06-23 01:19:29 +0000602 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner24943d22010-06-08 16:52:24 +0000603 if (target_sp)
604 {
605 ProcessSP process_sp (target_sp->GetProcessSP());
606 if (process_sp)
607 {
608 const char *signal_cstr = NULL;
609 if (signo)
610 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
611
612 process_sp->SetExitStatus (exit_status, signal_cstr);
613 }
614 }
615 return true;
616 }
617 return false;
618}
619
620
621uint32_t
622Process::GetNextThreadIndexID ()
623{
624 return ++m_thread_index_id;
625}
626
627StateType
628Process::GetState()
629{
630 // If any other threads access this we will need a mutex for it
631 return m_public_state.GetValue ();
632}
633
634void
635Process::SetPublicState (StateType new_state)
636{
Greg Clayton68ca8232011-01-25 02:58:48 +0000637 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000638 if (log)
639 log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state));
640 m_public_state.SetValue (new_state);
641}
642
643StateType
644Process::GetPrivateState ()
645{
646 return m_private_state.GetValue();
647}
648
649void
650Process::SetPrivateState (StateType new_state)
651{
Greg Clayton68ca8232011-01-25 02:58:48 +0000652 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000653 bool state_changed = false;
654
655 if (log)
656 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
657
658 Mutex::Locker locker(m_private_state.GetMutex());
659
660 const StateType old_state = m_private_state.GetValueNoLock ();
661 state_changed = old_state != new_state;
662 if (state_changed)
663 {
664 m_private_state.SetValueNoLock (new_state);
665 if (StateIsStoppedState(new_state))
666 {
667 m_stop_id++;
Greg Claytonfd119992011-01-07 06:08:19 +0000668 m_memory_cache.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000669 if (log)
670 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id);
671 }
672 // Use our target to get a shared pointer to ourselves...
673 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state));
674 }
675 else
676 {
677 if (log)
678 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state));
679 }
680}
681
682
683uint32_t
684Process::GetStopID() const
685{
686 return m_stop_id;
687}
688
689addr_t
690Process::GetImageInfoAddress()
691{
692 return LLDB_INVALID_ADDRESS;
693}
694
Greg Clayton0baa3942010-11-04 01:54:29 +0000695//----------------------------------------------------------------------
696// LoadImage
697//
698// This function provides a default implementation that works for most
699// unix variants. Any Process subclasses that need to do shared library
700// loading differently should override LoadImage and UnloadImage and
701// do what is needed.
702//----------------------------------------------------------------------
703uint32_t
704Process::LoadImage (const FileSpec &image_spec, Error &error)
705{
706 DynamicLoader *loader = GetDynamicLoader();
707 if (loader)
708 {
709 error = loader->CanLoadImage();
710 if (error.Fail())
711 return LLDB_INVALID_IMAGE_TOKEN;
712 }
713
714 if (error.Success())
715 {
716 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
717 if (thread_sp == NULL)
718 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
719
720 if (thread_sp)
721 {
722 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
723
724 if (frame_sp)
725 {
726 ExecutionContext exe_ctx;
727 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +0000728 bool unwind_on_error = true;
Sean Callanan6a925532011-01-13 08:53:35 +0000729 bool keep_in_memory = false;
Greg Clayton0baa3942010-11-04 01:54:29 +0000730 StreamString expr;
731 char path[PATH_MAX];
732 image_spec.GetPath(path, sizeof(path));
733 expr.Printf("dlopen (\"%s\", 2)", path);
734 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Ingham360f53f2010-11-30 02:22:11 +0000735 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan6a925532011-01-13 08:53:35 +0000736 ClangUserExpression::Evaluate (exe_ctx, keep_in_memory, unwind_on_error, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton0baa3942010-11-04 01:54:29 +0000737 if (result_valobj_sp->GetError().Success())
738 {
739 Scalar scalar;
740 if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar))
741 {
742 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
743 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
744 {
745 uint32_t image_token = m_image_tokens.size();
746 m_image_tokens.push_back (image_ptr);
747 return image_token;
748 }
749 }
750 }
751 }
752 }
753 }
754 return LLDB_INVALID_IMAGE_TOKEN;
755}
756
757//----------------------------------------------------------------------
758// UnloadImage
759//
760// This function provides a default implementation that works for most
761// unix variants. Any Process subclasses that need to do shared library
762// loading differently should override LoadImage and UnloadImage and
763// do what is needed.
764//----------------------------------------------------------------------
765Error
766Process::UnloadImage (uint32_t image_token)
767{
768 Error error;
769 if (image_token < m_image_tokens.size())
770 {
771 const addr_t image_addr = m_image_tokens[image_token];
772 if (image_addr == LLDB_INVALID_ADDRESS)
773 {
774 error.SetErrorString("image already unloaded");
775 }
776 else
777 {
778 DynamicLoader *loader = GetDynamicLoader();
779 if (loader)
780 error = loader->CanLoadImage();
781
782 if (error.Success())
783 {
784 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
785 if (thread_sp == NULL)
786 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
787
788 if (thread_sp)
789 {
790 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
791
792 if (frame_sp)
793 {
794 ExecutionContext exe_ctx;
795 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +0000796 bool unwind_on_error = true;
Sean Callanan6a925532011-01-13 08:53:35 +0000797 bool keep_in_memory = false;
Greg Clayton0baa3942010-11-04 01:54:29 +0000798 StreamString expr;
799 expr.Printf("dlclose ((void *)0x%llx)", image_addr);
800 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Ingham360f53f2010-11-30 02:22:11 +0000801 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan6a925532011-01-13 08:53:35 +0000802 ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, keep_in_memory, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton0baa3942010-11-04 01:54:29 +0000803 if (result_valobj_sp->GetError().Success())
804 {
805 Scalar scalar;
806 if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar))
807 {
808 if (scalar.UInt(1))
809 {
810 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
811 }
812 else
813 {
814 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
815 }
816 }
817 }
818 else
819 {
820 error = result_valobj_sp->GetError();
821 }
822 }
823 }
824 }
825 }
826 }
827 else
828 {
829 error.SetErrorString("invalid image token");
830 }
831 return error;
832}
833
Chris Lattner24943d22010-06-08 16:52:24 +0000834DynamicLoader *
835Process::GetDynamicLoader()
836{
837 return NULL;
838}
839
840const ABI *
841Process::GetABI()
842{
843 ConstString& triple = m_target_triple;
844
845 if (triple.IsEmpty())
846 return NULL;
847
848 if (m_abi_sp.get() == NULL)
849 {
850 m_abi_sp.reset(ABI::FindPlugin(triple));
851 }
852
853 return m_abi_sp.get();
854}
855
Jim Ingham642036f2010-09-23 02:01:19 +0000856LanguageRuntime *
857Process::GetLanguageRuntime(lldb::LanguageType language)
858{
859 LanguageRuntimeCollection::iterator pos;
860 pos = m_language_runtimes.find (language);
861 if (pos == m_language_runtimes.end())
862 {
863 lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language));
864
865 m_language_runtimes[language]
866 = runtime;
867 return runtime.get();
868 }
869 else
870 return (*pos).second.get();
871}
872
873CPPLanguageRuntime *
874Process::GetCPPLanguageRuntime ()
875{
876 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus);
877 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
878 return static_cast<CPPLanguageRuntime *> (runtime);
879 return NULL;
880}
881
882ObjCLanguageRuntime *
883Process::GetObjCLanguageRuntime ()
884{
885 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC);
886 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
887 return static_cast<ObjCLanguageRuntime *> (runtime);
888 return NULL;
889}
890
Chris Lattner24943d22010-06-08 16:52:24 +0000891BreakpointSiteList &
892Process::GetBreakpointSiteList()
893{
894 return m_breakpoint_site_list;
895}
896
897const BreakpointSiteList &
898Process::GetBreakpointSiteList() const
899{
900 return m_breakpoint_site_list;
901}
902
903
904void
905Process::DisableAllBreakpointSites ()
906{
907 m_breakpoint_site_list.SetEnabledForAll (false);
908}
909
910Error
911Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
912{
913 Error error (DisableBreakpointSiteByID (break_id));
914
915 if (error.Success())
916 m_breakpoint_site_list.Remove(break_id);
917
918 return error;
919}
920
921Error
922Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
923{
924 Error error;
925 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
926 if (bp_site_sp)
927 {
928 if (bp_site_sp->IsEnabled())
929 error = DisableBreakpoint (bp_site_sp.get());
930 }
931 else
932 {
933 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
934 }
935
936 return error;
937}
938
939Error
940Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
941{
942 Error error;
943 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
944 if (bp_site_sp)
945 {
946 if (!bp_site_sp->IsEnabled())
947 error = EnableBreakpoint (bp_site_sp.get());
948 }
949 else
950 {
951 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
952 }
953 return error;
954}
955
Stephen Wilson3fd1f362010-07-17 00:56:13 +0000956lldb::break_id_t
Chris Lattner24943d22010-06-08 16:52:24 +0000957Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware)
958{
Greg Claytoneea26402010-09-14 23:36:40 +0000959 const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +0000960 if (load_addr != LLDB_INVALID_ADDRESS)
961 {
962 BreakpointSiteSP bp_site_sp;
963
964 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
965 // create a new breakpoint site and add it.
966
967 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
968
969 if (bp_site_sp)
970 {
971 bp_site_sp->AddOwner (owner);
972 owner->SetBreakpointSite (bp_site_sp);
973 return bp_site_sp->GetID();
974 }
975 else
976 {
977 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware));
978 if (bp_site_sp)
979 {
980 if (EnableBreakpoint (bp_site_sp.get()).Success())
981 {
982 owner->SetBreakpointSite (bp_site_sp);
983 return m_breakpoint_site_list.Add (bp_site_sp);
984 }
985 }
986 }
987 }
988 // We failed to enable the breakpoint
989 return LLDB_INVALID_BREAK_ID;
990
991}
992
993void
994Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
995{
996 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
997 if (num_owners == 0)
998 {
999 DisableBreakpoint(bp_site_sp.get());
1000 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1001 }
1002}
1003
1004
1005size_t
1006Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1007{
1008 size_t bytes_removed = 0;
1009 addr_t intersect_addr;
1010 size_t intersect_size;
1011 size_t opcode_offset;
1012 size_t idx;
1013 BreakpointSiteSP bp;
1014
1015 for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx)
1016 {
1017 if (bp->GetType() == BreakpointSite::eSoftware)
1018 {
1019 if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
1020 {
1021 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1022 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
1023 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1024 size_t buf_offset = intersect_addr - bp_addr;
1025 ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
1026 }
1027 }
1028 }
1029 return bytes_removed;
1030}
1031
1032
1033Error
1034Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
1035{
1036 Error error;
1037 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +00001038 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001039 const addr_t bp_addr = bp_site->GetLoadAddress();
1040 if (log)
1041 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr);
1042 if (bp_site->IsEnabled())
1043 {
1044 if (log)
1045 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
1046 return error;
1047 }
1048
1049 if (bp_addr == LLDB_INVALID_ADDRESS)
1050 {
1051 error.SetErrorString("BreakpointSite contains an invalid load address.");
1052 return error;
1053 }
1054 // Ask the lldb::Process subclass to fill in the correct software breakpoint
1055 // trap for the breakpoint site
1056 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
1057
1058 if (bp_opcode_size == 0)
1059 {
1060 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr);
1061 }
1062 else
1063 {
1064 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
1065
1066 if (bp_opcode_bytes == NULL)
1067 {
1068 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
1069 return error;
1070 }
1071
1072 // Save the original opcode by reading it
1073 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
1074 {
1075 // Write a software breakpoint in place of the original opcode
1076 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1077 {
1078 uint8_t verify_bp_opcode_bytes[64];
1079 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1080 {
1081 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
1082 {
1083 bp_site->SetEnabled(true);
1084 bp_site->SetType (BreakpointSite::eSoftware);
1085 if (log)
1086 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS",
1087 bp_site->GetID(),
1088 (uint64_t)bp_addr);
1089 }
1090 else
1091 error.SetErrorString("Failed to verify the breakpoint trap in memory.");
1092 }
1093 else
1094 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
1095 }
1096 else
1097 error.SetErrorString("Unable to write breakpoint trap to memory.");
1098 }
1099 else
1100 error.SetErrorString("Unable to read memory at breakpoint address.");
1101 }
Stephen Wilsonc2b98252011-01-12 04:20:03 +00001102 if (log && error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +00001103 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1104 bp_site->GetID(),
1105 (uint64_t)bp_addr,
1106 error.AsCString());
1107 return error;
1108}
1109
1110Error
1111Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
1112{
1113 Error error;
1114 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +00001115 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001116 addr_t bp_addr = bp_site->GetLoadAddress();
1117 lldb::user_id_t breakID = bp_site->GetID();
1118 if (log)
Stephen Wilson9ff73ed2011-01-14 21:07:07 +00001119 log->Printf ("Process::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001120
1121 if (bp_site->IsHardware())
1122 {
1123 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
1124 }
1125 else if (bp_site->IsEnabled())
1126 {
1127 const size_t break_op_size = bp_site->GetByteSize();
1128 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
1129 if (break_op_size > 0)
1130 {
1131 // Clear a software breakoint instruction
Greg Clayton54e7afa2010-07-09 20:39:50 +00001132 uint8_t curr_break_op[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +00001133 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner24943d22010-06-08 16:52:24 +00001134 bool break_op_found = false;
1135
1136 // Read the breakpoint opcode
1137 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
1138 {
1139 bool verify = false;
1140 // Make sure we have the a breakpoint opcode exists at this address
1141 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
1142 {
1143 break_op_found = true;
1144 // We found a valid breakpoint opcode at this address, now restore
1145 // the saved opcode.
1146 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
1147 {
1148 verify = true;
1149 }
1150 else
1151 error.SetErrorString("Memory write failed when restoring original opcode.");
1152 }
1153 else
1154 {
1155 error.SetErrorString("Original breakpoint trap is no longer in memory.");
1156 // Set verify to true and so we can check if the original opcode has already been restored
1157 verify = true;
1158 }
1159
1160 if (verify)
1161 {
Greg Clayton54e7afa2010-07-09 20:39:50 +00001162 uint8_t verify_opcode[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +00001163 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner24943d22010-06-08 16:52:24 +00001164 // Verify that our original opcode made it back to the inferior
1165 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
1166 {
1167 // compare the memory we just read with the original opcode
1168 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
1169 {
1170 // SUCCESS
1171 bp_site->SetEnabled(false);
1172 if (log)
1173 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
1174 return error;
1175 }
1176 else
1177 {
1178 if (break_op_found)
1179 error.SetErrorString("Failed to restore original opcode.");
1180 }
1181 }
1182 else
1183 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
1184 }
1185 }
1186 else
1187 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
1188 }
1189 }
1190 else
1191 {
1192 if (log)
1193 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
1194 return error;
1195 }
1196
1197 if (log)
1198 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1199 bp_site->GetID(),
1200 (uint64_t)bp_addr,
1201 error.AsCString());
1202 return error;
1203
1204}
1205
Greg Claytonfd119992011-01-07 06:08:19 +00001206// Comment out line below to disable memory caching
1207#define ENABLE_MEMORY_CACHING
1208// Uncomment to verify memory caching works after making changes to caching code
1209//#define VERIFY_MEMORY_READS
1210
1211#if defined (ENABLE_MEMORY_CACHING)
1212
1213#if defined (VERIFY_MEMORY_READS)
Chris Lattner24943d22010-06-08 16:52:24 +00001214
1215size_t
1216Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1217{
Greg Claytonfd119992011-01-07 06:08:19 +00001218 // Memory caching is enabled, with debug verification
1219 if (buf && size)
1220 {
1221 // Uncomment the line below to make sure memory caching is working.
1222 // I ran this through the test suite and got no assertions, so I am
1223 // pretty confident this is working well. If any changes are made to
1224 // memory caching, uncomment the line below and test your changes!
1225
1226 // Verify all memory reads by using the cache first, then redundantly
1227 // reading the same memory from the inferior and comparing to make sure
1228 // everything is exactly the same.
1229 std::string verify_buf (size, '\0');
1230 assert (verify_buf.size() == size);
1231 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
1232 Error verify_error;
1233 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
1234 assert (cache_bytes_read == verify_bytes_read);
1235 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
1236 assert (verify_error.Success() == error.Success());
1237 return cache_bytes_read;
1238 }
1239 return 0;
1240}
1241
1242#else // #if defined (VERIFY_MEMORY_READS)
1243
1244size_t
1245Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1246{
1247 // Memory caching enabled, no verification
1248 return m_memory_cache.Read (this, addr, buf, size, error);
1249}
1250
1251#endif // #else for #if defined (VERIFY_MEMORY_READS)
1252
1253#else // #if defined (ENABLE_MEMORY_CACHING)
1254
1255size_t
1256Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1257{
1258 // Memory caching is disabled
1259 return ReadMemoryFromInferior (addr, buf, size, error);
1260}
1261
1262#endif // #else for #if defined (ENABLE_MEMORY_CACHING)
1263
1264
1265size_t
1266Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
1267{
Chris Lattner24943d22010-06-08 16:52:24 +00001268 if (buf == NULL || size == 0)
1269 return 0;
1270
1271 size_t bytes_read = 0;
1272 uint8_t *bytes = (uint8_t *)buf;
1273
1274 while (bytes_read < size)
1275 {
1276 const size_t curr_size = size - bytes_read;
1277 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
1278 bytes + bytes_read,
1279 curr_size,
1280 error);
1281 bytes_read += curr_bytes_read;
1282 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
1283 break;
1284 }
1285
1286 // Replace any software breakpoint opcodes that fall into this range back
1287 // into "buf" before we return
1288 if (bytes_read > 0)
1289 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
1290 return bytes_read;
1291}
1292
Greg Claytonf72fdee2010-12-16 20:01:20 +00001293uint64_t
1294Process::ReadUnsignedInteger (lldb::addr_t vm_addr, size_t integer_byte_size, Error &error)
1295{
1296 if (integer_byte_size > sizeof(uint64_t))
1297 {
1298 error.SetErrorString ("unsupported integer size");
1299 }
1300 else
1301 {
1302 uint8_t tmp[sizeof(uint64_t)];
1303 DataExtractor data (tmp, integer_byte_size, GetByteOrder(), GetAddressByteSize());
1304 if (ReadMemory (vm_addr, tmp, integer_byte_size, error) == integer_byte_size)
1305 {
1306 uint32_t offset = 0;
1307 return data.GetMaxU64 (&offset, integer_byte_size);
1308 }
1309 }
1310 // Any plug-in that doesn't return success a memory read with the number
1311 // of bytes that were requested should be setting the error
1312 assert (error.Fail());
1313 return 0;
1314}
1315
Chris Lattner24943d22010-06-08 16:52:24 +00001316size_t
1317Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
1318{
1319 size_t bytes_written = 0;
1320 const uint8_t *bytes = (const uint8_t *)buf;
1321
1322 while (bytes_written < size)
1323 {
1324 const size_t curr_size = size - bytes_written;
1325 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
1326 bytes + bytes_written,
1327 curr_size,
1328 error);
1329 bytes_written += curr_bytes_written;
1330 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
1331 break;
1332 }
1333 return bytes_written;
1334}
1335
1336size_t
1337Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1338{
Greg Claytonfd119992011-01-07 06:08:19 +00001339#if defined (ENABLE_MEMORY_CACHING)
1340 m_memory_cache.Flush (addr, size);
1341#endif
1342
Chris Lattner24943d22010-06-08 16:52:24 +00001343 if (buf == NULL || size == 0)
1344 return 0;
1345 // We need to write any data that would go where any current software traps
1346 // (enabled software breakpoints) any software traps (breakpoints) that we
1347 // may have placed in our tasks memory.
1348
1349 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
1350 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
1351
1352 if (iter == end || iter->second->GetLoadAddress() > addr + size)
1353 return DoWriteMemory(addr, buf, size, error);
1354
1355 BreakpointSiteList::collection::const_iterator pos;
1356 size_t bytes_written = 0;
Greg Clayton54e7afa2010-07-09 20:39:50 +00001357 addr_t intersect_addr = 0;
1358 size_t intersect_size = 0;
1359 size_t opcode_offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001360 const uint8_t *ubuf = (const uint8_t *)buf;
1361
1362 for (pos = iter; pos != end; ++pos)
1363 {
1364 BreakpointSiteSP bp;
1365 bp = pos->second;
1366
1367 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
1368 assert(addr <= intersect_addr && intersect_addr < addr + size);
1369 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
1370 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1371
1372 // Check for bytes before this breakpoint
1373 const addr_t curr_addr = addr + bytes_written;
1374 if (intersect_addr > curr_addr)
1375 {
1376 // There are some bytes before this breakpoint that we need to
1377 // just write to memory
1378 size_t curr_size = intersect_addr - curr_addr;
1379 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
1380 ubuf + bytes_written,
1381 curr_size,
1382 error);
1383 bytes_written += curr_bytes_written;
1384 if (curr_bytes_written != curr_size)
1385 {
1386 // We weren't able to write all of the requested bytes, we
1387 // are done looping and will return the number of bytes that
1388 // we have written so far.
1389 break;
1390 }
1391 }
1392
1393 // Now write any bytes that would cover up any software breakpoints
1394 // directly into the breakpoint opcode buffer
1395 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
1396 bytes_written += intersect_size;
1397 }
1398
1399 // Write any remaining bytes after the last breakpoint if we have any left
1400 if (bytes_written < size)
1401 bytes_written += WriteMemoryPrivate (addr + bytes_written,
1402 ubuf + bytes_written,
1403 size - bytes_written,
1404 error);
1405
1406 return bytes_written;
1407}
1408
1409addr_t
1410Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
1411{
1412 // Fixme: we should track the blocks we've allocated, and clean them up...
1413 // We could even do our own allocator here if that ends up being more efficient.
Greg Clayton2860ba92011-01-23 19:58:49 +00001414 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
1415 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1416 if (log)
Greg Claytonb349adc2011-01-24 06:30:45 +00001417 log->Printf("Process::AllocateMemory(size=%4zu, permissions=%c%c%c) => 0x%16.16llx (m_stop_id = %u)",
Greg Clayton2860ba92011-01-23 19:58:49 +00001418 size,
1419 permissions & ePermissionsReadable ? 'r' : '-',
1420 permissions & ePermissionsWritable ? 'w' : '-',
1421 permissions & ePermissionsExecutable ? 'x' : '-',
1422 (uint64_t)allocated_addr,
1423 m_stop_id);
1424 return allocated_addr;
Chris Lattner24943d22010-06-08 16:52:24 +00001425}
1426
1427Error
1428Process::DeallocateMemory (addr_t ptr)
1429{
Greg Clayton2860ba92011-01-23 19:58:49 +00001430 Error error(DoDeallocateMemory (ptr));
1431
1432 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1433 if (log)
1434 log->Printf("Process::DeallocateMemory(addr=0x%16.16llx) => err = %s (m_stop_id = %u)",
1435 ptr,
1436 error.AsCString("SUCCESS"),
1437 m_stop_id);
1438 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00001439}
1440
1441
1442Error
1443Process::EnableWatchpoint (WatchpointLocation *watchpoint)
1444{
1445 Error error;
1446 error.SetErrorString("watchpoints are not supported");
1447 return error;
1448}
1449
1450Error
1451Process::DisableWatchpoint (WatchpointLocation *watchpoint)
1452{
1453 Error error;
1454 error.SetErrorString("watchpoints are not supported");
1455 return error;
1456}
1457
1458StateType
1459Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
1460{
1461 StateType state;
1462 // Now wait for the process to launch and return control to us, and then
1463 // call DidLaunch:
1464 while (1)
1465 {
Greg Clayton72e1c782011-01-22 23:43:18 +00001466 event_sp.reset();
1467 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
1468
1469 if (StateIsStoppedState(state))
Chris Lattner24943d22010-06-08 16:52:24 +00001470 break;
Greg Clayton72e1c782011-01-22 23:43:18 +00001471
1472 // If state is invalid, then we timed out
1473 if (state == eStateInvalid)
1474 break;
1475
1476 if (event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001477 HandlePrivateEvent (event_sp);
1478 }
1479 return state;
1480}
1481
1482Error
1483Process::Launch
1484(
1485 char const *argv[],
1486 char const *envp[],
Greg Clayton452bf612010-08-31 18:35:14 +00001487 uint32_t launch_flags,
Chris Lattner24943d22010-06-08 16:52:24 +00001488 const char *stdin_path,
1489 const char *stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +00001490 const char *stderr_path,
1491 const char *working_directory
Chris Lattner24943d22010-06-08 16:52:24 +00001492)
1493{
1494 Error error;
1495 m_target_triple.Clear();
1496 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001497 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001498
1499 Module *exe_module = m_target.GetExecutableModule().get();
1500 if (exe_module)
1501 {
1502 char exec_file_path[PATH_MAX];
1503 exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path));
1504 if (exe_module->GetFileSpec().Exists())
1505 {
1506 error = WillLaunch (exe_module);
1507 if (error.Success())
1508 {
Greg Claytond8c62532010-10-07 04:19:01 +00001509 SetPublicState (eStateLaunching);
Chris Lattner24943d22010-06-08 16:52:24 +00001510 // The args coming in should not contain the application name, the
1511 // lldb_private::Process class will add this in case the executable
1512 // gets resolved to a different file than was given on the command
1513 // line (like when an applicaiton bundle is specified and will
1514 // resolve to the contained exectuable file, or the file given was
1515 // a symlink or other file system link that resolves to a different
1516 // file).
1517
1518 // Get the resolved exectuable path
1519
1520 // Make a new argument vector
1521 std::vector<const char *> exec_path_plus_argv;
1522 // Append the resolved executable path
1523 exec_path_plus_argv.push_back (exec_file_path);
1524
1525 // Push all args if there are any
1526 if (argv)
1527 {
1528 for (int i = 0; argv[i]; ++i)
1529 exec_path_plus_argv.push_back(argv[i]);
1530 }
1531
1532 // Push a NULL to terminate the args.
1533 exec_path_plus_argv.push_back(NULL);
1534
1535 // Now launch using these arguments.
Greg Clayton53d68e72010-07-20 22:52:08 +00001536 error = DoLaunch (exe_module,
1537 exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(),
1538 envp,
Greg Clayton452bf612010-08-31 18:35:14 +00001539 launch_flags,
Greg Clayton53d68e72010-07-20 22:52:08 +00001540 stdin_path,
1541 stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +00001542 stderr_path,
1543 working_directory);
Chris Lattner24943d22010-06-08 16:52:24 +00001544
1545 if (error.Fail())
1546 {
1547 if (GetID() != LLDB_INVALID_PROCESS_ID)
1548 {
1549 SetID (LLDB_INVALID_PROCESS_ID);
1550 const char *error_string = error.AsCString();
1551 if (error_string == NULL)
1552 error_string = "launch failed";
1553 SetExitStatus (-1, error_string);
1554 }
1555 }
1556 else
1557 {
1558 EventSP event_sp;
1559 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1560
1561 if (state == eStateStopped || state == eStateCrashed)
1562 {
1563 DidLaunch ();
1564
1565 // This delays passing the stopped event to listeners till DidLaunch gets
1566 // a chance to complete...
1567 HandlePrivateEvent (event_sp);
1568 StartPrivateStateThread ();
1569 }
1570 else if (state == eStateExited)
1571 {
1572 // We exited while trying to launch somehow. Don't call DidLaunch as that's
1573 // not likely to work, and return an invalid pid.
1574 HandlePrivateEvent (event_sp);
1575 }
1576 }
1577 }
1578 }
1579 else
1580 {
1581 error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path);
1582 }
1583 }
1584 return error;
1585}
1586
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001587Process::NextEventAction::EventActionResult
1588Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001589{
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001590 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
1591 switch (state)
Greg Claytonc1d37752010-10-18 01:45:30 +00001592 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001593 case eStateRunning:
1594 return eEventActionRetry;
1595
1596 case eStateStopped:
1597 case eStateCrashed:
Jim Ingham7508e732010-08-09 23:31:02 +00001598 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001599 // During attach, prior to sending the eStateStopped event,
1600 // lldb_private::Process subclasses must set the process must set
1601 // the new process ID.
1602 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
1603 m_process->DidAttach ();
1604 // Figure out which one is the executable, and set that in our target:
1605 ModuleList &modules = m_process->GetTarget().GetImages();
1606
1607 size_t num_modules = modules.GetSize();
1608 for (int i = 0; i < num_modules; i++)
Jim Ingham7508e732010-08-09 23:31:02 +00001609 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001610 ModuleSP module_sp = modules.GetModuleAtIndex(i);
1611 if (module_sp->IsExecutable())
Jim Ingham7508e732010-08-09 23:31:02 +00001612 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001613 ModuleSP exec_module = m_process->GetTarget().GetExecutableModule();
1614 if (!exec_module || exec_module != module_sp)
1615 {
1616
1617 m_process->GetTarget().SetExecutableModule (module_sp, false);
1618 }
1619 break;
Jim Ingham7508e732010-08-09 23:31:02 +00001620 }
Jim Ingham7508e732010-08-09 23:31:02 +00001621 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001622 return eEventActionSuccess;
Jim Ingham7508e732010-08-09 23:31:02 +00001623 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001624
1625
1626 break;
1627 default:
1628 case eStateExited:
1629 case eStateInvalid:
1630 m_exit_string.assign ("No valid Process");
1631 return eEventActionExit;
1632 break;
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001633 }
1634}
Chris Lattner24943d22010-06-08 16:52:24 +00001635
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001636Process::NextEventAction::EventActionResult
1637Process::AttachCompletionHandler::HandleBeingInterrupted()
1638{
1639 return eEventActionSuccess;
1640}
1641
1642const char *
1643Process::AttachCompletionHandler::GetExitString ()
1644{
1645 return m_exit_string.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +00001646}
1647
1648Error
1649Process::Attach (lldb::pid_t attach_pid)
1650{
1651
1652 m_target_triple.Clear();
1653 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001654 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001655
Jim Ingham7508e732010-08-09 23:31:02 +00001656 // Find the process and its architecture. Make sure it matches the architecture
1657 // of the current Target, and if not adjust it.
1658
1659 ArchSpec attach_spec = GetArchSpecForExistingProcess (attach_pid);
1660 if (attach_spec != GetTarget().GetArchitecture())
1661 {
1662 // Set the architecture on the target.
1663 GetTarget().SetArchitecture(attach_spec);
1664 }
1665
Greg Clayton54e7afa2010-07-09 20:39:50 +00001666 Error error (WillAttachToProcessWithID(attach_pid));
Chris Lattner24943d22010-06-08 16:52:24 +00001667 if (error.Success())
1668 {
Greg Claytond8c62532010-10-07 04:19:01 +00001669 SetPublicState (eStateAttaching);
1670
Greg Clayton54e7afa2010-07-09 20:39:50 +00001671 error = DoAttachToProcessWithID (attach_pid);
Chris Lattner24943d22010-06-08 16:52:24 +00001672 if (error.Success())
1673 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001674 SetNextEventAction(new Process::AttachCompletionHandler(this));
1675 StartPrivateStateThread();
Chris Lattner24943d22010-06-08 16:52:24 +00001676 }
1677 else
1678 {
1679 if (GetID() != LLDB_INVALID_PROCESS_ID)
1680 {
1681 SetID (LLDB_INVALID_PROCESS_ID);
1682 const char *error_string = error.AsCString();
1683 if (error_string == NULL)
1684 error_string = "attach failed";
1685
1686 SetExitStatus(-1, error_string);
1687 }
1688 }
1689 }
1690 return error;
1691}
1692
1693Error
1694Process::Attach (const char *process_name, bool wait_for_launch)
1695{
1696 m_target_triple.Clear();
1697 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001698 m_process_input_reader.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001699
1700 // Find the process and its architecture. Make sure it matches the architecture
1701 // of the current Target, and if not adjust it.
1702
Jim Inghamea294182010-08-17 21:54:19 +00001703 if (!wait_for_launch)
Jim Ingham7508e732010-08-09 23:31:02 +00001704 {
Jim Inghamea294182010-08-17 21:54:19 +00001705 ArchSpec attach_spec = GetArchSpecForExistingProcess (process_name);
Greg Claytonc1d37752010-10-18 01:45:30 +00001706 if (attach_spec.IsValid() && attach_spec != GetTarget().GetArchitecture())
Jim Inghamea294182010-08-17 21:54:19 +00001707 {
1708 // Set the architecture on the target.
1709 GetTarget().SetArchitecture(attach_spec);
1710 }
Jim Ingham7508e732010-08-09 23:31:02 +00001711 }
Jim Inghamea294182010-08-17 21:54:19 +00001712
Greg Clayton54e7afa2010-07-09 20:39:50 +00001713 Error error (WillAttachToProcessWithName(process_name, wait_for_launch));
Chris Lattner24943d22010-06-08 16:52:24 +00001714 if (error.Success())
1715 {
Greg Claytond8c62532010-10-07 04:19:01 +00001716 SetPublicState (eStateAttaching);
Greg Clayton54e7afa2010-07-09 20:39:50 +00001717 error = DoAttachToProcessWithName (process_name, wait_for_launch);
Chris Lattner24943d22010-06-08 16:52:24 +00001718 if (error.Fail())
1719 {
1720 if (GetID() != LLDB_INVALID_PROCESS_ID)
1721 {
1722 SetID (LLDB_INVALID_PROCESS_ID);
1723 const char *error_string = error.AsCString();
1724 if (error_string == NULL)
1725 error_string = "attach failed";
1726
1727 SetExitStatus(-1, error_string);
1728 }
1729 }
1730 else
1731 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001732 SetNextEventAction(new Process::AttachCompletionHandler(this));
1733 StartPrivateStateThread();
Chris Lattner24943d22010-06-08 16:52:24 +00001734 }
1735 }
1736 return error;
1737}
1738
1739Error
Greg Claytone71e2582011-02-04 01:58:07 +00001740Process::ConnectRemote (const char *remote_url)
1741{
1742 m_target_triple.Clear();
1743 m_abi_sp.reset();
1744 m_process_input_reader.reset();
1745
1746 // Find the process and its architecture. Make sure it matches the architecture
1747 // of the current Target, and if not adjust it.
1748
1749 Error error (DoConnectRemote (remote_url));
1750 if (error.Success())
1751 {
1752 SetNextEventAction(new Process::AttachCompletionHandler(this));
1753 StartPrivateStateThread();
1754// TimeValue timeout;
1755// timeout = TimeValue::Now();
1756// timeout.OffsetWithMicroSeconds(000);
1757// EventSP event_sp;
1758// StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1759//
1760// if (state == eStateStopped || state == eStateCrashed)
1761// {
1762// DidLaunch ();
1763//
1764// // This delays passing the stopped event to listeners till DidLaunch gets
1765// // a chance to complete...
1766// HandlePrivateEvent (event_sp);
1767// StartPrivateStateThread ();
1768// }
1769// else if (state == eStateExited)
1770// {
1771// // We exited while trying to launch somehow. Don't call DidLaunch as that's
1772// // not likely to work, and return an invalid pid.
1773// HandlePrivateEvent (event_sp);
1774// }
1775//
1776// StartPrivateStateThread();
1777 }
1778 return error;
1779}
1780
1781
1782Error
Chris Lattner24943d22010-06-08 16:52:24 +00001783Process::Resume ()
1784{
Greg Claytone005f2c2010-11-06 01:53:30 +00001785 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001786 if (log)
Jim Inghamac959662011-01-24 06:34:17 +00001787 log->Printf("Process::Resume() m_stop_id = %u, public state: %s private state: %s",
1788 m_stop_id,
1789 StateAsCString(m_public_state.GetValue()),
1790 StateAsCString(m_private_state.GetValue()));
Chris Lattner24943d22010-06-08 16:52:24 +00001791
1792 Error error (WillResume());
1793 // Tell the process it is about to resume before the thread list
1794 if (error.Success())
1795 {
Johnny Chen9c11d472010-12-02 20:53:05 +00001796 // Now let the thread list know we are about to resume so it
Chris Lattner24943d22010-06-08 16:52:24 +00001797 // can let all of our threads know that they are about to be
1798 // resumed. Threads will each be called with
1799 // Thread::WillResume(StateType) where StateType contains the state
1800 // that they are supposed to have when the process is resumed
1801 // (suspended/running/stepping). Threads should also check
1802 // their resume signal in lldb::Thread::GetResumeSignal()
1803 // to see if they are suppoed to start back up with a signal.
1804 if (m_thread_list.WillResume())
1805 {
1806 error = DoResume();
1807 if (error.Success())
1808 {
1809 DidResume();
1810 m_thread_list.DidResume();
Jim Inghamac959662011-01-24 06:34:17 +00001811 if (log)
1812 log->Printf ("Process thinks the process has resumed.");
Chris Lattner24943d22010-06-08 16:52:24 +00001813 }
1814 }
1815 else
1816 {
Jim Inghamac959662011-01-24 06:34:17 +00001817 error.SetErrorStringWithFormat("Process::WillResume() thread list returned false after WillResume");
Chris Lattner24943d22010-06-08 16:52:24 +00001818 }
1819 }
Jim Inghamac959662011-01-24 06:34:17 +00001820 else if (log)
1821 log->Printf ("Process::WillResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner24943d22010-06-08 16:52:24 +00001822 return error;
1823}
1824
1825Error
1826Process::Halt ()
1827{
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001828 // Pause our private state thread so we can ensure no one else eats
1829 // the stop event out from under us.
1830 PausePrivateStateThread();
Greg Clayton20d338f2010-11-18 05:57:03 +00001831
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001832 EventSP event_sp;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001833 Error error (WillHalt());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001834
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001835 if (error.Success())
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001836 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001837
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001838 bool caused_stop = false;
1839
1840 // Ask the process subclass to actually halt our process
1841 error = DoHalt(caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +00001842 if (error.Success())
Jim Ingham3ae449a2010-11-17 02:32:00 +00001843 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001844 if (m_public_state.GetValue() == eStateAttaching)
1845 {
1846 SetExitStatus(SIGKILL, "Cancelled async attach.");
1847 Destroy ();
1848 }
1849 else
Jim Ingham3ae449a2010-11-17 02:32:00 +00001850 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001851 // If "caused_stop" is true, then DoHalt stopped the process. If
1852 // "caused_stop" is false, the process was already stopped.
1853 // If the DoHalt caused the process to stop, then we want to catch
1854 // this event and set the interrupted bool to true before we pass
1855 // this along so clients know that the process was interrupted by
1856 // a halt command.
1857 if (caused_stop)
Greg Clayton20d338f2010-11-18 05:57:03 +00001858 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001859 // Wait for 2 seconds for the process to stop.
1860 TimeValue timeout_time;
1861 timeout_time = TimeValue::Now();
1862 timeout_time.OffsetWithSeconds(1);
1863 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, event_sp);
1864
1865 if (state == eStateInvalid)
Greg Clayton20d338f2010-11-18 05:57:03 +00001866 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001867 // We timeout out and didn't get a stop event...
1868 error.SetErrorString ("Halt timed out.");
Greg Clayton20d338f2010-11-18 05:57:03 +00001869 }
1870 else
1871 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001872 if (StateIsStoppedState (state))
1873 {
1874 // We caused the process to interrupt itself, so mark this
1875 // as such in the stop event so clients can tell an interrupted
1876 // process from a natural stop
1877 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
1878 }
1879 else
1880 {
1881 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1882 if (log)
1883 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
1884 error.SetErrorString ("Did not get stopped event after halt.");
1885 }
Greg Clayton20d338f2010-11-18 05:57:03 +00001886 }
1887 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001888 DidHalt();
Jim Ingham3ae449a2010-11-17 02:32:00 +00001889 }
1890 }
Chris Lattner24943d22010-06-08 16:52:24 +00001891 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001892 // Resume our private state thread before we post the event (if any)
1893 ResumePrivateStateThread();
1894
1895 // Post any event we might have consumed. If all goes well, we will have
1896 // stopped the process, intercepted the event and set the interrupted
1897 // bool in the event. Post it to the private event queue and that will end up
1898 // correctly setting the state.
1899 if (event_sp)
1900 m_private_state_broadcaster.BroadcastEvent(event_sp);
1901
Chris Lattner24943d22010-06-08 16:52:24 +00001902 return error;
1903}
1904
1905Error
1906Process::Detach ()
1907{
1908 Error error (WillDetach());
1909
1910 if (error.Success())
1911 {
1912 DisableAllBreakpointSites();
1913 error = DoDetach();
1914 if (error.Success())
1915 {
1916 DidDetach();
1917 StopPrivateStateThread();
1918 }
1919 }
1920 return error;
1921}
1922
1923Error
1924Process::Destroy ()
1925{
1926 Error error (WillDestroy());
1927 if (error.Success())
1928 {
1929 DisableAllBreakpointSites();
1930 error = DoDestroy();
1931 if (error.Success())
1932 {
1933 DidDestroy();
1934 StopPrivateStateThread();
1935 }
Caroline Tice861efb32010-11-16 05:07:41 +00001936 m_stdio_communication.StopReadThread();
1937 m_stdio_communication.Disconnect();
1938 if (m_process_input_reader && m_process_input_reader->IsActive())
1939 m_target.GetDebugger().PopInputReader (m_process_input_reader);
1940 if (m_process_input_reader)
1941 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001942 }
1943 return error;
1944}
1945
1946Error
1947Process::Signal (int signal)
1948{
1949 Error error (WillSignal());
1950 if (error.Success())
1951 {
1952 error = DoSignal(signal);
1953 if (error.Success())
1954 DidSignal();
1955 }
1956 return error;
1957}
1958
1959UnixSignals &
1960Process::GetUnixSignals ()
1961{
1962 return m_unix_signals;
1963}
1964
1965Target &
1966Process::GetTarget ()
1967{
1968 return m_target;
1969}
1970
1971const Target &
1972Process::GetTarget () const
1973{
1974 return m_target;
1975}
1976
1977uint32_t
1978Process::GetAddressByteSize()
1979{
Greg Clayton20d338f2010-11-18 05:57:03 +00001980 if (m_addr_byte_size == 0)
1981 return m_target.GetArchitecture().GetAddressByteSize();
1982 return m_addr_byte_size;
Chris Lattner24943d22010-06-08 16:52:24 +00001983}
1984
1985bool
1986Process::ShouldBroadcastEvent (Event *event_ptr)
1987{
1988 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
1989 bool return_value = true;
Greg Claytone005f2c2010-11-06 01:53:30 +00001990 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001991
1992 switch (state)
1993 {
Greg Claytone71e2582011-02-04 01:58:07 +00001994 case eStateConnected:
Chris Lattner24943d22010-06-08 16:52:24 +00001995 case eStateAttaching:
1996 case eStateLaunching:
1997 case eStateDetached:
1998 case eStateExited:
1999 case eStateUnloaded:
2000 // These events indicate changes in the state of the debugging session, always report them.
2001 return_value = true;
2002 break;
2003 case eStateInvalid:
2004 // We stopped for no apparent reason, don't report it.
2005 return_value = false;
2006 break;
2007 case eStateRunning:
2008 case eStateStepping:
2009 // If we've started the target running, we handle the cases where we
2010 // are already running and where there is a transition from stopped to
2011 // running differently.
2012 // running -> running: Automatically suppress extra running events
2013 // stopped -> running: Report except when there is one or more no votes
2014 // and no yes votes.
2015 SynchronouslyNotifyStateChanged (state);
2016 switch (m_public_state.GetValue())
2017 {
2018 case eStateRunning:
2019 case eStateStepping:
2020 // We always suppress multiple runnings with no PUBLIC stop in between.
2021 return_value = false;
2022 break;
2023 default:
2024 // TODO: make this work correctly. For now always report
2025 // run if we aren't running so we don't miss any runnning
2026 // events. If I run the lldb/test/thread/a.out file and
2027 // break at main.cpp:58, run and hit the breakpoints on
2028 // multiple threads, then somehow during the stepping over
2029 // of all breakpoints no run gets reported.
2030 return_value = true;
2031
2032 // This is a transition from stop to run.
2033 switch (m_thread_list.ShouldReportRun (event_ptr))
2034 {
2035 case eVoteYes:
2036 case eVoteNoOpinion:
2037 return_value = true;
2038 break;
2039 case eVoteNo:
2040 return_value = false;
2041 break;
2042 }
2043 break;
2044 }
2045 break;
2046 case eStateStopped:
2047 case eStateCrashed:
2048 case eStateSuspended:
2049 {
2050 // We've stopped. First see if we're going to restart the target.
2051 // If we are going to stop, then we always broadcast the event.
2052 // 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 +00002053 // If no thread has an opinion, we don't report it.
Jim Ingham3ae449a2010-11-17 02:32:00 +00002054 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner24943d22010-06-08 16:52:24 +00002055 {
Greg Clayton20d338f2010-11-18 05:57:03 +00002056 if (log)
2057 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", event_ptr, StateAsCString(state));
Jim Ingham3ae449a2010-11-17 02:32:00 +00002058 return true;
2059 }
2060 else
2061 {
Chris Lattner24943d22010-06-08 16:52:24 +00002062 RefreshStateAfterStop ();
2063
2064 if (m_thread_list.ShouldStop (event_ptr) == false)
2065 {
2066 switch (m_thread_list.ShouldReportStop (event_ptr))
2067 {
2068 case eVoteYes:
2069 Process::ProcessEventData::SetRestartedInEvent (event_ptr, true);
Johnny Chen028784b2010-10-14 00:54:32 +00002070 // Intentional fall-through here.
Chris Lattner24943d22010-06-08 16:52:24 +00002071 case eVoteNoOpinion:
Chris Lattner24943d22010-06-08 16:52:24 +00002072 case eVoteNo:
2073 return_value = false;
2074 break;
2075 }
2076
2077 if (log)
Jim Ingham3ae449a2010-11-17 02:32:00 +00002078 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state));
Chris Lattner24943d22010-06-08 16:52:24 +00002079 Resume ();
2080 }
2081 else
2082 {
2083 return_value = true;
2084 SynchronouslyNotifyStateChanged (state);
2085 }
2086 }
2087 }
2088 }
2089
2090 if (log)
2091 log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO");
2092 return return_value;
2093}
2094
2095//------------------------------------------------------------------
2096// Thread Queries
2097//------------------------------------------------------------------
2098
2099ThreadList &
2100Process::GetThreadList ()
2101{
2102 return m_thread_list;
2103}
2104
2105const ThreadList &
2106Process::GetThreadList () const
2107{
2108 return m_thread_list;
2109}
2110
2111
2112bool
2113Process::StartPrivateStateThread ()
2114{
Greg Claytone005f2c2010-11-06 01:53:30 +00002115 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002116
2117 if (log)
2118 log->Printf ("Process::%s ( )", __FUNCTION__);
2119
2120 // Create a thread that watches our internal state and controls which
2121 // events make it to clients (into the DCProcess event queue).
Greg Claytona875b642011-01-09 21:07:35 +00002122 char thread_name[1024];
2123 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%i)>", GetID());
2124 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002125 return IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002126}
2127
2128void
2129Process::PausePrivateStateThread ()
2130{
2131 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
2132}
2133
2134void
2135Process::ResumePrivateStateThread ()
2136{
2137 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
2138}
2139
2140void
2141Process::StopPrivateStateThread ()
2142{
2143 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
2144}
2145
2146void
2147Process::ControlPrivateStateThread (uint32_t signal)
2148{
Greg Claytone005f2c2010-11-06 01:53:30 +00002149 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002150
2151 assert (signal == eBroadcastInternalStateControlStop ||
2152 signal == eBroadcastInternalStateControlPause ||
2153 signal == eBroadcastInternalStateControlResume);
2154
2155 if (log)
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002156 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner24943d22010-06-08 16:52:24 +00002157
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002158 // Signal the private state thread. First we should copy this is case the
2159 // thread starts exiting since the private state thread will NULL this out
2160 // when it exits
2161 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton09c81ef2011-02-08 01:34:25 +00002162 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00002163 {
2164 TimeValue timeout_time;
2165 bool timed_out;
2166
2167 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
2168
2169 timeout_time = TimeValue::Now();
2170 timeout_time.OffsetWithSeconds(2);
2171 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
2172 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2173
2174 if (signal == eBroadcastInternalStateControlStop)
2175 {
2176 if (timed_out)
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002177 Host::ThreadCancel (private_state_thread, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +00002178
2179 thread_result_t result = NULL;
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002180 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Claytonc607d862010-07-22 18:34:21 +00002181 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00002182 }
2183 }
2184}
2185
2186void
2187Process::HandlePrivateEvent (EventSP &event_sp)
2188{
Greg Claytone005f2c2010-11-06 01:53:30 +00002189 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002190
Greg Clayton68ca8232011-01-25 02:58:48 +00002191 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002192
2193 // First check to see if anybody wants a shot at this event:
Jim Ingham68bffc52011-01-29 04:05:41 +00002194 if (m_next_event_action_ap.get() != NULL)
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002195 {
Jim Ingham68bffc52011-01-29 04:05:41 +00002196 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002197 switch (action_result)
2198 {
2199 case NextEventAction::eEventActionSuccess:
2200 SetNextEventAction(NULL);
2201 break;
2202 case NextEventAction::eEventActionRetry:
2203 break;
2204 case NextEventAction::eEventActionExit:
Jim Ingham84c86382011-01-29 01:57:31 +00002205 // Handle Exiting Here. If we already got an exited event,
2206 // we should just propagate it. Otherwise, swallow this event,
2207 // and set our state to exit so the next event will kill us.
2208 if (new_state != eStateExited)
2209 {
2210 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham68bffc52011-01-29 04:05:41 +00002211 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham84c86382011-01-29 01:57:31 +00002212 SetNextEventAction(NULL);
2213 return;
2214 }
2215 SetNextEventAction(NULL);
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002216 break;
2217 }
2218 }
2219
Chris Lattner24943d22010-06-08 16:52:24 +00002220 // See if we should broadcast this state to external clients?
2221 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00002222
2223 if (should_broadcast)
2224 {
2225 if (log)
2226 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002227 log->Printf ("Process::%s (pid = %i) broadcasting new state %s (old state %s) to %s",
2228 __FUNCTION__,
2229 GetID(),
2230 StateAsCString(new_state),
2231 StateAsCString (GetState ()),
2232 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner24943d22010-06-08 16:52:24 +00002233 }
Greg Clayton68ca8232011-01-25 02:58:48 +00002234 if (StateIsRunningState (new_state))
Caroline Tice861efb32010-11-16 05:07:41 +00002235 PushProcessInputReader ();
2236 else
2237 PopProcessInputReader ();
Chris Lattner24943d22010-06-08 16:52:24 +00002238 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
2239 BroadcastEvent (event_sp);
2240 }
2241 else
2242 {
2243 if (log)
2244 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002245 log->Printf ("Process::%s (pid = %i) suppressing state %s (old state %s): should_broadcast == false",
2246 __FUNCTION__,
2247 GetID(),
2248 StateAsCString(new_state),
2249 StateAsCString (GetState ()),
2250 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner24943d22010-06-08 16:52:24 +00002251 }
2252 }
2253}
2254
2255void *
2256Process::PrivateStateThread (void *arg)
2257{
2258 Process *proc = static_cast<Process*> (arg);
2259 void *result = proc->RunPrivateStateThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00002260 return result;
2261}
2262
2263void *
2264Process::RunPrivateStateThread ()
2265{
2266 bool control_only = false;
2267 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2268
Greg Claytone005f2c2010-11-06 01:53:30 +00002269 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002270 if (log)
2271 log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID());
2272
2273 bool exit_now = false;
2274 while (!exit_now)
2275 {
2276 EventSP event_sp;
2277 WaitForEventsPrivate (NULL, event_sp, control_only);
2278 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
2279 {
2280 switch (event_sp->GetType())
2281 {
2282 case eBroadcastInternalStateControlStop:
2283 exit_now = true;
2284 continue; // Go to next loop iteration so we exit without
2285 break; // doing any internal state managment below
2286
2287 case eBroadcastInternalStateControlPause:
2288 control_only = true;
2289 break;
2290
2291 case eBroadcastInternalStateControlResume:
2292 control_only = false;
2293 break;
2294 }
Jim Ingham3ae449a2010-11-17 02:32:00 +00002295
Jim Ingham3ae449a2010-11-17 02:32:00 +00002296 if (log)
2297 log->Printf ("Process::%s (arg = %p, pid = %i) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType());
2298
Chris Lattner24943d22010-06-08 16:52:24 +00002299 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham3ae449a2010-11-17 02:32:00 +00002300 continue;
Chris Lattner24943d22010-06-08 16:52:24 +00002301 }
2302
2303
2304 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2305
2306 if (internal_state != eStateInvalid)
2307 {
2308 HandlePrivateEvent (event_sp);
2309 }
2310
Greg Clayton3b2c41c2010-10-18 04:14:23 +00002311 if (internal_state == eStateInvalid ||
2312 internal_state == eStateExited ||
2313 internal_state == eStateDetached )
Jim Ingham3ae449a2010-11-17 02:32:00 +00002314 {
Jim Ingham3ae449a2010-11-17 02:32:00 +00002315 if (log)
2316 log->Printf ("Process::%s (arg = %p, pid = %i) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state));
2317
Chris Lattner24943d22010-06-08 16:52:24 +00002318 break;
Jim Ingham3ae449a2010-11-17 02:32:00 +00002319 }
Chris Lattner24943d22010-06-08 16:52:24 +00002320 }
2321
Caroline Tice926060e2010-10-29 21:48:37 +00002322 // Verify log is still enabled before attempting to write to it...
Chris Lattner24943d22010-06-08 16:52:24 +00002323 if (log)
2324 log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID());
2325
Greg Claytona4881d02011-01-22 07:12:45 +00002326 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
2327 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00002328 return NULL;
2329}
2330
Chris Lattner24943d22010-06-08 16:52:24 +00002331//------------------------------------------------------------------
2332// Process Event Data
2333//------------------------------------------------------------------
2334
2335Process::ProcessEventData::ProcessEventData () :
2336 EventData (),
2337 m_process_sp (),
2338 m_state (eStateInvalid),
Greg Clayton54e7afa2010-07-09 20:39:50 +00002339 m_restarted (false),
Jim Ingham3ae449a2010-11-17 02:32:00 +00002340 m_update_state (false),
2341 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00002342{
2343}
2344
2345Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
2346 EventData (),
2347 m_process_sp (process_sp),
2348 m_state (state),
Greg Clayton54e7afa2010-07-09 20:39:50 +00002349 m_restarted (false),
Jim Ingham3ae449a2010-11-17 02:32:00 +00002350 m_update_state (false),
2351 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00002352{
2353}
2354
2355Process::ProcessEventData::~ProcessEventData()
2356{
2357}
2358
2359const ConstString &
2360Process::ProcessEventData::GetFlavorString ()
2361{
2362 static ConstString g_flavor ("Process::ProcessEventData");
2363 return g_flavor;
2364}
2365
2366const ConstString &
2367Process::ProcessEventData::GetFlavor () const
2368{
2369 return ProcessEventData::GetFlavorString ();
2370}
2371
Chris Lattner24943d22010-06-08 16:52:24 +00002372void
2373Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
2374{
2375 // This function gets called twice for each event, once when the event gets pulled
2376 // off of the private process event queue, and once when it gets pulled off of
2377 // the public event queue. m_update_state is used to distinguish these
2378 // two cases; it is false when we're just pulling it off for private handling,
2379 // and we don't want to do the breakpoint command handling then.
2380
2381 if (!m_update_state)
2382 return;
2383
2384 m_process_sp->SetPublicState (m_state);
2385
2386 // If we're stopped and haven't restarted, then do the breakpoint commands here:
2387 if (m_state == eStateStopped && ! m_restarted)
2388 {
2389 int num_threads = m_process_sp->GetThreadList().GetSize();
2390 int idx;
Greg Clayton643ee732010-08-04 01:40:35 +00002391
Chris Lattner24943d22010-06-08 16:52:24 +00002392 for (idx = 0; idx < num_threads; ++idx)
2393 {
2394 lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx);
2395
Jim Ingham6297a3a2010-10-20 00:39:53 +00002396 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
2397 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002398 {
Jim Ingham6297a3a2010-10-20 00:39:53 +00002399 stop_info_sp->PerformAction(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +00002400 }
2401 }
Greg Clayton643ee732010-08-04 01:40:35 +00002402
Jim Ingham6fb8baa2010-08-10 00:59:59 +00002403 // The stop action might restart the target. If it does, then we want to mark that in the
2404 // event so that whoever is receiving it will know to wait for the running event and reflect
2405 // that state appropriately.
2406
Chris Lattner24943d22010-06-08 16:52:24 +00002407 if (m_process_sp->GetPrivateState() == eStateRunning)
2408 SetRestarted(true);
2409 }
2410}
2411
2412void
2413Process::ProcessEventData::Dump (Stream *s) const
2414{
2415 if (m_process_sp)
2416 s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID());
2417
2418 s->Printf("state = %s", StateAsCString(GetState()));;
2419}
2420
2421const Process::ProcessEventData *
2422Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
2423{
2424 if (event_ptr)
2425 {
2426 const EventData *event_data = event_ptr->GetData();
2427 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
2428 return static_cast <const ProcessEventData *> (event_ptr->GetData());
2429 }
2430 return NULL;
2431}
2432
2433ProcessSP
2434Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
2435{
2436 ProcessSP process_sp;
2437 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2438 if (data)
2439 process_sp = data->GetProcessSP();
2440 return process_sp;
2441}
2442
2443StateType
2444Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
2445{
2446 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2447 if (data == NULL)
2448 return eStateInvalid;
2449 else
2450 return data->GetState();
2451}
2452
2453bool
2454Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
2455{
2456 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2457 if (data == NULL)
2458 return false;
2459 else
2460 return data->GetRestarted();
2461}
2462
2463void
2464Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
2465{
2466 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2467 if (data != NULL)
2468 data->SetRestarted(new_value);
2469}
2470
2471bool
Jim Ingham3ae449a2010-11-17 02:32:00 +00002472Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
2473{
2474 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2475 if (data == NULL)
2476 return false;
2477 else
2478 return data->GetInterrupted ();
2479}
2480
2481void
2482Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
2483{
2484 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2485 if (data != NULL)
2486 data->SetInterrupted(new_value);
2487}
2488
2489bool
Chris Lattner24943d22010-06-08 16:52:24 +00002490Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
2491{
2492 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2493 if (data)
2494 {
2495 data->SetUpdateStateOnRemoval();
2496 return true;
2497 }
2498 return false;
2499}
2500
Chris Lattner24943d22010-06-08 16:52:24 +00002501Target *
2502Process::CalculateTarget ()
2503{
2504 return &m_target;
2505}
2506
2507Process *
2508Process::CalculateProcess ()
2509{
2510 return this;
2511}
2512
2513Thread *
2514Process::CalculateThread ()
2515{
2516 return NULL;
2517}
2518
2519StackFrame *
2520Process::CalculateStackFrame ()
2521{
2522 return NULL;
2523}
2524
2525void
Greg Claytona830adb2010-10-04 01:05:56 +00002526Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00002527{
2528 exe_ctx.target = &m_target;
2529 exe_ctx.process = this;
2530 exe_ctx.thread = NULL;
2531 exe_ctx.frame = NULL;
2532}
2533
2534lldb::ProcessSP
2535Process::GetSP ()
2536{
2537 return GetTarget().GetProcessSP();
2538}
2539
Jim Ingham7508e732010-08-09 23:31:02 +00002540uint32_t
2541Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2542{
2543 return 0;
2544}
2545
2546ArchSpec
2547Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
2548{
2549 return Host::GetArchSpecForExistingProcess (pid);
2550}
2551
2552ArchSpec
2553Process::GetArchSpecForExistingProcess (const char *process_name)
2554{
2555 return Host::GetArchSpecForExistingProcess (process_name);
2556}
2557
Caroline Tice861efb32010-11-16 05:07:41 +00002558void
2559Process::AppendSTDOUT (const char * s, size_t len)
2560{
Greg Clayton20d338f2010-11-18 05:57:03 +00002561 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Tice861efb32010-11-16 05:07:41 +00002562 m_stdout_data.append (s, len);
2563
Greg Claytonb3781332010-12-05 19:16:56 +00002564 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (GetTarget().GetProcessSP(), GetState()));
Caroline Tice861efb32010-11-16 05:07:41 +00002565}
2566
2567void
2568Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
2569{
2570 Process *process = (Process *) baton;
2571 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
2572}
2573
2574size_t
2575Process::ProcessInputReaderCallback (void *baton,
2576 InputReader &reader,
2577 lldb::InputReaderAction notification,
2578 const char *bytes,
2579 size_t bytes_len)
2580{
2581 Process *process = (Process *) baton;
2582
2583 switch (notification)
2584 {
2585 case eInputReaderActivate:
2586 break;
2587
2588 case eInputReaderDeactivate:
2589 break;
2590
2591 case eInputReaderReactivate:
2592 break;
2593
2594 case eInputReaderGotToken:
2595 {
2596 Error error;
2597 process->PutSTDIN (bytes, bytes_len, error);
2598 }
2599 break;
2600
Caroline Ticec4f55fe2010-11-19 20:47:54 +00002601 case eInputReaderInterrupt:
2602 process->Halt ();
2603 break;
2604
2605 case eInputReaderEndOfFile:
2606 process->AppendSTDOUT ("^D", 2);
2607 break;
2608
Caroline Tice861efb32010-11-16 05:07:41 +00002609 case eInputReaderDone:
2610 break;
2611
2612 }
2613
2614 return bytes_len;
2615}
2616
2617void
2618Process::ResetProcessInputReader ()
2619{
2620 m_process_input_reader.reset();
2621}
2622
2623void
2624Process::SetUpProcessInputReader (int file_descriptor)
2625{
2626 // First set up the Read Thread for reading/handling process I/O
2627
2628 std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true));
2629
2630 if (conn_ap.get())
2631 {
2632 m_stdio_communication.SetConnection (conn_ap.release());
2633 if (m_stdio_communication.IsConnected())
2634 {
2635 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
2636 m_stdio_communication.StartReadThread();
2637
2638 // Now read thread is set up, set up input reader.
2639
2640 if (!m_process_input_reader.get())
2641 {
2642 m_process_input_reader.reset (new InputReader(m_target.GetDebugger()));
2643 Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback,
2644 this,
2645 eInputReaderGranularityByte,
2646 NULL,
2647 NULL,
2648 false));
2649
2650 if (err.Fail())
2651 m_process_input_reader.reset();
2652 }
2653 }
2654 }
2655}
2656
2657void
2658Process::PushProcessInputReader ()
2659{
2660 if (m_process_input_reader && !m_process_input_reader->IsActive())
2661 m_target.GetDebugger().PushInputReader (m_process_input_reader);
2662}
2663
2664void
2665Process::PopProcessInputReader ()
2666{
2667 if (m_process_input_reader && m_process_input_reader->IsActive())
2668 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2669}
2670
Greg Clayton990de7b2010-11-18 23:32:35 +00002671
2672void
2673Process::Initialize ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002674{
Greg Clayton990de7b2010-11-18 23:32:35 +00002675 UserSettingsControllerSP &usc = GetSettingsController();
2676 usc.reset (new SettingsController);
2677 UserSettingsController::InitializeSettingsController (usc,
2678 SettingsController::global_settings_table,
2679 SettingsController::instance_settings_table);
2680}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002681
Greg Clayton990de7b2010-11-18 23:32:35 +00002682void
2683Process::Terminate ()
2684{
2685 UserSettingsControllerSP &usc = GetSettingsController();
2686 UserSettingsController::FinalizeSettingsController (usc);
2687 usc.reset();
2688}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002689
Greg Clayton990de7b2010-11-18 23:32:35 +00002690UserSettingsControllerSP &
2691Process::GetSettingsController ()
2692{
2693 static UserSettingsControllerSP g_settings_controller;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002694 return g_settings_controller;
2695}
2696
Caroline Tice1ebef442010-09-27 00:30:10 +00002697void
2698Process::UpdateInstanceName ()
2699{
2700 ModuleSP module_sp = GetTarget().GetExecutableModule();
2701 if (module_sp)
2702 {
2703 StreamString sstr;
2704 sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString());
2705
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00002706 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
Caroline Tice1ebef442010-09-27 00:30:10 +00002707 sstr.GetData());
2708 }
2709}
2710
Greg Clayton427f2902010-12-14 02:59:59 +00002711ExecutionResults
Jim Ingham360f53f2010-11-30 02:22:11 +00002712Process::RunThreadPlan (ExecutionContext &exe_ctx,
2713 lldb::ThreadPlanSP &thread_plan_sp,
2714 bool stop_others,
2715 bool try_all_threads,
2716 bool discard_on_error,
2717 uint32_t single_thread_timeout_usec,
2718 Stream &errors)
2719{
2720 ExecutionResults return_value = eExecutionSetupError;
2721
Jim Ingham15dcb7c2011-01-20 02:03:18 +00002722 if (thread_plan_sp.get() == NULL)
2723 {
2724 errors.Printf("RunThreadPlan called with empty thread plan.");
2725 return lldb::eExecutionSetupError;
2726 }
2727
Jim Inghamac959662011-01-24 06:34:17 +00002728 if (m_private_state.GetValue() != eStateStopped)
2729 {
2730 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
2731 // REMOVE BEAR TRAP...
2732 // abort();
2733 }
2734
Jim Ingham360f53f2010-11-30 02:22:11 +00002735 // Save this value for restoration of the execution context after we run
2736 uint32_t tid = exe_ctx.thread->GetIndexID();
2737
2738 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
2739 // so we should arrange to reset them as well.
2740
2741 lldb::ThreadSP selected_thread_sp = exe_ctx.process->GetThreadList().GetSelectedThread();
2742 lldb::StackFrameSP selected_frame_sp;
2743
2744 uint32_t selected_tid;
2745 if (selected_thread_sp != NULL)
2746 {
2747 selected_tid = selected_thread_sp->GetIndexID();
2748 selected_frame_sp = selected_thread_sp->GetSelectedFrame();
2749 }
2750 else
2751 {
2752 selected_tid = LLDB_INVALID_THREAD_ID;
2753 }
2754
2755 exe_ctx.thread->QueueThreadPlan(thread_plan_sp, true);
2756
Jim Ingham6ae318c2011-01-23 21:14:08 +00002757 Listener listener("lldb.process.listener.run-thread-plan");
Jim Ingham360f53f2010-11-30 02:22:11 +00002758 exe_ctx.process->HijackProcessEvents(&listener);
Jim Inghamac959662011-01-24 06:34:17 +00002759
Jim Ingham6ae318c2011-01-23 21:14:08 +00002760 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham15dcb7c2011-01-20 02:03:18 +00002761 if (log)
2762 {
2763 StreamString s;
2764 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Greg Clayton68ca8232011-01-25 02:58:48 +00002765 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4x to run thread plan \"%s\".", exe_ctx.thread->GetIndexID(), exe_ctx.thread->GetID(), s.GetData());
Jim Ingham15dcb7c2011-01-20 02:03:18 +00002766 }
2767
Jim Ingham360f53f2010-11-30 02:22:11 +00002768 Error resume_error = exe_ctx.process->Resume ();
2769 if (!resume_error.Success())
2770 {
2771 errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString());
2772 exe_ctx.process->RestoreProcessEvents();
Greg Clayton427f2902010-12-14 02:59:59 +00002773 return lldb::eExecutionSetupError;
Jim Ingham360f53f2010-11-30 02:22:11 +00002774 }
2775
2776 // We need to call the function synchronously, so spin waiting for it to return.
2777 // If we get interrupted while executing, we're going to lose our context, and
2778 // won't be able to gather the result at this point.
2779 // We set the timeout AFTER the resume, since the resume takes some time and we
2780 // don't want to charge that to the timeout.
2781
2782 TimeValue* timeout_ptr = NULL;
2783 TimeValue real_timeout;
2784
2785 if (single_thread_timeout_usec != 0)
2786 {
2787 real_timeout = TimeValue::Now();
2788 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
2789 timeout_ptr = &real_timeout;
2790 }
2791
Jim Ingham360f53f2010-11-30 02:22:11 +00002792 while (1)
2793 {
2794 lldb::EventSP event_sp;
2795 lldb::StateType stop_state = lldb::eStateInvalid;
2796 // Now wait for the process to stop again:
2797 bool got_event = listener.WaitForEvent (timeout_ptr, event_sp);
2798
2799 if (!got_event)
2800 {
2801 // Right now this is the only way to tell we've timed out...
2802 // We should interrupt the process here...
2803 // Not really sure what to do if Halt fails here...
Stephen Wilsonc2b98252011-01-12 04:20:03 +00002804 if (log) {
Jim Ingham360f53f2010-11-30 02:22:11 +00002805 if (try_all_threads)
Greg Clayton68ca8232011-01-25 02:58:48 +00002806 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, trying with all threads enabled.",
Jim Ingham360f53f2010-11-30 02:22:11 +00002807 single_thread_timeout_usec);
2808 else
Greg Clayton68ca8232011-01-25 02:58:48 +00002809 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, abandoning execution.",
Jim Ingham360f53f2010-11-30 02:22:11 +00002810 single_thread_timeout_usec);
Stephen Wilsonc2b98252011-01-12 04:20:03 +00002811 }
Jim Ingham360f53f2010-11-30 02:22:11 +00002812
Jim Inghamc556b462011-01-22 01:30:53 +00002813 Error halt_error = exe_ctx.process->Halt();
2814
2815 if (halt_error.Success())
Jim Ingham360f53f2010-11-30 02:22:11 +00002816 {
2817 timeout_ptr = NULL;
2818 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00002819 log->Printf ("Process::RunThreadPlan(): Halt succeeded.");
Jim Ingham360f53f2010-11-30 02:22:11 +00002820
2821 // Between the time that we got the timeout and the time we halted, but target
2822 // might have actually completed the plan. If so, we're done. Note, I call WFE here with a short
2823 // timeout to
2824 got_event = listener.WaitForEvent(NULL, event_sp);
2825
2826 if (got_event)
2827 {
2828 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2829 if (log)
2830 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002831 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
Jim Ingham360f53f2010-11-30 02:22:11 +00002832 if (stop_state == lldb::eStateStopped && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
2833 log->Printf (" Event was the Halt interruption event.");
2834 }
2835
2836 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
2837 {
2838 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00002839 log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. Exiting wait loop.");
Greg Clayton427f2902010-12-14 02:59:59 +00002840 return_value = lldb::eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +00002841 break;
2842 }
2843
2844 if (try_all_threads
2845 && (stop_state == lldb::eStateStopped && Process::ProcessEventData::GetInterruptedFromEvent (event_sp.get())))
2846 {
2847
2848 thread_plan_sp->SetStopOthers (false);
2849 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00002850 log->Printf ("Process::RunThreadPlan(): About to resume.");
Jim Ingham360f53f2010-11-30 02:22:11 +00002851
2852 exe_ctx.process->Resume();
2853 continue;
2854 }
2855 else
2856 {
2857 exe_ctx.process->RestoreProcessEvents ();
Greg Clayton427f2902010-12-14 02:59:59 +00002858 return lldb::eExecutionInterrupted;
Jim Ingham360f53f2010-11-30 02:22:11 +00002859 }
2860 }
2861 }
Jim Inghamc556b462011-01-22 01:30:53 +00002862 else
2863 {
2864
2865 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00002866 log->Printf ("Process::RunThreadPlan(): halt failed: error = \"%s\", I'm just going to wait a little longer and see if the world gets nicer to me.",
Jim Inghamc556b462011-01-22 01:30:53 +00002867 halt_error.AsCString());
Jim Inghamac959662011-01-24 06:34:17 +00002868// abort();
Jim Inghamc556b462011-01-22 01:30:53 +00002869
Jim Ingham6ae318c2011-01-23 21:14:08 +00002870 if (single_thread_timeout_usec != 0)
2871 {
2872 real_timeout = TimeValue::Now();
2873 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
2874 timeout_ptr = &real_timeout;
2875 }
2876 continue;
Jim Inghamc556b462011-01-22 01:30:53 +00002877 }
2878
Jim Ingham360f53f2010-11-30 02:22:11 +00002879 }
2880
2881 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2882 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00002883 log->Printf("Process::RunThreadPlan(): got event: %s.", StateAsCString(stop_state));
Jim Ingham360f53f2010-11-30 02:22:11 +00002884
2885 if (stop_state == lldb::eStateRunning || stop_state == lldb::eStateStepping)
2886 continue;
2887
2888 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
2889 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002890 if (log)
2891 log->Printf("Process::RunThreadPlan(): thread plan is done");
Greg Clayton427f2902010-12-14 02:59:59 +00002892 return_value = lldb::eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +00002893 break;
2894 }
2895 else if (exe_ctx.thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
2896 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002897 if (log)
2898 log->Printf("Process::RunThreadPlan(): thread plan was discarded");
Greg Clayton427f2902010-12-14 02:59:59 +00002899 return_value = lldb::eExecutionDiscarded;
Jim Ingham360f53f2010-11-30 02:22:11 +00002900 break;
2901 }
2902 else
2903 {
2904 if (log)
2905 {
2906 StreamString s;
Jim Inghamc556b462011-01-22 01:30:53 +00002907 if (event_sp)
2908 event_sp->Dump (&s);
2909 else
2910 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002911 log->Printf ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
Jim Inghamc556b462011-01-22 01:30:53 +00002912 }
2913
Jim Ingham360f53f2010-11-30 02:22:11 +00002914 StreamString ts;
2915
2916 const char *event_explanation;
2917
2918 do
2919 {
2920 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
2921
2922 if (!event_data)
2923 {
2924 event_explanation = "<no event data>";
2925 break;
2926 }
2927
2928 Process *process = event_data->GetProcessSP().get();
2929
2930 if (!process)
2931 {
2932 event_explanation = "<no process>";
2933 break;
2934 }
2935
2936 ThreadList &thread_list = process->GetThreadList();
2937
2938 uint32_t num_threads = thread_list.GetSize();
2939 uint32_t thread_index;
2940
2941 ts.Printf("<%u threads> ", num_threads);
2942
2943 for (thread_index = 0;
2944 thread_index < num_threads;
2945 ++thread_index)
2946 {
2947 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
2948
2949 if (!thread)
2950 {
2951 ts.Printf("<?> ");
2952 continue;
2953 }
2954
Jim Inghamc556b462011-01-22 01:30:53 +00002955 ts.Printf("<0x%4.4x ", thread->GetID());
Greg Clayton08d7d3a2011-01-06 22:15:06 +00002956 RegisterContext *register_context = thread->GetRegisterContext().get();
Jim Ingham360f53f2010-11-30 02:22:11 +00002957
2958 if (register_context)
2959 ts.Printf("[ip 0x%llx] ", register_context->GetPC());
2960 else
2961 ts.Printf("[ip unknown] ");
2962
2963 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
2964 if (stop_info_sp)
2965 {
2966 const char *stop_desc = stop_info_sp->GetDescription();
2967 if (stop_desc)
2968 ts.PutCString (stop_desc);
2969 }
2970 ts.Printf(">");
2971 }
2972
2973 event_explanation = ts.GetData();
2974 } while (0);
2975
Jim Inghamc556b462011-01-22 01:30:53 +00002976 // See if any of the threads that stopped think we ought to stop. Otherwise continue on.
2977 if (!GetThreadList().ShouldStop(event_sp.get()))
2978 {
2979 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00002980 log->Printf("Process::RunThreadPlan(): execution interrupted, but nobody wanted to stop, so we continued: %s %s",
Jim Inghamc556b462011-01-22 01:30:53 +00002981 s.GetData(), event_explanation);
2982 if (single_thread_timeout_usec != 0)
2983 {
2984 real_timeout = TimeValue::Now();
2985 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
2986 timeout_ptr = &real_timeout;
2987 }
2988
2989 continue;
2990 }
2991 else
2992 {
2993 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00002994 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
Jim Inghamc556b462011-01-22 01:30:53 +00002995 }
Jim Ingham360f53f2010-11-30 02:22:11 +00002996 }
2997
2998 if (discard_on_error && thread_plan_sp)
2999 {
3000 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3001 }
Greg Clayton427f2902010-12-14 02:59:59 +00003002 return_value = lldb::eExecutionInterrupted;
Jim Ingham360f53f2010-11-30 02:22:11 +00003003 break;
3004 }
3005 }
3006
3007 if (exe_ctx.process)
3008 exe_ctx.process->RestoreProcessEvents ();
3009
3010 // Thread we ran the function in may have gone away because we ran the target
3011 // Check that it's still there.
3012 exe_ctx.thread = exe_ctx.process->GetThreadList().FindThreadByIndexID(tid, true).get();
3013 if (exe_ctx.thread)
3014 exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex(0).get();
3015
3016 // Also restore the current process'es selected frame & thread, since this function calling may
3017 // be done behind the user's back.
3018
3019 if (selected_tid != LLDB_INVALID_THREAD_ID)
3020 {
3021 if (exe_ctx.process->GetThreadList().SetSelectedThreadByIndexID (selected_tid))
3022 {
3023 // We were able to restore the selected thread, now restore the frame:
3024 exe_ctx.process->GetThreadList().GetSelectedThread()->SetSelectedFrame(selected_frame_sp.get());
3025 }
3026 }
3027
3028 return return_value;
3029}
3030
3031const char *
3032Process::ExecutionResultAsCString (ExecutionResults result)
3033{
3034 const char *result_name;
3035
3036 switch (result)
3037 {
Greg Clayton427f2902010-12-14 02:59:59 +00003038 case lldb::eExecutionCompleted:
Jim Ingham360f53f2010-11-30 02:22:11 +00003039 result_name = "eExecutionCompleted";
3040 break;
Greg Clayton427f2902010-12-14 02:59:59 +00003041 case lldb::eExecutionDiscarded:
Jim Ingham360f53f2010-11-30 02:22:11 +00003042 result_name = "eExecutionDiscarded";
3043 break;
Greg Clayton427f2902010-12-14 02:59:59 +00003044 case lldb::eExecutionInterrupted:
Jim Ingham360f53f2010-11-30 02:22:11 +00003045 result_name = "eExecutionInterrupted";
3046 break;
Greg Clayton427f2902010-12-14 02:59:59 +00003047 case lldb::eExecutionSetupError:
Jim Ingham360f53f2010-11-30 02:22:11 +00003048 result_name = "eExecutionSetupError";
3049 break;
Greg Clayton427f2902010-12-14 02:59:59 +00003050 case lldb::eExecutionTimedOut:
Jim Ingham360f53f2010-11-30 02:22:11 +00003051 result_name = "eExecutionTimedOut";
3052 break;
3053 }
3054 return result_name;
3055}
3056
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003057//--------------------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00003058// class Process::SettingsController
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003059//--------------------------------------------------------------
3060
Greg Claytond0a5a232010-09-19 02:33:57 +00003061Process::SettingsController::SettingsController () :
Caroline Tice5bc8c972010-09-20 20:44:43 +00003062 UserSettingsController ("process", Target::GetSettingsController())
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003063{
Greg Clayton638351a2010-12-04 00:10:17 +00003064 m_default_settings.reset (new ProcessInstanceSettings (*this,
3065 false,
Caroline Tice004afcb2010-09-08 17:48:55 +00003066 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003067}
3068
Greg Claytond0a5a232010-09-19 02:33:57 +00003069Process::SettingsController::~SettingsController ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003070{
3071}
3072
3073lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +00003074Process::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003075{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003076 ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*GetSettingsController(),
3077 false,
3078 instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003079 lldb::InstanceSettingsSP new_settings_sp (new_settings);
3080 return new_settings_sp;
3081}
3082
3083//--------------------------------------------------------------
3084// class ProcessInstanceSettings
3085//--------------------------------------------------------------
3086
Greg Clayton638351a2010-12-04 00:10:17 +00003087ProcessInstanceSettings::ProcessInstanceSettings
3088(
3089 UserSettingsController &owner,
3090 bool live_instance,
3091 const char *name
3092) :
3093 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003094 m_run_args (),
3095 m_env_vars (),
3096 m_input_path (),
3097 m_output_path (),
3098 m_error_path (),
3099 m_plugin (),
Caroline Ticebd666012010-12-03 18:46:09 +00003100 m_disable_aslr (true),
Greg Clayton638351a2010-12-04 00:10:17 +00003101 m_disable_stdio (false),
3102 m_inherit_host_env (true),
3103 m_got_host_env (false)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003104{
Caroline Tice396704b2010-09-09 18:26:37 +00003105 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
3106 // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers.
3107 // 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 +00003108 // This is true for CreateInstanceName() too.
3109
3110 if (GetInstanceName () == InstanceSettings::InvalidName())
3111 {
3112 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
3113 m_owner.RegisterInstanceSettings (this);
3114 }
Caroline Tice396704b2010-09-09 18:26:37 +00003115
3116 if (live_instance)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003117 {
3118 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3119 CopyInstanceSettings (pending_settings,false);
Caroline Tice396704b2010-09-09 18:26:37 +00003120 //m_owner.RemovePendingSettings (m_instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003121 }
3122}
3123
3124ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003125 InstanceSettings (*Process::GetSettingsController(), CreateInstanceName().AsCString()),
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003126 m_run_args (rhs.m_run_args),
3127 m_env_vars (rhs.m_env_vars),
3128 m_input_path (rhs.m_input_path),
3129 m_output_path (rhs.m_output_path),
3130 m_error_path (rhs.m_error_path),
3131 m_plugin (rhs.m_plugin),
Caroline Ticebd666012010-12-03 18:46:09 +00003132 m_disable_aslr (rhs.m_disable_aslr),
3133 m_disable_stdio (rhs.m_disable_stdio)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003134{
3135 if (m_instance_name != InstanceSettings::GetDefaultName())
3136 {
3137 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3138 CopyInstanceSettings (pending_settings,false);
3139 m_owner.RemovePendingSettings (m_instance_name);
3140 }
3141}
3142
3143ProcessInstanceSettings::~ProcessInstanceSettings ()
3144{
3145}
3146
3147ProcessInstanceSettings&
3148ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs)
3149{
3150 if (this != &rhs)
3151 {
3152 m_run_args = rhs.m_run_args;
3153 m_env_vars = rhs.m_env_vars;
3154 m_input_path = rhs.m_input_path;
3155 m_output_path = rhs.m_output_path;
3156 m_error_path = rhs.m_error_path;
3157 m_plugin = rhs.m_plugin;
3158 m_disable_aslr = rhs.m_disable_aslr;
Caroline Ticebd666012010-12-03 18:46:09 +00003159 m_disable_stdio = rhs.m_disable_stdio;
Greg Clayton638351a2010-12-04 00:10:17 +00003160 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003161 }
3162
3163 return *this;
3164}
3165
3166
3167void
3168ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
3169 const char *index_value,
3170 const char *value,
3171 const ConstString &instance_name,
3172 const SettingEntry &entry,
3173 lldb::VarSetOperationType op,
3174 Error &err,
3175 bool pending)
3176{
3177 if (var_name == RunArgsVarName())
3178 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
3179 else if (var_name == EnvVarsVarName())
Greg Clayton638351a2010-12-04 00:10:17 +00003180 {
3181 GetHostEnvironmentIfNeeded ();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003182 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
Greg Clayton638351a2010-12-04 00:10:17 +00003183 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003184 else if (var_name == InputPathVarName())
3185 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
3186 else if (var_name == OutputPathVarName())
3187 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
3188 else if (var_name == ErrorPathVarName())
3189 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
3190 else if (var_name == PluginVarName())
3191 UserSettingsController::UpdateEnumVariable (entry.enum_values, (int *) &m_plugin, value, err);
Greg Clayton638351a2010-12-04 00:10:17 +00003192 else if (var_name == InheritHostEnvVarName())
3193 UserSettingsController::UpdateBooleanVariable (op, m_inherit_host_env, value, err);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003194 else if (var_name == DisableASLRVarName())
3195 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err);
Caroline Ticebd666012010-12-03 18:46:09 +00003196 else if (var_name == DisableSTDIOVarName ())
3197 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, err);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003198}
3199
3200void
3201ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
3202 bool pending)
3203{
3204 if (new_settings.get() == NULL)
3205 return;
3206
3207 ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get();
3208
3209 m_run_args = new_process_settings->m_run_args;
3210 m_env_vars = new_process_settings->m_env_vars;
3211 m_input_path = new_process_settings->m_input_path;
3212 m_output_path = new_process_settings->m_output_path;
3213 m_error_path = new_process_settings->m_error_path;
3214 m_plugin = new_process_settings->m_plugin;
3215 m_disable_aslr = new_process_settings->m_disable_aslr;
Caroline Ticebd666012010-12-03 18:46:09 +00003216 m_disable_stdio = new_process_settings->m_disable_stdio;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003217}
3218
Caroline Ticebcb5b452010-09-20 21:37:42 +00003219bool
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003220ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
3221 const ConstString &var_name,
Caroline Tice5bc8c972010-09-20 20:44:43 +00003222 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00003223 Error *err)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003224{
3225 if (var_name == RunArgsVarName())
3226 {
3227 if (m_run_args.GetArgumentCount() > 0)
Greg Claytonc14069e2010-09-14 03:47:41 +00003228 {
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003229 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
3230 value.AppendString (m_run_args.GetArgumentAtIndex (i));
Greg Claytonc14069e2010-09-14 03:47:41 +00003231 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003232 }
3233 else if (var_name == EnvVarsVarName())
3234 {
Greg Clayton638351a2010-12-04 00:10:17 +00003235 GetHostEnvironmentIfNeeded ();
3236
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003237 if (m_env_vars.size() > 0)
3238 {
3239 std::map<std::string, std::string>::iterator pos;
3240 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
3241 {
3242 StreamString value_str;
3243 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
3244 value.AppendString (value_str.GetData());
3245 }
3246 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003247 }
3248 else if (var_name == InputPathVarName())
3249 {
3250 value.AppendString (m_input_path.c_str());
3251 }
3252 else if (var_name == OutputPathVarName())
3253 {
3254 value.AppendString (m_output_path.c_str());
3255 }
3256 else if (var_name == ErrorPathVarName())
3257 {
3258 value.AppendString (m_error_path.c_str());
3259 }
3260 else if (var_name == PluginVarName())
3261 {
3262 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, (int) m_plugin));
3263 }
Greg Claytona99b0bf2010-12-04 00:12:24 +00003264 else if (var_name == InheritHostEnvVarName())
3265 {
3266 if (m_inherit_host_env)
3267 value.AppendString ("true");
3268 else
3269 value.AppendString ("false");
3270 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003271 else if (var_name == DisableASLRVarName())
3272 {
3273 if (m_disable_aslr)
3274 value.AppendString ("true");
3275 else
3276 value.AppendString ("false");
3277 }
Caroline Ticebd666012010-12-03 18:46:09 +00003278 else if (var_name == DisableSTDIOVarName())
3279 {
3280 if (m_disable_stdio)
3281 value.AppendString ("true");
3282 else
3283 value.AppendString ("false");
3284 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003285 else
Caroline Ticebcb5b452010-09-20 21:37:42 +00003286 {
3287 if (err)
3288 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
3289 return false;
3290 }
3291 return true;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003292}
3293
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003294const ConstString
3295ProcessInstanceSettings::CreateInstanceName ()
3296{
3297 static int instance_count = 1;
3298 StreamString sstr;
3299
3300 sstr.Printf ("process_%d", instance_count);
3301 ++instance_count;
3302
3303 const ConstString ret_val (sstr.GetData());
3304 return ret_val;
3305}
3306
3307const ConstString &
3308ProcessInstanceSettings::RunArgsVarName ()
3309{
3310 static ConstString run_args_var_name ("run-args");
3311
3312 return run_args_var_name;
3313}
3314
3315const ConstString &
3316ProcessInstanceSettings::EnvVarsVarName ()
3317{
3318 static ConstString env_vars_var_name ("env-vars");
3319
3320 return env_vars_var_name;
3321}
3322
3323const ConstString &
Greg Clayton638351a2010-12-04 00:10:17 +00003324ProcessInstanceSettings::InheritHostEnvVarName ()
3325{
3326 static ConstString g_name ("inherit-env");
3327
3328 return g_name;
3329}
3330
3331const ConstString &
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003332ProcessInstanceSettings::InputPathVarName ()
3333{
3334 static ConstString input_path_var_name ("input-path");
3335
3336 return input_path_var_name;
3337}
3338
3339const ConstString &
3340ProcessInstanceSettings::OutputPathVarName ()
3341{
Caroline Tice87097232010-09-07 18:35:40 +00003342 static ConstString output_path_var_name ("output-path");
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003343
3344 return output_path_var_name;
3345}
3346
3347const ConstString &
3348ProcessInstanceSettings::ErrorPathVarName ()
3349{
Caroline Tice87097232010-09-07 18:35:40 +00003350 static ConstString error_path_var_name ("error-path");
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003351
3352 return error_path_var_name;
3353}
3354
3355const ConstString &
3356ProcessInstanceSettings::PluginVarName ()
3357{
3358 static ConstString plugin_var_name ("plugin");
3359
3360 return plugin_var_name;
3361}
3362
3363
3364const ConstString &
3365ProcessInstanceSettings::DisableASLRVarName ()
3366{
3367 static ConstString disable_aslr_var_name ("disable-aslr");
3368
3369 return disable_aslr_var_name;
3370}
3371
Caroline Ticebd666012010-12-03 18:46:09 +00003372const ConstString &
3373ProcessInstanceSettings::DisableSTDIOVarName ()
3374{
3375 static ConstString disable_stdio_var_name ("disable-stdio");
3376
3377 return disable_stdio_var_name;
3378}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003379
3380//--------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00003381// SettingsController Variable Tables
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003382//--------------------------------------------------
3383
3384SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00003385Process::SettingsController::global_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003386{
3387 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
3388 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
3389};
3390
3391
3392lldb::OptionEnumValueElement
Greg Claytond0a5a232010-09-19 02:33:57 +00003393Process::SettingsController::g_plugins[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003394{
Caroline Ticef2c330d2010-09-09 18:01:59 +00003395 { eMacosx, "process.macosx", "Use the native MacOSX debugger plugin" },
3396 { eRemoteDebugger, "process.gdb-remote" , "Use the GDB Remote protocol based debugger plugin" },
3397 { 0, NULL, NULL }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003398};
3399
3400SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00003401Process::SettingsController::instance_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003402{
Greg Clayton638351a2010-12-04 00:10:17 +00003403 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
3404 { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
3405 { "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." },
3406 { "inherit-env", eSetVarTypeBoolean, "true", NULL, false, false, "Inherit the environment from the process that is running LLDB." },
Greg Claytonde915be2011-01-23 05:56:20 +00003407 { "input-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for reading its input." },
3408 { "output-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writing its output." },
3409 { "error-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writings its error messages." },
3410 { "plugin", eSetVarTypeEnum, NULL, g_plugins, false, false, "The plugin to be used to run the process." },
Greg Clayton638351a2010-12-04 00:10:17 +00003411 { "disable-aslr", eSetVarTypeBoolean, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
3412 { "disable-stdio", eSetVarTypeBoolean, "false", NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" },
3413 { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003414};
3415
3416
Jim Ingham7508e732010-08-09 23:31:02 +00003417