blob: 940ff2bd9494f804677f6c10b0d8bd57e0ed8707 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Process.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Process.h"
11
12#include "lldb/lldb-private-log.h"
13
14#include "lldb/Breakpoint/StoppointCallbackContext.h"
15#include "lldb/Breakpoint/BreakpointLocation.h"
16#include "lldb/Core/Event.h"
Caroline Tice861efb32010-11-16 05:07:41 +000017#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/Debugger.h"
Caroline Tice861efb32010-11-16 05:07:41 +000019#include "lldb/Core/InputReader.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Log.h"
21#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/State.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000023#include "lldb/Expression/ClangUserExpression.h"
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000024#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Host/Host.h"
26#include "lldb/Target/ABI.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000027#include "lldb/Target/DynamicLoader.h"
Jim Ingham642036f2010-09-23 02:01:19 +000028#include "lldb/Target/LanguageRuntime.h"
29#include "lldb/Target/CPPLanguageRuntime.h"
30#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000031#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000033#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034#include "lldb/Target/Target.h"
35#include "lldb/Target/TargetList.h"
36#include "lldb/Target/Thread.h"
37#include "lldb/Target/ThreadPlan.h"
38
39using namespace lldb;
40using namespace lldb_private;
41
Greg Clayton24bc5d92011-03-30 18:16:51 +000042void
43ProcessInfo::Dump (Stream &s, Platform *platform) const
44{
45 const char *cstr;
Greg Claytonff39f742011-04-01 00:29:43 +000046 if (m_pid != LLDB_INVALID_PROCESS_ID)
47 s.Printf (" pid = %i\n", m_pid);
48
49 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
50 s.Printf (" parent = %i\n", m_parent_pid);
51
52 if (m_executable)
53 {
54 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
55 s.PutCString (" file = ");
56 m_executable.Dump(&s);
57 s.EOL();
58 }
59 const uint32_t argc = m_args.GetSize();
60 if (argc > 0)
61 {
62 for (uint32_t i=0; i<argc; i++)
63 {
64 if (i < 10)
65 s.Printf (" arg[%u] = %s\n", i, m_args.GetStringAtIndex(i));
66 else
67 s.Printf ("arg[%u] = %s\n", i, m_args.GetStringAtIndex(i));
68 }
69 }
70 if (m_arch.IsValid())
71 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
72
Greg Clayton24bc5d92011-03-30 18:16:51 +000073 if (m_real_uid != UINT32_MAX)
74 {
75 cstr = platform->GetUserName (m_real_uid);
Greg Claytonff39f742011-04-01 00:29:43 +000076 s.Printf (" uid = %-5u (%s)\n", m_real_uid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000077 }
78 if (m_real_gid != UINT32_MAX)
79 {
80 cstr = platform->GetGroupName (m_real_gid);
Greg Claytonff39f742011-04-01 00:29:43 +000081 s.Printf (" gid = %-5u (%s)\n", m_real_gid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000082 }
83 if (m_effective_uid != UINT32_MAX)
84 {
85 cstr = platform->GetUserName (m_effective_uid);
Greg Claytonff39f742011-04-01 00:29:43 +000086 s.Printf (" euid = %-5u (%s)\n", m_effective_uid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000087 }
88 if (m_effective_gid != UINT32_MAX)
89 {
90 cstr = platform->GetGroupName (m_effective_gid);
Greg Claytonff39f742011-04-01 00:29:43 +000091 s.Printf (" egid = %-5u (%s)\n", m_effective_gid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000092 }
93}
94
95void
Greg Claytonff39f742011-04-01 00:29:43 +000096ProcessInfo::DumpTableHeader (Stream &s, Platform *platform, bool verbose)
Greg Clayton24bc5d92011-03-30 18:16:51 +000097{
Greg Claytonff39f742011-04-01 00:29:43 +000098 if (verbose)
99 {
100 s.PutCString ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE NAME\n");
101 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
102 }
103 else
104 {
105 s.PutCString ("PID PARENT USER ARCH NAME\n");
106 s.PutCString ("====== ====== ========== ======= ============================\n");
107 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000108}
109
110void
Greg Claytonff39f742011-04-01 00:29:43 +0000111ProcessInfo::DumpAsTableRow (Stream &s, Platform *platform, bool verbose) const
Greg Clayton24bc5d92011-03-30 18:16:51 +0000112{
113 if (m_pid != LLDB_INVALID_PROCESS_ID)
114 {
115 const char *cstr;
116 s.Printf ("%-6u %-6u ", m_pid, m_parent_pid);
117
Greg Clayton24bc5d92011-03-30 18:16:51 +0000118
Greg Claytonff39f742011-04-01 00:29:43 +0000119 if (verbose)
120 {
121 cstr = platform->GetUserName (m_real_uid);
122 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
123 s.Printf ("%-10s ", cstr);
124 else
125 s.Printf ("%-10u ", m_real_uid);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000126
Greg Claytonff39f742011-04-01 00:29:43 +0000127 cstr = platform->GetGroupName (m_real_gid);
128 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
129 s.Printf ("%-10s ", cstr);
130 else
131 s.Printf ("%-10u ", m_real_gid);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000132
Greg Claytonff39f742011-04-01 00:29:43 +0000133 cstr = platform->GetUserName (m_effective_uid);
134 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
135 s.Printf ("%-10s ", cstr);
136 else
137 s.Printf ("%-10u ", m_effective_uid);
138
139 cstr = platform->GetGroupName (m_effective_gid);
140 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
141 s.Printf ("%-10s ", cstr);
142 else
143 s.Printf ("%-10u ", m_effective_gid);
144 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
145 }
146 else
147 {
148 s.Printf ("%-10s %.*-7s ",
149 platform->GetUserName (m_effective_uid),
150 (int)m_arch.GetTriple().getArchName().size(),
151 m_arch.GetTriple().getArchName().data());
152 }
153
154 if (verbose)
155 {
156 const uint32_t argc = m_args.GetSize();
157 if (argc > 0)
158 {
159 for (uint32_t i=0; i<argc; i++)
160 {
161 if (i > 0)
162 s.PutChar (' ');
163 s.PutCString (m_args.GetStringAtIndex(i));
164 }
165 }
166 }
167 else
168 {
169 s.PutCString (GetName());
170 }
171
172 s.EOL();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000173 }
174}
175
176bool
177ProcessInfoMatch::NameMatches (const char *process_name) const
178{
179 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
180 return true;
181 const char *match_name = m_match_info.GetName();
182 if (!match_name)
183 return true;
184
185 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
186}
187
188bool
189ProcessInfoMatch::Matches (const ProcessInfo &proc_info) const
190{
191 if (!NameMatches (proc_info.GetName()))
192 return false;
193
194 if (m_match_info.ProcessIDIsValid() &&
195 m_match_info.GetProcessID() != proc_info.GetProcessID())
196 return false;
197
198 if (m_match_info.ParentProcessIDIsValid() &&
199 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
200 return false;
201
202 if (m_match_info.RealUserIDIsValid () &&
203 m_match_info.GetRealUserID() != proc_info.GetRealUserID())
204 return false;
205
206 if (m_match_info.RealGroupIDIsValid () &&
207 m_match_info.GetRealGroupID() != proc_info.GetRealGroupID())
208 return false;
209
210 if (m_match_info.EffectiveUserIDIsValid () &&
211 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
212 return false;
213
214 if (m_match_info.EffectiveGroupIDIsValid () &&
215 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
216 return false;
217
218 if (m_match_info.GetArchitecture().IsValid() &&
219 m_match_info.GetArchitecture() != proc_info.GetArchitecture())
220 return false;
221 return true;
222}
223
224bool
225ProcessInfoMatch::MatchAllProcesses () const
226{
227 if (m_name_match_type != eNameMatchIgnore)
228 return false;
229
230 if (m_match_info.ProcessIDIsValid())
231 return false;
232
233 if (m_match_info.ParentProcessIDIsValid())
234 return false;
235
236 if (m_match_info.RealUserIDIsValid ())
237 return false;
238
239 if (m_match_info.RealGroupIDIsValid ())
240 return false;
241
242 if (m_match_info.EffectiveUserIDIsValid ())
243 return false;
244
245 if (m_match_info.EffectiveGroupIDIsValid ())
246 return false;
247
248 if (m_match_info.GetArchitecture().IsValid())
249 return false;
250
251 if (m_match_all_users)
252 return false;
253
254 return true;
255
256}
257
258void
259ProcessInfoMatch::Clear()
260{
261 m_match_info.Clear();
262 m_name_match_type = eNameMatchIgnore;
263 m_match_all_users = false;
264}
Greg Claytonfd119992011-01-07 06:08:19 +0000265
266//----------------------------------------------------------------------
267// MemoryCache constructor
268//----------------------------------------------------------------------
269Process::MemoryCache::MemoryCache() :
270 m_cache_line_byte_size (512),
271 m_cache_mutex (Mutex::eMutexTypeRecursive),
272 m_cache ()
273{
274}
275
276//----------------------------------------------------------------------
277// Destructor
278//----------------------------------------------------------------------
279Process::MemoryCache::~MemoryCache()
280{
281}
282
283void
284Process::MemoryCache::Clear()
285{
286 Mutex::Locker locker (m_cache_mutex);
287 m_cache.clear();
288}
289
290void
291Process::MemoryCache::Flush (addr_t addr, size_t size)
292{
293 if (size == 0)
294 return;
295
296 const uint32_t cache_line_byte_size = m_cache_line_byte_size;
297 const addr_t end_addr = (addr + size - 1);
298 const addr_t flush_start_addr = addr - (addr % cache_line_byte_size);
299 const addr_t flush_end_addr = end_addr - (end_addr % cache_line_byte_size);
300
301 Mutex::Locker locker (m_cache_mutex);
302 if (m_cache.empty())
303 return;
304
305 assert ((flush_start_addr % cache_line_byte_size) == 0);
306
307 for (addr_t curr_addr = flush_start_addr; curr_addr <= flush_end_addr; curr_addr += cache_line_byte_size)
308 {
309 collection::iterator pos = m_cache.find (curr_addr);
310 if (pos != m_cache.end())
311 m_cache.erase(pos);
312 }
313}
314
315size_t
316Process::MemoryCache::Read
317(
318 Process *process,
319 addr_t addr,
320 void *dst,
321 size_t dst_len,
322 Error &error
323)
324{
325 size_t bytes_left = dst_len;
326 if (dst && bytes_left > 0)
327 {
328 const uint32_t cache_line_byte_size = m_cache_line_byte_size;
329 uint8_t *dst_buf = (uint8_t *)dst;
330 addr_t curr_addr = addr - (addr % cache_line_byte_size);
331 addr_t cache_offset = addr - curr_addr;
332 Mutex::Locker locker (m_cache_mutex);
333
334 while (bytes_left > 0)
335 {
336 collection::const_iterator pos = m_cache.find (curr_addr);
337 collection::const_iterator end = m_cache.end ();
338
339 if (pos != end)
340 {
341 size_t curr_read_size = cache_line_byte_size - cache_offset;
342 if (curr_read_size > bytes_left)
343 curr_read_size = bytes_left;
344
345 memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes() + cache_offset, curr_read_size);
346
347 bytes_left -= curr_read_size;
348 curr_addr += curr_read_size + cache_offset;
349 cache_offset = 0;
350
351 if (bytes_left > 0)
352 {
353 // Get sequential cache page hits
354 for (++pos; (pos != end) && (bytes_left > 0); ++pos)
355 {
356 assert ((curr_addr % cache_line_byte_size) == 0);
357
358 if (pos->first != curr_addr)
359 break;
360
361 curr_read_size = pos->second->GetByteSize();
362 if (curr_read_size > bytes_left)
363 curr_read_size = bytes_left;
364
365 memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes(), curr_read_size);
366
367 bytes_left -= curr_read_size;
368 curr_addr += curr_read_size;
369
370 // We have a cache page that succeeded to read some bytes
371 // but not an entire page. If this happens, we must cap
372 // off how much data we are able to read...
373 if (pos->second->GetByteSize() != cache_line_byte_size)
374 return dst_len - bytes_left;
375 }
376 }
377 }
378
379 // We need to read from the process
380
381 if (bytes_left > 0)
382 {
383 assert ((curr_addr % cache_line_byte_size) == 0);
384 std::auto_ptr<DataBufferHeap> data_buffer_heap_ap(new DataBufferHeap (cache_line_byte_size, 0));
385 size_t process_bytes_read = process->ReadMemoryFromInferior (curr_addr,
386 data_buffer_heap_ap->GetBytes(),
387 data_buffer_heap_ap->GetByteSize(),
388 error);
389 if (process_bytes_read == 0)
390 return dst_len - bytes_left;
391
392 if (process_bytes_read != cache_line_byte_size)
393 data_buffer_heap_ap->SetByteSize (process_bytes_read);
394 m_cache[curr_addr] = DataBufferSP (data_buffer_heap_ap.release());
395 // We have read data and put it into the cache, continue through the
396 // loop again to get the data out of the cache...
397 }
398 }
399 }
400
401 return dst_len - bytes_left;
402}
403
Chris Lattner24943d22010-06-08 16:52:24 +0000404Process*
405Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener)
406{
407 ProcessCreateInstance create_callback = NULL;
408 if (plugin_name)
409 {
410 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name);
411 if (create_callback)
412 {
413 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
414 if (debugger_ap->CanDebug(target))
415 return debugger_ap.release();
416 }
417 }
418 else
419 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000420 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000421 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000422 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
423 if (debugger_ap->CanDebug(target))
424 return debugger_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000425 }
426 }
427 return NULL;
428}
429
430
431//----------------------------------------------------------------------
432// Process constructor
433//----------------------------------------------------------------------
434Process::Process(Target &target, Listener &listener) :
435 UserID (LLDB_INVALID_PROCESS_ID),
Greg Clayton49ce6822010-10-31 03:01:06 +0000436 Broadcaster ("lldb.process"),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000437 ProcessInstanceSettings (*GetSettingsController()),
Chris Lattner24943d22010-06-08 16:52:24 +0000438 m_target (target),
Chris Lattner24943d22010-06-08 16:52:24 +0000439 m_public_state (eStateUnloaded),
440 m_private_state (eStateUnloaded),
441 m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"),
442 m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"),
443 m_private_state_listener ("lldb.process.internal_state_listener"),
444 m_private_state_control_wait(),
445 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
446 m_stop_id (0),
447 m_thread_index_id (0),
448 m_exit_status (-1),
449 m_exit_string (),
450 m_thread_list (this),
451 m_notifications (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000452 m_image_tokens (),
453 m_listener (listener),
454 m_breakpoint_site_list (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000455 m_dynamic_checkers_ap (),
Caroline Tice861efb32010-11-16 05:07:41 +0000456 m_unix_signals (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000457 m_abi_sp (),
Caroline Tice861efb32010-11-16 05:07:41 +0000458 m_process_input_reader (),
Greg Claytona875b642011-01-09 21:07:35 +0000459 m_stdio_communication ("process.stdio"),
Greg Clayton20d338f2010-11-18 05:57:03 +0000460 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonfd119992011-01-07 06:08:19 +0000461 m_stdout_data (),
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000462 m_memory_cache (),
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000463 m_next_event_action_ap()
Chris Lattner24943d22010-06-08 16:52:24 +0000464{
Caroline Tice1ebef442010-09-27 00:30:10 +0000465 UpdateInstanceName();
466
Greg Claytone005f2c2010-11-06 01:53:30 +0000467 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000468 if (log)
469 log->Printf ("%p Process::Process()", this);
470
Greg Clayton49ce6822010-10-31 03:01:06 +0000471 SetEventName (eBroadcastBitStateChanged, "state-changed");
472 SetEventName (eBroadcastBitInterrupt, "interrupt");
473 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
474 SetEventName (eBroadcastBitSTDERR, "stderr-available");
475
Chris Lattner24943d22010-06-08 16:52:24 +0000476 listener.StartListeningForEvents (this,
477 eBroadcastBitStateChanged |
478 eBroadcastBitInterrupt |
479 eBroadcastBitSTDOUT |
480 eBroadcastBitSTDERR);
481
482 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
483 eBroadcastBitStateChanged);
484
485 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
486 eBroadcastInternalStateControlStop |
487 eBroadcastInternalStateControlPause |
488 eBroadcastInternalStateControlResume);
489}
490
491//----------------------------------------------------------------------
492// Destructor
493//----------------------------------------------------------------------
494Process::~Process()
495{
Greg Claytone005f2c2010-11-06 01:53:30 +0000496 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000497 if (log)
498 log->Printf ("%p Process::~Process()", this);
499 StopPrivateStateThread();
500}
501
502void
503Process::Finalize()
504{
505 // Do any cleanup needed prior to being destructed... Subclasses
506 // that override this method should call this superclass method as well.
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000507
508 // We need to destroy the loader before the derived Process class gets destroyed
509 // since it is very likely that undoing the loader will require access to the real process.
510 if (m_dyld_ap.get() != NULL)
511 m_dyld_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000512}
513
514void
515Process::RegisterNotificationCallbacks (const Notifications& callbacks)
516{
517 m_notifications.push_back(callbacks);
518 if (callbacks.initialize != NULL)
519 callbacks.initialize (callbacks.baton, this);
520}
521
522bool
523Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
524{
525 std::vector<Notifications>::iterator pos, end = m_notifications.end();
526 for (pos = m_notifications.begin(); pos != end; ++pos)
527 {
528 if (pos->baton == callbacks.baton &&
529 pos->initialize == callbacks.initialize &&
530 pos->process_state_changed == callbacks.process_state_changed)
531 {
532 m_notifications.erase(pos);
533 return true;
534 }
535 }
536 return false;
537}
538
539void
540Process::SynchronouslyNotifyStateChanged (StateType state)
541{
542 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
543 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
544 {
545 if (notification_pos->process_state_changed)
546 notification_pos->process_state_changed (notification_pos->baton, this, state);
547 }
548}
549
550// FIXME: We need to do some work on events before the general Listener sees them.
551// For instance if we are continuing from a breakpoint, we need to ensure that we do
552// the little "insert real insn, step & stop" trick. But we can't do that when the
553// event is delivered by the broadcaster - since that is done on the thread that is
554// waiting for new events, so if we needed more than one event for our handling, we would
555// stall. So instead we do it when we fetch the event off of the queue.
556//
557
558StateType
559Process::GetNextEvent (EventSP &event_sp)
560{
561 StateType state = eStateInvalid;
562
563 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
564 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
565
566 return state;
567}
568
569
570StateType
571Process::WaitForProcessToStop (const TimeValue *timeout)
572{
573 StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded };
574 return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType));
575}
576
577
578StateType
579Process::WaitForState
580(
581 const TimeValue *timeout,
582 const StateType *match_states, const uint32_t num_match_states
583)
584{
585 EventSP event_sp;
586 uint32_t i;
Greg Claytond8c62532010-10-07 04:19:01 +0000587 StateType state = GetState();
Chris Lattner24943d22010-06-08 16:52:24 +0000588 while (state != eStateInvalid)
589 {
Greg Claytond8c62532010-10-07 04:19:01 +0000590 // If we are exited or detached, we won't ever get back to any
591 // other valid state...
592 if (state == eStateDetached || state == eStateExited)
593 return state;
594
Chris Lattner24943d22010-06-08 16:52:24 +0000595 state = WaitForStateChangedEvents (timeout, event_sp);
596
597 for (i=0; i<num_match_states; ++i)
598 {
599 if (match_states[i] == state)
600 return state;
601 }
602 }
603 return state;
604}
605
Jim Ingham63e24d72010-10-11 23:53:14 +0000606bool
607Process::HijackProcessEvents (Listener *listener)
608{
609 if (listener != NULL)
610 {
611 return HijackBroadcaster(listener, eBroadcastBitStateChanged);
612 }
613 else
614 return false;
615}
616
617void
618Process::RestoreProcessEvents ()
619{
620 RestoreBroadcaster();
621}
622
Jim Inghamf9f40c22011-02-08 05:20:59 +0000623bool
624Process::HijackPrivateProcessEvents (Listener *listener)
625{
626 if (listener != NULL)
627 {
628 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged);
629 }
630 else
631 return false;
632}
633
634void
635Process::RestorePrivateProcessEvents ()
636{
637 m_private_state_broadcaster.RestoreBroadcaster();
638}
639
Chris Lattner24943d22010-06-08 16:52:24 +0000640StateType
641Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
642{
Greg Claytone005f2c2010-11-06 01:53:30 +0000643 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000644
645 if (log)
646 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
647
648 StateType state = eStateInvalid;
Greg Clayton36f63a92010-10-19 03:25:40 +0000649 if (m_listener.WaitForEventForBroadcasterWithType (timeout,
650 this,
651 eBroadcastBitStateChanged,
652 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +0000653 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
654
655 if (log)
656 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
657 __FUNCTION__,
658 timeout,
659 StateAsCString(state));
660 return state;
661}
662
663Event *
664Process::PeekAtStateChangedEvents ()
665{
Greg Claytone005f2c2010-11-06 01:53:30 +0000666 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000667
668 if (log)
669 log->Printf ("Process::%s...", __FUNCTION__);
670
671 Event *event_ptr;
Greg Clayton36f63a92010-10-19 03:25:40 +0000672 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
673 eBroadcastBitStateChanged);
Chris Lattner24943d22010-06-08 16:52:24 +0000674 if (log)
675 {
676 if (event_ptr)
677 {
678 log->Printf ("Process::%s (event_ptr) => %s",
679 __FUNCTION__,
680 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
681 }
682 else
683 {
684 log->Printf ("Process::%s no events found",
685 __FUNCTION__);
686 }
687 }
688 return event_ptr;
689}
690
691StateType
692Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
693{
Greg Claytone005f2c2010-11-06 01:53:30 +0000694 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000695
696 if (log)
697 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
698
699 StateType state = eStateInvalid;
Greg Clayton72e1c782011-01-22 23:43:18 +0000700 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
701 &m_private_state_broadcaster,
702 eBroadcastBitStateChanged,
703 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +0000704 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
705
706 // This is a bit of a hack, but when we wait here we could very well return
707 // to the command-line, and that could disable the log, which would render the
708 // log we got above invalid.
Chris Lattner24943d22010-06-08 16:52:24 +0000709 if (log)
Greg Clayton72e1c782011-01-22 23:43:18 +0000710 {
711 if (state == eStateInvalid)
712 log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout);
713 else
714 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
715 }
Chris Lattner24943d22010-06-08 16:52:24 +0000716 return state;
717}
718
719bool
720Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
721{
Greg Claytone005f2c2010-11-06 01:53:30 +0000722 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000723
724 if (log)
725 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
726
727 if (control_only)
728 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
729 else
730 return m_private_state_listener.WaitForEvent(timeout, event_sp);
731}
732
733bool
734Process::IsRunning () const
735{
736 return StateIsRunningState (m_public_state.GetValue());
737}
738
739int
740Process::GetExitStatus ()
741{
742 if (m_public_state.GetValue() == eStateExited)
743 return m_exit_status;
744 return -1;
745}
746
Greg Clayton638351a2010-12-04 00:10:17 +0000747
748void
749Process::ProcessInstanceSettings::GetHostEnvironmentIfNeeded ()
750{
751 if (m_inherit_host_env && !m_got_host_env)
752 {
753 m_got_host_env = true;
754 StringList host_env;
755 const size_t host_env_count = Host::GetEnvironment (host_env);
756 for (size_t idx=0; idx<host_env_count; idx++)
757 {
758 const char *env_entry = host_env.GetStringAtIndex (idx);
759 if (env_entry)
760 {
Greg Clayton1f3dd642010-12-15 20:52:40 +0000761 const char *equal_pos = ::strchr(env_entry, '=');
Greg Clayton638351a2010-12-04 00:10:17 +0000762 if (equal_pos)
763 {
764 std::string key (env_entry, equal_pos - env_entry);
765 std::string value (equal_pos + 1);
766 if (m_env_vars.find (key) == m_env_vars.end())
767 m_env_vars[key] = value;
768 }
769 }
770 }
771 }
772}
773
774
775size_t
776Process::ProcessInstanceSettings::GetEnvironmentAsArgs (Args &env)
777{
778 GetHostEnvironmentIfNeeded ();
779
780 dictionary::const_iterator pos, end = m_env_vars.end();
781 for (pos = m_env_vars.begin(); pos != end; ++pos)
782 {
783 std::string env_var_equal_value (pos->first);
784 env_var_equal_value.append(1, '=');
785 env_var_equal_value.append (pos->second);
786 env.AppendArgument (env_var_equal_value.c_str());
787 }
788 return env.GetArgumentCount();
789}
790
791
Chris Lattner24943d22010-06-08 16:52:24 +0000792const char *
793Process::GetExitDescription ()
794{
795 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
796 return m_exit_string.c_str();
797 return NULL;
798}
799
Greg Clayton72e1c782011-01-22 23:43:18 +0000800bool
Chris Lattner24943d22010-06-08 16:52:24 +0000801Process::SetExitStatus (int status, const char *cstr)
802{
Greg Clayton68ca8232011-01-25 02:58:48 +0000803 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
804 if (log)
805 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
806 status, status,
807 cstr ? "\"" : "",
808 cstr ? cstr : "NULL",
809 cstr ? "\"" : "");
810
Greg Clayton72e1c782011-01-22 23:43:18 +0000811 // We were already in the exited state
812 if (m_private_state.GetValue() == eStateExited)
Greg Clayton68ca8232011-01-25 02:58:48 +0000813 {
Greg Clayton644ddfb2011-01-26 23:47:29 +0000814 if (log)
815 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton72e1c782011-01-22 23:43:18 +0000816 return false;
Greg Clayton68ca8232011-01-25 02:58:48 +0000817 }
Greg Clayton72e1c782011-01-22 23:43:18 +0000818
819 m_exit_status = status;
820 if (cstr)
821 m_exit_string = cstr;
822 else
823 m_exit_string.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000824
Greg Clayton72e1c782011-01-22 23:43:18 +0000825 DidExit ();
Greg Clayton58e844b2010-12-08 05:08:21 +0000826
Greg Clayton72e1c782011-01-22 23:43:18 +0000827 SetPrivateState (eStateExited);
828 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000829}
830
831// This static callback can be used to watch for local child processes on
832// the current host. The the child process exits, the process will be
833// found in the global target list (we want to be completely sure that the
834// lldb_private::Process doesn't go away before we can deliver the signal.
835bool
836Process::SetProcessExitStatus
837(
838 void *callback_baton,
839 lldb::pid_t pid,
840 int signo, // Zero for no signal
841 int exit_status // Exit value of process if signal is zero
842)
843{
844 if (signo == 0 || exit_status)
845 {
Greg Clayton63094e02010-06-23 01:19:29 +0000846 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner24943d22010-06-08 16:52:24 +0000847 if (target_sp)
848 {
849 ProcessSP process_sp (target_sp->GetProcessSP());
850 if (process_sp)
851 {
852 const char *signal_cstr = NULL;
853 if (signo)
854 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
855
856 process_sp->SetExitStatus (exit_status, signal_cstr);
857 }
858 }
859 return true;
860 }
861 return false;
862}
863
864
865uint32_t
866Process::GetNextThreadIndexID ()
867{
868 return ++m_thread_index_id;
869}
870
871StateType
872Process::GetState()
873{
874 // If any other threads access this we will need a mutex for it
875 return m_public_state.GetValue ();
876}
877
878void
879Process::SetPublicState (StateType new_state)
880{
Greg Clayton68ca8232011-01-25 02:58:48 +0000881 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000882 if (log)
883 log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state));
884 m_public_state.SetValue (new_state);
885}
886
887StateType
888Process::GetPrivateState ()
889{
890 return m_private_state.GetValue();
891}
892
893void
894Process::SetPrivateState (StateType new_state)
895{
Greg Clayton68ca8232011-01-25 02:58:48 +0000896 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000897 bool state_changed = false;
898
899 if (log)
900 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
901
902 Mutex::Locker locker(m_private_state.GetMutex());
903
904 const StateType old_state = m_private_state.GetValueNoLock ();
905 state_changed = old_state != new_state;
906 if (state_changed)
907 {
908 m_private_state.SetValueNoLock (new_state);
909 if (StateIsStoppedState(new_state))
910 {
911 m_stop_id++;
Greg Claytonfd119992011-01-07 06:08:19 +0000912 m_memory_cache.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000913 if (log)
914 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id);
915 }
916 // Use our target to get a shared pointer to ourselves...
917 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state));
918 }
919 else
920 {
921 if (log)
922 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state));
923 }
924}
925
926
927uint32_t
928Process::GetStopID() const
929{
930 return m_stop_id;
931}
932
933addr_t
934Process::GetImageInfoAddress()
935{
936 return LLDB_INVALID_ADDRESS;
937}
938
Greg Clayton0baa3942010-11-04 01:54:29 +0000939//----------------------------------------------------------------------
940// LoadImage
941//
942// This function provides a default implementation that works for most
943// unix variants. Any Process subclasses that need to do shared library
944// loading differently should override LoadImage and UnloadImage and
945// do what is needed.
946//----------------------------------------------------------------------
947uint32_t
948Process::LoadImage (const FileSpec &image_spec, Error &error)
949{
950 DynamicLoader *loader = GetDynamicLoader();
951 if (loader)
952 {
953 error = loader->CanLoadImage();
954 if (error.Fail())
955 return LLDB_INVALID_IMAGE_TOKEN;
956 }
957
958 if (error.Success())
959 {
960 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
961 if (thread_sp == NULL)
962 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
963
964 if (thread_sp)
965 {
966 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
967
968 if (frame_sp)
969 {
970 ExecutionContext exe_ctx;
971 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +0000972 bool unwind_on_error = true;
Sean Callanan6a925532011-01-13 08:53:35 +0000973 bool keep_in_memory = false;
Greg Clayton0baa3942010-11-04 01:54:29 +0000974 StreamString expr;
975 char path[PATH_MAX];
976 image_spec.GetPath(path, sizeof(path));
977 expr.Printf("dlopen (\"%s\", 2)", path);
978 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Ingham360f53f2010-11-30 02:22:11 +0000979 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan6a925532011-01-13 08:53:35 +0000980 ClangUserExpression::Evaluate (exe_ctx, keep_in_memory, unwind_on_error, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton0baa3942010-11-04 01:54:29 +0000981 if (result_valobj_sp->GetError().Success())
982 {
983 Scalar scalar;
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000984 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton0baa3942010-11-04 01:54:29 +0000985 {
986 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
987 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
988 {
989 uint32_t image_token = m_image_tokens.size();
990 m_image_tokens.push_back (image_ptr);
991 return image_token;
992 }
993 }
994 }
995 }
996 }
997 }
998 return LLDB_INVALID_IMAGE_TOKEN;
999}
1000
1001//----------------------------------------------------------------------
1002// UnloadImage
1003//
1004// This function provides a default implementation that works for most
1005// unix variants. Any Process subclasses that need to do shared library
1006// loading differently should override LoadImage and UnloadImage and
1007// do what is needed.
1008//----------------------------------------------------------------------
1009Error
1010Process::UnloadImage (uint32_t image_token)
1011{
1012 Error error;
1013 if (image_token < m_image_tokens.size())
1014 {
1015 const addr_t image_addr = m_image_tokens[image_token];
1016 if (image_addr == LLDB_INVALID_ADDRESS)
1017 {
1018 error.SetErrorString("image already unloaded");
1019 }
1020 else
1021 {
1022 DynamicLoader *loader = GetDynamicLoader();
1023 if (loader)
1024 error = loader->CanLoadImage();
1025
1026 if (error.Success())
1027 {
1028 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
1029 if (thread_sp == NULL)
1030 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
1031
1032 if (thread_sp)
1033 {
1034 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
1035
1036 if (frame_sp)
1037 {
1038 ExecutionContext exe_ctx;
1039 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +00001040 bool unwind_on_error = true;
Sean Callanan6a925532011-01-13 08:53:35 +00001041 bool keep_in_memory = false;
Greg Clayton0baa3942010-11-04 01:54:29 +00001042 StreamString expr;
1043 expr.Printf("dlclose ((void *)0x%llx)", image_addr);
1044 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Ingham360f53f2010-11-30 02:22:11 +00001045 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan6a925532011-01-13 08:53:35 +00001046 ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, keep_in_memory, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton0baa3942010-11-04 01:54:29 +00001047 if (result_valobj_sp->GetError().Success())
1048 {
1049 Scalar scalar;
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001050 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton0baa3942010-11-04 01:54:29 +00001051 {
1052 if (scalar.UInt(1))
1053 {
1054 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1055 }
1056 else
1057 {
1058 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1059 }
1060 }
1061 }
1062 else
1063 {
1064 error = result_valobj_sp->GetError();
1065 }
1066 }
1067 }
1068 }
1069 }
1070 }
1071 else
1072 {
1073 error.SetErrorString("invalid image token");
1074 }
1075 return error;
1076}
1077
Chris Lattner24943d22010-06-08 16:52:24 +00001078const ABI *
1079Process::GetABI()
1080{
Chris Lattner24943d22010-06-08 16:52:24 +00001081 if (m_abi_sp.get() == NULL)
Greg Clayton395fc332011-02-15 21:59:32 +00001082 m_abi_sp.reset(ABI::FindPlugin(m_target.GetArchitecture()));
Chris Lattner24943d22010-06-08 16:52:24 +00001083
1084 return m_abi_sp.get();
1085}
1086
Jim Ingham642036f2010-09-23 02:01:19 +00001087LanguageRuntime *
1088Process::GetLanguageRuntime(lldb::LanguageType language)
1089{
1090 LanguageRuntimeCollection::iterator pos;
1091 pos = m_language_runtimes.find (language);
1092 if (pos == m_language_runtimes.end())
1093 {
1094 lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language));
1095
1096 m_language_runtimes[language]
1097 = runtime;
1098 return runtime.get();
1099 }
1100 else
1101 return (*pos).second.get();
1102}
1103
1104CPPLanguageRuntime *
1105Process::GetCPPLanguageRuntime ()
1106{
1107 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus);
1108 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
1109 return static_cast<CPPLanguageRuntime *> (runtime);
1110 return NULL;
1111}
1112
1113ObjCLanguageRuntime *
1114Process::GetObjCLanguageRuntime ()
1115{
1116 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC);
1117 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
1118 return static_cast<ObjCLanguageRuntime *> (runtime);
1119 return NULL;
1120}
1121
Chris Lattner24943d22010-06-08 16:52:24 +00001122BreakpointSiteList &
1123Process::GetBreakpointSiteList()
1124{
1125 return m_breakpoint_site_list;
1126}
1127
1128const BreakpointSiteList &
1129Process::GetBreakpointSiteList() const
1130{
1131 return m_breakpoint_site_list;
1132}
1133
1134
1135void
1136Process::DisableAllBreakpointSites ()
1137{
1138 m_breakpoint_site_list.SetEnabledForAll (false);
1139}
1140
1141Error
1142Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
1143{
1144 Error error (DisableBreakpointSiteByID (break_id));
1145
1146 if (error.Success())
1147 m_breakpoint_site_list.Remove(break_id);
1148
1149 return error;
1150}
1151
1152Error
1153Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
1154{
1155 Error error;
1156 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1157 if (bp_site_sp)
1158 {
1159 if (bp_site_sp->IsEnabled())
1160 error = DisableBreakpoint (bp_site_sp.get());
1161 }
1162 else
1163 {
1164 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
1165 }
1166
1167 return error;
1168}
1169
1170Error
1171Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
1172{
1173 Error error;
1174 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1175 if (bp_site_sp)
1176 {
1177 if (!bp_site_sp->IsEnabled())
1178 error = EnableBreakpoint (bp_site_sp.get());
1179 }
1180 else
1181 {
1182 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
1183 }
1184 return error;
1185}
1186
Stephen Wilson3fd1f362010-07-17 00:56:13 +00001187lldb::break_id_t
Chris Lattner24943d22010-06-08 16:52:24 +00001188Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware)
1189{
Greg Claytoneea26402010-09-14 23:36:40 +00001190 const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +00001191 if (load_addr != LLDB_INVALID_ADDRESS)
1192 {
1193 BreakpointSiteSP bp_site_sp;
1194
1195 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
1196 // create a new breakpoint site and add it.
1197
1198 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
1199
1200 if (bp_site_sp)
1201 {
1202 bp_site_sp->AddOwner (owner);
1203 owner->SetBreakpointSite (bp_site_sp);
1204 return bp_site_sp->GetID();
1205 }
1206 else
1207 {
1208 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware));
1209 if (bp_site_sp)
1210 {
1211 if (EnableBreakpoint (bp_site_sp.get()).Success())
1212 {
1213 owner->SetBreakpointSite (bp_site_sp);
1214 return m_breakpoint_site_list.Add (bp_site_sp);
1215 }
1216 }
1217 }
1218 }
1219 // We failed to enable the breakpoint
1220 return LLDB_INVALID_BREAK_ID;
1221
1222}
1223
1224void
1225Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
1226{
1227 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
1228 if (num_owners == 0)
1229 {
1230 DisableBreakpoint(bp_site_sp.get());
1231 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1232 }
1233}
1234
1235
1236size_t
1237Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1238{
1239 size_t bytes_removed = 0;
1240 addr_t intersect_addr;
1241 size_t intersect_size;
1242 size_t opcode_offset;
1243 size_t idx;
1244 BreakpointSiteSP bp;
1245
1246 for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx)
1247 {
1248 if (bp->GetType() == BreakpointSite::eSoftware)
1249 {
1250 if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
1251 {
1252 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1253 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
1254 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1255 size_t buf_offset = intersect_addr - bp_addr;
1256 ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
1257 }
1258 }
1259 }
1260 return bytes_removed;
1261}
1262
1263
Greg Claytonb1888f22011-03-19 01:12:21 +00001264
1265size_t
1266Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
1267{
1268 PlatformSP platform_sp (m_target.GetPlatform());
1269 if (platform_sp)
1270 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
1271 return 0;
1272}
1273
Chris Lattner24943d22010-06-08 16:52:24 +00001274Error
1275Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
1276{
1277 Error error;
1278 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +00001279 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001280 const addr_t bp_addr = bp_site->GetLoadAddress();
1281 if (log)
1282 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr);
1283 if (bp_site->IsEnabled())
1284 {
1285 if (log)
1286 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
1287 return error;
1288 }
1289
1290 if (bp_addr == LLDB_INVALID_ADDRESS)
1291 {
1292 error.SetErrorString("BreakpointSite contains an invalid load address.");
1293 return error;
1294 }
1295 // Ask the lldb::Process subclass to fill in the correct software breakpoint
1296 // trap for the breakpoint site
1297 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
1298
1299 if (bp_opcode_size == 0)
1300 {
1301 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr);
1302 }
1303 else
1304 {
1305 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
1306
1307 if (bp_opcode_bytes == NULL)
1308 {
1309 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
1310 return error;
1311 }
1312
1313 // Save the original opcode by reading it
1314 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
1315 {
1316 // Write a software breakpoint in place of the original opcode
1317 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1318 {
1319 uint8_t verify_bp_opcode_bytes[64];
1320 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1321 {
1322 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
1323 {
1324 bp_site->SetEnabled(true);
1325 bp_site->SetType (BreakpointSite::eSoftware);
1326 if (log)
1327 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS",
1328 bp_site->GetID(),
1329 (uint64_t)bp_addr);
1330 }
1331 else
1332 error.SetErrorString("Failed to verify the breakpoint trap in memory.");
1333 }
1334 else
1335 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
1336 }
1337 else
1338 error.SetErrorString("Unable to write breakpoint trap to memory.");
1339 }
1340 else
1341 error.SetErrorString("Unable to read memory at breakpoint address.");
1342 }
Stephen Wilsonc2b98252011-01-12 04:20:03 +00001343 if (log && error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +00001344 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1345 bp_site->GetID(),
1346 (uint64_t)bp_addr,
1347 error.AsCString());
1348 return error;
1349}
1350
1351Error
1352Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
1353{
1354 Error error;
1355 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +00001356 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001357 addr_t bp_addr = bp_site->GetLoadAddress();
1358 lldb::user_id_t breakID = bp_site->GetID();
1359 if (log)
Stephen Wilson9ff73ed2011-01-14 21:07:07 +00001360 log->Printf ("Process::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001361
1362 if (bp_site->IsHardware())
1363 {
1364 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
1365 }
1366 else if (bp_site->IsEnabled())
1367 {
1368 const size_t break_op_size = bp_site->GetByteSize();
1369 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
1370 if (break_op_size > 0)
1371 {
1372 // Clear a software breakoint instruction
Greg Clayton54e7afa2010-07-09 20:39:50 +00001373 uint8_t curr_break_op[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +00001374 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner24943d22010-06-08 16:52:24 +00001375 bool break_op_found = false;
1376
1377 // Read the breakpoint opcode
1378 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
1379 {
1380 bool verify = false;
1381 // Make sure we have the a breakpoint opcode exists at this address
1382 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
1383 {
1384 break_op_found = true;
1385 // We found a valid breakpoint opcode at this address, now restore
1386 // the saved opcode.
1387 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
1388 {
1389 verify = true;
1390 }
1391 else
1392 error.SetErrorString("Memory write failed when restoring original opcode.");
1393 }
1394 else
1395 {
1396 error.SetErrorString("Original breakpoint trap is no longer in memory.");
1397 // Set verify to true and so we can check if the original opcode has already been restored
1398 verify = true;
1399 }
1400
1401 if (verify)
1402 {
Greg Clayton54e7afa2010-07-09 20:39:50 +00001403 uint8_t verify_opcode[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +00001404 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner24943d22010-06-08 16:52:24 +00001405 // Verify that our original opcode made it back to the inferior
1406 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
1407 {
1408 // compare the memory we just read with the original opcode
1409 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
1410 {
1411 // SUCCESS
1412 bp_site->SetEnabled(false);
1413 if (log)
1414 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
1415 return error;
1416 }
1417 else
1418 {
1419 if (break_op_found)
1420 error.SetErrorString("Failed to restore original opcode.");
1421 }
1422 }
1423 else
1424 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
1425 }
1426 }
1427 else
1428 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
1429 }
1430 }
1431 else
1432 {
1433 if (log)
1434 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
1435 return error;
1436 }
1437
1438 if (log)
1439 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1440 bp_site->GetID(),
1441 (uint64_t)bp_addr,
1442 error.AsCString());
1443 return error;
1444
1445}
1446
Greg Claytonfd119992011-01-07 06:08:19 +00001447// Comment out line below to disable memory caching
1448#define ENABLE_MEMORY_CACHING
1449// Uncomment to verify memory caching works after making changes to caching code
1450//#define VERIFY_MEMORY_READS
1451
1452#if defined (ENABLE_MEMORY_CACHING)
1453
1454#if defined (VERIFY_MEMORY_READS)
Chris Lattner24943d22010-06-08 16:52:24 +00001455
1456size_t
1457Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1458{
Greg Claytonfd119992011-01-07 06:08:19 +00001459 // Memory caching is enabled, with debug verification
1460 if (buf && size)
1461 {
1462 // Uncomment the line below to make sure memory caching is working.
1463 // I ran this through the test suite and got no assertions, so I am
1464 // pretty confident this is working well. If any changes are made to
1465 // memory caching, uncomment the line below and test your changes!
1466
1467 // Verify all memory reads by using the cache first, then redundantly
1468 // reading the same memory from the inferior and comparing to make sure
1469 // everything is exactly the same.
1470 std::string verify_buf (size, '\0');
1471 assert (verify_buf.size() == size);
1472 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
1473 Error verify_error;
1474 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
1475 assert (cache_bytes_read == verify_bytes_read);
1476 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
1477 assert (verify_error.Success() == error.Success());
1478 return cache_bytes_read;
1479 }
1480 return 0;
1481}
1482
1483#else // #if defined (VERIFY_MEMORY_READS)
1484
1485size_t
1486Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1487{
1488 // Memory caching enabled, no verification
1489 return m_memory_cache.Read (this, addr, buf, size, error);
1490}
1491
1492#endif // #else for #if defined (VERIFY_MEMORY_READS)
1493
1494#else // #if defined (ENABLE_MEMORY_CACHING)
1495
1496size_t
1497Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1498{
1499 // Memory caching is disabled
1500 return ReadMemoryFromInferior (addr, buf, size, error);
1501}
1502
1503#endif // #else for #if defined (ENABLE_MEMORY_CACHING)
1504
1505
1506size_t
1507Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
1508{
Chris Lattner24943d22010-06-08 16:52:24 +00001509 if (buf == NULL || size == 0)
1510 return 0;
1511
1512 size_t bytes_read = 0;
1513 uint8_t *bytes = (uint8_t *)buf;
1514
1515 while (bytes_read < size)
1516 {
1517 const size_t curr_size = size - bytes_read;
1518 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
1519 bytes + bytes_read,
1520 curr_size,
1521 error);
1522 bytes_read += curr_bytes_read;
1523 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
1524 break;
1525 }
1526
1527 // Replace any software breakpoint opcodes that fall into this range back
1528 // into "buf" before we return
1529 if (bytes_read > 0)
1530 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
1531 return bytes_read;
1532}
1533
Greg Claytonf72fdee2010-12-16 20:01:20 +00001534uint64_t
1535Process::ReadUnsignedInteger (lldb::addr_t vm_addr, size_t integer_byte_size, Error &error)
1536{
1537 if (integer_byte_size > sizeof(uint64_t))
1538 {
1539 error.SetErrorString ("unsupported integer size");
1540 }
1541 else
1542 {
1543 uint8_t tmp[sizeof(uint64_t)];
Greg Clayton395fc332011-02-15 21:59:32 +00001544 DataExtractor data (tmp,
1545 integer_byte_size,
1546 m_target.GetArchitecture().GetByteOrder(),
1547 m_target.GetArchitecture().GetAddressByteSize());
Greg Claytonf72fdee2010-12-16 20:01:20 +00001548 if (ReadMemory (vm_addr, tmp, integer_byte_size, error) == integer_byte_size)
1549 {
1550 uint32_t offset = 0;
1551 return data.GetMaxU64 (&offset, integer_byte_size);
1552 }
1553 }
1554 // Any plug-in that doesn't return success a memory read with the number
1555 // of bytes that were requested should be setting the error
1556 assert (error.Fail());
1557 return 0;
1558}
1559
Chris Lattner24943d22010-06-08 16:52:24 +00001560size_t
1561Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
1562{
1563 size_t bytes_written = 0;
1564 const uint8_t *bytes = (const uint8_t *)buf;
1565
1566 while (bytes_written < size)
1567 {
1568 const size_t curr_size = size - bytes_written;
1569 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
1570 bytes + bytes_written,
1571 curr_size,
1572 error);
1573 bytes_written += curr_bytes_written;
1574 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
1575 break;
1576 }
1577 return bytes_written;
1578}
1579
1580size_t
1581Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1582{
Greg Claytonfd119992011-01-07 06:08:19 +00001583#if defined (ENABLE_MEMORY_CACHING)
1584 m_memory_cache.Flush (addr, size);
1585#endif
1586
Chris Lattner24943d22010-06-08 16:52:24 +00001587 if (buf == NULL || size == 0)
1588 return 0;
1589 // We need to write any data that would go where any current software traps
1590 // (enabled software breakpoints) any software traps (breakpoints) that we
1591 // may have placed in our tasks memory.
1592
1593 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
1594 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
1595
1596 if (iter == end || iter->second->GetLoadAddress() > addr + size)
1597 return DoWriteMemory(addr, buf, size, error);
1598
1599 BreakpointSiteList::collection::const_iterator pos;
1600 size_t bytes_written = 0;
Greg Clayton54e7afa2010-07-09 20:39:50 +00001601 addr_t intersect_addr = 0;
1602 size_t intersect_size = 0;
1603 size_t opcode_offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001604 const uint8_t *ubuf = (const uint8_t *)buf;
1605
1606 for (pos = iter; pos != end; ++pos)
1607 {
1608 BreakpointSiteSP bp;
1609 bp = pos->second;
1610
1611 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
1612 assert(addr <= intersect_addr && intersect_addr < addr + size);
1613 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
1614 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1615
1616 // Check for bytes before this breakpoint
1617 const addr_t curr_addr = addr + bytes_written;
1618 if (intersect_addr > curr_addr)
1619 {
1620 // There are some bytes before this breakpoint that we need to
1621 // just write to memory
1622 size_t curr_size = intersect_addr - curr_addr;
1623 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
1624 ubuf + bytes_written,
1625 curr_size,
1626 error);
1627 bytes_written += curr_bytes_written;
1628 if (curr_bytes_written != curr_size)
1629 {
1630 // We weren't able to write all of the requested bytes, we
1631 // are done looping and will return the number of bytes that
1632 // we have written so far.
1633 break;
1634 }
1635 }
1636
1637 // Now write any bytes that would cover up any software breakpoints
1638 // directly into the breakpoint opcode buffer
1639 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
1640 bytes_written += intersect_size;
1641 }
1642
1643 // Write any remaining bytes after the last breakpoint if we have any left
1644 if (bytes_written < size)
1645 bytes_written += WriteMemoryPrivate (addr + bytes_written,
1646 ubuf + bytes_written,
1647 size - bytes_written,
1648 error);
1649
1650 return bytes_written;
1651}
1652
1653addr_t
1654Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
1655{
1656 // Fixme: we should track the blocks we've allocated, and clean them up...
1657 // We could even do our own allocator here if that ends up being more efficient.
Greg Clayton2860ba92011-01-23 19:58:49 +00001658 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
1659 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1660 if (log)
Greg Claytonb349adc2011-01-24 06:30:45 +00001661 log->Printf("Process::AllocateMemory(size=%4zu, permissions=%c%c%c) => 0x%16.16llx (m_stop_id = %u)",
Greg Clayton2860ba92011-01-23 19:58:49 +00001662 size,
1663 permissions & ePermissionsReadable ? 'r' : '-',
1664 permissions & ePermissionsWritable ? 'w' : '-',
1665 permissions & ePermissionsExecutable ? 'x' : '-',
1666 (uint64_t)allocated_addr,
1667 m_stop_id);
1668 return allocated_addr;
Chris Lattner24943d22010-06-08 16:52:24 +00001669}
1670
1671Error
1672Process::DeallocateMemory (addr_t ptr)
1673{
Greg Clayton2860ba92011-01-23 19:58:49 +00001674 Error error(DoDeallocateMemory (ptr));
1675
1676 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1677 if (log)
1678 log->Printf("Process::DeallocateMemory(addr=0x%16.16llx) => err = %s (m_stop_id = %u)",
1679 ptr,
1680 error.AsCString("SUCCESS"),
1681 m_stop_id);
1682 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00001683}
1684
1685
1686Error
1687Process::EnableWatchpoint (WatchpointLocation *watchpoint)
1688{
1689 Error error;
1690 error.SetErrorString("watchpoints are not supported");
1691 return error;
1692}
1693
1694Error
1695Process::DisableWatchpoint (WatchpointLocation *watchpoint)
1696{
1697 Error error;
1698 error.SetErrorString("watchpoints are not supported");
1699 return error;
1700}
1701
1702StateType
1703Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
1704{
1705 StateType state;
1706 // Now wait for the process to launch and return control to us, and then
1707 // call DidLaunch:
1708 while (1)
1709 {
Greg Clayton72e1c782011-01-22 23:43:18 +00001710 event_sp.reset();
1711 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
1712
1713 if (StateIsStoppedState(state))
Chris Lattner24943d22010-06-08 16:52:24 +00001714 break;
Greg Clayton72e1c782011-01-22 23:43:18 +00001715
1716 // If state is invalid, then we timed out
1717 if (state == eStateInvalid)
1718 break;
1719
1720 if (event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001721 HandlePrivateEvent (event_sp);
1722 }
1723 return state;
1724}
1725
1726Error
1727Process::Launch
1728(
1729 char const *argv[],
1730 char const *envp[],
Greg Clayton452bf612010-08-31 18:35:14 +00001731 uint32_t launch_flags,
Chris Lattner24943d22010-06-08 16:52:24 +00001732 const char *stdin_path,
1733 const char *stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +00001734 const char *stderr_path,
1735 const char *working_directory
Chris Lattner24943d22010-06-08 16:52:24 +00001736)
1737{
1738 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +00001739 m_abi_sp.reset();
Greg Clayton75c703d2011-02-16 04:46:07 +00001740 m_dyld_ap.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001741 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001742
1743 Module *exe_module = m_target.GetExecutableModule().get();
1744 if (exe_module)
1745 {
1746 char exec_file_path[PATH_MAX];
1747 exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path));
1748 if (exe_module->GetFileSpec().Exists())
1749 {
Greg Claytona2f74232011-02-24 22:24:29 +00001750 if (PrivateStateThreadIsValid ())
1751 PausePrivateStateThread ();
1752
Chris Lattner24943d22010-06-08 16:52:24 +00001753 error = WillLaunch (exe_module);
1754 if (error.Success())
1755 {
Greg Claytond8c62532010-10-07 04:19:01 +00001756 SetPublicState (eStateLaunching);
Chris Lattner24943d22010-06-08 16:52:24 +00001757 // The args coming in should not contain the application name, the
1758 // lldb_private::Process class will add this in case the executable
1759 // gets resolved to a different file than was given on the command
1760 // line (like when an applicaiton bundle is specified and will
1761 // resolve to the contained exectuable file, or the file given was
1762 // a symlink or other file system link that resolves to a different
1763 // file).
1764
1765 // Get the resolved exectuable path
1766
1767 // Make a new argument vector
1768 std::vector<const char *> exec_path_plus_argv;
1769 // Append the resolved executable path
1770 exec_path_plus_argv.push_back (exec_file_path);
1771
1772 // Push all args if there are any
1773 if (argv)
1774 {
1775 for (int i = 0; argv[i]; ++i)
1776 exec_path_plus_argv.push_back(argv[i]);
1777 }
1778
1779 // Push a NULL to terminate the args.
1780 exec_path_plus_argv.push_back(NULL);
1781
1782 // Now launch using these arguments.
Greg Clayton53d68e72010-07-20 22:52:08 +00001783 error = DoLaunch (exe_module,
1784 exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(),
1785 envp,
Greg Clayton452bf612010-08-31 18:35:14 +00001786 launch_flags,
Greg Clayton53d68e72010-07-20 22:52:08 +00001787 stdin_path,
1788 stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +00001789 stderr_path,
1790 working_directory);
Chris Lattner24943d22010-06-08 16:52:24 +00001791
1792 if (error.Fail())
1793 {
1794 if (GetID() != LLDB_INVALID_PROCESS_ID)
1795 {
1796 SetID (LLDB_INVALID_PROCESS_ID);
1797 const char *error_string = error.AsCString();
1798 if (error_string == NULL)
1799 error_string = "launch failed";
1800 SetExitStatus (-1, error_string);
1801 }
1802 }
1803 else
1804 {
1805 EventSP event_sp;
1806 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1807
1808 if (state == eStateStopped || state == eStateCrashed)
1809 {
Greg Clayton75c703d2011-02-16 04:46:07 +00001810
Chris Lattner24943d22010-06-08 16:52:24 +00001811 DidLaunch ();
1812
Greg Clayton4fdf7602011-03-20 04:57:14 +00001813 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Greg Clayton75c703d2011-02-16 04:46:07 +00001814 if (m_dyld_ap.get())
1815 m_dyld_ap->DidLaunch();
1816
Chris Lattner24943d22010-06-08 16:52:24 +00001817 // This delays passing the stopped event to listeners till DidLaunch gets
1818 // a chance to complete...
1819 HandlePrivateEvent (event_sp);
Greg Claytona2f74232011-02-24 22:24:29 +00001820
1821 if (PrivateStateThreadIsValid ())
1822 ResumePrivateStateThread ();
1823 else
1824 StartPrivateStateThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00001825 }
1826 else if (state == eStateExited)
1827 {
1828 // We exited while trying to launch somehow. Don't call DidLaunch as that's
1829 // not likely to work, and return an invalid pid.
1830 HandlePrivateEvent (event_sp);
1831 }
1832 }
1833 }
1834 }
1835 else
1836 {
1837 error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path);
1838 }
1839 }
1840 return error;
1841}
1842
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001843Process::NextEventAction::EventActionResult
1844Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001845{
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001846 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
1847 switch (state)
Greg Claytonc1d37752010-10-18 01:45:30 +00001848 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001849 case eStateRunning:
Greg Claytona2f74232011-02-24 22:24:29 +00001850 case eStateConnected:
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001851 return eEventActionRetry;
1852
1853 case eStateStopped:
1854 case eStateCrashed:
Jim Ingham7508e732010-08-09 23:31:02 +00001855 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001856 // During attach, prior to sending the eStateStopped event,
1857 // lldb_private::Process subclasses must set the process must set
1858 // the new process ID.
1859 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
Greg Clayton75c703d2011-02-16 04:46:07 +00001860 m_process->CompleteAttach ();
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001861 return eEventActionSuccess;
Jim Ingham7508e732010-08-09 23:31:02 +00001862 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001863
1864
1865 break;
1866 default:
1867 case eStateExited:
1868 case eStateInvalid:
1869 m_exit_string.assign ("No valid Process");
1870 return eEventActionExit;
1871 break;
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001872 }
1873}
Chris Lattner24943d22010-06-08 16:52:24 +00001874
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001875Process::NextEventAction::EventActionResult
1876Process::AttachCompletionHandler::HandleBeingInterrupted()
1877{
1878 return eEventActionSuccess;
1879}
1880
1881const char *
1882Process::AttachCompletionHandler::GetExitString ()
1883{
1884 return m_exit_string.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +00001885}
1886
1887Error
1888Process::Attach (lldb::pid_t attach_pid)
1889{
1890
Chris Lattner24943d22010-06-08 16:52:24 +00001891 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001892 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001893
Jim Ingham7508e732010-08-09 23:31:02 +00001894 // Find the process and its architecture. Make sure it matches the architecture
1895 // of the current Target, and if not adjust it.
1896
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001897 ProcessInfo process_info;
Greg Claytonb1888f22011-03-19 01:12:21 +00001898 PlatformSP platform_sp (m_target.GetDebugger().GetPlatformList().GetSelectedPlatform ());
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001899 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +00001900 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001901 if (platform_sp->GetProcessInfo (attach_pid, process_info))
1902 {
1903 const ArchSpec &process_arch = process_info.GetArchitecture();
1904 if (process_arch.IsValid())
1905 GetTarget().SetArchitecture(process_arch);
1906 }
Jim Ingham7508e732010-08-09 23:31:02 +00001907 }
1908
Greg Clayton75c703d2011-02-16 04:46:07 +00001909 m_dyld_ap.reset();
1910
Greg Clayton54e7afa2010-07-09 20:39:50 +00001911 Error error (WillAttachToProcessWithID(attach_pid));
Chris Lattner24943d22010-06-08 16:52:24 +00001912 if (error.Success())
1913 {
Greg Claytond8c62532010-10-07 04:19:01 +00001914 SetPublicState (eStateAttaching);
1915
Greg Clayton54e7afa2010-07-09 20:39:50 +00001916 error = DoAttachToProcessWithID (attach_pid);
Chris Lattner24943d22010-06-08 16:52:24 +00001917 if (error.Success())
1918 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001919 SetNextEventAction(new Process::AttachCompletionHandler(this));
1920 StartPrivateStateThread();
Chris Lattner24943d22010-06-08 16:52:24 +00001921 }
1922 else
1923 {
1924 if (GetID() != LLDB_INVALID_PROCESS_ID)
1925 {
1926 SetID (LLDB_INVALID_PROCESS_ID);
1927 const char *error_string = error.AsCString();
1928 if (error_string == NULL)
1929 error_string = "attach failed";
1930
1931 SetExitStatus(-1, error_string);
1932 }
1933 }
1934 }
1935 return error;
1936}
1937
1938Error
1939Process::Attach (const char *process_name, bool wait_for_launch)
1940{
Chris Lattner24943d22010-06-08 16:52:24 +00001941 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001942 m_process_input_reader.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001943
1944 // Find the process and its architecture. Make sure it matches the architecture
1945 // of the current Target, and if not adjust it.
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001946 Error error;
Jim Ingham7508e732010-08-09 23:31:02 +00001947
Jim Inghamea294182010-08-17 21:54:19 +00001948 if (!wait_for_launch)
Jim Ingham7508e732010-08-09 23:31:02 +00001949 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001950 ProcessInfoList process_infos;
Greg Claytonb1888f22011-03-19 01:12:21 +00001951 PlatformSP platform_sp (m_target.GetDebugger().GetPlatformList().GetSelectedPlatform ());
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001952 if (platform_sp)
Jim Inghamea294182010-08-17 21:54:19 +00001953 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001954 ProcessInfoMatch match_info;
1955 match_info.GetProcessInfo().SetName(process_name);
1956 match_info.SetNameMatchType (eNameMatchEquals);
1957 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001958 if (process_infos.GetSize() > 1)
Chris Lattner24943d22010-06-08 16:52:24 +00001959 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001960 error.SetErrorStringWithFormat ("More than one process named %s\n", process_name);
1961 }
1962 else if (process_infos.GetSize() == 0)
1963 {
1964 error.SetErrorStringWithFormat ("Could not find a process named %s\n", process_name);
1965 }
1966 else
1967 {
1968 ProcessInfo process_info;
1969 if (process_infos.GetInfoAtIndex (0, process_info))
1970 {
1971 const ArchSpec &process_arch = process_info.GetArchitecture();
1972 if (process_arch.IsValid() && process_arch != GetTarget().GetArchitecture())
1973 {
1974 // Set the architecture on the target.
1975 GetTarget().SetArchitecture (process_arch);
1976 }
1977 }
Chris Lattner24943d22010-06-08 16:52:24 +00001978 }
1979 }
1980 else
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001981 {
1982 error.SetErrorString ("Invalid platform");
1983 }
1984 }
1985
1986 if (error.Success())
1987 {
1988 m_dyld_ap.reset();
1989
1990 error = WillAttachToProcessWithName(process_name, wait_for_launch);
1991 if (error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +00001992 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001993 SetPublicState (eStateAttaching);
1994 error = DoAttachToProcessWithName (process_name, wait_for_launch);
1995 if (error.Fail())
1996 {
1997 if (GetID() != LLDB_INVALID_PROCESS_ID)
1998 {
1999 SetID (LLDB_INVALID_PROCESS_ID);
2000 const char *error_string = error.AsCString();
2001 if (error_string == NULL)
2002 error_string = "attach failed";
2003
2004 SetExitStatus(-1, error_string);
2005 }
2006 }
2007 else
2008 {
2009 SetNextEventAction(new Process::AttachCompletionHandler(this));
2010 StartPrivateStateThread();
2011 }
Chris Lattner24943d22010-06-08 16:52:24 +00002012 }
2013 }
2014 return error;
2015}
2016
Greg Clayton75c703d2011-02-16 04:46:07 +00002017void
2018Process::CompleteAttach ()
2019{
2020 // Let the process subclass figure out at much as it can about the process
2021 // before we go looking for a dynamic loader plug-in.
2022 DidAttach();
2023
2024 // We have complete the attach, now it is time to find the dynamic loader
2025 // plug-in
Greg Clayton4fdf7602011-03-20 04:57:14 +00002026 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Greg Clayton75c703d2011-02-16 04:46:07 +00002027 if (m_dyld_ap.get())
2028 m_dyld_ap->DidAttach();
2029
2030 // Figure out which one is the executable, and set that in our target:
2031 ModuleList &modules = m_target.GetImages();
2032
2033 size_t num_modules = modules.GetSize();
2034 for (int i = 0; i < num_modules; i++)
2035 {
2036 ModuleSP module_sp (modules.GetModuleAtIndex(i));
2037 if (module_sp->IsExecutable())
2038 {
2039 ModuleSP target_exe_module_sp (m_target.GetExecutableModule());
2040 if (target_exe_module_sp != module_sp)
2041 m_target.SetExecutableModule (module_sp, false);
2042 break;
2043 }
2044 }
2045}
2046
Chris Lattner24943d22010-06-08 16:52:24 +00002047Error
Greg Claytone71e2582011-02-04 01:58:07 +00002048Process::ConnectRemote (const char *remote_url)
2049{
Greg Claytone71e2582011-02-04 01:58:07 +00002050 m_abi_sp.reset();
2051 m_process_input_reader.reset();
2052
2053 // Find the process and its architecture. Make sure it matches the architecture
2054 // of the current Target, and if not adjust it.
2055
2056 Error error (DoConnectRemote (remote_url));
2057 if (error.Success())
2058 {
Greg Claytona2f74232011-02-24 22:24:29 +00002059 if (GetID() != LLDB_INVALID_PROCESS_ID)
2060 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00002061 EventSP event_sp;
2062 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
2063
2064 if (state == eStateStopped || state == eStateCrashed)
2065 {
2066 // If we attached and actually have a process on the other end, then
2067 // this ended up being the equivalent of an attach.
2068 CompleteAttach ();
2069
2070 // This delays passing the stopped event to listeners till
2071 // CompleteAttach gets a chance to complete...
2072 HandlePrivateEvent (event_sp);
2073
2074 }
Greg Claytona2f74232011-02-24 22:24:29 +00002075 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00002076
2077 if (PrivateStateThreadIsValid ())
2078 ResumePrivateStateThread ();
2079 else
2080 StartPrivateStateThread ();
Greg Claytone71e2582011-02-04 01:58:07 +00002081 }
2082 return error;
2083}
2084
2085
2086Error
Chris Lattner24943d22010-06-08 16:52:24 +00002087Process::Resume ()
2088{
Greg Claytone005f2c2010-11-06 01:53:30 +00002089 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002090 if (log)
Jim Inghamac959662011-01-24 06:34:17 +00002091 log->Printf("Process::Resume() m_stop_id = %u, public state: %s private state: %s",
2092 m_stop_id,
2093 StateAsCString(m_public_state.GetValue()),
2094 StateAsCString(m_private_state.GetValue()));
Chris Lattner24943d22010-06-08 16:52:24 +00002095
2096 Error error (WillResume());
2097 // Tell the process it is about to resume before the thread list
2098 if (error.Success())
2099 {
Johnny Chen9c11d472010-12-02 20:53:05 +00002100 // Now let the thread list know we are about to resume so it
Chris Lattner24943d22010-06-08 16:52:24 +00002101 // can let all of our threads know that they are about to be
2102 // resumed. Threads will each be called with
2103 // Thread::WillResume(StateType) where StateType contains the state
2104 // that they are supposed to have when the process is resumed
2105 // (suspended/running/stepping). Threads should also check
2106 // their resume signal in lldb::Thread::GetResumeSignal()
2107 // to see if they are suppoed to start back up with a signal.
2108 if (m_thread_list.WillResume())
2109 {
2110 error = DoResume();
2111 if (error.Success())
2112 {
2113 DidResume();
2114 m_thread_list.DidResume();
Jim Inghamac959662011-01-24 06:34:17 +00002115 if (log)
2116 log->Printf ("Process thinks the process has resumed.");
Chris Lattner24943d22010-06-08 16:52:24 +00002117 }
2118 }
2119 else
2120 {
Jim Inghamac959662011-01-24 06:34:17 +00002121 error.SetErrorStringWithFormat("Process::WillResume() thread list returned false after WillResume");
Chris Lattner24943d22010-06-08 16:52:24 +00002122 }
2123 }
Jim Inghamac959662011-01-24 06:34:17 +00002124 else if (log)
2125 log->Printf ("Process::WillResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner24943d22010-06-08 16:52:24 +00002126 return error;
2127}
2128
2129Error
2130Process::Halt ()
2131{
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002132 // Pause our private state thread so we can ensure no one else eats
2133 // the stop event out from under us.
Jim Inghamf9f40c22011-02-08 05:20:59 +00002134 Listener halt_listener ("lldb.process.halt_listener");
2135 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton20d338f2010-11-18 05:57:03 +00002136
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002137 EventSP event_sp;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002138 Error error (WillHalt());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002139
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002140 if (error.Success())
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002141 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002142
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002143 bool caused_stop = false;
2144
2145 // Ask the process subclass to actually halt our process
2146 error = DoHalt(caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +00002147 if (error.Success())
Jim Ingham3ae449a2010-11-17 02:32:00 +00002148 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002149 if (m_public_state.GetValue() == eStateAttaching)
2150 {
2151 SetExitStatus(SIGKILL, "Cancelled async attach.");
2152 Destroy ();
2153 }
2154 else
Jim Ingham3ae449a2010-11-17 02:32:00 +00002155 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002156 // If "caused_stop" is true, then DoHalt stopped the process. If
2157 // "caused_stop" is false, the process was already stopped.
2158 // If the DoHalt caused the process to stop, then we want to catch
2159 // this event and set the interrupted bool to true before we pass
2160 // this along so clients know that the process was interrupted by
2161 // a halt command.
2162 if (caused_stop)
Greg Clayton20d338f2010-11-18 05:57:03 +00002163 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00002164 // Wait for 1 second for the process to stop.
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002165 TimeValue timeout_time;
2166 timeout_time = TimeValue::Now();
2167 timeout_time.OffsetWithSeconds(1);
Jim Inghamf9f40c22011-02-08 05:20:59 +00002168 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
2169 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002170
Jim Inghamf9f40c22011-02-08 05:20:59 +00002171 if (!got_event || state == eStateInvalid)
Greg Clayton20d338f2010-11-18 05:57:03 +00002172 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002173 // We timeout out and didn't get a stop event...
Jim Inghamf9f40c22011-02-08 05:20:59 +00002174 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton20d338f2010-11-18 05:57:03 +00002175 }
2176 else
2177 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002178 if (StateIsStoppedState (state))
2179 {
2180 // We caused the process to interrupt itself, so mark this
2181 // as such in the stop event so clients can tell an interrupted
2182 // process from a natural stop
2183 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
2184 }
2185 else
2186 {
2187 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2188 if (log)
2189 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
2190 error.SetErrorString ("Did not get stopped event after halt.");
2191 }
Greg Clayton20d338f2010-11-18 05:57:03 +00002192 }
2193 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002194 DidHalt();
Jim Ingham3ae449a2010-11-17 02:32:00 +00002195 }
2196 }
Chris Lattner24943d22010-06-08 16:52:24 +00002197 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002198 // Resume our private state thread before we post the event (if any)
Jim Inghamf9f40c22011-02-08 05:20:59 +00002199 RestorePrivateProcessEvents();
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002200
2201 // Post any event we might have consumed. If all goes well, we will have
2202 // stopped the process, intercepted the event and set the interrupted
2203 // bool in the event. Post it to the private event queue and that will end up
2204 // correctly setting the state.
2205 if (event_sp)
2206 m_private_state_broadcaster.BroadcastEvent(event_sp);
2207
Chris Lattner24943d22010-06-08 16:52:24 +00002208 return error;
2209}
2210
2211Error
2212Process::Detach ()
2213{
2214 Error error (WillDetach());
2215
2216 if (error.Success())
2217 {
2218 DisableAllBreakpointSites();
2219 error = DoDetach();
2220 if (error.Success())
2221 {
2222 DidDetach();
2223 StopPrivateStateThread();
2224 }
2225 }
2226 return error;
2227}
2228
2229Error
2230Process::Destroy ()
2231{
2232 Error error (WillDestroy());
2233 if (error.Success())
2234 {
2235 DisableAllBreakpointSites();
2236 error = DoDestroy();
2237 if (error.Success())
2238 {
2239 DidDestroy();
2240 StopPrivateStateThread();
2241 }
Caroline Tice861efb32010-11-16 05:07:41 +00002242 m_stdio_communication.StopReadThread();
2243 m_stdio_communication.Disconnect();
2244 if (m_process_input_reader && m_process_input_reader->IsActive())
2245 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2246 if (m_process_input_reader)
2247 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00002248 }
2249 return error;
2250}
2251
2252Error
2253Process::Signal (int signal)
2254{
2255 Error error (WillSignal());
2256 if (error.Success())
2257 {
2258 error = DoSignal(signal);
2259 if (error.Success())
2260 DidSignal();
2261 }
2262 return error;
2263}
2264
Greg Clayton395fc332011-02-15 21:59:32 +00002265lldb::ByteOrder
2266Process::GetByteOrder () const
Chris Lattner24943d22010-06-08 16:52:24 +00002267{
Greg Clayton395fc332011-02-15 21:59:32 +00002268 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner24943d22010-06-08 16:52:24 +00002269}
2270
2271uint32_t
Greg Clayton395fc332011-02-15 21:59:32 +00002272Process::GetAddressByteSize () const
Chris Lattner24943d22010-06-08 16:52:24 +00002273{
Greg Clayton395fc332011-02-15 21:59:32 +00002274 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +00002275}
2276
Greg Clayton395fc332011-02-15 21:59:32 +00002277
Chris Lattner24943d22010-06-08 16:52:24 +00002278bool
2279Process::ShouldBroadcastEvent (Event *event_ptr)
2280{
2281 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
2282 bool return_value = true;
Greg Claytone005f2c2010-11-06 01:53:30 +00002283 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002284
2285 switch (state)
2286 {
Greg Claytone71e2582011-02-04 01:58:07 +00002287 case eStateConnected:
Chris Lattner24943d22010-06-08 16:52:24 +00002288 case eStateAttaching:
2289 case eStateLaunching:
2290 case eStateDetached:
2291 case eStateExited:
2292 case eStateUnloaded:
2293 // These events indicate changes in the state of the debugging session, always report them.
2294 return_value = true;
2295 break;
2296 case eStateInvalid:
2297 // We stopped for no apparent reason, don't report it.
2298 return_value = false;
2299 break;
2300 case eStateRunning:
2301 case eStateStepping:
2302 // If we've started the target running, we handle the cases where we
2303 // are already running and where there is a transition from stopped to
2304 // running differently.
2305 // running -> running: Automatically suppress extra running events
2306 // stopped -> running: Report except when there is one or more no votes
2307 // and no yes votes.
2308 SynchronouslyNotifyStateChanged (state);
2309 switch (m_public_state.GetValue())
2310 {
2311 case eStateRunning:
2312 case eStateStepping:
2313 // We always suppress multiple runnings with no PUBLIC stop in between.
2314 return_value = false;
2315 break;
2316 default:
2317 // TODO: make this work correctly. For now always report
2318 // run if we aren't running so we don't miss any runnning
2319 // events. If I run the lldb/test/thread/a.out file and
2320 // break at main.cpp:58, run and hit the breakpoints on
2321 // multiple threads, then somehow during the stepping over
2322 // of all breakpoints no run gets reported.
2323 return_value = true;
2324
2325 // This is a transition from stop to run.
2326 switch (m_thread_list.ShouldReportRun (event_ptr))
2327 {
2328 case eVoteYes:
2329 case eVoteNoOpinion:
2330 return_value = true;
2331 break;
2332 case eVoteNo:
2333 return_value = false;
2334 break;
2335 }
2336 break;
2337 }
2338 break;
2339 case eStateStopped:
2340 case eStateCrashed:
2341 case eStateSuspended:
2342 {
2343 // We've stopped. First see if we're going to restart the target.
2344 // If we are going to stop, then we always broadcast the event.
2345 // If we aren't going to stop, let the thread plans decide if we're going to report this event.
Jim Ingham5a47e8b2010-06-19 04:45:32 +00002346 // If no thread has an opinion, we don't report it.
Jim Ingham3ae449a2010-11-17 02:32:00 +00002347 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner24943d22010-06-08 16:52:24 +00002348 {
Greg Clayton20d338f2010-11-18 05:57:03 +00002349 if (log)
2350 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", event_ptr, StateAsCString(state));
Jim Ingham3ae449a2010-11-17 02:32:00 +00002351 return true;
2352 }
2353 else
2354 {
Chris Lattner24943d22010-06-08 16:52:24 +00002355 RefreshStateAfterStop ();
2356
2357 if (m_thread_list.ShouldStop (event_ptr) == false)
2358 {
2359 switch (m_thread_list.ShouldReportStop (event_ptr))
2360 {
2361 case eVoteYes:
2362 Process::ProcessEventData::SetRestartedInEvent (event_ptr, true);
Johnny Chen028784b2010-10-14 00:54:32 +00002363 // Intentional fall-through here.
Chris Lattner24943d22010-06-08 16:52:24 +00002364 case eVoteNoOpinion:
Chris Lattner24943d22010-06-08 16:52:24 +00002365 case eVoteNo:
2366 return_value = false;
2367 break;
2368 }
2369
2370 if (log)
Jim Ingham3ae449a2010-11-17 02:32:00 +00002371 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state));
Chris Lattner24943d22010-06-08 16:52:24 +00002372 Resume ();
2373 }
2374 else
2375 {
2376 return_value = true;
2377 SynchronouslyNotifyStateChanged (state);
2378 }
2379 }
2380 }
2381 }
2382
2383 if (log)
2384 log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO");
2385 return return_value;
2386}
2387
Chris Lattner24943d22010-06-08 16:52:24 +00002388
2389bool
2390Process::StartPrivateStateThread ()
2391{
Greg Claytone005f2c2010-11-06 01:53:30 +00002392 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002393
2394 if (log)
2395 log->Printf ("Process::%s ( )", __FUNCTION__);
2396
2397 // Create a thread that watches our internal state and controls which
2398 // events make it to clients (into the DCProcess event queue).
Greg Claytona875b642011-01-09 21:07:35 +00002399 char thread_name[1024];
2400 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%i)>", GetID());
2401 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002402 return IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002403}
2404
2405void
2406Process::PausePrivateStateThread ()
2407{
2408 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
2409}
2410
2411void
2412Process::ResumePrivateStateThread ()
2413{
2414 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
2415}
2416
2417void
2418Process::StopPrivateStateThread ()
2419{
2420 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
2421}
2422
2423void
2424Process::ControlPrivateStateThread (uint32_t signal)
2425{
Greg Claytone005f2c2010-11-06 01:53:30 +00002426 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002427
2428 assert (signal == eBroadcastInternalStateControlStop ||
2429 signal == eBroadcastInternalStateControlPause ||
2430 signal == eBroadcastInternalStateControlResume);
2431
2432 if (log)
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002433 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner24943d22010-06-08 16:52:24 +00002434
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002435 // Signal the private state thread. First we should copy this is case the
2436 // thread starts exiting since the private state thread will NULL this out
2437 // when it exits
2438 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton09c81ef2011-02-08 01:34:25 +00002439 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00002440 {
2441 TimeValue timeout_time;
2442 bool timed_out;
2443
2444 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
2445
2446 timeout_time = TimeValue::Now();
2447 timeout_time.OffsetWithSeconds(2);
2448 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
2449 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2450
2451 if (signal == eBroadcastInternalStateControlStop)
2452 {
2453 if (timed_out)
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002454 Host::ThreadCancel (private_state_thread, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +00002455
2456 thread_result_t result = NULL;
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002457 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Claytonc607d862010-07-22 18:34:21 +00002458 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00002459 }
2460 }
2461}
2462
2463void
2464Process::HandlePrivateEvent (EventSP &event_sp)
2465{
Greg Claytone005f2c2010-11-06 01:53:30 +00002466 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002467
Greg Clayton68ca8232011-01-25 02:58:48 +00002468 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002469
2470 // First check to see if anybody wants a shot at this event:
Jim Ingham68bffc52011-01-29 04:05:41 +00002471 if (m_next_event_action_ap.get() != NULL)
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002472 {
Jim Ingham68bffc52011-01-29 04:05:41 +00002473 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002474 switch (action_result)
2475 {
2476 case NextEventAction::eEventActionSuccess:
2477 SetNextEventAction(NULL);
2478 break;
2479 case NextEventAction::eEventActionRetry:
2480 break;
2481 case NextEventAction::eEventActionExit:
Jim Ingham84c86382011-01-29 01:57:31 +00002482 // Handle Exiting Here. If we already got an exited event,
2483 // we should just propagate it. Otherwise, swallow this event,
2484 // and set our state to exit so the next event will kill us.
2485 if (new_state != eStateExited)
2486 {
2487 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham68bffc52011-01-29 04:05:41 +00002488 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham84c86382011-01-29 01:57:31 +00002489 SetNextEventAction(NULL);
2490 return;
2491 }
2492 SetNextEventAction(NULL);
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002493 break;
2494 }
2495 }
2496
Chris Lattner24943d22010-06-08 16:52:24 +00002497 // See if we should broadcast this state to external clients?
2498 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00002499
2500 if (should_broadcast)
2501 {
2502 if (log)
2503 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002504 log->Printf ("Process::%s (pid = %i) broadcasting new state %s (old state %s) to %s",
2505 __FUNCTION__,
2506 GetID(),
2507 StateAsCString(new_state),
2508 StateAsCString (GetState ()),
2509 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner24943d22010-06-08 16:52:24 +00002510 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00002511 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton68ca8232011-01-25 02:58:48 +00002512 if (StateIsRunningState (new_state))
Caroline Tice861efb32010-11-16 05:07:41 +00002513 PushProcessInputReader ();
2514 else
2515 PopProcessInputReader ();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002516
Chris Lattner24943d22010-06-08 16:52:24 +00002517 BroadcastEvent (event_sp);
2518 }
2519 else
2520 {
2521 if (log)
2522 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002523 log->Printf ("Process::%s (pid = %i) suppressing state %s (old state %s): should_broadcast == false",
2524 __FUNCTION__,
2525 GetID(),
2526 StateAsCString(new_state),
2527 StateAsCString (GetState ()),
2528 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner24943d22010-06-08 16:52:24 +00002529 }
2530 }
2531}
2532
2533void *
2534Process::PrivateStateThread (void *arg)
2535{
2536 Process *proc = static_cast<Process*> (arg);
2537 void *result = proc->RunPrivateStateThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00002538 return result;
2539}
2540
2541void *
2542Process::RunPrivateStateThread ()
2543{
2544 bool control_only = false;
2545 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2546
Greg Claytone005f2c2010-11-06 01:53:30 +00002547 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002548 if (log)
2549 log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID());
2550
2551 bool exit_now = false;
2552 while (!exit_now)
2553 {
2554 EventSP event_sp;
2555 WaitForEventsPrivate (NULL, event_sp, control_only);
2556 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
2557 {
2558 switch (event_sp->GetType())
2559 {
2560 case eBroadcastInternalStateControlStop:
2561 exit_now = true;
2562 continue; // Go to next loop iteration so we exit without
2563 break; // doing any internal state managment below
2564
2565 case eBroadcastInternalStateControlPause:
2566 control_only = true;
2567 break;
2568
2569 case eBroadcastInternalStateControlResume:
2570 control_only = false;
2571 break;
2572 }
Jim Ingham3ae449a2010-11-17 02:32:00 +00002573
Jim Ingham3ae449a2010-11-17 02:32:00 +00002574 if (log)
2575 log->Printf ("Process::%s (arg = %p, pid = %i) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType());
2576
Chris Lattner24943d22010-06-08 16:52:24 +00002577 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham3ae449a2010-11-17 02:32:00 +00002578 continue;
Chris Lattner24943d22010-06-08 16:52:24 +00002579 }
2580
2581
2582 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2583
2584 if (internal_state != eStateInvalid)
2585 {
2586 HandlePrivateEvent (event_sp);
2587 }
2588
Greg Clayton3b2c41c2010-10-18 04:14:23 +00002589 if (internal_state == eStateInvalid ||
2590 internal_state == eStateExited ||
2591 internal_state == eStateDetached )
Jim Ingham3ae449a2010-11-17 02:32:00 +00002592 {
Jim Ingham3ae449a2010-11-17 02:32:00 +00002593 if (log)
2594 log->Printf ("Process::%s (arg = %p, pid = %i) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state));
2595
Chris Lattner24943d22010-06-08 16:52:24 +00002596 break;
Jim Ingham3ae449a2010-11-17 02:32:00 +00002597 }
Chris Lattner24943d22010-06-08 16:52:24 +00002598 }
2599
Caroline Tice926060e2010-10-29 21:48:37 +00002600 // Verify log is still enabled before attempting to write to it...
Chris Lattner24943d22010-06-08 16:52:24 +00002601 if (log)
2602 log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID());
2603
Greg Claytona4881d02011-01-22 07:12:45 +00002604 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
2605 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00002606 return NULL;
2607}
2608
Chris Lattner24943d22010-06-08 16:52:24 +00002609//------------------------------------------------------------------
2610// Process Event Data
2611//------------------------------------------------------------------
2612
2613Process::ProcessEventData::ProcessEventData () :
2614 EventData (),
2615 m_process_sp (),
2616 m_state (eStateInvalid),
Greg Clayton54e7afa2010-07-09 20:39:50 +00002617 m_restarted (false),
Jim Ingham3ae449a2010-11-17 02:32:00 +00002618 m_update_state (false),
2619 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00002620{
2621}
2622
2623Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
2624 EventData (),
2625 m_process_sp (process_sp),
2626 m_state (state),
Greg Clayton54e7afa2010-07-09 20:39:50 +00002627 m_restarted (false),
Jim Ingham3ae449a2010-11-17 02:32:00 +00002628 m_update_state (false),
2629 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00002630{
2631}
2632
2633Process::ProcessEventData::~ProcessEventData()
2634{
2635}
2636
2637const ConstString &
2638Process::ProcessEventData::GetFlavorString ()
2639{
2640 static ConstString g_flavor ("Process::ProcessEventData");
2641 return g_flavor;
2642}
2643
2644const ConstString &
2645Process::ProcessEventData::GetFlavor () const
2646{
2647 return ProcessEventData::GetFlavorString ();
2648}
2649
Chris Lattner24943d22010-06-08 16:52:24 +00002650void
2651Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
2652{
2653 // This function gets called twice for each event, once when the event gets pulled
2654 // off of the private process event queue, and once when it gets pulled off of
2655 // the public event queue. m_update_state is used to distinguish these
2656 // two cases; it is false when we're just pulling it off for private handling,
2657 // and we don't want to do the breakpoint command handling then.
2658
2659 if (!m_update_state)
2660 return;
2661
2662 m_process_sp->SetPublicState (m_state);
2663
2664 // If we're stopped and haven't restarted, then do the breakpoint commands here:
2665 if (m_state == eStateStopped && ! m_restarted)
2666 {
2667 int num_threads = m_process_sp->GetThreadList().GetSize();
2668 int idx;
Greg Clayton643ee732010-08-04 01:40:35 +00002669
Chris Lattner24943d22010-06-08 16:52:24 +00002670 for (idx = 0; idx < num_threads; ++idx)
2671 {
2672 lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx);
2673
Jim Ingham6297a3a2010-10-20 00:39:53 +00002674 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
2675 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002676 {
Jim Ingham6297a3a2010-10-20 00:39:53 +00002677 stop_info_sp->PerformAction(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +00002678 }
2679 }
Greg Clayton643ee732010-08-04 01:40:35 +00002680
Jim Ingham6fb8baa2010-08-10 00:59:59 +00002681 // The stop action might restart the target. If it does, then we want to mark that in the
2682 // event so that whoever is receiving it will know to wait for the running event and reflect
2683 // that state appropriately.
2684
Chris Lattner24943d22010-06-08 16:52:24 +00002685 if (m_process_sp->GetPrivateState() == eStateRunning)
2686 SetRestarted(true);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002687 else
2688 {
2689 // Finally, if we didn't restart, run the Stop Hooks here:
2690 // They might also restart the target, so watch for that.
2691 m_process_sp->GetTarget().RunStopHooks();
2692 if (m_process_sp->GetPrivateState() == eStateRunning)
2693 SetRestarted(true);
2694 }
2695
Chris Lattner24943d22010-06-08 16:52:24 +00002696 }
2697}
2698
2699void
2700Process::ProcessEventData::Dump (Stream *s) const
2701{
2702 if (m_process_sp)
2703 s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID());
2704
2705 s->Printf("state = %s", StateAsCString(GetState()));;
2706}
2707
2708const Process::ProcessEventData *
2709Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
2710{
2711 if (event_ptr)
2712 {
2713 const EventData *event_data = event_ptr->GetData();
2714 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
2715 return static_cast <const ProcessEventData *> (event_ptr->GetData());
2716 }
2717 return NULL;
2718}
2719
2720ProcessSP
2721Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
2722{
2723 ProcessSP process_sp;
2724 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2725 if (data)
2726 process_sp = data->GetProcessSP();
2727 return process_sp;
2728}
2729
2730StateType
2731Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
2732{
2733 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2734 if (data == NULL)
2735 return eStateInvalid;
2736 else
2737 return data->GetState();
2738}
2739
2740bool
2741Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
2742{
2743 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2744 if (data == NULL)
2745 return false;
2746 else
2747 return data->GetRestarted();
2748}
2749
2750void
2751Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
2752{
2753 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2754 if (data != NULL)
2755 data->SetRestarted(new_value);
2756}
2757
2758bool
Jim Ingham3ae449a2010-11-17 02:32:00 +00002759Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
2760{
2761 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2762 if (data == NULL)
2763 return false;
2764 else
2765 return data->GetInterrupted ();
2766}
2767
2768void
2769Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
2770{
2771 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2772 if (data != NULL)
2773 data->SetInterrupted(new_value);
2774}
2775
2776bool
Chris Lattner24943d22010-06-08 16:52:24 +00002777Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
2778{
2779 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2780 if (data)
2781 {
2782 data->SetUpdateStateOnRemoval();
2783 return true;
2784 }
2785 return false;
2786}
2787
Chris Lattner24943d22010-06-08 16:52:24 +00002788void
Greg Claytona830adb2010-10-04 01:05:56 +00002789Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00002790{
2791 exe_ctx.target = &m_target;
2792 exe_ctx.process = this;
2793 exe_ctx.thread = NULL;
2794 exe_ctx.frame = NULL;
2795}
2796
2797lldb::ProcessSP
2798Process::GetSP ()
2799{
2800 return GetTarget().GetProcessSP();
2801}
2802
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002803//uint32_t
2804//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2805//{
2806// return 0;
2807//}
2808//
2809//ArchSpec
2810//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
2811//{
2812// return Host::GetArchSpecForExistingProcess (pid);
2813//}
2814//
2815//ArchSpec
2816//Process::GetArchSpecForExistingProcess (const char *process_name)
2817//{
2818// return Host::GetArchSpecForExistingProcess (process_name);
2819//}
2820//
Caroline Tice861efb32010-11-16 05:07:41 +00002821void
2822Process::AppendSTDOUT (const char * s, size_t len)
2823{
Greg Clayton20d338f2010-11-18 05:57:03 +00002824 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Tice861efb32010-11-16 05:07:41 +00002825 m_stdout_data.append (s, len);
2826
Greg Claytonb3781332010-12-05 19:16:56 +00002827 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (GetTarget().GetProcessSP(), GetState()));
Caroline Tice861efb32010-11-16 05:07:41 +00002828}
2829
2830void
2831Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
2832{
2833 Process *process = (Process *) baton;
2834 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
2835}
2836
2837size_t
2838Process::ProcessInputReaderCallback (void *baton,
2839 InputReader &reader,
2840 lldb::InputReaderAction notification,
2841 const char *bytes,
2842 size_t bytes_len)
2843{
2844 Process *process = (Process *) baton;
2845
2846 switch (notification)
2847 {
2848 case eInputReaderActivate:
2849 break;
2850
2851 case eInputReaderDeactivate:
2852 break;
2853
2854 case eInputReaderReactivate:
2855 break;
2856
2857 case eInputReaderGotToken:
2858 {
2859 Error error;
2860 process->PutSTDIN (bytes, bytes_len, error);
2861 }
2862 break;
2863
Caroline Ticec4f55fe2010-11-19 20:47:54 +00002864 case eInputReaderInterrupt:
2865 process->Halt ();
2866 break;
2867
2868 case eInputReaderEndOfFile:
2869 process->AppendSTDOUT ("^D", 2);
2870 break;
2871
Caroline Tice861efb32010-11-16 05:07:41 +00002872 case eInputReaderDone:
2873 break;
2874
2875 }
2876
2877 return bytes_len;
2878}
2879
2880void
2881Process::ResetProcessInputReader ()
2882{
2883 m_process_input_reader.reset();
2884}
2885
2886void
2887Process::SetUpProcessInputReader (int file_descriptor)
2888{
2889 // First set up the Read Thread for reading/handling process I/O
2890
2891 std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true));
2892
2893 if (conn_ap.get())
2894 {
2895 m_stdio_communication.SetConnection (conn_ap.release());
2896 if (m_stdio_communication.IsConnected())
2897 {
2898 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
2899 m_stdio_communication.StartReadThread();
2900
2901 // Now read thread is set up, set up input reader.
2902
2903 if (!m_process_input_reader.get())
2904 {
2905 m_process_input_reader.reset (new InputReader(m_target.GetDebugger()));
2906 Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback,
2907 this,
2908 eInputReaderGranularityByte,
2909 NULL,
2910 NULL,
2911 false));
2912
2913 if (err.Fail())
2914 m_process_input_reader.reset();
2915 }
2916 }
2917 }
2918}
2919
2920void
2921Process::PushProcessInputReader ()
2922{
2923 if (m_process_input_reader && !m_process_input_reader->IsActive())
2924 m_target.GetDebugger().PushInputReader (m_process_input_reader);
2925}
2926
2927void
2928Process::PopProcessInputReader ()
2929{
2930 if (m_process_input_reader && m_process_input_reader->IsActive())
2931 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2932}
2933
Greg Claytond284b662011-02-18 01:44:25 +00002934// The process needs to know about installed plug-ins
Greg Clayton990de7b2010-11-18 23:32:35 +00002935void
Caroline Tice2a456812011-03-10 22:14:10 +00002936Process::SettingsInitialize ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002937{
Greg Claytonb3448432011-03-24 21:19:54 +00002938 static std::vector<OptionEnumValueElement> g_plugins;
Greg Claytond284b662011-02-18 01:44:25 +00002939
2940 int i=0;
2941 const char *name;
2942 OptionEnumValueElement option_enum;
2943 while ((name = PluginManager::GetProcessPluginNameAtIndex (i)) != NULL)
2944 {
2945 if (name)
2946 {
2947 option_enum.value = i;
2948 option_enum.string_value = name;
2949 option_enum.usage = PluginManager::GetProcessPluginDescriptionAtIndex (i);
2950 g_plugins.push_back (option_enum);
2951 }
2952 ++i;
2953 }
2954 option_enum.value = 0;
2955 option_enum.string_value = NULL;
2956 option_enum.usage = NULL;
2957 g_plugins.push_back (option_enum);
2958
2959 for (i=0; (name = SettingsController::instance_settings_table[i].var_name); ++i)
2960 {
2961 if (::strcmp (name, "plugin") == 0)
2962 {
2963 SettingsController::instance_settings_table[i].enum_values = &g_plugins[0];
2964 break;
2965 }
2966 }
Greg Clayton990de7b2010-11-18 23:32:35 +00002967 UserSettingsControllerSP &usc = GetSettingsController();
2968 usc.reset (new SettingsController);
2969 UserSettingsController::InitializeSettingsController (usc,
2970 SettingsController::global_settings_table,
2971 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00002972
2973 // Now call SettingsInitialize() for each 'child' of Process settings
2974 Thread::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00002975}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002976
Greg Clayton990de7b2010-11-18 23:32:35 +00002977void
Caroline Tice2a456812011-03-10 22:14:10 +00002978Process::SettingsTerminate ()
Greg Claytond284b662011-02-18 01:44:25 +00002979{
Caroline Tice2a456812011-03-10 22:14:10 +00002980 // Must call SettingsTerminate() on each 'child' of Process settings before terminating Process settings.
2981
2982 Thread::SettingsTerminate ();
2983
2984 // Now terminate Process Settings.
2985
Greg Clayton990de7b2010-11-18 23:32:35 +00002986 UserSettingsControllerSP &usc = GetSettingsController();
2987 UserSettingsController::FinalizeSettingsController (usc);
2988 usc.reset();
2989}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002990
Greg Clayton990de7b2010-11-18 23:32:35 +00002991UserSettingsControllerSP &
2992Process::GetSettingsController ()
2993{
2994 static UserSettingsControllerSP g_settings_controller;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002995 return g_settings_controller;
2996}
2997
Caroline Tice1ebef442010-09-27 00:30:10 +00002998void
2999Process::UpdateInstanceName ()
3000{
3001 ModuleSP module_sp = GetTarget().GetExecutableModule();
3002 if (module_sp)
3003 {
3004 StreamString sstr;
3005 sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString());
3006
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003007 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
Caroline Tice1ebef442010-09-27 00:30:10 +00003008 sstr.GetData());
3009 }
3010}
3011
Greg Clayton427f2902010-12-14 02:59:59 +00003012ExecutionResults
Jim Ingham360f53f2010-11-30 02:22:11 +00003013Process::RunThreadPlan (ExecutionContext &exe_ctx,
3014 lldb::ThreadPlanSP &thread_plan_sp,
3015 bool stop_others,
3016 bool try_all_threads,
3017 bool discard_on_error,
3018 uint32_t single_thread_timeout_usec,
3019 Stream &errors)
3020{
3021 ExecutionResults return_value = eExecutionSetupError;
3022
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003023 if (thread_plan_sp.get() == NULL)
3024 {
3025 errors.Printf("RunThreadPlan called with empty thread plan.");
Greg Claytonb3448432011-03-24 21:19:54 +00003026 return eExecutionSetupError;
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003027 }
3028
Jim Inghamac959662011-01-24 06:34:17 +00003029 if (m_private_state.GetValue() != eStateStopped)
3030 {
3031 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Greg Claytonb3448432011-03-24 21:19:54 +00003032 return eExecutionSetupError;
Jim Inghamac959662011-01-24 06:34:17 +00003033 }
3034
Jim Ingham360f53f2010-11-30 02:22:11 +00003035 // Save this value for restoration of the execution context after we run
3036 uint32_t tid = exe_ctx.thread->GetIndexID();
3037
3038 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
3039 // so we should arrange to reset them as well.
3040
3041 lldb::ThreadSP selected_thread_sp = exe_ctx.process->GetThreadList().GetSelectedThread();
3042 lldb::StackFrameSP selected_frame_sp;
3043
3044 uint32_t selected_tid;
3045 if (selected_thread_sp != NULL)
3046 {
3047 selected_tid = selected_thread_sp->GetIndexID();
3048 selected_frame_sp = selected_thread_sp->GetSelectedFrame();
3049 }
3050 else
3051 {
3052 selected_tid = LLDB_INVALID_THREAD_ID;
3053 }
3054
3055 exe_ctx.thread->QueueThreadPlan(thread_plan_sp, true);
3056
Jim Ingham6ae318c2011-01-23 21:14:08 +00003057 Listener listener("lldb.process.listener.run-thread-plan");
Jim Inghamf9f40c22011-02-08 05:20:59 +00003058
3059 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
3060 // restored on exit to the function.
3061
3062 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Jim Inghamac959662011-01-24 06:34:17 +00003063
Jim Ingham6ae318c2011-01-23 21:14:08 +00003064 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003065 if (log)
3066 {
3067 StreamString s;
3068 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Jim Inghamf9f40c22011-02-08 05:20:59 +00003069 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4x to run thread plan \"%s\".",
3070 exe_ctx.thread->GetIndexID(),
3071 exe_ctx.thread->GetID(),
3072 s.GetData());
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003073 }
3074
Jim Inghamf9f40c22011-02-08 05:20:59 +00003075 bool got_event;
3076 lldb::EventSP event_sp;
3077 lldb::StateType stop_state = lldb::eStateInvalid;
Jim Ingham360f53f2010-11-30 02:22:11 +00003078
3079 TimeValue* timeout_ptr = NULL;
3080 TimeValue real_timeout;
3081
Jim Inghamf9f40c22011-02-08 05:20:59 +00003082 bool first_timeout = true;
3083 bool do_resume = true;
Jim Ingham360f53f2010-11-30 02:22:11 +00003084
Jim Ingham360f53f2010-11-30 02:22:11 +00003085 while (1)
3086 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003087 // We usually want to resume the process if we get to the top of the loop.
3088 // The only exception is if we get two running events with no intervening
3089 // stop, which can happen, we will just wait for then next stop event.
Jim Ingham360f53f2010-11-30 02:22:11 +00003090
Jim Inghamf9f40c22011-02-08 05:20:59 +00003091 if (do_resume)
Jim Ingham360f53f2010-11-30 02:22:11 +00003092 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003093 // Do the initial resume and wait for the running event before going further.
3094
3095 Error resume_error = exe_ctx.process->Resume ();
3096 if (!resume_error.Success())
3097 {
3098 errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString());
Greg Claytonb3448432011-03-24 21:19:54 +00003099 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003100 break;
3101 }
3102
3103 real_timeout = TimeValue::Now();
3104 real_timeout.OffsetWithMicroSeconds(500000);
3105 timeout_ptr = &real_timeout;
3106
3107 got_event = listener.WaitForEvent(NULL, event_sp);
3108 if (!got_event)
3109 {
3110 if (log)
3111 log->Printf("Didn't get any event after initial resume, exiting.");
3112
3113 errors.Printf("Didn't get any event after initial resume, exiting.");
Greg Claytonb3448432011-03-24 21:19:54 +00003114 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003115 break;
3116 }
3117
3118 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3119 if (stop_state != eStateRunning)
3120 {
3121 if (log)
3122 log->Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state));
3123
3124 errors.Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state));
Greg Claytonb3448432011-03-24 21:19:54 +00003125 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003126 break;
3127 }
3128
3129 if (log)
3130 log->Printf ("Resuming succeeded.");
3131 // We need to call the function synchronously, so spin waiting for it to return.
3132 // If we get interrupted while executing, we're going to lose our context, and
3133 // won't be able to gather the result at this point.
3134 // We set the timeout AFTER the resume, since the resume takes some time and we
3135 // don't want to charge that to the timeout.
3136
3137 if (single_thread_timeout_usec != 0)
3138 {
3139 real_timeout = TimeValue::Now();
3140 if (first_timeout)
3141 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
3142 else
3143 real_timeout.OffsetWithSeconds(10);
3144
3145 timeout_ptr = &real_timeout;
3146 }
3147 }
3148 else
3149 {
3150 if (log)
3151 log->Printf ("Handled an extra running event.");
3152 do_resume = true;
3153 }
3154
3155 // Now wait for the process to stop again:
3156 stop_state = lldb::eStateInvalid;
3157 event_sp.reset();
3158 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
3159
3160 if (got_event)
3161 {
3162 if (event_sp.get())
3163 {
3164 bool keep_going = false;
3165 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3166 if (log)
3167 log->Printf("In while loop, got event: %s.", StateAsCString(stop_state));
3168
3169 switch (stop_state)
3170 {
3171 case lldb::eStateStopped:
3172 // Yay, we're done.
3173 if (log)
3174 log->Printf ("Execution completed successfully.");
Greg Claytonb3448432011-03-24 21:19:54 +00003175 return_value = eExecutionCompleted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003176 break;
3177 case lldb::eStateCrashed:
3178 if (log)
3179 log->Printf ("Execution crashed.");
Greg Claytonb3448432011-03-24 21:19:54 +00003180 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003181 break;
3182 case lldb::eStateRunning:
3183 do_resume = false;
3184 keep_going = true;
3185 break;
3186 default:
3187 if (log)
3188 log->Printf("Execution stopped with unexpected state: %s.", StateAsCString(stop_state));
Greg Claytonb3448432011-03-24 21:19:54 +00003189 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003190 break;
3191 }
3192 if (keep_going)
3193 continue;
3194 else
3195 break;
3196 }
3197 else
3198 {
3199 if (log)
3200 log->Printf ("got_event was true, but the event pointer was null. How odd...");
Greg Claytonb3448432011-03-24 21:19:54 +00003201 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003202 break;
3203 }
3204 }
3205 else
3206 {
3207 // If we didn't get an event that means we've timed out...
3208 // We will interrupt the process here. Depending on what we were asked to do we will
3209 // either exit, or try with all threads running for the same timeout.
Jim Ingham360f53f2010-11-30 02:22:11 +00003210 // Not really sure what to do if Halt fails here...
Jim Inghamf9f40c22011-02-08 05:20:59 +00003211
Stephen Wilsonc2b98252011-01-12 04:20:03 +00003212 if (log) {
Jim Ingham360f53f2010-11-30 02:22:11 +00003213 if (try_all_threads)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003214 {
3215 if (first_timeout)
3216 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
3217 "trying with all threads enabled.",
3218 single_thread_timeout_usec);
3219 else
3220 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
3221 "and timeout: %d timed out.",
3222 single_thread_timeout_usec);
3223 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003224 else
Jim Inghamf9f40c22011-02-08 05:20:59 +00003225 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
3226 "halt and abandoning execution.",
Jim Ingham360f53f2010-11-30 02:22:11 +00003227 single_thread_timeout_usec);
Stephen Wilsonc2b98252011-01-12 04:20:03 +00003228 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003229
Jim Inghamc556b462011-01-22 01:30:53 +00003230 Error halt_error = exe_ctx.process->Halt();
Jim Inghamc556b462011-01-22 01:30:53 +00003231 if (halt_error.Success())
Jim Ingham360f53f2010-11-30 02:22:11 +00003232 {
Jim Ingham360f53f2010-11-30 02:22:11 +00003233 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00003234 log->Printf ("Process::RunThreadPlan(): Halt succeeded.");
Jim Ingham360f53f2010-11-30 02:22:11 +00003235
Jim Inghamf9f40c22011-02-08 05:20:59 +00003236 // If halt succeeds, it always produces a stopped event. Wait for that:
3237
3238 real_timeout = TimeValue::Now();
3239 real_timeout.OffsetWithMicroSeconds(500000);
3240
3241 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Jim Ingham360f53f2010-11-30 02:22:11 +00003242
3243 if (got_event)
3244 {
3245 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3246 if (log)
3247 {
Greg Clayton68ca8232011-01-25 02:58:48 +00003248 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
Jim Inghamf9f40c22011-02-08 05:20:59 +00003249 if (stop_state == lldb::eStateStopped
3250 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
Jim Ingham360f53f2010-11-30 02:22:11 +00003251 log->Printf (" Event was the Halt interruption event.");
3252 }
3253
Jim Inghamf9f40c22011-02-08 05:20:59 +00003254 if (stop_state == lldb::eStateStopped)
Jim Ingham360f53f2010-11-30 02:22:11 +00003255 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003256 // Between the time we initiated the Halt and the time we delivered it, the process could have
3257 // already finished its job. Check that here:
Jim Ingham360f53f2010-11-30 02:22:11 +00003258
Jim Inghamf9f40c22011-02-08 05:20:59 +00003259 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3260 {
3261 if (log)
3262 log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
3263 "Exiting wait loop.");
Greg Claytonb3448432011-03-24 21:19:54 +00003264 return_value = eExecutionCompleted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003265 break;
3266 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003267
Jim Inghamf9f40c22011-02-08 05:20:59 +00003268 if (!try_all_threads)
3269 {
3270 if (log)
3271 log->Printf ("try_all_threads was false, we stopped so now we're quitting.");
Greg Claytonb3448432011-03-24 21:19:54 +00003272 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003273 break;
3274 }
3275
3276 if (first_timeout)
3277 {
3278 // Set all the other threads to run, and return to the top of the loop, which will continue;
3279 first_timeout = false;
3280 thread_plan_sp->SetStopOthers (false);
3281 if (log)
3282 log->Printf ("Process::RunThreadPlan(): About to resume.");
3283
3284 continue;
3285 }
3286 else
3287 {
3288 // Running all threads failed, so return Interrupted.
3289 if (log)
3290 log->Printf("Process::RunThreadPlan(): running all threads timed out.");
Greg Claytonb3448432011-03-24 21:19:54 +00003291 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003292 break;
3293 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003294 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00003295 }
3296 else
3297 { if (log)
3298 log->Printf("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
3299 "I'm getting out of here passing Interrupted.");
Greg Claytonb3448432011-03-24 21:19:54 +00003300 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003301 break;
Jim Ingham360f53f2010-11-30 02:22:11 +00003302 }
3303 }
Jim Inghamc556b462011-01-22 01:30:53 +00003304 else
3305 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003306 // This branch is to work around some problems with gdb-remote's Halt. It is a little racy, and can return
3307 // an error from halt, but if you wait a bit you'll get a stopped event anyway.
Jim Inghamc556b462011-01-22 01:30:53 +00003308 if (log)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003309 log->Printf ("Process::RunThreadPlan(): halt failed: error = \"%s\", I'm just going to wait a little longer and see if I get a stopped event.",
3310 halt_error.AsCString());
3311 real_timeout = TimeValue::Now();
3312 real_timeout.OffsetWithMicroSeconds(500000);
3313 timeout_ptr = &real_timeout;
3314 got_event = listener.WaitForEvent(&real_timeout, event_sp);
3315 if (!got_event || event_sp.get() == NULL)
Jim Ingham6ae318c2011-01-23 21:14:08 +00003316 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003317 // This is not going anywhere, bag out.
3318 if (log)
3319 log->Printf ("Process::RunThreadPlan(): halt failed: and waiting for the stopped event failed.");
Greg Claytonb3448432011-03-24 21:19:54 +00003320 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003321 break;
Jim Ingham6ae318c2011-01-23 21:14:08 +00003322 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00003323 else
3324 {
3325 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3326 if (log)
3327 log->Printf ("Process::RunThreadPlan(): halt failed: but then I got a stopped event. Whatever...");
3328 if (stop_state == lldb::eStateStopped)
3329 {
3330 // Between the time we initiated the Halt and the time we delivered it, the process could have
3331 // already finished its job. Check that here:
3332
3333 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3334 {
3335 if (log)
3336 log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
3337 "Exiting wait loop.");
Greg Claytonb3448432011-03-24 21:19:54 +00003338 return_value = eExecutionCompleted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003339 break;
3340 }
3341
3342 if (first_timeout)
3343 {
3344 // Set all the other threads to run, and return to the top of the loop, which will continue;
3345 first_timeout = false;
3346 thread_plan_sp->SetStopOthers (false);
3347 if (log)
3348 log->Printf ("Process::RunThreadPlan(): About to resume.");
3349
3350 continue;
3351 }
3352 else
3353 {
3354 // Running all threads failed, so return Interrupted.
3355 if (log)
3356 log->Printf("Process::RunThreadPlan(): running all threads timed out.");
Greg Claytonb3448432011-03-24 21:19:54 +00003357 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003358 break;
3359 }
3360 }
3361 else
3362 {
3363 log->Printf ("Process::RunThreadPlan(): halt failed, I waited and didn't get"
3364 " a stopped event, instead got %s.", StateAsCString(stop_state));
Greg Claytonb3448432011-03-24 21:19:54 +00003365 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003366 break;
3367 }
3368 }
Jim Inghamc556b462011-01-22 01:30:53 +00003369 }
3370
Jim Ingham360f53f2010-11-30 02:22:11 +00003371 }
3372
Jim Inghamf9f40c22011-02-08 05:20:59 +00003373 } // END WAIT LOOP
3374
3375 // Now do some processing on the results of the run:
3376 if (return_value == eExecutionInterrupted)
3377 {
Jim Ingham360f53f2010-11-30 02:22:11 +00003378 if (log)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003379 {
3380 StreamString s;
3381 if (event_sp)
3382 event_sp->Dump (&s);
3383 else
3384 {
3385 log->Printf ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
3386 }
3387
3388 StreamString ts;
3389
3390 const char *event_explanation;
3391
3392 do
3393 {
3394 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
3395
3396 if (!event_data)
3397 {
3398 event_explanation = "<no event data>";
3399 break;
3400 }
3401
3402 Process *process = event_data->GetProcessSP().get();
3403
3404 if (!process)
3405 {
3406 event_explanation = "<no process>";
3407 break;
3408 }
3409
3410 ThreadList &thread_list = process->GetThreadList();
3411
3412 uint32_t num_threads = thread_list.GetSize();
3413 uint32_t thread_index;
3414
3415 ts.Printf("<%u threads> ", num_threads);
3416
3417 for (thread_index = 0;
3418 thread_index < num_threads;
3419 ++thread_index)
3420 {
3421 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
3422
3423 if (!thread)
3424 {
3425 ts.Printf("<?> ");
3426 continue;
3427 }
3428
3429 ts.Printf("<0x%4.4x ", thread->GetID());
3430 RegisterContext *register_context = thread->GetRegisterContext().get();
3431
3432 if (register_context)
3433 ts.Printf("[ip 0x%llx] ", register_context->GetPC());
3434 else
3435 ts.Printf("[ip unknown] ");
3436
3437 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
3438 if (stop_info_sp)
3439 {
3440 const char *stop_desc = stop_info_sp->GetDescription();
3441 if (stop_desc)
3442 ts.PutCString (stop_desc);
3443 }
3444 ts.Printf(">");
3445 }
3446
3447 event_explanation = ts.GetData();
3448 } while (0);
3449
3450 if (log)
3451 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
3452
3453 if (discard_on_error && thread_plan_sp)
3454 {
3455 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3456 }
3457 }
3458 }
3459 else if (return_value == eExecutionSetupError)
3460 {
3461 if (log)
3462 log->Printf("Process::RunThreadPlan(): execution set up error.");
3463
3464 if (discard_on_error && thread_plan_sp)
3465 {
3466 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3467 }
3468 }
3469 else
3470 {
Jim Ingham360f53f2010-11-30 02:22:11 +00003471 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3472 {
Greg Clayton68ca8232011-01-25 02:58:48 +00003473 if (log)
3474 log->Printf("Process::RunThreadPlan(): thread plan is done");
Greg Claytonb3448432011-03-24 21:19:54 +00003475 return_value = eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +00003476 }
3477 else if (exe_ctx.thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
3478 {
Greg Clayton68ca8232011-01-25 02:58:48 +00003479 if (log)
3480 log->Printf("Process::RunThreadPlan(): thread plan was discarded");
Greg Claytonb3448432011-03-24 21:19:54 +00003481 return_value = eExecutionDiscarded;
Jim Ingham360f53f2010-11-30 02:22:11 +00003482 }
3483 else
3484 {
3485 if (log)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003486 log->Printf("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Ingham360f53f2010-11-30 02:22:11 +00003487 if (discard_on_error && thread_plan_sp)
3488 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003489 if (log)
3490 log->Printf("Process::RunThreadPlan(): discarding thread plan 'cause discard_on_error is set.");
Jim Ingham360f53f2010-11-30 02:22:11 +00003491 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3492 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003493 }
3494 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00003495
Jim Ingham360f53f2010-11-30 02:22:11 +00003496 // Thread we ran the function in may have gone away because we ran the target
3497 // Check that it's still there.
3498 exe_ctx.thread = exe_ctx.process->GetThreadList().FindThreadByIndexID(tid, true).get();
3499 if (exe_ctx.thread)
3500 exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex(0).get();
3501
3502 // Also restore the current process'es selected frame & thread, since this function calling may
3503 // be done behind the user's back.
3504
3505 if (selected_tid != LLDB_INVALID_THREAD_ID)
3506 {
3507 if (exe_ctx.process->GetThreadList().SetSelectedThreadByIndexID (selected_tid))
3508 {
3509 // We were able to restore the selected thread, now restore the frame:
3510 exe_ctx.process->GetThreadList().GetSelectedThread()->SetSelectedFrame(selected_frame_sp.get());
3511 }
3512 }
3513
3514 return return_value;
3515}
3516
3517const char *
3518Process::ExecutionResultAsCString (ExecutionResults result)
3519{
3520 const char *result_name;
3521
3522 switch (result)
3523 {
Greg Claytonb3448432011-03-24 21:19:54 +00003524 case eExecutionCompleted:
Jim Ingham360f53f2010-11-30 02:22:11 +00003525 result_name = "eExecutionCompleted";
3526 break;
Greg Claytonb3448432011-03-24 21:19:54 +00003527 case eExecutionDiscarded:
Jim Ingham360f53f2010-11-30 02:22:11 +00003528 result_name = "eExecutionDiscarded";
3529 break;
Greg Claytonb3448432011-03-24 21:19:54 +00003530 case eExecutionInterrupted:
Jim Ingham360f53f2010-11-30 02:22:11 +00003531 result_name = "eExecutionInterrupted";
3532 break;
Greg Claytonb3448432011-03-24 21:19:54 +00003533 case eExecutionSetupError:
Jim Ingham360f53f2010-11-30 02:22:11 +00003534 result_name = "eExecutionSetupError";
3535 break;
Greg Claytonb3448432011-03-24 21:19:54 +00003536 case eExecutionTimedOut:
Jim Ingham360f53f2010-11-30 02:22:11 +00003537 result_name = "eExecutionTimedOut";
3538 break;
3539 }
3540 return result_name;
3541}
3542
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003543//--------------------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00003544// class Process::SettingsController
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003545//--------------------------------------------------------------
3546
Greg Claytond0a5a232010-09-19 02:33:57 +00003547Process::SettingsController::SettingsController () :
Caroline Tice5bc8c972010-09-20 20:44:43 +00003548 UserSettingsController ("process", Target::GetSettingsController())
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003549{
Greg Clayton638351a2010-12-04 00:10:17 +00003550 m_default_settings.reset (new ProcessInstanceSettings (*this,
3551 false,
Caroline Tice004afcb2010-09-08 17:48:55 +00003552 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003553}
3554
Greg Claytond0a5a232010-09-19 02:33:57 +00003555Process::SettingsController::~SettingsController ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003556{
3557}
3558
3559lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +00003560Process::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003561{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003562 ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*GetSettingsController(),
3563 false,
3564 instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003565 lldb::InstanceSettingsSP new_settings_sp (new_settings);
3566 return new_settings_sp;
3567}
3568
3569//--------------------------------------------------------------
3570// class ProcessInstanceSettings
3571//--------------------------------------------------------------
3572
Greg Clayton638351a2010-12-04 00:10:17 +00003573ProcessInstanceSettings::ProcessInstanceSettings
3574(
3575 UserSettingsController &owner,
3576 bool live_instance,
3577 const char *name
3578) :
3579 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003580 m_run_args (),
3581 m_env_vars (),
3582 m_input_path (),
3583 m_output_path (),
3584 m_error_path (),
Caroline Ticebd666012010-12-03 18:46:09 +00003585 m_disable_aslr (true),
Greg Clayton638351a2010-12-04 00:10:17 +00003586 m_disable_stdio (false),
3587 m_inherit_host_env (true),
3588 m_got_host_env (false)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003589{
Caroline Tice396704b2010-09-09 18:26:37 +00003590 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
3591 // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers.
3592 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
Caroline Tice75b11a32010-09-16 19:05:55 +00003593 // This is true for CreateInstanceName() too.
3594
3595 if (GetInstanceName () == InstanceSettings::InvalidName())
3596 {
3597 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
3598 m_owner.RegisterInstanceSettings (this);
3599 }
Caroline Tice396704b2010-09-09 18:26:37 +00003600
3601 if (live_instance)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003602 {
3603 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3604 CopyInstanceSettings (pending_settings,false);
Caroline Tice396704b2010-09-09 18:26:37 +00003605 //m_owner.RemovePendingSettings (m_instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003606 }
3607}
3608
3609ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003610 InstanceSettings (*Process::GetSettingsController(), CreateInstanceName().AsCString()),
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003611 m_run_args (rhs.m_run_args),
3612 m_env_vars (rhs.m_env_vars),
3613 m_input_path (rhs.m_input_path),
3614 m_output_path (rhs.m_output_path),
3615 m_error_path (rhs.m_error_path),
Caroline Ticebd666012010-12-03 18:46:09 +00003616 m_disable_aslr (rhs.m_disable_aslr),
3617 m_disable_stdio (rhs.m_disable_stdio)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003618{
3619 if (m_instance_name != InstanceSettings::GetDefaultName())
3620 {
3621 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3622 CopyInstanceSettings (pending_settings,false);
3623 m_owner.RemovePendingSettings (m_instance_name);
3624 }
3625}
3626
3627ProcessInstanceSettings::~ProcessInstanceSettings ()
3628{
3629}
3630
3631ProcessInstanceSettings&
3632ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs)
3633{
3634 if (this != &rhs)
3635 {
3636 m_run_args = rhs.m_run_args;
3637 m_env_vars = rhs.m_env_vars;
3638 m_input_path = rhs.m_input_path;
3639 m_output_path = rhs.m_output_path;
3640 m_error_path = rhs.m_error_path;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003641 m_disable_aslr = rhs.m_disable_aslr;
Caroline Ticebd666012010-12-03 18:46:09 +00003642 m_disable_stdio = rhs.m_disable_stdio;
Greg Clayton638351a2010-12-04 00:10:17 +00003643 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003644 }
3645
3646 return *this;
3647}
3648
3649
3650void
3651ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
3652 const char *index_value,
3653 const char *value,
3654 const ConstString &instance_name,
3655 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00003656 VarSetOperationType op,
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003657 Error &err,
3658 bool pending)
3659{
3660 if (var_name == RunArgsVarName())
3661 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
3662 else if (var_name == EnvVarsVarName())
Greg Clayton638351a2010-12-04 00:10:17 +00003663 {
3664 GetHostEnvironmentIfNeeded ();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003665 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
Greg Clayton638351a2010-12-04 00:10:17 +00003666 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003667 else if (var_name == InputPathVarName())
3668 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
3669 else if (var_name == OutputPathVarName())
3670 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
3671 else if (var_name == ErrorPathVarName())
3672 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003673 else if (var_name == DisableASLRVarName())
3674 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err);
Caroline Ticebd666012010-12-03 18:46:09 +00003675 else if (var_name == DisableSTDIOVarName ())
3676 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, err);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003677}
3678
3679void
3680ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
3681 bool pending)
3682{
3683 if (new_settings.get() == NULL)
3684 return;
3685
3686 ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get();
3687
3688 m_run_args = new_process_settings->m_run_args;
3689 m_env_vars = new_process_settings->m_env_vars;
3690 m_input_path = new_process_settings->m_input_path;
3691 m_output_path = new_process_settings->m_output_path;
3692 m_error_path = new_process_settings->m_error_path;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003693 m_disable_aslr = new_process_settings->m_disable_aslr;
Caroline Ticebd666012010-12-03 18:46:09 +00003694 m_disable_stdio = new_process_settings->m_disable_stdio;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003695}
3696
Caroline Ticebcb5b452010-09-20 21:37:42 +00003697bool
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003698ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
3699 const ConstString &var_name,
Caroline Tice5bc8c972010-09-20 20:44:43 +00003700 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00003701 Error *err)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003702{
3703 if (var_name == RunArgsVarName())
3704 {
3705 if (m_run_args.GetArgumentCount() > 0)
Greg Claytonc14069e2010-09-14 03:47:41 +00003706 {
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003707 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
3708 value.AppendString (m_run_args.GetArgumentAtIndex (i));
Greg Claytonc14069e2010-09-14 03:47:41 +00003709 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003710 }
3711 else if (var_name == EnvVarsVarName())
3712 {
Greg Clayton638351a2010-12-04 00:10:17 +00003713 GetHostEnvironmentIfNeeded ();
3714
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003715 if (m_env_vars.size() > 0)
3716 {
3717 std::map<std::string, std::string>::iterator pos;
3718 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
3719 {
3720 StreamString value_str;
3721 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
3722 value.AppendString (value_str.GetData());
3723 }
3724 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003725 }
3726 else if (var_name == InputPathVarName())
3727 {
3728 value.AppendString (m_input_path.c_str());
3729 }
3730 else if (var_name == OutputPathVarName())
3731 {
3732 value.AppendString (m_output_path.c_str());
3733 }
3734 else if (var_name == ErrorPathVarName())
3735 {
3736 value.AppendString (m_error_path.c_str());
3737 }
Greg Claytona99b0bf2010-12-04 00:12:24 +00003738 else if (var_name == InheritHostEnvVarName())
3739 {
3740 if (m_inherit_host_env)
3741 value.AppendString ("true");
3742 else
3743 value.AppendString ("false");
3744 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003745 else if (var_name == DisableASLRVarName())
3746 {
3747 if (m_disable_aslr)
3748 value.AppendString ("true");
3749 else
3750 value.AppendString ("false");
3751 }
Caroline Ticebd666012010-12-03 18:46:09 +00003752 else if (var_name == DisableSTDIOVarName())
3753 {
3754 if (m_disable_stdio)
3755 value.AppendString ("true");
3756 else
3757 value.AppendString ("false");
3758 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003759 else
Caroline Ticebcb5b452010-09-20 21:37:42 +00003760 {
3761 if (err)
3762 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
3763 return false;
3764 }
3765 return true;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003766}
3767
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003768const ConstString
3769ProcessInstanceSettings::CreateInstanceName ()
3770{
3771 static int instance_count = 1;
3772 StreamString sstr;
3773
3774 sstr.Printf ("process_%d", instance_count);
3775 ++instance_count;
3776
3777 const ConstString ret_val (sstr.GetData());
3778 return ret_val;
3779}
3780
3781const ConstString &
3782ProcessInstanceSettings::RunArgsVarName ()
3783{
3784 static ConstString run_args_var_name ("run-args");
3785
3786 return run_args_var_name;
3787}
3788
3789const ConstString &
3790ProcessInstanceSettings::EnvVarsVarName ()
3791{
3792 static ConstString env_vars_var_name ("env-vars");
3793
3794 return env_vars_var_name;
3795}
3796
3797const ConstString &
Greg Clayton638351a2010-12-04 00:10:17 +00003798ProcessInstanceSettings::InheritHostEnvVarName ()
3799{
3800 static ConstString g_name ("inherit-env");
3801
3802 return g_name;
3803}
3804
3805const ConstString &
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003806ProcessInstanceSettings::InputPathVarName ()
3807{
3808 static ConstString input_path_var_name ("input-path");
3809
3810 return input_path_var_name;
3811}
3812
3813const ConstString &
3814ProcessInstanceSettings::OutputPathVarName ()
3815{
Caroline Tice87097232010-09-07 18:35:40 +00003816 static ConstString output_path_var_name ("output-path");
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003817
3818 return output_path_var_name;
3819}
3820
3821const ConstString &
3822ProcessInstanceSettings::ErrorPathVarName ()
3823{
Caroline Tice87097232010-09-07 18:35:40 +00003824 static ConstString error_path_var_name ("error-path");
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003825
3826 return error_path_var_name;
3827}
3828
3829const ConstString &
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003830ProcessInstanceSettings::DisableASLRVarName ()
3831{
3832 static ConstString disable_aslr_var_name ("disable-aslr");
3833
3834 return disable_aslr_var_name;
3835}
3836
Caroline Ticebd666012010-12-03 18:46:09 +00003837const ConstString &
3838ProcessInstanceSettings::DisableSTDIOVarName ()
3839{
3840 static ConstString disable_stdio_var_name ("disable-stdio");
3841
3842 return disable_stdio_var_name;
3843}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003844
3845//--------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00003846// SettingsController Variable Tables
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003847//--------------------------------------------------
3848
3849SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00003850Process::SettingsController::global_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003851{
3852 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
3853 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
3854};
3855
3856
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003857SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00003858Process::SettingsController::instance_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003859{
Greg Clayton638351a2010-12-04 00:10:17 +00003860 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
3861 { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
3862 { "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." },
3863 { "inherit-env", eSetVarTypeBoolean, "true", NULL, false, false, "Inherit the environment from the process that is running LLDB." },
Greg Claytonde915be2011-01-23 05:56:20 +00003864 { "input-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for reading its input." },
3865 { "output-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writing its output." },
3866 { "error-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writings its error messages." },
Greg Claytond284b662011-02-18 01:44:25 +00003867 { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
Greg Clayton638351a2010-12-04 00:10:17 +00003868 { "disable-aslr", eSetVarTypeBoolean, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
3869 { "disable-stdio", eSetVarTypeBoolean, "false", NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" },
3870 { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003871};
3872
3873
Jim Ingham7508e732010-08-09 23:31:02 +00003874