blob: 9b0194951c17a00099f26885e0a0b443c3cdb330 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Process.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Process.h"
11
12#include "lldb/lldb-private-log.h"
13
14#include "lldb/Breakpoint/StoppointCallbackContext.h"
15#include "lldb/Breakpoint/BreakpointLocation.h"
16#include "lldb/Core/Event.h"
Caroline Ticeef5c6d02010-11-16 05:07:41 +000017#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Debugger.h"
Caroline Ticeef5c6d02010-11-16 05:07:41 +000019#include "lldb/Core/InputReader.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Log.h"
21#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/State.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000023#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Host/Host.h"
25#include "lldb/Target/ABI.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000026#include "lldb/Target/DynamicLoader.h"
Jim Ingham22777012010-09-23 02:01:19 +000027#include "lldb/Target/LanguageRuntime.h"
28#include "lldb/Target/CPPLanguageRuntime.h"
29#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000031#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-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 Clayton58be07b2011-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 Lattner30fdc8d2010-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 Claytonc982c762010-07-09 20:39:50 +0000195 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196 {
Greg Claytonc982c762010-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 Lattner30fdc8d2010-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 Claytoncfd1ace2010-10-31 03:01:06 +0000211 Broadcaster ("lldb.process"),
Greg Claytondbe54502010-11-19 03:46:01 +0000212 ProcessInstanceSettings (*GetSettingsController()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213 m_target (target),
Chris Lattner30fdc8d2010-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 Clayton3af9ea52010-11-18 05:57:03 +0000227 m_image_tokens (),
228 m_listener (listener),
229 m_breakpoint_site_list (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000230 m_dynamic_checkers_ap (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +0000231 m_unix_signals (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000232 m_target_triple (),
233 m_byte_order (eByteOrderHost),
234 m_addr_byte_size (0),
235 m_abi_sp (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +0000236 m_process_input_reader (),
Greg Clayton3e06bd92011-01-09 21:07:35 +0000237 m_stdio_communication ("process.stdio"),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000238 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton58be07b2011-01-07 06:08:19 +0000239 m_stdout_data (),
Jim Inghambb3a2832011-01-29 01:49:25 +0000240 m_memory_cache (),
241 m_next_event_action(NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242{
Caroline Tice1559a462010-09-27 00:30:10 +0000243 UpdateInstanceName();
244
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000245 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 if (log)
247 log->Printf ("%p Process::Process()", this);
248
Greg Claytoncfd1ace2010-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 Lattner30fdc8d2010-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 Clayton2d4edfb2010-11-06 01:53:30 +0000274 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 if (log)
276 log->Printf ("%p Process::~Process()", this);
Jim Inghambb3a2832011-01-29 01:49:25 +0000277 if (m_next_event_action)
278 SetNextEventAction(NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 StopPrivateStateThread();
280}
281
282void
283Process::Finalize()
284{
285 // Do any cleanup needed prior to being destructed... Subclasses
286 // that override this method should call this superclass method as well.
287}
288
289void
290Process::RegisterNotificationCallbacks (const Notifications& callbacks)
291{
292 m_notifications.push_back(callbacks);
293 if (callbacks.initialize != NULL)
294 callbacks.initialize (callbacks.baton, this);
295}
296
297bool
298Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
299{
300 std::vector<Notifications>::iterator pos, end = m_notifications.end();
301 for (pos = m_notifications.begin(); pos != end; ++pos)
302 {
303 if (pos->baton == callbacks.baton &&
304 pos->initialize == callbacks.initialize &&
305 pos->process_state_changed == callbacks.process_state_changed)
306 {
307 m_notifications.erase(pos);
308 return true;
309 }
310 }
311 return false;
312}
313
314void
315Process::SynchronouslyNotifyStateChanged (StateType state)
316{
317 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
318 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
319 {
320 if (notification_pos->process_state_changed)
321 notification_pos->process_state_changed (notification_pos->baton, this, state);
322 }
323}
324
325// FIXME: We need to do some work on events before the general Listener sees them.
326// For instance if we are continuing from a breakpoint, we need to ensure that we do
327// the little "insert real insn, step & stop" trick. But we can't do that when the
328// event is delivered by the broadcaster - since that is done on the thread that is
329// waiting for new events, so if we needed more than one event for our handling, we would
330// stall. So instead we do it when we fetch the event off of the queue.
331//
332
333StateType
334Process::GetNextEvent (EventSP &event_sp)
335{
336 StateType state = eStateInvalid;
337
338 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
339 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
340
341 return state;
342}
343
344
345StateType
346Process::WaitForProcessToStop (const TimeValue *timeout)
347{
348 StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded };
349 return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType));
350}
351
352
353StateType
354Process::WaitForState
355(
356 const TimeValue *timeout,
357 const StateType *match_states, const uint32_t num_match_states
358)
359{
360 EventSP event_sp;
361 uint32_t i;
Greg Clayton05faeb72010-10-07 04:19:01 +0000362 StateType state = GetState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 while (state != eStateInvalid)
364 {
Greg Clayton05faeb72010-10-07 04:19:01 +0000365 // If we are exited or detached, we won't ever get back to any
366 // other valid state...
367 if (state == eStateDetached || state == eStateExited)
368 return state;
369
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 state = WaitForStateChangedEvents (timeout, event_sp);
371
372 for (i=0; i<num_match_states; ++i)
373 {
374 if (match_states[i] == state)
375 return state;
376 }
377 }
378 return state;
379}
380
Jim Ingham30f9b212010-10-11 23:53:14 +0000381bool
382Process::HijackProcessEvents (Listener *listener)
383{
384 if (listener != NULL)
385 {
386 return HijackBroadcaster(listener, eBroadcastBitStateChanged);
387 }
388 else
389 return false;
390}
391
392void
393Process::RestoreProcessEvents ()
394{
395 RestoreBroadcaster();
396}
397
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398StateType
399Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
400{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000401 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402
403 if (log)
404 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
405
406 StateType state = eStateInvalid;
Greg Clayton3fcbed62010-10-19 03:25:40 +0000407 if (m_listener.WaitForEventForBroadcasterWithType (timeout,
408 this,
409 eBroadcastBitStateChanged,
410 event_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
412
413 if (log)
414 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
415 __FUNCTION__,
416 timeout,
417 StateAsCString(state));
418 return state;
419}
420
421Event *
422Process::PeekAtStateChangedEvents ()
423{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000424 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425
426 if (log)
427 log->Printf ("Process::%s...", __FUNCTION__);
428
429 Event *event_ptr;
Greg Clayton3fcbed62010-10-19 03:25:40 +0000430 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
431 eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 if (log)
433 {
434 if (event_ptr)
435 {
436 log->Printf ("Process::%s (event_ptr) => %s",
437 __FUNCTION__,
438 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
439 }
440 else
441 {
442 log->Printf ("Process::%s no events found",
443 __FUNCTION__);
444 }
445 }
446 return event_ptr;
447}
448
449StateType
450Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
451{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000452 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453
454 if (log)
455 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
456
457 StateType state = eStateInvalid;
Greg Clayton6779606a2011-01-22 23:43:18 +0000458 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
459 &m_private_state_broadcaster,
460 eBroadcastBitStateChanged,
461 event_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
463
464 // This is a bit of a hack, but when we wait here we could very well return
465 // to the command-line, and that could disable the log, which would render the
466 // log we got above invalid.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 if (log)
Greg Clayton6779606a2011-01-22 23:43:18 +0000468 {
469 if (state == eStateInvalid)
470 log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout);
471 else
472 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
473 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 return state;
475}
476
477bool
478Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
479{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000480 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481
482 if (log)
483 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
484
485 if (control_only)
486 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
487 else
488 return m_private_state_listener.WaitForEvent(timeout, event_sp);
489}
490
491bool
492Process::IsRunning () const
493{
494 return StateIsRunningState (m_public_state.GetValue());
495}
496
497int
498Process::GetExitStatus ()
499{
500 if (m_public_state.GetValue() == eStateExited)
501 return m_exit_status;
502 return -1;
503}
504
Greg Clayton85851dd2010-12-04 00:10:17 +0000505
506void
507Process::ProcessInstanceSettings::GetHostEnvironmentIfNeeded ()
508{
509 if (m_inherit_host_env && !m_got_host_env)
510 {
511 m_got_host_env = true;
512 StringList host_env;
513 const size_t host_env_count = Host::GetEnvironment (host_env);
514 for (size_t idx=0; idx<host_env_count; idx++)
515 {
516 const char *env_entry = host_env.GetStringAtIndex (idx);
517 if (env_entry)
518 {
Greg Claytone2956ee2010-12-15 20:52:40 +0000519 const char *equal_pos = ::strchr(env_entry, '=');
Greg Clayton85851dd2010-12-04 00:10:17 +0000520 if (equal_pos)
521 {
522 std::string key (env_entry, equal_pos - env_entry);
523 std::string value (equal_pos + 1);
524 if (m_env_vars.find (key) == m_env_vars.end())
525 m_env_vars[key] = value;
526 }
527 }
528 }
529 }
530}
531
532
533size_t
534Process::ProcessInstanceSettings::GetEnvironmentAsArgs (Args &env)
535{
536 GetHostEnvironmentIfNeeded ();
537
538 dictionary::const_iterator pos, end = m_env_vars.end();
539 for (pos = m_env_vars.begin(); pos != end; ++pos)
540 {
541 std::string env_var_equal_value (pos->first);
542 env_var_equal_value.append(1, '=');
543 env_var_equal_value.append (pos->second);
544 env.AppendArgument (env_var_equal_value.c_str());
545 }
546 return env.GetArgumentCount();
547}
548
549
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550const char *
551Process::GetExitDescription ()
552{
553 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
554 return m_exit_string.c_str();
555 return NULL;
556}
557
Greg Clayton6779606a2011-01-22 23:43:18 +0000558bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559Process::SetExitStatus (int status, const char *cstr)
560{
Greg Clayton414f5d32011-01-25 02:58:48 +0000561 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
562 if (log)
563 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
564 status, status,
565 cstr ? "\"" : "",
566 cstr ? cstr : "NULL",
567 cstr ? "\"" : "");
568
Greg Clayton6779606a2011-01-22 23:43:18 +0000569 // We were already in the exited state
570 if (m_private_state.GetValue() == eStateExited)
Greg Clayton414f5d32011-01-25 02:58:48 +0000571 {
Greg Clayton385d6032011-01-26 23:47:29 +0000572 if (log)
573 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton6779606a2011-01-22 23:43:18 +0000574 return false;
Greg Clayton414f5d32011-01-25 02:58:48 +0000575 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000576
577 m_exit_status = status;
578 if (cstr)
579 m_exit_string = cstr;
580 else
581 m_exit_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582
Greg Clayton6779606a2011-01-22 23:43:18 +0000583 DidExit ();
Greg Clayton10177aa2010-12-08 05:08:21 +0000584
Greg Clayton6779606a2011-01-22 23:43:18 +0000585 SetPrivateState (eStateExited);
586 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
589// This static callback can be used to watch for local child processes on
590// the current host. The the child process exits, the process will be
591// found in the global target list (we want to be completely sure that the
592// lldb_private::Process doesn't go away before we can deliver the signal.
593bool
594Process::SetProcessExitStatus
595(
596 void *callback_baton,
597 lldb::pid_t pid,
598 int signo, // Zero for no signal
599 int exit_status // Exit value of process if signal is zero
600)
601{
602 if (signo == 0 || exit_status)
603 {
Greg Clayton66111032010-06-23 01:19:29 +0000604 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 if (target_sp)
606 {
607 ProcessSP process_sp (target_sp->GetProcessSP());
608 if (process_sp)
609 {
610 const char *signal_cstr = NULL;
611 if (signo)
612 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
613
614 process_sp->SetExitStatus (exit_status, signal_cstr);
615 }
616 }
617 return true;
618 }
619 return false;
620}
621
622
623uint32_t
624Process::GetNextThreadIndexID ()
625{
626 return ++m_thread_index_id;
627}
628
629StateType
630Process::GetState()
631{
632 // If any other threads access this we will need a mutex for it
633 return m_public_state.GetValue ();
634}
635
636void
637Process::SetPublicState (StateType new_state)
638{
Greg Clayton414f5d32011-01-25 02:58:48 +0000639 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 if (log)
641 log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state));
642 m_public_state.SetValue (new_state);
643}
644
645StateType
646Process::GetPrivateState ()
647{
648 return m_private_state.GetValue();
649}
650
651void
652Process::SetPrivateState (StateType new_state)
653{
Greg Clayton414f5d32011-01-25 02:58:48 +0000654 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655 bool state_changed = false;
656
657 if (log)
658 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
659
660 Mutex::Locker locker(m_private_state.GetMutex());
661
662 const StateType old_state = m_private_state.GetValueNoLock ();
663 state_changed = old_state != new_state;
664 if (state_changed)
665 {
666 m_private_state.SetValueNoLock (new_state);
667 if (StateIsStoppedState(new_state))
668 {
669 m_stop_id++;
Greg Clayton58be07b2011-01-07 06:08:19 +0000670 m_memory_cache.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671 if (log)
672 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id);
673 }
674 // Use our target to get a shared pointer to ourselves...
675 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state));
676 }
677 else
678 {
679 if (log)
680 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state));
681 }
682}
683
684
685uint32_t
686Process::GetStopID() const
687{
688 return m_stop_id;
689}
690
691addr_t
692Process::GetImageInfoAddress()
693{
694 return LLDB_INVALID_ADDRESS;
695}
696
Greg Clayton8f343b02010-11-04 01:54:29 +0000697//----------------------------------------------------------------------
698// LoadImage
699//
700// This function provides a default implementation that works for most
701// unix variants. Any Process subclasses that need to do shared library
702// loading differently should override LoadImage and UnloadImage and
703// do what is needed.
704//----------------------------------------------------------------------
705uint32_t
706Process::LoadImage (const FileSpec &image_spec, Error &error)
707{
708 DynamicLoader *loader = GetDynamicLoader();
709 if (loader)
710 {
711 error = loader->CanLoadImage();
712 if (error.Fail())
713 return LLDB_INVALID_IMAGE_TOKEN;
714 }
715
716 if (error.Success())
717 {
718 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
719 if (thread_sp == NULL)
720 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
721
722 if (thread_sp)
723 {
724 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
725
726 if (frame_sp)
727 {
728 ExecutionContext exe_ctx;
729 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Ingham399f1ca2010-11-05 19:25:48 +0000730 bool unwind_on_error = true;
Sean Callanan92adcac2011-01-13 08:53:35 +0000731 bool keep_in_memory = false;
Greg Clayton8f343b02010-11-04 01:54:29 +0000732 StreamString expr;
733 char path[PATH_MAX];
734 image_spec.GetPath(path, sizeof(path));
735 expr.Printf("dlopen (\"%s\", 2)", path);
736 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +0000737 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan92adcac2011-01-13 08:53:35 +0000738 ClangUserExpression::Evaluate (exe_ctx, keep_in_memory, unwind_on_error, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton8f343b02010-11-04 01:54:29 +0000739 if (result_valobj_sp->GetError().Success())
740 {
741 Scalar scalar;
742 if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar))
743 {
744 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
745 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
746 {
747 uint32_t image_token = m_image_tokens.size();
748 m_image_tokens.push_back (image_ptr);
749 return image_token;
750 }
751 }
752 }
753 }
754 }
755 }
756 return LLDB_INVALID_IMAGE_TOKEN;
757}
758
759//----------------------------------------------------------------------
760// UnloadImage
761//
762// This function provides a default implementation that works for most
763// unix variants. Any Process subclasses that need to do shared library
764// loading differently should override LoadImage and UnloadImage and
765// do what is needed.
766//----------------------------------------------------------------------
767Error
768Process::UnloadImage (uint32_t image_token)
769{
770 Error error;
771 if (image_token < m_image_tokens.size())
772 {
773 const addr_t image_addr = m_image_tokens[image_token];
774 if (image_addr == LLDB_INVALID_ADDRESS)
775 {
776 error.SetErrorString("image already unloaded");
777 }
778 else
779 {
780 DynamicLoader *loader = GetDynamicLoader();
781 if (loader)
782 error = loader->CanLoadImage();
783
784 if (error.Success())
785 {
786 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
787 if (thread_sp == NULL)
788 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
789
790 if (thread_sp)
791 {
792 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
793
794 if (frame_sp)
795 {
796 ExecutionContext exe_ctx;
797 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Ingham399f1ca2010-11-05 19:25:48 +0000798 bool unwind_on_error = true;
Sean Callanan92adcac2011-01-13 08:53:35 +0000799 bool keep_in_memory = false;
Greg Clayton8f343b02010-11-04 01:54:29 +0000800 StreamString expr;
801 expr.Printf("dlclose ((void *)0x%llx)", image_addr);
802 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +0000803 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan92adcac2011-01-13 08:53:35 +0000804 ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, keep_in_memory, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton8f343b02010-11-04 01:54:29 +0000805 if (result_valobj_sp->GetError().Success())
806 {
807 Scalar scalar;
808 if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar))
809 {
810 if (scalar.UInt(1))
811 {
812 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
813 }
814 else
815 {
816 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
817 }
818 }
819 }
820 else
821 {
822 error = result_valobj_sp->GetError();
823 }
824 }
825 }
826 }
827 }
828 }
829 else
830 {
831 error.SetErrorString("invalid image token");
832 }
833 return error;
834}
835
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836DynamicLoader *
837Process::GetDynamicLoader()
838{
839 return NULL;
840}
841
842const ABI *
843Process::GetABI()
844{
845 ConstString& triple = m_target_triple;
846
847 if (triple.IsEmpty())
848 return NULL;
849
850 if (m_abi_sp.get() == NULL)
851 {
852 m_abi_sp.reset(ABI::FindPlugin(triple));
853 }
854
855 return m_abi_sp.get();
856}
857
Jim Ingham22777012010-09-23 02:01:19 +0000858LanguageRuntime *
859Process::GetLanguageRuntime(lldb::LanguageType language)
860{
861 LanguageRuntimeCollection::iterator pos;
862 pos = m_language_runtimes.find (language);
863 if (pos == m_language_runtimes.end())
864 {
865 lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language));
866
867 m_language_runtimes[language]
868 = runtime;
869 return runtime.get();
870 }
871 else
872 return (*pos).second.get();
873}
874
875CPPLanguageRuntime *
876Process::GetCPPLanguageRuntime ()
877{
878 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus);
879 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
880 return static_cast<CPPLanguageRuntime *> (runtime);
881 return NULL;
882}
883
884ObjCLanguageRuntime *
885Process::GetObjCLanguageRuntime ()
886{
887 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC);
888 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
889 return static_cast<ObjCLanguageRuntime *> (runtime);
890 return NULL;
891}
892
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893BreakpointSiteList &
894Process::GetBreakpointSiteList()
895{
896 return m_breakpoint_site_list;
897}
898
899const BreakpointSiteList &
900Process::GetBreakpointSiteList() const
901{
902 return m_breakpoint_site_list;
903}
904
905
906void
907Process::DisableAllBreakpointSites ()
908{
909 m_breakpoint_site_list.SetEnabledForAll (false);
910}
911
912Error
913Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
914{
915 Error error (DisableBreakpointSiteByID (break_id));
916
917 if (error.Success())
918 m_breakpoint_site_list.Remove(break_id);
919
920 return error;
921}
922
923Error
924Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
925{
926 Error error;
927 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
928 if (bp_site_sp)
929 {
930 if (bp_site_sp->IsEnabled())
931 error = DisableBreakpoint (bp_site_sp.get());
932 }
933 else
934 {
935 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
936 }
937
938 return error;
939}
940
941Error
942Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
943{
944 Error error;
945 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
946 if (bp_site_sp)
947 {
948 if (!bp_site_sp->IsEnabled())
949 error = EnableBreakpoint (bp_site_sp.get());
950 }
951 else
952 {
953 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
954 }
955 return error;
956}
957
Stephen Wilson50bd94f2010-07-17 00:56:13 +0000958lldb::break_id_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware)
960{
Greg Claytonf5e56de2010-09-14 23:36:40 +0000961 const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 if (load_addr != LLDB_INVALID_ADDRESS)
963 {
964 BreakpointSiteSP bp_site_sp;
965
966 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
967 // create a new breakpoint site and add it.
968
969 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
970
971 if (bp_site_sp)
972 {
973 bp_site_sp->AddOwner (owner);
974 owner->SetBreakpointSite (bp_site_sp);
975 return bp_site_sp->GetID();
976 }
977 else
978 {
979 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware));
980 if (bp_site_sp)
981 {
982 if (EnableBreakpoint (bp_site_sp.get()).Success())
983 {
984 owner->SetBreakpointSite (bp_site_sp);
985 return m_breakpoint_site_list.Add (bp_site_sp);
986 }
987 }
988 }
989 }
990 // We failed to enable the breakpoint
991 return LLDB_INVALID_BREAK_ID;
992
993}
994
995void
996Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
997{
998 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
999 if (num_owners == 0)
1000 {
1001 DisableBreakpoint(bp_site_sp.get());
1002 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1003 }
1004}
1005
1006
1007size_t
1008Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1009{
1010 size_t bytes_removed = 0;
1011 addr_t intersect_addr;
1012 size_t intersect_size;
1013 size_t opcode_offset;
1014 size_t idx;
1015 BreakpointSiteSP bp;
1016
1017 for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx)
1018 {
1019 if (bp->GetType() == BreakpointSite::eSoftware)
1020 {
1021 if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
1022 {
1023 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1024 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
1025 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1026 size_t buf_offset = intersect_addr - bp_addr;
1027 ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
1028 }
1029 }
1030 }
1031 return bytes_removed;
1032}
1033
1034
1035Error
1036Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
1037{
1038 Error error;
1039 assert (bp_site != NULL);
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001040 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 const addr_t bp_addr = bp_site->GetLoadAddress();
1042 if (log)
1043 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr);
1044 if (bp_site->IsEnabled())
1045 {
1046 if (log)
1047 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
1048 return error;
1049 }
1050
1051 if (bp_addr == LLDB_INVALID_ADDRESS)
1052 {
1053 error.SetErrorString("BreakpointSite contains an invalid load address.");
1054 return error;
1055 }
1056 // Ask the lldb::Process subclass to fill in the correct software breakpoint
1057 // trap for the breakpoint site
1058 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
1059
1060 if (bp_opcode_size == 0)
1061 {
1062 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr);
1063 }
1064 else
1065 {
1066 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
1067
1068 if (bp_opcode_bytes == NULL)
1069 {
1070 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
1071 return error;
1072 }
1073
1074 // Save the original opcode by reading it
1075 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
1076 {
1077 // Write a software breakpoint in place of the original opcode
1078 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1079 {
1080 uint8_t verify_bp_opcode_bytes[64];
1081 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1082 {
1083 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
1084 {
1085 bp_site->SetEnabled(true);
1086 bp_site->SetType (BreakpointSite::eSoftware);
1087 if (log)
1088 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS",
1089 bp_site->GetID(),
1090 (uint64_t)bp_addr);
1091 }
1092 else
1093 error.SetErrorString("Failed to verify the breakpoint trap in memory.");
1094 }
1095 else
1096 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
1097 }
1098 else
1099 error.SetErrorString("Unable to write breakpoint trap to memory.");
1100 }
1101 else
1102 error.SetErrorString("Unable to read memory at breakpoint address.");
1103 }
Stephen Wilson78a4feb2011-01-12 04:20:03 +00001104 if (log && error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1106 bp_site->GetID(),
1107 (uint64_t)bp_addr,
1108 error.AsCString());
1109 return error;
1110}
1111
1112Error
1113Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
1114{
1115 Error error;
1116 assert (bp_site != NULL);
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001117 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 addr_t bp_addr = bp_site->GetLoadAddress();
1119 lldb::user_id_t breakID = bp_site->GetID();
1120 if (log)
Stephen Wilson5394e0d2011-01-14 21:07:07 +00001121 log->Printf ("Process::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122
1123 if (bp_site->IsHardware())
1124 {
1125 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
1126 }
1127 else if (bp_site->IsEnabled())
1128 {
1129 const size_t break_op_size = bp_site->GetByteSize();
1130 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
1131 if (break_op_size > 0)
1132 {
1133 // Clear a software breakoint instruction
Greg Claytonc982c762010-07-09 20:39:50 +00001134 uint8_t curr_break_op[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00001135 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136 bool break_op_found = false;
1137
1138 // Read the breakpoint opcode
1139 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
1140 {
1141 bool verify = false;
1142 // Make sure we have the a breakpoint opcode exists at this address
1143 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
1144 {
1145 break_op_found = true;
1146 // We found a valid breakpoint opcode at this address, now restore
1147 // the saved opcode.
1148 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
1149 {
1150 verify = true;
1151 }
1152 else
1153 error.SetErrorString("Memory write failed when restoring original opcode.");
1154 }
1155 else
1156 {
1157 error.SetErrorString("Original breakpoint trap is no longer in memory.");
1158 // Set verify to true and so we can check if the original opcode has already been restored
1159 verify = true;
1160 }
1161
1162 if (verify)
1163 {
Greg Claytonc982c762010-07-09 20:39:50 +00001164 uint8_t verify_opcode[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00001165 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 // Verify that our original opcode made it back to the inferior
1167 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
1168 {
1169 // compare the memory we just read with the original opcode
1170 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
1171 {
1172 // SUCCESS
1173 bp_site->SetEnabled(false);
1174 if (log)
1175 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
1176 return error;
1177 }
1178 else
1179 {
1180 if (break_op_found)
1181 error.SetErrorString("Failed to restore original opcode.");
1182 }
1183 }
1184 else
1185 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
1186 }
1187 }
1188 else
1189 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
1190 }
1191 }
1192 else
1193 {
1194 if (log)
1195 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
1196 return error;
1197 }
1198
1199 if (log)
1200 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1201 bp_site->GetID(),
1202 (uint64_t)bp_addr,
1203 error.AsCString());
1204 return error;
1205
1206}
1207
Greg Clayton58be07b2011-01-07 06:08:19 +00001208// Comment out line below to disable memory caching
1209#define ENABLE_MEMORY_CACHING
1210// Uncomment to verify memory caching works after making changes to caching code
1211//#define VERIFY_MEMORY_READS
1212
1213#if defined (ENABLE_MEMORY_CACHING)
1214
1215#if defined (VERIFY_MEMORY_READS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216
1217size_t
1218Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1219{
Greg Clayton58be07b2011-01-07 06:08:19 +00001220 // Memory caching is enabled, with debug verification
1221 if (buf && size)
1222 {
1223 // Uncomment the line below to make sure memory caching is working.
1224 // I ran this through the test suite and got no assertions, so I am
1225 // pretty confident this is working well. If any changes are made to
1226 // memory caching, uncomment the line below and test your changes!
1227
1228 // Verify all memory reads by using the cache first, then redundantly
1229 // reading the same memory from the inferior and comparing to make sure
1230 // everything is exactly the same.
1231 std::string verify_buf (size, '\0');
1232 assert (verify_buf.size() == size);
1233 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
1234 Error verify_error;
1235 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
1236 assert (cache_bytes_read == verify_bytes_read);
1237 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
1238 assert (verify_error.Success() == error.Success());
1239 return cache_bytes_read;
1240 }
1241 return 0;
1242}
1243
1244#else // #if defined (VERIFY_MEMORY_READS)
1245
1246size_t
1247Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1248{
1249 // Memory caching enabled, no verification
1250 return m_memory_cache.Read (this, addr, buf, size, error);
1251}
1252
1253#endif // #else for #if defined (VERIFY_MEMORY_READS)
1254
1255#else // #if defined (ENABLE_MEMORY_CACHING)
1256
1257size_t
1258Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1259{
1260 // Memory caching is disabled
1261 return ReadMemoryFromInferior (addr, buf, size, error);
1262}
1263
1264#endif // #else for #if defined (ENABLE_MEMORY_CACHING)
1265
1266
1267size_t
1268Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
1269{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 if (buf == NULL || size == 0)
1271 return 0;
1272
1273 size_t bytes_read = 0;
1274 uint8_t *bytes = (uint8_t *)buf;
1275
1276 while (bytes_read < size)
1277 {
1278 const size_t curr_size = size - bytes_read;
1279 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
1280 bytes + bytes_read,
1281 curr_size,
1282 error);
1283 bytes_read += curr_bytes_read;
1284 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
1285 break;
1286 }
1287
1288 // Replace any software breakpoint opcodes that fall into this range back
1289 // into "buf" before we return
1290 if (bytes_read > 0)
1291 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
1292 return bytes_read;
1293}
1294
Greg Clayton58a4c462010-12-16 20:01:20 +00001295uint64_t
1296Process::ReadUnsignedInteger (lldb::addr_t vm_addr, size_t integer_byte_size, Error &error)
1297{
1298 if (integer_byte_size > sizeof(uint64_t))
1299 {
1300 error.SetErrorString ("unsupported integer size");
1301 }
1302 else
1303 {
1304 uint8_t tmp[sizeof(uint64_t)];
1305 DataExtractor data (tmp, integer_byte_size, GetByteOrder(), GetAddressByteSize());
1306 if (ReadMemory (vm_addr, tmp, integer_byte_size, error) == integer_byte_size)
1307 {
1308 uint32_t offset = 0;
1309 return data.GetMaxU64 (&offset, integer_byte_size);
1310 }
1311 }
1312 // Any plug-in that doesn't return success a memory read with the number
1313 // of bytes that were requested should be setting the error
1314 assert (error.Fail());
1315 return 0;
1316}
1317
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318size_t
1319Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
1320{
1321 size_t bytes_written = 0;
1322 const uint8_t *bytes = (const uint8_t *)buf;
1323
1324 while (bytes_written < size)
1325 {
1326 const size_t curr_size = size - bytes_written;
1327 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
1328 bytes + bytes_written,
1329 curr_size,
1330 error);
1331 bytes_written += curr_bytes_written;
1332 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
1333 break;
1334 }
1335 return bytes_written;
1336}
1337
1338size_t
1339Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1340{
Greg Clayton58be07b2011-01-07 06:08:19 +00001341#if defined (ENABLE_MEMORY_CACHING)
1342 m_memory_cache.Flush (addr, size);
1343#endif
1344
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345 if (buf == NULL || size == 0)
1346 return 0;
1347 // We need to write any data that would go where any current software traps
1348 // (enabled software breakpoints) any software traps (breakpoints) that we
1349 // may have placed in our tasks memory.
1350
1351 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
1352 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
1353
1354 if (iter == end || iter->second->GetLoadAddress() > addr + size)
1355 return DoWriteMemory(addr, buf, size, error);
1356
1357 BreakpointSiteList::collection::const_iterator pos;
1358 size_t bytes_written = 0;
Greg Claytonc982c762010-07-09 20:39:50 +00001359 addr_t intersect_addr = 0;
1360 size_t intersect_size = 0;
1361 size_t opcode_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001362 const uint8_t *ubuf = (const uint8_t *)buf;
1363
1364 for (pos = iter; pos != end; ++pos)
1365 {
1366 BreakpointSiteSP bp;
1367 bp = pos->second;
1368
1369 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
1370 assert(addr <= intersect_addr && intersect_addr < addr + size);
1371 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
1372 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1373
1374 // Check for bytes before this breakpoint
1375 const addr_t curr_addr = addr + bytes_written;
1376 if (intersect_addr > curr_addr)
1377 {
1378 // There are some bytes before this breakpoint that we need to
1379 // just write to memory
1380 size_t curr_size = intersect_addr - curr_addr;
1381 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
1382 ubuf + bytes_written,
1383 curr_size,
1384 error);
1385 bytes_written += curr_bytes_written;
1386 if (curr_bytes_written != curr_size)
1387 {
1388 // We weren't able to write all of the requested bytes, we
1389 // are done looping and will return the number of bytes that
1390 // we have written so far.
1391 break;
1392 }
1393 }
1394
1395 // Now write any bytes that would cover up any software breakpoints
1396 // directly into the breakpoint opcode buffer
1397 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
1398 bytes_written += intersect_size;
1399 }
1400
1401 // Write any remaining bytes after the last breakpoint if we have any left
1402 if (bytes_written < size)
1403 bytes_written += WriteMemoryPrivate (addr + bytes_written,
1404 ubuf + bytes_written,
1405 size - bytes_written,
1406 error);
1407
1408 return bytes_written;
1409}
1410
1411addr_t
1412Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
1413{
1414 // Fixme: we should track the blocks we've allocated, and clean them up...
1415 // We could even do our own allocator here if that ends up being more efficient.
Greg Claytonb2daec92011-01-23 19:58:49 +00001416 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
1417 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1418 if (log)
Greg Clayton2ad66702011-01-24 06:30:45 +00001419 log->Printf("Process::AllocateMemory(size=%4zu, permissions=%c%c%c) => 0x%16.16llx (m_stop_id = %u)",
Greg Claytonb2daec92011-01-23 19:58:49 +00001420 size,
1421 permissions & ePermissionsReadable ? 'r' : '-',
1422 permissions & ePermissionsWritable ? 'w' : '-',
1423 permissions & ePermissionsExecutable ? 'x' : '-',
1424 (uint64_t)allocated_addr,
1425 m_stop_id);
1426 return allocated_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427}
1428
1429Error
1430Process::DeallocateMemory (addr_t ptr)
1431{
Greg Claytonb2daec92011-01-23 19:58:49 +00001432 Error error(DoDeallocateMemory (ptr));
1433
1434 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1435 if (log)
1436 log->Printf("Process::DeallocateMemory(addr=0x%16.16llx) => err = %s (m_stop_id = %u)",
1437 ptr,
1438 error.AsCString("SUCCESS"),
1439 m_stop_id);
1440 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441}
1442
1443
1444Error
1445Process::EnableWatchpoint (WatchpointLocation *watchpoint)
1446{
1447 Error error;
1448 error.SetErrorString("watchpoints are not supported");
1449 return error;
1450}
1451
1452Error
1453Process::DisableWatchpoint (WatchpointLocation *watchpoint)
1454{
1455 Error error;
1456 error.SetErrorString("watchpoints are not supported");
1457 return error;
1458}
1459
1460StateType
1461Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
1462{
1463 StateType state;
1464 // Now wait for the process to launch and return control to us, and then
1465 // call DidLaunch:
1466 while (1)
1467 {
Greg Clayton6779606a2011-01-22 23:43:18 +00001468 event_sp.reset();
1469 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
1470
1471 if (StateIsStoppedState(state))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 break;
Greg Clayton6779606a2011-01-22 23:43:18 +00001473
1474 // If state is invalid, then we timed out
1475 if (state == eStateInvalid)
1476 break;
1477
1478 if (event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001479 HandlePrivateEvent (event_sp);
1480 }
1481 return state;
1482}
1483
1484Error
1485Process::Launch
1486(
1487 char const *argv[],
1488 char const *envp[],
Greg Claytonf681b942010-08-31 18:35:14 +00001489 uint32_t launch_flags,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490 const char *stdin_path,
1491 const char *stdout_path,
Greg Claytonbd82a5d2011-01-23 05:56:20 +00001492 const char *stderr_path,
1493 const char *working_directory
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001494)
1495{
1496 Error error;
1497 m_target_triple.Clear();
1498 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001499 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500
1501 Module *exe_module = m_target.GetExecutableModule().get();
1502 if (exe_module)
1503 {
1504 char exec_file_path[PATH_MAX];
1505 exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path));
1506 if (exe_module->GetFileSpec().Exists())
1507 {
1508 error = WillLaunch (exe_module);
1509 if (error.Success())
1510 {
Greg Clayton05faeb72010-10-07 04:19:01 +00001511 SetPublicState (eStateLaunching);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 // The args coming in should not contain the application name, the
1513 // lldb_private::Process class will add this in case the executable
1514 // gets resolved to a different file than was given on the command
1515 // line (like when an applicaiton bundle is specified and will
1516 // resolve to the contained exectuable file, or the file given was
1517 // a symlink or other file system link that resolves to a different
1518 // file).
1519
1520 // Get the resolved exectuable path
1521
1522 // Make a new argument vector
1523 std::vector<const char *> exec_path_plus_argv;
1524 // Append the resolved executable path
1525 exec_path_plus_argv.push_back (exec_file_path);
1526
1527 // Push all args if there are any
1528 if (argv)
1529 {
1530 for (int i = 0; argv[i]; ++i)
1531 exec_path_plus_argv.push_back(argv[i]);
1532 }
1533
1534 // Push a NULL to terminate the args.
1535 exec_path_plus_argv.push_back(NULL);
1536
1537 // Now launch using these arguments.
Greg Clayton471b31c2010-07-20 22:52:08 +00001538 error = DoLaunch (exe_module,
1539 exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(),
1540 envp,
Greg Claytonf681b942010-08-31 18:35:14 +00001541 launch_flags,
Greg Clayton471b31c2010-07-20 22:52:08 +00001542 stdin_path,
1543 stdout_path,
Greg Claytonbd82a5d2011-01-23 05:56:20 +00001544 stderr_path,
1545 working_directory);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546
1547 if (error.Fail())
1548 {
1549 if (GetID() != LLDB_INVALID_PROCESS_ID)
1550 {
1551 SetID (LLDB_INVALID_PROCESS_ID);
1552 const char *error_string = error.AsCString();
1553 if (error_string == NULL)
1554 error_string = "launch failed";
1555 SetExitStatus (-1, error_string);
1556 }
1557 }
1558 else
1559 {
1560 EventSP event_sp;
1561 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1562
1563 if (state == eStateStopped || state == eStateCrashed)
1564 {
1565 DidLaunch ();
1566
1567 // This delays passing the stopped event to listeners till DidLaunch gets
1568 // a chance to complete...
1569 HandlePrivateEvent (event_sp);
1570 StartPrivateStateThread ();
1571 }
1572 else if (state == eStateExited)
1573 {
1574 // We exited while trying to launch somehow. Don't call DidLaunch as that's
1575 // not likely to work, and return an invalid pid.
1576 HandlePrivateEvent (event_sp);
1577 }
1578 }
1579 }
1580 }
1581 else
1582 {
1583 error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path);
1584 }
1585 }
1586 return error;
1587}
1588
Jim Inghambb3a2832011-01-29 01:49:25 +00001589Process::NextEventAction::EventActionResult
1590Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001591{
Jim Inghambb3a2832011-01-29 01:49:25 +00001592 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
1593 switch (state)
Greg Clayton19388cf2010-10-18 01:45:30 +00001594 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001595 case eStateStopped:
1596 case eStateCrashed:
1597 {
1598 m_process->DidAttach ();
Jim Ingham5aee1622010-08-09 23:31:02 +00001599 // Figure out which one is the executable, and set that in our target:
Jim Inghambb3a2832011-01-29 01:49:25 +00001600 ModuleList &modules = m_process->GetTarget().GetImages();
Jim Ingham5aee1622010-08-09 23:31:02 +00001601
1602 size_t num_modules = modules.GetSize();
1603 for (int i = 0; i < num_modules; i++)
1604 {
1605 ModuleSP module_sp = modules.GetModuleAtIndex(i);
1606 if (module_sp->IsExecutable())
1607 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001608 ModuleSP exec_module = m_process->GetTarget().GetExecutableModule();
Jim Ingham5aee1622010-08-09 23:31:02 +00001609 if (!exec_module || exec_module != module_sp)
1610 {
1611
Jim Inghambb3a2832011-01-29 01:49:25 +00001612 m_process->GetTarget().SetExecutableModule (module_sp, false);
Jim Ingham5aee1622010-08-09 23:31:02 +00001613 }
1614 break;
1615 }
1616 }
Jim Inghambb3a2832011-01-29 01:49:25 +00001617 return eEventActionSuccess;
1618 }
1619 break;
1620 default:
1621 case eStateExited:
1622 case eStateInvalid:
1623 m_exit_string.assign ("No valid Process");
1624 return eEventActionExit;
1625 break;
1626 }
1627}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001628
Jim Inghambb3a2832011-01-29 01:49:25 +00001629Process::NextEventAction::EventActionResult
1630Process::AttachCompletionHandler::HandleBeingInterrupted()
1631{
1632 return eEventActionSuccess;
1633}
1634
1635const char *
1636Process::AttachCompletionHandler::GetExitString ()
1637{
1638 return m_exit_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639}
1640
1641Error
1642Process::Attach (lldb::pid_t attach_pid)
1643{
1644
1645 m_target_triple.Clear();
1646 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001647 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001648
Jim Ingham5aee1622010-08-09 23:31:02 +00001649 // Find the process and its architecture. Make sure it matches the architecture
1650 // of the current Target, and if not adjust it.
1651
1652 ArchSpec attach_spec = GetArchSpecForExistingProcess (attach_pid);
1653 if (attach_spec != GetTarget().GetArchitecture())
1654 {
1655 // Set the architecture on the target.
1656 GetTarget().SetArchitecture(attach_spec);
1657 }
1658
Greg Claytonc982c762010-07-09 20:39:50 +00001659 Error error (WillAttachToProcessWithID(attach_pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001660 if (error.Success())
1661 {
Greg Clayton05faeb72010-10-07 04:19:01 +00001662 SetPublicState (eStateAttaching);
1663
Greg Claytonc982c762010-07-09 20:39:50 +00001664 error = DoAttachToProcessWithID (attach_pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001665 if (error.Success())
1666 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001667 SetNextEventAction(new Process::AttachCompletionHandler(this));
1668 StartPrivateStateThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001669 }
1670 else
1671 {
1672 if (GetID() != LLDB_INVALID_PROCESS_ID)
1673 {
1674 SetID (LLDB_INVALID_PROCESS_ID);
1675 const char *error_string = error.AsCString();
1676 if (error_string == NULL)
1677 error_string = "attach failed";
1678
1679 SetExitStatus(-1, error_string);
1680 }
1681 }
1682 }
1683 return error;
1684}
1685
1686Error
1687Process::Attach (const char *process_name, bool wait_for_launch)
1688{
1689 m_target_triple.Clear();
1690 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001691 m_process_input_reader.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00001692
1693 // Find the process and its architecture. Make sure it matches the architecture
1694 // of the current Target, and if not adjust it.
1695
Jim Ingham2ecb7422010-08-17 21:54:19 +00001696 if (!wait_for_launch)
Jim Ingham5aee1622010-08-09 23:31:02 +00001697 {
Jim Ingham2ecb7422010-08-17 21:54:19 +00001698 ArchSpec attach_spec = GetArchSpecForExistingProcess (process_name);
Greg Clayton19388cf2010-10-18 01:45:30 +00001699 if (attach_spec.IsValid() && attach_spec != GetTarget().GetArchitecture())
Jim Ingham2ecb7422010-08-17 21:54:19 +00001700 {
1701 // Set the architecture on the target.
1702 GetTarget().SetArchitecture(attach_spec);
1703 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001704 }
Jim Ingham2ecb7422010-08-17 21:54:19 +00001705
Greg Claytonc982c762010-07-09 20:39:50 +00001706 Error error (WillAttachToProcessWithName(process_name, wait_for_launch));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707 if (error.Success())
1708 {
Greg Clayton05faeb72010-10-07 04:19:01 +00001709 SetPublicState (eStateAttaching);
Greg Claytonc982c762010-07-09 20:39:50 +00001710 error = DoAttachToProcessWithName (process_name, wait_for_launch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001711 if (error.Fail())
1712 {
1713 if (GetID() != LLDB_INVALID_PROCESS_ID)
1714 {
1715 SetID (LLDB_INVALID_PROCESS_ID);
1716 const char *error_string = error.AsCString();
1717 if (error_string == NULL)
1718 error_string = "attach failed";
1719
1720 SetExitStatus(-1, error_string);
1721 }
1722 }
1723 else
1724 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001725 SetNextEventAction(new Process::AttachCompletionHandler(this));
1726 StartPrivateStateThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727 }
1728 }
1729 return error;
1730}
1731
1732Error
1733Process::Resume ()
1734{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001735 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 if (log)
Jim Ingham444586b2011-01-24 06:34:17 +00001737 log->Printf("Process::Resume() m_stop_id = %u, public state: %s private state: %s",
1738 m_stop_id,
1739 StateAsCString(m_public_state.GetValue()),
1740 StateAsCString(m_private_state.GetValue()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741
1742 Error error (WillResume());
1743 // Tell the process it is about to resume before the thread list
1744 if (error.Success())
1745 {
Johnny Chenc4221e42010-12-02 20:53:05 +00001746 // Now let the thread list know we are about to resume so it
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001747 // can let all of our threads know that they are about to be
1748 // resumed. Threads will each be called with
1749 // Thread::WillResume(StateType) where StateType contains the state
1750 // that they are supposed to have when the process is resumed
1751 // (suspended/running/stepping). Threads should also check
1752 // their resume signal in lldb::Thread::GetResumeSignal()
1753 // to see if they are suppoed to start back up with a signal.
1754 if (m_thread_list.WillResume())
1755 {
1756 error = DoResume();
1757 if (error.Success())
1758 {
1759 DidResume();
1760 m_thread_list.DidResume();
Jim Ingham444586b2011-01-24 06:34:17 +00001761 if (log)
1762 log->Printf ("Process thinks the process has resumed.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001763 }
1764 }
1765 else
1766 {
Jim Ingham444586b2011-01-24 06:34:17 +00001767 error.SetErrorStringWithFormat("Process::WillResume() thread list returned false after WillResume");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768 }
1769 }
Jim Ingham444586b2011-01-24 06:34:17 +00001770 else if (log)
1771 log->Printf ("Process::WillResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772 return error;
1773}
1774
1775Error
1776Process::Halt ()
1777{
Jim Inghambb3a2832011-01-29 01:49:25 +00001778 // Pause our private state thread so we can ensure no one else eats
1779 // the stop event out from under us.
1780 PausePrivateStateThread();
Greg Clayton3af9ea52010-11-18 05:57:03 +00001781
Jim Inghambb3a2832011-01-29 01:49:25 +00001782 EventSP event_sp;
1783 Error error;
1784
1785 if (m_public_state.GetValue() == eStateAttaching)
1786 {
1787 SetExitStatus(SIGKILL, "Cancelled async attach.");
1788 }
1789 else
1790 {
1791 error = WillHalt();
1792
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001793 if (error.Success())
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001794 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001795
1796 bool caused_stop = false;
1797
1798 // Ask the process subclass to actually halt our process
1799 error = DoHalt(caused_stop);
1800 if (error.Success())
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001801 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001802 // If "caused_stop" is true, then DoHalt stopped the process. If
1803 // "caused_stop" is false, the process was already stopped.
1804 // If the DoHalt caused the process to stop, then we want to catch
1805 // this event and set the interrupted bool to true before we pass
1806 // this along so clients know that the process was interrupted by
1807 // a halt command.
1808 if (caused_stop)
Greg Clayton3af9ea52010-11-18 05:57:03 +00001809 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001810 // Wait for 2 seconds for the process to stop.
1811 TimeValue timeout_time;
1812 timeout_time = TimeValue::Now();
1813 timeout_time.OffsetWithSeconds(1);
1814 StateType state = WaitForStateChangedEventsPrivate (&timeout_time, event_sp);
1815
1816 if (state == eStateInvalid)
Greg Clayton3af9ea52010-11-18 05:57:03 +00001817 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001818 // We timeout out and didn't get a stop event...
1819 error.SetErrorString ("Halt timed out.");
Greg Clayton3af9ea52010-11-18 05:57:03 +00001820 }
1821 else
1822 {
Jim Inghambb3a2832011-01-29 01:49:25 +00001823 if (StateIsStoppedState (state))
1824 {
1825 // We caused the process to interrupt itself, so mark this
1826 // as such in the stop event so clients can tell an interrupted
1827 // process from a natural stop
1828 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
1829 }
1830 else
1831 {
1832 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1833 if (log)
1834 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
1835 error.SetErrorString ("Did not get stopped event after halt.");
1836 }
Greg Clayton3af9ea52010-11-18 05:57:03 +00001837 }
1838 }
Jim Inghambb3a2832011-01-29 01:49:25 +00001839 DidHalt();
1840
Jim Ingham0d8bcc72010-11-17 02:32:00 +00001841 }
1842 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001843 }
Jim Inghambb3a2832011-01-29 01:49:25 +00001844 // Resume our private state thread before we post the event (if any)
1845 ResumePrivateStateThread();
1846
1847 // Post any event we might have consumed. If all goes well, we will have
1848 // stopped the process, intercepted the event and set the interrupted
1849 // bool in the event. Post it to the private event queue and that will end up
1850 // correctly setting the state.
1851 if (event_sp)
1852 m_private_state_broadcaster.BroadcastEvent(event_sp);
1853
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001854 return error;
1855}
1856
1857Error
1858Process::Detach ()
1859{
1860 Error error (WillDetach());
1861
1862 if (error.Success())
1863 {
1864 DisableAllBreakpointSites();
1865 error = DoDetach();
1866 if (error.Success())
1867 {
1868 DidDetach();
1869 StopPrivateStateThread();
1870 }
1871 }
1872 return error;
1873}
1874
1875Error
1876Process::Destroy ()
1877{
1878 Error error (WillDestroy());
1879 if (error.Success())
1880 {
1881 DisableAllBreakpointSites();
1882 error = DoDestroy();
1883 if (error.Success())
1884 {
1885 DidDestroy();
1886 StopPrivateStateThread();
1887 }
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001888 m_stdio_communication.StopReadThread();
1889 m_stdio_communication.Disconnect();
1890 if (m_process_input_reader && m_process_input_reader->IsActive())
1891 m_target.GetDebugger().PopInputReader (m_process_input_reader);
1892 if (m_process_input_reader)
1893 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894 }
1895 return error;
1896}
1897
1898Error
1899Process::Signal (int signal)
1900{
1901 Error error (WillSignal());
1902 if (error.Success())
1903 {
1904 error = DoSignal(signal);
1905 if (error.Success())
1906 DidSignal();
1907 }
1908 return error;
1909}
1910
1911UnixSignals &
1912Process::GetUnixSignals ()
1913{
1914 return m_unix_signals;
1915}
1916
1917Target &
1918Process::GetTarget ()
1919{
1920 return m_target;
1921}
1922
1923const Target &
1924Process::GetTarget () const
1925{
1926 return m_target;
1927}
1928
1929uint32_t
1930Process::GetAddressByteSize()
1931{
Greg Clayton3af9ea52010-11-18 05:57:03 +00001932 if (m_addr_byte_size == 0)
1933 return m_target.GetArchitecture().GetAddressByteSize();
1934 return m_addr_byte_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001935}
1936
1937bool
1938Process::ShouldBroadcastEvent (Event *event_ptr)
1939{
1940 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
1941 bool return_value = true;
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001942 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001943
1944 switch (state)
1945 {
1946 case eStateAttaching:
1947 case eStateLaunching:
1948 case eStateDetached:
1949 case eStateExited:
1950 case eStateUnloaded:
1951 // These events indicate changes in the state of the debugging session, always report them.
1952 return_value = true;
1953 break;
1954 case eStateInvalid:
1955 // We stopped for no apparent reason, don't report it.
1956 return_value = false;
1957 break;
1958 case eStateRunning:
1959 case eStateStepping:
1960 // If we've started the target running, we handle the cases where we
1961 // are already running and where there is a transition from stopped to
1962 // running differently.
1963 // running -> running: Automatically suppress extra running events
1964 // stopped -> running: Report except when there is one or more no votes
1965 // and no yes votes.
1966 SynchronouslyNotifyStateChanged (state);
1967 switch (m_public_state.GetValue())
1968 {
1969 case eStateRunning:
1970 case eStateStepping:
1971 // We always suppress multiple runnings with no PUBLIC stop in between.
1972 return_value = false;
1973 break;
1974 default:
1975 // TODO: make this work correctly. For now always report
1976 // run if we aren't running so we don't miss any runnning
1977 // events. If I run the lldb/test/thread/a.out file and
1978 // break at main.cpp:58, run and hit the breakpoints on
1979 // multiple threads, then somehow during the stepping over
1980 // of all breakpoints no run gets reported.
1981 return_value = true;
1982
1983 // This is a transition from stop to run.
1984 switch (m_thread_list.ShouldReportRun (event_ptr))
1985 {
1986 case eVoteYes:
1987 case eVoteNoOpinion:
1988 return_value = true;
1989 break;
1990 case eVoteNo:
1991 return_value = false;
1992 break;
1993 }
1994 break;
1995 }
1996 break;
1997 case eStateStopped:
1998 case eStateCrashed:
1999 case eStateSuspended:
2000 {
2001 // We've stopped. First see if we're going to restart the target.
2002 // If we are going to stop, then we always broadcast the event.
2003 // If we aren't going to stop, let the thread plans decide if we're going to report this event.
Jim Inghamb01e7422010-06-19 04:45:32 +00002004 // If no thread has an opinion, we don't report it.
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002005 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006 {
Greg Clayton3af9ea52010-11-18 05:57:03 +00002007 if (log)
2008 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", event_ptr, StateAsCString(state));
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002009 return true;
2010 }
2011 else
2012 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002013 RefreshStateAfterStop ();
2014
2015 if (m_thread_list.ShouldStop (event_ptr) == false)
2016 {
2017 switch (m_thread_list.ShouldReportStop (event_ptr))
2018 {
2019 case eVoteYes:
2020 Process::ProcessEventData::SetRestartedInEvent (event_ptr, true);
Johnny Chen3c230652010-10-14 00:54:32 +00002021 // Intentional fall-through here.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002022 case eVoteNoOpinion:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002023 case eVoteNo:
2024 return_value = false;
2025 break;
2026 }
2027
2028 if (log)
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002029 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002030 Resume ();
2031 }
2032 else
2033 {
2034 return_value = true;
2035 SynchronouslyNotifyStateChanged (state);
2036 }
2037 }
2038 }
2039 }
2040
2041 if (log)
2042 log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO");
2043 return return_value;
2044}
2045
2046//------------------------------------------------------------------
2047// Thread Queries
2048//------------------------------------------------------------------
2049
2050ThreadList &
2051Process::GetThreadList ()
2052{
2053 return m_thread_list;
2054}
2055
2056const ThreadList &
2057Process::GetThreadList () const
2058{
2059 return m_thread_list;
2060}
2061
2062
2063bool
2064Process::StartPrivateStateThread ()
2065{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002066 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002067
2068 if (log)
2069 log->Printf ("Process::%s ( )", __FUNCTION__);
2070
2071 // Create a thread that watches our internal state and controls which
2072 // events make it to clients (into the DCProcess event queue).
Greg Clayton3e06bd92011-01-09 21:07:35 +00002073 char thread_name[1024];
2074 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%i)>", GetID());
2075 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002076 return m_private_state_thread != LLDB_INVALID_HOST_THREAD;
2077}
2078
2079void
2080Process::PausePrivateStateThread ()
2081{
2082 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
2083}
2084
2085void
2086Process::ResumePrivateStateThread ()
2087{
2088 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
2089}
2090
2091void
2092Process::StopPrivateStateThread ()
2093{
2094 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
2095}
2096
2097void
2098Process::ControlPrivateStateThread (uint32_t signal)
2099{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002100 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002101
2102 assert (signal == eBroadcastInternalStateControlStop ||
2103 signal == eBroadcastInternalStateControlPause ||
2104 signal == eBroadcastInternalStateControlResume);
2105
2106 if (log)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00002107 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002108
Greg Clayton7ecb3a02011-01-22 17:43:17 +00002109 // Signal the private state thread. First we should copy this is case the
2110 // thread starts exiting since the private state thread will NULL this out
2111 // when it exits
2112 const lldb::thread_t private_state_thread = m_private_state_thread;
2113 if (private_state_thread != LLDB_INVALID_HOST_THREAD)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002114 {
2115 TimeValue timeout_time;
2116 bool timed_out;
2117
2118 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
2119
2120 timeout_time = TimeValue::Now();
2121 timeout_time.OffsetWithSeconds(2);
2122 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
2123 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2124
2125 if (signal == eBroadcastInternalStateControlStop)
2126 {
2127 if (timed_out)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00002128 Host::ThreadCancel (private_state_thread, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002129
2130 thread_result_t result = NULL;
Greg Clayton7ecb3a02011-01-22 17:43:17 +00002131 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Clayton49182ed2010-07-22 18:34:21 +00002132 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002133 }
2134 }
2135}
2136
2137void
2138Process::HandlePrivateEvent (EventSP &event_sp)
2139{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002140 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghambb3a2832011-01-29 01:49:25 +00002141
Greg Clayton414f5d32011-01-25 02:58:48 +00002142 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00002143
2144 // First check to see if anybody wants a shot at this event:
2145 if (m_next_event_action != NULL)
2146 {
2147 NextEventAction::EventActionResult action_result = m_next_event_action->PerformAction(event_sp);
2148 switch (action_result)
2149 {
2150 case NextEventAction::eEventActionSuccess:
2151 SetNextEventAction(NULL);
2152 break;
2153 case NextEventAction::eEventActionRetry:
2154 break;
2155 case NextEventAction::eEventActionExit:
Jim Ingham2a5fdd42011-01-29 01:57:31 +00002156 // Handle Exiting Here. If we already got an exited event,
2157 // we should just propagate it. Otherwise, swallow this event,
2158 // and set our state to exit so the next event will kill us.
2159 if (new_state != eStateExited)
2160 {
2161 // FIXME: should cons up an exited event, and discard this one.
2162 SetExitStatus(0, m_next_event_action->GetExitString());
2163 SetNextEventAction(NULL);
2164 return;
2165 }
2166 SetNextEventAction(NULL);
Jim Inghambb3a2832011-01-29 01:49:25 +00002167 break;
2168 }
2169 }
2170
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002171 // See if we should broadcast this state to external clients?
2172 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002173
2174 if (should_broadcast)
2175 {
2176 if (log)
2177 {
Greg Clayton414f5d32011-01-25 02:58:48 +00002178 log->Printf ("Process::%s (pid = %i) broadcasting new state %s (old state %s) to %s",
2179 __FUNCTION__,
2180 GetID(),
2181 StateAsCString(new_state),
2182 StateAsCString (GetState ()),
2183 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184 }
Greg Clayton414f5d32011-01-25 02:58:48 +00002185 if (StateIsRunningState (new_state))
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002186 PushProcessInputReader ();
2187 else
2188 PopProcessInputReader ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002189 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
2190 BroadcastEvent (event_sp);
2191 }
2192 else
2193 {
2194 if (log)
2195 {
Greg Clayton414f5d32011-01-25 02:58:48 +00002196 log->Printf ("Process::%s (pid = %i) suppressing state %s (old state %s): should_broadcast == false",
2197 __FUNCTION__,
2198 GetID(),
2199 StateAsCString(new_state),
2200 StateAsCString (GetState ()),
2201 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002202 }
2203 }
2204}
2205
2206void *
2207Process::PrivateStateThread (void *arg)
2208{
2209 Process *proc = static_cast<Process*> (arg);
2210 void *result = proc->RunPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002211 return result;
2212}
2213
2214void *
2215Process::RunPrivateStateThread ()
2216{
2217 bool control_only = false;
2218 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2219
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002220 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002221 if (log)
2222 log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID());
2223
2224 bool exit_now = false;
2225 while (!exit_now)
2226 {
2227 EventSP event_sp;
2228 WaitForEventsPrivate (NULL, event_sp, control_only);
2229 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
2230 {
2231 switch (event_sp->GetType())
2232 {
2233 case eBroadcastInternalStateControlStop:
2234 exit_now = true;
2235 continue; // Go to next loop iteration so we exit without
2236 break; // doing any internal state managment below
2237
2238 case eBroadcastInternalStateControlPause:
2239 control_only = true;
2240 break;
2241
2242 case eBroadcastInternalStateControlResume:
2243 control_only = false;
2244 break;
2245 }
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002246
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002247 if (log)
2248 log->Printf ("Process::%s (arg = %p, pid = %i) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType());
2249
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002250 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002251 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002252 }
2253
2254
2255 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2256
2257 if (internal_state != eStateInvalid)
2258 {
2259 HandlePrivateEvent (event_sp);
2260 }
2261
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002262 if (internal_state == eStateInvalid ||
2263 internal_state == eStateExited ||
2264 internal_state == eStateDetached )
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002265 {
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002266 if (log)
2267 log->Printf ("Process::%s (arg = %p, pid = %i) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state));
2268
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002269 break;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002270 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002271 }
2272
Caroline Tice20ad3c42010-10-29 21:48:37 +00002273 // Verify log is still enabled before attempting to write to it...
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274 if (log)
2275 log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID());
2276
Greg Clayton6ed95942011-01-22 07:12:45 +00002277 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
2278 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002279 return NULL;
2280}
2281
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002282//------------------------------------------------------------------
2283// Process Event Data
2284//------------------------------------------------------------------
2285
2286Process::ProcessEventData::ProcessEventData () :
2287 EventData (),
2288 m_process_sp (),
2289 m_state (eStateInvalid),
Greg Claytonc982c762010-07-09 20:39:50 +00002290 m_restarted (false),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002291 m_update_state (false),
2292 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002293{
2294}
2295
2296Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
2297 EventData (),
2298 m_process_sp (process_sp),
2299 m_state (state),
Greg Claytonc982c762010-07-09 20:39:50 +00002300 m_restarted (false),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002301 m_update_state (false),
2302 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002303{
2304}
2305
2306Process::ProcessEventData::~ProcessEventData()
2307{
2308}
2309
2310const ConstString &
2311Process::ProcessEventData::GetFlavorString ()
2312{
2313 static ConstString g_flavor ("Process::ProcessEventData");
2314 return g_flavor;
2315}
2316
2317const ConstString &
2318Process::ProcessEventData::GetFlavor () const
2319{
2320 return ProcessEventData::GetFlavorString ();
2321}
2322
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002323void
2324Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
2325{
2326 // This function gets called twice for each event, once when the event gets pulled
2327 // off of the private process event queue, and once when it gets pulled off of
2328 // the public event queue. m_update_state is used to distinguish these
2329 // two cases; it is false when we're just pulling it off for private handling,
2330 // and we don't want to do the breakpoint command handling then.
2331
2332 if (!m_update_state)
2333 return;
2334
2335 m_process_sp->SetPublicState (m_state);
2336
2337 // If we're stopped and haven't restarted, then do the breakpoint commands here:
2338 if (m_state == eStateStopped && ! m_restarted)
2339 {
2340 int num_threads = m_process_sp->GetThreadList().GetSize();
2341 int idx;
Greg Claytonf4b47e12010-08-04 01:40:35 +00002342
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002343 for (idx = 0; idx < num_threads; ++idx)
2344 {
2345 lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx);
2346
Jim Inghamb15bfc72010-10-20 00:39:53 +00002347 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
2348 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002349 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00002350 stop_info_sp->PerformAction(event_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002351 }
2352 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00002353
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00002354 // The stop action might restart the target. If it does, then we want to mark that in the
2355 // event so that whoever is receiving it will know to wait for the running event and reflect
2356 // that state appropriately.
2357
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002358 if (m_process_sp->GetPrivateState() == eStateRunning)
2359 SetRestarted(true);
2360 }
2361}
2362
2363void
2364Process::ProcessEventData::Dump (Stream *s) const
2365{
2366 if (m_process_sp)
2367 s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID());
2368
2369 s->Printf("state = %s", StateAsCString(GetState()));;
2370}
2371
2372const Process::ProcessEventData *
2373Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
2374{
2375 if (event_ptr)
2376 {
2377 const EventData *event_data = event_ptr->GetData();
2378 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
2379 return static_cast <const ProcessEventData *> (event_ptr->GetData());
2380 }
2381 return NULL;
2382}
2383
2384ProcessSP
2385Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
2386{
2387 ProcessSP process_sp;
2388 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2389 if (data)
2390 process_sp = data->GetProcessSP();
2391 return process_sp;
2392}
2393
2394StateType
2395Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
2396{
2397 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2398 if (data == NULL)
2399 return eStateInvalid;
2400 else
2401 return data->GetState();
2402}
2403
2404bool
2405Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
2406{
2407 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2408 if (data == NULL)
2409 return false;
2410 else
2411 return data->GetRestarted();
2412}
2413
2414void
2415Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
2416{
2417 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2418 if (data != NULL)
2419 data->SetRestarted(new_value);
2420}
2421
2422bool
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002423Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
2424{
2425 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2426 if (data == NULL)
2427 return false;
2428 else
2429 return data->GetInterrupted ();
2430}
2431
2432void
2433Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
2434{
2435 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2436 if (data != NULL)
2437 data->SetInterrupted(new_value);
2438}
2439
2440bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002441Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
2442{
2443 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2444 if (data)
2445 {
2446 data->SetUpdateStateOnRemoval();
2447 return true;
2448 }
2449 return false;
2450}
2451
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002452Target *
2453Process::CalculateTarget ()
2454{
2455 return &m_target;
2456}
2457
2458Process *
2459Process::CalculateProcess ()
2460{
2461 return this;
2462}
2463
2464Thread *
2465Process::CalculateThread ()
2466{
2467 return NULL;
2468}
2469
2470StackFrame *
2471Process::CalculateStackFrame ()
2472{
2473 return NULL;
2474}
2475
2476void
Greg Clayton0603aa92010-10-04 01:05:56 +00002477Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002478{
2479 exe_ctx.target = &m_target;
2480 exe_ctx.process = this;
2481 exe_ctx.thread = NULL;
2482 exe_ctx.frame = NULL;
2483}
2484
2485lldb::ProcessSP
2486Process::GetSP ()
2487{
2488 return GetTarget().GetProcessSP();
2489}
2490
Jim Ingham5aee1622010-08-09 23:31:02 +00002491uint32_t
2492Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2493{
2494 return 0;
2495}
2496
2497ArchSpec
2498Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
2499{
2500 return Host::GetArchSpecForExistingProcess (pid);
2501}
2502
2503ArchSpec
2504Process::GetArchSpecForExistingProcess (const char *process_name)
2505{
2506 return Host::GetArchSpecForExistingProcess (process_name);
2507}
2508
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002509void
2510Process::AppendSTDOUT (const char * s, size_t len)
2511{
Greg Clayton3af9ea52010-11-18 05:57:03 +00002512 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002513 m_stdout_data.append (s, len);
2514
Greg Claytona9ff3062010-12-05 19:16:56 +00002515 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (GetTarget().GetProcessSP(), GetState()));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002516}
2517
2518void
2519Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
2520{
2521 Process *process = (Process *) baton;
2522 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
2523}
2524
2525size_t
2526Process::ProcessInputReaderCallback (void *baton,
2527 InputReader &reader,
2528 lldb::InputReaderAction notification,
2529 const char *bytes,
2530 size_t bytes_len)
2531{
2532 Process *process = (Process *) baton;
2533
2534 switch (notification)
2535 {
2536 case eInputReaderActivate:
2537 break;
2538
2539 case eInputReaderDeactivate:
2540 break;
2541
2542 case eInputReaderReactivate:
2543 break;
2544
2545 case eInputReaderGotToken:
2546 {
2547 Error error;
2548 process->PutSTDIN (bytes, bytes_len, error);
2549 }
2550 break;
2551
Caroline Ticeefed6132010-11-19 20:47:54 +00002552 case eInputReaderInterrupt:
2553 process->Halt ();
2554 break;
2555
2556 case eInputReaderEndOfFile:
2557 process->AppendSTDOUT ("^D", 2);
2558 break;
2559
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002560 case eInputReaderDone:
2561 break;
2562
2563 }
2564
2565 return bytes_len;
2566}
2567
2568void
2569Process::ResetProcessInputReader ()
2570{
2571 m_process_input_reader.reset();
2572}
2573
2574void
2575Process::SetUpProcessInputReader (int file_descriptor)
2576{
2577 // First set up the Read Thread for reading/handling process I/O
2578
2579 std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true));
2580
2581 if (conn_ap.get())
2582 {
2583 m_stdio_communication.SetConnection (conn_ap.release());
2584 if (m_stdio_communication.IsConnected())
2585 {
2586 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
2587 m_stdio_communication.StartReadThread();
2588
2589 // Now read thread is set up, set up input reader.
2590
2591 if (!m_process_input_reader.get())
2592 {
2593 m_process_input_reader.reset (new InputReader(m_target.GetDebugger()));
2594 Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback,
2595 this,
2596 eInputReaderGranularityByte,
2597 NULL,
2598 NULL,
2599 false));
2600
2601 if (err.Fail())
2602 m_process_input_reader.reset();
2603 }
2604 }
2605 }
2606}
2607
2608void
2609Process::PushProcessInputReader ()
2610{
2611 if (m_process_input_reader && !m_process_input_reader->IsActive())
2612 m_target.GetDebugger().PushInputReader (m_process_input_reader);
2613}
2614
2615void
2616Process::PopProcessInputReader ()
2617{
2618 if (m_process_input_reader && m_process_input_reader->IsActive())
2619 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2620}
2621
Greg Clayton99d0faf2010-11-18 23:32:35 +00002622
2623void
2624Process::Initialize ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002625{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002626 UserSettingsControllerSP &usc = GetSettingsController();
2627 usc.reset (new SettingsController);
2628 UserSettingsController::InitializeSettingsController (usc,
2629 SettingsController::global_settings_table,
2630 SettingsController::instance_settings_table);
2631}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002632
Greg Clayton99d0faf2010-11-18 23:32:35 +00002633void
2634Process::Terminate ()
2635{
2636 UserSettingsControllerSP &usc = GetSettingsController();
2637 UserSettingsController::FinalizeSettingsController (usc);
2638 usc.reset();
2639}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002640
Greg Clayton99d0faf2010-11-18 23:32:35 +00002641UserSettingsControllerSP &
2642Process::GetSettingsController ()
2643{
2644 static UserSettingsControllerSP g_settings_controller;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002645 return g_settings_controller;
2646}
2647
Caroline Tice1559a462010-09-27 00:30:10 +00002648void
2649Process::UpdateInstanceName ()
2650{
2651 ModuleSP module_sp = GetTarget().GetExecutableModule();
2652 if (module_sp)
2653 {
2654 StreamString sstr;
2655 sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString());
2656
Greg Claytondbe54502010-11-19 03:46:01 +00002657 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
Caroline Tice1559a462010-09-27 00:30:10 +00002658 sstr.GetData());
2659 }
2660}
2661
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002662ExecutionResults
Jim Inghamf48169b2010-11-30 02:22:11 +00002663Process::RunThreadPlan (ExecutionContext &exe_ctx,
2664 lldb::ThreadPlanSP &thread_plan_sp,
2665 bool stop_others,
2666 bool try_all_threads,
2667 bool discard_on_error,
2668 uint32_t single_thread_timeout_usec,
2669 Stream &errors)
2670{
2671 ExecutionResults return_value = eExecutionSetupError;
2672
Jim Ingham77787032011-01-20 02:03:18 +00002673 if (thread_plan_sp.get() == NULL)
2674 {
2675 errors.Printf("RunThreadPlan called with empty thread plan.");
2676 return lldb::eExecutionSetupError;
2677 }
2678
Jim Ingham444586b2011-01-24 06:34:17 +00002679 if (m_private_state.GetValue() != eStateStopped)
2680 {
2681 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
2682 // REMOVE BEAR TRAP...
2683 // abort();
2684 }
2685
Jim Inghamf48169b2010-11-30 02:22:11 +00002686 // Save this value for restoration of the execution context after we run
2687 uint32_t tid = exe_ctx.thread->GetIndexID();
2688
2689 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
2690 // so we should arrange to reset them as well.
2691
2692 lldb::ThreadSP selected_thread_sp = exe_ctx.process->GetThreadList().GetSelectedThread();
2693 lldb::StackFrameSP selected_frame_sp;
2694
2695 uint32_t selected_tid;
2696 if (selected_thread_sp != NULL)
2697 {
2698 selected_tid = selected_thread_sp->GetIndexID();
2699 selected_frame_sp = selected_thread_sp->GetSelectedFrame();
2700 }
2701 else
2702 {
2703 selected_tid = LLDB_INVALID_THREAD_ID;
2704 }
2705
2706 exe_ctx.thread->QueueThreadPlan(thread_plan_sp, true);
2707
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00002708 Listener listener("lldb.process.listener.run-thread-plan");
Jim Inghamf48169b2010-11-30 02:22:11 +00002709 exe_ctx.process->HijackProcessEvents(&listener);
Jim Ingham444586b2011-01-24 06:34:17 +00002710
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00002711 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham77787032011-01-20 02:03:18 +00002712 if (log)
2713 {
2714 StreamString s;
2715 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Greg Clayton414f5d32011-01-25 02:58:48 +00002716 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 Ingham77787032011-01-20 02:03:18 +00002717 }
2718
Jim Inghamf48169b2010-11-30 02:22:11 +00002719 Error resume_error = exe_ctx.process->Resume ();
2720 if (!resume_error.Success())
2721 {
2722 errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString());
2723 exe_ctx.process->RestoreProcessEvents();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002724 return lldb::eExecutionSetupError;
Jim Inghamf48169b2010-11-30 02:22:11 +00002725 }
2726
2727 // We need to call the function synchronously, so spin waiting for it to return.
2728 // If we get interrupted while executing, we're going to lose our context, and
2729 // won't be able to gather the result at this point.
2730 // We set the timeout AFTER the resume, since the resume takes some time and we
2731 // don't want to charge that to the timeout.
2732
2733 TimeValue* timeout_ptr = NULL;
2734 TimeValue real_timeout;
2735
2736 if (single_thread_timeout_usec != 0)
2737 {
2738 real_timeout = TimeValue::Now();
2739 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
2740 timeout_ptr = &real_timeout;
2741 }
2742
Jim Inghamf48169b2010-11-30 02:22:11 +00002743 while (1)
2744 {
2745 lldb::EventSP event_sp;
2746 lldb::StateType stop_state = lldb::eStateInvalid;
2747 // Now wait for the process to stop again:
2748 bool got_event = listener.WaitForEvent (timeout_ptr, event_sp);
2749
2750 if (!got_event)
2751 {
2752 // Right now this is the only way to tell we've timed out...
2753 // We should interrupt the process here...
2754 // Not really sure what to do if Halt fails here...
Stephen Wilson78a4feb2011-01-12 04:20:03 +00002755 if (log) {
Jim Inghamf48169b2010-11-30 02:22:11 +00002756 if (try_all_threads)
Greg Clayton414f5d32011-01-25 02:58:48 +00002757 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, trying with all threads enabled.",
Jim Inghamf48169b2010-11-30 02:22:11 +00002758 single_thread_timeout_usec);
2759 else
Greg Clayton414f5d32011-01-25 02:58:48 +00002760 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, abandoning execution.",
Jim Inghamf48169b2010-11-30 02:22:11 +00002761 single_thread_timeout_usec);
Stephen Wilson78a4feb2011-01-12 04:20:03 +00002762 }
Jim Inghamf48169b2010-11-30 02:22:11 +00002763
Jim Inghame22e88b2011-01-22 01:30:53 +00002764 Error halt_error = exe_ctx.process->Halt();
2765
2766 if (halt_error.Success())
Jim Inghamf48169b2010-11-30 02:22:11 +00002767 {
2768 timeout_ptr = NULL;
2769 if (log)
Greg Clayton414f5d32011-01-25 02:58:48 +00002770 log->Printf ("Process::RunThreadPlan(): Halt succeeded.");
Jim Inghamf48169b2010-11-30 02:22:11 +00002771
2772 // Between the time that we got the timeout and the time we halted, but target
2773 // might have actually completed the plan. If so, we're done. Note, I call WFE here with a short
2774 // timeout to
2775 got_event = listener.WaitForEvent(NULL, event_sp);
2776
2777 if (got_event)
2778 {
2779 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2780 if (log)
2781 {
Greg Clayton414f5d32011-01-25 02:58:48 +00002782 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
Jim Inghamf48169b2010-11-30 02:22:11 +00002783 if (stop_state == lldb::eStateStopped && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
2784 log->Printf (" Event was the Halt interruption event.");
2785 }
2786
2787 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
2788 {
2789 if (log)
Greg Clayton414f5d32011-01-25 02:58:48 +00002790 log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. Exiting wait loop.");
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002791 return_value = lldb::eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +00002792 break;
2793 }
2794
2795 if (try_all_threads
2796 && (stop_state == lldb::eStateStopped && Process::ProcessEventData::GetInterruptedFromEvent (event_sp.get())))
2797 {
2798
2799 thread_plan_sp->SetStopOthers (false);
2800 if (log)
Greg Clayton414f5d32011-01-25 02:58:48 +00002801 log->Printf ("Process::RunThreadPlan(): About to resume.");
Jim Inghamf48169b2010-11-30 02:22:11 +00002802
2803 exe_ctx.process->Resume();
2804 continue;
2805 }
2806 else
2807 {
2808 exe_ctx.process->RestoreProcessEvents ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002809 return lldb::eExecutionInterrupted;
Jim Inghamf48169b2010-11-30 02:22:11 +00002810 }
2811 }
2812 }
Jim Inghame22e88b2011-01-22 01:30:53 +00002813 else
2814 {
2815
2816 if (log)
Greg Clayton414f5d32011-01-25 02:58:48 +00002817 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 Inghame22e88b2011-01-22 01:30:53 +00002818 halt_error.AsCString());
Jim Ingham444586b2011-01-24 06:34:17 +00002819// abort();
Jim Inghame22e88b2011-01-22 01:30:53 +00002820
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00002821 if (single_thread_timeout_usec != 0)
2822 {
2823 real_timeout = TimeValue::Now();
2824 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
2825 timeout_ptr = &real_timeout;
2826 }
2827 continue;
Jim Inghame22e88b2011-01-22 01:30:53 +00002828 }
2829
Jim Inghamf48169b2010-11-30 02:22:11 +00002830 }
2831
2832 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2833 if (log)
Greg Clayton414f5d32011-01-25 02:58:48 +00002834 log->Printf("Process::RunThreadPlan(): got event: %s.", StateAsCString(stop_state));
Jim Inghamf48169b2010-11-30 02:22:11 +00002835
2836 if (stop_state == lldb::eStateRunning || stop_state == lldb::eStateStepping)
2837 continue;
2838
2839 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
2840 {
Greg Clayton414f5d32011-01-25 02:58:48 +00002841 if (log)
2842 log->Printf("Process::RunThreadPlan(): thread plan is done");
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002843 return_value = lldb::eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +00002844 break;
2845 }
2846 else if (exe_ctx.thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
2847 {
Greg Clayton414f5d32011-01-25 02:58:48 +00002848 if (log)
2849 log->Printf("Process::RunThreadPlan(): thread plan was discarded");
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002850 return_value = lldb::eExecutionDiscarded;
Jim Inghamf48169b2010-11-30 02:22:11 +00002851 break;
2852 }
2853 else
2854 {
2855 if (log)
2856 {
2857 StreamString s;
Jim Inghame22e88b2011-01-22 01:30:53 +00002858 if (event_sp)
2859 event_sp->Dump (&s);
2860 else
2861 {
Greg Clayton414f5d32011-01-25 02:58:48 +00002862 log->Printf ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
Jim Inghame22e88b2011-01-22 01:30:53 +00002863 }
2864
Jim Inghamf48169b2010-11-30 02:22:11 +00002865 StreamString ts;
2866
2867 const char *event_explanation;
2868
2869 do
2870 {
2871 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
2872
2873 if (!event_data)
2874 {
2875 event_explanation = "<no event data>";
2876 break;
2877 }
2878
2879 Process *process = event_data->GetProcessSP().get();
2880
2881 if (!process)
2882 {
2883 event_explanation = "<no process>";
2884 break;
2885 }
2886
2887 ThreadList &thread_list = process->GetThreadList();
2888
2889 uint32_t num_threads = thread_list.GetSize();
2890 uint32_t thread_index;
2891
2892 ts.Printf("<%u threads> ", num_threads);
2893
2894 for (thread_index = 0;
2895 thread_index < num_threads;
2896 ++thread_index)
2897 {
2898 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
2899
2900 if (!thread)
2901 {
2902 ts.Printf("<?> ");
2903 continue;
2904 }
2905
Jim Inghame22e88b2011-01-22 01:30:53 +00002906 ts.Printf("<0x%4.4x ", thread->GetID());
Greg Clayton5ccbd292011-01-06 22:15:06 +00002907 RegisterContext *register_context = thread->GetRegisterContext().get();
Jim Inghamf48169b2010-11-30 02:22:11 +00002908
2909 if (register_context)
2910 ts.Printf("[ip 0x%llx] ", register_context->GetPC());
2911 else
2912 ts.Printf("[ip unknown] ");
2913
2914 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
2915 if (stop_info_sp)
2916 {
2917 const char *stop_desc = stop_info_sp->GetDescription();
2918 if (stop_desc)
2919 ts.PutCString (stop_desc);
2920 }
2921 ts.Printf(">");
2922 }
2923
2924 event_explanation = ts.GetData();
2925 } while (0);
2926
Jim Inghame22e88b2011-01-22 01:30:53 +00002927 // See if any of the threads that stopped think we ought to stop. Otherwise continue on.
2928 if (!GetThreadList().ShouldStop(event_sp.get()))
2929 {
2930 if (log)
Greg Clayton414f5d32011-01-25 02:58:48 +00002931 log->Printf("Process::RunThreadPlan(): execution interrupted, but nobody wanted to stop, so we continued: %s %s",
Jim Inghame22e88b2011-01-22 01:30:53 +00002932 s.GetData(), event_explanation);
2933 if (single_thread_timeout_usec != 0)
2934 {
2935 real_timeout = TimeValue::Now();
2936 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
2937 timeout_ptr = &real_timeout;
2938 }
2939
2940 continue;
2941 }
2942 else
2943 {
2944 if (log)
Greg Clayton414f5d32011-01-25 02:58:48 +00002945 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
Jim Inghame22e88b2011-01-22 01:30:53 +00002946 }
Jim Inghamf48169b2010-11-30 02:22:11 +00002947 }
2948
2949 if (discard_on_error && thread_plan_sp)
2950 {
2951 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
2952 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002953 return_value = lldb::eExecutionInterrupted;
Jim Inghamf48169b2010-11-30 02:22:11 +00002954 break;
2955 }
2956 }
2957
2958 if (exe_ctx.process)
2959 exe_ctx.process->RestoreProcessEvents ();
2960
2961 // Thread we ran the function in may have gone away because we ran the target
2962 // Check that it's still there.
2963 exe_ctx.thread = exe_ctx.process->GetThreadList().FindThreadByIndexID(tid, true).get();
2964 if (exe_ctx.thread)
2965 exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex(0).get();
2966
2967 // Also restore the current process'es selected frame & thread, since this function calling may
2968 // be done behind the user's back.
2969
2970 if (selected_tid != LLDB_INVALID_THREAD_ID)
2971 {
2972 if (exe_ctx.process->GetThreadList().SetSelectedThreadByIndexID (selected_tid))
2973 {
2974 // We were able to restore the selected thread, now restore the frame:
2975 exe_ctx.process->GetThreadList().GetSelectedThread()->SetSelectedFrame(selected_frame_sp.get());
2976 }
2977 }
2978
2979 return return_value;
2980}
2981
2982const char *
2983Process::ExecutionResultAsCString (ExecutionResults result)
2984{
2985 const char *result_name;
2986
2987 switch (result)
2988 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002989 case lldb::eExecutionCompleted:
Jim Inghamf48169b2010-11-30 02:22:11 +00002990 result_name = "eExecutionCompleted";
2991 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002992 case lldb::eExecutionDiscarded:
Jim Inghamf48169b2010-11-30 02:22:11 +00002993 result_name = "eExecutionDiscarded";
2994 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002995 case lldb::eExecutionInterrupted:
Jim Inghamf48169b2010-11-30 02:22:11 +00002996 result_name = "eExecutionInterrupted";
2997 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002998 case lldb::eExecutionSetupError:
Jim Inghamf48169b2010-11-30 02:22:11 +00002999 result_name = "eExecutionSetupError";
3000 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003001 case lldb::eExecutionTimedOut:
Jim Inghamf48169b2010-11-30 02:22:11 +00003002 result_name = "eExecutionTimedOut";
3003 break;
3004 }
3005 return result_name;
3006}
3007
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003008//--------------------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00003009// class Process::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003010//--------------------------------------------------------------
3011
Greg Clayton1b654882010-09-19 02:33:57 +00003012Process::SettingsController::SettingsController () :
Caroline Ticedaccaa92010-09-20 20:44:43 +00003013 UserSettingsController ("process", Target::GetSettingsController())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003014{
Greg Clayton85851dd2010-12-04 00:10:17 +00003015 m_default_settings.reset (new ProcessInstanceSettings (*this,
3016 false,
Caroline Tice91123da2010-09-08 17:48:55 +00003017 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003018}
3019
Greg Clayton1b654882010-09-19 02:33:57 +00003020Process::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003021{
3022}
3023
3024lldb::InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00003025Process::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003026{
Greg Claytondbe54502010-11-19 03:46:01 +00003027 ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*GetSettingsController(),
3028 false,
3029 instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003030 lldb::InstanceSettingsSP new_settings_sp (new_settings);
3031 return new_settings_sp;
3032}
3033
3034//--------------------------------------------------------------
3035// class ProcessInstanceSettings
3036//--------------------------------------------------------------
3037
Greg Clayton85851dd2010-12-04 00:10:17 +00003038ProcessInstanceSettings::ProcessInstanceSettings
3039(
3040 UserSettingsController &owner,
3041 bool live_instance,
3042 const char *name
3043) :
3044 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003045 m_run_args (),
3046 m_env_vars (),
3047 m_input_path (),
3048 m_output_path (),
3049 m_error_path (),
3050 m_plugin (),
Caroline Ticef8da8632010-12-03 18:46:09 +00003051 m_disable_aslr (true),
Greg Clayton85851dd2010-12-04 00:10:17 +00003052 m_disable_stdio (false),
3053 m_inherit_host_env (true),
3054 m_got_host_env (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003055{
Caroline Ticef20e8232010-09-09 18:26:37 +00003056 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
3057 // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers.
3058 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
Caroline Tice9e41c152010-09-16 19:05:55 +00003059 // This is true for CreateInstanceName() too.
3060
3061 if (GetInstanceName () == InstanceSettings::InvalidName())
3062 {
3063 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
3064 m_owner.RegisterInstanceSettings (this);
3065 }
Caroline Ticef20e8232010-09-09 18:26:37 +00003066
3067 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003068 {
3069 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3070 CopyInstanceSettings (pending_settings,false);
Caroline Ticef20e8232010-09-09 18:26:37 +00003071 //m_owner.RemovePendingSettings (m_instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003072 }
3073}
3074
3075ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:01 +00003076 InstanceSettings (*Process::GetSettingsController(), CreateInstanceName().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003077 m_run_args (rhs.m_run_args),
3078 m_env_vars (rhs.m_env_vars),
3079 m_input_path (rhs.m_input_path),
3080 m_output_path (rhs.m_output_path),
3081 m_error_path (rhs.m_error_path),
3082 m_plugin (rhs.m_plugin),
Caroline Ticef8da8632010-12-03 18:46:09 +00003083 m_disable_aslr (rhs.m_disable_aslr),
3084 m_disable_stdio (rhs.m_disable_stdio)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003085{
3086 if (m_instance_name != InstanceSettings::GetDefaultName())
3087 {
3088 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3089 CopyInstanceSettings (pending_settings,false);
3090 m_owner.RemovePendingSettings (m_instance_name);
3091 }
3092}
3093
3094ProcessInstanceSettings::~ProcessInstanceSettings ()
3095{
3096}
3097
3098ProcessInstanceSettings&
3099ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs)
3100{
3101 if (this != &rhs)
3102 {
3103 m_run_args = rhs.m_run_args;
3104 m_env_vars = rhs.m_env_vars;
3105 m_input_path = rhs.m_input_path;
3106 m_output_path = rhs.m_output_path;
3107 m_error_path = rhs.m_error_path;
3108 m_plugin = rhs.m_plugin;
3109 m_disable_aslr = rhs.m_disable_aslr;
Caroline Ticef8da8632010-12-03 18:46:09 +00003110 m_disable_stdio = rhs.m_disable_stdio;
Greg Clayton85851dd2010-12-04 00:10:17 +00003111 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003112 }
3113
3114 return *this;
3115}
3116
3117
3118void
3119ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
3120 const char *index_value,
3121 const char *value,
3122 const ConstString &instance_name,
3123 const SettingEntry &entry,
3124 lldb::VarSetOperationType op,
3125 Error &err,
3126 bool pending)
3127{
3128 if (var_name == RunArgsVarName())
3129 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
3130 else if (var_name == EnvVarsVarName())
Greg Clayton85851dd2010-12-04 00:10:17 +00003131 {
3132 GetHostEnvironmentIfNeeded ();
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003133 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
Greg Clayton85851dd2010-12-04 00:10:17 +00003134 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003135 else if (var_name == InputPathVarName())
3136 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
3137 else if (var_name == OutputPathVarName())
3138 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
3139 else if (var_name == ErrorPathVarName())
3140 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
3141 else if (var_name == PluginVarName())
3142 UserSettingsController::UpdateEnumVariable (entry.enum_values, (int *) &m_plugin, value, err);
Greg Clayton85851dd2010-12-04 00:10:17 +00003143 else if (var_name == InheritHostEnvVarName())
3144 UserSettingsController::UpdateBooleanVariable (op, m_inherit_host_env, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003145 else if (var_name == DisableASLRVarName())
3146 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err);
Caroline Ticef8da8632010-12-03 18:46:09 +00003147 else if (var_name == DisableSTDIOVarName ())
3148 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003149}
3150
3151void
3152ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
3153 bool pending)
3154{
3155 if (new_settings.get() == NULL)
3156 return;
3157
3158 ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get();
3159
3160 m_run_args = new_process_settings->m_run_args;
3161 m_env_vars = new_process_settings->m_env_vars;
3162 m_input_path = new_process_settings->m_input_path;
3163 m_output_path = new_process_settings->m_output_path;
3164 m_error_path = new_process_settings->m_error_path;
3165 m_plugin = new_process_settings->m_plugin;
3166 m_disable_aslr = new_process_settings->m_disable_aslr;
Caroline Ticef8da8632010-12-03 18:46:09 +00003167 m_disable_stdio = new_process_settings->m_disable_stdio;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003168}
3169
Caroline Tice12cecd72010-09-20 21:37:42 +00003170bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003171ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
3172 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00003173 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00003174 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003175{
3176 if (var_name == RunArgsVarName())
3177 {
3178 if (m_run_args.GetArgumentCount() > 0)
Greg Claytona52c1552010-09-14 03:47:41 +00003179 {
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003180 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
3181 value.AppendString (m_run_args.GetArgumentAtIndex (i));
Greg Claytona52c1552010-09-14 03:47:41 +00003182 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003183 }
3184 else if (var_name == EnvVarsVarName())
3185 {
Greg Clayton85851dd2010-12-04 00:10:17 +00003186 GetHostEnvironmentIfNeeded ();
3187
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003188 if (m_env_vars.size() > 0)
3189 {
3190 std::map<std::string, std::string>::iterator pos;
3191 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
3192 {
3193 StreamString value_str;
3194 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
3195 value.AppendString (value_str.GetData());
3196 }
3197 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003198 }
3199 else if (var_name == InputPathVarName())
3200 {
3201 value.AppendString (m_input_path.c_str());
3202 }
3203 else if (var_name == OutputPathVarName())
3204 {
3205 value.AppendString (m_output_path.c_str());
3206 }
3207 else if (var_name == ErrorPathVarName())
3208 {
3209 value.AppendString (m_error_path.c_str());
3210 }
3211 else if (var_name == PluginVarName())
3212 {
3213 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, (int) m_plugin));
3214 }
Greg Clayton5c5f1a12010-12-04 00:12:24 +00003215 else if (var_name == InheritHostEnvVarName())
3216 {
3217 if (m_inherit_host_env)
3218 value.AppendString ("true");
3219 else
3220 value.AppendString ("false");
3221 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003222 else if (var_name == DisableASLRVarName())
3223 {
3224 if (m_disable_aslr)
3225 value.AppendString ("true");
3226 else
3227 value.AppendString ("false");
3228 }
Caroline Ticef8da8632010-12-03 18:46:09 +00003229 else if (var_name == DisableSTDIOVarName())
3230 {
3231 if (m_disable_stdio)
3232 value.AppendString ("true");
3233 else
3234 value.AppendString ("false");
3235 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003236 else
Caroline Tice12cecd72010-09-20 21:37:42 +00003237 {
3238 if (err)
3239 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
3240 return false;
3241 }
3242 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003243}
3244
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003245const ConstString
3246ProcessInstanceSettings::CreateInstanceName ()
3247{
3248 static int instance_count = 1;
3249 StreamString sstr;
3250
3251 sstr.Printf ("process_%d", instance_count);
3252 ++instance_count;
3253
3254 const ConstString ret_val (sstr.GetData());
3255 return ret_val;
3256}
3257
3258const ConstString &
3259ProcessInstanceSettings::RunArgsVarName ()
3260{
3261 static ConstString run_args_var_name ("run-args");
3262
3263 return run_args_var_name;
3264}
3265
3266const ConstString &
3267ProcessInstanceSettings::EnvVarsVarName ()
3268{
3269 static ConstString env_vars_var_name ("env-vars");
3270
3271 return env_vars_var_name;
3272}
3273
3274const ConstString &
Greg Clayton85851dd2010-12-04 00:10:17 +00003275ProcessInstanceSettings::InheritHostEnvVarName ()
3276{
3277 static ConstString g_name ("inherit-env");
3278
3279 return g_name;
3280}
3281
3282const ConstString &
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003283ProcessInstanceSettings::InputPathVarName ()
3284{
3285 static ConstString input_path_var_name ("input-path");
3286
3287 return input_path_var_name;
3288}
3289
3290const ConstString &
3291ProcessInstanceSettings::OutputPathVarName ()
3292{
Caroline Tice49e27372010-09-07 18:35:40 +00003293 static ConstString output_path_var_name ("output-path");
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003294
3295 return output_path_var_name;
3296}
3297
3298const ConstString &
3299ProcessInstanceSettings::ErrorPathVarName ()
3300{
Caroline Tice49e27372010-09-07 18:35:40 +00003301 static ConstString error_path_var_name ("error-path");
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003302
3303 return error_path_var_name;
3304}
3305
3306const ConstString &
3307ProcessInstanceSettings::PluginVarName ()
3308{
3309 static ConstString plugin_var_name ("plugin");
3310
3311 return plugin_var_name;
3312}
3313
3314
3315const ConstString &
3316ProcessInstanceSettings::DisableASLRVarName ()
3317{
3318 static ConstString disable_aslr_var_name ("disable-aslr");
3319
3320 return disable_aslr_var_name;
3321}
3322
Caroline Ticef8da8632010-12-03 18:46:09 +00003323const ConstString &
3324ProcessInstanceSettings::DisableSTDIOVarName ()
3325{
3326 static ConstString disable_stdio_var_name ("disable-stdio");
3327
3328 return disable_stdio_var_name;
3329}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003330
3331//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00003332// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003333//--------------------------------------------------
3334
3335SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00003336Process::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003337{
3338 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
3339 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
3340};
3341
3342
3343lldb::OptionEnumValueElement
Greg Clayton1b654882010-09-19 02:33:57 +00003344Process::SettingsController::g_plugins[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003345{
Caroline Tice5c9fdfa2010-09-09 18:01:59 +00003346 { eMacosx, "process.macosx", "Use the native MacOSX debugger plugin" },
3347 { eRemoteDebugger, "process.gdb-remote" , "Use the GDB Remote protocol based debugger plugin" },
3348 { 0, NULL, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003349};
3350
3351SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00003352Process::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003353{
Greg Clayton85851dd2010-12-04 00:10:17 +00003354 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
3355 { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
3356 { "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." },
3357 { "inherit-env", eSetVarTypeBoolean, "true", NULL, false, false, "Inherit the environment from the process that is running LLDB." },
Greg Claytonbd82a5d2011-01-23 05:56:20 +00003358 { "input-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for reading its input." },
3359 { "output-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writing its output." },
3360 { "error-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writings its error messages." },
3361 { "plugin", eSetVarTypeEnum, NULL, g_plugins, false, false, "The plugin to be used to run the process." },
Greg Clayton85851dd2010-12-04 00:10:17 +00003362 { "disable-aslr", eSetVarTypeBoolean, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
3363 { "disable-stdio", eSetVarTypeBoolean, "false", NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" },
3364 { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003365};
3366
3367
Jim Ingham5aee1622010-08-09 23:31:02 +00003368