blob: 07e1cdf52410b261cb18b900e5172e56c6645e6b [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Process.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Process.h"
11
12#include "lldb/lldb-private-log.h"
13
14#include "lldb/Breakpoint/StoppointCallbackContext.h"
15#include "lldb/Breakpoint/BreakpointLocation.h"
16#include "lldb/Core/Event.h"
Caroline Tice861efb32010-11-16 05:07:41 +000017#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/Debugger.h"
Caroline Tice861efb32010-11-16 05:07:41 +000019#include "lldb/Core/InputReader.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Log.h"
21#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/State.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000023#include "lldb/Expression/ClangUserExpression.h"
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000024#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Host/Host.h"
26#include "lldb/Target/ABI.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000027#include "lldb/Target/DynamicLoader.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000028#include "lldb/Target/OperatingSystem.h"
Jim Ingham642036f2010-09-23 02:01:19 +000029#include "lldb/Target/LanguageRuntime.h"
30#include "lldb/Target/CPPLanguageRuntime.h"
31#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytone4b9c1f2011-03-08 22:40:15 +000032#include "lldb/Target/Platform.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000034#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035#include "lldb/Target/Target.h"
36#include "lldb/Target/TargetList.h"
37#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadPlan.h"
39
40using namespace lldb;
41using namespace lldb_private;
42
Greg Clayton24bc5d92011-03-30 18:16:51 +000043void
Greg Claytonb72d0f02011-04-12 05:54:46 +000044ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const
Greg Clayton24bc5d92011-03-30 18:16:51 +000045{
46 const char *cstr;
Greg Claytonff39f742011-04-01 00:29:43 +000047 if (m_pid != LLDB_INVALID_PROCESS_ID)
Greg Claytond9919d32011-12-01 23:28:38 +000048 s.Printf (" pid = %llu\n", m_pid);
Greg Claytonff39f742011-04-01 00:29:43 +000049
50 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
Greg Claytond9919d32011-12-01 23:28:38 +000051 s.Printf (" parent = %llu\n", m_parent_pid);
Greg Claytonff39f742011-04-01 00:29:43 +000052
53 if (m_executable)
54 {
55 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
56 s.PutCString (" file = ");
57 m_executable.Dump(&s);
58 s.EOL();
59 }
Greg Claytonb72d0f02011-04-12 05:54:46 +000060 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Claytonff39f742011-04-01 00:29:43 +000061 if (argc > 0)
62 {
63 for (uint32_t i=0; i<argc; i++)
64 {
Greg Claytonb72d0f02011-04-12 05:54:46 +000065 const char *arg = m_arguments.GetArgumentAtIndex(i);
Greg Claytonff39f742011-04-01 00:29:43 +000066 if (i < 10)
Greg Claytonb72d0f02011-04-12 05:54:46 +000067 s.Printf (" arg[%u] = %s\n", i, arg);
Greg Claytonff39f742011-04-01 00:29:43 +000068 else
Greg Claytonb72d0f02011-04-12 05:54:46 +000069 s.Printf ("arg[%u] = %s\n", i, arg);
Greg Claytonff39f742011-04-01 00:29:43 +000070 }
71 }
Greg Claytonb72d0f02011-04-12 05:54:46 +000072
73 const uint32_t envc = m_environment.GetArgumentCount();
74 if (envc > 0)
75 {
76 for (uint32_t i=0; i<envc; i++)
77 {
78 const char *env = m_environment.GetArgumentAtIndex(i);
79 if (i < 10)
80 s.Printf (" env[%u] = %s\n", i, env);
81 else
82 s.Printf ("env[%u] = %s\n", i, env);
83 }
84 }
85
Greg Claytonff39f742011-04-01 00:29:43 +000086 if (m_arch.IsValid())
87 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
88
Greg Claytonb72d0f02011-04-12 05:54:46 +000089 if (m_uid != UINT32_MAX)
Greg Clayton24bc5d92011-03-30 18:16:51 +000090 {
Greg Claytonb72d0f02011-04-12 05:54:46 +000091 cstr = platform->GetUserName (m_uid);
92 s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000093 }
Greg Claytonb72d0f02011-04-12 05:54:46 +000094 if (m_gid != UINT32_MAX)
Greg Clayton24bc5d92011-03-30 18:16:51 +000095 {
Greg Claytonb72d0f02011-04-12 05:54:46 +000096 cstr = platform->GetGroupName (m_gid);
97 s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +000098 }
Greg Claytonb72d0f02011-04-12 05:54:46 +000099 if (m_euid != UINT32_MAX)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000100 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000101 cstr = platform->GetUserName (m_euid);
102 s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +0000103 }
Greg Claytonb72d0f02011-04-12 05:54:46 +0000104 if (m_egid != UINT32_MAX)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000105 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000106 cstr = platform->GetGroupName (m_egid);
107 s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : "");
Greg Clayton24bc5d92011-03-30 18:16:51 +0000108 }
109}
110
111void
Greg Claytonb72d0f02011-04-12 05:54:46 +0000112ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000113{
Greg Claytonb72d0f02011-04-12 05:54:46 +0000114 const char *label;
115 if (show_args || verbose)
116 label = "ARGUMENTS";
117 else
118 label = "NAME";
119
Greg Claytonff39f742011-04-01 00:29:43 +0000120 if (verbose)
121 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000122 s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label);
Greg Claytonff39f742011-04-01 00:29:43 +0000123 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
124 }
125 else
126 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000127 s.Printf ("PID PARENT USER ARCH %s\n", label);
Greg Claytonff39f742011-04-01 00:29:43 +0000128 s.PutCString ("====== ====== ========== ======= ============================\n");
129 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000130}
131
132void
Greg Claytonb72d0f02011-04-12 05:54:46 +0000133ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const
Greg Clayton24bc5d92011-03-30 18:16:51 +0000134{
135 if (m_pid != LLDB_INVALID_PROCESS_ID)
136 {
137 const char *cstr;
Greg Claytond9919d32011-12-01 23:28:38 +0000138 s.Printf ("%-6llu %-6llu ", m_pid, m_parent_pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000139
Greg Clayton24bc5d92011-03-30 18:16:51 +0000140
Greg Claytonff39f742011-04-01 00:29:43 +0000141 if (verbose)
142 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000143 cstr = platform->GetUserName (m_uid);
Greg Claytonff39f742011-04-01 00:29:43 +0000144 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
145 s.Printf ("%-10s ", cstr);
146 else
Greg Claytonb72d0f02011-04-12 05:54:46 +0000147 s.Printf ("%-10u ", m_uid);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000148
Greg Claytonb72d0f02011-04-12 05:54:46 +0000149 cstr = platform->GetGroupName (m_gid);
Greg Claytonff39f742011-04-01 00:29:43 +0000150 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
151 s.Printf ("%-10s ", cstr);
152 else
Greg Claytonb72d0f02011-04-12 05:54:46 +0000153 s.Printf ("%-10u ", m_gid);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000154
Greg Claytonb72d0f02011-04-12 05:54:46 +0000155 cstr = platform->GetUserName (m_euid);
Greg Claytonff39f742011-04-01 00:29:43 +0000156 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
157 s.Printf ("%-10s ", cstr);
158 else
Greg Claytonb72d0f02011-04-12 05:54:46 +0000159 s.Printf ("%-10u ", m_euid);
Greg Claytonff39f742011-04-01 00:29:43 +0000160
Greg Claytonb72d0f02011-04-12 05:54:46 +0000161 cstr = platform->GetGroupName (m_egid);
Greg Claytonff39f742011-04-01 00:29:43 +0000162 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
163 s.Printf ("%-10s ", cstr);
164 else
Greg Claytonb72d0f02011-04-12 05:54:46 +0000165 s.Printf ("%-10u ", m_egid);
Greg Claytonff39f742011-04-01 00:29:43 +0000166 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
167 }
168 else
169 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000170 s.Printf ("%-10s %-7d %s ",
Greg Claytonb72d0f02011-04-12 05:54:46 +0000171 platform->GetUserName (m_euid),
Greg Claytonff39f742011-04-01 00:29:43 +0000172 (int)m_arch.GetTriple().getArchName().size(),
173 m_arch.GetTriple().getArchName().data());
174 }
175
Greg Claytonb72d0f02011-04-12 05:54:46 +0000176 if (verbose || show_args)
Greg Claytonff39f742011-04-01 00:29:43 +0000177 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000178 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Claytonff39f742011-04-01 00:29:43 +0000179 if (argc > 0)
180 {
181 for (uint32_t i=0; i<argc; i++)
182 {
183 if (i > 0)
184 s.PutChar (' ');
Greg Claytonb72d0f02011-04-12 05:54:46 +0000185 s.PutCString (m_arguments.GetArgumentAtIndex(i));
Greg Claytonff39f742011-04-01 00:29:43 +0000186 }
187 }
188 }
189 else
190 {
191 s.PutCString (GetName());
192 }
193
194 s.EOL();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000195 }
196}
197
Greg Claytonb72d0f02011-04-12 05:54:46 +0000198
199void
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000200ProcessInfo::SetArguments (char const **argv,
201 bool first_arg_is_executable,
202 bool first_arg_is_executable_and_argument)
203{
204 m_arguments.SetArguments (argv);
205
206 // Is the first argument the executable?
207 if (first_arg_is_executable)
208 {
209 const char *first_arg = m_arguments.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}
225void
226ProcessInfo::SetArguments (const Args& args,
227 bool first_arg_is_executable,
228 bool first_arg_is_executable_and_argument)
Greg Claytonb72d0f02011-04-12 05:54:46 +0000229{
230 // Copy all arguments
231 m_arguments = args;
232
233 // Is the first argument the executable?
234 if (first_arg_is_executable)
235 {
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000236 const char *first_arg = m_arguments.GetArgumentAtIndex (0);
Greg Claytonb72d0f02011-04-12 05:54:46 +0000237 if (first_arg)
238 {
239 // Yes the first argument is an executable, set it as the executable
240 // in the launch options. Don't resolve the file path as the path
241 // could be a remote platform path
242 const bool resolve = false;
243 m_executable.SetFile(first_arg, resolve);
244
245 // If argument zero is an executable and shouldn't be included
246 // in the arguments, remove it from the front of the arguments
247 if (first_arg_is_executable_and_argument == false)
248 m_arguments.DeleteArgumentAtIndex (0);
249 }
250 }
251}
252
Greg Claytonabb33022011-11-08 02:43:13 +0000253void
Greg Clayton464c6162011-11-17 22:14:31 +0000254ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty)
Greg Claytonabb33022011-11-08 02:43:13 +0000255{
256 // If notthing was specified, then check the process for any default
257 // settings that were set with "settings set"
258 if (m_file_actions.empty())
259 {
Greg Claytonabb33022011-11-08 02:43:13 +0000260 if (m_flags.Test(eLaunchFlagDisableSTDIO))
261 {
262 AppendSuppressFileAction (STDERR_FILENO, true , true );
263 AppendSuppressFileAction (STDIN_FILENO , true , false);
264 AppendSuppressFileAction (STDOUT_FILENO, false, true );
265 }
266 else
267 {
268 // Check for any values that might have gotten set with any of:
269 // (lldb) settings set target.input-path
270 // (lldb) settings set target.output-path
271 // (lldb) settings set target.error-path
Greg Clayton464c6162011-11-17 22:14:31 +0000272 const char *in_path = NULL;
273 const char *out_path = NULL;
274 const char *err_path = NULL;
Greg Claytonabb33022011-11-08 02:43:13 +0000275 if (target)
276 {
Greg Clayton464c6162011-11-17 22:14:31 +0000277 in_path = target->GetStandardErrorPath();
278 out_path = target->GetStandardInputPath();
279 err_path = target->GetStandardOutputPath();
280 }
281
282 if (default_to_use_pty && (!in_path && !out_path && !err_path))
283 {
284 if (m_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, NULL, 0))
Greg Claytonabb33022011-11-08 02:43:13 +0000285 {
Greg Clayton464c6162011-11-17 22:14:31 +0000286 in_path = out_path = err_path = m_pty.GetSlaveName (NULL, 0);
Greg Claytonabb33022011-11-08 02:43:13 +0000287 }
288 }
289
Greg Clayton464c6162011-11-17 22:14:31 +0000290 if (in_path)
291 AppendOpenFileAction(STDERR_FILENO, in_path, true, true);
292
293 if (out_path)
294 AppendOpenFileAction(STDIN_FILENO, out_path, true, false);
295
296 if (err_path)
297 AppendOpenFileAction(STDOUT_FILENO, err_path, false, true);
Greg Claytonabb33022011-11-08 02:43:13 +0000298 }
299 }
300}
301
Greg Clayton527154d2011-11-15 03:53:30 +0000302
303bool
304ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error, bool localhost)
305{
306 error.Clear();
307
308 if (GetFlags().Test (eLaunchFlagLaunchInShell))
309 {
310 const char *shell_executable = GetShell();
311 if (shell_executable)
312 {
313 char shell_resolved_path[PATH_MAX];
314
315 if (localhost)
316 {
317 FileSpec shell_filespec (shell_executable, true);
318
319 if (!shell_filespec.Exists())
320 {
321 // Resolve the path in case we just got "bash", "sh" or "tcsh"
322 if (!shell_filespec.ResolveExecutableLocation ())
323 {
324 error.SetErrorStringWithFormat("invalid shell path '%s'", shell_executable);
325 return false;
326 }
327 }
328 shell_filespec.GetPath (shell_resolved_path, sizeof(shell_resolved_path));
329 shell_executable = shell_resolved_path;
330 }
331
332 Args shell_arguments;
333 std::string safe_arg;
334 shell_arguments.AppendArgument (shell_executable);
335 StreamString shell_command;
336 shell_arguments.AppendArgument ("-c");
337 shell_command.PutCString ("exec");
338 if (GetArchitecture().IsValid())
339 {
340 shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
341 // Set the resume count to 2:
342 // 1 - stop in shell
343 // 2 - stop in /usr/bin/arch
344 // 3 - then we will stop in our program
345 SetResumeCount(2);
346 }
347 else
348 {
349 // Set the resume count to 1:
350 // 1 - stop in shell
351 // 2 - then we will stop in our program
352 SetResumeCount(1);
353 }
354
355 const char **argv = GetArguments().GetConstArgumentVector ();
356 if (argv)
357 {
358 for (size_t i=0; argv[i] != NULL; ++i)
359 {
360 const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
361 shell_command.Printf(" %s", arg);
362 }
363 }
364 shell_arguments.AppendArgument (shell_command.GetString().c_str());
365
366 m_executable.SetFile(shell_executable, false);
367 m_arguments = shell_arguments;
368 return true;
369 }
370 else
371 {
372 error.SetErrorString ("invalid shell path");
373 }
374 }
375 else
376 {
377 error.SetErrorString ("not launching in shell");
378 }
379 return false;
380}
381
382
Greg Clayton24bc5d92011-03-30 18:16:51 +0000383bool
Greg Claytonb72d0f02011-04-12 05:54:46 +0000384ProcessLaunchInfo::FileAction::Open (int fd, const char *path, bool read, bool write)
385{
386 if ((read || write) && fd >= 0 && path && path[0])
387 {
388 m_action = eFileActionOpen;
389 m_fd = fd;
390 if (read && write)
Greg Clayton527154d2011-11-15 03:53:30 +0000391 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000392 else if (read)
Greg Clayton527154d2011-11-15 03:53:30 +0000393 m_arg = O_NOCTTY | O_RDONLY;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000394 else
Greg Clayton527154d2011-11-15 03:53:30 +0000395 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000396 m_path.assign (path);
397 return true;
398 }
399 else
400 {
401 Clear();
402 }
403 return false;
404}
405
406bool
407ProcessLaunchInfo::FileAction::Close (int fd)
408{
409 Clear();
410 if (fd >= 0)
411 {
412 m_action = eFileActionClose;
413 m_fd = fd;
414 }
415 return m_fd >= 0;
416}
417
418
419bool
420ProcessLaunchInfo::FileAction::Duplicate (int fd, int dup_fd)
421{
422 Clear();
423 if (fd >= 0 && dup_fd >= 0)
424 {
425 m_action = eFileActionDuplicate;
426 m_fd = fd;
427 m_arg = dup_fd;
428 }
429 return m_fd >= 0;
430}
431
432
433
434bool
435ProcessLaunchInfo::FileAction::AddPosixSpawnFileAction (posix_spawn_file_actions_t *file_actions,
436 const FileAction *info,
437 Log *log,
438 Error& error)
439{
440 if (info == NULL)
441 return false;
442
443 switch (info->m_action)
444 {
445 case eFileActionNone:
446 error.Clear();
447 break;
448
449 case eFileActionClose:
450 if (info->m_fd == -1)
451 error.SetErrorString ("invalid fd for posix_spawn_file_actions_addclose(...)");
452 else
453 {
454 error.SetError (::posix_spawn_file_actions_addclose (file_actions, info->m_fd),
455 eErrorTypePOSIX);
456 if (log && (error.Fail() || log))
457 error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)",
458 file_actions, info->m_fd);
459 }
460 break;
461
462 case eFileActionDuplicate:
463 if (info->m_fd == -1)
464 error.SetErrorString ("invalid fd for posix_spawn_file_actions_adddup2(...)");
465 else if (info->m_arg == -1)
466 error.SetErrorString ("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
467 else
468 {
469 error.SetError (::posix_spawn_file_actions_adddup2 (file_actions, info->m_fd, info->m_arg),
470 eErrorTypePOSIX);
471 if (log && (error.Fail() || log))
472 error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
473 file_actions, info->m_fd, info->m_arg);
474 }
475 break;
476
477 case eFileActionOpen:
478 if (info->m_fd == -1)
479 error.SetErrorString ("invalid fd in posix_spawn_file_actions_addopen(...)");
480 else
481 {
482 int oflag = info->m_arg;
Greg Clayton527154d2011-11-15 03:53:30 +0000483
Greg Claytonb72d0f02011-04-12 05:54:46 +0000484 mode_t mode = 0;
485
Greg Clayton527154d2011-11-15 03:53:30 +0000486 if (oflag & O_CREAT)
487 mode = 0640;
488
Greg Claytonb72d0f02011-04-12 05:54:46 +0000489 error.SetError (::posix_spawn_file_actions_addopen (file_actions,
490 info->m_fd,
491 info->m_path.c_str(),
492 oflag,
493 mode),
494 eErrorTypePOSIX);
495 if (error.Fail() || log)
496 error.PutToLog(log,
497 "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)",
498 file_actions, info->m_fd, info->m_path.c_str(), oflag, mode);
499 }
500 break;
501
502 default:
503 error.SetErrorStringWithFormat ("invalid file action: %i", info->m_action);
504 break;
505 }
506 return error.Success();
507}
508
509Error
Greg Clayton143fcc32011-04-13 00:18:08 +0000510ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Claytonb72d0f02011-04-12 05:54:46 +0000511{
512 Error error;
513 char short_option = (char) m_getopt_table[option_idx].val;
514
515 switch (short_option)
516 {
517 case 's': // Stop at program entry point
518 launch_info.GetFlags().Set (eLaunchFlagStopAtEntry);
519 break;
520
521 case 'e': // STDERR for read + write
522 {
523 ProcessLaunchInfo::FileAction action;
524 if (action.Open(STDERR_FILENO, option_arg, true, true))
525 launch_info.AppendFileAction (action);
526 }
527 break;
528
529 case 'i': // STDIN for read only
530 {
531 ProcessLaunchInfo::FileAction action;
532 if (action.Open(STDIN_FILENO, option_arg, true, false))
533 launch_info.AppendFileAction (action);
534 }
535 break;
536
537 case 'o': // Open STDOUT for write only
538 {
539 ProcessLaunchInfo::FileAction action;
540 if (action.Open(STDOUT_FILENO, option_arg, false, true))
541 launch_info.AppendFileAction (action);
542 }
543 break;
544
545 case 'p': // Process plug-in name
546 launch_info.SetProcessPluginName (option_arg);
547 break;
548
549 case 'n': // Disable STDIO
550 {
551 ProcessLaunchInfo::FileAction action;
552 if (action.Open(STDERR_FILENO, "/dev/null", true, true))
553 launch_info.AppendFileAction (action);
554 if (action.Open(STDOUT_FILENO, "/dev/null", false, true))
555 launch_info.AppendFileAction (action);
556 if (action.Open(STDIN_FILENO, "/dev/null", true, false))
557 launch_info.AppendFileAction (action);
558 }
559 break;
560
561 case 'w':
562 launch_info.SetWorkingDirectory (option_arg);
563 break;
564
565 case 't': // Open process in new terminal window
566 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
567 break;
568
569 case 'a':
570 launch_info.GetArchitecture().SetTriple (option_arg,
571 m_interpreter.GetPlatform(true).get());
572 break;
573
574 case 'A':
575 launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
576 break;
577
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000578 case 'c':
Greg Clayton527154d2011-11-15 03:53:30 +0000579 if (option_arg && option_arg[0])
580 launch_info.SetShell (option_arg);
581 else
582 launch_info.SetShell ("/bin/bash");
Greg Clayton36bc5ea2011-11-03 21:22:33 +0000583 break;
584
Greg Claytonb72d0f02011-04-12 05:54:46 +0000585 case 'v':
586 launch_info.GetEnvironmentEntries().AppendArgument(option_arg);
587 break;
588
589 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000590 error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option);
Greg Claytonb72d0f02011-04-12 05:54:46 +0000591 break;
592
593 }
594 return error;
595}
596
597OptionDefinition
598ProcessLaunchCommandOptions::g_option_table[] =
599{
600{ 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."},
601{ LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', no_argument, NULL, 0, eArgTypeNone, "Disable address space layout randomization when launching a process."},
602{ LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
603{ LLDB_OPT_SET_ALL, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."},
604{ LLDB_OPT_SET_ALL, false, "arch", 'a', required_argument, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."},
605{ 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."},
Greg Clayton527154d2011-11-15 03:53:30 +0000606{ LLDB_OPT_SET_ALL, false, "shell", 'c', optional_argument, NULL, 0, eArgTypePath, "Run the process in a shell (not supported on all platforms)."},
Greg Claytonb72d0f02011-04-12 05:54:46 +0000607
608{ LLDB_OPT_SET_1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."},
609{ LLDB_OPT_SET_1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."},
610{ LLDB_OPT_SET_1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."},
611
612{ LLDB_OPT_SET_2 , false, "tty", 't', no_argument, NULL, 0, eArgTypeNone, "Start the process in a terminal (not supported on all platforms)."},
613
614{ 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."},
615
616{ 0 , false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
617};
618
619
620
621bool
622ProcessInstanceInfoMatch::NameMatches (const char *process_name) const
Greg Clayton24bc5d92011-03-30 18:16:51 +0000623{
624 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
625 return true;
626 const char *match_name = m_match_info.GetName();
627 if (!match_name)
628 return true;
629
630 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
631}
632
633bool
Greg Claytonb72d0f02011-04-12 05:54:46 +0000634ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const
Greg Clayton24bc5d92011-03-30 18:16:51 +0000635{
636 if (!NameMatches (proc_info.GetName()))
637 return false;
638
639 if (m_match_info.ProcessIDIsValid() &&
640 m_match_info.GetProcessID() != proc_info.GetProcessID())
641 return false;
642
643 if (m_match_info.ParentProcessIDIsValid() &&
644 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
645 return false;
646
Greg Claytonb72d0f02011-04-12 05:54:46 +0000647 if (m_match_info.UserIDIsValid () &&
648 m_match_info.GetUserID() != proc_info.GetUserID())
Greg Clayton24bc5d92011-03-30 18:16:51 +0000649 return false;
650
Greg Claytonb72d0f02011-04-12 05:54:46 +0000651 if (m_match_info.GroupIDIsValid () &&
652 m_match_info.GetGroupID() != proc_info.GetGroupID())
Greg Clayton24bc5d92011-03-30 18:16:51 +0000653 return false;
654
655 if (m_match_info.EffectiveUserIDIsValid () &&
656 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
657 return false;
658
659 if (m_match_info.EffectiveGroupIDIsValid () &&
660 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
661 return false;
662
663 if (m_match_info.GetArchitecture().IsValid() &&
664 m_match_info.GetArchitecture() != proc_info.GetArchitecture())
665 return false;
666 return true;
667}
668
669bool
Greg Claytonb72d0f02011-04-12 05:54:46 +0000670ProcessInstanceInfoMatch::MatchAllProcesses () const
Greg Clayton24bc5d92011-03-30 18:16:51 +0000671{
672 if (m_name_match_type != eNameMatchIgnore)
673 return false;
674
675 if (m_match_info.ProcessIDIsValid())
676 return false;
677
678 if (m_match_info.ParentProcessIDIsValid())
679 return false;
680
Greg Claytonb72d0f02011-04-12 05:54:46 +0000681 if (m_match_info.UserIDIsValid ())
Greg Clayton24bc5d92011-03-30 18:16:51 +0000682 return false;
683
Greg Claytonb72d0f02011-04-12 05:54:46 +0000684 if (m_match_info.GroupIDIsValid ())
Greg Clayton24bc5d92011-03-30 18:16:51 +0000685 return false;
686
687 if (m_match_info.EffectiveUserIDIsValid ())
688 return false;
689
690 if (m_match_info.EffectiveGroupIDIsValid ())
691 return false;
692
693 if (m_match_info.GetArchitecture().IsValid())
694 return false;
695
696 if (m_match_all_users)
697 return false;
698
699 return true;
700
701}
702
703void
Greg Claytonb72d0f02011-04-12 05:54:46 +0000704ProcessInstanceInfoMatch::Clear()
Greg Clayton24bc5d92011-03-30 18:16:51 +0000705{
706 m_match_info.Clear();
707 m_name_match_type = eNameMatchIgnore;
708 m_match_all_users = false;
709}
Greg Claytonfd119992011-01-07 06:08:19 +0000710
Chris Lattner24943d22010-06-08 16:52:24 +0000711Process*
712Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener)
713{
714 ProcessCreateInstance create_callback = NULL;
715 if (plugin_name)
716 {
717 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name);
718 if (create_callback)
719 {
720 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
Greg Clayton8d2ea282011-07-17 20:36:25 +0000721 if (debugger_ap->CanDebug(target, true))
Chris Lattner24943d22010-06-08 16:52:24 +0000722 return debugger_ap.release();
723 }
724 }
725 else
726 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000727 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000728 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000729 std::auto_ptr<Process> debugger_ap(create_callback(target, listener));
Greg Clayton8d2ea282011-07-17 20:36:25 +0000730 if (debugger_ap->CanDebug(target, false))
Greg Clayton54e7afa2010-07-09 20:39:50 +0000731 return debugger_ap.release();
Chris Lattner24943d22010-06-08 16:52:24 +0000732 }
733 }
734 return NULL;
735}
736
737
738//----------------------------------------------------------------------
739// Process constructor
740//----------------------------------------------------------------------
741Process::Process(Target &target, Listener &listener) :
742 UserID (LLDB_INVALID_PROCESS_ID),
Greg Clayton49ce6822010-10-31 03:01:06 +0000743 Broadcaster ("lldb.process"),
Greg Clayton334d33a2012-01-30 07:41:31 +0000744 ProcessInstanceSettings (GetSettingsController()),
Chris Lattner24943d22010-06-08 16:52:24 +0000745 m_target (target),
Chris Lattner24943d22010-06-08 16:52:24 +0000746 m_public_state (eStateUnloaded),
747 m_private_state (eStateUnloaded),
748 m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"),
749 m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"),
750 m_private_state_listener ("lldb.process.internal_state_listener"),
751 m_private_state_control_wait(),
752 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
Jim Ingham21f37ad2011-08-09 02:12:22 +0000753 m_mod_id (),
Chris Lattner24943d22010-06-08 16:52:24 +0000754 m_thread_index_id (0),
755 m_exit_status (-1),
756 m_exit_string (),
757 m_thread_list (this),
758 m_notifications (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000759 m_image_tokens (),
760 m_listener (listener),
761 m_breakpoint_site_list (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000762 m_dynamic_checkers_ap (),
Caroline Tice861efb32010-11-16 05:07:41 +0000763 m_unix_signals (),
Greg Clayton20d338f2010-11-18 05:57:03 +0000764 m_abi_sp (),
Caroline Tice861efb32010-11-16 05:07:41 +0000765 m_process_input_reader (),
Greg Claytona875b642011-01-09 21:07:35 +0000766 m_stdio_communication ("process.stdio"),
Greg Clayton20d338f2010-11-18 05:57:03 +0000767 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonfd119992011-01-07 06:08:19 +0000768 m_stdout_data (),
Greg Claytonbd06ff42011-11-13 04:45:22 +0000769 m_stderr_data (),
Greg Clayton613b8732011-05-17 03:37:42 +0000770 m_memory_cache (*this),
771 m_allocated_memory_cache (*this),
Greg Claytonffa43a62011-11-17 04:46:02 +0000772 m_should_detach (false),
Sean Callanan6cf6c472011-09-20 23:01:51 +0000773 m_next_event_action_ap(),
774 m_can_jit(eCanJITYes)
Chris Lattner24943d22010-06-08 16:52:24 +0000775{
Caroline Tice1ebef442010-09-27 00:30:10 +0000776 UpdateInstanceName();
777
Greg Claytone005f2c2010-11-06 01:53:30 +0000778 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000779 if (log)
780 log->Printf ("%p Process::Process()", this);
781
Greg Clayton49ce6822010-10-31 03:01:06 +0000782 SetEventName (eBroadcastBitStateChanged, "state-changed");
783 SetEventName (eBroadcastBitInterrupt, "interrupt");
784 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
785 SetEventName (eBroadcastBitSTDERR, "stderr-available");
786
Chris Lattner24943d22010-06-08 16:52:24 +0000787 listener.StartListeningForEvents (this,
788 eBroadcastBitStateChanged |
789 eBroadcastBitInterrupt |
790 eBroadcastBitSTDOUT |
791 eBroadcastBitSTDERR);
792
793 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
794 eBroadcastBitStateChanged);
795
796 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
797 eBroadcastInternalStateControlStop |
798 eBroadcastInternalStateControlPause |
799 eBroadcastInternalStateControlResume);
800}
801
802//----------------------------------------------------------------------
803// Destructor
804//----------------------------------------------------------------------
805Process::~Process()
806{
Greg Claytone005f2c2010-11-06 01:53:30 +0000807 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000808 if (log)
809 log->Printf ("%p Process::~Process()", this);
810 StopPrivateStateThread();
811}
812
813void
814Process::Finalize()
815{
Greg Claytonffa43a62011-11-17 04:46:02 +0000816 switch (GetPrivateState())
817 {
818 case eStateConnected:
819 case eStateAttaching:
820 case eStateLaunching:
821 case eStateStopped:
822 case eStateRunning:
823 case eStateStepping:
824 case eStateCrashed:
825 case eStateSuspended:
826 if (GetShouldDetach())
827 Detach();
828 else
829 Destroy();
830 break;
831
832 case eStateInvalid:
833 case eStateUnloaded:
834 case eStateDetached:
835 case eStateExited:
836 break;
837 }
838
Greg Clayton2f57db02011-10-01 00:45:15 +0000839 // Clear our broadcaster before we proceed with destroying
840 Broadcaster::Clear();
841
Chris Lattner24943d22010-06-08 16:52:24 +0000842 // Do any cleanup needed prior to being destructed... Subclasses
843 // that override this method should call this superclass method as well.
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000844
845 // We need to destroy the loader before the derived Process class gets destroyed
846 // since it is very likely that undoing the loader will require access to the real process.
Greg Clayton182be6a2012-01-20 23:08:34 +0000847 m_dynamic_checkers_ap.reset();
848 m_abi_sp.reset();
Greg Clayton37f962e2011-08-22 02:49:39 +0000849 m_os_ap.reset();
Greg Clayton182be6a2012-01-20 23:08:34 +0000850 m_dyld_ap.reset();
Greg Clayton13d24fb2012-01-29 20:56:30 +0000851 m_thread_list.Destroy();
Greg Clayton182be6a2012-01-20 23:08:34 +0000852 std::vector<Notifications> empty_notifications;
853 m_notifications.swap(empty_notifications);
854 m_image_tokens.clear();
855 m_memory_cache.Clear();
856 m_allocated_memory_cache.Clear();
857 m_language_runtimes.clear();
858 m_next_event_action_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000859}
860
861void
862Process::RegisterNotificationCallbacks (const Notifications& callbacks)
863{
864 m_notifications.push_back(callbacks);
865 if (callbacks.initialize != NULL)
866 callbacks.initialize (callbacks.baton, this);
867}
868
869bool
870Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
871{
872 std::vector<Notifications>::iterator pos, end = m_notifications.end();
873 for (pos = m_notifications.begin(); pos != end; ++pos)
874 {
875 if (pos->baton == callbacks.baton &&
876 pos->initialize == callbacks.initialize &&
877 pos->process_state_changed == callbacks.process_state_changed)
878 {
879 m_notifications.erase(pos);
880 return true;
881 }
882 }
883 return false;
884}
885
886void
887Process::SynchronouslyNotifyStateChanged (StateType state)
888{
889 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
890 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
891 {
892 if (notification_pos->process_state_changed)
893 notification_pos->process_state_changed (notification_pos->baton, this, state);
894 }
895}
896
897// FIXME: We need to do some work on events before the general Listener sees them.
898// For instance if we are continuing from a breakpoint, we need to ensure that we do
899// the little "insert real insn, step & stop" trick. But we can't do that when the
900// event is delivered by the broadcaster - since that is done on the thread that is
901// waiting for new events, so if we needed more than one event for our handling, we would
902// stall. So instead we do it when we fetch the event off of the queue.
903//
904
905StateType
906Process::GetNextEvent (EventSP &event_sp)
907{
908 StateType state = eStateInvalid;
909
910 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
911 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
912
913 return state;
914}
915
916
917StateType
918Process::WaitForProcessToStop (const TimeValue *timeout)
919{
Jim Ingham21f37ad2011-08-09 02:12:22 +0000920 // We can't just wait for a "stopped" event, because the stopped event may have restarted the target.
921 // We have to actually check each event, and in the case of a stopped event check the restarted flag
922 // on the event.
923 EventSP event_sp;
924 StateType state = GetState();
925 // If we are exited or detached, we won't ever get back to any
926 // other valid state...
927 if (state == eStateDetached || state == eStateExited)
928 return state;
929
930 while (state != eStateInvalid)
931 {
932 state = WaitForStateChangedEvents (timeout, event_sp);
933 switch (state)
934 {
935 case eStateCrashed:
936 case eStateDetached:
937 case eStateExited:
938 case eStateUnloaded:
939 return state;
940 case eStateStopped:
941 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
942 continue;
943 else
944 return state;
945 default:
946 continue;
947 }
948 }
949 return state;
Chris Lattner24943d22010-06-08 16:52:24 +0000950}
951
952
953StateType
954Process::WaitForState
955(
956 const TimeValue *timeout,
957 const StateType *match_states, const uint32_t num_match_states
958)
959{
960 EventSP event_sp;
961 uint32_t i;
Greg Claytond8c62532010-10-07 04:19:01 +0000962 StateType state = GetState();
Chris Lattner24943d22010-06-08 16:52:24 +0000963 while (state != eStateInvalid)
964 {
Greg Claytond8c62532010-10-07 04:19:01 +0000965 // If we are exited or detached, we won't ever get back to any
966 // other valid state...
967 if (state == eStateDetached || state == eStateExited)
968 return state;
969
Chris Lattner24943d22010-06-08 16:52:24 +0000970 state = WaitForStateChangedEvents (timeout, event_sp);
971
972 for (i=0; i<num_match_states; ++i)
973 {
974 if (match_states[i] == state)
975 return state;
976 }
977 }
978 return state;
979}
980
Jim Ingham63e24d72010-10-11 23:53:14 +0000981bool
982Process::HijackProcessEvents (Listener *listener)
983{
984 if (listener != NULL)
985 {
986 return HijackBroadcaster(listener, eBroadcastBitStateChanged);
987 }
988 else
989 return false;
990}
991
992void
993Process::RestoreProcessEvents ()
994{
995 RestoreBroadcaster();
996}
997
Jim Inghamf9f40c22011-02-08 05:20:59 +0000998bool
999Process::HijackPrivateProcessEvents (Listener *listener)
1000{
1001 if (listener != NULL)
1002 {
1003 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged);
1004 }
1005 else
1006 return false;
1007}
1008
1009void
1010Process::RestorePrivateProcessEvents ()
1011{
1012 m_private_state_broadcaster.RestoreBroadcaster();
1013}
1014
Chris Lattner24943d22010-06-08 16:52:24 +00001015StateType
1016Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
1017{
Greg Claytone005f2c2010-11-06 01:53:30 +00001018 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001019
1020 if (log)
1021 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
1022
1023 StateType state = eStateInvalid;
Greg Clayton36f63a92010-10-19 03:25:40 +00001024 if (m_listener.WaitForEventForBroadcasterWithType (timeout,
1025 this,
1026 eBroadcastBitStateChanged,
1027 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +00001028 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1029
1030 if (log)
1031 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
1032 __FUNCTION__,
1033 timeout,
1034 StateAsCString(state));
1035 return state;
1036}
1037
1038Event *
1039Process::PeekAtStateChangedEvents ()
1040{
Greg Claytone005f2c2010-11-06 01:53:30 +00001041 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001042
1043 if (log)
1044 log->Printf ("Process::%s...", __FUNCTION__);
1045
1046 Event *event_ptr;
Greg Clayton36f63a92010-10-19 03:25:40 +00001047 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
1048 eBroadcastBitStateChanged);
Chris Lattner24943d22010-06-08 16:52:24 +00001049 if (log)
1050 {
1051 if (event_ptr)
1052 {
1053 log->Printf ("Process::%s (event_ptr) => %s",
1054 __FUNCTION__,
1055 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
1056 }
1057 else
1058 {
1059 log->Printf ("Process::%s no events found",
1060 __FUNCTION__);
1061 }
1062 }
1063 return event_ptr;
1064}
1065
1066StateType
1067Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
1068{
Greg Claytone005f2c2010-11-06 01:53:30 +00001069 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001070
1071 if (log)
1072 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
1073
1074 StateType state = eStateInvalid;
Greg Clayton72e1c782011-01-22 23:43:18 +00001075 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
1076 &m_private_state_broadcaster,
1077 eBroadcastBitStateChanged,
1078 event_sp))
Chris Lattner24943d22010-06-08 16:52:24 +00001079 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1080
1081 // This is a bit of a hack, but when we wait here we could very well return
1082 // to the command-line, and that could disable the log, which would render the
1083 // log we got above invalid.
Chris Lattner24943d22010-06-08 16:52:24 +00001084 if (log)
Greg Clayton72e1c782011-01-22 23:43:18 +00001085 {
1086 if (state == eStateInvalid)
1087 log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout);
1088 else
1089 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
1090 }
Chris Lattner24943d22010-06-08 16:52:24 +00001091 return state;
1092}
1093
1094bool
1095Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
1096{
Greg Claytone005f2c2010-11-06 01:53:30 +00001097 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001098
1099 if (log)
1100 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
1101
1102 if (control_only)
1103 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
1104 else
1105 return m_private_state_listener.WaitForEvent(timeout, event_sp);
1106}
1107
1108bool
1109Process::IsRunning () const
1110{
1111 return StateIsRunningState (m_public_state.GetValue());
1112}
1113
1114int
1115Process::GetExitStatus ()
1116{
1117 if (m_public_state.GetValue() == eStateExited)
1118 return m_exit_status;
1119 return -1;
1120}
1121
Greg Clayton638351a2010-12-04 00:10:17 +00001122
Chris Lattner24943d22010-06-08 16:52:24 +00001123const char *
1124Process::GetExitDescription ()
1125{
1126 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
1127 return m_exit_string.c_str();
1128 return NULL;
1129}
1130
Greg Clayton72e1c782011-01-22 23:43:18 +00001131bool
Chris Lattner24943d22010-06-08 16:52:24 +00001132Process::SetExitStatus (int status, const char *cstr)
1133{
Greg Clayton68ca8232011-01-25 02:58:48 +00001134 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
1135 if (log)
1136 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
1137 status, status,
1138 cstr ? "\"" : "",
1139 cstr ? cstr : "NULL",
1140 cstr ? "\"" : "");
1141
Greg Clayton72e1c782011-01-22 23:43:18 +00001142 // We were already in the exited state
1143 if (m_private_state.GetValue() == eStateExited)
Greg Clayton68ca8232011-01-25 02:58:48 +00001144 {
Greg Clayton644ddfb2011-01-26 23:47:29 +00001145 if (log)
1146 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton72e1c782011-01-22 23:43:18 +00001147 return false;
Greg Clayton68ca8232011-01-25 02:58:48 +00001148 }
Greg Clayton72e1c782011-01-22 23:43:18 +00001149
1150 m_exit_status = status;
1151 if (cstr)
1152 m_exit_string = cstr;
1153 else
1154 m_exit_string.clear();
Chris Lattner24943d22010-06-08 16:52:24 +00001155
Greg Clayton72e1c782011-01-22 23:43:18 +00001156 DidExit ();
Greg Clayton58e844b2010-12-08 05:08:21 +00001157
Greg Clayton72e1c782011-01-22 23:43:18 +00001158 SetPrivateState (eStateExited);
1159 return true;
Chris Lattner24943d22010-06-08 16:52:24 +00001160}
1161
1162// This static callback can be used to watch for local child processes on
1163// the current host. The the child process exits, the process will be
1164// found in the global target list (we want to be completely sure that the
1165// lldb_private::Process doesn't go away before we can deliver the signal.
1166bool
Greg Clayton1c4642c2011-11-16 05:37:56 +00001167Process::SetProcessExitStatus (void *callback_baton,
1168 lldb::pid_t pid,
1169 bool exited,
1170 int signo, // Zero for no signal
1171 int exit_status // Exit value of process if signal is zero
Chris Lattner24943d22010-06-08 16:52:24 +00001172)
1173{
Greg Clayton1c4642c2011-11-16 05:37:56 +00001174 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
1175 if (log)
Greg Claytond9919d32011-12-01 23:28:38 +00001176 log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%llu, exited=%i, signal=%i, exit_status=%i)\n",
Greg Clayton1c4642c2011-11-16 05:37:56 +00001177 callback_baton,
1178 pid,
1179 exited,
1180 signo,
1181 exit_status);
1182
1183 if (exited)
Chris Lattner24943d22010-06-08 16:52:24 +00001184 {
Greg Clayton63094e02010-06-23 01:19:29 +00001185 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner24943d22010-06-08 16:52:24 +00001186 if (target_sp)
1187 {
1188 ProcessSP process_sp (target_sp->GetProcessSP());
1189 if (process_sp)
1190 {
1191 const char *signal_cstr = NULL;
1192 if (signo)
1193 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
1194
1195 process_sp->SetExitStatus (exit_status, signal_cstr);
1196 }
1197 }
1198 return true;
1199 }
1200 return false;
1201}
1202
1203
Greg Clayton37f962e2011-08-22 02:49:39 +00001204void
1205Process::UpdateThreadListIfNeeded ()
1206{
1207 const uint32_t stop_id = GetStopID();
1208 if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID())
1209 {
Greg Clayton20206082011-11-17 01:23:07 +00001210 const StateType state = GetPrivateState();
1211 if (StateIsStoppedState (state, true))
1212 {
1213 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytonffa43a62011-11-17 04:46:02 +00001214 // m_thread_list does have its own mutex, but we need to
1215 // hold onto the mutex between the call to UpdateThreadList(...)
1216 // and the os->UpdateThreadList(...) so it doesn't change on us
Greg Clayton20206082011-11-17 01:23:07 +00001217 ThreadList new_thread_list(this);
1218 // Always update the thread list with the protocol specific
1219 // thread list
1220 UpdateThreadList (m_thread_list, new_thread_list);
1221 OperatingSystem *os = GetOperatingSystem ();
1222 if (os)
1223 os->UpdateThreadList (m_thread_list, new_thread_list);
1224 m_thread_list.Update (new_thread_list);
1225 m_thread_list.SetStopID (stop_id);
1226 }
Greg Clayton37f962e2011-08-22 02:49:39 +00001227 }
1228}
1229
Chris Lattner24943d22010-06-08 16:52:24 +00001230uint32_t
1231Process::GetNextThreadIndexID ()
1232{
1233 return ++m_thread_index_id;
1234}
1235
1236StateType
1237Process::GetState()
1238{
1239 // If any other threads access this we will need a mutex for it
1240 return m_public_state.GetValue ();
1241}
1242
1243void
1244Process::SetPublicState (StateType new_state)
1245{
Greg Clayton68ca8232011-01-25 02:58:48 +00001246 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001247 if (log)
1248 log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state));
1249 m_public_state.SetValue (new_state);
1250}
1251
1252StateType
1253Process::GetPrivateState ()
1254{
1255 return m_private_state.GetValue();
1256}
1257
1258void
1259Process::SetPrivateState (StateType new_state)
1260{
Greg Clayton68ca8232011-01-25 02:58:48 +00001261 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00001262 bool state_changed = false;
1263
1264 if (log)
1265 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
1266
1267 Mutex::Locker locker(m_private_state.GetMutex());
1268
1269 const StateType old_state = m_private_state.GetValueNoLock ();
1270 state_changed = old_state != new_state;
1271 if (state_changed)
1272 {
1273 m_private_state.SetValueNoLock (new_state);
Greg Clayton20206082011-11-17 01:23:07 +00001274 if (StateIsStoppedState(new_state, false))
Chris Lattner24943d22010-06-08 16:52:24 +00001275 {
Jim Ingham21f37ad2011-08-09 02:12:22 +00001276 m_mod_id.BumpStopID();
Greg Claytonfd119992011-01-07 06:08:19 +00001277 m_memory_cache.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00001278 if (log)
Jim Ingham21f37ad2011-08-09 02:12:22 +00001279 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID());
Chris Lattner24943d22010-06-08 16:52:24 +00001280 }
1281 // Use our target to get a shared pointer to ourselves...
1282 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state));
1283 }
1284 else
1285 {
1286 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001287 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state));
Chris Lattner24943d22010-06-08 16:52:24 +00001288 }
1289}
1290
Jim Ingham0296fe72011-11-08 03:00:11 +00001291void
1292Process::SetRunningUserExpression (bool on)
1293{
1294 m_mod_id.SetRunningUserExpression (on);
1295}
1296
Chris Lattner24943d22010-06-08 16:52:24 +00001297addr_t
1298Process::GetImageInfoAddress()
1299{
1300 return LLDB_INVALID_ADDRESS;
1301}
1302
Greg Clayton0baa3942010-11-04 01:54:29 +00001303//----------------------------------------------------------------------
1304// LoadImage
1305//
1306// This function provides a default implementation that works for most
1307// unix variants. Any Process subclasses that need to do shared library
1308// loading differently should override LoadImage and UnloadImage and
1309// do what is needed.
1310//----------------------------------------------------------------------
1311uint32_t
1312Process::LoadImage (const FileSpec &image_spec, Error &error)
1313{
1314 DynamicLoader *loader = GetDynamicLoader();
1315 if (loader)
1316 {
1317 error = loader->CanLoadImage();
1318 if (error.Fail())
1319 return LLDB_INVALID_IMAGE_TOKEN;
1320 }
1321
1322 if (error.Success())
1323 {
1324 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton0baa3942010-11-04 01:54:29 +00001325
1326 if (thread_sp)
1327 {
1328 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
1329
1330 if (frame_sp)
1331 {
1332 ExecutionContext exe_ctx;
1333 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +00001334 bool unwind_on_error = true;
Greg Clayton0baa3942010-11-04 01:54:29 +00001335 StreamString expr;
1336 char path[PATH_MAX];
1337 image_spec.GetPath(path, sizeof(path));
1338 expr.Printf("dlopen (\"%s\", 2)", path);
1339 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Ingham360f53f2010-11-30 02:22:11 +00001340 lldb::ValueObjectSP result_valobj_sp;
Sean Callanandaa6efe2011-12-21 22:22:58 +00001341 ClangUserExpression::Evaluate (exe_ctx, eExecutionPolicyAlways, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny, unwind_on_error, expr.GetData(), prefix, result_valobj_sp);
Johnny Chenb14ec342011-09-09 00:01:43 +00001342 error = result_valobj_sp->GetError();
1343 if (error.Success())
Greg Clayton0baa3942010-11-04 01:54:29 +00001344 {
1345 Scalar scalar;
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001346 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton0baa3942010-11-04 01:54:29 +00001347 {
1348 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1349 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
1350 {
1351 uint32_t image_token = m_image_tokens.size();
1352 m_image_tokens.push_back (image_ptr);
1353 return image_token;
1354 }
1355 }
1356 }
1357 }
1358 }
1359 }
1360 return LLDB_INVALID_IMAGE_TOKEN;
1361}
1362
1363//----------------------------------------------------------------------
1364// UnloadImage
1365//
1366// This function provides a default implementation that works for most
1367// unix variants. Any Process subclasses that need to do shared library
1368// loading differently should override LoadImage and UnloadImage and
1369// do what is needed.
1370//----------------------------------------------------------------------
1371Error
1372Process::UnloadImage (uint32_t image_token)
1373{
1374 Error error;
1375 if (image_token < m_image_tokens.size())
1376 {
1377 const addr_t image_addr = m_image_tokens[image_token];
1378 if (image_addr == LLDB_INVALID_ADDRESS)
1379 {
1380 error.SetErrorString("image already unloaded");
1381 }
1382 else
1383 {
1384 DynamicLoader *loader = GetDynamicLoader();
1385 if (loader)
1386 error = loader->CanLoadImage();
1387
1388 if (error.Success())
1389 {
1390 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton0baa3942010-11-04 01:54:29 +00001391
1392 if (thread_sp)
1393 {
1394 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
1395
1396 if (frame_sp)
1397 {
1398 ExecutionContext exe_ctx;
1399 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Inghamea9d4262010-11-05 19:25:48 +00001400 bool unwind_on_error = true;
Greg Clayton0baa3942010-11-04 01:54:29 +00001401 StreamString expr;
1402 expr.Printf("dlclose ((void *)0x%llx)", image_addr);
1403 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Ingham360f53f2010-11-30 02:22:11 +00001404 lldb::ValueObjectSP result_valobj_sp;
Sean Callanandaa6efe2011-12-21 22:22:58 +00001405 ClangUserExpression::Evaluate (exe_ctx, eExecutionPolicyAlways, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny, unwind_on_error, expr.GetData(), prefix, result_valobj_sp);
Greg Clayton0baa3942010-11-04 01:54:29 +00001406 if (result_valobj_sp->GetError().Success())
1407 {
1408 Scalar scalar;
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001409 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton0baa3942010-11-04 01:54:29 +00001410 {
1411 if (scalar.UInt(1))
1412 {
1413 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1414 }
1415 else
1416 {
1417 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1418 }
1419 }
1420 }
1421 else
1422 {
1423 error = result_valobj_sp->GetError();
1424 }
1425 }
1426 }
1427 }
1428 }
1429 }
1430 else
1431 {
1432 error.SetErrorString("invalid image token");
1433 }
1434 return error;
1435}
1436
Greg Clayton75906e42011-05-11 18:39:18 +00001437const lldb::ABISP &
Chris Lattner24943d22010-06-08 16:52:24 +00001438Process::GetABI()
1439{
Greg Clayton75906e42011-05-11 18:39:18 +00001440 if (!m_abi_sp)
1441 m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture());
1442 return m_abi_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001443}
1444
Jim Ingham642036f2010-09-23 02:01:19 +00001445LanguageRuntime *
1446Process::GetLanguageRuntime(lldb::LanguageType language)
1447{
1448 LanguageRuntimeCollection::iterator pos;
1449 pos = m_language_runtimes.find (language);
1450 if (pos == m_language_runtimes.end())
1451 {
1452 lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language));
1453
1454 m_language_runtimes[language]
1455 = runtime;
1456 return runtime.get();
1457 }
1458 else
1459 return (*pos).second.get();
1460}
1461
1462CPPLanguageRuntime *
1463Process::GetCPPLanguageRuntime ()
1464{
1465 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus);
1466 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
1467 return static_cast<CPPLanguageRuntime *> (runtime);
1468 return NULL;
1469}
1470
1471ObjCLanguageRuntime *
1472Process::GetObjCLanguageRuntime ()
1473{
1474 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC);
1475 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
1476 return static_cast<ObjCLanguageRuntime *> (runtime);
1477 return NULL;
1478}
1479
Chris Lattner24943d22010-06-08 16:52:24 +00001480BreakpointSiteList &
1481Process::GetBreakpointSiteList()
1482{
1483 return m_breakpoint_site_list;
1484}
1485
1486const BreakpointSiteList &
1487Process::GetBreakpointSiteList() const
1488{
1489 return m_breakpoint_site_list;
1490}
1491
1492
1493void
1494Process::DisableAllBreakpointSites ()
1495{
1496 m_breakpoint_site_list.SetEnabledForAll (false);
1497}
1498
1499Error
1500Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
1501{
1502 Error error (DisableBreakpointSiteByID (break_id));
1503
1504 if (error.Success())
1505 m_breakpoint_site_list.Remove(break_id);
1506
1507 return error;
1508}
1509
1510Error
1511Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
1512{
1513 Error error;
1514 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1515 if (bp_site_sp)
1516 {
1517 if (bp_site_sp->IsEnabled())
1518 error = DisableBreakpoint (bp_site_sp.get());
1519 }
1520 else
1521 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001522 error.SetErrorStringWithFormat("invalid breakpoint site ID: %llu", break_id);
Chris Lattner24943d22010-06-08 16:52:24 +00001523 }
1524
1525 return error;
1526}
1527
1528Error
1529Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
1530{
1531 Error error;
1532 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1533 if (bp_site_sp)
1534 {
1535 if (!bp_site_sp->IsEnabled())
1536 error = EnableBreakpoint (bp_site_sp.get());
1537 }
1538 else
1539 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001540 error.SetErrorStringWithFormat("invalid breakpoint site ID: %llu", break_id);
Chris Lattner24943d22010-06-08 16:52:24 +00001541 }
1542 return error;
1543}
1544
Stephen Wilson3fd1f362010-07-17 00:56:13 +00001545lldb::break_id_t
Greg Clayton13d24fb2012-01-29 20:56:30 +00001546Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware)
Chris Lattner24943d22010-06-08 16:52:24 +00001547{
Greg Clayton265ab332011-05-19 18:17:41 +00001548 const addr_t load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
Chris Lattner24943d22010-06-08 16:52:24 +00001549 if (load_addr != LLDB_INVALID_ADDRESS)
1550 {
1551 BreakpointSiteSP bp_site_sp;
1552
1553 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
1554 // create a new breakpoint site and add it.
1555
1556 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
1557
1558 if (bp_site_sp)
1559 {
1560 bp_site_sp->AddOwner (owner);
1561 owner->SetBreakpointSite (bp_site_sp);
1562 return bp_site_sp->GetID();
1563 }
1564 else
1565 {
1566 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware));
1567 if (bp_site_sp)
1568 {
1569 if (EnableBreakpoint (bp_site_sp.get()).Success())
1570 {
1571 owner->SetBreakpointSite (bp_site_sp);
1572 return m_breakpoint_site_list.Add (bp_site_sp);
1573 }
1574 }
1575 }
1576 }
1577 // We failed to enable the breakpoint
1578 return LLDB_INVALID_BREAK_ID;
1579
1580}
1581
1582void
1583Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
1584{
1585 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
1586 if (num_owners == 0)
1587 {
1588 DisableBreakpoint(bp_site_sp.get());
1589 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1590 }
1591}
1592
1593
1594size_t
1595Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1596{
1597 size_t bytes_removed = 0;
1598 addr_t intersect_addr;
1599 size_t intersect_size;
1600 size_t opcode_offset;
1601 size_t idx;
Greg Clayton987c7eb2011-09-17 08:33:22 +00001602 BreakpointSiteSP bp_sp;
Jim Ingham82820f92011-06-29 19:42:28 +00001603 BreakpointSiteList bp_sites_in_range;
Chris Lattner24943d22010-06-08 16:52:24 +00001604
Jim Ingham82820f92011-06-29 19:42:28 +00001605 if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range))
Chris Lattner24943d22010-06-08 16:52:24 +00001606 {
Greg Clayton987c7eb2011-09-17 08:33:22 +00001607 for (idx = 0; (bp_sp = bp_sites_in_range.GetByIndex(idx)); ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001608 {
Greg Clayton987c7eb2011-09-17 08:33:22 +00001609 if (bp_sp->GetType() == BreakpointSite::eSoftware)
Chris Lattner24943d22010-06-08 16:52:24 +00001610 {
Greg Clayton987c7eb2011-09-17 08:33:22 +00001611 if (bp_sp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
Jim Ingham82820f92011-06-29 19:42:28 +00001612 {
1613 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1614 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
Greg Clayton987c7eb2011-09-17 08:33:22 +00001615 assert(opcode_offset + intersect_size <= bp_sp->GetByteSize());
Jim Ingham82820f92011-06-29 19:42:28 +00001616 size_t buf_offset = intersect_addr - bp_addr;
Greg Clayton987c7eb2011-09-17 08:33:22 +00001617 ::memcpy(buf + buf_offset, bp_sp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
Jim Ingham82820f92011-06-29 19:42:28 +00001618 }
Chris Lattner24943d22010-06-08 16:52:24 +00001619 }
1620 }
1621 }
1622 return bytes_removed;
1623}
1624
1625
Greg Claytonb1888f22011-03-19 01:12:21 +00001626
1627size_t
1628Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
1629{
1630 PlatformSP platform_sp (m_target.GetPlatform());
1631 if (platform_sp)
1632 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
1633 return 0;
1634}
1635
Chris Lattner24943d22010-06-08 16:52:24 +00001636Error
1637Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
1638{
1639 Error error;
1640 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +00001641 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001642 const addr_t bp_addr = bp_site->GetLoadAddress();
1643 if (log)
1644 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr);
1645 if (bp_site->IsEnabled())
1646 {
1647 if (log)
1648 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
1649 return error;
1650 }
1651
1652 if (bp_addr == LLDB_INVALID_ADDRESS)
1653 {
1654 error.SetErrorString("BreakpointSite contains an invalid load address.");
1655 return error;
1656 }
1657 // Ask the lldb::Process subclass to fill in the correct software breakpoint
1658 // trap for the breakpoint site
1659 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
1660
1661 if (bp_opcode_size == 0)
1662 {
Greg Clayton9c236732011-10-26 00:56:27 +00001663 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx", bp_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001664 }
1665 else
1666 {
1667 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
1668
1669 if (bp_opcode_bytes == NULL)
1670 {
1671 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
1672 return error;
1673 }
1674
1675 // Save the original opcode by reading it
1676 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
1677 {
1678 // Write a software breakpoint in place of the original opcode
1679 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1680 {
1681 uint8_t verify_bp_opcode_bytes[64];
1682 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
1683 {
1684 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
1685 {
1686 bp_site->SetEnabled(true);
1687 bp_site->SetType (BreakpointSite::eSoftware);
1688 if (log)
1689 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS",
1690 bp_site->GetID(),
1691 (uint64_t)bp_addr);
1692 }
1693 else
Greg Clayton9c236732011-10-26 00:56:27 +00001694 error.SetErrorString("failed to verify the breakpoint trap in memory.");
Chris Lattner24943d22010-06-08 16:52:24 +00001695 }
1696 else
1697 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
1698 }
1699 else
1700 error.SetErrorString("Unable to write breakpoint trap to memory.");
1701 }
1702 else
1703 error.SetErrorString("Unable to read memory at breakpoint address.");
1704 }
Stephen Wilsonc2b98252011-01-12 04:20:03 +00001705 if (log && error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +00001706 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1707 bp_site->GetID(),
1708 (uint64_t)bp_addr,
1709 error.AsCString());
1710 return error;
1711}
1712
1713Error
1714Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
1715{
1716 Error error;
1717 assert (bp_site != NULL);
Greg Claytone005f2c2010-11-06 01:53:30 +00001718 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +00001719 addr_t bp_addr = bp_site->GetLoadAddress();
1720 lldb::user_id_t breakID = bp_site->GetID();
1721 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00001722 log->Printf ("Process::DisableBreakpoint (breakID = %llu) addr = 0x%llx", breakID, (uint64_t)bp_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001723
1724 if (bp_site->IsHardware())
1725 {
1726 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
1727 }
1728 else if (bp_site->IsEnabled())
1729 {
1730 const size_t break_op_size = bp_site->GetByteSize();
1731 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
1732 if (break_op_size > 0)
1733 {
1734 // Clear a software breakoint instruction
Greg Clayton54e7afa2010-07-09 20:39:50 +00001735 uint8_t curr_break_op[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +00001736 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner24943d22010-06-08 16:52:24 +00001737 bool break_op_found = false;
1738
1739 // Read the breakpoint opcode
1740 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
1741 {
1742 bool verify = false;
1743 // Make sure we have the a breakpoint opcode exists at this address
1744 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
1745 {
1746 break_op_found = true;
1747 // We found a valid breakpoint opcode at this address, now restore
1748 // the saved opcode.
1749 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
1750 {
1751 verify = true;
1752 }
1753 else
1754 error.SetErrorString("Memory write failed when restoring original opcode.");
1755 }
1756 else
1757 {
1758 error.SetErrorString("Original breakpoint trap is no longer in memory.");
1759 // Set verify to true and so we can check if the original opcode has already been restored
1760 verify = true;
1761 }
1762
1763 if (verify)
1764 {
Greg Clayton54e7afa2010-07-09 20:39:50 +00001765 uint8_t verify_opcode[8];
Stephen Wilson141eeac2010-07-20 18:41:11 +00001766 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner24943d22010-06-08 16:52:24 +00001767 // Verify that our original opcode made it back to the inferior
1768 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
1769 {
1770 // compare the memory we just read with the original opcode
1771 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
1772 {
1773 // SUCCESS
1774 bp_site->SetEnabled(false);
1775 if (log)
1776 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
1777 return error;
1778 }
1779 else
1780 {
1781 if (break_op_found)
1782 error.SetErrorString("Failed to restore original opcode.");
1783 }
1784 }
1785 else
1786 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
1787 }
1788 }
1789 else
1790 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
1791 }
1792 }
1793 else
1794 {
1795 if (log)
1796 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
1797 return error;
1798 }
1799
1800 if (log)
1801 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s",
1802 bp_site->GetID(),
1803 (uint64_t)bp_addr,
1804 error.AsCString());
1805 return error;
1806
1807}
1808
Greg Claytonfd119992011-01-07 06:08:19 +00001809// Comment out line below to disable memory caching
1810#define ENABLE_MEMORY_CACHING
1811// Uncomment to verify memory caching works after making changes to caching code
1812//#define VERIFY_MEMORY_READS
1813
1814#if defined (ENABLE_MEMORY_CACHING)
1815
1816#if defined (VERIFY_MEMORY_READS)
Chris Lattner24943d22010-06-08 16:52:24 +00001817
1818size_t
1819Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1820{
Greg Claytonfd119992011-01-07 06:08:19 +00001821 // Memory caching is enabled, with debug verification
1822 if (buf && size)
1823 {
1824 // Uncomment the line below to make sure memory caching is working.
1825 // I ran this through the test suite and got no assertions, so I am
1826 // pretty confident this is working well. If any changes are made to
1827 // memory caching, uncomment the line below and test your changes!
1828
1829 // Verify all memory reads by using the cache first, then redundantly
1830 // reading the same memory from the inferior and comparing to make sure
1831 // everything is exactly the same.
1832 std::string verify_buf (size, '\0');
1833 assert (verify_buf.size() == size);
1834 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
1835 Error verify_error;
1836 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
1837 assert (cache_bytes_read == verify_bytes_read);
1838 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
1839 assert (verify_error.Success() == error.Success());
1840 return cache_bytes_read;
1841 }
1842 return 0;
1843}
1844
1845#else // #if defined (VERIFY_MEMORY_READS)
1846
1847size_t
1848Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1849{
1850 // Memory caching enabled, no verification
Greg Clayton613b8732011-05-17 03:37:42 +00001851 return m_memory_cache.Read (addr, buf, size, error);
Greg Claytonfd119992011-01-07 06:08:19 +00001852}
1853
1854#endif // #else for #if defined (VERIFY_MEMORY_READS)
1855
1856#else // #if defined (ENABLE_MEMORY_CACHING)
1857
1858size_t
1859Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
1860{
1861 // Memory caching is disabled
1862 return ReadMemoryFromInferior (addr, buf, size, error);
1863}
1864
1865#endif // #else for #if defined (ENABLE_MEMORY_CACHING)
1866
1867
1868size_t
Greg Clayton4a2e3372011-12-15 03:14:23 +00001869Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error)
Greg Claytonb72d0f02011-04-12 05:54:46 +00001870{
1871 size_t total_cstr_len = 0;
1872 if (dst && dst_max_len)
1873 {
Greg Clayton4a2e3372011-12-15 03:14:23 +00001874 result_error.Clear();
Greg Claytonb72d0f02011-04-12 05:54:46 +00001875 // NULL out everything just to be safe
1876 memset (dst, 0, dst_max_len);
1877 Error error;
1878 addr_t curr_addr = addr;
1879 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
1880 size_t bytes_left = dst_max_len - 1;
1881 char *curr_dst = dst;
1882
1883 while (bytes_left > 0)
1884 {
1885 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1886 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1887 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
1888
1889 if (bytes_read == 0)
1890 {
Greg Clayton4a2e3372011-12-15 03:14:23 +00001891 result_error = error;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001892 dst[total_cstr_len] = '\0';
1893 break;
1894 }
1895 const size_t len = strlen(curr_dst);
1896
1897 total_cstr_len += len;
1898
1899 if (len < bytes_to_read)
1900 break;
1901
1902 curr_dst += bytes_read;
1903 curr_addr += bytes_read;
1904 bytes_left -= bytes_read;
1905 }
1906 }
Greg Clayton4a2e3372011-12-15 03:14:23 +00001907 else
1908 {
1909 if (dst == NULL)
1910 result_error.SetErrorString("invalid arguments");
1911 else
1912 result_error.Clear();
1913 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001914 return total_cstr_len;
1915}
1916
1917size_t
Greg Claytonfd119992011-01-07 06:08:19 +00001918Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
1919{
Chris Lattner24943d22010-06-08 16:52:24 +00001920 if (buf == NULL || size == 0)
1921 return 0;
1922
1923 size_t bytes_read = 0;
1924 uint8_t *bytes = (uint8_t *)buf;
1925
1926 while (bytes_read < size)
1927 {
1928 const size_t curr_size = size - bytes_read;
1929 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
1930 bytes + bytes_read,
1931 curr_size,
1932 error);
1933 bytes_read += curr_bytes_read;
1934 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
1935 break;
1936 }
1937
1938 // Replace any software breakpoint opcodes that fall into this range back
1939 // into "buf" before we return
1940 if (bytes_read > 0)
1941 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
1942 return bytes_read;
1943}
1944
Greg Claytonf72fdee2010-12-16 20:01:20 +00001945uint64_t
Greg Claytonc0fa5332011-05-22 22:46:53 +00001946Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error)
Greg Claytonf72fdee2010-12-16 20:01:20 +00001947{
Greg Claytonc0fa5332011-05-22 22:46:53 +00001948 Scalar scalar;
1949 if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error))
1950 return scalar.ULongLong(fail_value);
1951 return fail_value;
1952}
1953
1954addr_t
1955Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error)
1956{
1957 Scalar scalar;
1958 if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error))
1959 return scalar.ULongLong(LLDB_INVALID_ADDRESS);
1960 return LLDB_INVALID_ADDRESS;
1961}
1962
1963
1964bool
1965Process::WritePointerToMemory (lldb::addr_t vm_addr,
1966 lldb::addr_t ptr_value,
1967 Error &error)
1968{
1969 Scalar scalar;
1970 const uint32_t addr_byte_size = GetAddressByteSize();
1971 if (addr_byte_size <= 4)
1972 scalar = (uint32_t)ptr_value;
Greg Claytonf72fdee2010-12-16 20:01:20 +00001973 else
Greg Claytonc0fa5332011-05-22 22:46:53 +00001974 scalar = ptr_value;
1975 return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size;
Greg Claytonf72fdee2010-12-16 20:01:20 +00001976}
1977
Chris Lattner24943d22010-06-08 16:52:24 +00001978size_t
1979Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
1980{
1981 size_t bytes_written = 0;
1982 const uint8_t *bytes = (const uint8_t *)buf;
1983
1984 while (bytes_written < size)
1985 {
1986 const size_t curr_size = size - bytes_written;
1987 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
1988 bytes + bytes_written,
1989 curr_size,
1990 error);
1991 bytes_written += curr_bytes_written;
1992 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
1993 break;
1994 }
1995 return bytes_written;
1996}
1997
1998size_t
1999Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2000{
Greg Claytonfd119992011-01-07 06:08:19 +00002001#if defined (ENABLE_MEMORY_CACHING)
2002 m_memory_cache.Flush (addr, size);
2003#endif
2004
Chris Lattner24943d22010-06-08 16:52:24 +00002005 if (buf == NULL || size == 0)
2006 return 0;
Jim Inghame41494a2011-04-16 00:01:13 +00002007
Jim Ingham21f37ad2011-08-09 02:12:22 +00002008 m_mod_id.BumpMemoryID();
Jim Inghame41494a2011-04-16 00:01:13 +00002009
Chris Lattner24943d22010-06-08 16:52:24 +00002010 // We need to write any data that would go where any current software traps
2011 // (enabled software breakpoints) any software traps (breakpoints) that we
2012 // may have placed in our tasks memory.
2013
2014 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
2015 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
2016
2017 if (iter == end || iter->second->GetLoadAddress() > addr + size)
Greg Claytonc8bc1c32011-05-16 02:35:02 +00002018 return WriteMemoryPrivate (addr, buf, size, error);
Chris Lattner24943d22010-06-08 16:52:24 +00002019
2020 BreakpointSiteList::collection::const_iterator pos;
2021 size_t bytes_written = 0;
Greg Clayton54e7afa2010-07-09 20:39:50 +00002022 addr_t intersect_addr = 0;
2023 size_t intersect_size = 0;
2024 size_t opcode_offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +00002025 const uint8_t *ubuf = (const uint8_t *)buf;
2026
2027 for (pos = iter; pos != end; ++pos)
2028 {
2029 BreakpointSiteSP bp;
2030 bp = pos->second;
2031
2032 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
2033 assert(addr <= intersect_addr && intersect_addr < addr + size);
2034 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
2035 assert(opcode_offset + intersect_size <= bp->GetByteSize());
2036
2037 // Check for bytes before this breakpoint
2038 const addr_t curr_addr = addr + bytes_written;
2039 if (intersect_addr > curr_addr)
2040 {
2041 // There are some bytes before this breakpoint that we need to
2042 // just write to memory
2043 size_t curr_size = intersect_addr - curr_addr;
2044 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
2045 ubuf + bytes_written,
2046 curr_size,
2047 error);
2048 bytes_written += curr_bytes_written;
2049 if (curr_bytes_written != curr_size)
2050 {
2051 // We weren't able to write all of the requested bytes, we
2052 // are done looping and will return the number of bytes that
2053 // we have written so far.
2054 break;
2055 }
2056 }
2057
2058 // Now write any bytes that would cover up any software breakpoints
2059 // directly into the breakpoint opcode buffer
2060 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
2061 bytes_written += intersect_size;
2062 }
2063
2064 // Write any remaining bytes after the last breakpoint if we have any left
2065 if (bytes_written < size)
2066 bytes_written += WriteMemoryPrivate (addr + bytes_written,
2067 ubuf + bytes_written,
2068 size - bytes_written,
2069 error);
Jim Inghame41494a2011-04-16 00:01:13 +00002070
Chris Lattner24943d22010-06-08 16:52:24 +00002071 return bytes_written;
2072}
Greg Claytonc0fa5332011-05-22 22:46:53 +00002073
2074size_t
2075Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, uint32_t byte_size, Error &error)
2076{
2077 if (byte_size == UINT32_MAX)
2078 byte_size = scalar.GetByteSize();
2079 if (byte_size > 0)
2080 {
2081 uint8_t buf[32];
2082 const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error);
2083 if (mem_size > 0)
2084 return WriteMemory(addr, buf, mem_size, error);
2085 else
2086 error.SetErrorString ("failed to get scalar as memory data");
2087 }
2088 else
2089 {
2090 error.SetErrorString ("invalid scalar value");
2091 }
2092 return 0;
2093}
2094
2095size_t
2096Process::ReadScalarIntegerFromMemory (addr_t addr,
2097 uint32_t byte_size,
2098 bool is_signed,
2099 Scalar &scalar,
2100 Error &error)
2101{
2102 uint64_t uval;
2103
2104 if (byte_size <= sizeof(uval))
2105 {
2106 size_t bytes_read = ReadMemory (addr, &uval, byte_size, error);
2107 if (bytes_read == byte_size)
2108 {
2109 DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize());
2110 uint32_t offset = 0;
2111 if (byte_size <= 4)
2112 scalar = data.GetMaxU32 (&offset, byte_size);
2113 else
2114 scalar = data.GetMaxU64 (&offset, byte_size);
2115
2116 if (is_signed)
2117 scalar.SignExtend(byte_size * 8);
2118 return bytes_read;
2119 }
2120 }
2121 else
2122 {
2123 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
2124 }
2125 return 0;
2126}
2127
Greg Clayton613b8732011-05-17 03:37:42 +00002128#define USE_ALLOCATE_MEMORY_CACHE 1
Chris Lattner24943d22010-06-08 16:52:24 +00002129addr_t
2130Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
2131{
Jim Inghame6bd1422011-06-20 17:32:44 +00002132 if (GetPrivateState() != eStateStopped)
2133 return LLDB_INVALID_ADDRESS;
2134
Greg Clayton613b8732011-05-17 03:37:42 +00002135#if defined (USE_ALLOCATE_MEMORY_CACHE)
2136 return m_allocated_memory_cache.AllocateMemory(size, permissions, error);
2137#else
Greg Clayton2860ba92011-01-23 19:58:49 +00002138 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
2139 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2140 if (log)
Jim Ingham21f37ad2011-08-09 02:12:22 +00002141 log->Printf("Process::AllocateMemory(size=%4zu, permissions=%s) => 0x%16.16llx (m_stop_id = %u m_memory_id = %u)",
Greg Clayton2860ba92011-01-23 19:58:49 +00002142 size,
Greg Clayton613b8732011-05-17 03:37:42 +00002143 GetPermissionsAsCString (permissions),
Greg Clayton2860ba92011-01-23 19:58:49 +00002144 (uint64_t)allocated_addr,
Jim Ingham21f37ad2011-08-09 02:12:22 +00002145 m_mod_id.GetStopID(),
2146 m_mod_id.GetMemoryID());
Greg Clayton2860ba92011-01-23 19:58:49 +00002147 return allocated_addr;
Greg Clayton613b8732011-05-17 03:37:42 +00002148#endif
Chris Lattner24943d22010-06-08 16:52:24 +00002149}
2150
Sean Callanan6cf6c472011-09-20 23:01:51 +00002151bool
2152Process::CanJIT ()
2153{
2154 return m_can_jit == eCanJITYes;
2155}
2156
2157void
2158Process::SetCanJIT (bool can_jit)
2159{
2160 m_can_jit = (can_jit ? eCanJITYes : eCanJITNo);
2161}
2162
Chris Lattner24943d22010-06-08 16:52:24 +00002163Error
2164Process::DeallocateMemory (addr_t ptr)
2165{
Greg Clayton613b8732011-05-17 03:37:42 +00002166 Error error;
2167#if defined (USE_ALLOCATE_MEMORY_CACHE)
2168 if (!m_allocated_memory_cache.DeallocateMemory(ptr))
2169 {
2170 error.SetErrorStringWithFormat ("deallocation of memory at 0x%llx failed.", (uint64_t)ptr);
2171 }
2172#else
2173 error = DoDeallocateMemory (ptr);
Greg Clayton2860ba92011-01-23 19:58:49 +00002174
2175 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2176 if (log)
Jim Ingham21f37ad2011-08-09 02:12:22 +00002177 log->Printf("Process::DeallocateMemory(addr=0x%16.16llx) => err = %s (m_stop_id = %u, m_memory_id = %u)",
Greg Clayton2860ba92011-01-23 19:58:49 +00002178 ptr,
2179 error.AsCString("SUCCESS"),
Jim Ingham21f37ad2011-08-09 02:12:22 +00002180 m_mod_id.GetStopID(),
2181 m_mod_id.GetMemoryID());
Greg Clayton613b8732011-05-17 03:37:42 +00002182#endif
Greg Clayton2860ba92011-01-23 19:58:49 +00002183 return error;
Chris Lattner24943d22010-06-08 16:52:24 +00002184}
2185
Greg Claytonb5a8f142012-02-05 02:38:54 +00002186ModuleSP
2187Process::ReadModuleFromMemory (const FileSpec& file_spec, lldb::addr_t header_addr)
2188{
2189 ModuleSP module_sp (new Module (file_spec, shared_from_this(), header_addr));
2190 if (module_sp)
2191 {
2192 m_target.GetImages().Append(module_sp);
2193 bool changed = false;
2194 module_sp->SetLoadAddress (m_target, 0, changed);
2195 }
2196 return module_sp;
2197}
Chris Lattner24943d22010-06-08 16:52:24 +00002198
2199Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00002200Process::EnableWatchpoint (Watchpoint *watchpoint)
Chris Lattner24943d22010-06-08 16:52:24 +00002201{
2202 Error error;
2203 error.SetErrorString("watchpoints are not supported");
2204 return error;
2205}
2206
2207Error
Johnny Chenecd4feb2011-10-14 00:42:25 +00002208Process::DisableWatchpoint (Watchpoint *watchpoint)
Chris Lattner24943d22010-06-08 16:52:24 +00002209{
2210 Error error;
2211 error.SetErrorString("watchpoints are not supported");
2212 return error;
2213}
2214
2215StateType
2216Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
2217{
2218 StateType state;
2219 // Now wait for the process to launch and return control to us, and then
2220 // call DidLaunch:
2221 while (1)
2222 {
Greg Clayton72e1c782011-01-22 23:43:18 +00002223 event_sp.reset();
2224 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
2225
Greg Clayton20206082011-11-17 01:23:07 +00002226 if (StateIsStoppedState(state, false))
Chris Lattner24943d22010-06-08 16:52:24 +00002227 break;
Greg Clayton72e1c782011-01-22 23:43:18 +00002228
2229 // If state is invalid, then we timed out
2230 if (state == eStateInvalid)
2231 break;
2232
2233 if (event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002234 HandlePrivateEvent (event_sp);
2235 }
2236 return state;
2237}
2238
2239Error
Greg Clayton36bc5ea2011-11-03 21:22:33 +00002240Process::Launch (const ProcessLaunchInfo &launch_info)
Chris Lattner24943d22010-06-08 16:52:24 +00002241{
2242 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +00002243 m_abi_sp.reset();
Greg Clayton75c703d2011-02-16 04:46:07 +00002244 m_dyld_ap.reset();
Greg Clayton37f962e2011-08-22 02:49:39 +00002245 m_os_ap.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00002246 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00002247
Greg Clayton5beb99d2011-08-11 02:48:45 +00002248 Module *exe_module = m_target.GetExecutableModulePointer();
Chris Lattner24943d22010-06-08 16:52:24 +00002249 if (exe_module)
2250 {
Greg Clayton180546b2011-04-30 01:09:13 +00002251 char local_exec_file_path[PATH_MAX];
2252 char platform_exec_file_path[PATH_MAX];
2253 exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path));
2254 exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path));
Chris Lattner24943d22010-06-08 16:52:24 +00002255 if (exe_module->GetFileSpec().Exists())
2256 {
Greg Claytona2f74232011-02-24 22:24:29 +00002257 if (PrivateStateThreadIsValid ())
2258 PausePrivateStateThread ();
2259
Chris Lattner24943d22010-06-08 16:52:24 +00002260 error = WillLaunch (exe_module);
2261 if (error.Success())
2262 {
Greg Claytond8c62532010-10-07 04:19:01 +00002263 SetPublicState (eStateLaunching);
Greg Claytonffa43a62011-11-17 04:46:02 +00002264 m_should_detach = false;
Chris Lattner24943d22010-06-08 16:52:24 +00002265
2266 // Now launch using these arguments.
Greg Clayton36bc5ea2011-11-03 21:22:33 +00002267 error = DoLaunch (exe_module, launch_info);
Chris Lattner24943d22010-06-08 16:52:24 +00002268
2269 if (error.Fail())
2270 {
2271 if (GetID() != LLDB_INVALID_PROCESS_ID)
2272 {
2273 SetID (LLDB_INVALID_PROCESS_ID);
2274 const char *error_string = error.AsCString();
2275 if (error_string == NULL)
2276 error_string = "launch failed";
2277 SetExitStatus (-1, error_string);
2278 }
2279 }
2280 else
2281 {
2282 EventSP event_sp;
Greg Clayton49859592011-06-22 01:42:17 +00002283 TimeValue timeout_time;
2284 timeout_time = TimeValue::Now();
2285 timeout_time.OffsetWithSeconds(10);
2286 StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00002287
Greg Clayton49859592011-06-22 01:42:17 +00002288 if (state == eStateInvalid || event_sp.get() == NULL)
2289 {
2290 // We were able to launch the process, but we failed to
2291 // catch the initial stop.
2292 SetExitStatus (0, "failed to catch stop after launch");
2293 Destroy();
2294 }
2295 else if (state == eStateStopped || state == eStateCrashed)
Chris Lattner24943d22010-06-08 16:52:24 +00002296 {
Greg Clayton75c703d2011-02-16 04:46:07 +00002297
Chris Lattner24943d22010-06-08 16:52:24 +00002298 DidLaunch ();
2299
Greg Clayton37f962e2011-08-22 02:49:39 +00002300 m_dyld_ap.reset (DynamicLoader::FindPlugin (this, NULL));
Greg Clayton75c703d2011-02-16 04:46:07 +00002301 if (m_dyld_ap.get())
2302 m_dyld_ap->DidLaunch();
2303
Greg Clayton37f962e2011-08-22 02:49:39 +00002304 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Chris Lattner24943d22010-06-08 16:52:24 +00002305 // This delays passing the stopped event to listeners till DidLaunch gets
2306 // a chance to complete...
2307 HandlePrivateEvent (event_sp);
Greg Claytona2f74232011-02-24 22:24:29 +00002308
2309 if (PrivateStateThreadIsValid ())
2310 ResumePrivateStateThread ();
2311 else
2312 StartPrivateStateThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00002313 }
2314 else if (state == eStateExited)
2315 {
2316 // We exited while trying to launch somehow. Don't call DidLaunch as that's
2317 // not likely to work, and return an invalid pid.
2318 HandlePrivateEvent (event_sp);
2319 }
2320 }
2321 }
2322 }
2323 else
2324 {
Greg Clayton9c236732011-10-26 00:56:27 +00002325 error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path);
Chris Lattner24943d22010-06-08 16:52:24 +00002326 }
2327 }
2328 return error;
2329}
2330
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002331Process::NextEventAction::EventActionResult
2332Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002333{
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002334 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
2335 switch (state)
Greg Claytonc1d37752010-10-18 01:45:30 +00002336 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002337 case eStateRunning:
Greg Claytona2f74232011-02-24 22:24:29 +00002338 case eStateConnected:
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002339 return eEventActionRetry;
2340
2341 case eStateStopped:
2342 case eStateCrashed:
Greg Clayton2d9adb72011-11-12 02:10:56 +00002343 {
2344 // During attach, prior to sending the eStateStopped event,
2345 // lldb_private::Process subclasses must set the process must set
2346 // the new process ID.
2347 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
2348 if (m_exec_count > 0)
2349 {
2350 --m_exec_count;
2351 m_process->Resume();
2352 return eEventActionRetry;
2353 }
2354 else
2355 {
2356 m_process->CompleteAttach ();
2357 return eEventActionSuccess;
2358 }
2359 }
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002360 break;
Greg Clayton2d9adb72011-11-12 02:10:56 +00002361
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002362 default:
2363 case eStateExited:
2364 case eStateInvalid:
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002365 break;
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002366 }
Greg Clayton2d9adb72011-11-12 02:10:56 +00002367
2368 m_exit_string.assign ("No valid Process");
2369 return eEventActionExit;
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002370}
Chris Lattner24943d22010-06-08 16:52:24 +00002371
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002372Process::NextEventAction::EventActionResult
2373Process::AttachCompletionHandler::HandleBeingInterrupted()
2374{
2375 return eEventActionSuccess;
2376}
2377
2378const char *
2379Process::AttachCompletionHandler::GetExitString ()
2380{
2381 return m_exit_string.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +00002382}
2383
2384Error
Greg Clayton527154d2011-11-15 03:53:30 +00002385Process::Attach (ProcessAttachInfo &attach_info)
Chris Lattner24943d22010-06-08 16:52:24 +00002386{
Chris Lattner24943d22010-06-08 16:52:24 +00002387 m_abi_sp.reset();
Caroline Tice861efb32010-11-16 05:07:41 +00002388 m_process_input_reader.reset();
Greg Clayton75c703d2011-02-16 04:46:07 +00002389 m_dyld_ap.reset();
Greg Clayton37f962e2011-08-22 02:49:39 +00002390 m_os_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00002391
Greg Clayton527154d2011-11-15 03:53:30 +00002392 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002393 Error error;
Greg Clayton527154d2011-11-15 03:53:30 +00002394 if (attach_pid == LLDB_INVALID_PROCESS_ID)
Jim Ingham7508e732010-08-09 23:31:02 +00002395 {
Greg Clayton527154d2011-11-15 03:53:30 +00002396 char process_name[PATH_MAX];
Jim Ingham0d7f7772011-09-15 01:10:17 +00002397
Greg Clayton527154d2011-11-15 03:53:30 +00002398 if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name)))
Jim Inghamea294182010-08-17 21:54:19 +00002399 {
Greg Clayton527154d2011-11-15 03:53:30 +00002400 const bool wait_for_launch = attach_info.GetWaitForLaunch();
2401
2402 if (wait_for_launch)
Chris Lattner24943d22010-06-08 16:52:24 +00002403 {
Greg Clayton527154d2011-11-15 03:53:30 +00002404 error = WillAttachToProcessWithName(process_name, wait_for_launch);
2405 if (error.Success())
2406 {
Greg Claytonffa43a62011-11-17 04:46:02 +00002407 m_should_detach = true;
2408
Greg Clayton527154d2011-11-15 03:53:30 +00002409 SetPublicState (eStateAttaching);
2410 error = DoAttachToProcessWithName (process_name, wait_for_launch);
2411 if (error.Fail())
2412 {
2413 if (GetID() != LLDB_INVALID_PROCESS_ID)
2414 {
2415 SetID (LLDB_INVALID_PROCESS_ID);
2416 if (error.AsCString() == NULL)
2417 error.SetErrorString("attach failed");
2418
2419 SetExitStatus(-1, error.AsCString());
2420 }
2421 }
2422 else
2423 {
2424 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
2425 StartPrivateStateThread();
2426 }
2427 return error;
2428 }
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002429 }
Greg Clayton527154d2011-11-15 03:53:30 +00002430 else
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002431 {
Greg Clayton527154d2011-11-15 03:53:30 +00002432 ProcessInstanceInfoList process_infos;
2433 PlatformSP platform_sp (m_target.GetPlatform ());
2434
2435 if (platform_sp)
2436 {
2437 ProcessInstanceInfoMatch match_info;
2438 match_info.GetProcessInfo() = attach_info;
2439 match_info.SetNameMatchType (eNameMatchEquals);
2440 platform_sp->FindProcesses (match_info, process_infos);
2441 const uint32_t num_matches = process_infos.GetSize();
2442 if (num_matches == 1)
2443 {
2444 attach_pid = process_infos.GetProcessIDAtIndex(0);
2445 // Fall through and attach using the above process ID
2446 }
2447 else
2448 {
2449 match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name));
2450 if (num_matches > 1)
2451 error.SetErrorStringWithFormat ("more than one process named %s", process_name);
2452 else
2453 error.SetErrorStringWithFormat ("could not find a process named %s", process_name);
2454 }
2455 }
2456 else
2457 {
2458 error.SetErrorString ("invalid platform, can't find processes by name");
2459 return error;
2460 }
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002461 }
Chris Lattner24943d22010-06-08 16:52:24 +00002462 }
2463 else
Greg Clayton527154d2011-11-15 03:53:30 +00002464 {
2465 error.SetErrorString ("invalid process name");
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002466 }
2467 }
Greg Clayton527154d2011-11-15 03:53:30 +00002468
2469 if (attach_pid != LLDB_INVALID_PROCESS_ID)
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002470 {
Greg Clayton527154d2011-11-15 03:53:30 +00002471 error = WillAttachToProcessWithID(attach_pid);
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002472 if (error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +00002473 {
Greg Claytonffa43a62011-11-17 04:46:02 +00002474 m_should_detach = true;
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002475 SetPublicState (eStateAttaching);
Greg Clayton527154d2011-11-15 03:53:30 +00002476
2477 error = DoAttachToProcessWithID (attach_pid);
2478 if (error.Success())
2479 {
2480
2481 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
2482 StartPrivateStateThread();
2483 }
2484 else
Greg Claytone4b9c1f2011-03-08 22:40:15 +00002485 {
2486 if (GetID() != LLDB_INVALID_PROCESS_ID)
2487 {
2488 SetID (LLDB_INVALID_PROCESS_ID);
2489 const char *error_string = error.AsCString();
2490 if (error_string == NULL)
2491 error_string = "attach failed";
2492
2493 SetExitStatus(-1, error_string);
2494 }
2495 }
Chris Lattner24943d22010-06-08 16:52:24 +00002496 }
2497 }
2498 return error;
2499}
2500
Greg Clayton527154d2011-11-15 03:53:30 +00002501//Error
2502//Process::Attach (const char *process_name, bool wait_for_launch)
2503//{
2504// m_abi_sp.reset();
2505// m_process_input_reader.reset();
2506//
2507// // Find the process and its architecture. Make sure it matches the architecture
2508// // of the current Target, and if not adjust it.
2509// Error error;
2510//
2511// if (!wait_for_launch)
2512// {
2513// ProcessInstanceInfoList process_infos;
2514// PlatformSP platform_sp (m_target.GetPlatform ());
2515// assert (platform_sp.get());
2516//
2517// if (platform_sp)
2518// {
2519// ProcessInstanceInfoMatch match_info;
2520// match_info.GetProcessInfo().SetName(process_name);
2521// match_info.SetNameMatchType (eNameMatchEquals);
2522// platform_sp->FindProcesses (match_info, process_infos);
2523// if (process_infos.GetSize() > 1)
2524// {
2525// error.SetErrorStringWithFormat ("more than one process named %s", process_name);
2526// }
2527// else if (process_infos.GetSize() == 0)
2528// {
2529// error.SetErrorStringWithFormat ("could not find a process named %s", process_name);
2530// }
2531// }
2532// else
2533// {
2534// error.SetErrorString ("invalid platform");
2535// }
2536// }
2537//
2538// if (error.Success())
2539// {
2540// m_dyld_ap.reset();
2541// m_os_ap.reset();
2542//
2543// error = WillAttachToProcessWithName(process_name, wait_for_launch);
2544// if (error.Success())
2545// {
2546// SetPublicState (eStateAttaching);
2547// error = DoAttachToProcessWithName (process_name, wait_for_launch);
2548// if (error.Fail())
2549// {
2550// if (GetID() != LLDB_INVALID_PROCESS_ID)
2551// {
2552// SetID (LLDB_INVALID_PROCESS_ID);
2553// const char *error_string = error.AsCString();
2554// if (error_string == NULL)
2555// error_string = "attach failed";
2556//
2557// SetExitStatus(-1, error_string);
2558// }
2559// }
2560// else
2561// {
2562// SetNextEventAction(new Process::AttachCompletionHandler(this, 0));
2563// StartPrivateStateThread();
2564// }
2565// }
2566// }
2567// return error;
2568//}
2569
Greg Clayton75c703d2011-02-16 04:46:07 +00002570void
2571Process::CompleteAttach ()
2572{
2573 // Let the process subclass figure out at much as it can about the process
2574 // before we go looking for a dynamic loader plug-in.
2575 DidAttach();
2576
Jim Ingham0d7f7772011-09-15 01:10:17 +00002577 // We just attached. If we have a platform, ask it for the process architecture, and if it isn't
2578 // the same as the one we've already set, switch architectures.
2579 PlatformSP platform_sp (m_target.GetPlatform ());
2580 assert (platform_sp.get());
2581 if (platform_sp)
2582 {
2583 ProcessInstanceInfo process_info;
2584 platform_sp->GetProcessInfo (GetID(), process_info);
2585 const ArchSpec &process_arch = process_info.GetArchitecture();
2586 if (process_arch.IsValid() && m_target.GetArchitecture() != process_arch)
2587 m_target.SetArchitecture (process_arch);
2588 }
2589
2590 // We have completed the attach, now it is time to find the dynamic loader
Greg Clayton75c703d2011-02-16 04:46:07 +00002591 // plug-in
Greg Clayton4fdf7602011-03-20 04:57:14 +00002592 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
Greg Clayton75c703d2011-02-16 04:46:07 +00002593 if (m_dyld_ap.get())
2594 m_dyld_ap->DidAttach();
2595
Greg Clayton37f962e2011-08-22 02:49:39 +00002596 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Clayton75c703d2011-02-16 04:46:07 +00002597 // Figure out which one is the executable, and set that in our target:
2598 ModuleList &modules = m_target.GetImages();
2599
2600 size_t num_modules = modules.GetSize();
2601 for (int i = 0; i < num_modules; i++)
2602 {
2603 ModuleSP module_sp (modules.GetModuleAtIndex(i));
Greg Claytonb72d0f02011-04-12 05:54:46 +00002604 if (module_sp && module_sp->IsExecutable())
Greg Clayton75c703d2011-02-16 04:46:07 +00002605 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00002606 if (m_target.GetExecutableModulePointer() != module_sp.get())
Greg Clayton75c703d2011-02-16 04:46:07 +00002607 m_target.SetExecutableModule (module_sp, false);
2608 break;
2609 }
2610 }
2611}
2612
Chris Lattner24943d22010-06-08 16:52:24 +00002613Error
Greg Claytone71e2582011-02-04 01:58:07 +00002614Process::ConnectRemote (const char *remote_url)
2615{
Greg Claytone71e2582011-02-04 01:58:07 +00002616 m_abi_sp.reset();
2617 m_process_input_reader.reset();
2618
2619 // Find the process and its architecture. Make sure it matches the architecture
2620 // of the current Target, and if not adjust it.
2621
2622 Error error (DoConnectRemote (remote_url));
2623 if (error.Success())
2624 {
Greg Claytona2f74232011-02-24 22:24:29 +00002625 if (GetID() != LLDB_INVALID_PROCESS_ID)
2626 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00002627 EventSP event_sp;
2628 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
2629
2630 if (state == eStateStopped || state == eStateCrashed)
2631 {
2632 // If we attached and actually have a process on the other end, then
2633 // this ended up being the equivalent of an attach.
2634 CompleteAttach ();
2635
2636 // This delays passing the stopped event to listeners till
2637 // CompleteAttach gets a chance to complete...
2638 HandlePrivateEvent (event_sp);
2639
2640 }
Greg Claytona2f74232011-02-24 22:24:29 +00002641 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00002642
2643 if (PrivateStateThreadIsValid ())
2644 ResumePrivateStateThread ();
2645 else
2646 StartPrivateStateThread ();
Greg Claytone71e2582011-02-04 01:58:07 +00002647 }
2648 return error;
2649}
2650
2651
2652Error
Chris Lattner24943d22010-06-08 16:52:24 +00002653Process::Resume ()
2654{
Greg Claytone005f2c2010-11-06 01:53:30 +00002655 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00002656 if (log)
Jim Inghamac959662011-01-24 06:34:17 +00002657 log->Printf("Process::Resume() m_stop_id = %u, public state: %s private state: %s",
Jim Ingham21f37ad2011-08-09 02:12:22 +00002658 m_mod_id.GetStopID(),
Jim Inghamac959662011-01-24 06:34:17 +00002659 StateAsCString(m_public_state.GetValue()),
2660 StateAsCString(m_private_state.GetValue()));
Chris Lattner24943d22010-06-08 16:52:24 +00002661
2662 Error error (WillResume());
2663 // Tell the process it is about to resume before the thread list
2664 if (error.Success())
2665 {
Johnny Chen9c11d472010-12-02 20:53:05 +00002666 // Now let the thread list know we are about to resume so it
Chris Lattner24943d22010-06-08 16:52:24 +00002667 // can let all of our threads know that they are about to be
2668 // resumed. Threads will each be called with
2669 // Thread::WillResume(StateType) where StateType contains the state
2670 // that they are supposed to have when the process is resumed
2671 // (suspended/running/stepping). Threads should also check
2672 // their resume signal in lldb::Thread::GetResumeSignal()
2673 // to see if they are suppoed to start back up with a signal.
2674 if (m_thread_list.WillResume())
2675 {
Jim Ingham0296fe72011-11-08 03:00:11 +00002676 m_mod_id.BumpResumeID();
Chris Lattner24943d22010-06-08 16:52:24 +00002677 error = DoResume();
2678 if (error.Success())
2679 {
2680 DidResume();
2681 m_thread_list.DidResume();
Jim Inghamac959662011-01-24 06:34:17 +00002682 if (log)
2683 log->Printf ("Process thinks the process has resumed.");
Chris Lattner24943d22010-06-08 16:52:24 +00002684 }
2685 }
2686 else
2687 {
Jim Inghamac959662011-01-24 06:34:17 +00002688 error.SetErrorStringWithFormat("Process::WillResume() thread list returned false after WillResume");
Chris Lattner24943d22010-06-08 16:52:24 +00002689 }
2690 }
Jim Inghamac959662011-01-24 06:34:17 +00002691 else if (log)
2692 log->Printf ("Process::WillResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner24943d22010-06-08 16:52:24 +00002693 return error;
2694}
2695
2696Error
2697Process::Halt ()
2698{
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002699 // Pause our private state thread so we can ensure no one else eats
2700 // the stop event out from under us.
Jim Inghamf9f40c22011-02-08 05:20:59 +00002701 Listener halt_listener ("lldb.process.halt_listener");
2702 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton20d338f2010-11-18 05:57:03 +00002703
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002704 EventSP event_sp;
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002705 Error error (WillHalt());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002706
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002707 if (error.Success())
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002708 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002709
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002710 bool caused_stop = false;
2711
2712 // Ask the process subclass to actually halt our process
2713 error = DoHalt(caused_stop);
Chris Lattner24943d22010-06-08 16:52:24 +00002714 if (error.Success())
Jim Ingham3ae449a2010-11-17 02:32:00 +00002715 {
Greg Clayton7e2f91c2011-01-29 07:10:55 +00002716 if (m_public_state.GetValue() == eStateAttaching)
2717 {
2718 SetExitStatus(SIGKILL, "Cancelled async attach.");
2719 Destroy ();
2720 }
2721 else
Jim Ingham3ae449a2010-11-17 02:32:00 +00002722 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002723 // If "caused_stop" is true, then DoHalt stopped the process. If
2724 // "caused_stop" is false, the process was already stopped.
2725 // If the DoHalt caused the process to stop, then we want to catch
2726 // this event and set the interrupted bool to true before we pass
2727 // this along so clients know that the process was interrupted by
2728 // a halt command.
2729 if (caused_stop)
Greg Clayton20d338f2010-11-18 05:57:03 +00002730 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00002731 // Wait for 1 second for the process to stop.
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002732 TimeValue timeout_time;
2733 timeout_time = TimeValue::Now();
2734 timeout_time.OffsetWithSeconds(1);
Jim Inghamf9f40c22011-02-08 05:20:59 +00002735 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
2736 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002737
Jim Inghamf9f40c22011-02-08 05:20:59 +00002738 if (!got_event || state == eStateInvalid)
Greg Clayton20d338f2010-11-18 05:57:03 +00002739 {
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002740 // We timeout out and didn't get a stop event...
Jim Inghamf9f40c22011-02-08 05:20:59 +00002741 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton20d338f2010-11-18 05:57:03 +00002742 }
2743 else
2744 {
Greg Clayton20206082011-11-17 01:23:07 +00002745 if (StateIsStoppedState (state, false))
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002746 {
2747 // We caused the process to interrupt itself, so mark this
2748 // as such in the stop event so clients can tell an interrupted
2749 // process from a natural stop
2750 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
2751 }
2752 else
2753 {
2754 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2755 if (log)
2756 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
2757 error.SetErrorString ("Did not get stopped event after halt.");
2758 }
Greg Clayton20d338f2010-11-18 05:57:03 +00002759 }
2760 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002761 DidHalt();
Jim Ingham3ae449a2010-11-17 02:32:00 +00002762 }
2763 }
Chris Lattner24943d22010-06-08 16:52:24 +00002764 }
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002765 // Resume our private state thread before we post the event (if any)
Jim Inghamf9f40c22011-02-08 05:20:59 +00002766 RestorePrivateProcessEvents();
Jim Inghamc2dc7c82011-01-29 01:49:25 +00002767
2768 // Post any event we might have consumed. If all goes well, we will have
2769 // stopped the process, intercepted the event and set the interrupted
2770 // bool in the event. Post it to the private event queue and that will end up
2771 // correctly setting the state.
2772 if (event_sp)
2773 m_private_state_broadcaster.BroadcastEvent(event_sp);
2774
Chris Lattner24943d22010-06-08 16:52:24 +00002775 return error;
2776}
2777
2778Error
2779Process::Detach ()
2780{
2781 Error error (WillDetach());
2782
2783 if (error.Success())
2784 {
2785 DisableAllBreakpointSites();
2786 error = DoDetach();
2787 if (error.Success())
2788 {
2789 DidDetach();
2790 StopPrivateStateThread();
2791 }
2792 }
2793 return error;
2794}
2795
2796Error
2797Process::Destroy ()
2798{
2799 Error error (WillDestroy());
2800 if (error.Success())
2801 {
2802 DisableAllBreakpointSites();
2803 error = DoDestroy();
2804 if (error.Success())
2805 {
2806 DidDestroy();
2807 StopPrivateStateThread();
2808 }
Caroline Tice861efb32010-11-16 05:07:41 +00002809 m_stdio_communication.StopReadThread();
2810 m_stdio_communication.Disconnect();
2811 if (m_process_input_reader && m_process_input_reader->IsActive())
2812 m_target.GetDebugger().PopInputReader (m_process_input_reader);
2813 if (m_process_input_reader)
2814 m_process_input_reader.reset();
Chris Lattner24943d22010-06-08 16:52:24 +00002815 }
2816 return error;
2817}
2818
2819Error
2820Process::Signal (int signal)
2821{
2822 Error error (WillSignal());
2823 if (error.Success())
2824 {
2825 error = DoSignal(signal);
2826 if (error.Success())
2827 DidSignal();
2828 }
2829 return error;
2830}
2831
Greg Clayton395fc332011-02-15 21:59:32 +00002832lldb::ByteOrder
2833Process::GetByteOrder () const
Chris Lattner24943d22010-06-08 16:52:24 +00002834{
Greg Clayton395fc332011-02-15 21:59:32 +00002835 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner24943d22010-06-08 16:52:24 +00002836}
2837
2838uint32_t
Greg Clayton395fc332011-02-15 21:59:32 +00002839Process::GetAddressByteSize () const
Chris Lattner24943d22010-06-08 16:52:24 +00002840{
Greg Clayton395fc332011-02-15 21:59:32 +00002841 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +00002842}
2843
Greg Clayton395fc332011-02-15 21:59:32 +00002844
Chris Lattner24943d22010-06-08 16:52:24 +00002845bool
2846Process::ShouldBroadcastEvent (Event *event_ptr)
2847{
2848 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
2849 bool return_value = true;
Greg Claytone005f2c2010-11-06 01:53:30 +00002850 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002851
2852 switch (state)
2853 {
Greg Claytone71e2582011-02-04 01:58:07 +00002854 case eStateConnected:
Chris Lattner24943d22010-06-08 16:52:24 +00002855 case eStateAttaching:
2856 case eStateLaunching:
2857 case eStateDetached:
2858 case eStateExited:
2859 case eStateUnloaded:
2860 // These events indicate changes in the state of the debugging session, always report them.
2861 return_value = true;
2862 break;
2863 case eStateInvalid:
2864 // We stopped for no apparent reason, don't report it.
2865 return_value = false;
2866 break;
2867 case eStateRunning:
2868 case eStateStepping:
2869 // If we've started the target running, we handle the cases where we
2870 // are already running and where there is a transition from stopped to
2871 // running differently.
2872 // running -> running: Automatically suppress extra running events
2873 // stopped -> running: Report except when there is one or more no votes
2874 // and no yes votes.
2875 SynchronouslyNotifyStateChanged (state);
2876 switch (m_public_state.GetValue())
2877 {
2878 case eStateRunning:
2879 case eStateStepping:
2880 // We always suppress multiple runnings with no PUBLIC stop in between.
2881 return_value = false;
2882 break;
2883 default:
2884 // TODO: make this work correctly. For now always report
2885 // run if we aren't running so we don't miss any runnning
2886 // events. If I run the lldb/test/thread/a.out file and
2887 // break at main.cpp:58, run and hit the breakpoints on
2888 // multiple threads, then somehow during the stepping over
2889 // of all breakpoints no run gets reported.
2890 return_value = true;
2891
2892 // This is a transition from stop to run.
2893 switch (m_thread_list.ShouldReportRun (event_ptr))
2894 {
2895 case eVoteYes:
2896 case eVoteNoOpinion:
2897 return_value = true;
2898 break;
2899 case eVoteNo:
2900 return_value = false;
2901 break;
2902 }
2903 break;
2904 }
2905 break;
2906 case eStateStopped:
2907 case eStateCrashed:
2908 case eStateSuspended:
2909 {
2910 // We've stopped. First see if we're going to restart the target.
2911 // If we are going to stop, then we always broadcast the event.
2912 // If we aren't going to stop, let the thread plans decide if we're going to report this event.
Jim Ingham5a47e8b2010-06-19 04:45:32 +00002913 // If no thread has an opinion, we don't report it.
Jim Ingham3ae449a2010-11-17 02:32:00 +00002914 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner24943d22010-06-08 16:52:24 +00002915 {
Greg Clayton20d338f2010-11-18 05:57:03 +00002916 if (log)
2917 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", event_ptr, StateAsCString(state));
Jim Ingham3ae449a2010-11-17 02:32:00 +00002918 return true;
2919 }
2920 else
2921 {
Chris Lattner24943d22010-06-08 16:52:24 +00002922 RefreshStateAfterStop ();
2923
2924 if (m_thread_list.ShouldStop (event_ptr) == false)
2925 {
2926 switch (m_thread_list.ShouldReportStop (event_ptr))
2927 {
2928 case eVoteYes:
2929 Process::ProcessEventData::SetRestartedInEvent (event_ptr, true);
Johnny Chen028784b2010-10-14 00:54:32 +00002930 // Intentional fall-through here.
Chris Lattner24943d22010-06-08 16:52:24 +00002931 case eVoteNoOpinion:
Chris Lattner24943d22010-06-08 16:52:24 +00002932 case eVoteNo:
2933 return_value = false;
2934 break;
2935 }
2936
2937 if (log)
Jim Ingham3ae449a2010-11-17 02:32:00 +00002938 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state));
Chris Lattner24943d22010-06-08 16:52:24 +00002939 Resume ();
2940 }
2941 else
2942 {
2943 return_value = true;
2944 SynchronouslyNotifyStateChanged (state);
2945 }
2946 }
2947 }
2948 }
2949
2950 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00002951 log->Printf ("Process::ShouldBroadcastEvent (%p) => %s - %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO");
Chris Lattner24943d22010-06-08 16:52:24 +00002952 return return_value;
2953}
2954
Chris Lattner24943d22010-06-08 16:52:24 +00002955
2956bool
2957Process::StartPrivateStateThread ()
2958{
Greg Claytone005f2c2010-11-06 01:53:30 +00002959 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002960
Greg Claytonb72d0f02011-04-12 05:54:46 +00002961 bool already_running = PrivateStateThreadIsValid ();
Chris Lattner24943d22010-06-08 16:52:24 +00002962 if (log)
Greg Claytonb72d0f02011-04-12 05:54:46 +00002963 log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread");
2964
2965 if (already_running)
2966 return true;
Chris Lattner24943d22010-06-08 16:52:24 +00002967
2968 // Create a thread that watches our internal state and controls which
2969 // events make it to clients (into the DCProcess event queue).
Greg Claytona875b642011-01-09 21:07:35 +00002970 char thread_name[1024];
Greg Clayton444e35b2011-10-19 18:09:39 +00002971 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%llu)>", GetID());
Greg Claytona875b642011-01-09 21:07:35 +00002972 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Greg Clayton09c81ef2011-02-08 01:34:25 +00002973 return IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
Chris Lattner24943d22010-06-08 16:52:24 +00002974}
2975
2976void
2977Process::PausePrivateStateThread ()
2978{
2979 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
2980}
2981
2982void
2983Process::ResumePrivateStateThread ()
2984{
2985 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
2986}
2987
2988void
2989Process::StopPrivateStateThread ()
2990{
Greg Claytonb72d0f02011-04-12 05:54:46 +00002991 if (PrivateStateThreadIsValid ())
2992 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
Chris Lattner24943d22010-06-08 16:52:24 +00002993}
2994
2995void
2996Process::ControlPrivateStateThread (uint32_t signal)
2997{
Greg Claytone005f2c2010-11-06 01:53:30 +00002998 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner24943d22010-06-08 16:52:24 +00002999
3000 assert (signal == eBroadcastInternalStateControlStop ||
3001 signal == eBroadcastInternalStateControlPause ||
3002 signal == eBroadcastInternalStateControlResume);
3003
3004 if (log)
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00003005 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner24943d22010-06-08 16:52:24 +00003006
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00003007 // Signal the private state thread. First we should copy this is case the
3008 // thread starts exiting since the private state thread will NULL this out
3009 // when it exits
3010 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton09c81ef2011-02-08 01:34:25 +00003011 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner24943d22010-06-08 16:52:24 +00003012 {
3013 TimeValue timeout_time;
3014 bool timed_out;
3015
3016 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
3017
3018 timeout_time = TimeValue::Now();
3019 timeout_time.OffsetWithSeconds(2);
3020 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
3021 m_private_state_control_wait.SetValue (false, eBroadcastNever);
3022
3023 if (signal == eBroadcastInternalStateControlStop)
3024 {
3025 if (timed_out)
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00003026 Host::ThreadCancel (private_state_thread, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +00003027
3028 thread_result_t result = NULL;
Greg Claytonf4fbc0b2011-01-22 17:43:17 +00003029 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Claytonc607d862010-07-22 18:34:21 +00003030 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00003031 }
3032 }
3033}
3034
3035void
3036Process::HandlePrivateEvent (EventSP &event_sp)
3037{
Greg Claytone005f2c2010-11-06 01:53:30 +00003038 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghamc2dc7c82011-01-29 01:49:25 +00003039
Greg Clayton68ca8232011-01-25 02:58:48 +00003040 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghamc2dc7c82011-01-29 01:49:25 +00003041
3042 // First check to see if anybody wants a shot at this event:
Jim Ingham68bffc52011-01-29 04:05:41 +00003043 if (m_next_event_action_ap.get() != NULL)
Jim Inghamc2dc7c82011-01-29 01:49:25 +00003044 {
Jim Ingham68bffc52011-01-29 04:05:41 +00003045 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Inghamc2dc7c82011-01-29 01:49:25 +00003046 switch (action_result)
3047 {
3048 case NextEventAction::eEventActionSuccess:
3049 SetNextEventAction(NULL);
3050 break;
Greg Clayton2d9adb72011-11-12 02:10:56 +00003051
Jim Inghamc2dc7c82011-01-29 01:49:25 +00003052 case NextEventAction::eEventActionRetry:
3053 break;
Greg Clayton2d9adb72011-11-12 02:10:56 +00003054
Jim Inghamc2dc7c82011-01-29 01:49:25 +00003055 case NextEventAction::eEventActionExit:
Jim Ingham84c86382011-01-29 01:57:31 +00003056 // Handle Exiting Here. If we already got an exited event,
3057 // we should just propagate it. Otherwise, swallow this event,
3058 // and set our state to exit so the next event will kill us.
3059 if (new_state != eStateExited)
3060 {
3061 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham68bffc52011-01-29 04:05:41 +00003062 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham84c86382011-01-29 01:57:31 +00003063 SetNextEventAction(NULL);
3064 return;
3065 }
3066 SetNextEventAction(NULL);
Jim Inghamc2dc7c82011-01-29 01:49:25 +00003067 break;
3068 }
3069 }
3070
Chris Lattner24943d22010-06-08 16:52:24 +00003071 // See if we should broadcast this state to external clients?
3072 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +00003073
3074 if (should_broadcast)
3075 {
3076 if (log)
3077 {
Greg Clayton444e35b2011-10-19 18:09:39 +00003078 log->Printf ("Process::%s (pid = %llu) broadcasting new state %s (old state %s) to %s",
Greg Clayton68ca8232011-01-25 02:58:48 +00003079 __FUNCTION__,
3080 GetID(),
3081 StateAsCString(new_state),
3082 StateAsCString (GetState ()),
3083 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner24943d22010-06-08 16:52:24 +00003084 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00003085 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton68ca8232011-01-25 02:58:48 +00003086 if (StateIsRunningState (new_state))
Caroline Tice861efb32010-11-16 05:07:41 +00003087 PushProcessInputReader ();
3088 else
3089 PopProcessInputReader ();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003090
Chris Lattner24943d22010-06-08 16:52:24 +00003091 BroadcastEvent (event_sp);
3092 }
3093 else
3094 {
3095 if (log)
3096 {
Greg Clayton444e35b2011-10-19 18:09:39 +00003097 log->Printf ("Process::%s (pid = %llu) suppressing state %s (old state %s): should_broadcast == false",
Greg Clayton68ca8232011-01-25 02:58:48 +00003098 __FUNCTION__,
3099 GetID(),
3100 StateAsCString(new_state),
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00003101 StateAsCString (GetState ()));
Chris Lattner24943d22010-06-08 16:52:24 +00003102 }
3103 }
3104}
3105
3106void *
3107Process::PrivateStateThread (void *arg)
3108{
3109 Process *proc = static_cast<Process*> (arg);
3110 void *result = proc->RunPrivateStateThread ();
Chris Lattner24943d22010-06-08 16:52:24 +00003111 return result;
3112}
3113
3114void *
3115Process::RunPrivateStateThread ()
3116{
3117 bool control_only = false;
3118 m_private_state_control_wait.SetValue (false, eBroadcastNever);
3119
Greg Claytone005f2c2010-11-06 01:53:30 +00003120 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner24943d22010-06-08 16:52:24 +00003121 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00003122 log->Printf ("Process::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00003123
3124 bool exit_now = false;
3125 while (!exit_now)
3126 {
3127 EventSP event_sp;
3128 WaitForEventsPrivate (NULL, event_sp, control_only);
3129 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
3130 {
3131 switch (event_sp->GetType())
3132 {
3133 case eBroadcastInternalStateControlStop:
3134 exit_now = true;
3135 continue; // Go to next loop iteration so we exit without
3136 break; // doing any internal state managment below
3137
3138 case eBroadcastInternalStateControlPause:
3139 control_only = true;
3140 break;
3141
3142 case eBroadcastInternalStateControlResume:
3143 control_only = false;
3144 break;
3145 }
Jim Ingham3ae449a2010-11-17 02:32:00 +00003146
Jim Ingham3ae449a2010-11-17 02:32:00 +00003147 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00003148 log->Printf ("Process::%s (arg = %p, pid = %llu) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType());
Jim Ingham3ae449a2010-11-17 02:32:00 +00003149
Chris Lattner24943d22010-06-08 16:52:24 +00003150 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham3ae449a2010-11-17 02:32:00 +00003151 continue;
Chris Lattner24943d22010-06-08 16:52:24 +00003152 }
3153
3154
3155 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3156
3157 if (internal_state != eStateInvalid)
3158 {
3159 HandlePrivateEvent (event_sp);
3160 }
3161
Greg Clayton3b2c41c2010-10-18 04:14:23 +00003162 if (internal_state == eStateInvalid ||
3163 internal_state == eStateExited ||
3164 internal_state == eStateDetached )
Jim Ingham3ae449a2010-11-17 02:32:00 +00003165 {
Jim Ingham3ae449a2010-11-17 02:32:00 +00003166 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00003167 log->Printf ("Process::%s (arg = %p, pid = %llu) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state));
Jim Ingham3ae449a2010-11-17 02:32:00 +00003168
Chris Lattner24943d22010-06-08 16:52:24 +00003169 break;
Jim Ingham3ae449a2010-11-17 02:32:00 +00003170 }
Chris Lattner24943d22010-06-08 16:52:24 +00003171 }
3172
Caroline Tice926060e2010-10-29 21:48:37 +00003173 // Verify log is still enabled before attempting to write to it...
Chris Lattner24943d22010-06-08 16:52:24 +00003174 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +00003175 log->Printf ("Process::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00003176
Greg Claytona4881d02011-01-22 07:12:45 +00003177 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
3178 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner24943d22010-06-08 16:52:24 +00003179 return NULL;
3180}
3181
Chris Lattner24943d22010-06-08 16:52:24 +00003182//------------------------------------------------------------------
3183// Process Event Data
3184//------------------------------------------------------------------
3185
3186Process::ProcessEventData::ProcessEventData () :
3187 EventData (),
3188 m_process_sp (),
3189 m_state (eStateInvalid),
Greg Clayton54e7afa2010-07-09 20:39:50 +00003190 m_restarted (false),
Jim Ingham6cf4d2b2011-05-22 21:45:01 +00003191 m_update_state (0),
Jim Ingham3ae449a2010-11-17 02:32:00 +00003192 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00003193{
3194}
3195
3196Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
3197 EventData (),
3198 m_process_sp (process_sp),
3199 m_state (state),
Greg Clayton54e7afa2010-07-09 20:39:50 +00003200 m_restarted (false),
Jim Ingham6cf4d2b2011-05-22 21:45:01 +00003201 m_update_state (0),
Jim Ingham3ae449a2010-11-17 02:32:00 +00003202 m_interrupted (false)
Chris Lattner24943d22010-06-08 16:52:24 +00003203{
3204}
3205
3206Process::ProcessEventData::~ProcessEventData()
3207{
3208}
3209
3210const ConstString &
3211Process::ProcessEventData::GetFlavorString ()
3212{
3213 static ConstString g_flavor ("Process::ProcessEventData");
3214 return g_flavor;
3215}
3216
3217const ConstString &
3218Process::ProcessEventData::GetFlavor () const
3219{
3220 return ProcessEventData::GetFlavorString ();
3221}
3222
Chris Lattner24943d22010-06-08 16:52:24 +00003223void
3224Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
3225{
3226 // This function gets called twice for each event, once when the event gets pulled
Jim Ingham6cf4d2b2011-05-22 21:45:01 +00003227 // off of the private process event queue, and then any number of times, first when it gets pulled off of
3228 // the public event queue, then other times when we're pretending that this is where we stopped at the
3229 // end of expression evaluation. m_update_state is used to distinguish these
3230 // three cases; it is 0 when we're just pulling it off for private handling,
3231 // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then.
Chris Lattner24943d22010-06-08 16:52:24 +00003232
Jim Ingham6cf4d2b2011-05-22 21:45:01 +00003233 if (m_update_state != 1)
Chris Lattner24943d22010-06-08 16:52:24 +00003234 return;
3235
3236 m_process_sp->SetPublicState (m_state);
3237
3238 // If we're stopped and haven't restarted, then do the breakpoint commands here:
3239 if (m_state == eStateStopped && ! m_restarted)
Jim Ingham0296fe72011-11-08 03:00:11 +00003240 {
3241 ThreadList &curr_thread_list = m_process_sp->GetThreadList();
Greg Claytond9919d32011-12-01 23:28:38 +00003242 uint32_t num_threads = curr_thread_list.GetSize();
3243 uint32_t idx;
Greg Clayton643ee732010-08-04 01:40:35 +00003244
Jim Ingham21f37ad2011-08-09 02:12:22 +00003245 // The actions might change one of the thread's stop_info's opinions about whether we should
3246 // stop the process, so we need to query that as we go.
Jim Ingham0296fe72011-11-08 03:00:11 +00003247
3248 // One other complication here, is that we try to catch any case where the target has run (except for expressions)
3249 // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and
3250 // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like
3251 // to also know if it has changed at all, so we make up a vector of the thread ID's and check what we get back
3252 // against this list & bag out if anything differs.
Greg Claytond9919d32011-12-01 23:28:38 +00003253 std::vector<uint32_t> thread_index_array(num_threads);
Jim Ingham0296fe72011-11-08 03:00:11 +00003254 for (idx = 0; idx < num_threads; ++idx)
3255 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID();
3256
Jim Ingham21f37ad2011-08-09 02:12:22 +00003257 bool still_should_stop = true;
3258
Chris Lattner24943d22010-06-08 16:52:24 +00003259 for (idx = 0; idx < num_threads; ++idx)
3260 {
Jim Ingham0296fe72011-11-08 03:00:11 +00003261 curr_thread_list = m_process_sp->GetThreadList();
3262 if (curr_thread_list.GetSize() != num_threads)
3263 {
3264 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham60526c42011-12-01 20:26:15 +00003265 if (log)
Greg Claytond9919d32011-12-01 23:28:38 +00003266 log->Printf("Number of threads changed from %u to %u while processing event.", num_threads, curr_thread_list.GetSize());
Jim Ingham0296fe72011-11-08 03:00:11 +00003267 break;
3268 }
3269
3270 lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx);
3271
3272 if (thread_sp->GetIndexID() != thread_index_array[idx])
3273 {
3274 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham60526c42011-12-01 20:26:15 +00003275 if (log)
Greg Claytond9919d32011-12-01 23:28:38 +00003276 log->Printf("The thread at position %u changed from %u to %u while processing event.",
Jim Ingham60526c42011-12-01 20:26:15 +00003277 idx,
3278 thread_index_array[idx],
3279 thread_sp->GetIndexID());
Jim Ingham0296fe72011-11-08 03:00:11 +00003280 break;
3281 }
3282
Jim Ingham6297a3a2010-10-20 00:39:53 +00003283 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
3284 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003285 {
Jim Ingham6297a3a2010-10-20 00:39:53 +00003286 stop_info_sp->PerformAction(event_ptr);
Jim Ingham21f37ad2011-08-09 02:12:22 +00003287 // The stop action might restart the target. If it does, then we want to mark that in the
3288 // event so that whoever is receiving it will know to wait for the running event and reflect
3289 // that state appropriately.
3290 // We also need to stop processing actions, since they aren't expecting the target to be running.
Jim Ingham0296fe72011-11-08 03:00:11 +00003291
3292 // FIXME: we might have run.
3293 if (stop_info_sp->HasTargetRunSinceMe())
Jim Ingham21f37ad2011-08-09 02:12:22 +00003294 {
3295 SetRestarted (true);
3296 break;
3297 }
3298 else if (!stop_info_sp->ShouldStop(event_ptr))
3299 {
3300 still_should_stop = false;
3301 }
Chris Lattner24943d22010-06-08 16:52:24 +00003302 }
3303 }
Jim Ingham6fb8baa2010-08-10 00:59:59 +00003304
Jim Ingham21f37ad2011-08-09 02:12:22 +00003305
3306 if (m_process_sp->GetPrivateState() != eStateRunning)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003307 {
Jim Ingham21f37ad2011-08-09 02:12:22 +00003308 if (!still_should_stop)
3309 {
3310 // We've been asked to continue, so do that here.
Jim Inghamd60d94a2011-03-11 03:53:59 +00003311 SetRestarted(true);
Jim Ingham21f37ad2011-08-09 02:12:22 +00003312 m_process_sp->Resume();
3313 }
3314 else
3315 {
3316 // If we didn't restart, run the Stop Hooks here:
3317 // They might also restart the target, so watch for that.
3318 m_process_sp->GetTarget().RunStopHooks();
3319 if (m_process_sp->GetPrivateState() == eStateRunning)
3320 SetRestarted(true);
3321 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00003322 }
3323
Chris Lattner24943d22010-06-08 16:52:24 +00003324 }
3325}
3326
3327void
3328Process::ProcessEventData::Dump (Stream *s) const
3329{
3330 if (m_process_sp)
Greg Clayton444e35b2011-10-19 18:09:39 +00003331 s->Printf(" process = %p (pid = %llu), ", m_process_sp.get(), m_process_sp->GetID());
Chris Lattner24943d22010-06-08 16:52:24 +00003332
Greg Claytonb72d0f02011-04-12 05:54:46 +00003333 s->Printf("state = %s", StateAsCString(GetState()));
Chris Lattner24943d22010-06-08 16:52:24 +00003334}
3335
3336const Process::ProcessEventData *
3337Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
3338{
3339 if (event_ptr)
3340 {
3341 const EventData *event_data = event_ptr->GetData();
3342 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
3343 return static_cast <const ProcessEventData *> (event_ptr->GetData());
3344 }
3345 return NULL;
3346}
3347
3348ProcessSP
3349Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
3350{
3351 ProcessSP process_sp;
3352 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
3353 if (data)
3354 process_sp = data->GetProcessSP();
3355 return process_sp;
3356}
3357
3358StateType
3359Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
3360{
3361 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
3362 if (data == NULL)
3363 return eStateInvalid;
3364 else
3365 return data->GetState();
3366}
3367
3368bool
3369Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
3370{
3371 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
3372 if (data == NULL)
3373 return false;
3374 else
3375 return data->GetRestarted();
3376}
3377
3378void
3379Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
3380{
3381 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
3382 if (data != NULL)
3383 data->SetRestarted(new_value);
3384}
3385
3386bool
Jim Ingham3ae449a2010-11-17 02:32:00 +00003387Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
3388{
3389 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
3390 if (data == NULL)
3391 return false;
3392 else
3393 return data->GetInterrupted ();
3394}
3395
3396void
3397Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
3398{
3399 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
3400 if (data != NULL)
3401 data->SetInterrupted(new_value);
3402}
3403
3404bool
Chris Lattner24943d22010-06-08 16:52:24 +00003405Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
3406{
3407 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
3408 if (data)
3409 {
3410 data->SetUpdateStateOnRemoval();
3411 return true;
3412 }
3413 return false;
3414}
3415
Chris Lattner24943d22010-06-08 16:52:24 +00003416void
Greg Claytona830adb2010-10-04 01:05:56 +00003417Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00003418{
Greg Clayton567e7f32011-09-22 04:58:26 +00003419 exe_ctx.SetTargetPtr (&m_target);
3420 exe_ctx.SetProcessPtr (this);
3421 exe_ctx.SetThreadPtr(NULL);
3422 exe_ctx.SetFramePtr (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +00003423}
3424
Greg Claytone4b9c1f2011-03-08 22:40:15 +00003425//uint32_t
3426//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
3427//{
3428// return 0;
3429//}
3430//
3431//ArchSpec
3432//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
3433//{
3434// return Host::GetArchSpecForExistingProcess (pid);
3435//}
3436//
3437//ArchSpec
3438//Process::GetArchSpecForExistingProcess (const char *process_name)
3439//{
3440// return Host::GetArchSpecForExistingProcess (process_name);
3441//}
3442//
Caroline Tice861efb32010-11-16 05:07:41 +00003443void
3444Process::AppendSTDOUT (const char * s, size_t len)
3445{
Greg Clayton20d338f2010-11-18 05:57:03 +00003446 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Tice861efb32010-11-16 05:07:41 +00003447 m_stdout_data.append (s, len);
Greg Claytonb3781332010-12-05 19:16:56 +00003448 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (GetTarget().GetProcessSP(), GetState()));
Caroline Tice861efb32010-11-16 05:07:41 +00003449}
3450
3451void
Greg Claytonbd06ff42011-11-13 04:45:22 +00003452Process::AppendSTDERR (const char * s, size_t len)
3453{
3454 Mutex::Locker locker (m_stdio_communication_mutex);
3455 m_stderr_data.append (s, len);
3456 BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (GetTarget().GetProcessSP(), GetState()));
3457}
3458
3459//------------------------------------------------------------------
3460// Process STDIO
3461//------------------------------------------------------------------
3462
3463size_t
3464Process::GetSTDOUT (char *buf, size_t buf_size, Error &error)
3465{
3466 Mutex::Locker locker(m_stdio_communication_mutex);
3467 size_t bytes_available = m_stdout_data.size();
3468 if (bytes_available > 0)
3469 {
3470 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3471 if (log)
3472 log->Printf ("Process::GetSTDOUT (buf = %p, size = %zu)", buf, buf_size);
3473 if (bytes_available > buf_size)
3474 {
3475 memcpy(buf, m_stdout_data.c_str(), buf_size);
3476 m_stdout_data.erase(0, buf_size);
3477 bytes_available = buf_size;
3478 }
3479 else
3480 {
3481 memcpy(buf, m_stdout_data.c_str(), bytes_available);
3482 m_stdout_data.clear();
3483 }
3484 }
3485 return bytes_available;
3486}
3487
3488
3489size_t
3490Process::GetSTDERR (char *buf, size_t buf_size, Error &error)
3491{
3492 Mutex::Locker locker(m_stdio_communication_mutex);
3493 size_t bytes_available = m_stderr_data.size();
3494 if (bytes_available > 0)
3495 {
3496 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3497 if (log)
3498 log->Printf ("Process::GetSTDERR (buf = %p, size = %zu)", buf, buf_size);
3499 if (bytes_available > buf_size)
3500 {
3501 memcpy(buf, m_stderr_data.c_str(), buf_size);
3502 m_stderr_data.erase(0, buf_size);
3503 bytes_available = buf_size;
3504 }
3505 else
3506 {
3507 memcpy(buf, m_stderr_data.c_str(), bytes_available);
3508 m_stderr_data.clear();
3509 }
3510 }
3511 return bytes_available;
3512}
3513
3514void
Caroline Tice861efb32010-11-16 05:07:41 +00003515Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
3516{
3517 Process *process = (Process *) baton;
3518 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
3519}
3520
3521size_t
3522Process::ProcessInputReaderCallback (void *baton,
3523 InputReader &reader,
3524 lldb::InputReaderAction notification,
3525 const char *bytes,
3526 size_t bytes_len)
3527{
3528 Process *process = (Process *) baton;
3529
3530 switch (notification)
3531 {
3532 case eInputReaderActivate:
3533 break;
3534
3535 case eInputReaderDeactivate:
3536 break;
3537
3538 case eInputReaderReactivate:
3539 break;
3540
Caroline Tice4a348082011-05-02 20:41:46 +00003541 case eInputReaderAsynchronousOutputWritten:
3542 break;
3543
Caroline Tice861efb32010-11-16 05:07:41 +00003544 case eInputReaderGotToken:
3545 {
3546 Error error;
3547 process->PutSTDIN (bytes, bytes_len, error);
3548 }
3549 break;
3550
Caroline Ticec4f55fe2010-11-19 20:47:54 +00003551 case eInputReaderInterrupt:
3552 process->Halt ();
3553 break;
3554
3555 case eInputReaderEndOfFile:
3556 process->AppendSTDOUT ("^D", 2);
3557 break;
3558
Caroline Tice861efb32010-11-16 05:07:41 +00003559 case eInputReaderDone:
3560 break;
3561
3562 }
3563
3564 return bytes_len;
3565}
3566
3567void
3568Process::ResetProcessInputReader ()
3569{
3570 m_process_input_reader.reset();
3571}
3572
3573void
Greg Clayton464c6162011-11-17 22:14:31 +00003574Process::SetSTDIOFileDescriptor (int file_descriptor)
Caroline Tice861efb32010-11-16 05:07:41 +00003575{
3576 // First set up the Read Thread for reading/handling process I/O
3577
3578 std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true));
3579
3580 if (conn_ap.get())
3581 {
3582 m_stdio_communication.SetConnection (conn_ap.release());
3583 if (m_stdio_communication.IsConnected())
3584 {
3585 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
3586 m_stdio_communication.StartReadThread();
3587
3588 // Now read thread is set up, set up input reader.
3589
3590 if (!m_process_input_reader.get())
3591 {
3592 m_process_input_reader.reset (new InputReader(m_target.GetDebugger()));
3593 Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback,
3594 this,
3595 eInputReaderGranularityByte,
3596 NULL,
3597 NULL,
3598 false));
3599
3600 if (err.Fail())
3601 m_process_input_reader.reset();
3602 }
3603 }
3604 }
3605}
3606
3607void
3608Process::PushProcessInputReader ()
3609{
3610 if (m_process_input_reader && !m_process_input_reader->IsActive())
3611 m_target.GetDebugger().PushInputReader (m_process_input_reader);
3612}
3613
3614void
3615Process::PopProcessInputReader ()
3616{
3617 if (m_process_input_reader && m_process_input_reader->IsActive())
3618 m_target.GetDebugger().PopInputReader (m_process_input_reader);
3619}
3620
Greg Claytond284b662011-02-18 01:44:25 +00003621// The process needs to know about installed plug-ins
Greg Clayton990de7b2010-11-18 23:32:35 +00003622void
Caroline Tice2a456812011-03-10 22:14:10 +00003623Process::SettingsInitialize ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003624{
Greg Claytonb3448432011-03-24 21:19:54 +00003625 static std::vector<OptionEnumValueElement> g_plugins;
Greg Claytond284b662011-02-18 01:44:25 +00003626
3627 int i=0;
3628 const char *name;
3629 OptionEnumValueElement option_enum;
3630 while ((name = PluginManager::GetProcessPluginNameAtIndex (i)) != NULL)
3631 {
3632 if (name)
3633 {
3634 option_enum.value = i;
3635 option_enum.string_value = name;
3636 option_enum.usage = PluginManager::GetProcessPluginDescriptionAtIndex (i);
3637 g_plugins.push_back (option_enum);
3638 }
3639 ++i;
3640 }
3641 option_enum.value = 0;
3642 option_enum.string_value = NULL;
3643 option_enum.usage = NULL;
3644 g_plugins.push_back (option_enum);
3645
3646 for (i=0; (name = SettingsController::instance_settings_table[i].var_name); ++i)
3647 {
3648 if (::strcmp (name, "plugin") == 0)
3649 {
3650 SettingsController::instance_settings_table[i].enum_values = &g_plugins[0];
3651 break;
3652 }
3653 }
Greg Clayton990de7b2010-11-18 23:32:35 +00003654 UserSettingsControllerSP &usc = GetSettingsController();
3655 usc.reset (new SettingsController);
3656 UserSettingsController::InitializeSettingsController (usc,
3657 SettingsController::global_settings_table,
3658 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00003659
3660 // Now call SettingsInitialize() for each 'child' of Process settings
3661 Thread::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00003662}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003663
Greg Clayton990de7b2010-11-18 23:32:35 +00003664void
Caroline Tice2a456812011-03-10 22:14:10 +00003665Process::SettingsTerminate ()
Greg Claytond284b662011-02-18 01:44:25 +00003666{
Caroline Tice2a456812011-03-10 22:14:10 +00003667 // Must call SettingsTerminate() on each 'child' of Process settings before terminating Process settings.
3668
3669 Thread::SettingsTerminate ();
3670
3671 // Now terminate Process Settings.
3672
Greg Clayton990de7b2010-11-18 23:32:35 +00003673 UserSettingsControllerSP &usc = GetSettingsController();
3674 UserSettingsController::FinalizeSettingsController (usc);
3675 usc.reset();
3676}
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003677
Greg Clayton990de7b2010-11-18 23:32:35 +00003678UserSettingsControllerSP &
3679Process::GetSettingsController ()
3680{
Greg Clayton334d33a2012-01-30 07:41:31 +00003681 static UserSettingsControllerSP g_settings_controller_sp;
3682 if (!g_settings_controller_sp)
3683 {
3684 g_settings_controller_sp.reset (new Process::SettingsController);
3685 // The first shared pointer to Process::SettingsController in
3686 // g_settings_controller_sp must be fully created above so that
3687 // the TargetInstanceSettings can use a weak_ptr to refer back
3688 // to the master setttings controller
3689 InstanceSettingsSP default_instance_settings_sp (new ProcessInstanceSettings (g_settings_controller_sp,
3690 false,
3691 InstanceSettings::GetDefaultName().AsCString()));
3692 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
3693 }
3694 return g_settings_controller_sp;
3695
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00003696}
3697
Caroline Tice1ebef442010-09-27 00:30:10 +00003698void
3699Process::UpdateInstanceName ()
3700{
Greg Clayton5beb99d2011-08-11 02:48:45 +00003701 Module *module = GetTarget().GetExecutableModulePointer();
Greg Clayton13d24fb2012-01-29 20:56:30 +00003702 if (module && module->GetFileSpec().GetFilename())
Caroline Tice1ebef442010-09-27 00:30:10 +00003703 {
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00003704 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
Greg Clayton13d24fb2012-01-29 20:56:30 +00003705 module->GetFileSpec().GetFilename().AsCString());
Caroline Tice1ebef442010-09-27 00:30:10 +00003706 }
3707}
3708
Greg Clayton427f2902010-12-14 02:59:59 +00003709ExecutionResults
Jim Ingham360f53f2010-11-30 02:22:11 +00003710Process::RunThreadPlan (ExecutionContext &exe_ctx,
3711 lldb::ThreadPlanSP &thread_plan_sp,
3712 bool stop_others,
3713 bool try_all_threads,
3714 bool discard_on_error,
3715 uint32_t single_thread_timeout_usec,
3716 Stream &errors)
3717{
3718 ExecutionResults return_value = eExecutionSetupError;
3719
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003720 if (thread_plan_sp.get() == NULL)
3721 {
3722 errors.Printf("RunThreadPlan called with empty thread plan.");
Greg Claytonb3448432011-03-24 21:19:54 +00003723 return eExecutionSetupError;
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003724 }
Greg Clayton567e7f32011-09-22 04:58:26 +00003725
3726 if (exe_ctx.GetProcessPtr() != this)
3727 {
3728 errors.Printf("RunThreadPlan called on wrong process.");
3729 return eExecutionSetupError;
3730 }
3731
3732 Thread *thread = exe_ctx.GetThreadPtr();
3733 if (thread == NULL)
3734 {
3735 errors.Printf("RunThreadPlan called with invalid thread.");
3736 return eExecutionSetupError;
3737 }
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003738
Jim Ingham5ab7fba2011-05-17 22:24:54 +00003739 // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes.
3740 // For that to be true the plan can't be private - since private plans suppress themselves in the
3741 // GetCompletedPlan call.
3742
3743 bool orig_plan_private = thread_plan_sp->GetPrivate();
3744 thread_plan_sp->SetPrivate(false);
3745
Jim Inghamac959662011-01-24 06:34:17 +00003746 if (m_private_state.GetValue() != eStateStopped)
3747 {
3748 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Greg Claytonb3448432011-03-24 21:19:54 +00003749 return eExecutionSetupError;
Jim Inghamac959662011-01-24 06:34:17 +00003750 }
3751
Jim Ingham7bbebaf2011-08-13 00:56:10 +00003752 // Save the thread & frame from the exe_ctx for restoration after we run
Greg Clayton567e7f32011-09-22 04:58:26 +00003753 const uint32_t thread_idx_id = thread->GetIndexID();
3754 StackID ctx_frame_id = thread->GetSelectedFrame()->GetStackID();
Jim Ingham360f53f2010-11-30 02:22:11 +00003755
3756 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
3757 // so we should arrange to reset them as well.
3758
Greg Clayton567e7f32011-09-22 04:58:26 +00003759 lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread();
Jim Ingham360f53f2010-11-30 02:22:11 +00003760
Jim Ingham7bbebaf2011-08-13 00:56:10 +00003761 uint32_t selected_tid;
3762 StackID selected_stack_id;
Greg Claytone40b6422011-09-18 18:59:15 +00003763 if (selected_thread_sp)
Jim Ingham360f53f2010-11-30 02:22:11 +00003764 {
3765 selected_tid = selected_thread_sp->GetIndexID();
Jim Ingham7bbebaf2011-08-13 00:56:10 +00003766 selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID();
Jim Ingham360f53f2010-11-30 02:22:11 +00003767 }
3768 else
3769 {
3770 selected_tid = LLDB_INVALID_THREAD_ID;
3771 }
3772
Greg Clayton567e7f32011-09-22 04:58:26 +00003773 thread->QueueThreadPlan(thread_plan_sp, true);
Jim Ingham360f53f2010-11-30 02:22:11 +00003774
Jim Ingham6ae318c2011-01-23 21:14:08 +00003775 Listener listener("lldb.process.listener.run-thread-plan");
Jim Inghamf9f40c22011-02-08 05:20:59 +00003776
3777 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
3778 // restored on exit to the function.
3779
3780 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Jim Inghamac959662011-01-24 06:34:17 +00003781
Jim Ingham6ae318c2011-01-23 21:14:08 +00003782 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003783 if (log)
3784 {
3785 StreamString s;
3786 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Greg Clayton444e35b2011-10-19 18:09:39 +00003787 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4llx to run thread plan \"%s\".",
Greg Clayton567e7f32011-09-22 04:58:26 +00003788 thread->GetIndexID(),
3789 thread->GetID(),
Jim Inghamf9f40c22011-02-08 05:20:59 +00003790 s.GetData());
Jim Ingham15dcb7c2011-01-20 02:03:18 +00003791 }
3792
Jim Inghamf9f40c22011-02-08 05:20:59 +00003793 bool got_event;
3794 lldb::EventSP event_sp;
3795 lldb::StateType stop_state = lldb::eStateInvalid;
Jim Ingham360f53f2010-11-30 02:22:11 +00003796
3797 TimeValue* timeout_ptr = NULL;
3798 TimeValue real_timeout;
3799
Jim Inghamf9f40c22011-02-08 05:20:59 +00003800 bool first_timeout = true;
3801 bool do_resume = true;
Jim Ingham360f53f2010-11-30 02:22:11 +00003802
Jim Ingham360f53f2010-11-30 02:22:11 +00003803 while (1)
3804 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003805 // We usually want to resume the process if we get to the top of the loop.
3806 // The only exception is if we get two running events with no intervening
3807 // stop, which can happen, we will just wait for then next stop event.
Jim Ingham360f53f2010-11-30 02:22:11 +00003808
Jim Inghamf9f40c22011-02-08 05:20:59 +00003809 if (do_resume)
Jim Ingham360f53f2010-11-30 02:22:11 +00003810 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00003811 // Do the initial resume and wait for the running event before going further.
3812
Greg Clayton567e7f32011-09-22 04:58:26 +00003813 Error resume_error = Resume ();
Jim Inghamf9f40c22011-02-08 05:20:59 +00003814 if (!resume_error.Success())
3815 {
3816 errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString());
Greg Claytonb3448432011-03-24 21:19:54 +00003817 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003818 break;
3819 }
3820
3821 real_timeout = TimeValue::Now();
3822 real_timeout.OffsetWithMicroSeconds(500000);
3823 timeout_ptr = &real_timeout;
3824
Sean Callananfaf04782012-01-05 02:00:14 +00003825 got_event = listener.WaitForEvent(timeout_ptr, event_sp);
Jim Inghamf9f40c22011-02-08 05:20:59 +00003826 if (!got_event)
3827 {
3828 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00003829 log->PutCString("Didn't get any event after initial resume, exiting.");
Jim Inghamf9f40c22011-02-08 05:20:59 +00003830
3831 errors.Printf("Didn't get any event after initial resume, exiting.");
Greg Claytonb3448432011-03-24 21:19:54 +00003832 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003833 break;
3834 }
3835
3836 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3837 if (stop_state != eStateRunning)
3838 {
3839 if (log)
3840 log->Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state));
3841
3842 errors.Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state));
Greg Claytonb3448432011-03-24 21:19:54 +00003843 return_value = eExecutionSetupError;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003844 break;
3845 }
3846
3847 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00003848 log->PutCString ("Resuming succeeded.");
Jim Inghamf9f40c22011-02-08 05:20:59 +00003849 // We need to call the function synchronously, so spin waiting for it to return.
3850 // If we get interrupted while executing, we're going to lose our context, and
3851 // won't be able to gather the result at this point.
3852 // We set the timeout AFTER the resume, since the resume takes some time and we
3853 // don't want to charge that to the timeout.
3854
3855 if (single_thread_timeout_usec != 0)
3856 {
3857 real_timeout = TimeValue::Now();
3858 if (first_timeout)
3859 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
3860 else
3861 real_timeout.OffsetWithSeconds(10);
3862
3863 timeout_ptr = &real_timeout;
3864 }
3865 }
3866 else
3867 {
3868 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00003869 log->PutCString ("Handled an extra running event.");
Jim Inghamf9f40c22011-02-08 05:20:59 +00003870 do_resume = true;
3871 }
3872
3873 // Now wait for the process to stop again:
3874 stop_state = lldb::eStateInvalid;
3875 event_sp.reset();
3876 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
3877
3878 if (got_event)
3879 {
3880 if (event_sp.get())
3881 {
3882 bool keep_going = false;
3883 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
3884 if (log)
3885 log->Printf("In while loop, got event: %s.", StateAsCString(stop_state));
3886
3887 switch (stop_state)
3888 {
3889 case lldb::eStateStopped:
Jim Ingham2370a972011-05-17 01:10:11 +00003890 {
Greg Clayton43994462011-06-03 22:12:42 +00003891 // Yay, we're done. Now make sure that our thread plan actually completed.
Greg Clayton567e7f32011-09-22 04:58:26 +00003892 ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id);
Greg Clayton43994462011-06-03 22:12:42 +00003893 if (!thread_sp)
Jim Ingham2370a972011-05-17 01:10:11 +00003894 {
Greg Clayton43994462011-06-03 22:12:42 +00003895 // Ooh, our thread has vanished. Unlikely that this was successful execution...
Jim Ingham2370a972011-05-17 01:10:11 +00003896 if (log)
Greg Clayton43994462011-06-03 22:12:42 +00003897 log->Printf ("Execution completed but our thread (index-id=%u) has vanished.", thread_idx_id);
3898 return_value = eExecutionInterrupted;
Jim Ingham2370a972011-05-17 01:10:11 +00003899 }
3900 else
3901 {
Greg Clayton43994462011-06-03 22:12:42 +00003902 StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
3903 StopReason stop_reason = eStopReasonInvalid;
3904 if (stop_info_sp)
3905 stop_reason = stop_info_sp->GetStopReason();
3906 if (stop_reason == eStopReasonPlanComplete)
3907 {
3908 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00003909 log->PutCString ("Execution completed successfully.");
Greg Clayton43994462011-06-03 22:12:42 +00003910 // Now mark this plan as private so it doesn't get reported as the stop reason
3911 // after this point.
3912 if (thread_plan_sp)
3913 thread_plan_sp->SetPrivate (orig_plan_private);
3914 return_value = eExecutionCompleted;
3915 }
3916 else
3917 {
3918 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00003919 log->PutCString ("Thread plan didn't successfully complete.");
Greg Clayton43994462011-06-03 22:12:42 +00003920
3921 return_value = eExecutionInterrupted;
3922 }
Jim Ingham2370a972011-05-17 01:10:11 +00003923 }
Greg Clayton43994462011-06-03 22:12:42 +00003924 }
3925 break;
3926
Jim Inghamf9f40c22011-02-08 05:20:59 +00003927 case lldb::eStateCrashed:
3928 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00003929 log->PutCString ("Execution crashed.");
Greg Claytonb3448432011-03-24 21:19:54 +00003930 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003931 break;
Greg Clayton43994462011-06-03 22:12:42 +00003932
Jim Inghamf9f40c22011-02-08 05:20:59 +00003933 case lldb::eStateRunning:
3934 do_resume = false;
3935 keep_going = true;
3936 break;
Greg Clayton43994462011-06-03 22:12:42 +00003937
Jim Inghamf9f40c22011-02-08 05:20:59 +00003938 default:
3939 if (log)
3940 log->Printf("Execution stopped with unexpected state: %s.", StateAsCString(stop_state));
Jim Ingham2370a972011-05-17 01:10:11 +00003941
3942 errors.Printf ("Execution stopped with unexpected state.");
Greg Claytonb3448432011-03-24 21:19:54 +00003943 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003944 break;
3945 }
3946 if (keep_going)
3947 continue;
3948 else
3949 break;
3950 }
3951 else
3952 {
3953 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00003954 log->PutCString ("got_event was true, but the event pointer was null. How odd...");
Greg Claytonb3448432011-03-24 21:19:54 +00003955 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00003956 break;
3957 }
3958 }
3959 else
3960 {
3961 // If we didn't get an event that means we've timed out...
3962 // We will interrupt the process here. Depending on what we were asked to do we will
3963 // either exit, or try with all threads running for the same timeout.
Jim Ingham360f53f2010-11-30 02:22:11 +00003964 // Not really sure what to do if Halt fails here...
Jim Inghamf9f40c22011-02-08 05:20:59 +00003965
Stephen Wilsonc2b98252011-01-12 04:20:03 +00003966 if (log) {
Jim Ingham360f53f2010-11-30 02:22:11 +00003967 if (try_all_threads)
Jim Inghamf9f40c22011-02-08 05:20:59 +00003968 {
3969 if (first_timeout)
3970 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
3971 "trying with all threads enabled.",
3972 single_thread_timeout_usec);
3973 else
3974 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
3975 "and timeout: %d timed out.",
3976 single_thread_timeout_usec);
3977 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003978 else
Jim Inghamf9f40c22011-02-08 05:20:59 +00003979 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
3980 "halt and abandoning execution.",
Jim Ingham360f53f2010-11-30 02:22:11 +00003981 single_thread_timeout_usec);
Stephen Wilsonc2b98252011-01-12 04:20:03 +00003982 }
Jim Ingham360f53f2010-11-30 02:22:11 +00003983
Greg Clayton567e7f32011-09-22 04:58:26 +00003984 Error halt_error = Halt();
Jim Inghamc556b462011-01-22 01:30:53 +00003985 if (halt_error.Success())
Jim Ingham360f53f2010-11-30 02:22:11 +00003986 {
Jim Ingham360f53f2010-11-30 02:22:11 +00003987 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00003988 log->PutCString ("Process::RunThreadPlan(): Halt succeeded.");
Jim Ingham360f53f2010-11-30 02:22:11 +00003989
Jim Inghamf9f40c22011-02-08 05:20:59 +00003990 // If halt succeeds, it always produces a stopped event. Wait for that:
3991
3992 real_timeout = TimeValue::Now();
3993 real_timeout.OffsetWithMicroSeconds(500000);
3994
3995 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Jim Ingham360f53f2010-11-30 02:22:11 +00003996
3997 if (got_event)
3998 {
3999 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
4000 if (log)
4001 {
Greg Clayton68ca8232011-01-25 02:58:48 +00004002 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
Jim Inghamf9f40c22011-02-08 05:20:59 +00004003 if (stop_state == lldb::eStateStopped
4004 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
Jim Inghamf6d3d792011-08-09 22:24:33 +00004005 log->PutCString (" Event was the Halt interruption event.");
Jim Ingham360f53f2010-11-30 02:22:11 +00004006 }
4007
Jim Inghamf9f40c22011-02-08 05:20:59 +00004008 if (stop_state == lldb::eStateStopped)
Jim Ingham360f53f2010-11-30 02:22:11 +00004009 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00004010 // Between the time we initiated the Halt and the time we delivered it, the process could have
4011 // already finished its job. Check that here:
Jim Ingham360f53f2010-11-30 02:22:11 +00004012
Greg Clayton567e7f32011-09-22 04:58:26 +00004013 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
Jim Inghamf9f40c22011-02-08 05:20:59 +00004014 {
4015 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004016 log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
Jim Inghamf9f40c22011-02-08 05:20:59 +00004017 "Exiting wait loop.");
Greg Claytonb3448432011-03-24 21:19:54 +00004018 return_value = eExecutionCompleted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004019 break;
4020 }
Jim Ingham360f53f2010-11-30 02:22:11 +00004021
Jim Inghamf9f40c22011-02-08 05:20:59 +00004022 if (!try_all_threads)
4023 {
4024 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004025 log->PutCString ("try_all_threads was false, we stopped so now we're quitting.");
Greg Claytonb3448432011-03-24 21:19:54 +00004026 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004027 break;
4028 }
4029
4030 if (first_timeout)
4031 {
4032 // Set all the other threads to run, and return to the top of the loop, which will continue;
4033 first_timeout = false;
4034 thread_plan_sp->SetStopOthers (false);
4035 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004036 log->PutCString ("Process::RunThreadPlan(): About to resume.");
Jim Inghamf9f40c22011-02-08 05:20:59 +00004037
4038 continue;
4039 }
4040 else
4041 {
4042 // Running all threads failed, so return Interrupted.
4043 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004044 log->PutCString("Process::RunThreadPlan(): running all threads timed out.");
Greg Claytonb3448432011-03-24 21:19:54 +00004045 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004046 break;
4047 }
Jim Ingham360f53f2010-11-30 02:22:11 +00004048 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00004049 }
4050 else
4051 { if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004052 log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
Jim Inghamf9f40c22011-02-08 05:20:59 +00004053 "I'm getting out of here passing Interrupted.");
Greg Claytonb3448432011-03-24 21:19:54 +00004054 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004055 break;
Jim Ingham360f53f2010-11-30 02:22:11 +00004056 }
4057 }
Jim Inghamc556b462011-01-22 01:30:53 +00004058 else
4059 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00004060 // This branch is to work around some problems with gdb-remote's Halt. It is a little racy, and can return
4061 // an error from halt, but if you wait a bit you'll get a stopped event anyway.
Jim Inghamc556b462011-01-22 01:30:53 +00004062 if (log)
Jim Inghamf9f40c22011-02-08 05:20:59 +00004063 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.",
4064 halt_error.AsCString());
4065 real_timeout = TimeValue::Now();
4066 real_timeout.OffsetWithMicroSeconds(500000);
4067 timeout_ptr = &real_timeout;
4068 got_event = listener.WaitForEvent(&real_timeout, event_sp);
4069 if (!got_event || event_sp.get() == NULL)
Jim Ingham6ae318c2011-01-23 21:14:08 +00004070 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00004071 // This is not going anywhere, bag out.
4072 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004073 log->PutCString ("Process::RunThreadPlan(): halt failed: and waiting for the stopped event failed.");
Greg Claytonb3448432011-03-24 21:19:54 +00004074 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004075 break;
Jim Ingham6ae318c2011-01-23 21:14:08 +00004076 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00004077 else
4078 {
4079 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
4080 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004081 log->PutCString ("Process::RunThreadPlan(): halt failed: but then I got a stopped event. Whatever...");
Jim Inghamf9f40c22011-02-08 05:20:59 +00004082 if (stop_state == lldb::eStateStopped)
4083 {
4084 // Between the time we initiated the Halt and the time we delivered it, the process could have
4085 // already finished its job. Check that here:
4086
Greg Clayton567e7f32011-09-22 04:58:26 +00004087 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
Jim Inghamf9f40c22011-02-08 05:20:59 +00004088 {
4089 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004090 log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
Jim Inghamf9f40c22011-02-08 05:20:59 +00004091 "Exiting wait loop.");
Greg Claytonb3448432011-03-24 21:19:54 +00004092 return_value = eExecutionCompleted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004093 break;
4094 }
4095
4096 if (first_timeout)
4097 {
4098 // Set all the other threads to run, and return to the top of the loop, which will continue;
4099 first_timeout = false;
4100 thread_plan_sp->SetStopOthers (false);
4101 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004102 log->PutCString ("Process::RunThreadPlan(): About to resume.");
Jim Inghamf9f40c22011-02-08 05:20:59 +00004103
4104 continue;
4105 }
4106 else
4107 {
4108 // Running all threads failed, so return Interrupted.
4109 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004110 log->PutCString ("Process::RunThreadPlan(): running all threads timed out.");
Greg Claytonb3448432011-03-24 21:19:54 +00004111 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004112 break;
4113 }
4114 }
4115 else
4116 {
Sean Callananed3f86b2011-08-09 22:07:08 +00004117 if (log)
4118 log->Printf ("Process::RunThreadPlan(): halt failed, I waited and didn't get"
4119 " a stopped event, instead got %s.", StateAsCString(stop_state));
Greg Claytonb3448432011-03-24 21:19:54 +00004120 return_value = eExecutionInterrupted;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004121 break;
4122 }
4123 }
Jim Inghamc556b462011-01-22 01:30:53 +00004124 }
4125
Jim Ingham360f53f2010-11-30 02:22:11 +00004126 }
4127
Jim Inghamf9f40c22011-02-08 05:20:59 +00004128 } // END WAIT LOOP
4129
4130 // Now do some processing on the results of the run:
4131 if (return_value == eExecutionInterrupted)
4132 {
Jim Ingham360f53f2010-11-30 02:22:11 +00004133 if (log)
Jim Inghamf9f40c22011-02-08 05:20:59 +00004134 {
4135 StreamString s;
4136 if (event_sp)
4137 event_sp->Dump (&s);
4138 else
4139 {
Jim Inghamf6d3d792011-08-09 22:24:33 +00004140 log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
Jim Inghamf9f40c22011-02-08 05:20:59 +00004141 }
4142
4143 StreamString ts;
4144
Jim Inghamf6d3d792011-08-09 22:24:33 +00004145 const char *event_explanation = NULL;
Jim Inghamf9f40c22011-02-08 05:20:59 +00004146
4147 do
4148 {
4149 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
4150
4151 if (!event_data)
4152 {
4153 event_explanation = "<no event data>";
4154 break;
4155 }
4156
4157 Process *process = event_data->GetProcessSP().get();
4158
4159 if (!process)
4160 {
4161 event_explanation = "<no process>";
4162 break;
4163 }
4164
4165 ThreadList &thread_list = process->GetThreadList();
4166
4167 uint32_t num_threads = thread_list.GetSize();
4168 uint32_t thread_index;
4169
4170 ts.Printf("<%u threads> ", num_threads);
4171
4172 for (thread_index = 0;
4173 thread_index < num_threads;
4174 ++thread_index)
4175 {
4176 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
4177
4178 if (!thread)
4179 {
4180 ts.Printf("<?> ");
4181 continue;
4182 }
4183
Greg Clayton444e35b2011-10-19 18:09:39 +00004184 ts.Printf("<0x%4.4llx ", thread->GetID());
Jim Inghamf9f40c22011-02-08 05:20:59 +00004185 RegisterContext *register_context = thread->GetRegisterContext().get();
4186
4187 if (register_context)
4188 ts.Printf("[ip 0x%llx] ", register_context->GetPC());
4189 else
4190 ts.Printf("[ip unknown] ");
4191
4192 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
4193 if (stop_info_sp)
4194 {
4195 const char *stop_desc = stop_info_sp->GetDescription();
4196 if (stop_desc)
4197 ts.PutCString (stop_desc);
4198 }
4199 ts.Printf(">");
4200 }
4201
4202 event_explanation = ts.GetData();
4203 } while (0);
4204
4205 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004206 {
4207 if (event_explanation)
4208 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
4209 else
4210 log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData());
4211 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00004212
4213 if (discard_on_error && thread_plan_sp)
4214 {
Greg Clayton567e7f32011-09-22 04:58:26 +00004215 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
Jim Ingham21f37ad2011-08-09 02:12:22 +00004216 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Inghamf9f40c22011-02-08 05:20:59 +00004217 }
4218 }
4219 }
4220 else if (return_value == eExecutionSetupError)
4221 {
4222 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004223 log->PutCString("Process::RunThreadPlan(): execution set up error.");
Jim Inghamf9f40c22011-02-08 05:20:59 +00004224
4225 if (discard_on_error && thread_plan_sp)
4226 {
Greg Clayton567e7f32011-09-22 04:58:26 +00004227 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
Jim Ingham21f37ad2011-08-09 02:12:22 +00004228 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Inghamf9f40c22011-02-08 05:20:59 +00004229 }
4230 }
4231 else
4232 {
Greg Clayton567e7f32011-09-22 04:58:26 +00004233 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
Jim Ingham360f53f2010-11-30 02:22:11 +00004234 {
Greg Clayton68ca8232011-01-25 02:58:48 +00004235 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004236 log->PutCString("Process::RunThreadPlan(): thread plan is done");
Greg Claytonb3448432011-03-24 21:19:54 +00004237 return_value = eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +00004238 }
Greg Clayton567e7f32011-09-22 04:58:26 +00004239 else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
Jim Ingham360f53f2010-11-30 02:22:11 +00004240 {
Greg Clayton68ca8232011-01-25 02:58:48 +00004241 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004242 log->PutCString("Process::RunThreadPlan(): thread plan was discarded");
Greg Claytonb3448432011-03-24 21:19:54 +00004243 return_value = eExecutionDiscarded;
Jim Ingham360f53f2010-11-30 02:22:11 +00004244 }
4245 else
4246 {
4247 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004248 log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Ingham360f53f2010-11-30 02:22:11 +00004249 if (discard_on_error && thread_plan_sp)
4250 {
Jim Inghamf9f40c22011-02-08 05:20:59 +00004251 if (log)
Jim Inghamf6d3d792011-08-09 22:24:33 +00004252 log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause discard_on_error is set.");
Greg Clayton567e7f32011-09-22 04:58:26 +00004253 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
Jim Ingham21f37ad2011-08-09 02:12:22 +00004254 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham360f53f2010-11-30 02:22:11 +00004255 }
Jim Ingham360f53f2010-11-30 02:22:11 +00004256 }
4257 }
Jim Inghamf9f40c22011-02-08 05:20:59 +00004258
Jim Ingham360f53f2010-11-30 02:22:11 +00004259 // Thread we ran the function in may have gone away because we ran the target
Jim Ingham7bbebaf2011-08-13 00:56:10 +00004260 // Check that it's still there, and if it is put it back in the context. Also restore the
4261 // frame in the context if it is still present.
Greg Clayton567e7f32011-09-22 04:58:26 +00004262 thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get();
4263 if (thread)
Jim Ingham7bbebaf2011-08-13 00:56:10 +00004264 {
Greg Clayton567e7f32011-09-22 04:58:26 +00004265 exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id));
Jim Ingham7bbebaf2011-08-13 00:56:10 +00004266 }
Jim Ingham360f53f2010-11-30 02:22:11 +00004267
4268 // Also restore the current process'es selected frame & thread, since this function calling may
4269 // be done behind the user's back.
4270
4271 if (selected_tid != LLDB_INVALID_THREAD_ID)
4272 {
Greg Clayton567e7f32011-09-22 04:58:26 +00004273 if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid())
Jim Ingham360f53f2010-11-30 02:22:11 +00004274 {
4275 // We were able to restore the selected thread, now restore the frame:
Greg Clayton567e7f32011-09-22 04:58:26 +00004276 StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id);
Jim Ingham7bbebaf2011-08-13 00:56:10 +00004277 if (old_frame_sp)
Greg Clayton567e7f32011-09-22 04:58:26 +00004278 GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get());
Jim Ingham360f53f2010-11-30 02:22:11 +00004279 }
4280 }
4281
4282 return return_value;
4283}
4284
4285const char *
4286Process::ExecutionResultAsCString (ExecutionResults result)
4287{
4288 const char *result_name;
4289
4290 switch (result)
4291 {
Greg Claytonb3448432011-03-24 21:19:54 +00004292 case eExecutionCompleted:
Jim Ingham360f53f2010-11-30 02:22:11 +00004293 result_name = "eExecutionCompleted";
4294 break;
Greg Claytonb3448432011-03-24 21:19:54 +00004295 case eExecutionDiscarded:
Jim Ingham360f53f2010-11-30 02:22:11 +00004296 result_name = "eExecutionDiscarded";
4297 break;
Greg Claytonb3448432011-03-24 21:19:54 +00004298 case eExecutionInterrupted:
Jim Ingham360f53f2010-11-30 02:22:11 +00004299 result_name = "eExecutionInterrupted";
4300 break;
Greg Claytonb3448432011-03-24 21:19:54 +00004301 case eExecutionSetupError:
Jim Ingham360f53f2010-11-30 02:22:11 +00004302 result_name = "eExecutionSetupError";
4303 break;
Greg Claytonb3448432011-03-24 21:19:54 +00004304 case eExecutionTimedOut:
Jim Ingham360f53f2010-11-30 02:22:11 +00004305 result_name = "eExecutionTimedOut";
4306 break;
4307 }
4308 return result_name;
4309}
4310
Greg Claytonabe0fed2011-04-18 08:33:37 +00004311void
4312Process::GetStatus (Stream &strm)
4313{
4314 const StateType state = GetState();
Greg Clayton20206082011-11-17 01:23:07 +00004315 if (StateIsStoppedState(state, false))
Greg Claytonabe0fed2011-04-18 08:33:37 +00004316 {
4317 if (state == eStateExited)
4318 {
4319 int exit_status = GetExitStatus();
4320 const char *exit_description = GetExitDescription();
Greg Clayton444e35b2011-10-19 18:09:39 +00004321 strm.Printf ("Process %llu exited with status = %i (0x%8.8x) %s\n",
Greg Claytonabe0fed2011-04-18 08:33:37 +00004322 GetID(),
4323 exit_status,
4324 exit_status,
4325 exit_description ? exit_description : "");
4326 }
4327 else
4328 {
4329 if (state == eStateConnected)
4330 strm.Printf ("Connected to remote target.\n");
4331 else
Greg Clayton444e35b2011-10-19 18:09:39 +00004332 strm.Printf ("Process %llu %s\n", GetID(), StateAsCString (state));
Greg Claytonabe0fed2011-04-18 08:33:37 +00004333 }
4334 }
4335 else
4336 {
Greg Clayton444e35b2011-10-19 18:09:39 +00004337 strm.Printf ("Process %llu is running.\n", GetID());
Greg Claytonabe0fed2011-04-18 08:33:37 +00004338 }
4339}
4340
4341size_t
4342Process::GetThreadStatus (Stream &strm,
4343 bool only_threads_with_stop_reason,
4344 uint32_t start_frame,
4345 uint32_t num_frames,
4346 uint32_t num_frames_with_source)
4347{
4348 size_t num_thread_infos_dumped = 0;
4349
4350 const size_t num_threads = GetThreadList().GetSize();
4351 for (uint32_t i = 0; i < num_threads; i++)
4352 {
4353 Thread *thread = GetThreadList().GetThreadAtIndex(i).get();
4354 if (thread)
4355 {
4356 if (only_threads_with_stop_reason)
4357 {
4358 if (thread->GetStopInfo().get() == NULL)
4359 continue;
4360 }
4361 thread->GetStatus (strm,
4362 start_frame,
4363 num_frames,
4364 num_frames_with_source);
4365 ++num_thread_infos_dumped;
4366 }
4367 }
4368 return num_thread_infos_dumped;
4369}
4370
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004371//--------------------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00004372// class Process::SettingsController
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004373//--------------------------------------------------------------
4374
Greg Claytond0a5a232010-09-19 02:33:57 +00004375Process::SettingsController::SettingsController () :
Caroline Tice5bc8c972010-09-20 20:44:43 +00004376 UserSettingsController ("process", Target::GetSettingsController())
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004377{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004378}
4379
Greg Claytond0a5a232010-09-19 02:33:57 +00004380Process::SettingsController::~SettingsController ()
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004381{
4382}
4383
4384lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +00004385Process::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004386{
Greg Clayton334d33a2012-01-30 07:41:31 +00004387 lldb::InstanceSettingsSP new_settings_sp (new ProcessInstanceSettings (GetSettingsController(),
4388 false,
4389 instance_name));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004390 return new_settings_sp;
4391}
4392
4393//--------------------------------------------------------------
4394// class ProcessInstanceSettings
4395//--------------------------------------------------------------
4396
Greg Clayton638351a2010-12-04 00:10:17 +00004397ProcessInstanceSettings::ProcessInstanceSettings
4398(
Greg Clayton334d33a2012-01-30 07:41:31 +00004399 const UserSettingsControllerSP &owner_sp,
Greg Clayton638351a2010-12-04 00:10:17 +00004400 bool live_instance,
4401 const char *name
4402) :
Greg Clayton334d33a2012-01-30 07:41:31 +00004403 InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004404{
Caroline Tice396704b2010-09-09 18:26:37 +00004405 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
4406 // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers.
4407 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
Caroline Tice75b11a32010-09-16 19:05:55 +00004408 // This is true for CreateInstanceName() too.
Greg Claytonabb33022011-11-08 02:43:13 +00004409
Caroline Tice75b11a32010-09-16 19:05:55 +00004410 if (GetInstanceName () == InstanceSettings::InvalidName())
4411 {
4412 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Clayton334d33a2012-01-30 07:41:31 +00004413 owner_sp->RegisterInstanceSettings (this);
Caroline Tice75b11a32010-09-16 19:05:55 +00004414 }
Greg Claytonabb33022011-11-08 02:43:13 +00004415
Caroline Tice396704b2010-09-09 18:26:37 +00004416 if (live_instance)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004417 {
Greg Clayton334d33a2012-01-30 07:41:31 +00004418 const lldb::InstanceSettingsSP &pending_settings = owner_sp->FindPendingSettings (m_instance_name);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004419 CopyInstanceSettings (pending_settings,false);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004420 }
4421}
4422
4423ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) :
Greg Clayton334d33a2012-01-30 07:41:31 +00004424 InstanceSettings (Process::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004425{
4426 if (m_instance_name != InstanceSettings::GetDefaultName())
4427 {
Greg Clayton334d33a2012-01-30 07:41:31 +00004428 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
4429 if (owner_sp)
4430 {
4431 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name), false);
4432 owner_sp->RemovePendingSettings (m_instance_name);
4433 }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004434 }
4435}
4436
4437ProcessInstanceSettings::~ProcessInstanceSettings ()
4438{
4439}
4440
4441ProcessInstanceSettings&
4442ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs)
4443{
4444 if (this != &rhs)
4445 {
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004446 }
4447
4448 return *this;
4449}
4450
4451
4452void
4453ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
4454 const char *index_value,
4455 const char *value,
4456 const ConstString &instance_name,
4457 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00004458 VarSetOperationType op,
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004459 Error &err,
4460 bool pending)
4461{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004462}
4463
4464void
4465ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
4466 bool pending)
4467{
Greg Claytonabb33022011-11-08 02:43:13 +00004468// if (new_settings.get() == NULL)
4469// return;
4470//
4471// ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004472}
4473
Caroline Ticebcb5b452010-09-20 21:37:42 +00004474bool
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004475ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
4476 const ConstString &var_name,
Caroline Tice5bc8c972010-09-20 20:44:43 +00004477 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00004478 Error *err)
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004479{
Greg Claytonabb33022011-11-08 02:43:13 +00004480 if (err)
4481 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
4482 return false;
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004483}
4484
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004485const ConstString
4486ProcessInstanceSettings::CreateInstanceName ()
4487{
4488 static int instance_count = 1;
4489 StreamString sstr;
4490
4491 sstr.Printf ("process_%d", instance_count);
4492 ++instance_count;
4493
4494 const ConstString ret_val (sstr.GetData());
4495 return ret_val;
4496}
4497
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004498//--------------------------------------------------
Greg Claytond0a5a232010-09-19 02:33:57 +00004499// SettingsController Variable Tables
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004500//--------------------------------------------------
4501
4502SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00004503Process::SettingsController::global_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004504{
4505 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
4506 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
4507};
4508
4509
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004510SettingEntry
Greg Claytond0a5a232010-09-19 02:33:57 +00004511Process::SettingsController::instance_settings_table[] =
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004512{
Greg Clayton638351a2010-12-04 00:10:17 +00004513 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
Greg Clayton638351a2010-12-04 00:10:17 +00004514 { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00004515};
4516
4517
Jim Ingham7508e732010-08-09 23:31:02 +00004518
Greg Claytonabb33022011-11-08 02:43:13 +00004519