blob: 1a9bc8b45626633cfbf83bf70c8cff23b53f604e [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Process.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Process.h"
11
12#include "lldb/lldb-private-log.h"
13
14#include "lldb/Breakpoint/StoppointCallbackContext.h"
15#include "lldb/Breakpoint/BreakpointLocation.h"
16#include "lldb/Core/Event.h"
Caroline Tice861efb32010-11-16 05:07:41 +000017#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/Debugger.h"
Caroline Tice861efb32010-11-16 05:07:41 +000019#include "lldb/Core/InputReader.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Log.h"
21#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/State.h"
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000023#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Host/Host.h"
25#include "lldb/Target/ABI.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000026#include "lldb/Target/DynamicLoader.h"
Jim Ingham642036f2010-09-23 02:01:19 +000027#include "lldb/Target/LanguageRuntime.h"
28#include "lldb/Target/CPPLanguageRuntime.h"
29#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000030#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000032#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Target/Target.h"
34#include "lldb/Target/TargetList.h"
35#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadPlan.h"
37
38using namespace lldb;
39using namespace lldb_private;
40
Greg Clayton24bc5d92011-03-30 18:16:51 +000041void
42ProcessInfo::Dump (Stream &s, Platform *platform) const
43{
44 const char *cstr;
Greg Claytonff39f742011-04-01 00:29:43 +000045 if (m_pid != LLDB_INVALID_PROCESS_ID)
46 s.Printf (" pid = %i\n", m_pid);
47
48 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
49 s.Printf (" parent = %i\n", m_parent_pid);
50
51 if (m_executable)
52 {
53 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
54 s.PutCString (" file = ");
55 m_executable.Dump(&s);
56 s.EOL();
57 }
58 const uint32_t argc = m_args.GetSize();
59 if (argc > 0)
60 {
61 for (uint32_t i=0; i<argc; i++)
62 {
63 if (i < 10)
64 s.Printf (" arg[%u] = %s\n", i, m_args.GetStringAtIndex(i));
65 else
66 s.Printf ("arg[%u] = %s\n", i, m_args.GetStringAtIndex(i));
67 }
68 }
69 if (m_arch.IsValid())
70 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
71
Greg Clayton24bc5d92011-03-30 18:16:51 +000072 if (m_real_uid != UINT32_MAX)
73 {
74 cstr = platform->GetUserName (m_real_uid);
Greg Claytonff39f742011-04-01 00:29:43 +000075 s.Printf (" uid = %-5u (%s)\n", m_real_uid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000076 }
77 if (m_real_gid != UINT32_MAX)
78 {
79 cstr = platform->GetGroupName (m_real_gid);
Greg Claytonff39f742011-04-01 00:29:43 +000080 s.Printf (" gid = %-5u (%s)\n", m_real_gid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000081 }
82 if (m_effective_uid != UINT32_MAX)
83 {
84 cstr = platform->GetUserName (m_effective_uid);
Greg Claytonff39f742011-04-01 00:29:43 +000085 s.Printf (" euid = %-5u (%s)\n", m_effective_uid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000086 }
87 if (m_effective_gid != UINT32_MAX)
88 {
89 cstr = platform->GetGroupName (m_effective_gid);
Greg Claytonff39f742011-04-01 00:29:43 +000090 s.Printf (" egid = %-5u (%s)\n", m_effective_gid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000091 }
92}
93
94void
Greg Claytonff39f742011-04-01 00:29:43 +000095ProcessInfo::DumpTableHeader (Stream &s, Platform *platform, bool verbose)
Greg Clayton24bc5d92011-03-30 18:16:51 +000096{
Greg Claytonff39f742011-04-01 00:29:43 +000097 if (verbose)
98 {
99 s.PutCString ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE NAME\n");
100 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
101 }
102 else
103 {
104 s.PutCString ("PID PARENT USER ARCH NAME\n");
105 s.PutCString ("====== ====== ========== ======= ============================\n");
106 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000107}
108
109void
Greg Claytonff39f742011-04-01 00:29:43 +0000110ProcessInfo::DumpAsTableRow (Stream &s, Platform *platform, bool verbose) const
Greg Clayton24bc5d92011-03-30 18:16:51 +0000111{
112 if (m_pid != LLDB_INVALID_PROCESS_ID)
113 {
114 const char *cstr;
115 s.Printf ("%-6u %-6u ", m_pid, m_parent_pid);
116
Greg Clayton24bc5d92011-03-30 18:16:51 +0000117
Greg Claytonff39f742011-04-01 00:29:43 +0000118 if (verbose)
119 {
120 cstr = platform->GetUserName (m_real_uid);
121 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
122 s.Printf ("%-10s ", cstr);
123 else
124 s.Printf ("%-10u ", m_real_uid);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000125
Greg Claytonff39f742011-04-01 00:29:43 +0000126 cstr = platform->GetGroupName (m_real_gid);
127 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
128 s.Printf ("%-10s ", cstr);
129 else
130 s.Printf ("%-10u ", m_real_gid);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000131
Greg Claytonff39f742011-04-01 00:29:43 +0000132 cstr = platform->GetUserName (m_effective_uid);
133 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
134 s.Printf ("%-10s ", cstr);
135 else
136 s.Printf ("%-10u ", m_effective_uid);
137
138 cstr = platform->GetGroupName (m_effective_gid);
139 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
140 s.Printf ("%-10s ", cstr);
141 else
142 s.Printf ("%-10u ", m_effective_gid);
143 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
144 }
145 else
146 {
147 s.Printf ("%-10s %.*-7s ",
148 platform->GetUserName (m_effective_uid),
149 (int)m_arch.GetTriple().getArchName().size(),
150 m_arch.GetTriple().getArchName().data());
151 }
152
153 if (verbose)
154 {
155 const uint32_t argc = m_args.GetSize();
156 if (argc > 0)
157 {
158 for (uint32_t i=0; i<argc; i++)
159 {
160 if (i > 0)
161 s.PutChar (' ');
162 s.PutCString (m_args.GetStringAtIndex(i));
163 }
164 }
165 }
166 else
167 {
168 s.PutCString (GetName());
169 }
170
171 s.EOL();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000172 }
173}
174
175bool
176ProcessInfoMatch::NameMatches (const char *process_name) const
177{
178 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
179 return true;
180 const char *match_name = m_match_info.GetName();
181 if (!match_name)
182 return true;
183
184 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
185}
186
187bool
188ProcessInfoMatch::Matches (const ProcessInfo &proc_info) const
189{
190 if (!NameMatches (proc_info.GetName()))
191 return false;
192
193 if (m_match_info.ProcessIDIsValid() &&
194 m_match_info.GetProcessID() != proc_info.GetProcessID())
195 return false;
196
197 if (m_match_info.ParentProcessIDIsValid() &&
198 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
199 return false;
200
201 if (m_match_info.RealUserIDIsValid () &&
202 m_match_info.GetRealUserID() != proc_info.GetRealUserID())
203 return false;
204
205 if (m_match_info.RealGroupIDIsValid () &&
206 m_match_info.GetRealGroupID() != proc_info.GetRealGroupID())
207 return false;
208
209 if (m_match_info.EffectiveUserIDIsValid () &&
210 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
211 return false;
212
213 if (m_match_info.EffectiveGroupIDIsValid () &&
214 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
215 return false;
216
217 if (m_match_info.GetArchitecture().IsValid() &&
218 m_match_info.GetArchitecture() != proc_info.GetArchitecture())
219 return false;
220 return true;
221}
222
223bool
224ProcessInfoMatch::MatchAllProcesses () const
225{
226 if (m_name_match_type != eNameMatchIgnore)
227 return false;
228
229 if (m_match_info.ProcessIDIsValid())
230 return false;
231
232 if (m_match_info.ParentProcessIDIsValid())
233 return false;
234
235 if (m_match_info.RealUserIDIsValid ())
236 return false;
237
238 if (m_match_info.RealGroupIDIsValid ())
239 return false;
240
241 if (m_match_info.EffectiveUserIDIsValid ())
242 return false;
243
244 if (m_match_info.EffectiveGroupIDIsValid ())
245 return false;
246
247 if (m_match_info.GetArchitecture().IsValid())
248 return false;
249
250 if (m_match_all_users)
251 return false;
252
253 return true;
254
255}
256
257void
258ProcessInfoMatch::Clear()
259{
260 m_match_info.Clear();
261 m_name_match_type = eNameMatchIgnore;
262 m_match_all_users = false;
263}
Greg Claytonfd119992011-01-07 06:08:19 +0000264
265//----------------------------------------------------------------------
266// MemoryCache constructor
267//----------------------------------------------------------------------
268Process::MemoryCache::MemoryCache() :
269 m_cache_line_byte_size (512),
270 m_cache_mutex (Mutex::eMutexTypeRecursive),
271 m_cache ()
272{
273}
274
275//----------------------------------------------------------------------
276// Destructor
277//----------------------------------------------------------------------
278Process::MemoryCache::~MemoryCache()
279{
280}
281
282void
283Process::MemoryCache::Clear()
284{
285 Mutex::Locker locker (m_cache_mutex);
286 m_cache.clear();
287}
288
289void
290Process::MemoryCache::Flush (addr_t addr, size_t size)
291{
292 if (size == 0)
293 return;
294
295 const uint32_t cache_line_byte_size = m_cache_line_byte_size;
296 const addr_t end_addr = (addr + size - 1);
297 const addr_t flush_start_addr = addr - (addr % cache_line_byte_size);
298 const addr_t flush_end_addr = end_addr - (end_addr % cache_line_byte_size);
299
300 Mutex::Locker locker (m_cache_mutex);
301 if (m_cache.empty())
302 return;
303
304 assert ((flush_start_addr % cache_line_byte_size) == 0);
305
306 for (addr_t curr_addr = flush_start_addr; curr_addr <= flush_end_addr; curr_addr += cache_line_byte_size)
307 {
308 collection::iterator pos = m_cache.find (curr_addr);
309 if (pos != m_cache.end())
310 m_cache.erase(pos);
311 }
312}
313
314size_t
315Process::MemoryCache::Read
316(
317 Process *process,
318 addr_t addr,
319 void *dst,
320 size_t dst_len,
321 Error &error
322)
323{
324 size_t bytes_left = dst_len;
325 if (dst && bytes_left > 0)
326 {
327 const uint32_t cache_line_byte_size = m_cache_line_byte_size;
328 uint8_t *dst_buf = (uint8_t *)dst;
329 addr_t curr_addr = addr - (addr % cache_line_byte_size);
330 addr_t cache_offset = addr - curr_addr;
331 Mutex::Locker locker (m_cache_mutex);
332
333 while (bytes_left > 0)
334 {
335 collection::const_iterator pos = m_cache.find (curr_addr);
336 collection::const_iterator end = m_cache.end ();
337
338 if (pos != end)
339 {
340 size_t curr_read_size = cache_line_byte_size - cache_offset;
341 if (curr_read_size > bytes_left)
342 curr_read_size = bytes_left;
343
344 memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes() + cache_offset, curr_read_size);
345
346 bytes_left -= curr_read_size;
347 curr_addr += curr_read_size + cache_offset;
348 cache_offset = 0;
349
350 if (bytes_left > 0)
351 {
352 // Get sequential cache page hits
353 for (++pos; (pos != end) && (bytes_left > 0); ++pos)
354 {
355 assert ((curr_addr % cache_line_byte_size) == 0);
356
357 if (pos->first != curr_addr)
358 break;
359
360 curr_read_size = pos->second->GetByteSize();
361 if (curr_read_size > bytes_left)
362 curr_read_size = bytes_left;
363
364 memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes(), curr_read_size);
365
366 bytes_left -= curr_read_size;
367 curr_addr += curr_read_size;
368
369 // We have a cache page that succeeded to read some bytes
370 // but not an entire page. If this happens, we must cap
371 // off how much data we are able to read...
372 if (pos->second->GetByteSize() != cache_line_byte_size)
373 return dst_len - bytes_left;
374 }
375 }
376 }
377
378 // We need to read from the process
379
380 if (bytes_left > 0)
381 {
382 assert ((curr_addr % cache_line_byte_size) == 0);
383 std::auto_ptr<DataBufferHeap> data_buffer_heap_ap(new DataBufferHeap (cache_line_byte_size, 0));
384 size_t process_bytes_read = process->ReadMemoryFromInferior (curr_addr,
385 data_buffer_heap_ap->GetBytes(),
386 data_buffer_heap_ap->GetByteSize(),
387 error);
388 if (process_bytes_read == 0)
389 return dst_len - bytes_left;
390
391 if (process_bytes_read != cache_line_byte_size)
392 data_buffer_heap_ap->SetByteSize (process_bytes_read);
393 m_cache[curr_addr] = DataBufferSP (data_buffer_heap_ap.release());
394 // We have read data and put it into the cache, continue through the
395 // loop again to get the data out of the cache...
396 }
397 }
398 }
399
400 return dst_len - bytes_left;
401}
402
Chris Lattner24943d22010-06-08 16:52:24 +0000403Process*
404Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener)
405{
406 ProcessCreateInstance create_callback = NULL;
407 if (plugin_name)
408 {
409 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name);
410 if (create_callback)
411 {
412 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
413 if (debugger_ap->CanDebug(target))
414 return debugger_ap.release();
415 }
416 }
417 else
418 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000419 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000420 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000421 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
422 if (debugger_ap->CanDebug(target))
423 return debugger_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000424 }
425 }
426 return NULL;
427}
428
429
430//----------------------------------------------------------------------
431// Process constructor
432//----------------------------------------------------------------------
433Process::Process(Target &target, Listener &listener) :
434 UserID (LLDB_INVALID_PROCESS_ID),
Greg Clayton49ce6822010-10-31 03:01:06 +0000435 Broadcaster ("lldb.process"),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000436 ProcessInstanceSettings (*GetSettingsController()),
Chris Lattner24943d22010-06-08 16:52:24 +0000437 m_target (target),
Chris Lattner24943d22010-06-08 16:52:24 +0000438 m_public_state (eStateUnloaded),
439 m_private_state (eStateUnloaded),
440 m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"),
441 m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"),
442 m_private_state_listener ("lldb.process.internal_state_listener"),
443 m_private_state_control_wait(),
444 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
445 m_stop_id (0),
446 m_thread_index_id (0),
447 m_exit_status (-1),
448 m_exit_string (),
449 m_thread_list (this),
450 m_notifications (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000451 m_image_tokens (),
452 m_listener (listener),
453 m_breakpoint_site_list (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000454 m_dynamic_checkers_ap (),
Caroline Tice861efb32010-11-16 05:07:41 +0000455 m_unix_signals (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000456 m_abi_sp (),
Caroline Tice861efb32010-11-16 05:07:41 +0000457 m_process_input_reader (),
Greg Claytona875b642011-01-09 21:07:35 +0000458 m_stdio_communication ("process.stdio"),
Greg Clayton20d338f2010-11-18 05:57:03 +0000459 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonfd119992011-01-07 06:08:19 +0000460 m_stdout_data (),
Jim Inghamc2dc7c82011-01-29 01:49:25 +0000461 m_memory_cache (),
Greg Clayton7e2f91c2011-01-29 07:10:55 +0000462 m_next_event_action_ap()
Chris Lattner24943d22010-06-08 16:52:24 +0000463{
Caroline Tice1ebef442010-09-27 00:30:10 +0000464 UpdateInstanceName();
465
Greg Claytone005f2c2010-11-06 01:53:30 +0000466 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000467 if (log)
468 log->Printf ("%p Process::Process()", this);
469
Greg Clayton49ce6822010-10-31 03:01:06 +0000470 SetEventName (eBroadcastBitStateChanged, "state-changed");
471 SetEventName (eBroadcastBitInterrupt, "interrupt");
472 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
473 SetEventName (eBroadcastBitSTDERR, "stderr-available");
474
Chris Lattner24943d22010-06-08 16:52:24 +0000475 listener.StartListeningForEvents (this,
476 eBroadcastBitStateChanged |
477 eBroadcastBitInterrupt |
478 eBroadcastBitSTDOUT |
479 eBroadcastBitSTDERR);
480
481 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
482 eBroadcastBitStateChanged);
483
484 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
485 eBroadcastInternalStateControlStop |
486 eBroadcastInternalStateControlPause |
487 eBroadcastInternalStateControlResume);
488}
489
490//----------------------------------------------------------------------
491// Destructor
492//----------------------------------------------------------------------
493Process::~Process()
494{
Greg Claytone005f2c2010-11-06 01:53:30 +0000495 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000496 if (log)
497 log->Printf ("%p Process::~Process()", this);
498 StopPrivateStateThread();
499}
500
501void
502Process::Finalize()
503{
504 // Do any cleanup needed prior to being destructed... Subclasses
505 // that override this method should call this superclass method as well.
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000506
507 // We need to destroy the loader before the derived Process class gets destroyed
508 // since it is very likely that undoing the loader will require access to the real process.
509 if (m_dyld_ap.get() != NULL)
510 m_dyld_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000511}
512
513void
514Process::RegisterNotificationCallbacks (const Notifications& callbacks)
515{
516 m_notifications.push_back(callbacks);
517 if (callbacks.initialize != NULL)
518 callbacks.initialize (callbacks.baton, this);
519}
520
521bool
522Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
523{
524 std::vector<Notifications>::iterator pos, end = m_notifications.end();
525 for (pos = m_notifications.begin(); pos != end; ++pos)
526 {
527 if (pos->baton == callbacks.baton &&
528 pos->initialize == callbacks.initialize &&
529 pos->process_state_changed == callbacks.process_state_changed)
530 {
531 m_notifications.erase(pos);
532 return true;
533 }
534 }
535 return false;
536}
537
538void
539Process::SynchronouslyNotifyStateChanged (StateType state)
540{
541 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
542 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
543 {
544 if (notification_pos->process_state_changed)
545 notification_pos->process_state_changed (notification_pos->baton, this, state);
546 }
547}
548
549// FIXME: We need to do some work on events before the general Listener sees them.
550// For instance if we are continuing from a breakpoint, we need to ensure that we do
551// the little "insert real insn, step & stop" trick. But we can't do that when the
552// event is delivered by the broadcaster - since that is done on the thread that is
553// waiting for new events, so if we needed more than one event for our handling, we would
554// stall. So instead we do it when we fetch the event off of the queue.
555//
556
557StateType
558Process::GetNextEvent (EventSP &event_sp)
559{
560 StateType state = eStateInvalid;
561
562 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
563 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
564
565 return state;
566}
567
568
569StateType
570Process::WaitForProcessToStop (const TimeValue *timeout)
571{
572 StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded };
573 return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType));
574}
575
576
577StateType
578Process::WaitForState
579(
580 const TimeValue *timeout,
581 const StateType *match_states, const uint32_t num_match_states
582)
583{
584 EventSP event_sp;
585 uint32_t i;
Greg Claytond8c62532010-10-07 04:19:01 +0000586 StateType state = GetState();
Chris Lattner24943d22010-06-08 16:52:24 +0000587 while (state != eStateInvalid)
588 {
Greg Claytond8c62532010-10-07 04:19:01 +0000589 // If we are exited or detached, we won't ever get back to any
590 // other valid state...
591 if (state == eStateDetached || state == eStateExited)
592 return state;
593
Chris Lattner24943d22010-06-08 16:52:24 +0000594 state = WaitForStateChangedEvents (timeout, event_sp);
595
596 for (i=0; i<num_match_states; ++i)
597 {
598 if (match_states[i] == state)
599 return state;
600 }
601 }
602 return state;
603}
604
Jim Ingham63e24d72010-10-11 23:53:14 +0000605bool
606Process::HijackProcessEvents (Listener *listener)
607{
608 if (listener != NULL)
609 {
610 return HijackBroadcaster(listener, eBroadcastBitStateChanged);
611 }
612 else
613 return false;
614}
615
616void
617Process::RestoreProcessEvents ()
618{
619 RestoreBroadcaster();
620}
621
Jim Inghamf9f40c22011-02-08 05:20:59 +0000622bool
623Process::HijackPrivateProcessEvents (Listener *listener)
624{
625 if (listener != NULL)
626 {
627 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged);
628 }
629 else
630 return false;
631}
632
633void
634Process::RestorePrivateProcessEvents ()
635{
636 m_private_state_broadcaster.RestoreBroadcaster();
637}
638
Chris Lattner24943d22010-06-08 16:52:24 +0000639StateType
640Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
641{
Greg Claytone005f2c2010-11-06 01:53:30 +0000642 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000643
644 if (log)
645 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
646
647 StateType state = eStateInvalid;
Greg Clayton36f63a92010-10-19 03:25:40 +0000648 if (m_listener.WaitForEventForBroadcasterWithType (timeout,
649 this,
650 eBroadcastBitStateChanged,
651 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +0000652 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
653
654 if (log)
655 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
656 __FUNCTION__,
657 timeout,
658 StateAsCString(state));
659 return state;
660}
661
662Event *
663Process::PeekAtStateChangedEvents ()
664{
Greg Claytone005f2c2010-11-06 01:53:30 +0000665 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000666
667 if (log)
668 log->Printf ("Process::%s...", __FUNCTION__);
669
670 Event *event_ptr;
Greg Clayton36f63a92010-10-19 03:25:40 +0000671 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
672 eBroadcastBitStateChanged);
Chris Lattner24943d22010-06-08 16:52:24 +0000673 if (log)
674 {
675 if (event_ptr)
676 {
677 log->Printf ("Process::%s (event_ptr) => %s",
678 __FUNCTION__,
679 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
680 }
681 else
682 {
683 log->Printf ("Process::%s no events found",
684 __FUNCTION__);
685 }
686 }
687 return event_ptr;
688}
689
690StateType
691Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
692{
Greg Claytone005f2c2010-11-06 01:53:30 +0000693 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000694
695 if (log)
696 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
697
698 StateType state = eStateInvalid;
Greg Clayton72e1c782011-01-22 23:43:18 +0000699 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
700 &m_private_state_broadcaster,
701 eBroadcastBitStateChanged,
702 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +0000703 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
704
705 // This is a bit of a hack, but when we wait here we could very well return
706 // to the command-line, and that could disable the log, which would render the
707 // log we got above invalid.
Chris Lattner24943d22010-06-08 16:52:24 +0000708 if (log)
Greg Clayton72e1c782011-01-22 23:43:18 +0000709 {
710 if (state == eStateInvalid)
711 log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout);
712 else
713 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
714 }
Chris Lattner24943d22010-06-08 16:52:24 +0000715 return state;
716}
717
718bool
719Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
720{
Greg Claytone005f2c2010-11-06 01:53:30 +0000721 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000722
723 if (log)
724 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
725
726 if (control_only)
727 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
728 else
729 return m_private_state_listener.WaitForEvent(timeout, event_sp);
730}
731
732bool
733Process::IsRunning () const
734{
735 return StateIsRunningState (m_public_state.GetValue());
736}
737
738int
739Process::GetExitStatus ()
740{
741 if (m_public_state.GetValue() == eStateExited)
742 return m_exit_status;
743 return -1;
744}
745
Greg Clayton638351a2010-12-04 00:10:17 +0000746
747void
748Process::ProcessInstanceSettings::GetHostEnvironmentIfNeeded ()
749{
750 if (m_inherit_host_env && !m_got_host_env)
751 {
752 m_got_host_env = true;
753 StringList host_env;
754 const size_t host_env_count = Host::GetEnvironment (host_env);
755 for (size_t idx=0; idx<host_env_count; idx++)
756 {
757 const char *env_entry = host_env.GetStringAtIndex (idx);
758 if (env_entry)
759 {
Greg Clayton1f3dd642010-12-15 20:52:40 +0000760 const char *equal_pos = ::strchr(env_entry, '=');
Greg Clayton638351a2010-12-04 00:10:17 +0000761 if (equal_pos)
762 {
763 std::string key (env_entry, equal_pos - env_entry);
764 std::string value (equal_pos + 1);
765 if (m_env_vars.find (key) == m_env_vars.end())
766 m_env_vars[key] = value;
767 }
768 }
769 }
770 }
771}
772
773
774size_t
775Process::ProcessInstanceSettings::GetEnvironmentAsArgs (Args &env)
776{
777 GetHostEnvironmentIfNeeded ();
778
779 dictionary::const_iterator pos, end = m_env_vars.end();
780 for (pos = m_env_vars.begin(); pos != end; ++pos)
781 {
782 std::string env_var_equal_value (pos->first);
783 env_var_equal_value.append(1, '=');
784 env_var_equal_value.append (pos->second);
785 env.AppendArgument (env_var_equal_value.c_str());
786 }
787 return env.GetArgumentCount();
788}
789
790
Chris Lattner24943d22010-06-08 16:52:24 +0000791const char *
792Process::GetExitDescription ()
793{
794 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
795 return m_exit_string.c_str();
796 return NULL;
797}
798
Greg Clayton72e1c782011-01-22 23:43:18 +0000799bool
Chris Lattner24943d22010-06-08 16:52:24 +0000800Process::SetExitStatus (int status, const char *cstr)
801{
Greg Clayton68ca8232011-01-25 02:58:48 +0000802 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
803 if (log)
804 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
805 status, status,
806 cstr ? "\"" : "",
807 cstr ? cstr : "NULL",
808 cstr ? "\"" : "");
809
Greg Clayton72e1c782011-01-22 23:43:18 +0000810 // We were already in the exited state
811 if (m_private_state.GetValue() == eStateExited)
Greg Clayton68ca8232011-01-25 02:58:48 +0000812 {
Greg Clayton644ddfb2011-01-26 23:47:29 +0000813 if (log)
814 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton72e1c782011-01-22 23:43:18 +0000815 return false;
Greg Clayton68ca8232011-01-25 02:58:48 +0000816 }
Greg Clayton72e1c782011-01-22 23:43:18 +0000817
818 m_exit_status = status;
819 if (cstr)
820 m_exit_string = cstr;
821 else
822 m_exit_string.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000823
Greg Clayton72e1c782011-01-22 23:43:18 +0000824 DidExit ();
Greg Clayton58e844b2010-12-08 05:08:21 +0000825
Greg Clayton72e1c782011-01-22 23:43:18 +0000826 SetPrivateState (eStateExited);
827 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000828}
829
830// This static callback can be used to watch for local child processes on
831// the current host. The the child process exits, the process will be
832// found in the global target list (we want to be completely sure that the
833// lldb_private::Process doesn't go away before we can deliver the signal.
834bool
835Process::SetProcessExitStatus
836(
837 void *callback_baton,
838 lldb::pid_t pid,
839 int signo, // Zero for no signal
840 int exit_status // Exit value of process if signal is zero
841)
842{
843 if (signo == 0 || exit_status)
844 {
Greg Clayton63094e02010-06-23 01:19:29 +0000845 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner24943d22010-06-08 16:52:24 +0000846 if (target_sp)
847 {
848 ProcessSP process_sp (target_sp->GetProcessSP());
849 if (process_sp)
850 {
851 const char *signal_cstr = NULL;
852 if (signo)
853 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
854
855 process_sp->SetExitStatus (exit_status, signal_cstr);
856 }
857 }
858 return true;
859 }
860 return false;
861}
862
863
864uint32_t
865Process::GetNextThreadIndexID ()
866{
867 return ++m_thread_index_id;
868}
869
870StateType
871Process::GetState()
872{
873 // If any other threads access this we will need a mutex for it
874 return m_public_state.GetValue ();
875}
876
877void
878Process::SetPublicState (StateType new_state)
879{
Greg Clayton68ca8232011-01-25 02:58:48 +0000880 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000881 if (log)
882 log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state));
883 m_public_state.SetValue (new_state);
884}
885
886StateType
887Process::GetPrivateState ()
888{
889 return m_private_state.GetValue();
890}
891
892void
893Process::SetPrivateState (StateType new_state)
894{
Greg Clayton68ca8232011-01-25 02:58:48 +0000895 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +0000896 bool state_changed = false;
897
898 if (log)
899 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
900
901 Mutex::Locker locker(m_private_state.GetMutex());
902
903 const StateType old_state = m_private_state.GetValueNoLock ();
904 state_changed = old_state != new_state;
905 if (state_changed)
906 {
907 m_private_state.SetValueNoLock (new_state);
908 if (StateIsStoppedState(new_state))
909 {
910 m_stop_id++;
Greg Claytonfd119992011-01-07 06:08:19 +0000911 m_memory_cache.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000912 if (log)
913 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id);
914 }
915 // Use our target to get a shared pointer to ourselves...
916 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state));
917 }
918 else
919 {
920 if (log)
921 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state));
922 }
923}
924
925
926uint32_t
927Process::GetStopID() const
928{
929 return m_stop_id;
930}
931
932addr_t
933Process::GetImageInfoAddress()
934{
935 return LLDB_INVALID_ADDRESS;
936}
937
Greg Clayton0baa3942010-11-04 01:54:29 +0000938//----------------------------------------------------------------------
939// LoadImage
940//
941// This function provides a default implementation that works for most
942// unix variants. Any Process subclasses that need to do shared library
943// loading differently should override LoadImage and UnloadImage and
944// do what is needed.
945//----------------------------------------------------------------------
946uint32_t
947Process::LoadImage (const FileSpec &image_spec, Error &error)
948{
949 DynamicLoader *loader = GetDynamicLoader();
950 if (loader)
951 {
952 error = loader->CanLoadImage();
953 if (error.Fail())
954 return LLDB_INVALID_IMAGE_TOKEN;
955 }
956
957 if (error.Success())
958 {
959 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
960 if (thread_sp == NULL)
961 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
962
963 if (thread_sp)
964 {
965 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
966
967 if (frame_sp)
968 {
969 ExecutionContext exe_ctx;
970 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +0000971 bool unwind_on_error = true;
Sean Callanan6a925532011-01-13 08:53:35 +0000972 bool keep_in_memory = false;
Greg Clayton0baa3942010-11-04 01:54:29 +0000973 StreamString expr;
974 char path[PATH_MAX];
975 image_spec.GetPath(path, sizeof(path));
976 expr.Printf("dlopen (\"%s\", 2)", path);
977 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Ingham360f53f2010-11-30 02:22:11 +0000978 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan6a925532011-01-13 08:53:35 +0000979 ClangUserExpression::Evaluate (exe_ctx, keep_in_memory, unwind_on_error, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton0baa3942010-11-04 01:54:29 +0000980 if (result_valobj_sp->GetError().Success())
981 {
982 Scalar scalar;
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000983 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton0baa3942010-11-04 01:54:29 +0000984 {
985 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
986 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
987 {
988 uint32_t image_token = m_image_tokens.size();
989 m_image_tokens.push_back (image_ptr);
990 return image_token;
991 }
992 }
993 }
994 }
995 }
996 }
997 return LLDB_INVALID_IMAGE_TOKEN;
998}
999
1000//----------------------------------------------------------------------
1001// UnloadImage
1002//
1003// This function provides a default implementation that works for most
1004// unix variants. Any Process subclasses that need to do shared library
1005// loading differently should override LoadImage and UnloadImage and
1006// do what is needed.
1007//----------------------------------------------------------------------
1008Error
1009Process::UnloadImage (uint32_t image_token)
1010{
1011 Error error;
1012 if (image_token < m_image_tokens.size())
1013 {
1014 const addr_t image_addr = m_image_tokens[image_token];
1015 if (image_addr == LLDB_INVALID_ADDRESS)
1016 {
1017 error.SetErrorString("image already unloaded");
1018 }
1019 else
1020 {
1021 DynamicLoader *loader = GetDynamicLoader();
1022 if (loader)
1023 error = loader->CanLoadImage();
1024
1025 if (error.Success())
1026 {
1027 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
1028 if (thread_sp == NULL)
1029 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
1030
1031 if (thread_sp)
1032 {
1033 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
1034
1035 if (frame_sp)
1036 {
1037 ExecutionContext exe_ctx;
1038 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +00001039 bool unwind_on_error = true;
Sean Callanan6a925532011-01-13 08:53:35 +00001040 bool keep_in_memory = false;
Greg Clayton0baa3942010-11-04 01:54:29 +00001041 StreamString expr;
1042 expr.Printf("dlclose ((void *)0x%llx)", image_addr);
1043 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Ingham360f53f2010-11-30 02:22:11 +00001044 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan6a925532011-01-13 08:53:35 +00001045 ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, keep_in_memory, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton0baa3942010-11-04 01:54:29 +00001046 if (result_valobj_sp->GetError().Success())
1047 {
1048 Scalar scalar;
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001049 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton0baa3942010-11-04 01:54:29 +00001050 {
1051 if (scalar.UInt(1))
1052 {
1053 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1054 }
1055 else
1056 {
1057 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1058 }
1059 }
1060 }
1061 else
1062 {
1063 error = result_valobj_sp->GetError();
1064 }
1065 }
1066 }
1067 }
1068 }
1069 }
1070 else
1071 {
1072 error.SetErrorString("invalid image token");
1073 }
1074 return error;
1075}
1076
Chris Lattner24943d22010-06-08 16:52:24 +00001077const ABI *
1078Process::GetABI()
1079{
Chris Lattner24943d22010-06-08 16:52:24 +00001080 if (m_abi_sp.get() == NULL)
Greg Clayton395fc332011-02-15 21:59:32 +00001081 m_abi_sp.reset(ABI::FindPlugin(m_target.GetArchitecture()));
Chris Lattner24943d22010-06-08 16:52:24 +00001082
1083 return m_abi_sp.get();
1084}
1085
Jim Ingham642036f2010-09-23 02:01:19 +00001086LanguageRuntime *
1087Process::GetLanguageRuntime(lldb::LanguageType language)
1088{
1089 LanguageRuntimeCollection::iterator pos;
1090 pos = m_language_runtimes.find (language);
1091 if (pos == m_language_runtimes.end())
1092 {
1093 lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language));
1094
1095 m_language_runtimes[language]
1096 = runtime;
1097 return runtime.get();
1098 }
1099 else
1100 return (*pos).second.get();
1101}
1102
1103CPPLanguageRuntime *
1104Process::GetCPPLanguageRuntime ()
1105{
1106 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus);
1107 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
1108 return static_cast<CPPLanguageRuntime *> (runtime);
1109 return NULL;
1110}
1111
1112ObjCLanguageRuntime *
1113Process::GetObjCLanguageRuntime ()
1114{
1115 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC);
1116 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
1117 return static_cast<ObjCLanguageRuntime *> (runtime);
1118 return NULL;
1119}
1120
Chris Lattner24943d22010-06-08 16:52:24 +00001121BreakpointSiteList &
1122Process::GetBreakpointSiteList()
1123{
1124 return m_breakpoint_site_list;
1125}
1126
1127const BreakpointSiteList &
1128Process::GetBreakpointSiteList() const
1129{
1130 return m_breakpoint_site_list;
1131}
1132
1133
1134void
1135Process::DisableAllBreakpointSites ()
1136{
1137 m_breakpoint_site_list.SetEnabledForAll (false);
1138}
1139
1140Error
1141Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
1142{
1143 Error error (DisableBreakpointSiteByID (break_id));
1144
1145 if (error.Success())
1146 m_breakpoint_site_list.Remove(break_id);
1147
1148 return error;
1149}
1150
1151Error
1152Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
1153{
1154 Error error;
1155 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1156 if (bp_site_sp)
1157 {
1158 if (bp_site_sp->IsEnabled())
1159 error = DisableBreakpoint (bp_site_sp.get());
1160 }
1161 else
1162 {
1163 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
1164 }
1165
1166 return error;
1167}
1168
1169Error
1170Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
1171{
1172 Error error;
1173 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1174 if (bp_site_sp)
1175 {
1176 if (!bp_site_sp->IsEnabled())
1177 error = EnableBreakpoint (bp_site_sp.get());
1178 }
1179 else
1180 {
1181 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
1182 }
1183 return error;
1184}
1185
Stephen Wilson3fd1f362010-07-17 00:56:13 +00001186lldb::break_id_t
Chris Lattner24943d22010-06-08 16:52:24 +00001187Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware)
1188{
Greg Claytoneea26402010-09-14 23:36:40 +00001189 const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +00001190 if (load_addr != LLDB_INVALID_ADDRESS)
1191 {
1192 BreakpointSiteSP bp_site_sp;
1193
1194 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
1195 // create a new breakpoint site and add it.
1196
1197 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
1198
1199 if (bp_site_sp)
1200 {
1201 bp_site_sp->AddOwner (owner);
1202 owner->SetBreakpointSite (bp_site_sp);
1203 return bp_site_sp->GetID();
1204 }
1205 else
1206 {
1207 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware));
1208 if (bp_site_sp)
1209 {
1210 if (EnableBreakpoint (bp_site_sp.get()).Success())
1211 {
1212 owner->SetBreakpointSite (bp_site_sp);
1213 return m_breakpoint_site_list.Add (bp_site_sp);
1214 }
1215 }
1216 }
1217 }
1218 // We failed to enable the breakpoint
1219 return LLDB_INVALID_BREAK_ID;
1220
1221}
1222
1223void
1224Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
1225{
1226 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
1227 if (num_owners == 0)
1228 {
1229 DisableBreakpoint(bp_site_sp.get());
1230 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1231 }
1232}
1233
1234
1235size_t
1236Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1237{
1238 size_t bytes_removed = 0;
1239 addr_t intersect_addr;
1240 size_t intersect_size;
1241 size_t opcode_offset;
1242 size_t idx;
1243 BreakpointSiteSP bp;
1244
1245 for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx)
1246 {
1247 if (bp->GetType() == BreakpointSite::eSoftware)
1248 {
1249 if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
1250 {
1251 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1252 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
1253 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1254 size_t buf_offset = intersect_addr - bp_addr;
1255 ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
1256 }
1257 }
1258 }
1259 return bytes_removed;
1260}
1261
1262
Greg Claytonb1888f22011-03-19 01:12:21 +00001263
1264size_t
1265Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
1266{
1267 PlatformSP platform_sp (m_target.GetPlatform());
1268 if (platform_sp)
1269 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
1270 return 0;
1271}
1272
Chris Lattner24943d22010-06-08 16:52:24 +00001273Error
1274Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
1275{
1276 Error error;
1277 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +00001278 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001279 const addr_t bp_addr = bp_site->GetLoadAddress();
1280 if (log)
1281 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr);
1282 if (bp_site->IsEnabled())
1283 {
1284 if (log)
1285 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
1286 return error;
1287 }
1288
1289 if (bp_addr == LLDB_INVALID_ADDRESS)
1290 {
1291 error.SetErrorString("BreakpointSite contains an invalid load address.");
1292 return error;
1293 }
1294 // Ask the lldb::Process subclass to fill in the correct software breakpoint
1295 // trap for the breakpoint site
1296 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
1297
1298 if (bp_opcode_size == 0)
1299 {
1300 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr);
1301 }
1302 else
1303 {
1304 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
1305
1306 if (bp_opcode_bytes == NULL)
1307 {
1308 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
1309 return error;
1310 }
1311
1312 // Save the original opcode by reading it
1313 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
1314 {
1315 // Write a software breakpoint in place of the original opcode
1316 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1317 {
1318 uint8_t verify_bp_opcode_bytes[64];
1319 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1320 {
1321 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
1322 {
1323 bp_site->SetEnabled(true);
1324 bp_site->SetType (BreakpointSite::eSoftware);
1325 if (log)
1326 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS",
1327 bp_site->GetID(),
1328 (uint64_t)bp_addr);
1329 }
1330 else
1331 error.SetErrorString("Failed to verify the breakpoint trap in memory.");
1332 }
1333 else
1334 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
1335 }
1336 else
1337 error.SetErrorString("Unable to write breakpoint trap to memory.");
1338 }
1339 else
1340 error.SetErrorString("Unable to read memory at breakpoint address.");
1341 }
Stephen Wilsonc2b98252011-01-12 04:20:03 +00001342 if (log && error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +00001343 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1344 bp_site->GetID(),
1345 (uint64_t)bp_addr,
1346 error.AsCString());
1347 return error;
1348}
1349
1350Error
1351Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
1352{
1353 Error error;
1354 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +00001355 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001356 addr_t bp_addr = bp_site->GetLoadAddress();
1357 lldb::user_id_t breakID = bp_site->GetID();
1358 if (log)
Stephen Wilson9ff73ed2011-01-14 21:07:07 +00001359 log->Printf ("Process::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001360
1361 if (bp_site->IsHardware())
1362 {
1363 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
1364 }
1365 else if (bp_site->IsEnabled())
1366 {
1367 const size_t break_op_size = bp_site->GetByteSize();
1368 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
1369 if (break_op_size > 0)
1370 {
1371 // Clear a software breakoint instruction
Greg Clayton54e7afa2010-07-09 20:39:50 +00001372 uint8_t curr_break_op[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +00001373 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner24943d22010-06-08 16:52:24 +00001374 bool break_op_found = false;
1375
1376 // Read the breakpoint opcode
1377 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
1378 {
1379 bool verify = false;
1380 // Make sure we have the a breakpoint opcode exists at this address
1381 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
1382 {
1383 break_op_found = true;
1384 // We found a valid breakpoint opcode at this address, now restore
1385 // the saved opcode.
1386 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
1387 {
1388 verify = true;
1389 }
1390 else
1391 error.SetErrorString("Memory write failed when restoring original opcode.");
1392 }
1393 else
1394 {
1395 error.SetErrorString("Original breakpoint trap is no longer in memory.");
1396 // Set verify to true and so we can check if the original opcode has already been restored
1397 verify = true;
1398 }
1399
1400 if (verify)
1401 {
Greg Clayton54e7afa2010-07-09 20:39:50 +00001402 uint8_t verify_opcode[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +00001403 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner24943d22010-06-08 16:52:24 +00001404 // Verify that our original opcode made it back to the inferior
1405 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
1406 {
1407 // compare the memory we just read with the original opcode
1408 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
1409 {
1410 // SUCCESS
1411 bp_site->SetEnabled(false);
1412 if (log)
1413 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
1414 return error;
1415 }
1416 else
1417 {
1418 if (break_op_found)
1419 error.SetErrorString("Failed to restore original opcode.");
1420 }
1421 }
1422 else
1423 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
1424 }
1425 }
1426 else
1427 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
1428 }
1429 }
1430 else
1431 {
1432 if (log)
1433 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
1434 return error;
1435 }
1436
1437 if (log)
1438 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1439 bp_site->GetID(),
1440 (uint64_t)bp_addr,
1441 error.AsCString());
1442 return error;
1443
1444}
1445
Greg Claytonfd119992011-01-07 06:08:19 +00001446// Comment out line below to disable memory caching
1447#define ENABLE_MEMORY_CACHING
1448// Uncomment to verify memory caching works after making changes to caching code
1449//#define VERIFY_MEMORY_READS
1450
1451#if defined (ENABLE_MEMORY_CACHING)
1452
1453#if defined (VERIFY_MEMORY_READS)
Chris Lattner24943d22010-06-08 16:52:24 +00001454
1455size_t
1456Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1457{
Greg Claytonfd119992011-01-07 06:08:19 +00001458 // Memory caching is enabled, with debug verification
1459 if (buf && size)
1460 {
1461 // Uncomment the line below to make sure memory caching is working.
1462 // I ran this through the test suite and got no assertions, so I am
1463 // pretty confident this is working well. If any changes are made to
1464 // memory caching, uncomment the line below and test your changes!
1465
1466 // Verify all memory reads by using the cache first, then redundantly
1467 // reading the same memory from the inferior and comparing to make sure
1468 // everything is exactly the same.
1469 std::string verify_buf (size, '\0');
1470 assert (verify_buf.size() == size);
1471 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
1472 Error verify_error;
1473 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
1474 assert (cache_bytes_read == verify_bytes_read);
1475 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
1476 assert (verify_error.Success() == error.Success());
1477 return cache_bytes_read;
1478 }
1479 return 0;
1480}
1481
1482#else // #if defined (VERIFY_MEMORY_READS)
1483
1484size_t
1485Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1486{
1487 // Memory caching enabled, no verification
1488 return m_memory_cache.Read (this, addr, buf, size, error);
1489}
1490
1491#endif // #else for #if defined (VERIFY_MEMORY_READS)
1492
1493#else // #if defined (ENABLE_MEMORY_CACHING)
1494
1495size_t
1496Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1497{
1498 // Memory caching is disabled
1499 return ReadMemoryFromInferior (addr, buf, size, error);
1500}
1501
1502#endif // #else for #if defined (ENABLE_MEMORY_CACHING)
1503
1504
1505size_t
1506Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
1507{
Chris Lattner24943d22010-06-08 16:52:24 +00001508 if (buf == NULL || size == 0)
1509 return 0;
1510
1511 size_t bytes_read = 0;
1512 uint8_t *bytes = (uint8_t *)buf;
1513
1514 while (bytes_read < size)
1515 {
1516 const size_t curr_size = size - bytes_read;
1517 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
1518 bytes + bytes_read,
1519 curr_size,
1520 error);
1521 bytes_read += curr_bytes_read;
1522 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
1523 break;
1524 }
1525
1526 // Replace any software breakpoint opcodes that fall into this range back
1527 // into "buf" before we return
1528 if (bytes_read > 0)
1529 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
1530 return bytes_read;
1531}
1532
Greg Claytonf72fdee2010-12-16 20:01:20 +00001533uint64_t
1534Process::ReadUnsignedInteger (lldb::addr_t vm_addr, size_t integer_byte_size, Error &error)
1535{
1536 if (integer_byte_size > sizeof(uint64_t))
1537 {
1538 error.SetErrorString ("unsupported integer size");
1539 }
1540 else
1541 {
1542 uint8_t tmp[sizeof(uint64_t)];
Greg Clayton395fc332011-02-15 21:59:32 +00001543 DataExtractor data (tmp,
1544 integer_byte_size,
1545 m_target.GetArchitecture().GetByteOrder(),
1546 m_target.GetArchitecture().GetAddressByteSize());
Greg Claytonf72fdee2010-12-16 20:01:20 +00001547 if (ReadMemory (vm_addr, tmp, integer_byte_size, error) == integer_byte_size)
1548 {
1549 uint32_t offset = 0;
1550 return data.GetMaxU64 (&offset, integer_byte_size);
1551 }
1552 }
1553 // Any plug-in that doesn't return success a memory read with the number
1554 // of bytes that were requested should be setting the error
1555 assert (error.Fail());
1556 return 0;
1557}
1558
Chris Lattner24943d22010-06-08 16:52:24 +00001559size_t
1560Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
1561{
1562 size_t bytes_written = 0;
1563 const uint8_t *bytes = (const uint8_t *)buf;
1564
1565 while (bytes_written < size)
1566 {
1567 const size_t curr_size = size - bytes_written;
1568 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
1569 bytes + bytes_written,
1570 curr_size,
1571 error);
1572 bytes_written += curr_bytes_written;
1573 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
1574 break;
1575 }
1576 return bytes_written;
1577}
1578
1579size_t
1580Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1581{
Greg Claytonfd119992011-01-07 06:08:19 +00001582#if defined (ENABLE_MEMORY_CACHING)
1583 m_memory_cache.Flush (addr, size);
1584#endif
1585
Chris Lattner24943d22010-06-08 16:52:24 +00001586 if (buf == NULL || size == 0)
1587 return 0;
1588 // We need to write any data that would go where any current software traps
1589 // (enabled software breakpoints) any software traps (breakpoints) that we
1590 // may have placed in our tasks memory.
1591
1592 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
1593 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
1594
1595 if (iter == end || iter->second->GetLoadAddress() > addr + size)
1596 return DoWriteMemory(addr, buf, size, error);
1597
1598 BreakpointSiteList::collection::const_iterator pos;
1599 size_t bytes_written = 0;
Greg Clayton54e7afa2010-07-09 20:39:50 +00001600 addr_t intersect_addr = 0;
1601 size_t intersect_size = 0;
1602 size_t opcode_offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001603 const uint8_t *ubuf = (const uint8_t *)buf;
1604
1605 for (pos = iter; pos != end; ++pos)
1606 {
1607 BreakpointSiteSP bp;
1608 bp = pos->second;
1609
1610 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
1611 assert(addr <= intersect_addr && intersect_addr < addr + size);
1612 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
1613 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1614
1615 // Check for bytes before this breakpoint
1616 const addr_t curr_addr = addr + bytes_written;
1617 if (intersect_addr > curr_addr)
1618 {
1619 // There are some bytes before this breakpoint that we need to
1620 // just write to memory
1621 size_t curr_size = intersect_addr - curr_addr;
1622 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
1623 ubuf + bytes_written,
1624 curr_size,
1625 error);
1626 bytes_written += curr_bytes_written;
1627 if (curr_bytes_written != curr_size)
1628 {
1629 // We weren't able to write all of the requested bytes, we
1630 // are done looping and will return the number of bytes that
1631 // we have written so far.
1632 break;
1633 }
1634 }
1635
1636 // Now write any bytes that would cover up any software breakpoints
1637 // directly into the breakpoint opcode buffer
1638 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
1639 bytes_written += intersect_size;
1640 }
1641
1642 // Write any remaining bytes after the last breakpoint if we have any left
1643 if (bytes_written < size)
1644 bytes_written += WriteMemoryPrivate (addr + bytes_written,
1645 ubuf + bytes_written,
1646 size - bytes_written,
1647 error);
1648
1649 return bytes_written;
1650}
1651
1652addr_t
1653Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
1654{
1655 // Fixme: we should track the blocks we've allocated, and clean them up...
1656 // We could even do our own allocator here if that ends up being more efficient.
Greg Clayton2860ba92011-01-23 19:58:49 +00001657 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
1658 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1659 if (log)
Greg Claytonb349adc2011-01-24 06:30:45 +00001660 log->Printf("Process::AllocateMemory(size=%4zu, permissions=%c%c%c) => 0x%16.16llx (m_stop_id = %u)",
Greg Clayton2860ba92011-01-23 19:58:49 +00001661 size,
1662 permissions & ePermissionsReadable ? 'r' : '-',
1663 permissions & ePermissionsWritable ? 'w' : '-',
1664 permissions & ePermissionsExecutable ? 'x' : '-',
1665 (uint64_t)allocated_addr,
1666 m_stop_id);
1667 return allocated_addr;
Chris Lattner24943d22010-06-08 16:52:24 +00001668}
1669
1670Error
1671Process::DeallocateMemory (addr_t ptr)
1672{
Greg Clayton2860ba92011-01-23 19:58:49 +00001673 Error error(DoDeallocateMemory (ptr));
1674
1675 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1676 if (log)
1677 log->Printf("Process::DeallocateMemory(addr=0x%16.16llx) => err = %s (m_stop_id = %u)",
1678 ptr,
1679 error.AsCString("SUCCESS"),
1680 m_stop_id);
1681 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00001682}
1683
1684
1685Error
1686Process::EnableWatchpoint (WatchpointLocation *watchpoint)
1687{
1688 Error error;
1689 error.SetErrorString("watchpoints are not supported");
1690 return error;
1691}
1692
1693Error
1694Process::DisableWatchpoint (WatchpointLocation *watchpoint)
1695{
1696 Error error;
1697 error.SetErrorString("watchpoints are not supported");
1698 return error;
1699}
1700
1701StateType
1702Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
1703{
1704 StateType state;
1705 // Now wait for the process to launch and return control to us, and then
1706 // call DidLaunch:
1707 while (1)
1708 {
Greg Clayton72e1c782011-01-22 23:43:18 +00001709 event_sp.reset();
1710 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
1711
1712 if (StateIsStoppedState(state))
Chris Lattner24943d22010-06-08 16:52:24 +00001713 break;
Greg Clayton72e1c782011-01-22 23:43:18 +00001714
1715 // If state is invalid, then we timed out
1716 if (state == eStateInvalid)
1717 break;
1718
1719 if (event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001720 HandlePrivateEvent (event_sp);
1721 }
1722 return state;
1723}
1724
1725Error
1726Process::Launch
1727(
1728 char const *argv[],
1729 char const *envp[],
Greg Clayton452bf612010-08-31 18:35:14 +00001730 uint32_t launch_flags,
Chris Lattner24943d22010-06-08 16:52:24 +00001731 const char *stdin_path,
1732 const char *stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +00001733 const char *stderr_path,
1734 const char *working_directory
Chris Lattner24943d22010-06-08 16:52:24 +00001735)
1736{
1737 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +00001738 m_abi_sp.reset();
Greg Clayton75c703d2011-02-16 04:46:07 +00001739 m_dyld_ap.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001740 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001741
1742 Module *exe_module = m_target.GetExecutableModule().get();
1743 if (exe_module)
1744 {
1745 char exec_file_path[PATH_MAX];
1746 exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path));
1747 if (exe_module->GetFileSpec().Exists())
1748 {
Greg Claytona2f74232011-02-24 22:24:29 +00001749 if (PrivateStateThreadIsValid ())
1750 PausePrivateStateThread ();
1751
Chris Lattner24943d22010-06-08 16:52:24 +00001752 error = WillLaunch (exe_module);
1753 if (error.Success())
1754 {
Greg Claytond8c62532010-10-07 04:19:01 +00001755 SetPublicState (eStateLaunching);
Chris Lattner24943d22010-06-08 16:52:24 +00001756 // The args coming in should not contain the application name, the
1757 // lldb_private::Process class will add this in case the executable
1758 // gets resolved to a different file than was given on the command
1759 // line (like when an applicaiton bundle is specified and will
1760 // resolve to the contained exectuable file, or the file given was
1761 // a symlink or other file system link that resolves to a different
1762 // file).
1763
1764 // Get the resolved exectuable path
1765
1766 // Make a new argument vector
1767 std::vector<const char *> exec_path_plus_argv;
1768 // Append the resolved executable path
1769 exec_path_plus_argv.push_back (exec_file_path);
1770
1771 // Push all args if there are any
1772 if (argv)
1773 {
1774 for (int i = 0; argv[i]; ++i)
1775 exec_path_plus_argv.push_back(argv[i]);
1776 }
1777
1778 // Push a NULL to terminate the args.
1779 exec_path_plus_argv.push_back(NULL);
1780
1781 // Now launch using these arguments.
Greg Clayton53d68e72010-07-20 22:52:08 +00001782 error = DoLaunch (exe_module,
1783 exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(),
1784 envp,
Greg Clayton452bf612010-08-31 18:35:14 +00001785 launch_flags,
Greg Clayton53d68e72010-07-20 22:52:08 +00001786 stdin_path,
1787 stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +00001788 stderr_path,
1789 working_directory);
Chris Lattner24943d22010-06-08 16:52:24 +00001790
1791 if (error.Fail())
1792 {
1793 if (GetID() != LLDB_INVALID_PROCESS_ID)
1794 {
1795 SetID (LLDB_INVALID_PROCESS_ID);
1796 const char *error_string = error.AsCString();
1797 if (error_string == NULL)
1798 error_string = "launch failed";
1799 SetExitStatus (-1, error_string);
1800 }
1801 }
1802 else
1803 {
1804 EventSP event_sp;
1805 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
1806
1807 if (state == eStateStopped || state == eStateCrashed)
1808 {
Greg Clayton75c703d2011-02-16 04:46:07 +00001809
Chris Lattner24943d22010-06-08 16:52:24 +00001810 DidLaunch ();
1811
Greg Clayton4fdf7602011-03-20 04:57:14 +00001812 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Greg Clayton75c703d2011-02-16 04:46:07 +00001813 if (m_dyld_ap.get())
1814 m_dyld_ap->DidLaunch();
1815
Chris Lattner24943d22010-06-08 16:52:24 +00001816 // This delays passing the stopped event to listeners till DidLaunch gets
1817 // a chance to complete...
1818 HandlePrivateEvent (event_sp);
Greg Claytona2f74232011-02-24 22:24:29 +00001819
1820 if (PrivateStateThreadIsValid ())
1821 ResumePrivateStateThread ();
1822 else
1823 StartPrivateStateThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00001824 }
1825 else if (state == eStateExited)
1826 {
1827 // We exited while trying to launch somehow. Don't call DidLaunch as that's
1828 // not likely to work, and return an invalid pid.
1829 HandlePrivateEvent (event_sp);
1830 }
1831 }
1832 }
1833 }
1834 else
1835 {
1836 error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path);
1837 }
1838 }
1839 return error;
1840}
1841
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001842Process::NextEventAction::EventActionResult
1843Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001844{
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001845 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
1846 switch (state)
Greg Claytonc1d37752010-10-18 01:45:30 +00001847 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001848 case eStateRunning:
Greg Claytona2f74232011-02-24 22:24:29 +00001849 case eStateConnected:
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001850 return eEventActionRetry;
1851
1852 case eStateStopped:
1853 case eStateCrashed:
Jim Ingham7508e732010-08-09 23:31:02 +00001854 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001855 // During attach, prior to sending the eStateStopped event,
1856 // lldb_private::Process subclasses must set the process must set
1857 // the new process ID.
1858 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
Greg Clayton75c703d2011-02-16 04:46:07 +00001859 m_process->CompleteAttach ();
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001860 return eEventActionSuccess;
Jim Ingham7508e732010-08-09 23:31:02 +00001861 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00001862
1863
1864 break;
1865 default:
1866 case eStateExited:
1867 case eStateInvalid:
1868 m_exit_string.assign ("No valid Process");
1869 return eEventActionExit;
1870 break;
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001871 }
1872}
Chris Lattner24943d22010-06-08 16:52:24 +00001873
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001874Process::NextEventAction::EventActionResult
1875Process::AttachCompletionHandler::HandleBeingInterrupted()
1876{
1877 return eEventActionSuccess;
1878}
1879
1880const char *
1881Process::AttachCompletionHandler::GetExitString ()
1882{
1883 return m_exit_string.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +00001884}
1885
1886Error
1887Process::Attach (lldb::pid_t attach_pid)
1888{
1889
Chris Lattner24943d22010-06-08 16:52:24 +00001890 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001891 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00001892
Jim Ingham7508e732010-08-09 23:31:02 +00001893 // Find the process and its architecture. Make sure it matches the architecture
1894 // of the current Target, and if not adjust it.
1895
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001896 ProcessInfo process_info;
Greg Claytonb1888f22011-03-19 01:12:21 +00001897 PlatformSP platform_sp (m_target.GetDebugger().GetPlatformList().GetSelectedPlatform ());
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001898 if (platform_sp)
Jim Ingham7508e732010-08-09 23:31:02 +00001899 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001900 if (platform_sp->GetProcessInfo (attach_pid, process_info))
1901 {
1902 const ArchSpec &process_arch = process_info.GetArchitecture();
1903 if (process_arch.IsValid())
1904 GetTarget().SetArchitecture(process_arch);
1905 }
Jim Ingham7508e732010-08-09 23:31:02 +00001906 }
1907
Greg Clayton75c703d2011-02-16 04:46:07 +00001908 m_dyld_ap.reset();
1909
Greg Clayton54e7afa2010-07-09 20:39:50 +00001910 Error error (WillAttachToProcessWithID(attach_pid));
Chris Lattner24943d22010-06-08 16:52:24 +00001911 if (error.Success())
1912 {
Greg Claytond8c62532010-10-07 04:19:01 +00001913 SetPublicState (eStateAttaching);
1914
Greg Clayton54e7afa2010-07-09 20:39:50 +00001915 error = DoAttachToProcessWithID (attach_pid);
Chris Lattner24943d22010-06-08 16:52:24 +00001916 if (error.Success())
1917 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00001918 SetNextEventAction(new Process::AttachCompletionHandler(this));
1919 StartPrivateStateThread();
Chris Lattner24943d22010-06-08 16:52:24 +00001920 }
1921 else
1922 {
1923 if (GetID() != LLDB_INVALID_PROCESS_ID)
1924 {
1925 SetID (LLDB_INVALID_PROCESS_ID);
1926 const char *error_string = error.AsCString();
1927 if (error_string == NULL)
1928 error_string = "attach failed";
1929
1930 SetExitStatus(-1, error_string);
1931 }
1932 }
1933 }
1934 return error;
1935}
1936
1937Error
1938Process::Attach (const char *process_name, bool wait_for_launch)
1939{
Chris Lattner24943d22010-06-08 16:52:24 +00001940 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00001941 m_process_input_reader.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001942
1943 // Find the process and its architecture. Make sure it matches the architecture
1944 // of the current Target, and if not adjust it.
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001945 Error error;
Jim Ingham7508e732010-08-09 23:31:02 +00001946
Jim Inghamea294182010-08-17 21:54:19 +00001947 if (!wait_for_launch)
Jim Ingham7508e732010-08-09 23:31:02 +00001948 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001949 ProcessInfoList process_infos;
Greg Claytonb1888f22011-03-19 01:12:21 +00001950 PlatformSP platform_sp (m_target.GetDebugger().GetPlatformList().GetSelectedPlatform ());
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001951 if (platform_sp)
Jim Inghamea294182010-08-17 21:54:19 +00001952 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001953 ProcessInfoMatch match_info;
1954 match_info.GetProcessInfo().SetName(process_name);
1955 match_info.SetNameMatchType (eNameMatchEquals);
1956 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001957 if (process_infos.GetSize() > 1)
Chris Lattner24943d22010-06-08 16:52:24 +00001958 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001959 error.SetErrorStringWithFormat ("More than one process named %s\n", process_name);
1960 }
1961 else if (process_infos.GetSize() == 0)
1962 {
1963 error.SetErrorStringWithFormat ("Could not find a process named %s\n", process_name);
1964 }
1965 else
1966 {
1967 ProcessInfo process_info;
1968 if (process_infos.GetInfoAtIndex (0, process_info))
1969 {
1970 const ArchSpec &process_arch = process_info.GetArchitecture();
1971 if (process_arch.IsValid() && process_arch != GetTarget().GetArchitecture())
1972 {
1973 // Set the architecture on the target.
1974 GetTarget().SetArchitecture (process_arch);
1975 }
1976 }
Chris Lattner24943d22010-06-08 16:52:24 +00001977 }
1978 }
1979 else
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001980 {
1981 error.SetErrorString ("Invalid platform");
1982 }
1983 }
1984
1985 if (error.Success())
1986 {
1987 m_dyld_ap.reset();
1988
1989 error = WillAttachToProcessWithName(process_name, wait_for_launch);
1990 if (error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +00001991 {
Greg Claytone4b9c1f2011-03-08 22:40:15 +00001992 SetPublicState (eStateAttaching);
1993 error = DoAttachToProcessWithName (process_name, wait_for_launch);
1994 if (error.Fail())
1995 {
1996 if (GetID() != LLDB_INVALID_PROCESS_ID)
1997 {
1998 SetID (LLDB_INVALID_PROCESS_ID);
1999 const char *error_string = error.AsCString();
2000 if (error_string == NULL)
2001 error_string = "attach failed";
2002
2003 SetExitStatus(-1, error_string);
2004 }
2005 }
2006 else
2007 {
2008 SetNextEventAction(new Process::AttachCompletionHandler(this));
2009 StartPrivateStateThread();
2010 }
Chris Lattner24943d22010-06-08 16:52:24 +00002011 }
2012 }
2013 return error;
2014}
2015
Greg Clayton75c703d2011-02-16 04:46:07 +00002016void
2017Process::CompleteAttach ()
2018{
2019 // Let the process subclass figure out at much as it can about the process
2020 // before we go looking for a dynamic loader plug-in.
2021 DidAttach();
2022
2023 // We have complete the attach, now it is time to find the dynamic loader
2024 // plug-in
Greg Clayton4fdf7602011-03-20 04:57:14 +00002025 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Greg Clayton75c703d2011-02-16 04:46:07 +00002026 if (m_dyld_ap.get())
2027 m_dyld_ap->DidAttach();
2028
2029 // Figure out which one is the executable, and set that in our target:
2030 ModuleList &modules = m_target.GetImages();
2031
2032 size_t num_modules = modules.GetSize();
2033 for (int i = 0; i < num_modules; i++)
2034 {
2035 ModuleSP module_sp (modules.GetModuleAtIndex(i));
2036 if (module_sp->IsExecutable())
2037 {
2038 ModuleSP target_exe_module_sp (m_target.GetExecutableModule());
2039 if (target_exe_module_sp != module_sp)
2040 m_target.SetExecutableModule (module_sp, false);
2041 break;
2042 }
2043 }
2044}
2045
Chris Lattner24943d22010-06-08 16:52:24 +00002046Error
Greg Claytone71e2582011-02-04 01:58:07 +00002047Process::ConnectRemote (const char *remote_url)
2048{
Greg Claytone71e2582011-02-04 01:58:07 +00002049 m_abi_sp.reset();
2050 m_process_input_reader.reset();
2051
2052 // Find the process and its architecture. Make sure it matches the architecture
2053 // of the current Target, and if not adjust it.
2054
2055 Error error (DoConnectRemote (remote_url));
2056 if (error.Success())
2057 {
Greg Claytona2f74232011-02-24 22:24:29 +00002058 if (GetID() != LLDB_INVALID_PROCESS_ID)
2059 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00002060 EventSP event_sp;
2061 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
2062
2063 if (state == eStateStopped || state == eStateCrashed)
2064 {
2065 // If we attached and actually have a process on the other end, then
2066 // this ended up being the equivalent of an attach.
2067 CompleteAttach ();
2068
2069 // This delays passing the stopped event to listeners till
2070 // CompleteAttach gets a chance to complete...
2071 HandlePrivateEvent (event_sp);
2072
2073 }
Greg Claytona2f74232011-02-24 22:24:29 +00002074 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00002075
2076 if (PrivateStateThreadIsValid ())
2077 ResumePrivateStateThread ();
2078 else
2079 StartPrivateStateThread ();
Greg Claytone71e2582011-02-04 01:58:07 +00002080 }
2081 return error;
2082}
2083
2084
2085Error
Chris Lattner24943d22010-06-08 16:52:24 +00002086Process::Resume ()
2087{
Greg Claytone005f2c2010-11-06 01:53:30 +00002088 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002089 if (log)
Jim Inghamac959662011-01-24 06:34:17 +00002090 log->Printf("Process::Resume() m_stop_id = %u, public state: %s private state: %s",
2091 m_stop_id,
2092 StateAsCString(m_public_state.GetValue()),
2093 StateAsCString(m_private_state.GetValue()));
Chris Lattner24943d22010-06-08 16:52:24 +00002094
2095 Error error (WillResume());
2096 // Tell the process it is about to resume before the thread list
2097 if (error.Success())
2098 {
Johnny Chen9c11d472010-12-02 20:53:05 +00002099 // Now let the thread list know we are about to resume so it
Chris Lattner24943d22010-06-08 16:52:24 +00002100 // can let all of our threads know that they are about to be
2101 // resumed. Threads will each be called with
2102 // Thread::WillResume(StateType) where StateType contains the state
2103 // that they are supposed to have when the process is resumed
2104 // (suspended/running/stepping). Threads should also check
2105 // their resume signal in lldb::Thread::GetResumeSignal()
2106 // to see if they are suppoed to start back up with a signal.
2107 if (m_thread_list.WillResume())
2108 {
2109 error = DoResume();
2110 if (error.Success())
2111 {
2112 DidResume();
2113 m_thread_list.DidResume();
Jim Inghamac959662011-01-24 06:34:17 +00002114 if (log)
2115 log->Printf ("Process thinks the process has resumed.");
Chris Lattner24943d22010-06-08 16:52:24 +00002116 }
2117 }
2118 else
2119 {
Jim Inghamac959662011-01-24 06:34:17 +00002120 error.SetErrorStringWithFormat("Process::WillResume() thread list returned false after WillResume");
Chris Lattner24943d22010-06-08 16:52:24 +00002121 }
2122 }
Jim Inghamac959662011-01-24 06:34:17 +00002123 else if (log)
2124 log->Printf ("Process::WillResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner24943d22010-06-08 16:52:24 +00002125 return error;
2126}
2127
2128Error
2129Process::Halt ()
2130{
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002131 // Pause our private state thread so we can ensure no one else eats
2132 // the stop event out from under us.
Jim Inghamf9f40c22011-02-08 05:20:59 +00002133 Listener halt_listener ("lldb.process.halt_listener");
2134 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton20d338f2010-11-18 05:57:03 +00002135
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002136 EventSP event_sp;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002137 Error error (WillHalt());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002138
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002139 if (error.Success())
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002140 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002141
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002142 bool caused_stop = false;
2143
2144 // Ask the process subclass to actually halt our process
2145 error = DoHalt(caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +00002146 if (error.Success())
Jim Ingham3ae449a2010-11-17 02:32:00 +00002147 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002148 if (m_public_state.GetValue() == eStateAttaching)
2149 {
2150 SetExitStatus(SIGKILL, "Cancelled async attach.");
2151 Destroy ();
2152 }
2153 else
Jim Ingham3ae449a2010-11-17 02:32:00 +00002154 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002155 // If "caused_stop" is true, then DoHalt stopped the process. If
2156 // "caused_stop" is false, the process was already stopped.
2157 // If the DoHalt caused the process to stop, then we want to catch
2158 // this event and set the interrupted bool to true before we pass
2159 // this along so clients know that the process was interrupted by
2160 // a halt command.
2161 if (caused_stop)
Greg Clayton20d338f2010-11-18 05:57:03 +00002162 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00002163 // Wait for 1 second for the process to stop.
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002164 TimeValue timeout_time;
2165 timeout_time = TimeValue::Now();
2166 timeout_time.OffsetWithSeconds(1);
Jim Inghamf9f40c22011-02-08 05:20:59 +00002167 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
2168 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002169
Jim Inghamf9f40c22011-02-08 05:20:59 +00002170 if (!got_event || state == eStateInvalid)
Greg Clayton20d338f2010-11-18 05:57:03 +00002171 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002172 // We timeout out and didn't get a stop event...
Jim Inghamf9f40c22011-02-08 05:20:59 +00002173 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton20d338f2010-11-18 05:57:03 +00002174 }
2175 else
2176 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002177 if (StateIsStoppedState (state))
2178 {
2179 // We caused the process to interrupt itself, so mark this
2180 // as such in the stop event so clients can tell an interrupted
2181 // process from a natural stop
2182 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
2183 }
2184 else
2185 {
2186 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2187 if (log)
2188 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
2189 error.SetErrorString ("Did not get stopped event after halt.");
2190 }
Greg Clayton20d338f2010-11-18 05:57:03 +00002191 }
2192 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002193 DidHalt();
Jim Ingham3ae449a2010-11-17 02:32:00 +00002194 }
2195 }
Chris Lattner24943d22010-06-08 16:52:24 +00002196 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002197 // Resume our private state thread before we post the event (if any)
Jim Inghamf9f40c22011-02-08 05:20:59 +00002198 RestorePrivateProcessEvents();
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002199
2200 // Post any event we might have consumed. If all goes well, we will have
2201 // stopped the process, intercepted the event and set the interrupted
2202 // bool in the event. Post it to the private event queue and that will end up
2203 // correctly setting the state.
2204 if (event_sp)
2205 m_private_state_broadcaster.BroadcastEvent(event_sp);
2206
Chris Lattner24943d22010-06-08 16:52:24 +00002207 return error;
2208}
2209
2210Error
2211Process::Detach ()
2212{
2213 Error error (WillDetach());
2214
2215 if (error.Success())
2216 {
2217 DisableAllBreakpointSites();
2218 error = DoDetach();
2219 if (error.Success())
2220 {
2221 DidDetach();
2222 StopPrivateStateThread();
2223 }
2224 }
2225 return error;
2226}
2227
2228Error
2229Process::Destroy ()
2230{
2231 Error error (WillDestroy());
2232 if (error.Success())
2233 {
2234 DisableAllBreakpointSites();
2235 error = DoDestroy();
2236 if (error.Success())
2237 {
2238 DidDestroy();
2239 StopPrivateStateThread();
2240 }
Caroline Tice861efb32010-11-16 05:07:41 +00002241 m_stdio_communication.StopReadThread();
2242 m_stdio_communication.Disconnect();
2243 if (m_process_input_reader && m_process_input_reader->IsActive())
2244 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2245 if (m_process_input_reader)
2246 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00002247 }
2248 return error;
2249}
2250
2251Error
2252Process::Signal (int signal)
2253{
2254 Error error (WillSignal());
2255 if (error.Success())
2256 {
2257 error = DoSignal(signal);
2258 if (error.Success())
2259 DidSignal();
2260 }
2261 return error;
2262}
2263
Greg Clayton395fc332011-02-15 21:59:32 +00002264lldb::ByteOrder
2265Process::GetByteOrder () const
Chris Lattner24943d22010-06-08 16:52:24 +00002266{
Greg Clayton395fc332011-02-15 21:59:32 +00002267 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner24943d22010-06-08 16:52:24 +00002268}
2269
2270uint32_t
Greg Clayton395fc332011-02-15 21:59:32 +00002271Process::GetAddressByteSize () const
Chris Lattner24943d22010-06-08 16:52:24 +00002272{
Greg Clayton395fc332011-02-15 21:59:32 +00002273 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +00002274}
2275
Greg Clayton395fc332011-02-15 21:59:32 +00002276
Chris Lattner24943d22010-06-08 16:52:24 +00002277bool
2278Process::ShouldBroadcastEvent (Event *event_ptr)
2279{
2280 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
2281 bool return_value = true;
Greg Claytone005f2c2010-11-06 01:53:30 +00002282 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002283
2284 switch (state)
2285 {
Greg Claytone71e2582011-02-04 01:58:07 +00002286 case eStateConnected:
Chris Lattner24943d22010-06-08 16:52:24 +00002287 case eStateAttaching:
2288 case eStateLaunching:
2289 case eStateDetached:
2290 case eStateExited:
2291 case eStateUnloaded:
2292 // These events indicate changes in the state of the debugging session, always report them.
2293 return_value = true;
2294 break;
2295 case eStateInvalid:
2296 // We stopped for no apparent reason, don't report it.
2297 return_value = false;
2298 break;
2299 case eStateRunning:
2300 case eStateStepping:
2301 // If we've started the target running, we handle the cases where we
2302 // are already running and where there is a transition from stopped to
2303 // running differently.
2304 // running -> running: Automatically suppress extra running events
2305 // stopped -> running: Report except when there is one or more no votes
2306 // and no yes votes.
2307 SynchronouslyNotifyStateChanged (state);
2308 switch (m_public_state.GetValue())
2309 {
2310 case eStateRunning:
2311 case eStateStepping:
2312 // We always suppress multiple runnings with no PUBLIC stop in between.
2313 return_value = false;
2314 break;
2315 default:
2316 // TODO: make this work correctly. For now always report
2317 // run if we aren't running so we don't miss any runnning
2318 // events. If I run the lldb/test/thread/a.out file and
2319 // break at main.cpp:58, run and hit the breakpoints on
2320 // multiple threads, then somehow during the stepping over
2321 // of all breakpoints no run gets reported.
2322 return_value = true;
2323
2324 // This is a transition from stop to run.
2325 switch (m_thread_list.ShouldReportRun (event_ptr))
2326 {
2327 case eVoteYes:
2328 case eVoteNoOpinion:
2329 return_value = true;
2330 break;
2331 case eVoteNo:
2332 return_value = false;
2333 break;
2334 }
2335 break;
2336 }
2337 break;
2338 case eStateStopped:
2339 case eStateCrashed:
2340 case eStateSuspended:
2341 {
2342 // We've stopped. First see if we're going to restart the target.
2343 // If we are going to stop, then we always broadcast the event.
2344 // 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 +00002345 // If no thread has an opinion, we don't report it.
Jim Ingham3ae449a2010-11-17 02:32:00 +00002346 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner24943d22010-06-08 16:52:24 +00002347 {
Greg Clayton20d338f2010-11-18 05:57:03 +00002348 if (log)
2349 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", event_ptr, StateAsCString(state));
Jim Ingham3ae449a2010-11-17 02:32:00 +00002350 return true;
2351 }
2352 else
2353 {
Chris Lattner24943d22010-06-08 16:52:24 +00002354 RefreshStateAfterStop ();
2355
2356 if (m_thread_list.ShouldStop (event_ptr) == false)
2357 {
2358 switch (m_thread_list.ShouldReportStop (event_ptr))
2359 {
2360 case eVoteYes:
2361 Process::ProcessEventData::SetRestartedInEvent (event_ptr, true);
Johnny Chen028784b2010-10-14 00:54:32 +00002362 // Intentional fall-through here.
Chris Lattner24943d22010-06-08 16:52:24 +00002363 case eVoteNoOpinion:
Chris Lattner24943d22010-06-08 16:52:24 +00002364 case eVoteNo:
2365 return_value = false;
2366 break;
2367 }
2368
2369 if (log)
Jim Ingham3ae449a2010-11-17 02:32:00 +00002370 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state));
Chris Lattner24943d22010-06-08 16:52:24 +00002371 Resume ();
2372 }
2373 else
2374 {
2375 return_value = true;
2376 SynchronouslyNotifyStateChanged (state);
2377 }
2378 }
2379 }
2380 }
2381
2382 if (log)
2383 log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO");
2384 return return_value;
2385}
2386
Chris Lattner24943d22010-06-08 16:52:24 +00002387
2388bool
2389Process::StartPrivateStateThread ()
2390{
Greg Claytone005f2c2010-11-06 01:53:30 +00002391 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002392
2393 if (log)
2394 log->Printf ("Process::%s ( )", __FUNCTION__);
2395
2396 // Create a thread that watches our internal state and controls which
2397 // events make it to clients (into the DCProcess event queue).
Greg Claytona875b642011-01-09 21:07:35 +00002398 char thread_name[1024];
2399 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%i)>", GetID());
2400 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002401 return IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002402}
2403
2404void
2405Process::PausePrivateStateThread ()
2406{
2407 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
2408}
2409
2410void
2411Process::ResumePrivateStateThread ()
2412{
2413 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
2414}
2415
2416void
2417Process::StopPrivateStateThread ()
2418{
2419 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
2420}
2421
2422void
2423Process::ControlPrivateStateThread (uint32_t signal)
2424{
Greg Claytone005f2c2010-11-06 01:53:30 +00002425 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002426
2427 assert (signal == eBroadcastInternalStateControlStop ||
2428 signal == eBroadcastInternalStateControlPause ||
2429 signal == eBroadcastInternalStateControlResume);
2430
2431 if (log)
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002432 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner24943d22010-06-08 16:52:24 +00002433
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002434 // Signal the private state thread. First we should copy this is case the
2435 // thread starts exiting since the private state thread will NULL this out
2436 // when it exits
2437 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton09c81ef2011-02-08 01:34:25 +00002438 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00002439 {
2440 TimeValue timeout_time;
2441 bool timed_out;
2442
2443 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
2444
2445 timeout_time = TimeValue::Now();
2446 timeout_time.OffsetWithSeconds(2);
2447 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
2448 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2449
2450 if (signal == eBroadcastInternalStateControlStop)
2451 {
2452 if (timed_out)
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002453 Host::ThreadCancel (private_state_thread, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +00002454
2455 thread_result_t result = NULL;
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00002456 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Claytonc607d862010-07-22 18:34:21 +00002457 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00002458 }
2459 }
2460}
2461
2462void
2463Process::HandlePrivateEvent (EventSP &event_sp)
2464{
Greg Claytone005f2c2010-11-06 01:53:30 +00002465 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002466
Greg Clayton68ca8232011-01-25 02:58:48 +00002467 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002468
2469 // First check to see if anybody wants a shot at this event:
Jim Ingham68bffc52011-01-29 04:05:41 +00002470 if (m_next_event_action_ap.get() != NULL)
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002471 {
Jim Ingham68bffc52011-01-29 04:05:41 +00002472 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002473 switch (action_result)
2474 {
2475 case NextEventAction::eEventActionSuccess:
2476 SetNextEventAction(NULL);
2477 break;
2478 case NextEventAction::eEventActionRetry:
2479 break;
2480 case NextEventAction::eEventActionExit:
Jim Ingham84c86382011-01-29 01:57:31 +00002481 // Handle Exiting Here. If we already got an exited event,
2482 // we should just propagate it. Otherwise, swallow this event,
2483 // and set our state to exit so the next event will kill us.
2484 if (new_state != eStateExited)
2485 {
2486 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham68bffc52011-01-29 04:05:41 +00002487 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham84c86382011-01-29 01:57:31 +00002488 SetNextEventAction(NULL);
2489 return;
2490 }
2491 SetNextEventAction(NULL);
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002492 break;
2493 }
2494 }
2495
Chris Lattner24943d22010-06-08 16:52:24 +00002496 // See if we should broadcast this state to external clients?
2497 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00002498
2499 if (should_broadcast)
2500 {
2501 if (log)
2502 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002503 log->Printf ("Process::%s (pid = %i) broadcasting new state %s (old state %s) to %s",
2504 __FUNCTION__,
2505 GetID(),
2506 StateAsCString(new_state),
2507 StateAsCString (GetState ()),
2508 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner24943d22010-06-08 16:52:24 +00002509 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00002510 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton68ca8232011-01-25 02:58:48 +00002511 if (StateIsRunningState (new_state))
Caroline Tice861efb32010-11-16 05:07:41 +00002512 PushProcessInputReader ();
2513 else
2514 PopProcessInputReader ();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002515
Chris Lattner24943d22010-06-08 16:52:24 +00002516 BroadcastEvent (event_sp);
2517 }
2518 else
2519 {
2520 if (log)
2521 {
Greg Clayton68ca8232011-01-25 02:58:48 +00002522 log->Printf ("Process::%s (pid = %i) suppressing state %s (old state %s): should_broadcast == false",
2523 __FUNCTION__,
2524 GetID(),
2525 StateAsCString(new_state),
2526 StateAsCString (GetState ()),
2527 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner24943d22010-06-08 16:52:24 +00002528 }
2529 }
2530}
2531
2532void *
2533Process::PrivateStateThread (void *arg)
2534{
2535 Process *proc = static_cast<Process*> (arg);
2536 void *result = proc->RunPrivateStateThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00002537 return result;
2538}
2539
2540void *
2541Process::RunPrivateStateThread ()
2542{
2543 bool control_only = false;
2544 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2545
Greg Claytone005f2c2010-11-06 01:53:30 +00002546 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002547 if (log)
2548 log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID());
2549
2550 bool exit_now = false;
2551 while (!exit_now)
2552 {
2553 EventSP event_sp;
2554 WaitForEventsPrivate (NULL, event_sp, control_only);
2555 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
2556 {
2557 switch (event_sp->GetType())
2558 {
2559 case eBroadcastInternalStateControlStop:
2560 exit_now = true;
2561 continue; // Go to next loop iteration so we exit without
2562 break; // doing any internal state managment below
2563
2564 case eBroadcastInternalStateControlPause:
2565 control_only = true;
2566 break;
2567
2568 case eBroadcastInternalStateControlResume:
2569 control_only = false;
2570 break;
2571 }
Jim Ingham3ae449a2010-11-17 02:32:00 +00002572
Jim Ingham3ae449a2010-11-17 02:32:00 +00002573 if (log)
2574 log->Printf ("Process::%s (arg = %p, pid = %i) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType());
2575
Chris Lattner24943d22010-06-08 16:52:24 +00002576 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham3ae449a2010-11-17 02:32:00 +00002577 continue;
Chris Lattner24943d22010-06-08 16:52:24 +00002578 }
2579
2580
2581 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2582
2583 if (internal_state != eStateInvalid)
2584 {
2585 HandlePrivateEvent (event_sp);
2586 }
2587
Greg Clayton3b2c41c2010-10-18 04:14:23 +00002588 if (internal_state == eStateInvalid ||
2589 internal_state == eStateExited ||
2590 internal_state == eStateDetached )
Jim Ingham3ae449a2010-11-17 02:32:00 +00002591 {
Jim Ingham3ae449a2010-11-17 02:32:00 +00002592 if (log)
2593 log->Printf ("Process::%s (arg = %p, pid = %i) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state));
2594
Chris Lattner24943d22010-06-08 16:52:24 +00002595 break;
Jim Ingham3ae449a2010-11-17 02:32:00 +00002596 }
Chris Lattner24943d22010-06-08 16:52:24 +00002597 }
2598
Caroline Tice926060e2010-10-29 21:48:37 +00002599 // Verify log is still enabled before attempting to write to it...
Chris Lattner24943d22010-06-08 16:52:24 +00002600 if (log)
2601 log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID());
2602
Greg Claytona4881d02011-01-22 07:12:45 +00002603 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
2604 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00002605 return NULL;
2606}
2607
Chris Lattner24943d22010-06-08 16:52:24 +00002608//------------------------------------------------------------------
2609// Process Event Data
2610//------------------------------------------------------------------
2611
2612Process::ProcessEventData::ProcessEventData () :
2613 EventData (),
2614 m_process_sp (),
2615 m_state (eStateInvalid),
Greg Clayton54e7afa2010-07-09 20:39:50 +00002616 m_restarted (false),
Jim Ingham3ae449a2010-11-17 02:32:00 +00002617 m_update_state (false),
2618 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00002619{
2620}
2621
2622Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
2623 EventData (),
2624 m_process_sp (process_sp),
2625 m_state (state),
Greg Clayton54e7afa2010-07-09 20:39:50 +00002626 m_restarted (false),
Jim Ingham3ae449a2010-11-17 02:32:00 +00002627 m_update_state (false),
2628 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00002629{
2630}
2631
2632Process::ProcessEventData::~ProcessEventData()
2633{
2634}
2635
2636const ConstString &
2637Process::ProcessEventData::GetFlavorString ()
2638{
2639 static ConstString g_flavor ("Process::ProcessEventData");
2640 return g_flavor;
2641}
2642
2643const ConstString &
2644Process::ProcessEventData::GetFlavor () const
2645{
2646 return ProcessEventData::GetFlavorString ();
2647}
2648
Chris Lattner24943d22010-06-08 16:52:24 +00002649void
2650Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
2651{
2652 // This function gets called twice for each event, once when the event gets pulled
2653 // off of the private process event queue, and once when it gets pulled off of
2654 // the public event queue. m_update_state is used to distinguish these
2655 // two cases; it is false when we're just pulling it off for private handling,
2656 // and we don't want to do the breakpoint command handling then.
2657
2658 if (!m_update_state)
2659 return;
2660
2661 m_process_sp->SetPublicState (m_state);
2662
2663 // If we're stopped and haven't restarted, then do the breakpoint commands here:
2664 if (m_state == eStateStopped && ! m_restarted)
2665 {
2666 int num_threads = m_process_sp->GetThreadList().GetSize();
2667 int idx;
Greg Clayton643ee732010-08-04 01:40:35 +00002668
Chris Lattner24943d22010-06-08 16:52:24 +00002669 for (idx = 0; idx < num_threads; ++idx)
2670 {
2671 lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx);
2672
Jim Ingham6297a3a2010-10-20 00:39:53 +00002673 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
2674 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002675 {
Jim Ingham6297a3a2010-10-20 00:39:53 +00002676 stop_info_sp->PerformAction(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +00002677 }
2678 }
Greg Clayton643ee732010-08-04 01:40:35 +00002679
Jim Ingham6fb8baa2010-08-10 00:59:59 +00002680 // The stop action might restart the target. If it does, then we want to mark that in the
2681 // event so that whoever is receiving it will know to wait for the running event and reflect
2682 // that state appropriately.
2683
Chris Lattner24943d22010-06-08 16:52:24 +00002684 if (m_process_sp->GetPrivateState() == eStateRunning)
2685 SetRestarted(true);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002686 else
2687 {
2688 // Finally, if we didn't restart, run the Stop Hooks here:
2689 // They might also restart the target, so watch for that.
2690 m_process_sp->GetTarget().RunStopHooks();
2691 if (m_process_sp->GetPrivateState() == eStateRunning)
2692 SetRestarted(true);
2693 }
2694
Chris Lattner24943d22010-06-08 16:52:24 +00002695 }
2696}
2697
2698void
2699Process::ProcessEventData::Dump (Stream *s) const
2700{
2701 if (m_process_sp)
2702 s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID());
2703
2704 s->Printf("state = %s", StateAsCString(GetState()));;
2705}
2706
2707const Process::ProcessEventData *
2708Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
2709{
2710 if (event_ptr)
2711 {
2712 const EventData *event_data = event_ptr->GetData();
2713 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
2714 return static_cast <const ProcessEventData *> (event_ptr->GetData());
2715 }
2716 return NULL;
2717}
2718
2719ProcessSP
2720Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
2721{
2722 ProcessSP process_sp;
2723 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2724 if (data)
2725 process_sp = data->GetProcessSP();
2726 return process_sp;
2727}
2728
2729StateType
2730Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
2731{
2732 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2733 if (data == NULL)
2734 return eStateInvalid;
2735 else
2736 return data->GetState();
2737}
2738
2739bool
2740Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
2741{
2742 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2743 if (data == NULL)
2744 return false;
2745 else
2746 return data->GetRestarted();
2747}
2748
2749void
2750Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
2751{
2752 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2753 if (data != NULL)
2754 data->SetRestarted(new_value);
2755}
2756
2757bool
Jim Ingham3ae449a2010-11-17 02:32:00 +00002758Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
2759{
2760 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2761 if (data == NULL)
2762 return false;
2763 else
2764 return data->GetInterrupted ();
2765}
2766
2767void
2768Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
2769{
2770 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2771 if (data != NULL)
2772 data->SetInterrupted(new_value);
2773}
2774
2775bool
Chris Lattner24943d22010-06-08 16:52:24 +00002776Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
2777{
2778 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
2779 if (data)
2780 {
2781 data->SetUpdateStateOnRemoval();
2782 return true;
2783 }
2784 return false;
2785}
2786
Chris Lattner24943d22010-06-08 16:52:24 +00002787void
Greg Claytona830adb2010-10-04 01:05:56 +00002788Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00002789{
2790 exe_ctx.target = &m_target;
2791 exe_ctx.process = this;
2792 exe_ctx.thread = NULL;
2793 exe_ctx.frame = NULL;
2794}
2795
2796lldb::ProcessSP
2797Process::GetSP ()
2798{
2799 return GetTarget().GetProcessSP();
2800}
2801
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002802//uint32_t
2803//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
2804//{
2805// return 0;
2806//}
2807//
2808//ArchSpec
2809//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
2810//{
2811// return Host::GetArchSpecForExistingProcess (pid);
2812//}
2813//
2814//ArchSpec
2815//Process::GetArchSpecForExistingProcess (const char *process_name)
2816//{
2817// return Host::GetArchSpecForExistingProcess (process_name);
2818//}
2819//
Caroline Tice861efb32010-11-16 05:07:41 +00002820void
2821Process::AppendSTDOUT (const char * s, size_t len)
2822{
Greg Clayton20d338f2010-11-18 05:57:03 +00002823 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Tice861efb32010-11-16 05:07:41 +00002824 m_stdout_data.append (s, len);
2825
Greg Claytonb3781332010-12-05 19:16:56 +00002826 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (GetTarget().GetProcessSP(), GetState()));
Caroline Tice861efb32010-11-16 05:07:41 +00002827}
2828
2829void
2830Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
2831{
2832 Process *process = (Process *) baton;
2833 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
2834}
2835
2836size_t
2837Process::ProcessInputReaderCallback (void *baton,
2838 InputReader &reader,
2839 lldb::InputReaderAction notification,
2840 const char *bytes,
2841 size_t bytes_len)
2842{
2843 Process *process = (Process *) baton;
2844
2845 switch (notification)
2846 {
2847 case eInputReaderActivate:
2848 break;
2849
2850 case eInputReaderDeactivate:
2851 break;
2852
2853 case eInputReaderReactivate:
2854 break;
2855
2856 case eInputReaderGotToken:
2857 {
2858 Error error;
2859 process->PutSTDIN (bytes, bytes_len, error);
2860 }
2861 break;
2862
Caroline Ticec4f55fe2010-11-19 20:47:54 +00002863 case eInputReaderInterrupt:
2864 process->Halt ();
2865 break;
2866
2867 case eInputReaderEndOfFile:
2868 process->AppendSTDOUT ("^D", 2);
2869 break;
2870
Caroline Tice861efb32010-11-16 05:07:41 +00002871 case eInputReaderDone:
2872 break;
2873
2874 }
2875
2876 return bytes_len;
2877}
2878
2879void
2880Process::ResetProcessInputReader ()
2881{
2882 m_process_input_reader.reset();
2883}
2884
2885void
2886Process::SetUpProcessInputReader (int file_descriptor)
2887{
2888 // First set up the Read Thread for reading/handling process I/O
2889
2890 std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true));
2891
2892 if (conn_ap.get())
2893 {
2894 m_stdio_communication.SetConnection (conn_ap.release());
2895 if (m_stdio_communication.IsConnected())
2896 {
2897 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
2898 m_stdio_communication.StartReadThread();
2899
2900 // Now read thread is set up, set up input reader.
2901
2902 if (!m_process_input_reader.get())
2903 {
2904 m_process_input_reader.reset (new InputReader(m_target.GetDebugger()));
2905 Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback,
2906 this,
2907 eInputReaderGranularityByte,
2908 NULL,
2909 NULL,
2910 false));
2911
2912 if (err.Fail())
2913 m_process_input_reader.reset();
2914 }
2915 }
2916 }
2917}
2918
2919void
2920Process::PushProcessInputReader ()
2921{
2922 if (m_process_input_reader && !m_process_input_reader->IsActive())
2923 m_target.GetDebugger().PushInputReader (m_process_input_reader);
2924}
2925
2926void
2927Process::PopProcessInputReader ()
2928{
2929 if (m_process_input_reader && m_process_input_reader->IsActive())
2930 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2931}
2932
Greg Claytond284b662011-02-18 01:44:25 +00002933// The process needs to know about installed plug-ins
Greg Clayton990de7b2010-11-18 23:32:35 +00002934void
Caroline Tice2a456812011-03-10 22:14:10 +00002935Process::SettingsInitialize ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002936{
Greg Claytonb3448432011-03-24 21:19:54 +00002937 static std::vector<OptionEnumValueElement> g_plugins;
Greg Claytond284b662011-02-18 01:44:25 +00002938
2939 int i=0;
2940 const char *name;
2941 OptionEnumValueElement option_enum;
2942 while ((name = PluginManager::GetProcessPluginNameAtIndex (i)) != NULL)
2943 {
2944 if (name)
2945 {
2946 option_enum.value = i;
2947 option_enum.string_value = name;
2948 option_enum.usage = PluginManager::GetProcessPluginDescriptionAtIndex (i);
2949 g_plugins.push_back (option_enum);
2950 }
2951 ++i;
2952 }
2953 option_enum.value = 0;
2954 option_enum.string_value = NULL;
2955 option_enum.usage = NULL;
2956 g_plugins.push_back (option_enum);
2957
2958 for (i=0; (name = SettingsController::instance_settings_table[i].var_name); ++i)
2959 {
2960 if (::strcmp (name, "plugin") == 0)
2961 {
2962 SettingsController::instance_settings_table[i].enum_values = &g_plugins[0];
2963 break;
2964 }
2965 }
Greg Clayton990de7b2010-11-18 23:32:35 +00002966 UserSettingsControllerSP &usc = GetSettingsController();
2967 usc.reset (new SettingsController);
2968 UserSettingsController::InitializeSettingsController (usc,
2969 SettingsController::global_settings_table,
2970 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00002971
2972 // Now call SettingsInitialize() for each 'child' of Process settings
2973 Thread::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00002974}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002975
Greg Clayton990de7b2010-11-18 23:32:35 +00002976void
Caroline Tice2a456812011-03-10 22:14:10 +00002977Process::SettingsTerminate ()
Greg Claytond284b662011-02-18 01:44:25 +00002978{
Caroline Tice2a456812011-03-10 22:14:10 +00002979 // Must call SettingsTerminate() on each 'child' of Process settings before terminating Process settings.
2980
2981 Thread::SettingsTerminate ();
2982
2983 // Now terminate Process Settings.
2984
Greg Clayton990de7b2010-11-18 23:32:35 +00002985 UserSettingsControllerSP &usc = GetSettingsController();
2986 UserSettingsController::FinalizeSettingsController (usc);
2987 usc.reset();
2988}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002989
Greg Clayton990de7b2010-11-18 23:32:35 +00002990UserSettingsControllerSP &
2991Process::GetSettingsController ()
2992{
2993 static UserSettingsControllerSP g_settings_controller;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00002994 return g_settings_controller;
2995}
2996
Caroline Tice1ebef442010-09-27 00:30:10 +00002997void
2998Process::UpdateInstanceName ()
2999{
3000 ModuleSP module_sp = GetTarget().GetExecutableModule();
3001 if (module_sp)
3002 {
3003 StreamString sstr;
3004 sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString());
3005
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003006 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
Caroline Tice1ebef442010-09-27 00:30:10 +00003007 sstr.GetData());
3008 }
3009}
3010
Greg Clayton427f2902010-12-14 02:59:59 +00003011ExecutionResults
Jim Ingham360f53f2010-11-30 02:22:11 +00003012Process::RunThreadPlan (ExecutionContext &exe_ctx,
3013 lldb::ThreadPlanSP &thread_plan_sp,
3014 bool stop_others,
3015 bool try_all_threads,
3016 bool discard_on_error,
3017 uint32_t single_thread_timeout_usec,
3018 Stream &errors)
3019{
3020 ExecutionResults return_value = eExecutionSetupError;
3021
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003022 if (thread_plan_sp.get() == NULL)
3023 {
3024 errors.Printf("RunThreadPlan called with empty thread plan.");
Greg Claytonb3448432011-03-24 21:19:54 +00003025 return eExecutionSetupError;
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003026 }
3027
Jim Inghamac959662011-01-24 06:34:17 +00003028 if (m_private_state.GetValue() != eStateStopped)
3029 {
3030 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Greg Claytonb3448432011-03-24 21:19:54 +00003031 return eExecutionSetupError;
Jim Inghamac959662011-01-24 06:34:17 +00003032 }
3033
Jim Ingham360f53f2010-11-30 02:22:11 +00003034 // Save this value for restoration of the execution context after we run
3035 uint32_t tid = exe_ctx.thread->GetIndexID();
3036
3037 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
3038 // so we should arrange to reset them as well.
3039
3040 lldb::ThreadSP selected_thread_sp = exe_ctx.process->GetThreadList().GetSelectedThread();
3041 lldb::StackFrameSP selected_frame_sp;
3042
3043 uint32_t selected_tid;
3044 if (selected_thread_sp != NULL)
3045 {
3046 selected_tid = selected_thread_sp->GetIndexID();
3047 selected_frame_sp = selected_thread_sp->GetSelectedFrame();
3048 }
3049 else
3050 {
3051 selected_tid = LLDB_INVALID_THREAD_ID;
3052 }
3053
3054 exe_ctx.thread->QueueThreadPlan(thread_plan_sp, true);
3055
Jim Ingham6ae318c2011-01-23 21:14:08 +00003056 Listener listener("lldb.process.listener.run-thread-plan");
Jim Inghamf9f40c22011-02-08 05:20:59 +00003057
3058 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
3059 // restored on exit to the function.
3060
3061 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Jim Inghamac959662011-01-24 06:34:17 +00003062
Jim Ingham6ae318c2011-01-23 21:14:08 +00003063 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003064 if (log)
3065 {
3066 StreamString s;
3067 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Jim Inghamf9f40c22011-02-08 05:20:59 +00003068 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4x to run thread plan \"%s\".",
3069 exe_ctx.thread->GetIndexID(),
3070 exe_ctx.thread->GetID(),
3071 s.GetData());
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003072 }
3073
Jim Inghamf9f40c22011-02-08 05:20:59 +00003074 bool got_event;
3075 lldb::EventSP event_sp;
3076 lldb::StateType stop_state = lldb::eStateInvalid;
Jim Ingham360f53f2010-11-30 02:22:11 +00003077
3078 TimeValue* timeout_ptr = NULL;
3079 TimeValue real_timeout;
3080
Jim Inghamf9f40c22011-02-08 05:20:59 +00003081 bool first_timeout = true;
3082 bool do_resume = true;
Jim Ingham360f53f2010-11-30 02:22:11 +00003083
Jim Ingham360f53f2010-11-30 02:22:11 +00003084 while (1)
3085 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003086 // We usually want to resume the process if we get to the top of the loop.
3087 // The only exception is if we get two running events with no intervening
3088 // stop, which can happen, we will just wait for then next stop event.
Jim Ingham360f53f2010-11-30 02:22:11 +00003089
Jim Inghamf9f40c22011-02-08 05:20:59 +00003090 if (do_resume)
Jim Ingham360f53f2010-11-30 02:22:11 +00003091 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003092 // Do the initial resume and wait for the running event before going further.
3093
3094 Error resume_error = exe_ctx.process->Resume ();
3095 if (!resume_error.Success())
3096 {
3097 errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString());
Greg Claytonb3448432011-03-24 21:19:54 +00003098 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003099 break;
3100 }
3101
3102 real_timeout = TimeValue::Now();
3103 real_timeout.OffsetWithMicroSeconds(500000);
3104 timeout_ptr = &real_timeout;
3105
3106 got_event = listener.WaitForEvent(NULL, event_sp);
3107 if (!got_event)
3108 {
3109 if (log)
3110 log->Printf("Didn't get any event after initial resume, exiting.");
3111
3112 errors.Printf("Didn't get any event after initial resume, exiting.");
Greg Claytonb3448432011-03-24 21:19:54 +00003113 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003114 break;
3115 }
3116
3117 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3118 if (stop_state != eStateRunning)
3119 {
3120 if (log)
3121 log->Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state));
3122
3123 errors.Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state));
Greg Claytonb3448432011-03-24 21:19:54 +00003124 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003125 break;
3126 }
3127
3128 if (log)
3129 log->Printf ("Resuming succeeded.");
3130 // We need to call the function synchronously, so spin waiting for it to return.
3131 // If we get interrupted while executing, we're going to lose our context, and
3132 // won't be able to gather the result at this point.
3133 // We set the timeout AFTER the resume, since the resume takes some time and we
3134 // don't want to charge that to the timeout.
3135
3136 if (single_thread_timeout_usec != 0)
3137 {
3138 real_timeout = TimeValue::Now();
3139 if (first_timeout)
3140 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
3141 else
3142 real_timeout.OffsetWithSeconds(10);
3143
3144 timeout_ptr = &real_timeout;
3145 }
3146 }
3147 else
3148 {
3149 if (log)
3150 log->Printf ("Handled an extra running event.");
3151 do_resume = true;
3152 }
3153
3154 // Now wait for the process to stop again:
3155 stop_state = lldb::eStateInvalid;
3156 event_sp.reset();
3157 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
3158
3159 if (got_event)
3160 {
3161 if (event_sp.get())
3162 {
3163 bool keep_going = false;
3164 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3165 if (log)
3166 log->Printf("In while loop, got event: %s.", StateAsCString(stop_state));
3167
3168 switch (stop_state)
3169 {
3170 case lldb::eStateStopped:
3171 // Yay, we're done.
3172 if (log)
3173 log->Printf ("Execution completed successfully.");
Greg Claytonb3448432011-03-24 21:19:54 +00003174 return_value = eExecutionCompleted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003175 break;
3176 case lldb::eStateCrashed:
3177 if (log)
3178 log->Printf ("Execution crashed.");
Greg Claytonb3448432011-03-24 21:19:54 +00003179 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003180 break;
3181 case lldb::eStateRunning:
3182 do_resume = false;
3183 keep_going = true;
3184 break;
3185 default:
3186 if (log)
3187 log->Printf("Execution stopped with unexpected state: %s.", StateAsCString(stop_state));
Greg Claytonb3448432011-03-24 21:19:54 +00003188 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003189 break;
3190 }
3191 if (keep_going)
3192 continue;
3193 else
3194 break;
3195 }
3196 else
3197 {
3198 if (log)
3199 log->Printf ("got_event was true, but the event pointer was null. How odd...");
Greg Claytonb3448432011-03-24 21:19:54 +00003200 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003201 break;
3202 }
3203 }
3204 else
3205 {
3206 // If we didn't get an event that means we've timed out...
3207 // We will interrupt the process here. Depending on what we were asked to do we will
3208 // either exit, or try with all threads running for the same timeout.
Jim Ingham360f53f2010-11-30 02:22:11 +00003209 // Not really sure what to do if Halt fails here...
Jim Inghamf9f40c22011-02-08 05:20:59 +00003210
Stephen Wilsonc2b98252011-01-12 04:20:03 +00003211 if (log) {
Jim Ingham360f53f2010-11-30 02:22:11 +00003212 if (try_all_threads)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003213 {
3214 if (first_timeout)
3215 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
3216 "trying with all threads enabled.",
3217 single_thread_timeout_usec);
3218 else
3219 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
3220 "and timeout: %d timed out.",
3221 single_thread_timeout_usec);
3222 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003223 else
Jim Inghamf9f40c22011-02-08 05:20:59 +00003224 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
3225 "halt and abandoning execution.",
Jim Ingham360f53f2010-11-30 02:22:11 +00003226 single_thread_timeout_usec);
Stephen Wilsonc2b98252011-01-12 04:20:03 +00003227 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003228
Jim Inghamc556b462011-01-22 01:30:53 +00003229 Error halt_error = exe_ctx.process->Halt();
Jim Inghamc556b462011-01-22 01:30:53 +00003230 if (halt_error.Success())
Jim Ingham360f53f2010-11-30 02:22:11 +00003231 {
Jim Ingham360f53f2010-11-30 02:22:11 +00003232 if (log)
Greg Clayton68ca8232011-01-25 02:58:48 +00003233 log->Printf ("Process::RunThreadPlan(): Halt succeeded.");
Jim Ingham360f53f2010-11-30 02:22:11 +00003234
Jim Inghamf9f40c22011-02-08 05:20:59 +00003235 // If halt succeeds, it always produces a stopped event. Wait for that:
3236
3237 real_timeout = TimeValue::Now();
3238 real_timeout.OffsetWithMicroSeconds(500000);
3239
3240 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Jim Ingham360f53f2010-11-30 02:22:11 +00003241
3242 if (got_event)
3243 {
3244 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3245 if (log)
3246 {
Greg Clayton68ca8232011-01-25 02:58:48 +00003247 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
Jim Inghamf9f40c22011-02-08 05:20:59 +00003248 if (stop_state == lldb::eStateStopped
3249 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
Jim Ingham360f53f2010-11-30 02:22:11 +00003250 log->Printf (" Event was the Halt interruption event.");
3251 }
3252
Jim Inghamf9f40c22011-02-08 05:20:59 +00003253 if (stop_state == lldb::eStateStopped)
Jim Ingham360f53f2010-11-30 02:22:11 +00003254 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003255 // Between the time we initiated the Halt and the time we delivered it, the process could have
3256 // already finished its job. Check that here:
Jim Ingham360f53f2010-11-30 02:22:11 +00003257
Jim Inghamf9f40c22011-02-08 05:20:59 +00003258 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3259 {
3260 if (log)
3261 log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
3262 "Exiting wait loop.");
Greg Claytonb3448432011-03-24 21:19:54 +00003263 return_value = eExecutionCompleted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003264 break;
3265 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003266
Jim Inghamf9f40c22011-02-08 05:20:59 +00003267 if (!try_all_threads)
3268 {
3269 if (log)
3270 log->Printf ("try_all_threads was false, we stopped so now we're quitting.");
Greg Claytonb3448432011-03-24 21:19:54 +00003271 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003272 break;
3273 }
3274
3275 if (first_timeout)
3276 {
3277 // Set all the other threads to run, and return to the top of the loop, which will continue;
3278 first_timeout = false;
3279 thread_plan_sp->SetStopOthers (false);
3280 if (log)
3281 log->Printf ("Process::RunThreadPlan(): About to resume.");
3282
3283 continue;
3284 }
3285 else
3286 {
3287 // Running all threads failed, so return Interrupted.
3288 if (log)
3289 log->Printf("Process::RunThreadPlan(): running all threads timed out.");
Greg Claytonb3448432011-03-24 21:19:54 +00003290 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003291 break;
3292 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003293 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00003294 }
3295 else
3296 { if (log)
3297 log->Printf("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
3298 "I'm getting out of here passing Interrupted.");
Greg Claytonb3448432011-03-24 21:19:54 +00003299 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003300 break;
Jim Ingham360f53f2010-11-30 02:22:11 +00003301 }
3302 }
Jim Inghamc556b462011-01-22 01:30:53 +00003303 else
3304 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003305 // This branch is to work around some problems with gdb-remote's Halt. It is a little racy, and can return
3306 // an error from halt, but if you wait a bit you'll get a stopped event anyway.
Jim Inghamc556b462011-01-22 01:30:53 +00003307 if (log)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003308 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.",
3309 halt_error.AsCString());
3310 real_timeout = TimeValue::Now();
3311 real_timeout.OffsetWithMicroSeconds(500000);
3312 timeout_ptr = &real_timeout;
3313 got_event = listener.WaitForEvent(&real_timeout, event_sp);
3314 if (!got_event || event_sp.get() == NULL)
Jim Ingham6ae318c2011-01-23 21:14:08 +00003315 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003316 // This is not going anywhere, bag out.
3317 if (log)
3318 log->Printf ("Process::RunThreadPlan(): halt failed: and waiting for the stopped event failed.");
Greg Claytonb3448432011-03-24 21:19:54 +00003319 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003320 break;
Jim Ingham6ae318c2011-01-23 21:14:08 +00003321 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00003322 else
3323 {
3324 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3325 if (log)
3326 log->Printf ("Process::RunThreadPlan(): halt failed: but then I got a stopped event. Whatever...");
3327 if (stop_state == lldb::eStateStopped)
3328 {
3329 // Between the time we initiated the Halt and the time we delivered it, the process could have
3330 // already finished its job. Check that here:
3331
3332 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3333 {
3334 if (log)
3335 log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
3336 "Exiting wait loop.");
Greg Claytonb3448432011-03-24 21:19:54 +00003337 return_value = eExecutionCompleted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003338 break;
3339 }
3340
3341 if (first_timeout)
3342 {
3343 // Set all the other threads to run, and return to the top of the loop, which will continue;
3344 first_timeout = false;
3345 thread_plan_sp->SetStopOthers (false);
3346 if (log)
3347 log->Printf ("Process::RunThreadPlan(): About to resume.");
3348
3349 continue;
3350 }
3351 else
3352 {
3353 // Running all threads failed, so return Interrupted.
3354 if (log)
3355 log->Printf("Process::RunThreadPlan(): running all threads timed out.");
Greg Claytonb3448432011-03-24 21:19:54 +00003356 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003357 break;
3358 }
3359 }
3360 else
3361 {
3362 log->Printf ("Process::RunThreadPlan(): halt failed, I waited and didn't get"
3363 " a stopped event, instead got %s.", StateAsCString(stop_state));
Greg Claytonb3448432011-03-24 21:19:54 +00003364 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003365 break;
3366 }
3367 }
Jim Inghamc556b462011-01-22 01:30:53 +00003368 }
3369
Jim Ingham360f53f2010-11-30 02:22:11 +00003370 }
3371
Jim Inghamf9f40c22011-02-08 05:20:59 +00003372 } // END WAIT LOOP
3373
3374 // Now do some processing on the results of the run:
3375 if (return_value == eExecutionInterrupted)
3376 {
Jim Ingham360f53f2010-11-30 02:22:11 +00003377 if (log)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003378 {
3379 StreamString s;
3380 if (event_sp)
3381 event_sp->Dump (&s);
3382 else
3383 {
3384 log->Printf ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
3385 }
3386
3387 StreamString ts;
3388
3389 const char *event_explanation;
3390
3391 do
3392 {
3393 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
3394
3395 if (!event_data)
3396 {
3397 event_explanation = "<no event data>";
3398 break;
3399 }
3400
3401 Process *process = event_data->GetProcessSP().get();
3402
3403 if (!process)
3404 {
3405 event_explanation = "<no process>";
3406 break;
3407 }
3408
3409 ThreadList &thread_list = process->GetThreadList();
3410
3411 uint32_t num_threads = thread_list.GetSize();
3412 uint32_t thread_index;
3413
3414 ts.Printf("<%u threads> ", num_threads);
3415
3416 for (thread_index = 0;
3417 thread_index < num_threads;
3418 ++thread_index)
3419 {
3420 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
3421
3422 if (!thread)
3423 {
3424 ts.Printf("<?> ");
3425 continue;
3426 }
3427
3428 ts.Printf("<0x%4.4x ", thread->GetID());
3429 RegisterContext *register_context = thread->GetRegisterContext().get();
3430
3431 if (register_context)
3432 ts.Printf("[ip 0x%llx] ", register_context->GetPC());
3433 else
3434 ts.Printf("[ip unknown] ");
3435
3436 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
3437 if (stop_info_sp)
3438 {
3439 const char *stop_desc = stop_info_sp->GetDescription();
3440 if (stop_desc)
3441 ts.PutCString (stop_desc);
3442 }
3443 ts.Printf(">");
3444 }
3445
3446 event_explanation = ts.GetData();
3447 } while (0);
3448
3449 if (log)
3450 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
3451
3452 if (discard_on_error && thread_plan_sp)
3453 {
3454 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3455 }
3456 }
3457 }
3458 else if (return_value == eExecutionSetupError)
3459 {
3460 if (log)
3461 log->Printf("Process::RunThreadPlan(): execution set up error.");
3462
3463 if (discard_on_error && thread_plan_sp)
3464 {
3465 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3466 }
3467 }
3468 else
3469 {
Jim Ingham360f53f2010-11-30 02:22:11 +00003470 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3471 {
Greg Clayton68ca8232011-01-25 02:58:48 +00003472 if (log)
3473 log->Printf("Process::RunThreadPlan(): thread plan is done");
Greg Claytonb3448432011-03-24 21:19:54 +00003474 return_value = eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +00003475 }
3476 else if (exe_ctx.thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
3477 {
Greg Clayton68ca8232011-01-25 02:58:48 +00003478 if (log)
3479 log->Printf("Process::RunThreadPlan(): thread plan was discarded");
Greg Claytonb3448432011-03-24 21:19:54 +00003480 return_value = eExecutionDiscarded;
Jim Ingham360f53f2010-11-30 02:22:11 +00003481 }
3482 else
3483 {
3484 if (log)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003485 log->Printf("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Ingham360f53f2010-11-30 02:22:11 +00003486 if (discard_on_error && thread_plan_sp)
3487 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003488 if (log)
3489 log->Printf("Process::RunThreadPlan(): discarding thread plan 'cause discard_on_error is set.");
Jim Ingham360f53f2010-11-30 02:22:11 +00003490 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3491 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003492 }
3493 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00003494
Jim Ingham360f53f2010-11-30 02:22:11 +00003495 // Thread we ran the function in may have gone away because we ran the target
3496 // Check that it's still there.
3497 exe_ctx.thread = exe_ctx.process->GetThreadList().FindThreadByIndexID(tid, true).get();
3498 if (exe_ctx.thread)
3499 exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex(0).get();
3500
3501 // Also restore the current process'es selected frame & thread, since this function calling may
3502 // be done behind the user's back.
3503
3504 if (selected_tid != LLDB_INVALID_THREAD_ID)
3505 {
3506 if (exe_ctx.process->GetThreadList().SetSelectedThreadByIndexID (selected_tid))
3507 {
3508 // We were able to restore the selected thread, now restore the frame:
3509 exe_ctx.process->GetThreadList().GetSelectedThread()->SetSelectedFrame(selected_frame_sp.get());
3510 }
3511 }
3512
3513 return return_value;
3514}
3515
3516const char *
3517Process::ExecutionResultAsCString (ExecutionResults result)
3518{
3519 const char *result_name;
3520
3521 switch (result)
3522 {
Greg Claytonb3448432011-03-24 21:19:54 +00003523 case eExecutionCompleted:
Jim Ingham360f53f2010-11-30 02:22:11 +00003524 result_name = "eExecutionCompleted";
3525 break;
Greg Claytonb3448432011-03-24 21:19:54 +00003526 case eExecutionDiscarded:
Jim Ingham360f53f2010-11-30 02:22:11 +00003527 result_name = "eExecutionDiscarded";
3528 break;
Greg Claytonb3448432011-03-24 21:19:54 +00003529 case eExecutionInterrupted:
Jim Ingham360f53f2010-11-30 02:22:11 +00003530 result_name = "eExecutionInterrupted";
3531 break;
Greg Claytonb3448432011-03-24 21:19:54 +00003532 case eExecutionSetupError:
Jim Ingham360f53f2010-11-30 02:22:11 +00003533 result_name = "eExecutionSetupError";
3534 break;
Greg Claytonb3448432011-03-24 21:19:54 +00003535 case eExecutionTimedOut:
Jim Ingham360f53f2010-11-30 02:22:11 +00003536 result_name = "eExecutionTimedOut";
3537 break;
3538 }
3539 return result_name;
3540}
3541
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003542//--------------------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00003543// class Process::SettingsController
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003544//--------------------------------------------------------------
3545
Greg Claytond0a5a232010-09-19 02:33:57 +00003546Process::SettingsController::SettingsController () :
Caroline Tice5bc8c972010-09-20 20:44:43 +00003547 UserSettingsController ("process", Target::GetSettingsController())
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003548{
Greg Clayton638351a2010-12-04 00:10:17 +00003549 m_default_settings.reset (new ProcessInstanceSettings (*this,
3550 false,
Caroline Tice004afcb2010-09-08 17:48:55 +00003551 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003552}
3553
Greg Claytond0a5a232010-09-19 02:33:57 +00003554Process::SettingsController::~SettingsController ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003555{
3556}
3557
3558lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +00003559Process::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003560{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003561 ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*GetSettingsController(),
3562 false,
3563 instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003564 lldb::InstanceSettingsSP new_settings_sp (new_settings);
3565 return new_settings_sp;
3566}
3567
3568//--------------------------------------------------------------
3569// class ProcessInstanceSettings
3570//--------------------------------------------------------------
3571
Greg Clayton638351a2010-12-04 00:10:17 +00003572ProcessInstanceSettings::ProcessInstanceSettings
3573(
3574 UserSettingsController &owner,
3575 bool live_instance,
3576 const char *name
3577) :
3578 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003579 m_run_args (),
3580 m_env_vars (),
3581 m_input_path (),
3582 m_output_path (),
3583 m_error_path (),
Caroline Ticebd666012010-12-03 18:46:09 +00003584 m_disable_aslr (true),
Greg Clayton638351a2010-12-04 00:10:17 +00003585 m_disable_stdio (false),
3586 m_inherit_host_env (true),
3587 m_got_host_env (false)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003588{
Caroline Tice396704b2010-09-09 18:26:37 +00003589 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
3590 // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers.
3591 // 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 +00003592 // This is true for CreateInstanceName() too.
3593
3594 if (GetInstanceName () == InstanceSettings::InvalidName())
3595 {
3596 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
3597 m_owner.RegisterInstanceSettings (this);
3598 }
Caroline Tice396704b2010-09-09 18:26:37 +00003599
3600 if (live_instance)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003601 {
3602 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3603 CopyInstanceSettings (pending_settings,false);
Caroline Tice396704b2010-09-09 18:26:37 +00003604 //m_owner.RemovePendingSettings (m_instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003605 }
3606}
3607
3608ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003609 InstanceSettings (*Process::GetSettingsController(), CreateInstanceName().AsCString()),
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003610 m_run_args (rhs.m_run_args),
3611 m_env_vars (rhs.m_env_vars),
3612 m_input_path (rhs.m_input_path),
3613 m_output_path (rhs.m_output_path),
3614 m_error_path (rhs.m_error_path),
Caroline Ticebd666012010-12-03 18:46:09 +00003615 m_disable_aslr (rhs.m_disable_aslr),
3616 m_disable_stdio (rhs.m_disable_stdio)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003617{
3618 if (m_instance_name != InstanceSettings::GetDefaultName())
3619 {
3620 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3621 CopyInstanceSettings (pending_settings,false);
3622 m_owner.RemovePendingSettings (m_instance_name);
3623 }
3624}
3625
3626ProcessInstanceSettings::~ProcessInstanceSettings ()
3627{
3628}
3629
3630ProcessInstanceSettings&
3631ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs)
3632{
3633 if (this != &rhs)
3634 {
3635 m_run_args = rhs.m_run_args;
3636 m_env_vars = rhs.m_env_vars;
3637 m_input_path = rhs.m_input_path;
3638 m_output_path = rhs.m_output_path;
3639 m_error_path = rhs.m_error_path;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003640 m_disable_aslr = rhs.m_disable_aslr;
Caroline Ticebd666012010-12-03 18:46:09 +00003641 m_disable_stdio = rhs.m_disable_stdio;
Greg Clayton638351a2010-12-04 00:10:17 +00003642 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003643 }
3644
3645 return *this;
3646}
3647
3648
3649void
3650ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
3651 const char *index_value,
3652 const char *value,
3653 const ConstString &instance_name,
3654 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00003655 VarSetOperationType op,
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003656 Error &err,
3657 bool pending)
3658{
3659 if (var_name == RunArgsVarName())
3660 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
3661 else if (var_name == EnvVarsVarName())
Greg Clayton638351a2010-12-04 00:10:17 +00003662 {
3663 GetHostEnvironmentIfNeeded ();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003664 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
Greg Clayton638351a2010-12-04 00:10:17 +00003665 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003666 else if (var_name == InputPathVarName())
3667 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
3668 else if (var_name == OutputPathVarName())
3669 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
3670 else if (var_name == ErrorPathVarName())
3671 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003672 else if (var_name == DisableASLRVarName())
3673 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err);
Caroline Ticebd666012010-12-03 18:46:09 +00003674 else if (var_name == DisableSTDIOVarName ())
3675 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, err);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003676}
3677
3678void
3679ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
3680 bool pending)
3681{
3682 if (new_settings.get() == NULL)
3683 return;
3684
3685 ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get();
3686
3687 m_run_args = new_process_settings->m_run_args;
3688 m_env_vars = new_process_settings->m_env_vars;
3689 m_input_path = new_process_settings->m_input_path;
3690 m_output_path = new_process_settings->m_output_path;
3691 m_error_path = new_process_settings->m_error_path;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003692 m_disable_aslr = new_process_settings->m_disable_aslr;
Caroline Ticebd666012010-12-03 18:46:09 +00003693 m_disable_stdio = new_process_settings->m_disable_stdio;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003694}
3695
Caroline Ticebcb5b452010-09-20 21:37:42 +00003696bool
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003697ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
3698 const ConstString &var_name,
Caroline Tice5bc8c972010-09-20 20:44:43 +00003699 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00003700 Error *err)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003701{
3702 if (var_name == RunArgsVarName())
3703 {
3704 if (m_run_args.GetArgumentCount() > 0)
Greg Claytonc14069e2010-09-14 03:47:41 +00003705 {
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003706 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
3707 value.AppendString (m_run_args.GetArgumentAtIndex (i));
Greg Claytonc14069e2010-09-14 03:47:41 +00003708 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003709 }
3710 else if (var_name == EnvVarsVarName())
3711 {
Greg Clayton638351a2010-12-04 00:10:17 +00003712 GetHostEnvironmentIfNeeded ();
3713
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003714 if (m_env_vars.size() > 0)
3715 {
3716 std::map<std::string, std::string>::iterator pos;
3717 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
3718 {
3719 StreamString value_str;
3720 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
3721 value.AppendString (value_str.GetData());
3722 }
3723 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003724 }
3725 else if (var_name == InputPathVarName())
3726 {
3727 value.AppendString (m_input_path.c_str());
3728 }
3729 else if (var_name == OutputPathVarName())
3730 {
3731 value.AppendString (m_output_path.c_str());
3732 }
3733 else if (var_name == ErrorPathVarName())
3734 {
3735 value.AppendString (m_error_path.c_str());
3736 }
Greg Claytona99b0bf2010-12-04 00:12:24 +00003737 else if (var_name == InheritHostEnvVarName())
3738 {
3739 if (m_inherit_host_env)
3740 value.AppendString ("true");
3741 else
3742 value.AppendString ("false");
3743 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003744 else if (var_name == DisableASLRVarName())
3745 {
3746 if (m_disable_aslr)
3747 value.AppendString ("true");
3748 else
3749 value.AppendString ("false");
3750 }
Caroline Ticebd666012010-12-03 18:46:09 +00003751 else if (var_name == DisableSTDIOVarName())
3752 {
3753 if (m_disable_stdio)
3754 value.AppendString ("true");
3755 else
3756 value.AppendString ("false");
3757 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003758 else
Caroline Ticebcb5b452010-09-20 21:37:42 +00003759 {
3760 if (err)
3761 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
3762 return false;
3763 }
3764 return true;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003765}
3766
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003767const ConstString
3768ProcessInstanceSettings::CreateInstanceName ()
3769{
3770 static int instance_count = 1;
3771 StreamString sstr;
3772
3773 sstr.Printf ("process_%d", instance_count);
3774 ++instance_count;
3775
3776 const ConstString ret_val (sstr.GetData());
3777 return ret_val;
3778}
3779
3780const ConstString &
3781ProcessInstanceSettings::RunArgsVarName ()
3782{
3783 static ConstString run_args_var_name ("run-args");
3784
3785 return run_args_var_name;
3786}
3787
3788const ConstString &
3789ProcessInstanceSettings::EnvVarsVarName ()
3790{
3791 static ConstString env_vars_var_name ("env-vars");
3792
3793 return env_vars_var_name;
3794}
3795
3796const ConstString &
Greg Clayton638351a2010-12-04 00:10:17 +00003797ProcessInstanceSettings::InheritHostEnvVarName ()
3798{
3799 static ConstString g_name ("inherit-env");
3800
3801 return g_name;
3802}
3803
3804const ConstString &
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003805ProcessInstanceSettings::InputPathVarName ()
3806{
3807 static ConstString input_path_var_name ("input-path");
3808
3809 return input_path_var_name;
3810}
3811
3812const ConstString &
3813ProcessInstanceSettings::OutputPathVarName ()
3814{
Caroline Tice87097232010-09-07 18:35:40 +00003815 static ConstString output_path_var_name ("output-path");
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003816
3817 return output_path_var_name;
3818}
3819
3820const ConstString &
3821ProcessInstanceSettings::ErrorPathVarName ()
3822{
Caroline Tice87097232010-09-07 18:35:40 +00003823 static ConstString error_path_var_name ("error-path");
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003824
3825 return error_path_var_name;
3826}
3827
3828const ConstString &
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003829ProcessInstanceSettings::DisableASLRVarName ()
3830{
3831 static ConstString disable_aslr_var_name ("disable-aslr");
3832
3833 return disable_aslr_var_name;
3834}
3835
Caroline Ticebd666012010-12-03 18:46:09 +00003836const ConstString &
3837ProcessInstanceSettings::DisableSTDIOVarName ()
3838{
3839 static ConstString disable_stdio_var_name ("disable-stdio");
3840
3841 return disable_stdio_var_name;
3842}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003843
3844//--------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00003845// SettingsController Variable Tables
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003846//--------------------------------------------------
3847
3848SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00003849Process::SettingsController::global_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003850{
3851 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
3852 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
3853};
3854
3855
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003856SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00003857Process::SettingsController::instance_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003858{
Greg Clayton638351a2010-12-04 00:10:17 +00003859 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
3860 { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
3861 { "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." },
3862 { "inherit-env", eSetVarTypeBoolean, "true", NULL, false, false, "Inherit the environment from the process that is running LLDB." },
Greg Claytonde915be2011-01-23 05:56:20 +00003863 { "input-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for reading its input." },
3864 { "output-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writing its output." },
3865 { "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 +00003866 { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
Greg Clayton638351a2010-12-04 00:10:17 +00003867 { "disable-aslr", eSetVarTypeBoolean, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
3868 { "disable-stdio", eSetVarTypeBoolean, "false", NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" },
3869 { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003870};
3871
3872
Jim Ingham7508e732010-08-09 23:31:02 +00003873