blob: 79310dd11ad5ec3f9f15cb856eec82bd1b4a266d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Process.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Process.h"
11
12#include "lldb/lldb-private-log.h"
13
14#include "lldb/Breakpoint/StoppointCallbackContext.h"
15#include "lldb/Breakpoint/BreakpointLocation.h"
16#include "lldb/Core/Event.h"
Caroline Ticeef5c6d02010-11-16 05:07:41 +000017#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Debugger.h"
Caroline Ticeef5c6d02010-11-16 05:07:41 +000019#include "lldb/Core/InputReader.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Log.h"
21#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/State.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000023#include "lldb/Expression/ClangUserExpression.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000024#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Host/Host.h"
26#include "lldb/Target/ABI.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000027#include "lldb/Target/DynamicLoader.h"
Jim Ingham22777012010-09-23 02:01:19 +000028#include "lldb/Target/LanguageRuntime.h"
29#include "lldb/Target/CPPLanguageRuntime.h"
30#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytone996fd32011-03-08 22:40:15 +000031#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000033#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Target/Target.h"
35#include "lldb/Target/TargetList.h"
36#include "lldb/Target/Thread.h"
37#include "lldb/Target/ThreadPlan.h"
38
39using namespace lldb;
40using namespace lldb_private;
41
Greg Clayton32e0a752011-03-30 18:16:51 +000042void
Greg Clayton8b82f082011-04-12 05:54:46 +000043ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const
Greg Clayton32e0a752011-03-30 18:16:51 +000044{
45 const char *cstr;
Greg Clayton95bf0fd2011-04-01 00:29:43 +000046 if (m_pid != LLDB_INVALID_PROCESS_ID)
47 s.Printf (" pid = %i\n", m_pid);
48
49 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
50 s.Printf (" parent = %i\n", m_parent_pid);
51
52 if (m_executable)
53 {
54 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
55 s.PutCString (" file = ");
56 m_executable.Dump(&s);
57 s.EOL();
58 }
Greg Clayton8b82f082011-04-12 05:54:46 +000059 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +000060 if (argc > 0)
61 {
62 for (uint32_t i=0; i<argc; i++)
63 {
Greg Clayton8b82f082011-04-12 05:54:46 +000064 const char *arg = m_arguments.GetArgumentAtIndex(i);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000065 if (i < 10)
Greg Clayton8b82f082011-04-12 05:54:46 +000066 s.Printf (" arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000067 else
Greg Clayton8b82f082011-04-12 05:54:46 +000068 s.Printf ("arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000069 }
70 }
Greg Clayton8b82f082011-04-12 05:54:46 +000071
72 const uint32_t envc = m_environment.GetArgumentCount();
73 if (envc > 0)
74 {
75 for (uint32_t i=0; i<envc; i++)
76 {
77 const char *env = m_environment.GetArgumentAtIndex(i);
78 if (i < 10)
79 s.Printf (" env[%u] = %s\n", i, env);
80 else
81 s.Printf ("env[%u] = %s\n", i, env);
82 }
83 }
84
Greg Clayton95bf0fd2011-04-01 00:29:43 +000085 if (m_arch.IsValid())
86 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
87
Greg Clayton8b82f082011-04-12 05:54:46 +000088 if (m_uid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +000089 {
Greg Clayton8b82f082011-04-12 05:54:46 +000090 cstr = platform->GetUserName (m_uid);
91 s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +000092 }
Greg Clayton8b82f082011-04-12 05:54:46 +000093 if (m_gid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +000094 {
Greg Clayton8b82f082011-04-12 05:54:46 +000095 cstr = platform->GetGroupName (m_gid);
96 s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +000097 }
Greg Clayton8b82f082011-04-12 05:54:46 +000098 if (m_euid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +000099 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000100 cstr = platform->GetUserName (m_euid);
101 s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000102 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000103 if (m_egid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000104 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000105 cstr = platform->GetGroupName (m_egid);
106 s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000107 }
108}
109
110void
Greg Clayton8b82f082011-04-12 05:54:46 +0000111ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose)
Greg Clayton32e0a752011-03-30 18:16:51 +0000112{
Greg Clayton8b82f082011-04-12 05:54:46 +0000113 const char *label;
114 if (show_args || verbose)
115 label = "ARGUMENTS";
116 else
117 label = "NAME";
118
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000119 if (verbose)
120 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000121 s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000122 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
123 }
124 else
125 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000126 s.Printf ("PID PARENT USER ARCH %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000127 s.PutCString ("====== ====== ========== ======= ============================\n");
128 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000129}
130
131void
Greg Clayton8b82f082011-04-12 05:54:46 +0000132ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000133{
134 if (m_pid != LLDB_INVALID_PROCESS_ID)
135 {
136 const char *cstr;
137 s.Printf ("%-6u %-6u ", m_pid, m_parent_pid);
138
Greg Clayton32e0a752011-03-30 18:16:51 +0000139
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000140 if (verbose)
141 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000142 cstr = platform->GetUserName (m_uid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000143 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
144 s.Printf ("%-10s ", cstr);
145 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000146 s.Printf ("%-10u ", m_uid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000147
Greg Clayton8b82f082011-04-12 05:54:46 +0000148 cstr = platform->GetGroupName (m_gid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000149 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
150 s.Printf ("%-10s ", cstr);
151 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000152 s.Printf ("%-10u ", m_gid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000153
Greg Clayton8b82f082011-04-12 05:54:46 +0000154 cstr = platform->GetUserName (m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000155 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
156 s.Printf ("%-10s ", cstr);
157 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000158 s.Printf ("%-10u ", m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000159
Greg Clayton8b82f082011-04-12 05:54:46 +0000160 cstr = platform->GetGroupName (m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000161 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
162 s.Printf ("%-10s ", cstr);
163 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000164 s.Printf ("%-10u ", m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000165 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
166 }
167 else
168 {
169 s.Printf ("%-10s %.*-7s ",
Greg Clayton8b82f082011-04-12 05:54:46 +0000170 platform->GetUserName (m_euid),
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000171 (int)m_arch.GetTriple().getArchName().size(),
172 m_arch.GetTriple().getArchName().data());
173 }
174
Greg Clayton8b82f082011-04-12 05:54:46 +0000175 if (verbose || show_args)
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000176 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000177 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000178 if (argc > 0)
179 {
180 for (uint32_t i=0; i<argc; i++)
181 {
182 if (i > 0)
183 s.PutChar (' ');
Greg Clayton8b82f082011-04-12 05:54:46 +0000184 s.PutCString (m_arguments.GetArgumentAtIndex(i));
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000185 }
186 }
187 }
188 else
189 {
190 s.PutCString (GetName());
191 }
192
193 s.EOL();
Greg Clayton32e0a752011-03-30 18:16:51 +0000194 }
195}
196
Greg Clayton8b82f082011-04-12 05:54:46 +0000197
198void
199ProcessInfo::SetArgumentsFromArgs (const Args& args,
200 bool first_arg_is_executable,
201 bool first_arg_is_executable_and_argument)
202{
203 // Copy all arguments
204 m_arguments = args;
205
206 // Is the first argument the executable?
207 if (first_arg_is_executable)
208 {
209 const char *first_arg = args.GetArgumentAtIndex (0);
210 if (first_arg)
211 {
212 // Yes the first argument is an executable, set it as the executable
213 // in the launch options. Don't resolve the file path as the path
214 // could be a remote platform path
215 const bool resolve = false;
216 m_executable.SetFile(first_arg, resolve);
217
218 // If argument zero is an executable and shouldn't be included
219 // in the arguments, remove it from the front of the arguments
220 if (first_arg_is_executable_and_argument == false)
221 m_arguments.DeleteArgumentAtIndex (0);
222 }
223 }
224}
225
Greg Clayton32e0a752011-03-30 18:16:51 +0000226bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000227ProcessLaunchInfo::FileAction::Open (int fd, const char *path, bool read, bool write)
228{
229 if ((read || write) && fd >= 0 && path && path[0])
230 {
231 m_action = eFileActionOpen;
232 m_fd = fd;
233 if (read && write)
234 m_arg = O_RDWR;
235 else if (read)
236 m_arg = O_RDONLY;
237 else
238 m_arg = O_WRONLY;
239 m_path.assign (path);
240 return true;
241 }
242 else
243 {
244 Clear();
245 }
246 return false;
247}
248
249bool
250ProcessLaunchInfo::FileAction::Close (int fd)
251{
252 Clear();
253 if (fd >= 0)
254 {
255 m_action = eFileActionClose;
256 m_fd = fd;
257 }
258 return m_fd >= 0;
259}
260
261
262bool
263ProcessLaunchInfo::FileAction::Duplicate (int fd, int dup_fd)
264{
265 Clear();
266 if (fd >= 0 && dup_fd >= 0)
267 {
268 m_action = eFileActionDuplicate;
269 m_fd = fd;
270 m_arg = dup_fd;
271 }
272 return m_fd >= 0;
273}
274
275
276
277bool
278ProcessLaunchInfo::FileAction::AddPosixSpawnFileAction (posix_spawn_file_actions_t *file_actions,
279 const FileAction *info,
280 Log *log,
281 Error& error)
282{
283 if (info == NULL)
284 return false;
285
286 switch (info->m_action)
287 {
288 case eFileActionNone:
289 error.Clear();
290 break;
291
292 case eFileActionClose:
293 if (info->m_fd == -1)
294 error.SetErrorString ("invalid fd for posix_spawn_file_actions_addclose(...)");
295 else
296 {
297 error.SetError (::posix_spawn_file_actions_addclose (file_actions, info->m_fd),
298 eErrorTypePOSIX);
299 if (log && (error.Fail() || log))
300 error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)",
301 file_actions, info->m_fd);
302 }
303 break;
304
305 case eFileActionDuplicate:
306 if (info->m_fd == -1)
307 error.SetErrorString ("invalid fd for posix_spawn_file_actions_adddup2(...)");
308 else if (info->m_arg == -1)
309 error.SetErrorString ("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
310 else
311 {
312 error.SetError (::posix_spawn_file_actions_adddup2 (file_actions, info->m_fd, info->m_arg),
313 eErrorTypePOSIX);
314 if (log && (error.Fail() || log))
315 error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
316 file_actions, info->m_fd, info->m_arg);
317 }
318 break;
319
320 case eFileActionOpen:
321 if (info->m_fd == -1)
322 error.SetErrorString ("invalid fd in posix_spawn_file_actions_addopen(...)");
323 else
324 {
325 int oflag = info->m_arg;
326 mode_t mode = 0;
327
328 error.SetError (::posix_spawn_file_actions_addopen (file_actions,
329 info->m_fd,
330 info->m_path.c_str(),
331 oflag,
332 mode),
333 eErrorTypePOSIX);
334 if (error.Fail() || log)
335 error.PutToLog(log,
336 "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)",
337 file_actions, info->m_fd, info->m_path.c_str(), oflag, mode);
338 }
339 break;
340
341 default:
342 error.SetErrorStringWithFormat ("invalid file action: %i", info->m_action);
343 break;
344 }
345 return error.Success();
346}
347
348Error
Greg Claytonf6b8b582011-04-13 00:18:08 +0000349ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Clayton8b82f082011-04-12 05:54:46 +0000350{
351 Error error;
352 char short_option = (char) m_getopt_table[option_idx].val;
353
354 switch (short_option)
355 {
356 case 's': // Stop at program entry point
357 launch_info.GetFlags().Set (eLaunchFlagStopAtEntry);
358 break;
359
360 case 'e': // STDERR for read + write
361 {
362 ProcessLaunchInfo::FileAction action;
363 if (action.Open(STDERR_FILENO, option_arg, true, true))
364 launch_info.AppendFileAction (action);
365 }
366 break;
367
368 case 'i': // STDIN for read only
369 {
370 ProcessLaunchInfo::FileAction action;
371 if (action.Open(STDIN_FILENO, option_arg, true, false))
372 launch_info.AppendFileAction (action);
373 }
374 break;
375
376 case 'o': // Open STDOUT for write only
377 {
378 ProcessLaunchInfo::FileAction action;
379 if (action.Open(STDOUT_FILENO, option_arg, false, true))
380 launch_info.AppendFileAction (action);
381 }
382 break;
383
384 case 'p': // Process plug-in name
385 launch_info.SetProcessPluginName (option_arg);
386 break;
387
388 case 'n': // Disable STDIO
389 {
390 ProcessLaunchInfo::FileAction action;
391 if (action.Open(STDERR_FILENO, "/dev/null", true, true))
392 launch_info.AppendFileAction (action);
393 if (action.Open(STDOUT_FILENO, "/dev/null", false, true))
394 launch_info.AppendFileAction (action);
395 if (action.Open(STDIN_FILENO, "/dev/null", true, false))
396 launch_info.AppendFileAction (action);
397 }
398 break;
399
400 case 'w':
401 launch_info.SetWorkingDirectory (option_arg);
402 break;
403
404 case 't': // Open process in new terminal window
405 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
406 break;
407
408 case 'a':
409 launch_info.GetArchitecture().SetTriple (option_arg,
410 m_interpreter.GetPlatform(true).get());
411 break;
412
413 case 'A':
414 launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
415 break;
416
417 case 'v':
418 launch_info.GetEnvironmentEntries().AppendArgument(option_arg);
419 break;
420
421 default:
422 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
423 break;
424
425 }
426 return error;
427}
428
429OptionDefinition
430ProcessLaunchCommandOptions::g_option_table[] =
431{
432{ LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', no_argument, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."},
433{ LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', no_argument, NULL, 0, eArgTypeNone, "Disable address space layout randomization when launching a process."},
434{ LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
435{ LLDB_OPT_SET_ALL, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
436{ LLDB_OPT_SET_ALL, false, "arch", 'a', required_argument, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."},
437{ LLDB_OPT_SET_ALL, false, "environment", 'v', required_argument, NULL, 0, eArgTypeNone, "Specify an environment variable name/value stirng (--environement NAME=VALUE). Can be specified multiple times for subsequent environment entries."},
438
439{ LLDB_OPT_SET_1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
440{ LLDB_OPT_SET_1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
441{ LLDB_OPT_SET_1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
442
443{ LLDB_OPT_SET_2 , false, "tty", 't', no_argument, NULL, 0, eArgTypeNone, "Start the process in a terminal (not supported on all platforms)."},
444
445{ LLDB_OPT_SET_3 , false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
446
447{ 0 , false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
448};
449
450
451
452bool
453ProcessInstanceInfoMatch::NameMatches (const char *process_name) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000454{
455 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
456 return true;
457 const char *match_name = m_match_info.GetName();
458 if (!match_name)
459 return true;
460
461 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
462}
463
464bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000465ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000466{
467 if (!NameMatches (proc_info.GetName()))
468 return false;
469
470 if (m_match_info.ProcessIDIsValid() &&
471 m_match_info.GetProcessID() != proc_info.GetProcessID())
472 return false;
473
474 if (m_match_info.ParentProcessIDIsValid() &&
475 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
476 return false;
477
Greg Clayton8b82f082011-04-12 05:54:46 +0000478 if (m_match_info.UserIDIsValid () &&
479 m_match_info.GetUserID() != proc_info.GetUserID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000480 return false;
481
Greg Clayton8b82f082011-04-12 05:54:46 +0000482 if (m_match_info.GroupIDIsValid () &&
483 m_match_info.GetGroupID() != proc_info.GetGroupID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000484 return false;
485
486 if (m_match_info.EffectiveUserIDIsValid () &&
487 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
488 return false;
489
490 if (m_match_info.EffectiveGroupIDIsValid () &&
491 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
492 return false;
493
494 if (m_match_info.GetArchitecture().IsValid() &&
495 m_match_info.GetArchitecture() != proc_info.GetArchitecture())
496 return false;
497 return true;
498}
499
500bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000501ProcessInstanceInfoMatch::MatchAllProcesses () const
Greg Clayton32e0a752011-03-30 18:16:51 +0000502{
503 if (m_name_match_type != eNameMatchIgnore)
504 return false;
505
506 if (m_match_info.ProcessIDIsValid())
507 return false;
508
509 if (m_match_info.ParentProcessIDIsValid())
510 return false;
511
Greg Clayton8b82f082011-04-12 05:54:46 +0000512 if (m_match_info.UserIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000513 return false;
514
Greg Clayton8b82f082011-04-12 05:54:46 +0000515 if (m_match_info.GroupIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000516 return false;
517
518 if (m_match_info.EffectiveUserIDIsValid ())
519 return false;
520
521 if (m_match_info.EffectiveGroupIDIsValid ())
522 return false;
523
524 if (m_match_info.GetArchitecture().IsValid())
525 return false;
526
527 if (m_match_all_users)
528 return false;
529
530 return true;
531
532}
533
534void
Greg Clayton8b82f082011-04-12 05:54:46 +0000535ProcessInstanceInfoMatch::Clear()
Greg Clayton32e0a752011-03-30 18:16:51 +0000536{
537 m_match_info.Clear();
538 m_name_match_type = eNameMatchIgnore;
539 m_match_all_users = false;
540}
Greg Clayton58be07b2011-01-07 06:08:19 +0000541
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542Process*
543Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener)
544{
545 ProcessCreateInstance create_callback = NULL;
546 if (plugin_name)
547 {
548 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name);
549 if (create_callback)
550 {
551 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
552 if (debugger_ap->CanDebug(target))
553 return debugger_ap.release();
554 }
555 }
556 else
557 {
Greg Claytonc982c762010-07-09 20:39:50 +0000558 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 {
Greg Claytonc982c762010-07-09 20:39:50 +0000560 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
561 if (debugger_ap->CanDebug(target))
562 return debugger_ap.release();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563 }
564 }
565 return NULL;
566}
567
568
569//----------------------------------------------------------------------
570// Process constructor
571//----------------------------------------------------------------------
572Process::Process(Target &target, Listener &listener) :
573 UserID (LLDB_INVALID_PROCESS_ID),
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000574 Broadcaster ("lldb.process"),
Greg Claytondbe54502010-11-19 03:46:01 +0000575 ProcessInstanceSettings (*GetSettingsController()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 m_target (target),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 m_public_state (eStateUnloaded),
578 m_private_state (eStateUnloaded),
579 m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"),
580 m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"),
581 m_private_state_listener ("lldb.process.internal_state_listener"),
582 m_private_state_control_wait(),
583 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
584 m_stop_id (0),
585 m_thread_index_id (0),
586 m_exit_status (-1),
587 m_exit_string (),
588 m_thread_list (this),
589 m_notifications (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000590 m_image_tokens (),
591 m_listener (listener),
592 m_breakpoint_site_list (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000593 m_dynamic_checkers_ap (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +0000594 m_unix_signals (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000595 m_abi_sp (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +0000596 m_process_input_reader (),
Greg Clayton3e06bd92011-01-09 21:07:35 +0000597 m_stdio_communication ("process.stdio"),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000598 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton58be07b2011-01-07 06:08:19 +0000599 m_stdout_data (),
Greg Claytond495c532011-05-17 03:37:42 +0000600 m_memory_cache (*this),
601 m_allocated_memory_cache (*this),
Greg Clayton513c26c2011-01-29 07:10:55 +0000602 m_next_event_action_ap()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603{
Caroline Tice1559a462010-09-27 00:30:10 +0000604 UpdateInstanceName();
605
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000606 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 if (log)
608 log->Printf ("%p Process::Process()", this);
609
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000610 SetEventName (eBroadcastBitStateChanged, "state-changed");
611 SetEventName (eBroadcastBitInterrupt, "interrupt");
612 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
613 SetEventName (eBroadcastBitSTDERR, "stderr-available");
614
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 listener.StartListeningForEvents (this,
616 eBroadcastBitStateChanged |
617 eBroadcastBitInterrupt |
618 eBroadcastBitSTDOUT |
619 eBroadcastBitSTDERR);
620
621 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
622 eBroadcastBitStateChanged);
623
624 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
625 eBroadcastInternalStateControlStop |
626 eBroadcastInternalStateControlPause |
627 eBroadcastInternalStateControlResume);
628}
629
630//----------------------------------------------------------------------
631// Destructor
632//----------------------------------------------------------------------
633Process::~Process()
634{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000635 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636 if (log)
637 log->Printf ("%p Process::~Process()", this);
638 StopPrivateStateThread();
639}
640
641void
642Process::Finalize()
643{
644 // Do any cleanup needed prior to being destructed... Subclasses
645 // that override this method should call this superclass method as well.
Jim Inghamd0a3e122011-02-16 17:54:55 +0000646
647 // We need to destroy the loader before the derived Process class gets destroyed
648 // since it is very likely that undoing the loader will require access to the real process.
649 if (m_dyld_ap.get() != NULL)
650 m_dyld_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651}
652
653void
654Process::RegisterNotificationCallbacks (const Notifications& callbacks)
655{
656 m_notifications.push_back(callbacks);
657 if (callbacks.initialize != NULL)
658 callbacks.initialize (callbacks.baton, this);
659}
660
661bool
662Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
663{
664 std::vector<Notifications>::iterator pos, end = m_notifications.end();
665 for (pos = m_notifications.begin(); pos != end; ++pos)
666 {
667 if (pos->baton == callbacks.baton &&
668 pos->initialize == callbacks.initialize &&
669 pos->process_state_changed == callbacks.process_state_changed)
670 {
671 m_notifications.erase(pos);
672 return true;
673 }
674 }
675 return false;
676}
677
678void
679Process::SynchronouslyNotifyStateChanged (StateType state)
680{
681 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
682 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
683 {
684 if (notification_pos->process_state_changed)
685 notification_pos->process_state_changed (notification_pos->baton, this, state);
686 }
687}
688
689// FIXME: We need to do some work on events before the general Listener sees them.
690// For instance if we are continuing from a breakpoint, we need to ensure that we do
691// the little "insert real insn, step & stop" trick. But we can't do that when the
692// event is delivered by the broadcaster - since that is done on the thread that is
693// waiting for new events, so if we needed more than one event for our handling, we would
694// stall. So instead we do it when we fetch the event off of the queue.
695//
696
697StateType
698Process::GetNextEvent (EventSP &event_sp)
699{
700 StateType state = eStateInvalid;
701
702 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
703 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
704
705 return state;
706}
707
708
709StateType
710Process::WaitForProcessToStop (const TimeValue *timeout)
711{
712 StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded };
713 return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType));
714}
715
716
717StateType
718Process::WaitForState
719(
720 const TimeValue *timeout,
721 const StateType *match_states, const uint32_t num_match_states
722)
723{
724 EventSP event_sp;
725 uint32_t i;
Greg Clayton05faeb72010-10-07 04:19:01 +0000726 StateType state = GetState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 while (state != eStateInvalid)
728 {
Greg Clayton05faeb72010-10-07 04:19:01 +0000729 // If we are exited or detached, we won't ever get back to any
730 // other valid state...
731 if (state == eStateDetached || state == eStateExited)
732 return state;
733
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 state = WaitForStateChangedEvents (timeout, event_sp);
735
736 for (i=0; i<num_match_states; ++i)
737 {
738 if (match_states[i] == state)
739 return state;
740 }
741 }
742 return state;
743}
744
Jim Ingham30f9b212010-10-11 23:53:14 +0000745bool
746Process::HijackProcessEvents (Listener *listener)
747{
748 if (listener != NULL)
749 {
750 return HijackBroadcaster(listener, eBroadcastBitStateChanged);
751 }
752 else
753 return false;
754}
755
756void
757Process::RestoreProcessEvents ()
758{
759 RestoreBroadcaster();
760}
761
Jim Ingham0f16e732011-02-08 05:20:59 +0000762bool
763Process::HijackPrivateProcessEvents (Listener *listener)
764{
765 if (listener != NULL)
766 {
767 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged);
768 }
769 else
770 return false;
771}
772
773void
774Process::RestorePrivateProcessEvents ()
775{
776 m_private_state_broadcaster.RestoreBroadcaster();
777}
778
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779StateType
780Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
781{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000782 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783
784 if (log)
785 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
786
787 StateType state = eStateInvalid;
Greg Clayton3fcbed62010-10-19 03:25:40 +0000788 if (m_listener.WaitForEventForBroadcasterWithType (timeout,
789 this,
790 eBroadcastBitStateChanged,
791 event_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
793
794 if (log)
795 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
796 __FUNCTION__,
797 timeout,
798 StateAsCString(state));
799 return state;
800}
801
802Event *
803Process::PeekAtStateChangedEvents ()
804{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000805 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806
807 if (log)
808 log->Printf ("Process::%s...", __FUNCTION__);
809
810 Event *event_ptr;
Greg Clayton3fcbed62010-10-19 03:25:40 +0000811 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
812 eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 if (log)
814 {
815 if (event_ptr)
816 {
817 log->Printf ("Process::%s (event_ptr) => %s",
818 __FUNCTION__,
819 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
820 }
821 else
822 {
823 log->Printf ("Process::%s no events found",
824 __FUNCTION__);
825 }
826 }
827 return event_ptr;
828}
829
830StateType
831Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
832{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000833 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834
835 if (log)
836 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
837
838 StateType state = eStateInvalid;
Greg Clayton6779606a2011-01-22 23:43:18 +0000839 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
840 &m_private_state_broadcaster,
841 eBroadcastBitStateChanged,
842 event_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
844
845 // This is a bit of a hack, but when we wait here we could very well return
846 // to the command-line, and that could disable the log, which would render the
847 // log we got above invalid.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 if (log)
Greg Clayton6779606a2011-01-22 23:43:18 +0000849 {
850 if (state == eStateInvalid)
851 log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout);
852 else
853 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
854 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 return state;
856}
857
858bool
859Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
860{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000861 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862
863 if (log)
864 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
865
866 if (control_only)
867 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
868 else
869 return m_private_state_listener.WaitForEvent(timeout, event_sp);
870}
871
872bool
873Process::IsRunning () const
874{
875 return StateIsRunningState (m_public_state.GetValue());
876}
877
878int
879Process::GetExitStatus ()
880{
881 if (m_public_state.GetValue() == eStateExited)
882 return m_exit_status;
883 return -1;
884}
885
Greg Clayton85851dd2010-12-04 00:10:17 +0000886
887void
888Process::ProcessInstanceSettings::GetHostEnvironmentIfNeeded ()
889{
890 if (m_inherit_host_env && !m_got_host_env)
891 {
892 m_got_host_env = true;
893 StringList host_env;
894 const size_t host_env_count = Host::GetEnvironment (host_env);
895 for (size_t idx=0; idx<host_env_count; idx++)
896 {
897 const char *env_entry = host_env.GetStringAtIndex (idx);
898 if (env_entry)
899 {
Greg Claytone2956ee2010-12-15 20:52:40 +0000900 const char *equal_pos = ::strchr(env_entry, '=');
Greg Clayton85851dd2010-12-04 00:10:17 +0000901 if (equal_pos)
902 {
903 std::string key (env_entry, equal_pos - env_entry);
904 std::string value (equal_pos + 1);
905 if (m_env_vars.find (key) == m_env_vars.end())
906 m_env_vars[key] = value;
907 }
908 }
909 }
910 }
911}
912
913
914size_t
915Process::ProcessInstanceSettings::GetEnvironmentAsArgs (Args &env)
916{
917 GetHostEnvironmentIfNeeded ();
918
919 dictionary::const_iterator pos, end = m_env_vars.end();
920 for (pos = m_env_vars.begin(); pos != end; ++pos)
921 {
922 std::string env_var_equal_value (pos->first);
923 env_var_equal_value.append(1, '=');
924 env_var_equal_value.append (pos->second);
925 env.AppendArgument (env_var_equal_value.c_str());
926 }
927 return env.GetArgumentCount();
928}
929
930
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931const char *
932Process::GetExitDescription ()
933{
934 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
935 return m_exit_string.c_str();
936 return NULL;
937}
938
Greg Clayton6779606a2011-01-22 23:43:18 +0000939bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940Process::SetExitStatus (int status, const char *cstr)
941{
Greg Clayton414f5d32011-01-25 02:58:48 +0000942 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
943 if (log)
944 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
945 status, status,
946 cstr ? "\"" : "",
947 cstr ? cstr : "NULL",
948 cstr ? "\"" : "");
949
Greg Clayton6779606a2011-01-22 23:43:18 +0000950 // We were already in the exited state
951 if (m_private_state.GetValue() == eStateExited)
Greg Clayton414f5d32011-01-25 02:58:48 +0000952 {
Greg Clayton385d6032011-01-26 23:47:29 +0000953 if (log)
954 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton6779606a2011-01-22 23:43:18 +0000955 return false;
Greg Clayton414f5d32011-01-25 02:58:48 +0000956 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000957
958 m_exit_status = status;
959 if (cstr)
960 m_exit_string = cstr;
961 else
962 m_exit_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963
Greg Clayton6779606a2011-01-22 23:43:18 +0000964 DidExit ();
Greg Clayton10177aa2010-12-08 05:08:21 +0000965
Greg Clayton6779606a2011-01-22 23:43:18 +0000966 SetPrivateState (eStateExited);
967 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968}
969
970// This static callback can be used to watch for local child processes on
971// the current host. The the child process exits, the process will be
972// found in the global target list (we want to be completely sure that the
973// lldb_private::Process doesn't go away before we can deliver the signal.
974bool
975Process::SetProcessExitStatus
976(
977 void *callback_baton,
978 lldb::pid_t pid,
979 int signo, // Zero for no signal
980 int exit_status // Exit value of process if signal is zero
981)
982{
983 if (signo == 0 || exit_status)
984 {
Greg Clayton66111032010-06-23 01:19:29 +0000985 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 if (target_sp)
987 {
988 ProcessSP process_sp (target_sp->GetProcessSP());
989 if (process_sp)
990 {
991 const char *signal_cstr = NULL;
992 if (signo)
993 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
994
995 process_sp->SetExitStatus (exit_status, signal_cstr);
996 }
997 }
998 return true;
999 }
1000 return false;
1001}
1002
1003
1004uint32_t
1005Process::GetNextThreadIndexID ()
1006{
1007 return ++m_thread_index_id;
1008}
1009
1010StateType
1011Process::GetState()
1012{
1013 // If any other threads access this we will need a mutex for it
1014 return m_public_state.GetValue ();
1015}
1016
1017void
1018Process::SetPublicState (StateType new_state)
1019{
Greg Clayton414f5d32011-01-25 02:58:48 +00001020 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021 if (log)
1022 log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state));
1023 m_public_state.SetValue (new_state);
1024}
1025
1026StateType
1027Process::GetPrivateState ()
1028{
1029 return m_private_state.GetValue();
1030}
1031
1032void
1033Process::SetPrivateState (StateType new_state)
1034{
Greg Clayton414f5d32011-01-25 02:58:48 +00001035 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036 bool state_changed = false;
1037
1038 if (log)
1039 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
1040
1041 Mutex::Locker locker(m_private_state.GetMutex());
1042
1043 const StateType old_state = m_private_state.GetValueNoLock ();
1044 state_changed = old_state != new_state;
1045 if (state_changed)
1046 {
1047 m_private_state.SetValueNoLock (new_state);
1048 if (StateIsStoppedState(new_state))
1049 {
1050 m_stop_id++;
Greg Clayton58be07b2011-01-07 06:08:19 +00001051 m_memory_cache.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 if (log)
1053 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id);
1054 }
1055 // Use our target to get a shared pointer to ourselves...
1056 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state));
1057 }
1058 else
1059 {
1060 if (log)
1061 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state));
1062 }
1063}
1064
1065
1066uint32_t
1067Process::GetStopID() const
1068{
1069 return m_stop_id;
1070}
1071
1072addr_t
1073Process::GetImageInfoAddress()
1074{
1075 return LLDB_INVALID_ADDRESS;
1076}
1077
Greg Clayton8f343b02010-11-04 01:54:29 +00001078//----------------------------------------------------------------------
1079// LoadImage
1080//
1081// This function provides a default implementation that works for most
1082// unix variants. Any Process subclasses that need to do shared library
1083// loading differently should override LoadImage and UnloadImage and
1084// do what is needed.
1085//----------------------------------------------------------------------
1086uint32_t
1087Process::LoadImage (const FileSpec &image_spec, Error &error)
1088{
1089 DynamicLoader *loader = GetDynamicLoader();
1090 if (loader)
1091 {
1092 error = loader->CanLoadImage();
1093 if (error.Fail())
1094 return LLDB_INVALID_IMAGE_TOKEN;
1095 }
1096
1097 if (error.Success())
1098 {
1099 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
1100 if (thread_sp == NULL)
1101 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
1102
1103 if (thread_sp)
1104 {
1105 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
1106
1107 if (frame_sp)
1108 {
1109 ExecutionContext exe_ctx;
1110 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Ingham399f1ca2010-11-05 19:25:48 +00001111 bool unwind_on_error = true;
Greg Clayton8f343b02010-11-04 01:54:29 +00001112 StreamString expr;
1113 char path[PATH_MAX];
1114 image_spec.GetPath(path, sizeof(path));
1115 expr.Printf("dlopen (\"%s\", 2)", path);
1116 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001117 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan63697e52011-05-07 01:06:41 +00001118 ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton8f343b02010-11-04 01:54:29 +00001119 if (result_valobj_sp->GetError().Success())
1120 {
1121 Scalar scalar;
Jim Ingham6035b672011-03-31 00:19:25 +00001122 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001123 {
1124 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1125 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
1126 {
1127 uint32_t image_token = m_image_tokens.size();
1128 m_image_tokens.push_back (image_ptr);
1129 return image_token;
1130 }
1131 }
1132 }
1133 }
1134 }
1135 }
1136 return LLDB_INVALID_IMAGE_TOKEN;
1137}
1138
1139//----------------------------------------------------------------------
1140// UnloadImage
1141//
1142// This function provides a default implementation that works for most
1143// unix variants. Any Process subclasses that need to do shared library
1144// loading differently should override LoadImage and UnloadImage and
1145// do what is needed.
1146//----------------------------------------------------------------------
1147Error
1148Process::UnloadImage (uint32_t image_token)
1149{
1150 Error error;
1151 if (image_token < m_image_tokens.size())
1152 {
1153 const addr_t image_addr = m_image_tokens[image_token];
1154 if (image_addr == LLDB_INVALID_ADDRESS)
1155 {
1156 error.SetErrorString("image already unloaded");
1157 }
1158 else
1159 {
1160 DynamicLoader *loader = GetDynamicLoader();
1161 if (loader)
1162 error = loader->CanLoadImage();
1163
1164 if (error.Success())
1165 {
1166 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
1167 if (thread_sp == NULL)
1168 thread_sp = GetThreadList ().GetThreadAtIndex(0, true);
1169
1170 if (thread_sp)
1171 {
1172 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
1173
1174 if (frame_sp)
1175 {
1176 ExecutionContext exe_ctx;
1177 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Ingham399f1ca2010-11-05 19:25:48 +00001178 bool unwind_on_error = true;
Greg Clayton8f343b02010-11-04 01:54:29 +00001179 StreamString expr;
1180 expr.Printf("dlclose ((void *)0x%llx)", image_addr);
1181 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001182 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan63697e52011-05-07 01:06:41 +00001183 ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton8f343b02010-11-04 01:54:29 +00001184 if (result_valobj_sp->GetError().Success())
1185 {
1186 Scalar scalar;
Jim Ingham6035b672011-03-31 00:19:25 +00001187 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001188 {
1189 if (scalar.UInt(1))
1190 {
1191 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1192 }
1193 else
1194 {
1195 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1196 }
1197 }
1198 }
1199 else
1200 {
1201 error = result_valobj_sp->GetError();
1202 }
1203 }
1204 }
1205 }
1206 }
1207 }
1208 else
1209 {
1210 error.SetErrorString("invalid image token");
1211 }
1212 return error;
1213}
1214
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001215const lldb::ABISP &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216Process::GetABI()
1217{
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001218 if (!m_abi_sp)
1219 m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture());
1220 return m_abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221}
1222
Jim Ingham22777012010-09-23 02:01:19 +00001223LanguageRuntime *
1224Process::GetLanguageRuntime(lldb::LanguageType language)
1225{
1226 LanguageRuntimeCollection::iterator pos;
1227 pos = m_language_runtimes.find (language);
1228 if (pos == m_language_runtimes.end())
1229 {
1230 lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language));
1231
1232 m_language_runtimes[language]
1233 = runtime;
1234 return runtime.get();
1235 }
1236 else
1237 return (*pos).second.get();
1238}
1239
1240CPPLanguageRuntime *
1241Process::GetCPPLanguageRuntime ()
1242{
1243 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus);
1244 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
1245 return static_cast<CPPLanguageRuntime *> (runtime);
1246 return NULL;
1247}
1248
1249ObjCLanguageRuntime *
1250Process::GetObjCLanguageRuntime ()
1251{
1252 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC);
1253 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
1254 return static_cast<ObjCLanguageRuntime *> (runtime);
1255 return NULL;
1256}
1257
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258BreakpointSiteList &
1259Process::GetBreakpointSiteList()
1260{
1261 return m_breakpoint_site_list;
1262}
1263
1264const BreakpointSiteList &
1265Process::GetBreakpointSiteList() const
1266{
1267 return m_breakpoint_site_list;
1268}
1269
1270
1271void
1272Process::DisableAllBreakpointSites ()
1273{
1274 m_breakpoint_site_list.SetEnabledForAll (false);
1275}
1276
1277Error
1278Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
1279{
1280 Error error (DisableBreakpointSiteByID (break_id));
1281
1282 if (error.Success())
1283 m_breakpoint_site_list.Remove(break_id);
1284
1285 return error;
1286}
1287
1288Error
1289Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
1290{
1291 Error error;
1292 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1293 if (bp_site_sp)
1294 {
1295 if (bp_site_sp->IsEnabled())
1296 error = DisableBreakpoint (bp_site_sp.get());
1297 }
1298 else
1299 {
1300 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
1301 }
1302
1303 return error;
1304}
1305
1306Error
1307Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
1308{
1309 Error error;
1310 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1311 if (bp_site_sp)
1312 {
1313 if (!bp_site_sp->IsEnabled())
1314 error = EnableBreakpoint (bp_site_sp.get());
1315 }
1316 else
1317 {
1318 error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id);
1319 }
1320 return error;
1321}
1322
Stephen Wilson50bd94f2010-07-17 00:56:13 +00001323lldb::break_id_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware)
1325{
Greg Clayton92bb12c2011-05-19 18:17:41 +00001326 const addr_t load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327 if (load_addr != LLDB_INVALID_ADDRESS)
1328 {
1329 BreakpointSiteSP bp_site_sp;
1330
1331 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
1332 // create a new breakpoint site and add it.
1333
1334 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
1335
1336 if (bp_site_sp)
1337 {
1338 bp_site_sp->AddOwner (owner);
1339 owner->SetBreakpointSite (bp_site_sp);
1340 return bp_site_sp->GetID();
1341 }
1342 else
1343 {
1344 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware));
1345 if (bp_site_sp)
1346 {
1347 if (EnableBreakpoint (bp_site_sp.get()).Success())
1348 {
1349 owner->SetBreakpointSite (bp_site_sp);
1350 return m_breakpoint_site_list.Add (bp_site_sp);
1351 }
1352 }
1353 }
1354 }
1355 // We failed to enable the breakpoint
1356 return LLDB_INVALID_BREAK_ID;
1357
1358}
1359
1360void
1361Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
1362{
1363 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
1364 if (num_owners == 0)
1365 {
1366 DisableBreakpoint(bp_site_sp.get());
1367 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1368 }
1369}
1370
1371
1372size_t
1373Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1374{
1375 size_t bytes_removed = 0;
1376 addr_t intersect_addr;
1377 size_t intersect_size;
1378 size_t opcode_offset;
1379 size_t idx;
1380 BreakpointSiteSP bp;
1381
1382 for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx)
1383 {
1384 if (bp->GetType() == BreakpointSite::eSoftware)
1385 {
1386 if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
1387 {
1388 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1389 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
1390 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1391 size_t buf_offset = intersect_addr - bp_addr;
1392 ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
1393 }
1394 }
1395 }
1396 return bytes_removed;
1397}
1398
1399
Greg Claytonded470d2011-03-19 01:12:21 +00001400
1401size_t
1402Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
1403{
1404 PlatformSP platform_sp (m_target.GetPlatform());
1405 if (platform_sp)
1406 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
1407 return 0;
1408}
1409
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410Error
1411Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
1412{
1413 Error error;
1414 assert (bp_site != NULL);
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001415 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416 const addr_t bp_addr = bp_site->GetLoadAddress();
1417 if (log)
1418 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr);
1419 if (bp_site->IsEnabled())
1420 {
1421 if (log)
1422 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
1423 return error;
1424 }
1425
1426 if (bp_addr == LLDB_INVALID_ADDRESS)
1427 {
1428 error.SetErrorString("BreakpointSite contains an invalid load address.");
1429 return error;
1430 }
1431 // Ask the lldb::Process subclass to fill in the correct software breakpoint
1432 // trap for the breakpoint site
1433 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
1434
1435 if (bp_opcode_size == 0)
1436 {
1437 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr);
1438 }
1439 else
1440 {
1441 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
1442
1443 if (bp_opcode_bytes == NULL)
1444 {
1445 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
1446 return error;
1447 }
1448
1449 // Save the original opcode by reading it
1450 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
1451 {
1452 // Write a software breakpoint in place of the original opcode
1453 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1454 {
1455 uint8_t verify_bp_opcode_bytes[64];
1456 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1457 {
1458 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
1459 {
1460 bp_site->SetEnabled(true);
1461 bp_site->SetType (BreakpointSite::eSoftware);
1462 if (log)
1463 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS",
1464 bp_site->GetID(),
1465 (uint64_t)bp_addr);
1466 }
1467 else
1468 error.SetErrorString("Failed to verify the breakpoint trap in memory.");
1469 }
1470 else
1471 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
1472 }
1473 else
1474 error.SetErrorString("Unable to write breakpoint trap to memory.");
1475 }
1476 else
1477 error.SetErrorString("Unable to read memory at breakpoint address.");
1478 }
Stephen Wilson78a4feb2011-01-12 04:20:03 +00001479 if (log && error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1481 bp_site->GetID(),
1482 (uint64_t)bp_addr,
1483 error.AsCString());
1484 return error;
1485}
1486
1487Error
1488Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
1489{
1490 Error error;
1491 assert (bp_site != NULL);
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001492 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001493 addr_t bp_addr = bp_site->GetLoadAddress();
1494 lldb::user_id_t breakID = bp_site->GetID();
1495 if (log)
Stephen Wilson5394e0d2011-01-14 21:07:07 +00001496 log->Printf ("Process::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497
1498 if (bp_site->IsHardware())
1499 {
1500 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
1501 }
1502 else if (bp_site->IsEnabled())
1503 {
1504 const size_t break_op_size = bp_site->GetByteSize();
1505 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
1506 if (break_op_size > 0)
1507 {
1508 // Clear a software breakoint instruction
Greg Claytonc982c762010-07-09 20:39:50 +00001509 uint8_t curr_break_op[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00001510 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511 bool break_op_found = false;
1512
1513 // Read the breakpoint opcode
1514 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
1515 {
1516 bool verify = false;
1517 // Make sure we have the a breakpoint opcode exists at this address
1518 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
1519 {
1520 break_op_found = true;
1521 // We found a valid breakpoint opcode at this address, now restore
1522 // the saved opcode.
1523 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
1524 {
1525 verify = true;
1526 }
1527 else
1528 error.SetErrorString("Memory write failed when restoring original opcode.");
1529 }
1530 else
1531 {
1532 error.SetErrorString("Original breakpoint trap is no longer in memory.");
1533 // Set verify to true and so we can check if the original opcode has already been restored
1534 verify = true;
1535 }
1536
1537 if (verify)
1538 {
Greg Claytonc982c762010-07-09 20:39:50 +00001539 uint8_t verify_opcode[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00001540 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001541 // Verify that our original opcode made it back to the inferior
1542 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
1543 {
1544 // compare the memory we just read with the original opcode
1545 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
1546 {
1547 // SUCCESS
1548 bp_site->SetEnabled(false);
1549 if (log)
1550 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
1551 return error;
1552 }
1553 else
1554 {
1555 if (break_op_found)
1556 error.SetErrorString("Failed to restore original opcode.");
1557 }
1558 }
1559 else
1560 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
1561 }
1562 }
1563 else
1564 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
1565 }
1566 }
1567 else
1568 {
1569 if (log)
1570 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
1571 return error;
1572 }
1573
1574 if (log)
1575 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1576 bp_site->GetID(),
1577 (uint64_t)bp_addr,
1578 error.AsCString());
1579 return error;
1580
1581}
1582
Greg Clayton58be07b2011-01-07 06:08:19 +00001583// Comment out line below to disable memory caching
1584#define ENABLE_MEMORY_CACHING
1585// Uncomment to verify memory caching works after making changes to caching code
1586//#define VERIFY_MEMORY_READS
1587
1588#if defined (ENABLE_MEMORY_CACHING)
1589
1590#if defined (VERIFY_MEMORY_READS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001591
1592size_t
1593Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1594{
Greg Clayton58be07b2011-01-07 06:08:19 +00001595 // Memory caching is enabled, with debug verification
1596 if (buf && size)
1597 {
1598 // Uncomment the line below to make sure memory caching is working.
1599 // I ran this through the test suite and got no assertions, so I am
1600 // pretty confident this is working well. If any changes are made to
1601 // memory caching, uncomment the line below and test your changes!
1602
1603 // Verify all memory reads by using the cache first, then redundantly
1604 // reading the same memory from the inferior and comparing to make sure
1605 // everything is exactly the same.
1606 std::string verify_buf (size, '\0');
1607 assert (verify_buf.size() == size);
1608 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
1609 Error verify_error;
1610 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
1611 assert (cache_bytes_read == verify_bytes_read);
1612 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
1613 assert (verify_error.Success() == error.Success());
1614 return cache_bytes_read;
1615 }
1616 return 0;
1617}
1618
1619#else // #if defined (VERIFY_MEMORY_READS)
1620
1621size_t
1622Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1623{
1624 // Memory caching enabled, no verification
Greg Claytond495c532011-05-17 03:37:42 +00001625 return m_memory_cache.Read (addr, buf, size, error);
Greg Clayton58be07b2011-01-07 06:08:19 +00001626}
1627
1628#endif // #else for #if defined (VERIFY_MEMORY_READS)
1629
1630#else // #if defined (ENABLE_MEMORY_CACHING)
1631
1632size_t
1633Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1634{
1635 // Memory caching is disabled
1636 return ReadMemoryFromInferior (addr, buf, size, error);
1637}
1638
1639#endif // #else for #if defined (ENABLE_MEMORY_CACHING)
1640
1641
1642size_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001643Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len)
1644{
1645 size_t total_cstr_len = 0;
1646 if (dst && dst_max_len)
1647 {
1648 // NULL out everything just to be safe
1649 memset (dst, 0, dst_max_len);
1650 Error error;
1651 addr_t curr_addr = addr;
1652 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
1653 size_t bytes_left = dst_max_len - 1;
1654 char *curr_dst = dst;
1655
1656 while (bytes_left > 0)
1657 {
1658 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1659 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1660 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
1661
1662 if (bytes_read == 0)
1663 {
1664 dst[total_cstr_len] = '\0';
1665 break;
1666 }
1667 const size_t len = strlen(curr_dst);
1668
1669 total_cstr_len += len;
1670
1671 if (len < bytes_to_read)
1672 break;
1673
1674 curr_dst += bytes_read;
1675 curr_addr += bytes_read;
1676 bytes_left -= bytes_read;
1677 }
1678 }
1679 return total_cstr_len;
1680}
1681
1682size_t
Greg Clayton58be07b2011-01-07 06:08:19 +00001683Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
1684{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685 if (buf == NULL || size == 0)
1686 return 0;
1687
1688 size_t bytes_read = 0;
1689 uint8_t *bytes = (uint8_t *)buf;
1690
1691 while (bytes_read < size)
1692 {
1693 const size_t curr_size = size - bytes_read;
1694 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
1695 bytes + bytes_read,
1696 curr_size,
1697 error);
1698 bytes_read += curr_bytes_read;
1699 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
1700 break;
1701 }
1702
1703 // Replace any software breakpoint opcodes that fall into this range back
1704 // into "buf" before we return
1705 if (bytes_read > 0)
1706 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
1707 return bytes_read;
1708}
1709
Greg Clayton58a4c462010-12-16 20:01:20 +00001710uint64_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001711Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error)
Greg Clayton58a4c462010-12-16 20:01:20 +00001712{
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001713 Scalar scalar;
1714 if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error))
1715 return scalar.ULongLong(fail_value);
1716 return fail_value;
1717}
1718
1719addr_t
1720Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error)
1721{
1722 Scalar scalar;
1723 if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error))
1724 return scalar.ULongLong(LLDB_INVALID_ADDRESS);
1725 return LLDB_INVALID_ADDRESS;
1726}
1727
1728
1729bool
1730Process::WritePointerToMemory (lldb::addr_t vm_addr,
1731 lldb::addr_t ptr_value,
1732 Error &error)
1733{
1734 Scalar scalar;
1735 const uint32_t addr_byte_size = GetAddressByteSize();
1736 if (addr_byte_size <= 4)
1737 scalar = (uint32_t)ptr_value;
Greg Clayton58a4c462010-12-16 20:01:20 +00001738 else
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001739 scalar = ptr_value;
1740 return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size;
Greg Clayton58a4c462010-12-16 20:01:20 +00001741}
1742
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001743size_t
1744Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
1745{
1746 size_t bytes_written = 0;
1747 const uint8_t *bytes = (const uint8_t *)buf;
1748
1749 while (bytes_written < size)
1750 {
1751 const size_t curr_size = size - bytes_written;
1752 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
1753 bytes + bytes_written,
1754 curr_size,
1755 error);
1756 bytes_written += curr_bytes_written;
1757 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
1758 break;
1759 }
1760 return bytes_written;
1761}
1762
1763size_t
1764Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
1765{
Greg Clayton58be07b2011-01-07 06:08:19 +00001766#if defined (ENABLE_MEMORY_CACHING)
1767 m_memory_cache.Flush (addr, size);
1768#endif
1769
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770 if (buf == NULL || size == 0)
1771 return 0;
Jim Ingham78a685a2011-04-16 00:01:13 +00001772
1773 // Need to bump the stop ID after writing so that ValueObjects will know to re-read themselves.
1774 // FUTURE: Doing this should be okay, but if anybody else gets upset about the stop_id changing when
1775 // the target hasn't run, then we will need to add a "memory generation" as well as a stop_id...
1776 m_stop_id++;
1777
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778 // We need to write any data that would go where any current software traps
1779 // (enabled software breakpoints) any software traps (breakpoints) that we
1780 // may have placed in our tasks memory.
1781
1782 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
1783 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
1784
1785 if (iter == end || iter->second->GetLoadAddress() > addr + size)
Greg Claytonb4aaf2e2011-05-16 02:35:02 +00001786 return WriteMemoryPrivate (addr, buf, size, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001787
1788 BreakpointSiteList::collection::const_iterator pos;
1789 size_t bytes_written = 0;
Greg Claytonc982c762010-07-09 20:39:50 +00001790 addr_t intersect_addr = 0;
1791 size_t intersect_size = 0;
1792 size_t opcode_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001793 const uint8_t *ubuf = (const uint8_t *)buf;
1794
1795 for (pos = iter; pos != end; ++pos)
1796 {
1797 BreakpointSiteSP bp;
1798 bp = pos->second;
1799
1800 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
1801 assert(addr <= intersect_addr && intersect_addr < addr + size);
1802 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
1803 assert(opcode_offset + intersect_size <= bp->GetByteSize());
1804
1805 // Check for bytes before this breakpoint
1806 const addr_t curr_addr = addr + bytes_written;
1807 if (intersect_addr > curr_addr)
1808 {
1809 // There are some bytes before this breakpoint that we need to
1810 // just write to memory
1811 size_t curr_size = intersect_addr - curr_addr;
1812 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
1813 ubuf + bytes_written,
1814 curr_size,
1815 error);
1816 bytes_written += curr_bytes_written;
1817 if (curr_bytes_written != curr_size)
1818 {
1819 // We weren't able to write all of the requested bytes, we
1820 // are done looping and will return the number of bytes that
1821 // we have written so far.
1822 break;
1823 }
1824 }
1825
1826 // Now write any bytes that would cover up any software breakpoints
1827 // directly into the breakpoint opcode buffer
1828 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
1829 bytes_written += intersect_size;
1830 }
1831
1832 // Write any remaining bytes after the last breakpoint if we have any left
1833 if (bytes_written < size)
1834 bytes_written += WriteMemoryPrivate (addr + bytes_written,
1835 ubuf + bytes_written,
1836 size - bytes_written,
1837 error);
Jim Ingham78a685a2011-04-16 00:01:13 +00001838
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001839 return bytes_written;
1840}
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001841
1842size_t
1843Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, uint32_t byte_size, Error &error)
1844{
1845 if (byte_size == UINT32_MAX)
1846 byte_size = scalar.GetByteSize();
1847 if (byte_size > 0)
1848 {
1849 uint8_t buf[32];
1850 const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error);
1851 if (mem_size > 0)
1852 return WriteMemory(addr, buf, mem_size, error);
1853 else
1854 error.SetErrorString ("failed to get scalar as memory data");
1855 }
1856 else
1857 {
1858 error.SetErrorString ("invalid scalar value");
1859 }
1860 return 0;
1861}
1862
1863size_t
1864Process::ReadScalarIntegerFromMemory (addr_t addr,
1865 uint32_t byte_size,
1866 bool is_signed,
1867 Scalar &scalar,
1868 Error &error)
1869{
1870 uint64_t uval;
1871
1872 if (byte_size <= sizeof(uval))
1873 {
1874 size_t bytes_read = ReadMemory (addr, &uval, byte_size, error);
1875 if (bytes_read == byte_size)
1876 {
1877 DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize());
1878 uint32_t offset = 0;
1879 if (byte_size <= 4)
1880 scalar = data.GetMaxU32 (&offset, byte_size);
1881 else
1882 scalar = data.GetMaxU64 (&offset, byte_size);
1883
1884 if (is_signed)
1885 scalar.SignExtend(byte_size * 8);
1886 return bytes_read;
1887 }
1888 }
1889 else
1890 {
1891 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1892 }
1893 return 0;
1894}
1895
Greg Claytond495c532011-05-17 03:37:42 +00001896#define USE_ALLOCATE_MEMORY_CACHE 1
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897addr_t
1898Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
1899{
Jim Inghamf72ce3a2011-06-20 17:32:44 +00001900 if (GetPrivateState() != eStateStopped)
1901 return LLDB_INVALID_ADDRESS;
1902
Greg Claytond495c532011-05-17 03:37:42 +00001903#if defined (USE_ALLOCATE_MEMORY_CACHE)
1904 return m_allocated_memory_cache.AllocateMemory(size, permissions, error);
1905#else
Greg Claytonb2daec92011-01-23 19:58:49 +00001906 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
1907 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1908 if (log)
Greg Claytond495c532011-05-17 03:37:42 +00001909 log->Printf("Process::AllocateMemory(size=%4zu, permissions=%s) => 0x%16.16llx (m_stop_id = %u)",
Greg Claytonb2daec92011-01-23 19:58:49 +00001910 size,
Greg Claytond495c532011-05-17 03:37:42 +00001911 GetPermissionsAsCString (permissions),
Greg Claytonb2daec92011-01-23 19:58:49 +00001912 (uint64_t)allocated_addr,
1913 m_stop_id);
1914 return allocated_addr;
Greg Claytond495c532011-05-17 03:37:42 +00001915#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001916}
1917
1918Error
1919Process::DeallocateMemory (addr_t ptr)
1920{
Greg Claytond495c532011-05-17 03:37:42 +00001921 Error error;
1922#if defined (USE_ALLOCATE_MEMORY_CACHE)
1923 if (!m_allocated_memory_cache.DeallocateMemory(ptr))
1924 {
1925 error.SetErrorStringWithFormat ("deallocation of memory at 0x%llx failed.", (uint64_t)ptr);
1926 }
1927#else
1928 error = DoDeallocateMemory (ptr);
Greg Claytonb2daec92011-01-23 19:58:49 +00001929
1930 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1931 if (log)
1932 log->Printf("Process::DeallocateMemory(addr=0x%16.16llx) => err = %s (m_stop_id = %u)",
1933 ptr,
1934 error.AsCString("SUCCESS"),
1935 m_stop_id);
Greg Claytond495c532011-05-17 03:37:42 +00001936#endif
Greg Claytonb2daec92011-01-23 19:58:49 +00001937 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001938}
1939
1940
1941Error
1942Process::EnableWatchpoint (WatchpointLocation *watchpoint)
1943{
1944 Error error;
1945 error.SetErrorString("watchpoints are not supported");
1946 return error;
1947}
1948
1949Error
1950Process::DisableWatchpoint (WatchpointLocation *watchpoint)
1951{
1952 Error error;
1953 error.SetErrorString("watchpoints are not supported");
1954 return error;
1955}
1956
1957StateType
1958Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
1959{
1960 StateType state;
1961 // Now wait for the process to launch and return control to us, and then
1962 // call DidLaunch:
1963 while (1)
1964 {
Greg Clayton6779606a2011-01-22 23:43:18 +00001965 event_sp.reset();
1966 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
1967
1968 if (StateIsStoppedState(state))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969 break;
Greg Clayton6779606a2011-01-22 23:43:18 +00001970
1971 // If state is invalid, then we timed out
1972 if (state == eStateInvalid)
1973 break;
1974
1975 if (event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976 HandlePrivateEvent (event_sp);
1977 }
1978 return state;
1979}
1980
1981Error
1982Process::Launch
1983(
1984 char const *argv[],
1985 char const *envp[],
Greg Claytonf681b942010-08-31 18:35:14 +00001986 uint32_t launch_flags,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001987 const char *stdin_path,
1988 const char *stdout_path,
Greg Claytonbd82a5d2011-01-23 05:56:20 +00001989 const char *stderr_path,
1990 const char *working_directory
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001991)
1992{
1993 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001994 m_abi_sp.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00001995 m_dyld_ap.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001996 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001997
1998 Module *exe_module = m_target.GetExecutableModule().get();
1999 if (exe_module)
2000 {
Greg Clayton2289fa42011-04-30 01:09:13 +00002001 char local_exec_file_path[PATH_MAX];
2002 char platform_exec_file_path[PATH_MAX];
2003 exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path));
2004 exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002005 if (exe_module->GetFileSpec().Exists())
2006 {
Greg Clayton71337622011-02-24 22:24:29 +00002007 if (PrivateStateThreadIsValid ())
2008 PausePrivateStateThread ();
2009
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002010 error = WillLaunch (exe_module);
2011 if (error.Success())
2012 {
Greg Clayton05faeb72010-10-07 04:19:01 +00002013 SetPublicState (eStateLaunching);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014 // The args coming in should not contain the application name, the
2015 // lldb_private::Process class will add this in case the executable
2016 // gets resolved to a different file than was given on the command
2017 // line (like when an applicaiton bundle is specified and will
2018 // resolve to the contained exectuable file, or the file given was
2019 // a symlink or other file system link that resolves to a different
2020 // file).
2021
2022 // Get the resolved exectuable path
2023
2024 // Make a new argument vector
2025 std::vector<const char *> exec_path_plus_argv;
2026 // Append the resolved executable path
Greg Clayton2289fa42011-04-30 01:09:13 +00002027 exec_path_plus_argv.push_back (platform_exec_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028
2029 // Push all args if there are any
2030 if (argv)
2031 {
2032 for (int i = 0; argv[i]; ++i)
2033 exec_path_plus_argv.push_back(argv[i]);
2034 }
2035
2036 // Push a NULL to terminate the args.
2037 exec_path_plus_argv.push_back(NULL);
2038
2039 // Now launch using these arguments.
Greg Clayton471b31c2010-07-20 22:52:08 +00002040 error = DoLaunch (exe_module,
2041 exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(),
2042 envp,
Greg Claytonf681b942010-08-31 18:35:14 +00002043 launch_flags,
Greg Clayton471b31c2010-07-20 22:52:08 +00002044 stdin_path,
2045 stdout_path,
Greg Claytonbd82a5d2011-01-23 05:56:20 +00002046 stderr_path,
2047 working_directory);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002048
2049 if (error.Fail())
2050 {
2051 if (GetID() != LLDB_INVALID_PROCESS_ID)
2052 {
2053 SetID (LLDB_INVALID_PROCESS_ID);
2054 const char *error_string = error.AsCString();
2055 if (error_string == NULL)
2056 error_string = "launch failed";
2057 SetExitStatus (-1, error_string);
2058 }
2059 }
2060 else
2061 {
2062 EventSP event_sp;
2063 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
2064
2065 if (state == eStateStopped || state == eStateCrashed)
2066 {
Greg Clayton93d3c8332011-02-16 04:46:07 +00002067
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002068 DidLaunch ();
2069
Greg Clayton7a5388b2011-03-20 04:57:14 +00002070 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Greg Clayton93d3c8332011-02-16 04:46:07 +00002071 if (m_dyld_ap.get())
2072 m_dyld_ap->DidLaunch();
2073
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002074 // This delays passing the stopped event to listeners till DidLaunch gets
2075 // a chance to complete...
2076 HandlePrivateEvent (event_sp);
Greg Clayton71337622011-02-24 22:24:29 +00002077
2078 if (PrivateStateThreadIsValid ())
2079 ResumePrivateStateThread ();
2080 else
2081 StartPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002082 }
2083 else if (state == eStateExited)
2084 {
2085 // We exited while trying to launch somehow. Don't call DidLaunch as that's
2086 // not likely to work, and return an invalid pid.
2087 HandlePrivateEvent (event_sp);
2088 }
2089 }
2090 }
2091 }
2092 else
2093 {
Greg Clayton2289fa42011-04-30 01:09:13 +00002094 error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", local_exec_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002095 }
2096 }
2097 return error;
2098}
2099
Jim Inghambb3a2832011-01-29 01:49:25 +00002100Process::NextEventAction::EventActionResult
2101Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002102{
Jim Inghambb3a2832011-01-29 01:49:25 +00002103 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
2104 switch (state)
Greg Clayton19388cf2010-10-18 01:45:30 +00002105 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002106 case eStateRunning:
Greg Clayton71337622011-02-24 22:24:29 +00002107 case eStateConnected:
Greg Clayton513c26c2011-01-29 07:10:55 +00002108 return eEventActionRetry;
2109
2110 case eStateStopped:
2111 case eStateCrashed:
Jim Ingham5aee1622010-08-09 23:31:02 +00002112 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002113 // During attach, prior to sending the eStateStopped event,
2114 // lldb_private::Process subclasses must set the process must set
2115 // the new process ID.
2116 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
Greg Clayton93d3c8332011-02-16 04:46:07 +00002117 m_process->CompleteAttach ();
Greg Clayton513c26c2011-01-29 07:10:55 +00002118 return eEventActionSuccess;
Jim Ingham5aee1622010-08-09 23:31:02 +00002119 }
Greg Clayton513c26c2011-01-29 07:10:55 +00002120
2121
2122 break;
2123 default:
2124 case eStateExited:
2125 case eStateInvalid:
2126 m_exit_string.assign ("No valid Process");
2127 return eEventActionExit;
2128 break;
Jim Inghambb3a2832011-01-29 01:49:25 +00002129 }
2130}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131
Jim Inghambb3a2832011-01-29 01:49:25 +00002132Process::NextEventAction::EventActionResult
2133Process::AttachCompletionHandler::HandleBeingInterrupted()
2134{
2135 return eEventActionSuccess;
2136}
2137
2138const char *
2139Process::AttachCompletionHandler::GetExitString ()
2140{
2141 return m_exit_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002142}
2143
2144Error
2145Process::Attach (lldb::pid_t attach_pid)
2146{
2147
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002148 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002149 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002150
Jim Ingham5aee1622010-08-09 23:31:02 +00002151 // Find the process and its architecture. Make sure it matches the architecture
2152 // of the current Target, and if not adjust it.
2153
Greg Clayton8b82f082011-04-12 05:54:46 +00002154 ProcessInstanceInfo process_info;
Greg Claytonded470d2011-03-19 01:12:21 +00002155 PlatformSP platform_sp (m_target.GetDebugger().GetPlatformList().GetSelectedPlatform ());
Greg Claytone996fd32011-03-08 22:40:15 +00002156 if (platform_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +00002157 {
Greg Claytone996fd32011-03-08 22:40:15 +00002158 if (platform_sp->GetProcessInfo (attach_pid, process_info))
2159 {
2160 const ArchSpec &process_arch = process_info.GetArchitecture();
2161 if (process_arch.IsValid())
2162 GetTarget().SetArchitecture(process_arch);
2163 }
Jim Ingham5aee1622010-08-09 23:31:02 +00002164 }
2165
Greg Clayton93d3c8332011-02-16 04:46:07 +00002166 m_dyld_ap.reset();
2167
Greg Claytonc982c762010-07-09 20:39:50 +00002168 Error error (WillAttachToProcessWithID(attach_pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002169 if (error.Success())
2170 {
Greg Clayton05faeb72010-10-07 04:19:01 +00002171 SetPublicState (eStateAttaching);
2172
Greg Claytonc982c762010-07-09 20:39:50 +00002173 error = DoAttachToProcessWithID (attach_pid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002174 if (error.Success())
2175 {
Jim Inghambb3a2832011-01-29 01:49:25 +00002176 SetNextEventAction(new Process::AttachCompletionHandler(this));
2177 StartPrivateStateThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002178 }
2179 else
2180 {
2181 if (GetID() != LLDB_INVALID_PROCESS_ID)
2182 {
2183 SetID (LLDB_INVALID_PROCESS_ID);
2184 const char *error_string = error.AsCString();
2185 if (error_string == NULL)
2186 error_string = "attach failed";
2187
2188 SetExitStatus(-1, error_string);
2189 }
2190 }
2191 }
2192 return error;
2193}
2194
2195Error
2196Process::Attach (const char *process_name, bool wait_for_launch)
2197{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002198 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002199 m_process_input_reader.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00002200
2201 // Find the process and its architecture. Make sure it matches the architecture
2202 // of the current Target, and if not adjust it.
Greg Claytone996fd32011-03-08 22:40:15 +00002203 Error error;
Jim Ingham5aee1622010-08-09 23:31:02 +00002204
Jim Ingham2ecb7422010-08-17 21:54:19 +00002205 if (!wait_for_launch)
Jim Ingham5aee1622010-08-09 23:31:02 +00002206 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002207 ProcessInstanceInfoList process_infos;
Greg Claytonded470d2011-03-19 01:12:21 +00002208 PlatformSP platform_sp (m_target.GetDebugger().GetPlatformList().GetSelectedPlatform ());
Greg Claytone996fd32011-03-08 22:40:15 +00002209 if (platform_sp)
Jim Ingham2ecb7422010-08-17 21:54:19 +00002210 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002211 ProcessInstanceInfoMatch match_info;
Greg Clayton32e0a752011-03-30 18:16:51 +00002212 match_info.GetProcessInfo().SetName(process_name);
2213 match_info.SetNameMatchType (eNameMatchEquals);
2214 platform_sp->FindProcesses (match_info, process_infos);
Greg Claytone996fd32011-03-08 22:40:15 +00002215 if (process_infos.GetSize() > 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002216 {
Greg Claytone996fd32011-03-08 22:40:15 +00002217 error.SetErrorStringWithFormat ("More than one process named %s\n", process_name);
2218 }
2219 else if (process_infos.GetSize() == 0)
2220 {
2221 error.SetErrorStringWithFormat ("Could not find a process named %s\n", process_name);
2222 }
2223 else
2224 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002225 ProcessInstanceInfo process_info;
Greg Claytone996fd32011-03-08 22:40:15 +00002226 if (process_infos.GetInfoAtIndex (0, process_info))
2227 {
2228 const ArchSpec &process_arch = process_info.GetArchitecture();
2229 if (process_arch.IsValid() && process_arch != GetTarget().GetArchitecture())
2230 {
2231 // Set the architecture on the target.
2232 GetTarget().SetArchitecture (process_arch);
2233 }
2234 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002235 }
2236 }
2237 else
Greg Claytone996fd32011-03-08 22:40:15 +00002238 {
2239 error.SetErrorString ("Invalid platform");
2240 }
2241 }
2242
2243 if (error.Success())
2244 {
2245 m_dyld_ap.reset();
2246
2247 error = WillAttachToProcessWithName(process_name, wait_for_launch);
2248 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002249 {
Greg Claytone996fd32011-03-08 22:40:15 +00002250 SetPublicState (eStateAttaching);
2251 error = DoAttachToProcessWithName (process_name, wait_for_launch);
2252 if (error.Fail())
2253 {
2254 if (GetID() != LLDB_INVALID_PROCESS_ID)
2255 {
2256 SetID (LLDB_INVALID_PROCESS_ID);
2257 const char *error_string = error.AsCString();
2258 if (error_string == NULL)
2259 error_string = "attach failed";
2260
2261 SetExitStatus(-1, error_string);
2262 }
2263 }
2264 else
2265 {
2266 SetNextEventAction(new Process::AttachCompletionHandler(this));
2267 StartPrivateStateThread();
2268 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002269 }
2270 }
2271 return error;
2272}
2273
Greg Clayton93d3c8332011-02-16 04:46:07 +00002274void
2275Process::CompleteAttach ()
2276{
2277 // Let the process subclass figure out at much as it can about the process
2278 // before we go looking for a dynamic loader plug-in.
2279 DidAttach();
2280
2281 // We have complete the attach, now it is time to find the dynamic loader
2282 // plug-in
Greg Clayton7a5388b2011-03-20 04:57:14 +00002283 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Greg Clayton93d3c8332011-02-16 04:46:07 +00002284 if (m_dyld_ap.get())
2285 m_dyld_ap->DidAttach();
2286
2287 // Figure out which one is the executable, and set that in our target:
2288 ModuleList &modules = m_target.GetImages();
2289
2290 size_t num_modules = modules.GetSize();
2291 for (int i = 0; i < num_modules; i++)
2292 {
2293 ModuleSP module_sp (modules.GetModuleAtIndex(i));
Greg Clayton8b82f082011-04-12 05:54:46 +00002294 if (module_sp && module_sp->IsExecutable())
Greg Clayton93d3c8332011-02-16 04:46:07 +00002295 {
2296 ModuleSP target_exe_module_sp (m_target.GetExecutableModule());
2297 if (target_exe_module_sp != module_sp)
2298 m_target.SetExecutableModule (module_sp, false);
2299 break;
2300 }
2301 }
2302}
2303
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002304Error
Greg Claytonb766a732011-02-04 01:58:07 +00002305Process::ConnectRemote (const char *remote_url)
2306{
Greg Claytonb766a732011-02-04 01:58:07 +00002307 m_abi_sp.reset();
2308 m_process_input_reader.reset();
2309
2310 // Find the process and its architecture. Make sure it matches the architecture
2311 // of the current Target, and if not adjust it.
2312
2313 Error error (DoConnectRemote (remote_url));
2314 if (error.Success())
2315 {
Greg Clayton71337622011-02-24 22:24:29 +00002316 if (GetID() != LLDB_INVALID_PROCESS_ID)
2317 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002318 EventSP event_sp;
2319 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
2320
2321 if (state == eStateStopped || state == eStateCrashed)
2322 {
2323 // If we attached and actually have a process on the other end, then
2324 // this ended up being the equivalent of an attach.
2325 CompleteAttach ();
2326
2327 // This delays passing the stopped event to listeners till
2328 // CompleteAttach gets a chance to complete...
2329 HandlePrivateEvent (event_sp);
2330
2331 }
Greg Clayton71337622011-02-24 22:24:29 +00002332 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002333
2334 if (PrivateStateThreadIsValid ())
2335 ResumePrivateStateThread ();
2336 else
2337 StartPrivateStateThread ();
Greg Claytonb766a732011-02-04 01:58:07 +00002338 }
2339 return error;
2340}
2341
2342
2343Error
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002344Process::Resume ()
2345{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002346 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002347 if (log)
Jim Ingham444586b2011-01-24 06:34:17 +00002348 log->Printf("Process::Resume() m_stop_id = %u, public state: %s private state: %s",
2349 m_stop_id,
2350 StateAsCString(m_public_state.GetValue()),
2351 StateAsCString(m_private_state.GetValue()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002352
2353 Error error (WillResume());
2354 // Tell the process it is about to resume before the thread list
2355 if (error.Success())
2356 {
Johnny Chenc4221e42010-12-02 20:53:05 +00002357 // Now let the thread list know we are about to resume so it
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002358 // can let all of our threads know that they are about to be
2359 // resumed. Threads will each be called with
2360 // Thread::WillResume(StateType) where StateType contains the state
2361 // that they are supposed to have when the process is resumed
2362 // (suspended/running/stepping). Threads should also check
2363 // their resume signal in lldb::Thread::GetResumeSignal()
2364 // to see if they are suppoed to start back up with a signal.
2365 if (m_thread_list.WillResume())
2366 {
2367 error = DoResume();
2368 if (error.Success())
2369 {
2370 DidResume();
2371 m_thread_list.DidResume();
Jim Ingham444586b2011-01-24 06:34:17 +00002372 if (log)
2373 log->Printf ("Process thinks the process has resumed.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002374 }
2375 }
2376 else
2377 {
Jim Ingham444586b2011-01-24 06:34:17 +00002378 error.SetErrorStringWithFormat("Process::WillResume() thread list returned false after WillResume");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002379 }
2380 }
Jim Ingham444586b2011-01-24 06:34:17 +00002381 else if (log)
2382 log->Printf ("Process::WillResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002383 return error;
2384}
2385
2386Error
2387Process::Halt ()
2388{
Jim Inghambb3a2832011-01-29 01:49:25 +00002389 // Pause our private state thread so we can ensure no one else eats
2390 // the stop event out from under us.
Jim Ingham0f16e732011-02-08 05:20:59 +00002391 Listener halt_listener ("lldb.process.halt_listener");
2392 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton3af9ea52010-11-18 05:57:03 +00002393
Jim Inghambb3a2832011-01-29 01:49:25 +00002394 EventSP event_sp;
Greg Clayton513c26c2011-01-29 07:10:55 +00002395 Error error (WillHalt());
Jim Inghambb3a2832011-01-29 01:49:25 +00002396
Greg Clayton513c26c2011-01-29 07:10:55 +00002397 if (error.Success())
Jim Inghambb3a2832011-01-29 01:49:25 +00002398 {
Jim Inghambb3a2832011-01-29 01:49:25 +00002399
Greg Clayton513c26c2011-01-29 07:10:55 +00002400 bool caused_stop = false;
2401
2402 // Ask the process subclass to actually halt our process
2403 error = DoHalt(caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002404 if (error.Success())
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002405 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002406 if (m_public_state.GetValue() == eStateAttaching)
2407 {
2408 SetExitStatus(SIGKILL, "Cancelled async attach.");
2409 Destroy ();
2410 }
2411 else
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002412 {
Jim Inghambb3a2832011-01-29 01:49:25 +00002413 // If "caused_stop" is true, then DoHalt stopped the process. If
2414 // "caused_stop" is false, the process was already stopped.
2415 // If the DoHalt caused the process to stop, then we want to catch
2416 // this event and set the interrupted bool to true before we pass
2417 // this along so clients know that the process was interrupted by
2418 // a halt command.
2419 if (caused_stop)
Greg Clayton3af9ea52010-11-18 05:57:03 +00002420 {
Jim Ingham0f16e732011-02-08 05:20:59 +00002421 // Wait for 1 second for the process to stop.
Jim Inghambb3a2832011-01-29 01:49:25 +00002422 TimeValue timeout_time;
2423 timeout_time = TimeValue::Now();
2424 timeout_time.OffsetWithSeconds(1);
Jim Ingham0f16e732011-02-08 05:20:59 +00002425 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
2426 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00002427
Jim Ingham0f16e732011-02-08 05:20:59 +00002428 if (!got_event || state == eStateInvalid)
Greg Clayton3af9ea52010-11-18 05:57:03 +00002429 {
Jim Inghambb3a2832011-01-29 01:49:25 +00002430 // We timeout out and didn't get a stop event...
Jim Ingham0f16e732011-02-08 05:20:59 +00002431 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton3af9ea52010-11-18 05:57:03 +00002432 }
2433 else
2434 {
Jim Inghambb3a2832011-01-29 01:49:25 +00002435 if (StateIsStoppedState (state))
2436 {
2437 // We caused the process to interrupt itself, so mark this
2438 // as such in the stop event so clients can tell an interrupted
2439 // process from a natural stop
2440 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
2441 }
2442 else
2443 {
2444 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2445 if (log)
2446 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
2447 error.SetErrorString ("Did not get stopped event after halt.");
2448 }
Greg Clayton3af9ea52010-11-18 05:57:03 +00002449 }
2450 }
Jim Inghambb3a2832011-01-29 01:49:25 +00002451 DidHalt();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002452 }
2453 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002454 }
Jim Inghambb3a2832011-01-29 01:49:25 +00002455 // Resume our private state thread before we post the event (if any)
Jim Ingham0f16e732011-02-08 05:20:59 +00002456 RestorePrivateProcessEvents();
Jim Inghambb3a2832011-01-29 01:49:25 +00002457
2458 // Post any event we might have consumed. If all goes well, we will have
2459 // stopped the process, intercepted the event and set the interrupted
2460 // bool in the event. Post it to the private event queue and that will end up
2461 // correctly setting the state.
2462 if (event_sp)
2463 m_private_state_broadcaster.BroadcastEvent(event_sp);
2464
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002465 return error;
2466}
2467
2468Error
2469Process::Detach ()
2470{
2471 Error error (WillDetach());
2472
2473 if (error.Success())
2474 {
2475 DisableAllBreakpointSites();
2476 error = DoDetach();
2477 if (error.Success())
2478 {
2479 DidDetach();
2480 StopPrivateStateThread();
2481 }
2482 }
2483 return error;
2484}
2485
2486Error
2487Process::Destroy ()
2488{
2489 Error error (WillDestroy());
2490 if (error.Success())
2491 {
2492 DisableAllBreakpointSites();
2493 error = DoDestroy();
2494 if (error.Success())
2495 {
2496 DidDestroy();
2497 StopPrivateStateThread();
2498 }
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002499 m_stdio_communication.StopReadThread();
2500 m_stdio_communication.Disconnect();
2501 if (m_process_input_reader && m_process_input_reader->IsActive())
2502 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2503 if (m_process_input_reader)
2504 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002505 }
2506 return error;
2507}
2508
2509Error
2510Process::Signal (int signal)
2511{
2512 Error error (WillSignal());
2513 if (error.Success())
2514 {
2515 error = DoSignal(signal);
2516 if (error.Success())
2517 DidSignal();
2518 }
2519 return error;
2520}
2521
Greg Clayton514487e2011-02-15 21:59:32 +00002522lldb::ByteOrder
2523Process::GetByteOrder () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002524{
Greg Clayton514487e2011-02-15 21:59:32 +00002525 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002526}
2527
2528uint32_t
Greg Clayton514487e2011-02-15 21:59:32 +00002529Process::GetAddressByteSize () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002530{
Greg Clayton514487e2011-02-15 21:59:32 +00002531 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002532}
2533
Greg Clayton514487e2011-02-15 21:59:32 +00002534
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002535bool
2536Process::ShouldBroadcastEvent (Event *event_ptr)
2537{
2538 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
2539 bool return_value = true;
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002540 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002541
2542 switch (state)
2543 {
Greg Claytonb766a732011-02-04 01:58:07 +00002544 case eStateConnected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002545 case eStateAttaching:
2546 case eStateLaunching:
2547 case eStateDetached:
2548 case eStateExited:
2549 case eStateUnloaded:
2550 // These events indicate changes in the state of the debugging session, always report them.
2551 return_value = true;
2552 break;
2553 case eStateInvalid:
2554 // We stopped for no apparent reason, don't report it.
2555 return_value = false;
2556 break;
2557 case eStateRunning:
2558 case eStateStepping:
2559 // If we've started the target running, we handle the cases where we
2560 // are already running and where there is a transition from stopped to
2561 // running differently.
2562 // running -> running: Automatically suppress extra running events
2563 // stopped -> running: Report except when there is one or more no votes
2564 // and no yes votes.
2565 SynchronouslyNotifyStateChanged (state);
2566 switch (m_public_state.GetValue())
2567 {
2568 case eStateRunning:
2569 case eStateStepping:
2570 // We always suppress multiple runnings with no PUBLIC stop in between.
2571 return_value = false;
2572 break;
2573 default:
2574 // TODO: make this work correctly. For now always report
2575 // run if we aren't running so we don't miss any runnning
2576 // events. If I run the lldb/test/thread/a.out file and
2577 // break at main.cpp:58, run and hit the breakpoints on
2578 // multiple threads, then somehow during the stepping over
2579 // of all breakpoints no run gets reported.
2580 return_value = true;
2581
2582 // This is a transition from stop to run.
2583 switch (m_thread_list.ShouldReportRun (event_ptr))
2584 {
2585 case eVoteYes:
2586 case eVoteNoOpinion:
2587 return_value = true;
2588 break;
2589 case eVoteNo:
2590 return_value = false;
2591 break;
2592 }
2593 break;
2594 }
2595 break;
2596 case eStateStopped:
2597 case eStateCrashed:
2598 case eStateSuspended:
2599 {
2600 // We've stopped. First see if we're going to restart the target.
2601 // If we are going to stop, then we always broadcast the event.
2602 // If we aren't going to stop, let the thread plans decide if we're going to report this event.
Jim Inghamb01e7422010-06-19 04:45:32 +00002603 // If no thread has an opinion, we don't report it.
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002604 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002605 {
Greg Clayton3af9ea52010-11-18 05:57:03 +00002606 if (log)
2607 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", event_ptr, StateAsCString(state));
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002608 return true;
2609 }
2610 else
2611 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002612 RefreshStateAfterStop ();
2613
2614 if (m_thread_list.ShouldStop (event_ptr) == false)
2615 {
2616 switch (m_thread_list.ShouldReportStop (event_ptr))
2617 {
2618 case eVoteYes:
2619 Process::ProcessEventData::SetRestartedInEvent (event_ptr, true);
Johnny Chen3c230652010-10-14 00:54:32 +00002620 // Intentional fall-through here.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002621 case eVoteNoOpinion:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002622 case eVoteNo:
2623 return_value = false;
2624 break;
2625 }
2626
2627 if (log)
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002628 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002629 Resume ();
2630 }
2631 else
2632 {
2633 return_value = true;
2634 SynchronouslyNotifyStateChanged (state);
2635 }
2636 }
2637 }
2638 }
2639
2640 if (log)
2641 log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO");
2642 return return_value;
2643}
2644
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002645
2646bool
2647Process::StartPrivateStateThread ()
2648{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002649 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002650
Greg Clayton8b82f082011-04-12 05:54:46 +00002651 bool already_running = PrivateStateThreadIsValid ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002652 if (log)
Greg Clayton8b82f082011-04-12 05:54:46 +00002653 log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread");
2654
2655 if (already_running)
2656 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002657
2658 // Create a thread that watches our internal state and controls which
2659 // events make it to clients (into the DCProcess event queue).
Greg Clayton3e06bd92011-01-09 21:07:35 +00002660 char thread_name[1024];
2661 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%i)>", GetID());
2662 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Greg Clayton2da6d492011-02-08 01:34:25 +00002663 return IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002664}
2665
2666void
2667Process::PausePrivateStateThread ()
2668{
2669 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
2670}
2671
2672void
2673Process::ResumePrivateStateThread ()
2674{
2675 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
2676}
2677
2678void
2679Process::StopPrivateStateThread ()
2680{
Greg Clayton8b82f082011-04-12 05:54:46 +00002681 if (PrivateStateThreadIsValid ())
2682 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002683}
2684
2685void
2686Process::ControlPrivateStateThread (uint32_t signal)
2687{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002688 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002689
2690 assert (signal == eBroadcastInternalStateControlStop ||
2691 signal == eBroadcastInternalStateControlPause ||
2692 signal == eBroadcastInternalStateControlResume);
2693
2694 if (log)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00002695 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002696
Greg Clayton7ecb3a02011-01-22 17:43:17 +00002697 // Signal the private state thread. First we should copy this is case the
2698 // thread starts exiting since the private state thread will NULL this out
2699 // when it exits
2700 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton2da6d492011-02-08 01:34:25 +00002701 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002702 {
2703 TimeValue timeout_time;
2704 bool timed_out;
2705
2706 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
2707
2708 timeout_time = TimeValue::Now();
2709 timeout_time.OffsetWithSeconds(2);
2710 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
2711 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2712
2713 if (signal == eBroadcastInternalStateControlStop)
2714 {
2715 if (timed_out)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00002716 Host::ThreadCancel (private_state_thread, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002717
2718 thread_result_t result = NULL;
Greg Clayton7ecb3a02011-01-22 17:43:17 +00002719 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Clayton49182ed2010-07-22 18:34:21 +00002720 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002721 }
2722 }
2723}
2724
2725void
2726Process::HandlePrivateEvent (EventSP &event_sp)
2727{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002728 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghambb3a2832011-01-29 01:49:25 +00002729
Greg Clayton414f5d32011-01-25 02:58:48 +00002730 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00002731
2732 // First check to see if anybody wants a shot at this event:
Jim Ingham754ab982011-01-29 04:05:41 +00002733 if (m_next_event_action_ap.get() != NULL)
Jim Inghambb3a2832011-01-29 01:49:25 +00002734 {
Jim Ingham754ab982011-01-29 04:05:41 +00002735 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Inghambb3a2832011-01-29 01:49:25 +00002736 switch (action_result)
2737 {
2738 case NextEventAction::eEventActionSuccess:
2739 SetNextEventAction(NULL);
2740 break;
2741 case NextEventAction::eEventActionRetry:
2742 break;
2743 case NextEventAction::eEventActionExit:
Jim Ingham2a5fdd42011-01-29 01:57:31 +00002744 // Handle Exiting Here. If we already got an exited event,
2745 // we should just propagate it. Otherwise, swallow this event,
2746 // and set our state to exit so the next event will kill us.
2747 if (new_state != eStateExited)
2748 {
2749 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham754ab982011-01-29 04:05:41 +00002750 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham2a5fdd42011-01-29 01:57:31 +00002751 SetNextEventAction(NULL);
2752 return;
2753 }
2754 SetNextEventAction(NULL);
Jim Inghambb3a2832011-01-29 01:49:25 +00002755 break;
2756 }
2757 }
2758
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002759 // See if we should broadcast this state to external clients?
2760 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002761
2762 if (should_broadcast)
2763 {
2764 if (log)
2765 {
Greg Clayton414f5d32011-01-25 02:58:48 +00002766 log->Printf ("Process::%s (pid = %i) broadcasting new state %s (old state %s) to %s",
2767 __FUNCTION__,
2768 GetID(),
2769 StateAsCString(new_state),
2770 StateAsCString (GetState ()),
2771 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002772 }
Jim Ingham9575d842011-03-11 03:53:59 +00002773 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton414f5d32011-01-25 02:58:48 +00002774 if (StateIsRunningState (new_state))
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002775 PushProcessInputReader ();
2776 else
2777 PopProcessInputReader ();
Jim Ingham9575d842011-03-11 03:53:59 +00002778
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002779 BroadcastEvent (event_sp);
2780 }
2781 else
2782 {
2783 if (log)
2784 {
Greg Clayton414f5d32011-01-25 02:58:48 +00002785 log->Printf ("Process::%s (pid = %i) suppressing state %s (old state %s): should_broadcast == false",
2786 __FUNCTION__,
2787 GetID(),
2788 StateAsCString(new_state),
2789 StateAsCString (GetState ()),
2790 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002791 }
2792 }
2793}
2794
2795void *
2796Process::PrivateStateThread (void *arg)
2797{
2798 Process *proc = static_cast<Process*> (arg);
2799 void *result = proc->RunPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002800 return result;
2801}
2802
2803void *
2804Process::RunPrivateStateThread ()
2805{
2806 bool control_only = false;
2807 m_private_state_control_wait.SetValue (false, eBroadcastNever);
2808
Greg Clayton2d4edfb2010-11-06 01:53:30 +00002809 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002810 if (log)
2811 log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID());
2812
2813 bool exit_now = false;
2814 while (!exit_now)
2815 {
2816 EventSP event_sp;
2817 WaitForEventsPrivate (NULL, event_sp, control_only);
2818 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
2819 {
2820 switch (event_sp->GetType())
2821 {
2822 case eBroadcastInternalStateControlStop:
2823 exit_now = true;
2824 continue; // Go to next loop iteration so we exit without
2825 break; // doing any internal state managment below
2826
2827 case eBroadcastInternalStateControlPause:
2828 control_only = true;
2829 break;
2830
2831 case eBroadcastInternalStateControlResume:
2832 control_only = false;
2833 break;
2834 }
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002835
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002836 if (log)
2837 log->Printf ("Process::%s (arg = %p, pid = %i) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType());
2838
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002839 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002840 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002841 }
2842
2843
2844 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
2845
2846 if (internal_state != eStateInvalid)
2847 {
2848 HandlePrivateEvent (event_sp);
2849 }
2850
Greg Clayton58d1c9a2010-10-18 04:14:23 +00002851 if (internal_state == eStateInvalid ||
2852 internal_state == eStateExited ||
2853 internal_state == eStateDetached )
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002854 {
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002855 if (log)
2856 log->Printf ("Process::%s (arg = %p, pid = %i) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state));
2857
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002858 break;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002859 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002860 }
2861
Caroline Tice20ad3c42010-10-29 21:48:37 +00002862 // Verify log is still enabled before attempting to write to it...
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002863 if (log)
2864 log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID());
2865
Greg Clayton6ed95942011-01-22 07:12:45 +00002866 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
2867 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002868 return NULL;
2869}
2870
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002871//------------------------------------------------------------------
2872// Process Event Data
2873//------------------------------------------------------------------
2874
2875Process::ProcessEventData::ProcessEventData () :
2876 EventData (),
2877 m_process_sp (),
2878 m_state (eStateInvalid),
Greg Claytonc982c762010-07-09 20:39:50 +00002879 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00002880 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002881 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002882{
2883}
2884
2885Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
2886 EventData (),
2887 m_process_sp (process_sp),
2888 m_state (state),
Greg Claytonc982c762010-07-09 20:39:50 +00002889 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00002890 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00002891 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002892{
2893}
2894
2895Process::ProcessEventData::~ProcessEventData()
2896{
2897}
2898
2899const ConstString &
2900Process::ProcessEventData::GetFlavorString ()
2901{
2902 static ConstString g_flavor ("Process::ProcessEventData");
2903 return g_flavor;
2904}
2905
2906const ConstString &
2907Process::ProcessEventData::GetFlavor () const
2908{
2909 return ProcessEventData::GetFlavorString ();
2910}
2911
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002912void
2913Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
2914{
2915 // This function gets called twice for each event, once when the event gets pulled
Jim Inghama8604692011-05-22 21:45:01 +00002916 // off of the private process event queue, and then any number of times, first when it gets pulled off of
2917 // the public event queue, then other times when we're pretending that this is where we stopped at the
2918 // end of expression evaluation. m_update_state is used to distinguish these
2919 // three cases; it is 0 when we're just pulling it off for private handling,
2920 // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002921
Jim Inghama8604692011-05-22 21:45:01 +00002922 if (m_update_state != 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002923 return;
2924
2925 m_process_sp->SetPublicState (m_state);
2926
2927 // If we're stopped and haven't restarted, then do the breakpoint commands here:
2928 if (m_state == eStateStopped && ! m_restarted)
2929 {
2930 int num_threads = m_process_sp->GetThreadList().GetSize();
2931 int idx;
Greg Claytonf4b47e12010-08-04 01:40:35 +00002932
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002933 for (idx = 0; idx < num_threads; ++idx)
2934 {
2935 lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx);
2936
Jim Inghamb15bfc72010-10-20 00:39:53 +00002937 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
2938 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002939 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00002940 stop_info_sp->PerformAction(event_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002941 }
2942 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00002943
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00002944 // The stop action might restart the target. If it does, then we want to mark that in the
2945 // event so that whoever is receiving it will know to wait for the running event and reflect
2946 // that state appropriately.
2947
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002948 if (m_process_sp->GetPrivateState() == eStateRunning)
2949 SetRestarted(true);
Jim Ingham9575d842011-03-11 03:53:59 +00002950 else
2951 {
2952 // Finally, if we didn't restart, run the Stop Hooks here:
2953 // They might also restart the target, so watch for that.
2954 m_process_sp->GetTarget().RunStopHooks();
2955 if (m_process_sp->GetPrivateState() == eStateRunning)
2956 SetRestarted(true);
2957 }
2958
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002959 }
2960}
2961
2962void
2963Process::ProcessEventData::Dump (Stream *s) const
2964{
2965 if (m_process_sp)
2966 s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID());
2967
Greg Clayton8b82f082011-04-12 05:54:46 +00002968 s->Printf("state = %s", StateAsCString(GetState()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002969}
2970
2971const Process::ProcessEventData *
2972Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
2973{
2974 if (event_ptr)
2975 {
2976 const EventData *event_data = event_ptr->GetData();
2977 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
2978 return static_cast <const ProcessEventData *> (event_ptr->GetData());
2979 }
2980 return NULL;
2981}
2982
2983ProcessSP
2984Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
2985{
2986 ProcessSP process_sp;
2987 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2988 if (data)
2989 process_sp = data->GetProcessSP();
2990 return process_sp;
2991}
2992
2993StateType
2994Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
2995{
2996 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
2997 if (data == NULL)
2998 return eStateInvalid;
2999 else
3000 return data->GetState();
3001}
3002
3003bool
3004Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
3005{
3006 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
3007 if (data == NULL)
3008 return false;
3009 else
3010 return data->GetRestarted();
3011}
3012
3013void
3014Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
3015{
3016 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
3017 if (data != NULL)
3018 data->SetRestarted(new_value);
3019}
3020
3021bool
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003022Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
3023{
3024 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
3025 if (data == NULL)
3026 return false;
3027 else
3028 return data->GetInterrupted ();
3029}
3030
3031void
3032Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
3033{
3034 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
3035 if (data != NULL)
3036 data->SetInterrupted(new_value);
3037}
3038
3039bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003040Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
3041{
3042 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
3043 if (data)
3044 {
3045 data->SetUpdateStateOnRemoval();
3046 return true;
3047 }
3048 return false;
3049}
3050
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003051void
Greg Clayton0603aa92010-10-04 01:05:56 +00003052Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003053{
3054 exe_ctx.target = &m_target;
3055 exe_ctx.process = this;
3056 exe_ctx.thread = NULL;
3057 exe_ctx.frame = NULL;
3058}
3059
3060lldb::ProcessSP
3061Process::GetSP ()
3062{
3063 return GetTarget().GetProcessSP();
3064}
3065
Greg Claytone996fd32011-03-08 22:40:15 +00003066//uint32_t
3067//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
3068//{
3069// return 0;
3070//}
3071//
3072//ArchSpec
3073//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
3074//{
3075// return Host::GetArchSpecForExistingProcess (pid);
3076//}
3077//
3078//ArchSpec
3079//Process::GetArchSpecForExistingProcess (const char *process_name)
3080//{
3081// return Host::GetArchSpecForExistingProcess (process_name);
3082//}
3083//
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003084void
3085Process::AppendSTDOUT (const char * s, size_t len)
3086{
Greg Clayton3af9ea52010-11-18 05:57:03 +00003087 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003088 m_stdout_data.append (s, len);
3089
Greg Claytona9ff3062010-12-05 19:16:56 +00003090 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (GetTarget().GetProcessSP(), GetState()));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003091}
3092
3093void
3094Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
3095{
3096 Process *process = (Process *) baton;
3097 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
3098}
3099
3100size_t
3101Process::ProcessInputReaderCallback (void *baton,
3102 InputReader &reader,
3103 lldb::InputReaderAction notification,
3104 const char *bytes,
3105 size_t bytes_len)
3106{
3107 Process *process = (Process *) baton;
3108
3109 switch (notification)
3110 {
3111 case eInputReaderActivate:
3112 break;
3113
3114 case eInputReaderDeactivate:
3115 break;
3116
3117 case eInputReaderReactivate:
3118 break;
3119
Caroline Tice969ed3d2011-05-02 20:41:46 +00003120 case eInputReaderAsynchronousOutputWritten:
3121 break;
3122
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003123 case eInputReaderGotToken:
3124 {
3125 Error error;
3126 process->PutSTDIN (bytes, bytes_len, error);
3127 }
3128 break;
3129
Caroline Ticeefed6132010-11-19 20:47:54 +00003130 case eInputReaderInterrupt:
3131 process->Halt ();
3132 break;
3133
3134 case eInputReaderEndOfFile:
3135 process->AppendSTDOUT ("^D", 2);
3136 break;
3137
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003138 case eInputReaderDone:
3139 break;
3140
3141 }
3142
3143 return bytes_len;
3144}
3145
3146void
3147Process::ResetProcessInputReader ()
3148{
3149 m_process_input_reader.reset();
3150}
3151
3152void
3153Process::SetUpProcessInputReader (int file_descriptor)
3154{
3155 // First set up the Read Thread for reading/handling process I/O
3156
3157 std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true));
3158
3159 if (conn_ap.get())
3160 {
3161 m_stdio_communication.SetConnection (conn_ap.release());
3162 if (m_stdio_communication.IsConnected())
3163 {
3164 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
3165 m_stdio_communication.StartReadThread();
3166
3167 // Now read thread is set up, set up input reader.
3168
3169 if (!m_process_input_reader.get())
3170 {
3171 m_process_input_reader.reset (new InputReader(m_target.GetDebugger()));
3172 Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback,
3173 this,
3174 eInputReaderGranularityByte,
3175 NULL,
3176 NULL,
3177 false));
3178
3179 if (err.Fail())
3180 m_process_input_reader.reset();
3181 }
3182 }
3183 }
3184}
3185
3186void
3187Process::PushProcessInputReader ()
3188{
3189 if (m_process_input_reader && !m_process_input_reader->IsActive())
3190 m_target.GetDebugger().PushInputReader (m_process_input_reader);
3191}
3192
3193void
3194Process::PopProcessInputReader ()
3195{
3196 if (m_process_input_reader && m_process_input_reader->IsActive())
3197 m_target.GetDebugger().PopInputReader (m_process_input_reader);
3198}
3199
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003200// The process needs to know about installed plug-ins
Greg Clayton99d0faf2010-11-18 23:32:35 +00003201void
Caroline Tice20bd37f2011-03-10 22:14:10 +00003202Process::SettingsInitialize ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003203{
Greg Claytone0d378b2011-03-24 21:19:54 +00003204 static std::vector<OptionEnumValueElement> g_plugins;
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003205
3206 int i=0;
3207 const char *name;
3208 OptionEnumValueElement option_enum;
3209 while ((name = PluginManager::GetProcessPluginNameAtIndex (i)) != NULL)
3210 {
3211 if (name)
3212 {
3213 option_enum.value = i;
3214 option_enum.string_value = name;
3215 option_enum.usage = PluginManager::GetProcessPluginDescriptionAtIndex (i);
3216 g_plugins.push_back (option_enum);
3217 }
3218 ++i;
3219 }
3220 option_enum.value = 0;
3221 option_enum.string_value = NULL;
3222 option_enum.usage = NULL;
3223 g_plugins.push_back (option_enum);
3224
3225 for (i=0; (name = SettingsController::instance_settings_table[i].var_name); ++i)
3226 {
3227 if (::strcmp (name, "plugin") == 0)
3228 {
3229 SettingsController::instance_settings_table[i].enum_values = &g_plugins[0];
3230 break;
3231 }
3232 }
Greg Clayton99d0faf2010-11-18 23:32:35 +00003233 UserSettingsControllerSP &usc = GetSettingsController();
3234 usc.reset (new SettingsController);
3235 UserSettingsController::InitializeSettingsController (usc,
3236 SettingsController::global_settings_table,
3237 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:10 +00003238
3239 // Now call SettingsInitialize() for each 'child' of Process settings
3240 Thread::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00003241}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003242
Greg Clayton99d0faf2010-11-18 23:32:35 +00003243void
Caroline Tice20bd37f2011-03-10 22:14:10 +00003244Process::SettingsTerminate ()
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003245{
Caroline Tice20bd37f2011-03-10 22:14:10 +00003246 // Must call SettingsTerminate() on each 'child' of Process settings before terminating Process settings.
3247
3248 Thread::SettingsTerminate ();
3249
3250 // Now terminate Process Settings.
3251
Greg Clayton99d0faf2010-11-18 23:32:35 +00003252 UserSettingsControllerSP &usc = GetSettingsController();
3253 UserSettingsController::FinalizeSettingsController (usc);
3254 usc.reset();
3255}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003256
Greg Clayton99d0faf2010-11-18 23:32:35 +00003257UserSettingsControllerSP &
3258Process::GetSettingsController ()
3259{
3260 static UserSettingsControllerSP g_settings_controller;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003261 return g_settings_controller;
3262}
3263
Caroline Tice1559a462010-09-27 00:30:10 +00003264void
3265Process::UpdateInstanceName ()
3266{
3267 ModuleSP module_sp = GetTarget().GetExecutableModule();
3268 if (module_sp)
3269 {
3270 StreamString sstr;
3271 sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString());
3272
Greg Claytondbe54502010-11-19 03:46:01 +00003273 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
Greg Clayton8b82f082011-04-12 05:54:46 +00003274 sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:10 +00003275 }
3276}
3277
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003278ExecutionResults
Jim Inghamf48169b2010-11-30 02:22:11 +00003279Process::RunThreadPlan (ExecutionContext &exe_ctx,
3280 lldb::ThreadPlanSP &thread_plan_sp,
3281 bool stop_others,
3282 bool try_all_threads,
3283 bool discard_on_error,
3284 uint32_t single_thread_timeout_usec,
3285 Stream &errors)
3286{
3287 ExecutionResults return_value = eExecutionSetupError;
3288
Jim Ingham77787032011-01-20 02:03:18 +00003289 if (thread_plan_sp.get() == NULL)
3290 {
3291 errors.Printf("RunThreadPlan called with empty thread plan.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003292 return eExecutionSetupError;
Jim Ingham77787032011-01-20 02:03:18 +00003293 }
3294
Jim Ingham17e5c4e2011-05-17 22:24:54 +00003295 // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes.
3296 // For that to be true the plan can't be private - since private plans suppress themselves in the
3297 // GetCompletedPlan call.
3298
3299 bool orig_plan_private = thread_plan_sp->GetPrivate();
3300 thread_plan_sp->SetPrivate(false);
3301
Jim Ingham444586b2011-01-24 06:34:17 +00003302 if (m_private_state.GetValue() != eStateStopped)
3303 {
3304 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003305 return eExecutionSetupError;
Jim Ingham444586b2011-01-24 06:34:17 +00003306 }
3307
Jim Inghamf48169b2010-11-30 02:22:11 +00003308 // Save this value for restoration of the execution context after we run
Greg Clayton92bb12c2011-05-19 18:17:41 +00003309 const uint32_t thread_idx_id = exe_ctx.thread->GetIndexID();
Jim Inghamf48169b2010-11-30 02:22:11 +00003310
3311 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
3312 // so we should arrange to reset them as well.
3313
3314 lldb::ThreadSP selected_thread_sp = exe_ctx.process->GetThreadList().GetSelectedThread();
3315 lldb::StackFrameSP selected_frame_sp;
3316
3317 uint32_t selected_tid;
3318 if (selected_thread_sp != NULL)
3319 {
3320 selected_tid = selected_thread_sp->GetIndexID();
3321 selected_frame_sp = selected_thread_sp->GetSelectedFrame();
3322 }
3323 else
3324 {
3325 selected_tid = LLDB_INVALID_THREAD_ID;
3326 }
3327
3328 exe_ctx.thread->QueueThreadPlan(thread_plan_sp, true);
3329
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00003330 Listener listener("lldb.process.listener.run-thread-plan");
Jim Ingham0f16e732011-02-08 05:20:59 +00003331
3332 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
3333 // restored on exit to the function.
3334
3335 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Jim Ingham444586b2011-01-24 06:34:17 +00003336
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00003337 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham77787032011-01-20 02:03:18 +00003338 if (log)
3339 {
3340 StreamString s;
3341 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Jim Ingham0f16e732011-02-08 05:20:59 +00003342 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4x to run thread plan \"%s\".",
3343 exe_ctx.thread->GetIndexID(),
3344 exe_ctx.thread->GetID(),
3345 s.GetData());
Jim Ingham77787032011-01-20 02:03:18 +00003346 }
3347
Jim Ingham0f16e732011-02-08 05:20:59 +00003348 bool got_event;
3349 lldb::EventSP event_sp;
3350 lldb::StateType stop_state = lldb::eStateInvalid;
Jim Inghamf48169b2010-11-30 02:22:11 +00003351
3352 TimeValue* timeout_ptr = NULL;
3353 TimeValue real_timeout;
3354
Jim Ingham0f16e732011-02-08 05:20:59 +00003355 bool first_timeout = true;
3356 bool do_resume = true;
Jim Inghamf48169b2010-11-30 02:22:11 +00003357
Jim Inghamf48169b2010-11-30 02:22:11 +00003358 while (1)
3359 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003360 // We usually want to resume the process if we get to the top of the loop.
3361 // The only exception is if we get two running events with no intervening
3362 // stop, which can happen, we will just wait for then next stop event.
Jim Inghamf48169b2010-11-30 02:22:11 +00003363
Jim Ingham0f16e732011-02-08 05:20:59 +00003364 if (do_resume)
Jim Inghamf48169b2010-11-30 02:22:11 +00003365 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003366 // Do the initial resume and wait for the running event before going further.
3367
3368 Error resume_error = exe_ctx.process->Resume ();
3369 if (!resume_error.Success())
3370 {
3371 errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString());
Greg Claytone0d378b2011-03-24 21:19:54 +00003372 return_value = eExecutionSetupError;
Jim Ingham0f16e732011-02-08 05:20:59 +00003373 break;
3374 }
3375
3376 real_timeout = TimeValue::Now();
3377 real_timeout.OffsetWithMicroSeconds(500000);
3378 timeout_ptr = &real_timeout;
3379
3380 got_event = listener.WaitForEvent(NULL, event_sp);
3381 if (!got_event)
3382 {
3383 if (log)
3384 log->Printf("Didn't get any event after initial resume, exiting.");
3385
3386 errors.Printf("Didn't get any event after initial resume, exiting.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003387 return_value = eExecutionSetupError;
Jim Ingham0f16e732011-02-08 05:20:59 +00003388 break;
3389 }
3390
3391 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3392 if (stop_state != eStateRunning)
3393 {
3394 if (log)
3395 log->Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state));
3396
3397 errors.Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state));
Greg Claytone0d378b2011-03-24 21:19:54 +00003398 return_value = eExecutionSetupError;
Jim Ingham0f16e732011-02-08 05:20:59 +00003399 break;
3400 }
3401
3402 if (log)
3403 log->Printf ("Resuming succeeded.");
3404 // We need to call the function synchronously, so spin waiting for it to return.
3405 // If we get interrupted while executing, we're going to lose our context, and
3406 // won't be able to gather the result at this point.
3407 // We set the timeout AFTER the resume, since the resume takes some time and we
3408 // don't want to charge that to the timeout.
3409
3410 if (single_thread_timeout_usec != 0)
3411 {
3412 real_timeout = TimeValue::Now();
3413 if (first_timeout)
3414 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
3415 else
3416 real_timeout.OffsetWithSeconds(10);
3417
3418 timeout_ptr = &real_timeout;
3419 }
3420 }
3421 else
3422 {
3423 if (log)
3424 log->Printf ("Handled an extra running event.");
3425 do_resume = true;
3426 }
3427
3428 // Now wait for the process to stop again:
3429 stop_state = lldb::eStateInvalid;
3430 event_sp.reset();
3431 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
3432
3433 if (got_event)
3434 {
3435 if (event_sp.get())
3436 {
3437 bool keep_going = false;
3438 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3439 if (log)
3440 log->Printf("In while loop, got event: %s.", StateAsCString(stop_state));
3441
3442 switch (stop_state)
3443 {
3444 case lldb::eStateStopped:
Jim Ingham160f78c2011-05-17 01:10:11 +00003445 {
Greg Clayton54e8ac52011-06-03 22:12:42 +00003446 // Yay, we're done. Now make sure that our thread plan actually completed.
3447 ThreadSP thread_sp = exe_ctx.process->GetThreadList().FindThreadByIndexID (thread_idx_id);
3448 if (!thread_sp)
Jim Ingham160f78c2011-05-17 01:10:11 +00003449 {
Greg Clayton54e8ac52011-06-03 22:12:42 +00003450 // Ooh, our thread has vanished. Unlikely that this was successful execution...
Jim Ingham160f78c2011-05-17 01:10:11 +00003451 if (log)
Greg Clayton54e8ac52011-06-03 22:12:42 +00003452 log->Printf ("Execution completed but our thread (index-id=%u) has vanished.", thread_idx_id);
3453 return_value = eExecutionInterrupted;
Jim Ingham160f78c2011-05-17 01:10:11 +00003454 }
3455 else
3456 {
Greg Clayton54e8ac52011-06-03 22:12:42 +00003457 StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
3458 StopReason stop_reason = eStopReasonInvalid;
3459 if (stop_info_sp)
3460 stop_reason = stop_info_sp->GetStopReason();
3461 if (stop_reason == eStopReasonPlanComplete)
3462 {
3463 if (log)
3464 log->Printf ("Execution completed successfully.");
3465 // Now mark this plan as private so it doesn't get reported as the stop reason
3466 // after this point.
3467 if (thread_plan_sp)
3468 thread_plan_sp->SetPrivate (orig_plan_private);
3469 return_value = eExecutionCompleted;
3470 }
3471 else
3472 {
3473 if (log)
3474 log->Printf ("Thread plan didn't successfully complete.");
3475
3476 return_value = eExecutionInterrupted;
3477 }
Jim Ingham160f78c2011-05-17 01:10:11 +00003478 }
Greg Clayton54e8ac52011-06-03 22:12:42 +00003479 }
3480 break;
3481
Jim Ingham0f16e732011-02-08 05:20:59 +00003482 case lldb::eStateCrashed:
3483 if (log)
3484 log->Printf ("Execution crashed.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003485 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003486 break;
Greg Clayton54e8ac52011-06-03 22:12:42 +00003487
Jim Ingham0f16e732011-02-08 05:20:59 +00003488 case lldb::eStateRunning:
3489 do_resume = false;
3490 keep_going = true;
3491 break;
Greg Clayton54e8ac52011-06-03 22:12:42 +00003492
Jim Ingham0f16e732011-02-08 05:20:59 +00003493 default:
3494 if (log)
3495 log->Printf("Execution stopped with unexpected state: %s.", StateAsCString(stop_state));
Jim Ingham160f78c2011-05-17 01:10:11 +00003496
3497 errors.Printf ("Execution stopped with unexpected state.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003498 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003499 break;
3500 }
3501 if (keep_going)
3502 continue;
3503 else
3504 break;
3505 }
3506 else
3507 {
3508 if (log)
3509 log->Printf ("got_event was true, but the event pointer was null. How odd...");
Greg Claytone0d378b2011-03-24 21:19:54 +00003510 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003511 break;
3512 }
3513 }
3514 else
3515 {
3516 // If we didn't get an event that means we've timed out...
3517 // We will interrupt the process here. Depending on what we were asked to do we will
3518 // either exit, or try with all threads running for the same timeout.
Jim Inghamf48169b2010-11-30 02:22:11 +00003519 // Not really sure what to do if Halt fails here...
Jim Ingham0f16e732011-02-08 05:20:59 +00003520
Stephen Wilson78a4feb2011-01-12 04:20:03 +00003521 if (log) {
Jim Inghamf48169b2010-11-30 02:22:11 +00003522 if (try_all_threads)
Jim Ingham0f16e732011-02-08 05:20:59 +00003523 {
3524 if (first_timeout)
3525 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
3526 "trying with all threads enabled.",
3527 single_thread_timeout_usec);
3528 else
3529 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
3530 "and timeout: %d timed out.",
3531 single_thread_timeout_usec);
3532 }
Jim Inghamf48169b2010-11-30 02:22:11 +00003533 else
Jim Ingham0f16e732011-02-08 05:20:59 +00003534 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
3535 "halt and abandoning execution.",
Jim Inghamf48169b2010-11-30 02:22:11 +00003536 single_thread_timeout_usec);
Stephen Wilson78a4feb2011-01-12 04:20:03 +00003537 }
Jim Inghamf48169b2010-11-30 02:22:11 +00003538
Jim Inghame22e88b2011-01-22 01:30:53 +00003539 Error halt_error = exe_ctx.process->Halt();
Jim Inghame22e88b2011-01-22 01:30:53 +00003540 if (halt_error.Success())
Jim Inghamf48169b2010-11-30 02:22:11 +00003541 {
Jim Inghamf48169b2010-11-30 02:22:11 +00003542 if (log)
Greg Clayton414f5d32011-01-25 02:58:48 +00003543 log->Printf ("Process::RunThreadPlan(): Halt succeeded.");
Jim Inghamf48169b2010-11-30 02:22:11 +00003544
Jim Ingham0f16e732011-02-08 05:20:59 +00003545 // If halt succeeds, it always produces a stopped event. Wait for that:
3546
3547 real_timeout = TimeValue::Now();
3548 real_timeout.OffsetWithMicroSeconds(500000);
3549
3550 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Jim Inghamf48169b2010-11-30 02:22:11 +00003551
3552 if (got_event)
3553 {
3554 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3555 if (log)
3556 {
Greg Clayton414f5d32011-01-25 02:58:48 +00003557 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
Jim Ingham0f16e732011-02-08 05:20:59 +00003558 if (stop_state == lldb::eStateStopped
3559 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
Jim Inghamf48169b2010-11-30 02:22:11 +00003560 log->Printf (" Event was the Halt interruption event.");
3561 }
3562
Jim Ingham0f16e732011-02-08 05:20:59 +00003563 if (stop_state == lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +00003564 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003565 // Between the time we initiated the Halt and the time we delivered it, the process could have
3566 // already finished its job. Check that here:
Jim Inghamf48169b2010-11-30 02:22:11 +00003567
Jim Ingham0f16e732011-02-08 05:20:59 +00003568 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3569 {
3570 if (log)
3571 log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
3572 "Exiting wait loop.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003573 return_value = eExecutionCompleted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003574 break;
3575 }
Jim Inghamf48169b2010-11-30 02:22:11 +00003576
Jim Ingham0f16e732011-02-08 05:20:59 +00003577 if (!try_all_threads)
3578 {
3579 if (log)
3580 log->Printf ("try_all_threads was false, we stopped so now we're quitting.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003581 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003582 break;
3583 }
3584
3585 if (first_timeout)
3586 {
3587 // Set all the other threads to run, and return to the top of the loop, which will continue;
3588 first_timeout = false;
3589 thread_plan_sp->SetStopOthers (false);
3590 if (log)
3591 log->Printf ("Process::RunThreadPlan(): About to resume.");
3592
3593 continue;
3594 }
3595 else
3596 {
3597 // Running all threads failed, so return Interrupted.
3598 if (log)
3599 log->Printf("Process::RunThreadPlan(): running all threads timed out.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003600 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003601 break;
3602 }
Jim Inghamf48169b2010-11-30 02:22:11 +00003603 }
Jim Ingham0f16e732011-02-08 05:20:59 +00003604 }
3605 else
3606 { if (log)
3607 log->Printf("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
3608 "I'm getting out of here passing Interrupted.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003609 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003610 break;
Jim Inghamf48169b2010-11-30 02:22:11 +00003611 }
3612 }
Jim Inghame22e88b2011-01-22 01:30:53 +00003613 else
3614 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003615 // This branch is to work around some problems with gdb-remote's Halt. It is a little racy, and can return
3616 // an error from halt, but if you wait a bit you'll get a stopped event anyway.
Jim Inghame22e88b2011-01-22 01:30:53 +00003617 if (log)
Jim Ingham0f16e732011-02-08 05:20:59 +00003618 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.",
3619 halt_error.AsCString());
3620 real_timeout = TimeValue::Now();
3621 real_timeout.OffsetWithMicroSeconds(500000);
3622 timeout_ptr = &real_timeout;
3623 got_event = listener.WaitForEvent(&real_timeout, event_sp);
3624 if (!got_event || event_sp.get() == NULL)
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00003625 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003626 // This is not going anywhere, bag out.
3627 if (log)
3628 log->Printf ("Process::RunThreadPlan(): halt failed: and waiting for the stopped event failed.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003629 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003630 break;
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00003631 }
Jim Ingham0f16e732011-02-08 05:20:59 +00003632 else
3633 {
3634 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3635 if (log)
3636 log->Printf ("Process::RunThreadPlan(): halt failed: but then I got a stopped event. Whatever...");
3637 if (stop_state == lldb::eStateStopped)
3638 {
3639 // Between the time we initiated the Halt and the time we delivered it, the process could have
3640 // already finished its job. Check that here:
3641
3642 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3643 {
3644 if (log)
3645 log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
3646 "Exiting wait loop.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003647 return_value = eExecutionCompleted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003648 break;
3649 }
3650
3651 if (first_timeout)
3652 {
3653 // Set all the other threads to run, and return to the top of the loop, which will continue;
3654 first_timeout = false;
3655 thread_plan_sp->SetStopOthers (false);
3656 if (log)
3657 log->Printf ("Process::RunThreadPlan(): About to resume.");
3658
3659 continue;
3660 }
3661 else
3662 {
3663 // Running all threads failed, so return Interrupted.
3664 if (log)
3665 log->Printf("Process::RunThreadPlan(): running all threads timed out.");
Greg Claytone0d378b2011-03-24 21:19:54 +00003666 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003667 break;
3668 }
3669 }
3670 else
3671 {
3672 log->Printf ("Process::RunThreadPlan(): halt failed, I waited and didn't get"
3673 " a stopped event, instead got %s.", StateAsCString(stop_state));
Greg Claytone0d378b2011-03-24 21:19:54 +00003674 return_value = eExecutionInterrupted;
Jim Ingham0f16e732011-02-08 05:20:59 +00003675 break;
3676 }
3677 }
Jim Inghame22e88b2011-01-22 01:30:53 +00003678 }
3679
Jim Inghamf48169b2010-11-30 02:22:11 +00003680 }
3681
Jim Ingham0f16e732011-02-08 05:20:59 +00003682 } // END WAIT LOOP
3683
3684 // Now do some processing on the results of the run:
3685 if (return_value == eExecutionInterrupted)
3686 {
Jim Inghamf48169b2010-11-30 02:22:11 +00003687 if (log)
Jim Ingham0f16e732011-02-08 05:20:59 +00003688 {
3689 StreamString s;
3690 if (event_sp)
3691 event_sp->Dump (&s);
3692 else
3693 {
3694 log->Printf ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
3695 }
3696
3697 StreamString ts;
3698
3699 const char *event_explanation;
3700
3701 do
3702 {
3703 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
3704
3705 if (!event_data)
3706 {
3707 event_explanation = "<no event data>";
3708 break;
3709 }
3710
3711 Process *process = event_data->GetProcessSP().get();
3712
3713 if (!process)
3714 {
3715 event_explanation = "<no process>";
3716 break;
3717 }
3718
3719 ThreadList &thread_list = process->GetThreadList();
3720
3721 uint32_t num_threads = thread_list.GetSize();
3722 uint32_t thread_index;
3723
3724 ts.Printf("<%u threads> ", num_threads);
3725
3726 for (thread_index = 0;
3727 thread_index < num_threads;
3728 ++thread_index)
3729 {
3730 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
3731
3732 if (!thread)
3733 {
3734 ts.Printf("<?> ");
3735 continue;
3736 }
3737
3738 ts.Printf("<0x%4.4x ", thread->GetID());
3739 RegisterContext *register_context = thread->GetRegisterContext().get();
3740
3741 if (register_context)
3742 ts.Printf("[ip 0x%llx] ", register_context->GetPC());
3743 else
3744 ts.Printf("[ip unknown] ");
3745
3746 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
3747 if (stop_info_sp)
3748 {
3749 const char *stop_desc = stop_info_sp->GetDescription();
3750 if (stop_desc)
3751 ts.PutCString (stop_desc);
3752 }
3753 ts.Printf(">");
3754 }
3755
3756 event_explanation = ts.GetData();
3757 } while (0);
3758
3759 if (log)
3760 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
3761
3762 if (discard_on_error && thread_plan_sp)
3763 {
3764 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3765 }
3766 }
3767 }
3768 else if (return_value == eExecutionSetupError)
3769 {
3770 if (log)
3771 log->Printf("Process::RunThreadPlan(): execution set up error.");
3772
3773 if (discard_on_error && thread_plan_sp)
3774 {
3775 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3776 }
3777 }
3778 else
3779 {
Jim Inghamf48169b2010-11-30 02:22:11 +00003780 if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get()))
3781 {
Greg Clayton414f5d32011-01-25 02:58:48 +00003782 if (log)
3783 log->Printf("Process::RunThreadPlan(): thread plan is done");
Greg Claytone0d378b2011-03-24 21:19:54 +00003784 return_value = eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +00003785 }
3786 else if (exe_ctx.thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
3787 {
Greg Clayton414f5d32011-01-25 02:58:48 +00003788 if (log)
3789 log->Printf("Process::RunThreadPlan(): thread plan was discarded");
Greg Claytone0d378b2011-03-24 21:19:54 +00003790 return_value = eExecutionDiscarded;
Jim Inghamf48169b2010-11-30 02:22:11 +00003791 }
3792 else
3793 {
3794 if (log)
Jim Ingham0f16e732011-02-08 05:20:59 +00003795 log->Printf("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Inghamf48169b2010-11-30 02:22:11 +00003796 if (discard_on_error && thread_plan_sp)
3797 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003798 if (log)
3799 log->Printf("Process::RunThreadPlan(): discarding thread plan 'cause discard_on_error is set.");
Jim Inghamf48169b2010-11-30 02:22:11 +00003800 exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
3801 }
Jim Inghamf48169b2010-11-30 02:22:11 +00003802 }
3803 }
Jim Ingham0f16e732011-02-08 05:20:59 +00003804
Jim Inghamf48169b2010-11-30 02:22:11 +00003805 // Thread we ran the function in may have gone away because we ran the target
3806 // Check that it's still there.
Greg Clayton92bb12c2011-05-19 18:17:41 +00003807 exe_ctx.thread = exe_ctx.process->GetThreadList().FindThreadByIndexID(thread_idx_id, true).get();
Jim Inghamf48169b2010-11-30 02:22:11 +00003808 if (exe_ctx.thread)
3809 exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex(0).get();
3810
3811 // Also restore the current process'es selected frame & thread, since this function calling may
3812 // be done behind the user's back.
3813
3814 if (selected_tid != LLDB_INVALID_THREAD_ID)
3815 {
3816 if (exe_ctx.process->GetThreadList().SetSelectedThreadByIndexID (selected_tid))
3817 {
3818 // We were able to restore the selected thread, now restore the frame:
3819 exe_ctx.process->GetThreadList().GetSelectedThread()->SetSelectedFrame(selected_frame_sp.get());
3820 }
3821 }
3822
3823 return return_value;
3824}
3825
3826const char *
3827Process::ExecutionResultAsCString (ExecutionResults result)
3828{
3829 const char *result_name;
3830
3831 switch (result)
3832 {
Greg Claytone0d378b2011-03-24 21:19:54 +00003833 case eExecutionCompleted:
Jim Inghamf48169b2010-11-30 02:22:11 +00003834 result_name = "eExecutionCompleted";
3835 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00003836 case eExecutionDiscarded:
Jim Inghamf48169b2010-11-30 02:22:11 +00003837 result_name = "eExecutionDiscarded";
3838 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00003839 case eExecutionInterrupted:
Jim Inghamf48169b2010-11-30 02:22:11 +00003840 result_name = "eExecutionInterrupted";
3841 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00003842 case eExecutionSetupError:
Jim Inghamf48169b2010-11-30 02:22:11 +00003843 result_name = "eExecutionSetupError";
3844 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00003845 case eExecutionTimedOut:
Jim Inghamf48169b2010-11-30 02:22:11 +00003846 result_name = "eExecutionTimedOut";
3847 break;
3848 }
3849 return result_name;
3850}
3851
Greg Clayton7260f622011-04-18 08:33:37 +00003852void
3853Process::GetStatus (Stream &strm)
3854{
3855 const StateType state = GetState();
3856 if (StateIsStoppedState(state))
3857 {
3858 if (state == eStateExited)
3859 {
3860 int exit_status = GetExitStatus();
3861 const char *exit_description = GetExitDescription();
3862 strm.Printf ("Process %d exited with status = %i (0x%8.8x) %s\n",
3863 GetID(),
3864 exit_status,
3865 exit_status,
3866 exit_description ? exit_description : "");
3867 }
3868 else
3869 {
3870 if (state == eStateConnected)
3871 strm.Printf ("Connected to remote target.\n");
3872 else
3873 strm.Printf ("Process %d %s\n", GetID(), StateAsCString (state));
3874 }
3875 }
3876 else
3877 {
3878 strm.Printf ("Process %d is running.\n", GetID());
3879 }
3880}
3881
3882size_t
3883Process::GetThreadStatus (Stream &strm,
3884 bool only_threads_with_stop_reason,
3885 uint32_t start_frame,
3886 uint32_t num_frames,
3887 uint32_t num_frames_with_source)
3888{
3889 size_t num_thread_infos_dumped = 0;
3890
3891 const size_t num_threads = GetThreadList().GetSize();
3892 for (uint32_t i = 0; i < num_threads; i++)
3893 {
3894 Thread *thread = GetThreadList().GetThreadAtIndex(i).get();
3895 if (thread)
3896 {
3897 if (only_threads_with_stop_reason)
3898 {
3899 if (thread->GetStopInfo().get() == NULL)
3900 continue;
3901 }
3902 thread->GetStatus (strm,
3903 start_frame,
3904 num_frames,
3905 num_frames_with_source);
3906 ++num_thread_infos_dumped;
3907 }
3908 }
3909 return num_thread_infos_dumped;
3910}
3911
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003912//--------------------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00003913// class Process::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003914//--------------------------------------------------------------
3915
Greg Clayton1b654882010-09-19 02:33:57 +00003916Process::SettingsController::SettingsController () :
Caroline Ticedaccaa92010-09-20 20:44:43 +00003917 UserSettingsController ("process", Target::GetSettingsController())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003918{
Greg Clayton85851dd2010-12-04 00:10:17 +00003919 m_default_settings.reset (new ProcessInstanceSettings (*this,
3920 false,
Caroline Tice91123da2010-09-08 17:48:55 +00003921 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003922}
3923
Greg Clayton1b654882010-09-19 02:33:57 +00003924Process::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003925{
3926}
3927
3928lldb::InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00003929Process::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003930{
Greg Claytondbe54502010-11-19 03:46:01 +00003931 ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*GetSettingsController(),
3932 false,
3933 instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003934 lldb::InstanceSettingsSP new_settings_sp (new_settings);
3935 return new_settings_sp;
3936}
3937
3938//--------------------------------------------------------------
3939// class ProcessInstanceSettings
3940//--------------------------------------------------------------
3941
Greg Clayton85851dd2010-12-04 00:10:17 +00003942ProcessInstanceSettings::ProcessInstanceSettings
3943(
3944 UserSettingsController &owner,
3945 bool live_instance,
3946 const char *name
3947) :
3948 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003949 m_run_args (),
3950 m_env_vars (),
3951 m_input_path (),
3952 m_output_path (),
3953 m_error_path (),
Caroline Ticef8da8632010-12-03 18:46:09 +00003954 m_disable_aslr (true),
Greg Clayton85851dd2010-12-04 00:10:17 +00003955 m_disable_stdio (false),
3956 m_inherit_host_env (true),
3957 m_got_host_env (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003958{
Caroline Ticef20e8232010-09-09 18:26:37 +00003959 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
3960 // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers.
3961 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
Caroline Tice9e41c152010-09-16 19:05:55 +00003962 // This is true for CreateInstanceName() too.
3963
3964 if (GetInstanceName () == InstanceSettings::InvalidName())
3965 {
3966 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
3967 m_owner.RegisterInstanceSettings (this);
3968 }
Caroline Ticef20e8232010-09-09 18:26:37 +00003969
3970 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003971 {
3972 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3973 CopyInstanceSettings (pending_settings,false);
Caroline Ticef20e8232010-09-09 18:26:37 +00003974 //m_owner.RemovePendingSettings (m_instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003975 }
3976}
3977
3978ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:01 +00003979 InstanceSettings (*Process::GetSettingsController(), CreateInstanceName().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003980 m_run_args (rhs.m_run_args),
3981 m_env_vars (rhs.m_env_vars),
3982 m_input_path (rhs.m_input_path),
3983 m_output_path (rhs.m_output_path),
3984 m_error_path (rhs.m_error_path),
Caroline Ticef8da8632010-12-03 18:46:09 +00003985 m_disable_aslr (rhs.m_disable_aslr),
3986 m_disable_stdio (rhs.m_disable_stdio)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00003987{
3988 if (m_instance_name != InstanceSettings::GetDefaultName())
3989 {
3990 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
3991 CopyInstanceSettings (pending_settings,false);
3992 m_owner.RemovePendingSettings (m_instance_name);
3993 }
3994}
3995
3996ProcessInstanceSettings::~ProcessInstanceSettings ()
3997{
3998}
3999
4000ProcessInstanceSettings&
4001ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs)
4002{
4003 if (this != &rhs)
4004 {
4005 m_run_args = rhs.m_run_args;
4006 m_env_vars = rhs.m_env_vars;
4007 m_input_path = rhs.m_input_path;
4008 m_output_path = rhs.m_output_path;
4009 m_error_path = rhs.m_error_path;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004010 m_disable_aslr = rhs.m_disable_aslr;
Caroline Ticef8da8632010-12-03 18:46:09 +00004011 m_disable_stdio = rhs.m_disable_stdio;
Greg Clayton85851dd2010-12-04 00:10:17 +00004012 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004013 }
4014
4015 return *this;
4016}
4017
4018
4019void
4020ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
4021 const char *index_value,
4022 const char *value,
4023 const ConstString &instance_name,
4024 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:54 +00004025 VarSetOperationType op,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004026 Error &err,
4027 bool pending)
4028{
4029 if (var_name == RunArgsVarName())
4030 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
4031 else if (var_name == EnvVarsVarName())
Greg Clayton85851dd2010-12-04 00:10:17 +00004032 {
Greg Clayton8b82f082011-04-12 05:54:46 +00004033 // This is nice for local debugging, but it is isn't correct for
4034 // remote debugging. We need to stop process.env-vars from being
4035 // populated with the host environment and add this as a launch option
4036 // and get the correct environment from the Target's platform.
4037 // GetHostEnvironmentIfNeeded ();
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004038 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
Greg Clayton85851dd2010-12-04 00:10:17 +00004039 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004040 else if (var_name == InputPathVarName())
4041 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
4042 else if (var_name == OutputPathVarName())
4043 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
4044 else if (var_name == ErrorPathVarName())
4045 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004046 else if (var_name == DisableASLRVarName())
Greg Clayton385aa282011-04-22 03:55:06 +00004047 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
Caroline Ticef8da8632010-12-03 18:46:09 +00004048 else if (var_name == DisableSTDIOVarName ())
Greg Clayton385aa282011-04-22 03:55:06 +00004049 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004050}
4051
4052void
4053ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
4054 bool pending)
4055{
4056 if (new_settings.get() == NULL)
4057 return;
4058
4059 ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get();
4060
4061 m_run_args = new_process_settings->m_run_args;
4062 m_env_vars = new_process_settings->m_env_vars;
4063 m_input_path = new_process_settings->m_input_path;
4064 m_output_path = new_process_settings->m_output_path;
4065 m_error_path = new_process_settings->m_error_path;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004066 m_disable_aslr = new_process_settings->m_disable_aslr;
Caroline Ticef8da8632010-12-03 18:46:09 +00004067 m_disable_stdio = new_process_settings->m_disable_stdio;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004068}
4069
Caroline Tice12cecd72010-09-20 21:37:42 +00004070bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004071ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
4072 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00004073 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00004074 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004075{
4076 if (var_name == RunArgsVarName())
4077 {
4078 if (m_run_args.GetArgumentCount() > 0)
Greg Claytona52c1552010-09-14 03:47:41 +00004079 {
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004080 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
4081 value.AppendString (m_run_args.GetArgumentAtIndex (i));
Greg Claytona52c1552010-09-14 03:47:41 +00004082 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004083 }
4084 else if (var_name == EnvVarsVarName())
4085 {
Greg Clayton85851dd2010-12-04 00:10:17 +00004086 GetHostEnvironmentIfNeeded ();
4087
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004088 if (m_env_vars.size() > 0)
4089 {
4090 std::map<std::string, std::string>::iterator pos;
4091 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
4092 {
4093 StreamString value_str;
4094 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
4095 value.AppendString (value_str.GetData());
4096 }
4097 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004098 }
4099 else if (var_name == InputPathVarName())
4100 {
4101 value.AppendString (m_input_path.c_str());
4102 }
4103 else if (var_name == OutputPathVarName())
4104 {
4105 value.AppendString (m_output_path.c_str());
4106 }
4107 else if (var_name == ErrorPathVarName())
4108 {
4109 value.AppendString (m_error_path.c_str());
4110 }
Greg Clayton5c5f1a12010-12-04 00:12:24 +00004111 else if (var_name == InheritHostEnvVarName())
4112 {
4113 if (m_inherit_host_env)
4114 value.AppendString ("true");
4115 else
4116 value.AppendString ("false");
4117 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004118 else if (var_name == DisableASLRVarName())
4119 {
4120 if (m_disable_aslr)
4121 value.AppendString ("true");
4122 else
4123 value.AppendString ("false");
4124 }
Caroline Ticef8da8632010-12-03 18:46:09 +00004125 else if (var_name == DisableSTDIOVarName())
4126 {
4127 if (m_disable_stdio)
4128 value.AppendString ("true");
4129 else
4130 value.AppendString ("false");
4131 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004132 else
Caroline Tice12cecd72010-09-20 21:37:42 +00004133 {
4134 if (err)
4135 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
4136 return false;
4137 }
4138 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004139}
4140
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004141const ConstString
4142ProcessInstanceSettings::CreateInstanceName ()
4143{
4144 static int instance_count = 1;
4145 StreamString sstr;
4146
4147 sstr.Printf ("process_%d", instance_count);
4148 ++instance_count;
4149
4150 const ConstString ret_val (sstr.GetData());
4151 return ret_val;
4152}
4153
4154const ConstString &
4155ProcessInstanceSettings::RunArgsVarName ()
4156{
4157 static ConstString run_args_var_name ("run-args");
4158
4159 return run_args_var_name;
4160}
4161
4162const ConstString &
4163ProcessInstanceSettings::EnvVarsVarName ()
4164{
4165 static ConstString env_vars_var_name ("env-vars");
4166
4167 return env_vars_var_name;
4168}
4169
4170const ConstString &
Greg Clayton85851dd2010-12-04 00:10:17 +00004171ProcessInstanceSettings::InheritHostEnvVarName ()
4172{
4173 static ConstString g_name ("inherit-env");
4174
4175 return g_name;
4176}
4177
4178const ConstString &
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004179ProcessInstanceSettings::InputPathVarName ()
4180{
4181 static ConstString input_path_var_name ("input-path");
4182
4183 return input_path_var_name;
4184}
4185
4186const ConstString &
4187ProcessInstanceSettings::OutputPathVarName ()
4188{
Caroline Tice49e27372010-09-07 18:35:40 +00004189 static ConstString output_path_var_name ("output-path");
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004190
4191 return output_path_var_name;
4192}
4193
4194const ConstString &
4195ProcessInstanceSettings::ErrorPathVarName ()
4196{
Caroline Tice49e27372010-09-07 18:35:40 +00004197 static ConstString error_path_var_name ("error-path");
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004198
4199 return error_path_var_name;
4200}
4201
4202const ConstString &
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004203ProcessInstanceSettings::DisableASLRVarName ()
4204{
4205 static ConstString disable_aslr_var_name ("disable-aslr");
4206
4207 return disable_aslr_var_name;
4208}
4209
Caroline Ticef8da8632010-12-03 18:46:09 +00004210const ConstString &
4211ProcessInstanceSettings::DisableSTDIOVarName ()
4212{
4213 static ConstString disable_stdio_var_name ("disable-stdio");
4214
4215 return disable_stdio_var_name;
4216}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004217
4218//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00004219// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004220//--------------------------------------------------
4221
4222SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00004223Process::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004224{
4225 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
4226 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
4227};
4228
4229
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004230SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00004231Process::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004232{
Greg Clayton85851dd2010-12-04 00:10:17 +00004233 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
4234 { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
4235 { "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." },
4236 { "inherit-env", eSetVarTypeBoolean, "true", NULL, false, false, "Inherit the environment from the process that is running LLDB." },
Greg Claytonbd82a5d2011-01-23 05:56:20 +00004237 { "input-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for reading its input." },
4238 { "output-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writing its output." },
4239 { "error-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writings its error messages." },
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004240 { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
Greg Clayton85851dd2010-12-04 00:10:17 +00004241 { "disable-aslr", eSetVarTypeBoolean, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
4242 { "disable-stdio", eSetVarTypeBoolean, "false", NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" },
4243 { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004244};
4245
4246
Jim Ingham5aee1622010-08-09 23:31:02 +00004247