blob: 652505252f151bc79c3fe6726ab7b9763c1c553d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Process.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Process.h"
13
14#include "lldb/lldb-private-log.h"
15
16#include "lldb/Breakpoint/StoppointCallbackContext.h"
17#include "lldb/Breakpoint/BreakpointLocation.h"
18#include "lldb/Core/Event.h"
Caroline Ticeef5c6d02010-11-16 05:07:41 +000019#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Debugger.h"
Caroline Ticeef5c6d02010-11-16 05:07:41 +000021#include "lldb/Core/InputReader.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/PluginManager.h"
25#include "lldb/Core/State.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000026#include "lldb/Expression/ClangUserExpression.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000027#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Host/Host.h"
29#include "lldb/Target/ABI.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000030#include "lldb/Target/DynamicLoader.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000031#include "lldb/Target/OperatingSystem.h"
Jim Ingham22777012010-09-23 02:01:19 +000032#include "lldb/Target/LanguageRuntime.h"
33#include "lldb/Target/CPPLanguageRuntime.h"
34#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytone996fd32011-03-08 22:40:15 +000035#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000037#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Target/Target.h"
39#include "lldb/Target/TargetList.h"
40#include "lldb/Target/Thread.h"
41#include "lldb/Target/ThreadPlan.h"
Jim Ingham076b3042012-04-10 01:21:57 +000042#include "lldb/Target/ThreadPlanBase.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
44using namespace lldb;
45using namespace lldb_private;
46
Greg Clayton67cc0632012-08-22 17:17:09 +000047
48// Comment out line below to disable memory caching, overriding the process setting
49// target.process.disable-memory-cache
50#define ENABLE_MEMORY_CACHING
51
52#ifdef ENABLE_MEMORY_CACHING
53#define DISABLE_MEM_CACHE_DEFAULT false
54#else
55#define DISABLE_MEM_CACHE_DEFAULT true
56#endif
57
58class ProcessOptionValueProperties : public OptionValueProperties
59{
60public:
61 ProcessOptionValueProperties (const ConstString &name) :
62 OptionValueProperties (name)
63 {
64 }
65
66 // This constructor is used when creating ProcessOptionValueProperties when it
67 // is part of a new lldb_private::Process instance. It will copy all current
68 // global property values as needed
69 ProcessOptionValueProperties (ProcessProperties *global_properties) :
70 OptionValueProperties(*global_properties->GetValueProperties())
71 {
72 }
73
74 virtual const Property *
75 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
76 {
77 // When gettings the value for a key from the process options, we will always
78 // try and grab the setting from the current process if there is one. Else we just
79 // use the one from this instance.
80 if (exe_ctx)
81 {
82 Process *process = exe_ctx->GetProcessPtr();
83 if (process)
84 {
85 ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get());
86 if (this != instance_properties)
87 return instance_properties->ProtectedGetPropertyAtIndex (idx);
88 }
89 }
90 return ProtectedGetPropertyAtIndex (idx);
91 }
92};
93
94static PropertyDefinition
95g_properties[] =
96{
97 { "disable-memory-cache" , OptionValue::eTypeBoolean, false, DISABLE_MEM_CACHE_DEFAULT, NULL, NULL, "Disable reading and caching of memory in fixed-size units." },
Jim Ingham8c3f2762012-11-29 00:41:12 +000098 { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. "
99 "For instance, to turn on debugserver logging set this to \"QSetLogging:bitmask=LOG_DEFAULT;\"" },
Jim Inghamafc1b122013-01-31 19:48:57 +0000100 { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." },
101 { "unwind-on-error-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, errors in expression evaluation will unwind the stack back to the state before the call." },
Greg Claytone1e835c2012-11-29 18:48:47 +0000102 { "python-os-plugin-path", OptionValue::eTypeFileSpec, false, true, NULL, NULL, "A path to a python OS plug-in module file that contains a OperatingSystemPlugIn class." },
Jim Ingham29950772013-01-26 02:19:28 +0000103 { "stop-on-sharedlibrary-events" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, stop when a shared library is loaded or unloaded." },
Jim Inghamacff8952013-05-02 00:27:30 +0000104 { "detach-keeps-stopped" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, detach will attempt to keep the process stopped." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000105 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
106};
107
108enum {
109 ePropertyDisableMemCache,
Greg Claytonc9d645d2012-10-18 22:40:37 +0000110 ePropertyExtraStartCommand,
Jim Ingham184e9812013-01-15 02:47:48 +0000111 ePropertyIgnoreBreakpointsInExpressions,
112 ePropertyUnwindOnErrorInExpressions,
Jim Ingham29950772013-01-26 02:19:28 +0000113 ePropertyPythonOSPluginPath,
Jim Inghamacff8952013-05-02 00:27:30 +0000114 ePropertyStopOnSharedLibraryEvents,
115 ePropertyDetachKeepsStopped
Greg Clayton67cc0632012-08-22 17:17:09 +0000116};
117
118ProcessProperties::ProcessProperties (bool is_global) :
119 Properties ()
120{
121 if (is_global)
122 {
123 m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process")));
124 m_collection_sp->Initialize(g_properties);
125 m_collection_sp->AppendProperty(ConstString("thread"),
Jim Ingham29950772013-01-26 02:19:28 +0000126 ConstString("Settings specific to threads."),
Greg Clayton67cc0632012-08-22 17:17:09 +0000127 true,
128 Thread::GetGlobalProperties()->GetValueProperties());
129 }
130 else
131 m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get()));
132}
133
134ProcessProperties::~ProcessProperties()
135{
136}
137
138bool
139ProcessProperties::GetDisableMemoryCache() const
140{
141 const uint32_t idx = ePropertyDisableMemCache;
142 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
143}
144
145Args
146ProcessProperties::GetExtraStartupCommands () const
147{
148 Args args;
149 const uint32_t idx = ePropertyExtraStartCommand;
150 m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args);
151 return args;
152}
153
154void
155ProcessProperties::SetExtraStartupCommands (const Args &args)
156{
157 const uint32_t idx = ePropertyExtraStartCommand;
158 m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args);
159}
160
Greg Claytonc9d645d2012-10-18 22:40:37 +0000161FileSpec
162ProcessProperties::GetPythonOSPluginPath () const
163{
164 const uint32_t idx = ePropertyPythonOSPluginPath;
165 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
166}
167
168void
169ProcessProperties::SetPythonOSPluginPath (const FileSpec &file)
170{
171 const uint32_t idx = ePropertyPythonOSPluginPath;
172 m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file);
173}
174
Jim Ingham184e9812013-01-15 02:47:48 +0000175
176bool
177ProcessProperties::GetIgnoreBreakpointsInExpressions () const
178{
179 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
180 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
181}
182
183void
184ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore)
185{
186 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
187 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
188}
189
190bool
191ProcessProperties::GetUnwindOnErrorInExpressions () const
192{
193 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
194 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
195}
196
197void
198ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore)
199{
200 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
201 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
202}
203
Jim Ingham29950772013-01-26 02:19:28 +0000204bool
205ProcessProperties::GetStopOnSharedLibraryEvents () const
206{
207 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
208 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
209}
210
211void
212ProcessProperties::SetStopOnSharedLibraryEvents (bool stop)
213{
214 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
215 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
216}
217
Jim Inghamacff8952013-05-02 00:27:30 +0000218bool
219ProcessProperties::GetDetachKeepsStopped () const
220{
221 const uint32_t idx = ePropertyDetachKeepsStopped;
222 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
223}
224
225void
226ProcessProperties::SetDetachKeepsStopped (bool stop)
227{
228 const uint32_t idx = ePropertyDetachKeepsStopped;
229 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
230}
231
Greg Clayton32e0a752011-03-30 18:16:51 +0000232void
Greg Clayton8b82f082011-04-12 05:54:46 +0000233ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000234{
235 const char *cstr;
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000236 if (m_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000237 s.Printf (" pid = %" PRIu64 "\n", m_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000238
239 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000240 s.Printf (" parent = %" PRIu64 "\n", m_parent_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000241
242 if (m_executable)
243 {
244 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
245 s.PutCString (" file = ");
246 m_executable.Dump(&s);
247 s.EOL();
248 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000249 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000250 if (argc > 0)
251 {
252 for (uint32_t i=0; i<argc; i++)
253 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000254 const char *arg = m_arguments.GetArgumentAtIndex(i);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000255 if (i < 10)
Greg Clayton8b82f082011-04-12 05:54:46 +0000256 s.Printf (" arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000257 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000258 s.Printf ("arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000259 }
260 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000261
262 const uint32_t envc = m_environment.GetArgumentCount();
263 if (envc > 0)
264 {
265 for (uint32_t i=0; i<envc; i++)
266 {
267 const char *env = m_environment.GetArgumentAtIndex(i);
268 if (i < 10)
269 s.Printf (" env[%u] = %s\n", i, env);
270 else
271 s.Printf ("env[%u] = %s\n", i, env);
272 }
273 }
274
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000275 if (m_arch.IsValid())
276 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
277
Greg Clayton8b82f082011-04-12 05:54:46 +0000278 if (m_uid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000279 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000280 cstr = platform->GetUserName (m_uid);
281 s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000282 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000283 if (m_gid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000284 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000285 cstr = platform->GetGroupName (m_gid);
286 s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000287 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000288 if (m_euid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000289 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000290 cstr = platform->GetUserName (m_euid);
291 s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000292 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000293 if (m_egid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000294 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000295 cstr = platform->GetGroupName (m_egid);
296 s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000297 }
298}
299
300void
Greg Clayton8b82f082011-04-12 05:54:46 +0000301ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose)
Greg Clayton32e0a752011-03-30 18:16:51 +0000302{
Greg Clayton8b82f082011-04-12 05:54:46 +0000303 const char *label;
304 if (show_args || verbose)
305 label = "ARGUMENTS";
306 else
307 label = "NAME";
308
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000309 if (verbose)
310 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000311 s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000312 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
313 }
314 else
315 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000316 s.Printf ("PID PARENT USER ARCH %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000317 s.PutCString ("====== ====== ========== ======= ============================\n");
318 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000319}
320
321void
Greg Clayton8b82f082011-04-12 05:54:46 +0000322ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000323{
324 if (m_pid != LLDB_INVALID_PROCESS_ID)
325 {
326 const char *cstr;
Daniel Malead01b2952012-11-29 21:49:15 +0000327 s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000328
Greg Clayton32e0a752011-03-30 18:16:51 +0000329
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000330 if (verbose)
331 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000332 cstr = platform->GetUserName (m_uid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000333 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
334 s.Printf ("%-10s ", cstr);
335 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000336 s.Printf ("%-10u ", m_uid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000337
Greg Clayton8b82f082011-04-12 05:54:46 +0000338 cstr = platform->GetGroupName (m_gid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000339 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
340 s.Printf ("%-10s ", cstr);
341 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000342 s.Printf ("%-10u ", m_gid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000343
Greg Clayton8b82f082011-04-12 05:54:46 +0000344 cstr = platform->GetUserName (m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000345 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
346 s.Printf ("%-10s ", cstr);
347 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000348 s.Printf ("%-10u ", m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000349
Greg Clayton8b82f082011-04-12 05:54:46 +0000350 cstr = platform->GetGroupName (m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000351 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
352 s.Printf ("%-10s ", cstr);
353 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000354 s.Printf ("%-10u ", m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000355 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
356 }
357 else
358 {
Jason Molendafd54b362011-09-20 21:44:10 +0000359 s.Printf ("%-10s %-7d %s ",
Greg Clayton8b82f082011-04-12 05:54:46 +0000360 platform->GetUserName (m_euid),
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000361 (int)m_arch.GetTriple().getArchName().size(),
362 m_arch.GetTriple().getArchName().data());
363 }
364
Greg Clayton8b82f082011-04-12 05:54:46 +0000365 if (verbose || show_args)
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000366 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000367 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000368 if (argc > 0)
369 {
370 for (uint32_t i=0; i<argc; i++)
371 {
372 if (i > 0)
373 s.PutChar (' ');
Greg Clayton8b82f082011-04-12 05:54:46 +0000374 s.PutCString (m_arguments.GetArgumentAtIndex(i));
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000375 }
376 }
377 }
378 else
379 {
380 s.PutCString (GetName());
381 }
382
383 s.EOL();
Greg Clayton32e0a752011-03-30 18:16:51 +0000384 }
385}
386
Greg Clayton8b82f082011-04-12 05:54:46 +0000387
388void
Greg Clayton45392552012-10-17 22:57:12 +0000389ProcessInfo::SetArguments (char const **argv, bool first_arg_is_executable)
Greg Clayton982c9762011-11-03 21:22:33 +0000390{
391 m_arguments.SetArguments (argv);
392
393 // Is the first argument the executable?
394 if (first_arg_is_executable)
395 {
396 const char *first_arg = m_arguments.GetArgumentAtIndex (0);
397 if (first_arg)
398 {
399 // Yes the first argument is an executable, set it as the executable
400 // in the launch options. Don't resolve the file path as the path
401 // could be a remote platform path
402 const bool resolve = false;
403 m_executable.SetFile(first_arg, resolve);
Greg Clayton982c9762011-11-03 21:22:33 +0000404 }
405 }
406}
407void
Greg Clayton45392552012-10-17 22:57:12 +0000408ProcessInfo::SetArguments (const Args& args, bool first_arg_is_executable)
Greg Clayton8b82f082011-04-12 05:54:46 +0000409{
410 // Copy all arguments
411 m_arguments = args;
412
413 // Is the first argument the executable?
414 if (first_arg_is_executable)
415 {
Greg Clayton982c9762011-11-03 21:22:33 +0000416 const char *first_arg = m_arguments.GetArgumentAtIndex (0);
Greg Clayton8b82f082011-04-12 05:54:46 +0000417 if (first_arg)
418 {
419 // Yes the first argument is an executable, set it as the executable
420 // in the launch options. Don't resolve the file path as the path
421 // could be a remote platform path
422 const bool resolve = false;
423 m_executable.SetFile(first_arg, resolve);
Greg Clayton8b82f082011-04-12 05:54:46 +0000424 }
425 }
426}
427
Greg Clayton1d885962011-11-08 02:43:13 +0000428void
Greg Claytonee95ed52011-11-17 22:14:31 +0000429ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty)
Greg Clayton1d885962011-11-08 02:43:13 +0000430{
431 // If notthing was specified, then check the process for any default
432 // settings that were set with "settings set"
433 if (m_file_actions.empty())
434 {
Greg Clayton1d885962011-11-08 02:43:13 +0000435 if (m_flags.Test(eLaunchFlagDisableSTDIO))
436 {
Greg Clayton9845a8d2012-03-06 04:01:04 +0000437 AppendSuppressFileAction (STDIN_FILENO , true, false);
438 AppendSuppressFileAction (STDOUT_FILENO, false, true);
439 AppendSuppressFileAction (STDERR_FILENO, false, true);
Greg Clayton1d885962011-11-08 02:43:13 +0000440 }
441 else
442 {
443 // Check for any values that might have gotten set with any of:
444 // (lldb) settings set target.input-path
445 // (lldb) settings set target.output-path
446 // (lldb) settings set target.error-path
Greg Clayton67cc0632012-08-22 17:17:09 +0000447 FileSpec in_path;
448 FileSpec out_path;
449 FileSpec err_path;
Greg Clayton1d885962011-11-08 02:43:13 +0000450 if (target)
451 {
Greg Clayton9845a8d2012-03-06 04:01:04 +0000452 in_path = target->GetStandardInputPath();
453 out_path = target->GetStandardOutputPath();
454 err_path = target->GetStandardErrorPath();
Greg Claytonee95ed52011-11-17 22:14:31 +0000455 }
456
Greg Clayton67cc0632012-08-22 17:17:09 +0000457 if (in_path || out_path || err_path)
458 {
459 char path[PATH_MAX];
460 if (in_path && in_path.GetPath(path, sizeof(path)))
461 AppendOpenFileAction(STDIN_FILENO, path, true, false);
462
463 if (out_path && out_path.GetPath(path, sizeof(path)))
464 AppendOpenFileAction(STDOUT_FILENO, path, false, true);
465
466 if (err_path && err_path.GetPath(path, sizeof(path)))
467 AppendOpenFileAction(STDERR_FILENO, path, false, true);
468 }
469 else if (default_to_use_pty)
Greg Claytonee95ed52011-11-17 22:14:31 +0000470 {
471 if (m_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, NULL, 0))
Greg Clayton1d885962011-11-08 02:43:13 +0000472 {
Greg Clayton67cc0632012-08-22 17:17:09 +0000473 const char *slave_path = m_pty.GetSlaveName (NULL, 0);
474 AppendOpenFileAction(STDIN_FILENO, slave_path, true, false);
475 AppendOpenFileAction(STDOUT_FILENO, slave_path, false, true);
476 AppendOpenFileAction(STDERR_FILENO, slave_path, false, true);
Greg Clayton1d885962011-11-08 02:43:13 +0000477 }
478 }
Greg Clayton1d885962011-11-08 02:43:13 +0000479 }
480 }
481}
482
Greg Clayton144f3a92011-11-15 03:53:30 +0000483
484bool
Greg Claytond1cf11a2012-04-14 01:42:46 +0000485ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error,
486 bool localhost,
487 bool will_debug,
488 bool first_arg_is_full_shell_command)
Greg Clayton144f3a92011-11-15 03:53:30 +0000489{
490 error.Clear();
491
492 if (GetFlags().Test (eLaunchFlagLaunchInShell))
493 {
494 const char *shell_executable = GetShell();
495 if (shell_executable)
496 {
497 char shell_resolved_path[PATH_MAX];
498
499 if (localhost)
500 {
501 FileSpec shell_filespec (shell_executable, true);
502
503 if (!shell_filespec.Exists())
504 {
505 // Resolve the path in case we just got "bash", "sh" or "tcsh"
506 if (!shell_filespec.ResolveExecutableLocation ())
507 {
508 error.SetErrorStringWithFormat("invalid shell path '%s'", shell_executable);
509 return false;
510 }
511 }
512 shell_filespec.GetPath (shell_resolved_path, sizeof(shell_resolved_path));
513 shell_executable = shell_resolved_path;
514 }
515
Greg Clayton45392552012-10-17 22:57:12 +0000516 const char **argv = GetArguments().GetConstArgumentVector ();
517 if (argv == NULL || argv[0] == NULL)
518 return false;
Greg Clayton144f3a92011-11-15 03:53:30 +0000519 Args shell_arguments;
520 std::string safe_arg;
521 shell_arguments.AppendArgument (shell_executable);
Greg Clayton144f3a92011-11-15 03:53:30 +0000522 shell_arguments.AppendArgument ("-c");
Greg Claytond1cf11a2012-04-14 01:42:46 +0000523 StreamString shell_command;
524 if (will_debug)
Greg Clayton144f3a92011-11-15 03:53:30 +0000525 {
Greg Clayton45392552012-10-17 22:57:12 +0000526 // Add a modified PATH environment variable in case argv[0]
527 // is a relative path
528 const char *argv0 = argv[0];
529 if (argv0 && (argv0[0] != '/' && argv0[0] != '~'))
530 {
531 // We have a relative path to our executable which may not work if
532 // we just try to run "a.out" (without it being converted to "./a.out")
533 const char *working_dir = GetWorkingDirectory();
Greg Clayton8938f8d2013-02-14 03:54:39 +0000534 // Be sure to put quotes around PATH's value in case any paths have spaces...
535 std::string new_path("PATH=\"");
Greg Clayton45392552012-10-17 22:57:12 +0000536 const size_t empty_path_len = new_path.size();
537
538 if (working_dir && working_dir[0])
539 {
540 new_path += working_dir;
541 }
542 else
543 {
544 char current_working_dir[PATH_MAX];
545 const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
546 if (cwd && cwd[0])
547 new_path += cwd;
548 }
549 const char *curr_path = getenv("PATH");
550 if (curr_path)
551 {
552 if (new_path.size() > empty_path_len)
553 new_path += ':';
554 new_path += curr_path;
555 }
Greg Clayton8938f8d2013-02-14 03:54:39 +0000556 new_path += "\" ";
Greg Clayton45392552012-10-17 22:57:12 +0000557 shell_command.PutCString(new_path.c_str());
558 }
559
Greg Claytond1cf11a2012-04-14 01:42:46 +0000560 shell_command.PutCString ("exec");
Greg Clayton45392552012-10-17 22:57:12 +0000561
Greg Clayton45392552012-10-17 22:57:12 +0000562 // Only Apple supports /usr/bin/arch being able to specify the architecture
Greg Claytond1cf11a2012-04-14 01:42:46 +0000563 if (GetArchitecture().IsValid())
564 {
565 shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
Greg Clayton45392552012-10-17 22:57:12 +0000566 // Set the resume count to 2:
Greg Claytond1cf11a2012-04-14 01:42:46 +0000567 // 1 - stop in shell
568 // 2 - stop in /usr/bin/arch
569 // 3 - then we will stop in our program
570 SetResumeCount(2);
571 }
572 else
573 {
Greg Clayton45392552012-10-17 22:57:12 +0000574 // Set the resume count to 1:
Greg Claytond1cf11a2012-04-14 01:42:46 +0000575 // 1 - stop in shell
576 // 2 - then we will stop in our program
577 SetResumeCount(1);
578 }
Greg Clayton144f3a92011-11-15 03:53:30 +0000579 }
Greg Clayton45392552012-10-17 22:57:12 +0000580
581 if (first_arg_is_full_shell_command)
Greg Clayton144f3a92011-11-15 03:53:30 +0000582 {
Greg Clayton45392552012-10-17 22:57:12 +0000583 // There should only be one argument that is the shell command itself to be used as is
584 if (argv[0] && !argv[1])
585 shell_command.Printf("%s", argv[0]);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000586 else
Greg Clayton45392552012-10-17 22:57:12 +0000587 return false;
Greg Clayton144f3a92011-11-15 03:53:30 +0000588 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000589 else
590 {
Greg Clayton45392552012-10-17 22:57:12 +0000591 for (size_t i=0; argv[i] != NULL; ++i)
592 {
593 const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
594 shell_command.Printf(" %s", arg);
595 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000596 }
Greg Clayton45392552012-10-17 22:57:12 +0000597 shell_arguments.AppendArgument (shell_command.GetString().c_str());
Greg Clayton144f3a92011-11-15 03:53:30 +0000598 m_executable.SetFile(shell_executable, false);
599 m_arguments = shell_arguments;
600 return true;
601 }
602 else
603 {
604 error.SetErrorString ("invalid shell path");
605 }
606 }
607 else
608 {
609 error.SetErrorString ("not launching in shell");
610 }
611 return false;
612}
613
614
Greg Clayton32e0a752011-03-30 18:16:51 +0000615bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000616ProcessLaunchInfo::FileAction::Open (int fd, const char *path, bool read, bool write)
617{
618 if ((read || write) && fd >= 0 && path && path[0])
619 {
620 m_action = eFileActionOpen;
621 m_fd = fd;
622 if (read && write)
Greg Clayton144f3a92011-11-15 03:53:30 +0000623 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
Greg Clayton8b82f082011-04-12 05:54:46 +0000624 else if (read)
Greg Clayton144f3a92011-11-15 03:53:30 +0000625 m_arg = O_NOCTTY | O_RDONLY;
Greg Clayton8b82f082011-04-12 05:54:46 +0000626 else
Greg Clayton144f3a92011-11-15 03:53:30 +0000627 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
Greg Clayton8b82f082011-04-12 05:54:46 +0000628 m_path.assign (path);
629 return true;
630 }
631 else
632 {
633 Clear();
634 }
635 return false;
636}
637
638bool
639ProcessLaunchInfo::FileAction::Close (int fd)
640{
641 Clear();
642 if (fd >= 0)
643 {
644 m_action = eFileActionClose;
645 m_fd = fd;
646 }
647 return m_fd >= 0;
648}
649
650
651bool
652ProcessLaunchInfo::FileAction::Duplicate (int fd, int dup_fd)
653{
654 Clear();
655 if (fd >= 0 && dup_fd >= 0)
656 {
657 m_action = eFileActionDuplicate;
658 m_fd = fd;
659 m_arg = dup_fd;
660 }
661 return m_fd >= 0;
662}
663
664
665
666bool
667ProcessLaunchInfo::FileAction::AddPosixSpawnFileAction (posix_spawn_file_actions_t *file_actions,
668 const FileAction *info,
669 Log *log,
670 Error& error)
671{
672 if (info == NULL)
673 return false;
674
675 switch (info->m_action)
676 {
677 case eFileActionNone:
678 error.Clear();
679 break;
680
681 case eFileActionClose:
682 if (info->m_fd == -1)
683 error.SetErrorString ("invalid fd for posix_spawn_file_actions_addclose(...)");
684 else
685 {
686 error.SetError (::posix_spawn_file_actions_addclose (file_actions, info->m_fd),
687 eErrorTypePOSIX);
688 if (log && (error.Fail() || log))
689 error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)",
690 file_actions, info->m_fd);
691 }
692 break;
693
694 case eFileActionDuplicate:
695 if (info->m_fd == -1)
696 error.SetErrorString ("invalid fd for posix_spawn_file_actions_adddup2(...)");
697 else if (info->m_arg == -1)
698 error.SetErrorString ("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
699 else
700 {
701 error.SetError (::posix_spawn_file_actions_adddup2 (file_actions, info->m_fd, info->m_arg),
702 eErrorTypePOSIX);
703 if (log && (error.Fail() || log))
704 error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
705 file_actions, info->m_fd, info->m_arg);
706 }
707 break;
708
709 case eFileActionOpen:
710 if (info->m_fd == -1)
711 error.SetErrorString ("invalid fd in posix_spawn_file_actions_addopen(...)");
712 else
713 {
714 int oflag = info->m_arg;
Greg Clayton144f3a92011-11-15 03:53:30 +0000715
Greg Clayton8b82f082011-04-12 05:54:46 +0000716 mode_t mode = 0;
717
Greg Clayton144f3a92011-11-15 03:53:30 +0000718 if (oflag & O_CREAT)
719 mode = 0640;
720
Greg Clayton8b82f082011-04-12 05:54:46 +0000721 error.SetError (::posix_spawn_file_actions_addopen (file_actions,
722 info->m_fd,
723 info->m_path.c_str(),
724 oflag,
725 mode),
726 eErrorTypePOSIX);
727 if (error.Fail() || log)
728 error.PutToLog(log,
729 "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)",
730 file_actions, info->m_fd, info->m_path.c_str(), oflag, mode);
731 }
732 break;
Greg Clayton8b82f082011-04-12 05:54:46 +0000733 }
734 return error.Success();
735}
736
737Error
Greg Claytonf6b8b582011-04-13 00:18:08 +0000738ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Clayton8b82f082011-04-12 05:54:46 +0000739{
740 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000741 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton8b82f082011-04-12 05:54:46 +0000742
743 switch (short_option)
744 {
745 case 's': // Stop at program entry point
746 launch_info.GetFlags().Set (eLaunchFlagStopAtEntry);
747 break;
748
Greg Clayton8b82f082011-04-12 05:54:46 +0000749 case 'i': // STDIN for read only
750 {
751 ProcessLaunchInfo::FileAction action;
Greg Clayton9845a8d2012-03-06 04:01:04 +0000752 if (action.Open (STDIN_FILENO, option_arg, true, false))
Greg Clayton8b82f082011-04-12 05:54:46 +0000753 launch_info.AppendFileAction (action);
754 }
755 break;
756
757 case 'o': // Open STDOUT for write only
758 {
759 ProcessLaunchInfo::FileAction action;
Greg Clayton9845a8d2012-03-06 04:01:04 +0000760 if (action.Open (STDOUT_FILENO, option_arg, false, true))
761 launch_info.AppendFileAction (action);
762 }
763 break;
764
765 case 'e': // STDERR for write only
766 {
767 ProcessLaunchInfo::FileAction action;
768 if (action.Open (STDERR_FILENO, option_arg, false, true))
Greg Clayton8b82f082011-04-12 05:54:46 +0000769 launch_info.AppendFileAction (action);
770 }
771 break;
772
Greg Clayton9845a8d2012-03-06 04:01:04 +0000773
Greg Clayton8b82f082011-04-12 05:54:46 +0000774 case 'p': // Process plug-in name
775 launch_info.SetProcessPluginName (option_arg);
776 break;
777
778 case 'n': // Disable STDIO
779 {
780 ProcessLaunchInfo::FileAction action;
Greg Clayton9845a8d2012-03-06 04:01:04 +0000781 if (action.Open (STDIN_FILENO, "/dev/null", true, false))
Greg Clayton8b82f082011-04-12 05:54:46 +0000782 launch_info.AppendFileAction (action);
Greg Clayton9845a8d2012-03-06 04:01:04 +0000783 if (action.Open (STDOUT_FILENO, "/dev/null", false, true))
Greg Clayton8b82f082011-04-12 05:54:46 +0000784 launch_info.AppendFileAction (action);
Greg Clayton9845a8d2012-03-06 04:01:04 +0000785 if (action.Open (STDERR_FILENO, "/dev/null", false, true))
Greg Clayton8b82f082011-04-12 05:54:46 +0000786 launch_info.AppendFileAction (action);
787 }
788 break;
789
790 case 'w':
791 launch_info.SetWorkingDirectory (option_arg);
792 break;
793
794 case 't': // Open process in new terminal window
795 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
796 break;
797
798 case 'a':
Greg Clayton70512312012-05-08 01:45:38 +0000799 if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get()))
800 launch_info.GetArchitecture().SetTriple (option_arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000801 break;
802
803 case 'A':
804 launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
805 break;
806
Greg Clayton982c9762011-11-03 21:22:33 +0000807 case 'c':
Greg Clayton144f3a92011-11-15 03:53:30 +0000808 if (option_arg && option_arg[0])
809 launch_info.SetShell (option_arg);
810 else
811 launch_info.SetShell ("/bin/bash");
Greg Clayton982c9762011-11-03 21:22:33 +0000812 break;
813
Greg Clayton8b82f082011-04-12 05:54:46 +0000814 case 'v':
815 launch_info.GetEnvironmentEntries().AppendArgument(option_arg);
816 break;
817
818 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000819 error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option);
Greg Clayton8b82f082011-04-12 05:54:46 +0000820 break;
821
822 }
823 return error;
824}
825
826OptionDefinition
827ProcessLaunchCommandOptions::g_option_table[] =
828{
829{ 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."},
830{ LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', no_argument, NULL, 0, eArgTypeNone, "Disable address space layout randomization when launching a process."},
831{ LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
Sean Callanan31542552012-10-24 01:12:14 +0000832{ LLDB_OPT_SET_ALL, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000833{ LLDB_OPT_SET_ALL, false, "arch", 'a', required_argument, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."},
834{ 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."},
Sean Callanan31542552012-10-24 01:12:14 +0000835{ LLDB_OPT_SET_ALL, false, "shell", 'c', optional_argument, NULL, 0, eArgTypeFilename, "Run the process in a shell (not supported on all platforms)."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000836
Sean Callanan31542552012-10-24 01:12:14 +0000837{ LLDB_OPT_SET_1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."},
838{ LLDB_OPT_SET_1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."},
839{ LLDB_OPT_SET_1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypeFilename, "Redirect stderr for the process to <filename>."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000840
841{ LLDB_OPT_SET_2 , false, "tty", 't', no_argument, NULL, 0, eArgTypeNone, "Start the process in a terminal (not supported on all platforms)."},
842
843{ 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."},
844
845{ 0 , false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
846};
847
848
849
850bool
851ProcessInstanceInfoMatch::NameMatches (const char *process_name) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000852{
853 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
854 return true;
855 const char *match_name = m_match_info.GetName();
856 if (!match_name)
857 return true;
858
859 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
860}
861
862bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000863ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000864{
865 if (!NameMatches (proc_info.GetName()))
866 return false;
867
868 if (m_match_info.ProcessIDIsValid() &&
869 m_match_info.GetProcessID() != proc_info.GetProcessID())
870 return false;
871
872 if (m_match_info.ParentProcessIDIsValid() &&
873 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
874 return false;
875
Greg Clayton8b82f082011-04-12 05:54:46 +0000876 if (m_match_info.UserIDIsValid () &&
877 m_match_info.GetUserID() != proc_info.GetUserID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000878 return false;
879
Greg Clayton8b82f082011-04-12 05:54:46 +0000880 if (m_match_info.GroupIDIsValid () &&
881 m_match_info.GetGroupID() != proc_info.GetGroupID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000882 return false;
883
884 if (m_match_info.EffectiveUserIDIsValid () &&
885 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
886 return false;
887
888 if (m_match_info.EffectiveGroupIDIsValid () &&
889 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
890 return false;
891
892 if (m_match_info.GetArchitecture().IsValid() &&
Sean Callananbf4b7be2012-12-13 22:07:14 +0000893 !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture()))
Greg Clayton32e0a752011-03-30 18:16:51 +0000894 return false;
895 return true;
896}
897
898bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000899ProcessInstanceInfoMatch::MatchAllProcesses () const
Greg Clayton32e0a752011-03-30 18:16:51 +0000900{
901 if (m_name_match_type != eNameMatchIgnore)
902 return false;
903
904 if (m_match_info.ProcessIDIsValid())
905 return false;
906
907 if (m_match_info.ParentProcessIDIsValid())
908 return false;
909
Greg Clayton8b82f082011-04-12 05:54:46 +0000910 if (m_match_info.UserIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000911 return false;
912
Greg Clayton8b82f082011-04-12 05:54:46 +0000913 if (m_match_info.GroupIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000914 return false;
915
916 if (m_match_info.EffectiveUserIDIsValid ())
917 return false;
918
919 if (m_match_info.EffectiveGroupIDIsValid ())
920 return false;
921
922 if (m_match_info.GetArchitecture().IsValid())
923 return false;
924
925 if (m_match_all_users)
926 return false;
927
928 return true;
929
930}
931
932void
Greg Clayton8b82f082011-04-12 05:54:46 +0000933ProcessInstanceInfoMatch::Clear()
Greg Clayton32e0a752011-03-30 18:16:51 +0000934{
935 m_match_info.Clear();
936 m_name_match_type = eNameMatchIgnore;
937 m_match_all_users = false;
938}
Greg Clayton58be07b2011-01-07 06:08:19 +0000939
Greg Claytonc3776bf2012-02-09 06:16:32 +0000940ProcessSP
941Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942{
Greg Clayton949e8222013-01-16 17:29:04 +0000943 static uint32_t g_process_unique_id = 0;
944
Greg Claytonc3776bf2012-02-09 06:16:32 +0000945 ProcessSP process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946 ProcessCreateInstance create_callback = NULL;
947 if (plugin_name)
948 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000949 ConstString const_plugin_name(plugin_name);
950 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951 if (create_callback)
952 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000953 process_sp = create_callback(target, listener, crash_file_path);
954 if (process_sp)
955 {
Greg Clayton949e8222013-01-16 17:29:04 +0000956 if (process_sp->CanDebug(target, true))
957 {
958 process_sp->m_process_unique_id = ++g_process_unique_id;
959 }
960 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000961 process_sp.reset();
962 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 }
964 }
965 else
966 {
Greg Claytonc982c762010-07-09 20:39:50 +0000967 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000969 process_sp = create_callback(target, listener, crash_file_path);
970 if (process_sp)
971 {
Greg Clayton949e8222013-01-16 17:29:04 +0000972 if (process_sp->CanDebug(target, false))
973 {
974 process_sp->m_process_unique_id = ++g_process_unique_id;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000975 break;
Greg Clayton949e8222013-01-16 17:29:04 +0000976 }
977 else
978 process_sp.reset();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000979 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980 }
981 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000982 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983}
984
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000985ConstString &
986Process::GetStaticBroadcasterClass ()
987{
988 static ConstString class_name ("lldb.process");
989 return class_name;
990}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991
992//----------------------------------------------------------------------
993// Process constructor
994//----------------------------------------------------------------------
995Process::Process(Target &target, Listener &listener) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000996 ProcessProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997 UserID (LLDB_INVALID_PROCESS_ID),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000998 Broadcaster (&(target.GetDebugger()), "lldb.process"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999 m_target (target),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000 m_public_state (eStateUnloaded),
1001 m_private_state (eStateUnloaded),
Jim Ingham4bddaeb2012-02-16 06:50:00 +00001002 m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"),
1003 m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 m_private_state_listener ("lldb.process.internal_state_listener"),
1005 m_private_state_control_wait(),
1006 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
Jim Ingham4b536182011-08-09 02:12:22 +00001007 m_mod_id (),
Greg Clayton949e8222013-01-16 17:29:04 +00001008 m_process_unique_id(0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009 m_thread_index_id (0),
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001010 m_thread_id_to_index_id_map (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 m_exit_status (-1),
1012 m_exit_string (),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001013 m_thread_mutex (Mutex::eMutexTypeRecursive),
1014 m_thread_list_real (this),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 m_thread_list (this),
1016 m_notifications (),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001017 m_image_tokens (),
1018 m_listener (listener),
1019 m_breakpoint_site_list (),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001020 m_dynamic_checkers_ap (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001021 m_unix_signals (),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001022 m_abi_sp (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001023 m_process_input_reader (),
Greg Clayton3e06bd92011-01-09 21:07:35 +00001024 m_stdio_communication ("process.stdio"),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001025 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton58be07b2011-01-07 06:08:19 +00001026 m_stdout_data (),
Greg Clayton93e86192011-11-13 04:45:22 +00001027 m_stderr_data (),
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001028 m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive),
1029 m_profile_data (),
Greg Claytond495c532011-05-17 03:37:42 +00001030 m_memory_cache (*this),
1031 m_allocated_memory_cache (*this),
Greg Claytone24c4ac2011-11-17 04:46:02 +00001032 m_should_detach (false),
Sean Callanan90539452011-09-20 23:01:51 +00001033 m_next_event_action_ap(),
Greg Clayton96249852013-04-18 16:57:27 +00001034 m_public_run_lock (),
Greg Clayton96249852013-04-18 16:57:27 +00001035 m_private_run_lock (),
Jim Inghamaacc3182012-06-06 00:29:30 +00001036 m_currently_handling_event(false),
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001037 m_finalize_called(false),
Greg Claytonf9b57b92013-05-10 23:48:10 +00001038 m_clear_thread_plans_on_stop (false),
Jim Ingham0161b492013-02-09 01:29:05 +00001039 m_last_broadcast_state (eStateInvalid),
Jason Molenda69b6b632013-03-05 03:33:59 +00001040 m_destroy_in_process (false),
1041 m_can_jit(eCanJITDontKnow)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042{
Jim Ingham4bddaeb2012-02-16 06:50:00 +00001043 CheckInWithManager ();
Caroline Tice1559a462010-09-27 00:30:10 +00001044
Greg Clayton5160ce52013-03-27 23:08:40 +00001045 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046 if (log)
1047 log->Printf ("%p Process::Process()", this);
1048
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001049 SetEventName (eBroadcastBitStateChanged, "state-changed");
1050 SetEventName (eBroadcastBitInterrupt, "interrupt");
1051 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
1052 SetEventName (eBroadcastBitSTDERR, "stderr-available");
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001053 SetEventName (eBroadcastBitProfileData, "profile-data-available");
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001054
Greg Clayton35a4cc52012-10-29 20:52:08 +00001055 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" );
1056 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" );
1057 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume");
1058
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059 listener.StartListeningForEvents (this,
1060 eBroadcastBitStateChanged |
1061 eBroadcastBitInterrupt |
1062 eBroadcastBitSTDOUT |
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001063 eBroadcastBitSTDERR |
1064 eBroadcastBitProfileData);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065
1066 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +00001067 eBroadcastBitStateChanged |
1068 eBroadcastBitInterrupt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069
1070 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
1071 eBroadcastInternalStateControlStop |
1072 eBroadcastInternalStateControlPause |
1073 eBroadcastInternalStateControlResume);
1074}
1075
1076//----------------------------------------------------------------------
1077// Destructor
1078//----------------------------------------------------------------------
1079Process::~Process()
1080{
Greg Clayton5160ce52013-03-27 23:08:40 +00001081 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082 if (log)
1083 log->Printf ("%p Process::~Process()", this);
1084 StopPrivateStateThread();
1085}
1086
Greg Clayton67cc0632012-08-22 17:17:09 +00001087const ProcessPropertiesSP &
1088Process::GetGlobalProperties()
1089{
1090 static ProcessPropertiesSP g_settings_sp;
1091 if (!g_settings_sp)
1092 g_settings_sp.reset (new ProcessProperties (true));
1093 return g_settings_sp;
1094}
1095
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096void
1097Process::Finalize()
1098{
Greg Claytone24c4ac2011-11-17 04:46:02 +00001099 switch (GetPrivateState())
1100 {
1101 case eStateConnected:
1102 case eStateAttaching:
1103 case eStateLaunching:
1104 case eStateStopped:
1105 case eStateRunning:
1106 case eStateStepping:
1107 case eStateCrashed:
1108 case eStateSuspended:
1109 if (GetShouldDetach())
Jim Inghamacff8952013-05-02 00:27:30 +00001110 {
1111 // FIXME: This will have to be a process setting:
1112 bool keep_stopped = false;
1113 Detach(keep_stopped);
1114 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001115 else
1116 Destroy();
1117 break;
1118
1119 case eStateInvalid:
1120 case eStateUnloaded:
1121 case eStateDetached:
1122 case eStateExited:
1123 break;
1124 }
1125
Greg Clayton1ed54f52011-10-01 00:45:15 +00001126 // Clear our broadcaster before we proceed with destroying
1127 Broadcaster::Clear();
1128
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 // Do any cleanup needed prior to being destructed... Subclasses
1130 // that override this method should call this superclass method as well.
Jim Inghamd0a3e122011-02-16 17:54:55 +00001131
1132 // We need to destroy the loader before the derived Process class gets destroyed
1133 // since it is very likely that undoing the loader will require access to the real process.
Greg Clayton894f82f2012-01-20 23:08:34 +00001134 m_dynamic_checkers_ap.reset();
1135 m_abi_sp.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001136 m_os_ap.reset();
Greg Clayton894f82f2012-01-20 23:08:34 +00001137 m_dyld_ap.reset();
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001138 m_thread_list_real.Destroy();
Greg Claytone1cd1be2012-01-29 20:56:30 +00001139 m_thread_list.Destroy();
Greg Clayton894f82f2012-01-20 23:08:34 +00001140 std::vector<Notifications> empty_notifications;
1141 m_notifications.swap(empty_notifications);
1142 m_image_tokens.clear();
1143 m_memory_cache.Clear();
1144 m_allocated_memory_cache.Clear();
1145 m_language_runtimes.clear();
1146 m_next_event_action_ap.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +00001147//#ifdef LLDB_CONFIGURATION_DEBUG
1148// StreamFile s(stdout, false);
1149// EventSP event_sp;
1150// while (m_private_state_listener.GetNextEvent(event_sp))
1151// {
1152// event_sp->Dump (&s);
1153// s.EOL();
1154// }
1155//#endif
1156 // We have to be very careful here as the m_private_state_listener might
1157 // contain events that have ProcessSP values in them which can keep this
1158 // process around forever. These events need to be cleared out.
1159 m_private_state_listener.Clear();
Greg Claytonaa49c832013-05-03 22:25:56 +00001160 m_public_run_lock.WriteTryLock(); // This will do nothing if already locked
Greg Clayton96249852013-04-18 16:57:27 +00001161 m_public_run_lock.WriteUnlock();
Greg Claytonaa49c832013-05-03 22:25:56 +00001162 m_private_run_lock.WriteTryLock(); // This will do nothing if already locked
Greg Clayton96249852013-04-18 16:57:27 +00001163 m_private_run_lock.WriteUnlock();
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001164 m_finalize_called = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165}
1166
1167void
1168Process::RegisterNotificationCallbacks (const Notifications& callbacks)
1169{
1170 m_notifications.push_back(callbacks);
1171 if (callbacks.initialize != NULL)
1172 callbacks.initialize (callbacks.baton, this);
1173}
1174
1175bool
1176Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
1177{
1178 std::vector<Notifications>::iterator pos, end = m_notifications.end();
1179 for (pos = m_notifications.begin(); pos != end; ++pos)
1180 {
1181 if (pos->baton == callbacks.baton &&
1182 pos->initialize == callbacks.initialize &&
1183 pos->process_state_changed == callbacks.process_state_changed)
1184 {
1185 m_notifications.erase(pos);
1186 return true;
1187 }
1188 }
1189 return false;
1190}
1191
1192void
1193Process::SynchronouslyNotifyStateChanged (StateType state)
1194{
1195 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
1196 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
1197 {
1198 if (notification_pos->process_state_changed)
1199 notification_pos->process_state_changed (notification_pos->baton, this, state);
1200 }
1201}
1202
1203// FIXME: We need to do some work on events before the general Listener sees them.
1204// For instance if we are continuing from a breakpoint, we need to ensure that we do
1205// the little "insert real insn, step & stop" trick. But we can't do that when the
1206// event is delivered by the broadcaster - since that is done on the thread that is
1207// waiting for new events, so if we needed more than one event for our handling, we would
1208// stall. So instead we do it when we fetch the event off of the queue.
1209//
1210
1211StateType
1212Process::GetNextEvent (EventSP &event_sp)
1213{
1214 StateType state = eStateInvalid;
1215
1216 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
1217 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
1218
1219 return state;
1220}
1221
1222
1223StateType
Greg Clayton85fb1b92012-09-11 02:33:37 +00001224Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225{
Jim Ingham4b536182011-08-09 02:12:22 +00001226 // We can't just wait for a "stopped" event, because the stopped event may have restarted the target.
1227 // We have to actually check each event, and in the case of a stopped event check the restarted flag
1228 // on the event.
Greg Clayton85fb1b92012-09-11 02:33:37 +00001229 if (event_sp_ptr)
1230 event_sp_ptr->reset();
Jim Ingham4b536182011-08-09 02:12:22 +00001231 StateType state = GetState();
1232 // If we are exited or detached, we won't ever get back to any
1233 // other valid state...
1234 if (state == eStateDetached || state == eStateExited)
1235 return state;
1236
1237 while (state != eStateInvalid)
1238 {
Greg Clayton85fb1b92012-09-11 02:33:37 +00001239 EventSP event_sp;
Jim Ingham4b536182011-08-09 02:12:22 +00001240 state = WaitForStateChangedEvents (timeout, event_sp);
Greg Clayton85fb1b92012-09-11 02:33:37 +00001241 if (event_sp_ptr && event_sp)
1242 *event_sp_ptr = event_sp;
1243
Jim Ingham4b536182011-08-09 02:12:22 +00001244 switch (state)
1245 {
1246 case eStateCrashed:
1247 case eStateDetached:
1248 case eStateExited:
1249 case eStateUnloaded:
1250 return state;
1251 case eStateStopped:
1252 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
1253 continue;
1254 else
1255 return state;
1256 default:
1257 continue;
1258 }
1259 }
1260 return state;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261}
1262
1263
1264StateType
1265Process::WaitForState
1266(
1267 const TimeValue *timeout,
1268 const StateType *match_states, const uint32_t num_match_states
1269)
1270{
1271 EventSP event_sp;
1272 uint32_t i;
Greg Clayton05faeb72010-10-07 04:19:01 +00001273 StateType state = GetState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 while (state != eStateInvalid)
1275 {
Greg Clayton05faeb72010-10-07 04:19:01 +00001276 // If we are exited or detached, we won't ever get back to any
1277 // other valid state...
1278 if (state == eStateDetached || state == eStateExited)
1279 return state;
1280
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281 state = WaitForStateChangedEvents (timeout, event_sp);
1282
1283 for (i=0; i<num_match_states; ++i)
1284 {
1285 if (match_states[i] == state)
1286 return state;
1287 }
1288 }
1289 return state;
1290}
1291
Jim Ingham30f9b212010-10-11 23:53:14 +00001292bool
1293Process::HijackProcessEvents (Listener *listener)
1294{
1295 if (listener != NULL)
1296 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001297 return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham30f9b212010-10-11 23:53:14 +00001298 }
1299 else
1300 return false;
1301}
1302
1303void
1304Process::RestoreProcessEvents ()
1305{
1306 RestoreBroadcaster();
1307}
1308
Jim Ingham0f16e732011-02-08 05:20:59 +00001309bool
1310Process::HijackPrivateProcessEvents (Listener *listener)
1311{
1312 if (listener != NULL)
1313 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001314 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham0f16e732011-02-08 05:20:59 +00001315 }
1316 else
1317 return false;
1318}
1319
1320void
1321Process::RestorePrivateProcessEvents ()
1322{
1323 m_private_state_broadcaster.RestoreBroadcaster();
1324}
1325
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326StateType
1327Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp)
1328{
Greg Clayton5160ce52013-03-27 23:08:40 +00001329 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330
1331 if (log)
1332 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
1333
1334 StateType state = eStateInvalid;
Greg Clayton3fcbed62010-10-19 03:25:40 +00001335 if (m_listener.WaitForEventForBroadcasterWithType (timeout,
1336 this,
Jim Inghamcfc09352012-07-27 23:57:19 +00001337 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
Greg Clayton3fcbed62010-10-19 03:25:40 +00001338 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001339 {
1340 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1341 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1342 else if (log)
1343 log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__);
1344 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345
1346 if (log)
1347 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
1348 __FUNCTION__,
1349 timeout,
1350 StateAsCString(state));
1351 return state;
1352}
1353
1354Event *
1355Process::PeekAtStateChangedEvents ()
1356{
Greg Clayton5160ce52013-03-27 23:08:40 +00001357 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358
1359 if (log)
1360 log->Printf ("Process::%s...", __FUNCTION__);
1361
1362 Event *event_ptr;
Greg Clayton3fcbed62010-10-19 03:25:40 +00001363 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
1364 eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 if (log)
1366 {
1367 if (event_ptr)
1368 {
1369 log->Printf ("Process::%s (event_ptr) => %s",
1370 __FUNCTION__,
1371 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
1372 }
1373 else
1374 {
1375 log->Printf ("Process::%s no events found",
1376 __FUNCTION__);
1377 }
1378 }
1379 return event_ptr;
1380}
1381
1382StateType
1383Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
1384{
Greg Clayton5160ce52013-03-27 23:08:40 +00001385 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386
1387 if (log)
1388 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
1389
1390 StateType state = eStateInvalid;
Greg Clayton6779606a2011-01-22 23:43:18 +00001391 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
1392 &m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +00001393 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
Greg Clayton6779606a2011-01-22 23:43:18 +00001394 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001395 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1396 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397
1398 // This is a bit of a hack, but when we wait here we could very well return
1399 // to the command-line, and that could disable the log, which would render the
1400 // log we got above invalid.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401 if (log)
Greg Clayton6779606a2011-01-22 23:43:18 +00001402 {
1403 if (state == eStateInvalid)
1404 log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout);
1405 else
1406 log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state));
1407 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 return state;
1409}
1410
1411bool
1412Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
1413{
Greg Clayton5160ce52013-03-27 23:08:40 +00001414 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001415
1416 if (log)
1417 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout);
1418
1419 if (control_only)
1420 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
1421 else
1422 return m_private_state_listener.WaitForEvent(timeout, event_sp);
1423}
1424
1425bool
1426Process::IsRunning () const
1427{
1428 return StateIsRunningState (m_public_state.GetValue());
1429}
1430
1431int
1432Process::GetExitStatus ()
1433{
1434 if (m_public_state.GetValue() == eStateExited)
1435 return m_exit_status;
1436 return -1;
1437}
1438
Greg Clayton85851dd2010-12-04 00:10:17 +00001439
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440const char *
1441Process::GetExitDescription ()
1442{
1443 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
1444 return m_exit_string.c_str();
1445 return NULL;
1446}
1447
Greg Clayton6779606a2011-01-22 23:43:18 +00001448bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001449Process::SetExitStatus (int status, const char *cstr)
1450{
Greg Clayton5160ce52013-03-27 23:08:40 +00001451 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Greg Clayton414f5d32011-01-25 02:58:48 +00001452 if (log)
1453 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
1454 status, status,
1455 cstr ? "\"" : "",
1456 cstr ? cstr : "NULL",
1457 cstr ? "\"" : "");
1458
Greg Clayton6779606a2011-01-22 23:43:18 +00001459 // We were already in the exited state
1460 if (m_private_state.GetValue() == eStateExited)
Greg Clayton414f5d32011-01-25 02:58:48 +00001461 {
Greg Clayton385d6032011-01-26 23:47:29 +00001462 if (log)
1463 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton6779606a2011-01-22 23:43:18 +00001464 return false;
Greg Clayton414f5d32011-01-25 02:58:48 +00001465 }
Greg Clayton6779606a2011-01-22 23:43:18 +00001466
1467 m_exit_status = status;
1468 if (cstr)
1469 m_exit_string = cstr;
1470 else
1471 m_exit_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472
Greg Clayton6779606a2011-01-22 23:43:18 +00001473 DidExit ();
Greg Clayton10177aa2010-12-08 05:08:21 +00001474
Greg Clayton6779606a2011-01-22 23:43:18 +00001475 SetPrivateState (eStateExited);
1476 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001477}
1478
1479// This static callback can be used to watch for local child processes on
1480// the current host. The the child process exits, the process will be
1481// found in the global target list (we want to be completely sure that the
1482// lldb_private::Process doesn't go away before we can deliver the signal.
1483bool
Greg Claytone4e45922011-11-16 05:37:56 +00001484Process::SetProcessExitStatus (void *callback_baton,
1485 lldb::pid_t pid,
1486 bool exited,
1487 int signo, // Zero for no signal
1488 int exit_status // Exit value of process if signal is zero
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489)
1490{
Greg Clayton5160ce52013-03-27 23:08:40 +00001491 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytone4e45922011-11-16 05:37:56 +00001492 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001493 log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n",
Greg Claytone4e45922011-11-16 05:37:56 +00001494 callback_baton,
1495 pid,
1496 exited,
1497 signo,
1498 exit_status);
1499
1500 if (exited)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501 {
Greg Clayton66111032010-06-23 01:19:29 +00001502 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 if (target_sp)
1504 {
1505 ProcessSP process_sp (target_sp->GetProcessSP());
1506 if (process_sp)
1507 {
1508 const char *signal_cstr = NULL;
1509 if (signo)
1510 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
1511
1512 process_sp->SetExitStatus (exit_status, signal_cstr);
1513 }
1514 }
1515 return true;
1516 }
1517 return false;
1518}
1519
1520
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001521void
1522Process::UpdateThreadListIfNeeded ()
1523{
1524 const uint32_t stop_id = GetStopID();
1525 if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID())
1526 {
Greg Clayton2637f822011-11-17 01:23:07 +00001527 const StateType state = GetPrivateState();
1528 if (StateIsStoppedState (state, true))
1529 {
1530 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytone24c4ac2011-11-17 04:46:02 +00001531 // m_thread_list does have its own mutex, but we need to
1532 // hold onto the mutex between the call to UpdateThreadList(...)
1533 // and the os->UpdateThreadList(...) so it doesn't change on us
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001534 ThreadList &old_thread_list = m_thread_list;
1535 ThreadList real_thread_list(this);
Greg Clayton2637f822011-11-17 01:23:07 +00001536 ThreadList new_thread_list(this);
1537 // Always update the thread list with the protocol specific
Greg Clayton9fc13552012-04-10 00:18:59 +00001538 // thread list, but only update if "true" is returned
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001539 if (UpdateThreadList (m_thread_list_real, real_thread_list))
Greg Clayton9fc13552012-04-10 00:18:59 +00001540 {
Jim Ingham09437922013-03-01 20:04:25 +00001541 // Don't call into the OperatingSystem to update the thread list if we are shutting down, since
1542 // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is
1543 // shutting us down, causing a deadlock.
1544 if (!m_destroy_in_process)
1545 {
1546 OperatingSystem *os = GetOperatingSystem ();
1547 if (os)
Greg Claytonb3ae8762013-04-12 20:07:46 +00001548 {
1549 // Clear any old backing threads where memory threads might have been
1550 // backed by actual threads from the lldb_private::Process subclass
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001551 size_t num_old_threads = old_thread_list.GetSize(false);
Greg Claytonb3ae8762013-04-12 20:07:46 +00001552 for (size_t i=0; i<num_old_threads; ++i)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001553 old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread();
Greg Claytonb3ae8762013-04-12 20:07:46 +00001554
1555 // Now let the OperatingSystem plug-in update the thread list
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001556 os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in
1557 real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass
1558 new_thread_list); // The new thread list that we will show to the user that gets filled in
Greg Claytonb3ae8762013-04-12 20:07:46 +00001559 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001560 else
1561 {
1562 // No OS plug-in, the new thread list is the same as the real thread list
1563 new_thread_list = real_thread_list;
1564 }
Jim Ingham09437922013-03-01 20:04:25 +00001565 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001566 m_thread_list.Update (new_thread_list);
1567 m_thread_list.SetStopID (stop_id);
Greg Clayton9fc13552012-04-10 00:18:59 +00001568 }
Greg Clayton2637f822011-11-17 01:23:07 +00001569 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001570 }
1571}
1572
Greg Claytona4d87472013-01-18 23:41:08 +00001573ThreadSP
1574Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context)
1575{
1576 OperatingSystem *os = GetOperatingSystem ();
1577 if (os)
1578 return os->CreateThread(tid, context);
1579 return ThreadSP();
1580}
1581
1582
1583
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001584// This is obsoleted. Staged removal for Xcode.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001585uint32_t
1586Process::GetNextThreadIndexID ()
1587{
1588 return ++m_thread_index_id;
1589}
1590
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001591uint32_t
1592Process::GetNextThreadIndexID (uint64_t thread_id)
1593{
1594 return AssignIndexIDToThread(thread_id);
1595}
1596
1597bool
1598Process::HasAssignedIndexIDToThread(uint64_t thread_id)
1599{
1600 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1601 if (iterator == m_thread_id_to_index_id_map.end())
1602 {
1603 return false;
1604 }
1605 else
1606 {
1607 return true;
1608 }
1609}
1610
1611uint32_t
1612Process::AssignIndexIDToThread(uint64_t thread_id)
1613{
1614 uint32_t result = 0;
1615 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1616 if (iterator == m_thread_id_to_index_id_map.end())
1617 {
1618 result = ++m_thread_index_id;
1619 m_thread_id_to_index_id_map[thread_id] = result;
1620 }
1621 else
1622 {
1623 result = iterator->second;
1624 }
1625
1626 return result;
1627}
1628
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001629StateType
1630Process::GetState()
1631{
1632 // If any other threads access this we will need a mutex for it
1633 return m_public_state.GetValue ();
1634}
1635
1636void
Jim Ingham221d51c2013-05-08 00:35:16 +00001637Process::SetPublicState (StateType new_state, bool restarted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638{
Greg Clayton5160ce52013-03-27 23:08:40 +00001639 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001640 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +00001641 log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001642 const StateType old_state = m_public_state.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001643 m_public_state.SetValue (new_state);
Jim Ingham3b8285d2012-04-19 01:40:33 +00001644
1645 // On the transition from Run to Stopped, we unlock the writer end of the
1646 // run lock. The lock gets locked in Resume, which is the public API
1647 // to tell the program to run.
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001648 if (!IsHijackedForEvent(eBroadcastBitStateChanged))
1649 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001650 if (new_state == eStateDetached)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001651 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001652 if (log)
1653 log->Printf("Process::SetPublicState (%s) -- unlocking run lock for detach", StateAsCString(new_state));
Greg Clayton96249852013-04-18 16:57:27 +00001654 m_public_run_lock.WriteUnlock();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001655 }
1656 else
1657 {
1658 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1659 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
Jim Ingham221d51c2013-05-08 00:35:16 +00001660 if ((old_state_is_stopped != new_state_is_stopped))
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001661 {
Jim Ingham221d51c2013-05-08 00:35:16 +00001662 if (new_state_is_stopped && !restarted)
Sean Callanan8b0737f2012-06-02 01:16:20 +00001663 {
1664 if (log)
1665 log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state));
Greg Clayton96249852013-04-18 16:57:27 +00001666 m_public_run_lock.WriteUnlock();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001667 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001668 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001669 }
1670 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671}
1672
Jim Ingham3b8285d2012-04-19 01:40:33 +00001673Error
1674Process::Resume ()
1675{
Greg Clayton5160ce52013-03-27 23:08:40 +00001676 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Jim Ingham3b8285d2012-04-19 01:40:33 +00001677 if (log)
1678 log->Printf("Process::Resume -- locking run lock");
Greg Clayton96249852013-04-18 16:57:27 +00001679 if (!m_public_run_lock.WriteTryLock())
Jim Ingham3b8285d2012-04-19 01:40:33 +00001680 {
1681 Error error("Resume request failed - process still running.");
1682 if (log)
1683 log->Printf ("Process::Resume: -- WriteTryLock failed, not resuming.");
1684 return error;
1685 }
1686 return PrivateResume();
1687}
1688
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689StateType
1690Process::GetPrivateState ()
1691{
1692 return m_private_state.GetValue();
1693}
1694
1695void
1696Process::SetPrivateState (StateType new_state)
1697{
Greg Clayton5160ce52013-03-27 23:08:40 +00001698 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 bool state_changed = false;
1700
1701 if (log)
1702 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
1703
Andrew Kaylor29d65742013-05-10 17:19:04 +00001704 Mutex::Locker thread_locker(m_thread_list.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001705 Mutex::Locker locker(m_private_state.GetMutex());
1706
1707 const StateType old_state = m_private_state.GetValueNoLock ();
1708 state_changed = old_state != new_state;
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001709 // This code is left commented out in case we ever need to control
1710 // the private process state with another run lock. Right now it doesn't
1711 // seem like we need to do this, but if we ever do, we can uncomment and
1712 // use this code.
Greg Claytonaa49c832013-05-03 22:25:56 +00001713 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1714 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
1715 if (old_state_is_stopped != new_state_is_stopped)
1716 {
1717 if (new_state_is_stopped)
1718 m_private_run_lock.WriteUnlock();
1719 else
1720 m_private_run_lock.WriteLock();
1721 }
Greg Claytonaa49c832013-05-03 22:25:56 +00001722
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723 if (state_changed)
1724 {
1725 m_private_state.SetValueNoLock (new_state);
Greg Clayton2637f822011-11-17 01:23:07 +00001726 if (StateIsStoppedState(new_state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727 {
Andrew Kaylor29d65742013-05-10 17:19:04 +00001728 // Note, this currently assumes that all threads in the list
1729 // stop when the process stops. In the future we will want to
1730 // support a debugging model where some threads continue to run
1731 // while others are stopped. When that happens we will either need
1732 // a way for the thread list to identify which threads are stopping
1733 // or create a special thread list containing only threads which
1734 // actually stopped.
1735 //
1736 // The process plugin is responsible for managing the actual
1737 // behavior of the threads and should have stopped any threads
1738 // that are going to stop before we get here.
1739 m_thread_list.DidStop();
1740
Jim Ingham4b536182011-08-09 02:12:22 +00001741 m_mod_id.BumpStopID();
Greg Clayton58be07b2011-01-07 06:08:19 +00001742 m_memory_cache.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001743 if (log)
Jim Ingham4b536182011-08-09 02:12:22 +00001744 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745 }
1746 // Use our target to get a shared pointer to ourselves...
Greg Clayton35a4cc52012-10-29 20:52:08 +00001747 if (m_finalize_called && PrivateStateThreadIsValid() == false)
1748 BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
1749 else
1750 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751 }
1752 else
1753 {
1754 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001755 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756 }
1757}
1758
Jim Ingham0faa43f2011-11-08 03:00:11 +00001759void
1760Process::SetRunningUserExpression (bool on)
1761{
1762 m_mod_id.SetRunningUserExpression (on);
1763}
1764
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001765addr_t
1766Process::GetImageInfoAddress()
1767{
1768 return LLDB_INVALID_ADDRESS;
1769}
1770
Greg Clayton8f343b02010-11-04 01:54:29 +00001771//----------------------------------------------------------------------
1772// LoadImage
1773//
1774// This function provides a default implementation that works for most
1775// unix variants. Any Process subclasses that need to do shared library
1776// loading differently should override LoadImage and UnloadImage and
1777// do what is needed.
1778//----------------------------------------------------------------------
1779uint32_t
1780Process::LoadImage (const FileSpec &image_spec, Error &error)
1781{
Greg Claytonac7a3db2012-04-18 00:05:19 +00001782 char path[PATH_MAX];
1783 image_spec.GetPath(path, sizeof(path));
1784
Greg Clayton8f343b02010-11-04 01:54:29 +00001785 DynamicLoader *loader = GetDynamicLoader();
1786 if (loader)
1787 {
1788 error = loader->CanLoadImage();
1789 if (error.Fail())
1790 return LLDB_INVALID_IMAGE_TOKEN;
1791 }
1792
1793 if (error.Success())
1794 {
1795 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001796
1797 if (thread_sp)
1798 {
1799 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
1800
1801 if (frame_sp)
1802 {
1803 ExecutionContext exe_ctx;
1804 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Ingham184e9812013-01-15 02:47:48 +00001805 const bool unwind_on_error = true;
1806 const bool ignore_breakpoints = true;
Greg Clayton8f343b02010-11-04 01:54:29 +00001807 StreamString expr;
Greg Clayton8f343b02010-11-04 01:54:29 +00001808 expr.Printf("dlopen (\"%s\", 2)", path);
1809 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001810 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001811 ClangUserExpression::Evaluate (exe_ctx,
1812 eExecutionPolicyAlways,
1813 lldb::eLanguageTypeUnknown,
1814 ClangUserExpression::eResultTypeAny,
1815 unwind_on_error,
Jim Ingham184e9812013-01-15 02:47:48 +00001816 ignore_breakpoints,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001817 expr.GetData(),
1818 prefix,
1819 result_valobj_sp,
1820 true,
1821 ClangUserExpression::kDefaultTimeout);
Johnny Chene92aa432011-09-09 00:01:43 +00001822 error = result_valobj_sp->GetError();
1823 if (error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001824 {
1825 Scalar scalar;
Jim Ingham6035b672011-03-31 00:19:25 +00001826 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001827 {
1828 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1829 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
1830 {
1831 uint32_t image_token = m_image_tokens.size();
1832 m_image_tokens.push_back (image_ptr);
1833 return image_token;
1834 }
1835 }
1836 }
1837 }
1838 }
1839 }
Greg Claytonac7a3db2012-04-18 00:05:19 +00001840 if (!error.AsCString())
1841 error.SetErrorStringWithFormat("unable to load '%s'", path);
Greg Clayton8f343b02010-11-04 01:54:29 +00001842 return LLDB_INVALID_IMAGE_TOKEN;
1843}
1844
1845//----------------------------------------------------------------------
1846// UnloadImage
1847//
1848// This function provides a default implementation that works for most
1849// unix variants. Any Process subclasses that need to do shared library
1850// loading differently should override LoadImage and UnloadImage and
1851// do what is needed.
1852//----------------------------------------------------------------------
1853Error
1854Process::UnloadImage (uint32_t image_token)
1855{
1856 Error error;
1857 if (image_token < m_image_tokens.size())
1858 {
1859 const addr_t image_addr = m_image_tokens[image_token];
1860 if (image_addr == LLDB_INVALID_ADDRESS)
1861 {
1862 error.SetErrorString("image already unloaded");
1863 }
1864 else
1865 {
1866 DynamicLoader *loader = GetDynamicLoader();
1867 if (loader)
1868 error = loader->CanLoadImage();
1869
1870 if (error.Success())
1871 {
1872 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001873
1874 if (thread_sp)
1875 {
1876 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
1877
1878 if (frame_sp)
1879 {
1880 ExecutionContext exe_ctx;
1881 frame_sp->CalculateExecutionContext (exe_ctx);
Jim Ingham184e9812013-01-15 02:47:48 +00001882 const bool unwind_on_error = true;
1883 const bool ignore_breakpoints = true;
Greg Clayton8f343b02010-11-04 01:54:29 +00001884 StreamString expr;
Daniel Malead01b2952012-11-29 21:49:15 +00001885 expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr);
Greg Clayton8f343b02010-11-04 01:54:29 +00001886 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001887 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001888 ClangUserExpression::Evaluate (exe_ctx,
1889 eExecutionPolicyAlways,
1890 lldb::eLanguageTypeUnknown,
1891 ClangUserExpression::eResultTypeAny,
1892 unwind_on_error,
Jim Ingham184e9812013-01-15 02:47:48 +00001893 ignore_breakpoints,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001894 expr.GetData(),
1895 prefix,
1896 result_valobj_sp,
1897 true,
1898 ClangUserExpression::kDefaultTimeout);
Greg Clayton8f343b02010-11-04 01:54:29 +00001899 if (result_valobj_sp->GetError().Success())
1900 {
1901 Scalar scalar;
Jim Ingham6035b672011-03-31 00:19:25 +00001902 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001903 {
1904 if (scalar.UInt(1))
1905 {
1906 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1907 }
1908 else
1909 {
1910 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1911 }
1912 }
1913 }
1914 else
1915 {
1916 error = result_valobj_sp->GetError();
1917 }
1918 }
1919 }
1920 }
1921 }
1922 }
1923 else
1924 {
1925 error.SetErrorString("invalid image token");
1926 }
1927 return error;
1928}
1929
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001930const lldb::ABISP &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931Process::GetABI()
1932{
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001933 if (!m_abi_sp)
1934 m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture());
1935 return m_abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936}
1937
Jim Ingham22777012010-09-23 02:01:19 +00001938LanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001939Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001940{
1941 LanguageRuntimeCollection::iterator pos;
1942 pos = m_language_runtimes.find (language);
Jim Inghamab175242012-03-10 00:22:19 +00001943 if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second))
Jim Ingham22777012010-09-23 02:01:19 +00001944 {
Jim Inghamab175242012-03-10 00:22:19 +00001945 lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language));
Jim Ingham22777012010-09-23 02:01:19 +00001946
Jim Inghamab175242012-03-10 00:22:19 +00001947 m_language_runtimes[language] = runtime_sp;
1948 return runtime_sp.get();
Jim Ingham22777012010-09-23 02:01:19 +00001949 }
1950 else
1951 return (*pos).second.get();
1952}
1953
1954CPPLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001955Process::GetCPPLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001956{
Jim Inghamab175242012-03-10 00:22:19 +00001957 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00001958 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
1959 return static_cast<CPPLanguageRuntime *> (runtime);
1960 return NULL;
1961}
1962
1963ObjCLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001964Process::GetObjCLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001965{
Jim Inghamab175242012-03-10 00:22:19 +00001966 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00001967 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
1968 return static_cast<ObjCLanguageRuntime *> (runtime);
1969 return NULL;
1970}
1971
Enrico Granatafd4c84e2012-05-21 16:51:35 +00001972bool
1973Process::IsPossibleDynamicValue (ValueObject& in_value)
1974{
1975 if (in_value.IsDynamic())
1976 return false;
1977 LanguageType known_type = in_value.GetObjectRuntimeLanguage();
1978
1979 if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
1980 {
1981 LanguageRuntime *runtime = GetLanguageRuntime (known_type);
1982 return runtime ? runtime->CouldHaveDynamicValue(in_value) : false;
1983 }
1984
1985 LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus);
1986 if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value))
1987 return true;
1988
1989 LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC);
1990 return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false;
1991}
1992
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993BreakpointSiteList &
1994Process::GetBreakpointSiteList()
1995{
1996 return m_breakpoint_site_list;
1997}
1998
1999const BreakpointSiteList &
2000Process::GetBreakpointSiteList() const
2001{
2002 return m_breakpoint_site_list;
2003}
2004
2005
2006void
2007Process::DisableAllBreakpointSites ()
2008{
2009 m_breakpoint_site_list.SetEnabledForAll (false);
Jim Ingham43c555d2012-07-04 00:35:43 +00002010 size_t num_sites = m_breakpoint_site_list.GetSize();
2011 for (size_t i = 0; i < num_sites; i++)
2012 {
Jim Ingham299c0c12013-02-15 02:06:30 +00002013 DisableBreakpointSite (m_breakpoint_site_list.GetByIndex(i).get());
Jim Ingham43c555d2012-07-04 00:35:43 +00002014 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002015}
2016
2017Error
2018Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
2019{
2020 Error error (DisableBreakpointSiteByID (break_id));
2021
2022 if (error.Success())
2023 m_breakpoint_site_list.Remove(break_id);
2024
2025 return error;
2026}
2027
2028Error
2029Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
2030{
2031 Error error;
2032 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
2033 if (bp_site_sp)
2034 {
2035 if (bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00002036 error = DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037 }
2038 else
2039 {
Daniel Malead01b2952012-11-29 21:49:15 +00002040 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002041 }
2042
2043 return error;
2044}
2045
2046Error
2047Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
2048{
2049 Error error;
2050 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
2051 if (bp_site_sp)
2052 {
2053 if (!bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00002054 error = EnableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002055 }
2056 else
2057 {
Daniel Malead01b2952012-11-29 21:49:15 +00002058 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002059 }
2060 return error;
2061}
2062
Stephen Wilson50bd94f2010-07-17 00:56:13 +00002063lldb::break_id_t
Greg Claytone1cd1be2012-01-29 20:56:30 +00002064Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002065{
Greg Clayton92bb12c2011-05-19 18:17:41 +00002066 const addr_t load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002067 if (load_addr != LLDB_INVALID_ADDRESS)
2068 {
2069 BreakpointSiteSP bp_site_sp;
2070
2071 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
2072 // create a new breakpoint site and add it.
2073
2074 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
2075
2076 if (bp_site_sp)
2077 {
2078 bp_site_sp->AddOwner (owner);
2079 owner->SetBreakpointSite (bp_site_sp);
2080 return bp_site_sp->GetID();
2081 }
2082 else
2083 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002084 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, use_hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002085 if (bp_site_sp)
2086 {
Jim Ingham299c0c12013-02-15 02:06:30 +00002087 if (EnableBreakpointSite (bp_site_sp.get()).Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002088 {
2089 owner->SetBreakpointSite (bp_site_sp);
2090 return m_breakpoint_site_list.Add (bp_site_sp);
2091 }
2092 }
2093 }
2094 }
2095 // We failed to enable the breakpoint
2096 return LLDB_INVALID_BREAK_ID;
2097
2098}
2099
2100void
2101Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
2102{
2103 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
2104 if (num_owners == 0)
2105 {
Jim Inghamf1ff3bb2013-04-06 00:16:39 +00002106 // Don't try to disable the site if we don't have a live process anymore.
2107 if (IsAlive())
2108 DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002109 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
2110 }
2111}
2112
2113
2114size_t
2115Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
2116{
2117 size_t bytes_removed = 0;
2118 addr_t intersect_addr;
2119 size_t intersect_size;
2120 size_t opcode_offset;
2121 size_t idx;
Greg Clayton4d122c42011-09-17 08:33:22 +00002122 BreakpointSiteSP bp_sp;
Jim Ingham20c77192011-06-29 19:42:28 +00002123 BreakpointSiteList bp_sites_in_range;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124
Jim Ingham20c77192011-06-29 19:42:28 +00002125 if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002126 {
Greg Clayton4d122c42011-09-17 08:33:22 +00002127 for (idx = 0; (bp_sp = bp_sites_in_range.GetByIndex(idx)); ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002128 {
Greg Clayton4d122c42011-09-17 08:33:22 +00002129 if (bp_sp->GetType() == BreakpointSite::eSoftware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002130 {
Greg Clayton4d122c42011-09-17 08:33:22 +00002131 if (bp_sp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
Jim Ingham20c77192011-06-29 19:42:28 +00002132 {
2133 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
2134 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
Greg Clayton4d122c42011-09-17 08:33:22 +00002135 assert(opcode_offset + intersect_size <= bp_sp->GetByteSize());
Jim Ingham20c77192011-06-29 19:42:28 +00002136 size_t buf_offset = intersect_addr - bp_addr;
Greg Clayton4d122c42011-09-17 08:33:22 +00002137 ::memcpy(buf + buf_offset, bp_sp->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
Jim Ingham20c77192011-06-29 19:42:28 +00002138 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002139 }
2140 }
2141 }
2142 return bytes_removed;
2143}
2144
2145
Greg Claytonded470d2011-03-19 01:12:21 +00002146
2147size_t
2148Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
2149{
2150 PlatformSP platform_sp (m_target.GetPlatform());
2151 if (platform_sp)
2152 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
2153 return 0;
2154}
2155
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002156Error
2157Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
2158{
2159 Error error;
2160 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002161 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002162 const addr_t bp_addr = bp_site->GetLoadAddress();
2163 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002164 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, bp_site->GetID(), (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002165 if (bp_site->IsEnabled())
2166 {
2167 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002168 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- already enabled", bp_site->GetID(), (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002169 return error;
2170 }
2171
2172 if (bp_addr == LLDB_INVALID_ADDRESS)
2173 {
2174 error.SetErrorString("BreakpointSite contains an invalid load address.");
2175 return error;
2176 }
2177 // Ask the lldb::Process subclass to fill in the correct software breakpoint
2178 // trap for the breakpoint site
2179 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
2180
2181 if (bp_opcode_size == 0)
2182 {
Daniel Malead01b2952012-11-29 21:49:15 +00002183 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%" PRIx64, bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184 }
2185 else
2186 {
2187 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
2188
2189 if (bp_opcode_bytes == NULL)
2190 {
2191 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
2192 return error;
2193 }
2194
2195 // Save the original opcode by reading it
2196 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
2197 {
2198 // Write a software breakpoint in place of the original opcode
2199 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2200 {
2201 uint8_t verify_bp_opcode_bytes[64];
2202 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2203 {
2204 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
2205 {
2206 bp_site->SetEnabled(true);
2207 bp_site->SetType (BreakpointSite::eSoftware);
2208 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002209 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002210 bp_site->GetID(),
2211 (uint64_t)bp_addr);
2212 }
2213 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002214 error.SetErrorString("failed to verify the breakpoint trap in memory.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002215 }
2216 else
2217 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
2218 }
2219 else
2220 error.SetErrorString("Unable to write breakpoint trap to memory.");
2221 }
2222 else
2223 error.SetErrorString("Unable to read memory at breakpoint address.");
2224 }
Stephen Wilson78a4feb2011-01-12 04:20:03 +00002225 if (log && error.Fail())
Daniel Malead01b2952012-11-29 21:49:15 +00002226 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002227 bp_site->GetID(),
2228 (uint64_t)bp_addr,
2229 error.AsCString());
2230 return error;
2231}
2232
2233Error
2234Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
2235{
2236 Error error;
2237 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002238 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002239 addr_t bp_addr = bp_site->GetLoadAddress();
2240 lldb::user_id_t breakID = bp_site->GetID();
2241 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002242 log->Printf ("Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 ") addr = 0x%" PRIx64, breakID, (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243
2244 if (bp_site->IsHardware())
2245 {
2246 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
2247 }
2248 else if (bp_site->IsEnabled())
2249 {
2250 const size_t break_op_size = bp_site->GetByteSize();
2251 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
2252 if (break_op_size > 0)
2253 {
2254 // Clear a software breakoint instruction
Greg Claytonc982c762010-07-09 20:39:50 +00002255 uint8_t curr_break_op[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002256 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002257 bool break_op_found = false;
2258
2259 // Read the breakpoint opcode
2260 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
2261 {
2262 bool verify = false;
2263 // Make sure we have the a breakpoint opcode exists at this address
2264 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
2265 {
2266 break_op_found = true;
2267 // We found a valid breakpoint opcode at this address, now restore
2268 // the saved opcode.
2269 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
2270 {
2271 verify = true;
2272 }
2273 else
2274 error.SetErrorString("Memory write failed when restoring original opcode.");
2275 }
2276 else
2277 {
2278 error.SetErrorString("Original breakpoint trap is no longer in memory.");
2279 // Set verify to true and so we can check if the original opcode has already been restored
2280 verify = true;
2281 }
2282
2283 if (verify)
2284 {
Greg Claytonc982c762010-07-09 20:39:50 +00002285 uint8_t verify_opcode[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002286 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002287 // Verify that our original opcode made it back to the inferior
2288 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
2289 {
2290 // compare the memory we just read with the original opcode
2291 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
2292 {
2293 // SUCCESS
2294 bp_site->SetEnabled(false);
2295 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002296 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002297 return error;
2298 }
2299 else
2300 {
2301 if (break_op_found)
2302 error.SetErrorString("Failed to restore original opcode.");
2303 }
2304 }
2305 else
2306 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
2307 }
2308 }
2309 else
2310 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
2311 }
2312 }
2313 else
2314 {
2315 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002316 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- already disabled", bp_site->GetID(), (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002317 return error;
2318 }
2319
2320 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002321 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002322 bp_site->GetID(),
2323 (uint64_t)bp_addr,
2324 error.AsCString());
2325 return error;
2326
2327}
2328
Greg Clayton58be07b2011-01-07 06:08:19 +00002329// Uncomment to verify memory caching works after making changes to caching code
2330//#define VERIFY_MEMORY_READS
2331
Sean Callanan64c0cf22012-06-07 22:26:42 +00002332size_t
2333Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2334{
2335 if (!GetDisableMemoryCache())
2336 {
Greg Clayton58be07b2011-01-07 06:08:19 +00002337#if defined (VERIFY_MEMORY_READS)
Sean Callanan64c0cf22012-06-07 22:26:42 +00002338 // Memory caching is enabled, with debug verification
2339
2340 if (buf && size)
2341 {
2342 // Uncomment the line below to make sure memory caching is working.
2343 // I ran this through the test suite and got no assertions, so I am
2344 // pretty confident this is working well. If any changes are made to
2345 // memory caching, uncomment the line below and test your changes!
2346
2347 // Verify all memory reads by using the cache first, then redundantly
2348 // reading the same memory from the inferior and comparing to make sure
2349 // everything is exactly the same.
2350 std::string verify_buf (size, '\0');
2351 assert (verify_buf.size() == size);
2352 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
2353 Error verify_error;
2354 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
2355 assert (cache_bytes_read == verify_bytes_read);
2356 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
2357 assert (verify_error.Success() == error.Success());
2358 return cache_bytes_read;
2359 }
2360 return 0;
2361#else // !defined(VERIFY_MEMORY_READS)
2362 // Memory caching is enabled, without debug verification
2363
2364 return m_memory_cache.Read (addr, buf, size, error);
2365#endif // defined (VERIFY_MEMORY_READS)
Greg Clayton58be07b2011-01-07 06:08:19 +00002366 }
Sean Callanan64c0cf22012-06-07 22:26:42 +00002367 else
2368 {
2369 // Memory caching is disabled
2370
2371 return ReadMemoryFromInferior (addr, buf, size, error);
2372 }
Greg Clayton58be07b2011-01-07 06:08:19 +00002373}
Greg Clayton58be07b2011-01-07 06:08:19 +00002374
Greg Clayton4c82d422012-05-18 23:20:01 +00002375size_t
2376Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error)
2377{
Greg Claytonde87c0f2012-05-19 00:18:00 +00002378 char buf[256];
Greg Clayton4c82d422012-05-18 23:20:01 +00002379 out_str.clear();
2380 addr_t curr_addr = addr;
2381 while (1)
2382 {
2383 size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error);
2384 if (length == 0)
2385 break;
2386 out_str.append(buf, length);
2387 // If we got "length - 1" bytes, we didn't get the whole C string, we
2388 // need to read some more characters
2389 if (length == sizeof(buf) - 1)
2390 curr_addr += length;
2391 else
2392 break;
2393 }
2394 return out_str.size();
2395}
2396
Greg Clayton58be07b2011-01-07 06:08:19 +00002397
2398size_t
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +00002399Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error,
2400 size_t type_width)
2401{
2402 size_t total_bytes_read = 0;
2403 if (dst && max_bytes && type_width && max_bytes >= type_width)
2404 {
2405 // Ensure a null terminator independent of the number of bytes that is read.
2406 memset (dst, 0, max_bytes);
2407 size_t bytes_left = max_bytes - type_width;
2408
2409 const char terminator[4] = {'\0', '\0', '\0', '\0'};
2410 assert(sizeof(terminator) >= type_width &&
2411 "Attempting to validate a string with more than 4 bytes per character!");
2412
2413 addr_t curr_addr = addr;
2414 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2415 char *curr_dst = dst;
2416
2417 error.Clear();
2418 while (bytes_left > 0 && error.Success())
2419 {
2420 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2421 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2422 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2423
2424 if (bytes_read == 0)
2425 break;
2426
2427 // Search for a null terminator of correct size and alignment in bytes_read
2428 size_t aligned_start = total_bytes_read - total_bytes_read % type_width;
2429 for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width)
2430 if (::strncmp(&dst[i], terminator, type_width) == 0)
2431 {
2432 error.Clear();
2433 return i;
2434 }
2435
2436 total_bytes_read += bytes_read;
2437 curr_dst += bytes_read;
2438 curr_addr += bytes_read;
2439 bytes_left -= bytes_read;
2440 }
2441 }
2442 else
2443 {
2444 if (max_bytes)
2445 error.SetErrorString("invalid arguments");
2446 }
2447 return total_bytes_read;
2448}
2449
2450// Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find
2451// null terminators.
2452size_t
Greg Claytone91b7952011-12-15 03:14:23 +00002453Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error)
Greg Clayton8b82f082011-04-12 05:54:46 +00002454{
2455 size_t total_cstr_len = 0;
2456 if (dst && dst_max_len)
2457 {
Greg Claytone91b7952011-12-15 03:14:23 +00002458 result_error.Clear();
Greg Clayton8b82f082011-04-12 05:54:46 +00002459 // NULL out everything just to be safe
2460 memset (dst, 0, dst_max_len);
2461 Error error;
2462 addr_t curr_addr = addr;
2463 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2464 size_t bytes_left = dst_max_len - 1;
2465 char *curr_dst = dst;
2466
2467 while (bytes_left > 0)
2468 {
2469 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2470 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2471 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2472
2473 if (bytes_read == 0)
2474 {
Greg Claytone91b7952011-12-15 03:14:23 +00002475 result_error = error;
Greg Clayton8b82f082011-04-12 05:54:46 +00002476 dst[total_cstr_len] = '\0';
2477 break;
2478 }
2479 const size_t len = strlen(curr_dst);
2480
2481 total_cstr_len += len;
2482
2483 if (len < bytes_to_read)
2484 break;
2485
2486 curr_dst += bytes_read;
2487 curr_addr += bytes_read;
2488 bytes_left -= bytes_read;
2489 }
2490 }
Greg Claytone91b7952011-12-15 03:14:23 +00002491 else
2492 {
2493 if (dst == NULL)
2494 result_error.SetErrorString("invalid arguments");
2495 else
2496 result_error.Clear();
2497 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002498 return total_cstr_len;
2499}
2500
2501size_t
Greg Clayton58be07b2011-01-07 06:08:19 +00002502Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
2503{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002504 if (buf == NULL || size == 0)
2505 return 0;
2506
2507 size_t bytes_read = 0;
2508 uint8_t *bytes = (uint8_t *)buf;
2509
2510 while (bytes_read < size)
2511 {
2512 const size_t curr_size = size - bytes_read;
2513 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
2514 bytes + bytes_read,
2515 curr_size,
2516 error);
2517 bytes_read += curr_bytes_read;
2518 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
2519 break;
2520 }
2521
2522 // Replace any software breakpoint opcodes that fall into this range back
2523 // into "buf" before we return
2524 if (bytes_read > 0)
2525 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
2526 return bytes_read;
2527}
2528
Greg Clayton58a4c462010-12-16 20:01:20 +00002529uint64_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002530Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error)
Greg Clayton58a4c462010-12-16 20:01:20 +00002531{
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002532 Scalar scalar;
2533 if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error))
2534 return scalar.ULongLong(fail_value);
2535 return fail_value;
2536}
2537
2538addr_t
2539Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error)
2540{
2541 Scalar scalar;
2542 if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error))
2543 return scalar.ULongLong(LLDB_INVALID_ADDRESS);
2544 return LLDB_INVALID_ADDRESS;
2545}
2546
2547
2548bool
2549Process::WritePointerToMemory (lldb::addr_t vm_addr,
2550 lldb::addr_t ptr_value,
2551 Error &error)
2552{
2553 Scalar scalar;
2554 const uint32_t addr_byte_size = GetAddressByteSize();
2555 if (addr_byte_size <= 4)
2556 scalar = (uint32_t)ptr_value;
Greg Clayton58a4c462010-12-16 20:01:20 +00002557 else
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002558 scalar = ptr_value;
2559 return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size;
Greg Clayton58a4c462010-12-16 20:01:20 +00002560}
2561
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002562size_t
2563Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
2564{
2565 size_t bytes_written = 0;
2566 const uint8_t *bytes = (const uint8_t *)buf;
2567
2568 while (bytes_written < size)
2569 {
2570 const size_t curr_size = size - bytes_written;
2571 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
2572 bytes + bytes_written,
2573 curr_size,
2574 error);
2575 bytes_written += curr_bytes_written;
2576 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
2577 break;
2578 }
2579 return bytes_written;
2580}
2581
2582size_t
2583Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2584{
Greg Clayton58be07b2011-01-07 06:08:19 +00002585#if defined (ENABLE_MEMORY_CACHING)
2586 m_memory_cache.Flush (addr, size);
2587#endif
2588
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002589 if (buf == NULL || size == 0)
2590 return 0;
Jim Ingham78a685a2011-04-16 00:01:13 +00002591
Jim Ingham4b536182011-08-09 02:12:22 +00002592 m_mod_id.BumpMemoryID();
Jim Ingham78a685a2011-04-16 00:01:13 +00002593
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002594 // We need to write any data that would go where any current software traps
2595 // (enabled software breakpoints) any software traps (breakpoints) that we
2596 // may have placed in our tasks memory.
2597
2598 BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr);
2599 BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end();
2600
2601 if (iter == end || iter->second->GetLoadAddress() > addr + size)
Greg Claytonb4aaf2e2011-05-16 02:35:02 +00002602 return WriteMemoryPrivate (addr, buf, size, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002603
2604 BreakpointSiteList::collection::const_iterator pos;
2605 size_t bytes_written = 0;
Greg Claytonc982c762010-07-09 20:39:50 +00002606 addr_t intersect_addr = 0;
2607 size_t intersect_size = 0;
2608 size_t opcode_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002609 const uint8_t *ubuf = (const uint8_t *)buf;
2610
2611 for (pos = iter; pos != end; ++pos)
2612 {
2613 BreakpointSiteSP bp;
2614 bp = pos->second;
2615
2616 assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset));
2617 assert(addr <= intersect_addr && intersect_addr < addr + size);
2618 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
2619 assert(opcode_offset + intersect_size <= bp->GetByteSize());
2620
2621 // Check for bytes before this breakpoint
2622 const addr_t curr_addr = addr + bytes_written;
2623 if (intersect_addr > curr_addr)
2624 {
2625 // There are some bytes before this breakpoint that we need to
2626 // just write to memory
2627 size_t curr_size = intersect_addr - curr_addr;
2628 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
2629 ubuf + bytes_written,
2630 curr_size,
2631 error);
2632 bytes_written += curr_bytes_written;
2633 if (curr_bytes_written != curr_size)
2634 {
2635 // We weren't able to write all of the requested bytes, we
2636 // are done looping and will return the number of bytes that
2637 // we have written so far.
2638 break;
2639 }
2640 }
2641
2642 // Now write any bytes that would cover up any software breakpoints
2643 // directly into the breakpoint opcode buffer
2644 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
2645 bytes_written += intersect_size;
2646 }
2647
2648 // Write any remaining bytes after the last breakpoint if we have any left
2649 if (bytes_written < size)
2650 bytes_written += WriteMemoryPrivate (addr + bytes_written,
2651 ubuf + bytes_written,
2652 size - bytes_written,
2653 error);
Jim Ingham78a685a2011-04-16 00:01:13 +00002654
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002655 return bytes_written;
2656}
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002657
2658size_t
Greg Claytonc7bece562013-01-25 18:06:21 +00002659Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, size_t byte_size, Error &error)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002660{
2661 if (byte_size == UINT32_MAX)
2662 byte_size = scalar.GetByteSize();
2663 if (byte_size > 0)
2664 {
2665 uint8_t buf[32];
2666 const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error);
2667 if (mem_size > 0)
2668 return WriteMemory(addr, buf, mem_size, error);
2669 else
2670 error.SetErrorString ("failed to get scalar as memory data");
2671 }
2672 else
2673 {
2674 error.SetErrorString ("invalid scalar value");
2675 }
2676 return 0;
2677}
2678
2679size_t
2680Process::ReadScalarIntegerFromMemory (addr_t addr,
2681 uint32_t byte_size,
2682 bool is_signed,
2683 Scalar &scalar,
2684 Error &error)
2685{
Greg Clayton7060f892013-05-01 23:41:30 +00002686 uint64_t uval = 0;
2687 if (byte_size == 0)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002688 {
Greg Clayton7060f892013-05-01 23:41:30 +00002689 error.SetErrorString ("byte size is zero");
2690 }
2691 else if (byte_size & (byte_size - 1))
2692 {
2693 error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size);
2694 }
2695 else if (byte_size <= sizeof(uval))
2696 {
2697 const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002698 if (bytes_read == byte_size)
2699 {
2700 DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00002701 lldb::offset_t offset = 0;
Greg Clayton7060f892013-05-01 23:41:30 +00002702 if (byte_size <= 4)
2703 scalar = data.GetMaxU32 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002704 else
Greg Clayton7060f892013-05-01 23:41:30 +00002705 scalar = data.GetMaxU64 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002706 if (is_signed)
2707 scalar.SignExtend(byte_size * 8);
2708 return bytes_read;
2709 }
2710 }
2711 else
2712 {
2713 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
2714 }
2715 return 0;
2716}
2717
Greg Claytond495c532011-05-17 03:37:42 +00002718#define USE_ALLOCATE_MEMORY_CACHE 1
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002719addr_t
2720Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
2721{
Jim Inghamf72ce3a2011-06-20 17:32:44 +00002722 if (GetPrivateState() != eStateStopped)
2723 return LLDB_INVALID_ADDRESS;
2724
Greg Claytond495c532011-05-17 03:37:42 +00002725#if defined (USE_ALLOCATE_MEMORY_CACHE)
2726 return m_allocated_memory_cache.AllocateMemory(size, permissions, error);
2727#else
Greg Claytonb2daec92011-01-23 19:58:49 +00002728 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
Greg Clayton5160ce52013-03-27 23:08:40 +00002729 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002730 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002731 log->Printf("Process::AllocateMemory(size=%4zu, permissions=%s) => 0x%16.16" PRIx64 " (m_stop_id = %u m_memory_id = %u)",
Greg Claytonb2daec92011-01-23 19:58:49 +00002732 size,
Greg Claytond495c532011-05-17 03:37:42 +00002733 GetPermissionsAsCString (permissions),
Greg Claytonb2daec92011-01-23 19:58:49 +00002734 (uint64_t)allocated_addr,
Jim Ingham4b536182011-08-09 02:12:22 +00002735 m_mod_id.GetStopID(),
2736 m_mod_id.GetMemoryID());
Greg Claytonb2daec92011-01-23 19:58:49 +00002737 return allocated_addr;
Greg Claytond495c532011-05-17 03:37:42 +00002738#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002739}
2740
Sean Callanan90539452011-09-20 23:01:51 +00002741bool
2742Process::CanJIT ()
2743{
Sean Callanana7b443a2012-02-14 22:50:38 +00002744 if (m_can_jit == eCanJITDontKnow)
2745 {
2746 Error err;
2747
2748 uint64_t allocated_memory = AllocateMemory(8,
2749 ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable,
2750 err);
2751
2752 if (err.Success())
2753 m_can_jit = eCanJITYes;
2754 else
2755 m_can_jit = eCanJITNo;
2756
2757 DeallocateMemory (allocated_memory);
2758 }
2759
Sean Callanan90539452011-09-20 23:01:51 +00002760 return m_can_jit == eCanJITYes;
2761}
2762
2763void
2764Process::SetCanJIT (bool can_jit)
2765{
2766 m_can_jit = (can_jit ? eCanJITYes : eCanJITNo);
2767}
2768
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002769Error
2770Process::DeallocateMemory (addr_t ptr)
2771{
Greg Claytond495c532011-05-17 03:37:42 +00002772 Error error;
2773#if defined (USE_ALLOCATE_MEMORY_CACHE)
2774 if (!m_allocated_memory_cache.DeallocateMemory(ptr))
2775 {
Daniel Malead01b2952012-11-29 21:49:15 +00002776 error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr);
Greg Claytond495c532011-05-17 03:37:42 +00002777 }
2778#else
2779 error = DoDeallocateMemory (ptr);
Greg Claytonb2daec92011-01-23 19:58:49 +00002780
Greg Clayton5160ce52013-03-27 23:08:40 +00002781 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002782 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002783 log->Printf("Process::DeallocateMemory(addr=0x%16.16" PRIx64 ") => err = %s (m_stop_id = %u, m_memory_id = %u)",
Greg Claytonb2daec92011-01-23 19:58:49 +00002784 ptr,
2785 error.AsCString("SUCCESS"),
Jim Ingham4b536182011-08-09 02:12:22 +00002786 m_mod_id.GetStopID(),
2787 m_mod_id.GetMemoryID());
Greg Claytond495c532011-05-17 03:37:42 +00002788#endif
Greg Claytonb2daec92011-01-23 19:58:49 +00002789 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002790}
2791
Han Ming Ongc811d382012-11-17 00:33:14 +00002792
Greg Claytonc9660542012-02-05 02:38:54 +00002793ModuleSP
Greg Claytonc859e2d2012-02-13 23:10:39 +00002794Process::ReadModuleFromMemory (const FileSpec& file_spec,
Greg Clayton39f7ee82013-02-01 21:38:35 +00002795 lldb::addr_t header_addr)
Greg Claytonc9660542012-02-05 02:38:54 +00002796{
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002797 ModuleSP module_sp (new Module (file_spec, ArchSpec()));
Greg Claytonc9660542012-02-05 02:38:54 +00002798 if (module_sp)
2799 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002800 Error error;
2801 ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error);
2802 if (objfile)
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002803 return module_sp;
Greg Claytonc9660542012-02-05 02:38:54 +00002804 }
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002805 return ModuleSP();
Greg Claytonc9660542012-02-05 02:38:54 +00002806}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002807
2808Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002809Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002810{
2811 Error error;
2812 error.SetErrorString("watchpoints are not supported");
2813 return error;
2814}
2815
2816Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002817Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002818{
2819 Error error;
2820 error.SetErrorString("watchpoints are not supported");
2821 return error;
2822}
2823
2824StateType
2825Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
2826{
2827 StateType state;
2828 // Now wait for the process to launch and return control to us, and then
2829 // call DidLaunch:
2830 while (1)
2831 {
Greg Clayton6779606a2011-01-22 23:43:18 +00002832 event_sp.reset();
2833 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
2834
Greg Clayton2637f822011-11-17 01:23:07 +00002835 if (StateIsStoppedState(state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002836 break;
Greg Clayton6779606a2011-01-22 23:43:18 +00002837
2838 // If state is invalid, then we timed out
2839 if (state == eStateInvalid)
2840 break;
2841
2842 if (event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002843 HandlePrivateEvent (event_sp);
2844 }
2845 return state;
2846}
2847
2848Error
Greg Clayton982c9762011-11-03 21:22:33 +00002849Process::Launch (const ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002850{
2851 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002852 m_abi_sp.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002853 m_dyld_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002854 m_os_ap.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002855 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002856
Greg Claytonaa149cb2011-08-11 02:48:45 +00002857 Module *exe_module = m_target.GetExecutableModulePointer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002858 if (exe_module)
2859 {
Greg Clayton2289fa42011-04-30 01:09:13 +00002860 char local_exec_file_path[PATH_MAX];
2861 char platform_exec_file_path[PATH_MAX];
2862 exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path));
2863 exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002864 if (exe_module->GetFileSpec().Exists())
2865 {
Greg Clayton71337622011-02-24 22:24:29 +00002866 if (PrivateStateThreadIsValid ())
2867 PausePrivateStateThread ();
2868
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002869 error = WillLaunch (exe_module);
2870 if (error.Success())
2871 {
Jim Ingham221d51c2013-05-08 00:35:16 +00002872 const bool restarted = false;
2873 SetPublicState (eStateLaunching, restarted);
Greg Claytone24c4ac2011-11-17 04:46:02 +00002874 m_should_detach = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002875
Greg Clayton96249852013-04-18 16:57:27 +00002876 if (m_public_run_lock.WriteTryLock())
Greg Clayton69fd4be2012-09-04 20:29:05 +00002877 {
2878 // Now launch using these arguments.
2879 error = DoLaunch (exe_module, launch_info);
2880 }
2881 else
2882 {
2883 // This shouldn't happen
2884 error.SetErrorString("failed to acquire process run lock");
2885 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002886
2887 if (error.Fail())
2888 {
2889 if (GetID() != LLDB_INVALID_PROCESS_ID)
2890 {
2891 SetID (LLDB_INVALID_PROCESS_ID);
2892 const char *error_string = error.AsCString();
2893 if (error_string == NULL)
2894 error_string = "launch failed";
2895 SetExitStatus (-1, error_string);
2896 }
2897 }
2898 else
2899 {
2900 EventSP event_sp;
Greg Clayton1a38ea72011-06-22 01:42:17 +00002901 TimeValue timeout_time;
2902 timeout_time = TimeValue::Now();
2903 timeout_time.OffsetWithSeconds(10);
2904 StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002905
Greg Clayton1a38ea72011-06-22 01:42:17 +00002906 if (state == eStateInvalid || event_sp.get() == NULL)
2907 {
2908 // We were able to launch the process, but we failed to
2909 // catch the initial stop.
2910 SetExitStatus (0, "failed to catch stop after launch");
2911 Destroy();
2912 }
2913 else if (state == eStateStopped || state == eStateCrashed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002914 {
Greg Clayton93d3c8332011-02-16 04:46:07 +00002915
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002916 DidLaunch ();
2917
Greg Claytonc859e2d2012-02-13 23:10:39 +00002918 DynamicLoader *dyld = GetDynamicLoader ();
2919 if (dyld)
2920 dyld->DidLaunch();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002921
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002922 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002923 // This delays passing the stopped event to listeners till DidLaunch gets
2924 // a chance to complete...
2925 HandlePrivateEvent (event_sp);
Greg Clayton71337622011-02-24 22:24:29 +00002926
2927 if (PrivateStateThreadIsValid ())
2928 ResumePrivateStateThread ();
2929 else
2930 StartPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002931 }
2932 else if (state == eStateExited)
2933 {
2934 // We exited while trying to launch somehow. Don't call DidLaunch as that's
2935 // not likely to work, and return an invalid pid.
2936 HandlePrivateEvent (event_sp);
2937 }
2938 }
2939 }
2940 }
2941 else
2942 {
Greg Clayton86edbf42011-10-26 00:56:27 +00002943 error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002944 }
2945 }
2946 return error;
2947}
2948
Greg Claytonc3776bf2012-02-09 06:16:32 +00002949
2950Error
2951Process::LoadCore ()
2952{
2953 Error error = DoLoadCore();
2954 if (error.Success())
2955 {
2956 if (PrivateStateThreadIsValid ())
2957 ResumePrivateStateThread ();
2958 else
2959 StartPrivateStateThread ();
2960
Greg Claytonc859e2d2012-02-13 23:10:39 +00002961 DynamicLoader *dyld = GetDynamicLoader ();
2962 if (dyld)
2963 dyld->DidAttach();
2964
2965 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Claytonc3776bf2012-02-09 06:16:32 +00002966 // We successfully loaded a core file, now pretend we stopped so we can
2967 // show all of the threads in the core file and explore the crashed
2968 // state.
2969 SetPrivateState (eStateStopped);
2970
2971 }
2972 return error;
2973}
2974
Greg Claytonc859e2d2012-02-13 23:10:39 +00002975DynamicLoader *
2976Process::GetDynamicLoader ()
2977{
2978 if (m_dyld_ap.get() == NULL)
2979 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
2980 return m_dyld_ap.get();
2981}
Greg Claytonc3776bf2012-02-09 06:16:32 +00002982
2983
Jim Inghambb3a2832011-01-29 01:49:25 +00002984Process::NextEventAction::EventActionResult
2985Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002986{
Jim Inghambb3a2832011-01-29 01:49:25 +00002987 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
2988 switch (state)
Greg Clayton19388cf2010-10-18 01:45:30 +00002989 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002990 case eStateRunning:
Greg Clayton71337622011-02-24 22:24:29 +00002991 case eStateConnected:
Greg Clayton513c26c2011-01-29 07:10:55 +00002992 return eEventActionRetry;
2993
2994 case eStateStopped:
2995 case eStateCrashed:
Greg Claytonc9ed4782011-11-12 02:10:56 +00002996 {
2997 // During attach, prior to sending the eStateStopped event,
Jim Inghamb1e2e842012-04-12 18:49:31 +00002998 // lldb_private::Process subclasses must set the new process ID.
Greg Claytonc9ed4782011-11-12 02:10:56 +00002999 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
Jim Ingham221d51c2013-05-08 00:35:16 +00003000 // We don't want these events to be reported, so go set the ShouldReportStop here:
3001 m_process->GetThreadList().SetShouldReportStop (eVoteNo);
3002
Greg Claytonc9ed4782011-11-12 02:10:56 +00003003 if (m_exec_count > 0)
3004 {
3005 --m_exec_count;
Jim Ingham221d51c2013-05-08 00:35:16 +00003006 RequestResume();
Greg Claytonc9ed4782011-11-12 02:10:56 +00003007 return eEventActionRetry;
3008 }
3009 else
3010 {
3011 m_process->CompleteAttach ();
3012 return eEventActionSuccess;
3013 }
3014 }
Greg Clayton513c26c2011-01-29 07:10:55 +00003015 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003016
Greg Clayton513c26c2011-01-29 07:10:55 +00003017 default:
3018 case eStateExited:
3019 case eStateInvalid:
Greg Clayton513c26c2011-01-29 07:10:55 +00003020 break;
Jim Inghambb3a2832011-01-29 01:49:25 +00003021 }
Greg Claytonc9ed4782011-11-12 02:10:56 +00003022
3023 m_exit_string.assign ("No valid Process");
3024 return eEventActionExit;
Jim Inghambb3a2832011-01-29 01:49:25 +00003025}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003026
Jim Inghambb3a2832011-01-29 01:49:25 +00003027Process::NextEventAction::EventActionResult
3028Process::AttachCompletionHandler::HandleBeingInterrupted()
3029{
3030 return eEventActionSuccess;
3031}
3032
3033const char *
3034Process::AttachCompletionHandler::GetExitString ()
3035{
3036 return m_exit_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003037}
3038
3039Error
Greg Clayton144f3a92011-11-15 03:53:30 +00003040Process::Attach (ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003041{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003042 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003043 m_process_input_reader.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00003044 m_dyld_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003045 m_os_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00003046
Greg Clayton144f3a92011-11-15 03:53:30 +00003047 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Claytone996fd32011-03-08 22:40:15 +00003048 Error error;
Greg Clayton144f3a92011-11-15 03:53:30 +00003049 if (attach_pid == LLDB_INVALID_PROCESS_ID)
Jim Ingham5aee1622010-08-09 23:31:02 +00003050 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003051 char process_name[PATH_MAX];
Jim Ingham4299fdb2011-09-15 01:10:17 +00003052
Greg Clayton144f3a92011-11-15 03:53:30 +00003053 if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name)))
Jim Ingham2ecb7422010-08-17 21:54:19 +00003054 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003055 const bool wait_for_launch = attach_info.GetWaitForLaunch();
3056
3057 if (wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003058 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003059 error = WillAttachToProcessWithName(process_name, wait_for_launch);
3060 if (error.Success())
3061 {
Greg Clayton96249852013-04-18 16:57:27 +00003062 if (m_public_run_lock.WriteTryLock())
Greg Clayton926cce72012-10-12 16:10:12 +00003063 {
3064 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003065 const bool restarted = false;
3066 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003067 // Now attach using these arguments.
3068 error = DoAttachToProcessWithName (process_name, wait_for_launch, attach_info);
3069 }
3070 else
3071 {
3072 // This shouldn't happen
3073 error.SetErrorString("failed to acquire process run lock");
3074 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00003075
Greg Clayton144f3a92011-11-15 03:53:30 +00003076 if (error.Fail())
3077 {
3078 if (GetID() != LLDB_INVALID_PROCESS_ID)
3079 {
3080 SetID (LLDB_INVALID_PROCESS_ID);
3081 if (error.AsCString() == NULL)
3082 error.SetErrorString("attach failed");
3083
3084 SetExitStatus(-1, error.AsCString());
3085 }
3086 }
3087 else
3088 {
3089 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3090 StartPrivateStateThread();
3091 }
3092 return error;
3093 }
Greg Claytone996fd32011-03-08 22:40:15 +00003094 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003095 else
Greg Claytone996fd32011-03-08 22:40:15 +00003096 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003097 ProcessInstanceInfoList process_infos;
3098 PlatformSP platform_sp (m_target.GetPlatform ());
3099
3100 if (platform_sp)
3101 {
3102 ProcessInstanceInfoMatch match_info;
3103 match_info.GetProcessInfo() = attach_info;
3104 match_info.SetNameMatchType (eNameMatchEquals);
3105 platform_sp->FindProcesses (match_info, process_infos);
3106 const uint32_t num_matches = process_infos.GetSize();
3107 if (num_matches == 1)
3108 {
3109 attach_pid = process_infos.GetProcessIDAtIndex(0);
3110 // Fall through and attach using the above process ID
3111 }
3112 else
3113 {
3114 match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name));
3115 if (num_matches > 1)
3116 error.SetErrorStringWithFormat ("more than one process named %s", process_name);
3117 else
3118 error.SetErrorStringWithFormat ("could not find a process named %s", process_name);
3119 }
3120 }
3121 else
3122 {
3123 error.SetErrorString ("invalid platform, can't find processes by name");
3124 return error;
3125 }
Greg Claytone996fd32011-03-08 22:40:15 +00003126 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003127 }
3128 else
Greg Clayton144f3a92011-11-15 03:53:30 +00003129 {
3130 error.SetErrorString ("invalid process name");
Greg Claytone996fd32011-03-08 22:40:15 +00003131 }
3132 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003133
3134 if (attach_pid != LLDB_INVALID_PROCESS_ID)
Greg Claytone996fd32011-03-08 22:40:15 +00003135 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003136 error = WillAttachToProcessWithID(attach_pid);
Greg Claytone996fd32011-03-08 22:40:15 +00003137 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003138 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003139
Greg Clayton96249852013-04-18 16:57:27 +00003140 if (m_public_run_lock.WriteTryLock())
Greg Clayton926cce72012-10-12 16:10:12 +00003141 {
3142 // Now attach using these arguments.
3143 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003144 const bool restarted = false;
3145 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003146 error = DoAttachToProcessWithID (attach_pid, attach_info);
3147 }
3148 else
3149 {
3150 // This shouldn't happen
3151 error.SetErrorString("failed to acquire process run lock");
3152 }
3153
Greg Clayton144f3a92011-11-15 03:53:30 +00003154 if (error.Success())
3155 {
3156
3157 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3158 StartPrivateStateThread();
3159 }
3160 else
Greg Claytone996fd32011-03-08 22:40:15 +00003161 {
3162 if (GetID() != LLDB_INVALID_PROCESS_ID)
3163 {
3164 SetID (LLDB_INVALID_PROCESS_ID);
3165 const char *error_string = error.AsCString();
3166 if (error_string == NULL)
3167 error_string = "attach failed";
3168
3169 SetExitStatus(-1, error_string);
3170 }
3171 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003172 }
3173 }
3174 return error;
3175}
3176
Greg Clayton93d3c8332011-02-16 04:46:07 +00003177void
3178Process::CompleteAttach ()
3179{
3180 // Let the process subclass figure out at much as it can about the process
3181 // before we go looking for a dynamic loader plug-in.
3182 DidAttach();
3183
Jim Ingham4299fdb2011-09-15 01:10:17 +00003184 // We just attached. If we have a platform, ask it for the process architecture, and if it isn't
3185 // the same as the one we've already set, switch architectures.
3186 PlatformSP platform_sp (m_target.GetPlatform ());
3187 assert (platform_sp.get());
3188 if (platform_sp)
3189 {
Greg Clayton70512312012-05-08 01:45:38 +00003190 const ArchSpec &target_arch = m_target.GetArchitecture();
Greg Clayton1e0c8842013-01-11 20:49:54 +00003191 if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL))
Greg Clayton70512312012-05-08 01:45:38 +00003192 {
3193 ArchSpec platform_arch;
3194 platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch);
3195 if (platform_sp)
3196 {
3197 m_target.SetPlatform (platform_sp);
3198 m_target.SetArchitecture(platform_arch);
3199 }
3200 }
3201 else
3202 {
3203 ProcessInstanceInfo process_info;
3204 platform_sp->GetProcessInfo (GetID(), process_info);
3205 const ArchSpec &process_arch = process_info.GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +00003206 if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch))
Greg Clayton70512312012-05-08 01:45:38 +00003207 m_target.SetArchitecture (process_arch);
3208 }
Jim Ingham4299fdb2011-09-15 01:10:17 +00003209 }
3210
3211 // We have completed the attach, now it is time to find the dynamic loader
Greg Clayton93d3c8332011-02-16 04:46:07 +00003212 // plug-in
Greg Claytonc859e2d2012-02-13 23:10:39 +00003213 DynamicLoader *dyld = GetDynamicLoader ();
3214 if (dyld)
3215 dyld->DidAttach();
Greg Clayton93d3c8332011-02-16 04:46:07 +00003216
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003217 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Clayton93d3c8332011-02-16 04:46:07 +00003218 // Figure out which one is the executable, and set that in our target:
Enrico Granata17598482012-11-08 02:22:02 +00003219 const ModuleList &target_modules = m_target.GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003220 Mutex::Locker modules_locker(target_modules.GetMutex());
3221 size_t num_modules = target_modules.GetSize();
3222 ModuleSP new_executable_module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003223
Greg Clayton93d3c8332011-02-16 04:46:07 +00003224 for (int i = 0; i < num_modules; i++)
3225 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003226 ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i));
Greg Clayton8b82f082011-04-12 05:54:46 +00003227 if (module_sp && module_sp->IsExecutable())
Greg Clayton93d3c8332011-02-16 04:46:07 +00003228 {
Greg Claytonaa149cb2011-08-11 02:48:45 +00003229 if (m_target.GetExecutableModulePointer() != module_sp.get())
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003230 new_executable_module_sp = module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003231 break;
3232 }
3233 }
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003234 if (new_executable_module_sp)
3235 m_target.SetExecutableModule (new_executable_module_sp, false);
Greg Clayton93d3c8332011-02-16 04:46:07 +00003236}
3237
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003238Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003239Process::ConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +00003240{
Greg Claytonb766a732011-02-04 01:58:07 +00003241 m_abi_sp.reset();
3242 m_process_input_reader.reset();
3243
3244 // Find the process and its architecture. Make sure it matches the architecture
3245 // of the current Target, and if not adjust it.
3246
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003247 Error error (DoConnectRemote (strm, remote_url));
Greg Claytonb766a732011-02-04 01:58:07 +00003248 if (error.Success())
3249 {
Greg Clayton71337622011-02-24 22:24:29 +00003250 if (GetID() != LLDB_INVALID_PROCESS_ID)
3251 {
Greg Clayton32e0a752011-03-30 18:16:51 +00003252 EventSP event_sp;
3253 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
3254
3255 if (state == eStateStopped || state == eStateCrashed)
3256 {
3257 // If we attached and actually have a process on the other end, then
3258 // this ended up being the equivalent of an attach.
3259 CompleteAttach ();
3260
3261 // This delays passing the stopped event to listeners till
3262 // CompleteAttach gets a chance to complete...
3263 HandlePrivateEvent (event_sp);
3264
3265 }
Greg Clayton71337622011-02-24 22:24:29 +00003266 }
Greg Clayton32e0a752011-03-30 18:16:51 +00003267
3268 if (PrivateStateThreadIsValid ())
3269 ResumePrivateStateThread ();
3270 else
3271 StartPrivateStateThread ();
Greg Claytonb766a732011-02-04 01:58:07 +00003272 }
3273 return error;
3274}
3275
3276
3277Error
Jim Ingham3b8285d2012-04-19 01:40:33 +00003278Process::PrivateResume ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003279{
Greg Clayton5160ce52013-03-27 23:08:40 +00003280 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003281 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003282 log->Printf("Process::PrivateResume() m_stop_id = %u, public state: %s private state: %s",
Jim Ingham4b536182011-08-09 02:12:22 +00003283 m_mod_id.GetStopID(),
Jim Ingham444586b2011-01-24 06:34:17 +00003284 StateAsCString(m_public_state.GetValue()),
3285 StateAsCString(m_private_state.GetValue()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003286
3287 Error error (WillResume());
3288 // Tell the process it is about to resume before the thread list
3289 if (error.Success())
3290 {
Johnny Chenc4221e42010-12-02 20:53:05 +00003291 // Now let the thread list know we are about to resume so it
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003292 // can let all of our threads know that they are about to be
3293 // resumed. Threads will each be called with
3294 // Thread::WillResume(StateType) where StateType contains the state
3295 // that they are supposed to have when the process is resumed
3296 // (suspended/running/stepping). Threads should also check
3297 // their resume signal in lldb::Thread::GetResumeSignal()
Jim Ingham221d51c2013-05-08 00:35:16 +00003298 // to see if they are supposed to start back up with a signal.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003299 if (m_thread_list.WillResume())
3300 {
Jim Ingham372787f2012-04-07 00:00:41 +00003301 // Last thing, do the PreResumeActions.
3302 if (!RunPreResumeActions())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003303 {
Jim Ingham0161b492013-02-09 01:29:05 +00003304 error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming.");
Jim Ingham372787f2012-04-07 00:00:41 +00003305 }
3306 else
3307 {
3308 m_mod_id.BumpResumeID();
3309 error = DoResume();
3310 if (error.Success())
3311 {
3312 DidResume();
3313 m_thread_list.DidResume();
3314 if (log)
3315 log->Printf ("Process thinks the process has resumed.");
3316 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003317 }
3318 }
3319 else
3320 {
Jim Ingham513c6bb2012-09-01 01:02:41 +00003321 // Somebody wanted to run without running. So generate a continue & a stopped event,
3322 // and let the world handle them.
3323 if (log)
3324 log->Printf ("Process::PrivateResume() asked to simulate a start & stop.");
3325
3326 SetPrivateState(eStateRunning);
3327 SetPrivateState(eStateStopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003328 }
3329 }
Jim Ingham444586b2011-01-24 06:34:17 +00003330 else if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003331 log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003332 return error;
3333}
3334
3335Error
Greg Claytonf9b57b92013-05-10 23:48:10 +00003336Process::Halt (bool clear_thread_plans)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003337{
Greg Claytonf9b57b92013-05-10 23:48:10 +00003338 // Don't clear the m_clear_thread_plans_on_stop, only set it to true if
3339 // in case it was already set and some thread plan logic calls halt on its
3340 // own.
3341 m_clear_thread_plans_on_stop |= clear_thread_plans;
3342
Jim Inghamaacc3182012-06-06 00:29:30 +00003343 // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since
3344 // we could just straightaway get another event. It just narrows the window...
3345 m_currently_handling_event.WaitForValueEqualTo(false);
3346
3347
Jim Inghambb3a2832011-01-29 01:49:25 +00003348 // Pause our private state thread so we can ensure no one else eats
3349 // the stop event out from under us.
Jim Ingham0f16e732011-02-08 05:20:59 +00003350 Listener halt_listener ("lldb.process.halt_listener");
3351 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton3af9ea52010-11-18 05:57:03 +00003352
Jim Inghambb3a2832011-01-29 01:49:25 +00003353 EventSP event_sp;
Greg Clayton513c26c2011-01-29 07:10:55 +00003354 Error error (WillHalt());
Jim Inghambb3a2832011-01-29 01:49:25 +00003355
Greg Clayton513c26c2011-01-29 07:10:55 +00003356 if (error.Success())
Jim Inghambb3a2832011-01-29 01:49:25 +00003357 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003358
Greg Clayton513c26c2011-01-29 07:10:55 +00003359 bool caused_stop = false;
3360
3361 // Ask the process subclass to actually halt our process
3362 error = DoHalt(caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003363 if (error.Success())
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003364 {
Greg Clayton513c26c2011-01-29 07:10:55 +00003365 if (m_public_state.GetValue() == eStateAttaching)
3366 {
3367 SetExitStatus(SIGKILL, "Cancelled async attach.");
3368 Destroy ();
3369 }
3370 else
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003371 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003372 // If "caused_stop" is true, then DoHalt stopped the process. If
3373 // "caused_stop" is false, the process was already stopped.
3374 // If the DoHalt caused the process to stop, then we want to catch
3375 // this event and set the interrupted bool to true before we pass
3376 // this along so clients know that the process was interrupted by
3377 // a halt command.
3378 if (caused_stop)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003379 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003380 // Wait for 1 second for the process to stop.
Jim Inghambb3a2832011-01-29 01:49:25 +00003381 TimeValue timeout_time;
3382 timeout_time = TimeValue::Now();
3383 timeout_time.OffsetWithSeconds(1);
Jim Ingham0f16e732011-02-08 05:20:59 +00003384 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
3385 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003386
Jim Ingham0f16e732011-02-08 05:20:59 +00003387 if (!got_event || state == eStateInvalid)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003388 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003389 // We timeout out and didn't get a stop event...
Jim Ingham0f16e732011-02-08 05:20:59 +00003390 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton3af9ea52010-11-18 05:57:03 +00003391 }
3392 else
3393 {
Greg Clayton2637f822011-11-17 01:23:07 +00003394 if (StateIsStoppedState (state, false))
Jim Inghambb3a2832011-01-29 01:49:25 +00003395 {
3396 // We caused the process to interrupt itself, so mark this
3397 // as such in the stop event so clients can tell an interrupted
3398 // process from a natural stop
3399 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
3400 }
3401 else
3402 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003403 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghambb3a2832011-01-29 01:49:25 +00003404 if (log)
3405 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
3406 error.SetErrorString ("Did not get stopped event after halt.");
3407 }
Greg Clayton3af9ea52010-11-18 05:57:03 +00003408 }
3409 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003410 DidHalt();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003411 }
3412 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003413 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003414 // Resume our private state thread before we post the event (if any)
Jim Ingham0f16e732011-02-08 05:20:59 +00003415 RestorePrivateProcessEvents();
Jim Inghambb3a2832011-01-29 01:49:25 +00003416
3417 // Post any event we might have consumed. If all goes well, we will have
3418 // stopped the process, intercepted the event and set the interrupted
3419 // bool in the event. Post it to the private event queue and that will end up
3420 // correctly setting the state.
3421 if (event_sp)
3422 m_private_state_broadcaster.BroadcastEvent(event_sp);
3423
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003424 return error;
3425}
3426
3427Error
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003428Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp)
3429{
3430 Error error;
3431 if (m_public_state.GetValue() == eStateRunning)
3432 {
3433 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3434 if (log)
3435 log->Printf("Process::Destroy() About to halt.");
3436 error = Halt();
3437 if (error.Success())
3438 {
3439 // Consume the halt event.
3440 TimeValue timeout (TimeValue::Now());
3441 timeout.OffsetWithSeconds(1);
3442 StateType state = WaitForProcessToStop (&timeout, &exit_event_sp);
3443
3444 // If the process exited while we were waiting for it to stop, put the exited event into
3445 // the shared pointer passed in and return. Our caller doesn't need to do anything else, since
3446 // they don't have a process anymore...
3447
3448 if (state == eStateExited || m_private_state.GetValue() == eStateExited)
3449 {
3450 if (log)
3451 log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt.");
3452 return error;
3453 }
3454 else
3455 exit_event_sp.reset(); // It is ok to consume any non-exit stop events
3456
3457 if (state != eStateStopped)
3458 {
3459 if (log)
3460 log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state));
3461 // If we really couldn't stop the process then we should just error out here, but if the
3462 // lower levels just bobbled sending the event and we really are stopped, then continue on.
3463 StateType private_state = m_private_state.GetValue();
3464 if (private_state != eStateStopped)
3465 {
3466 return error;
3467 }
3468 }
3469 }
3470 else
3471 {
3472 if (log)
3473 log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString());
3474 }
3475 }
3476 return error;
3477}
3478
3479Error
Jim Inghamacff8952013-05-02 00:27:30 +00003480Process::Detach (bool keep_stopped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003481{
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003482 EventSP exit_event_sp;
3483 Error error;
3484 m_destroy_in_process = true;
3485
3486 error = WillDetach();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003487
3488 if (error.Success())
3489 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003490 if (DetachRequiresHalt())
3491 {
3492 error = HaltForDestroyOrDetach (exit_event_sp);
3493 if (!error.Success())
3494 {
3495 m_destroy_in_process = false;
3496 return error;
3497 }
3498 else if (exit_event_sp)
3499 {
3500 // We shouldn't need to do anything else here. There's no process left to detach from...
3501 StopPrivateStateThread();
3502 m_destroy_in_process = false;
3503 return error;
3504 }
3505 }
3506
Jim Inghamacff8952013-05-02 00:27:30 +00003507 error = DoDetach(keep_stopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003508 if (error.Success())
3509 {
3510 DidDetach();
3511 StopPrivateStateThread();
3512 }
Jim Inghamacff8952013-05-02 00:27:30 +00003513 else
3514 {
3515 return error;
3516 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003517 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003518 m_destroy_in_process = false;
3519
3520 // If we exited when we were waiting for a process to stop, then
3521 // forward the event here so we don't lose the event
3522 if (exit_event_sp)
3523 {
3524 // Directly broadcast our exited event because we shut down our
3525 // private state thread above
3526 BroadcastEvent(exit_event_sp);
3527 }
3528
3529 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3530 // the last events through the event system, in which case we might strand the write lock. Unlock
3531 // it here so when we do to tear down the process we don't get an error destroying the lock.
3532
Greg Clayton96249852013-04-18 16:57:27 +00003533 m_public_run_lock.WriteUnlock();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003534 return error;
3535}
3536
3537Error
3538Process::Destroy ()
3539{
Jim Ingham09437922013-03-01 20:04:25 +00003540
3541 // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work
3542 // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt
3543 // failed and the process stays around for some reason it won't be in a confused state.
3544
3545 m_destroy_in_process = true;
3546
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003547 Error error (WillDestroy());
3548 if (error.Success())
3549 {
Greg Clayton85fb1b92012-09-11 02:33:37 +00003550 EventSP exit_event_sp;
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003551 if (DestroyRequiresHalt())
Jim Ingham04e0a222012-05-23 15:46:31 +00003552 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003553 error = HaltForDestroyOrDetach(exit_event_sp);
Jim Ingham04e0a222012-05-23 15:46:31 +00003554 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003555
Jim Inghamaacc3182012-06-06 00:29:30 +00003556 if (m_public_state.GetValue() != eStateRunning)
3557 {
3558 // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to
3559 // kill it, we don't want it hitting a breakpoint...
3560 // Only do this if we've stopped, however, since if we didn't manage to halt it above, then
3561 // we're not going to have much luck doing this now.
3562 m_thread_list.DiscardThreadPlans();
3563 DisableAllBreakpointSites();
3564 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003565
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003566 error = DoDestroy();
3567 if (error.Success())
3568 {
3569 DidDestroy();
3570 StopPrivateStateThread();
3571 }
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003572 m_stdio_communication.StopReadThread();
3573 m_stdio_communication.Disconnect();
3574 if (m_process_input_reader && m_process_input_reader->IsActive())
3575 m_target.GetDebugger().PopInputReader (m_process_input_reader);
3576 if (m_process_input_reader)
3577 m_process_input_reader.reset();
Greg Clayton85fb1b92012-09-11 02:33:37 +00003578
3579 // If we exited when we were waiting for a process to stop, then
3580 // forward the event here so we don't lose the event
3581 if (exit_event_sp)
3582 {
3583 // Directly broadcast our exited event because we shut down our
3584 // private state thread above
3585 BroadcastEvent(exit_event_sp);
3586 }
3587
Jim Inghamb1e2e842012-04-12 18:49:31 +00003588 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3589 // the last events through the event system, in which case we might strand the write lock. Unlock
3590 // it here so when we do to tear down the process we don't get an error destroying the lock.
Greg Clayton96249852013-04-18 16:57:27 +00003591 m_public_run_lock.WriteUnlock();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003592 }
Jim Ingham09437922013-03-01 20:04:25 +00003593
3594 m_destroy_in_process = false;
3595
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003596 return error;
3597}
3598
3599Error
3600Process::Signal (int signal)
3601{
3602 Error error (WillSignal());
3603 if (error.Success())
3604 {
3605 error = DoSignal(signal);
3606 if (error.Success())
3607 DidSignal();
3608 }
3609 return error;
3610}
3611
Greg Clayton514487e2011-02-15 21:59:32 +00003612lldb::ByteOrder
3613Process::GetByteOrder () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003614{
Greg Clayton514487e2011-02-15 21:59:32 +00003615 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003616}
3617
3618uint32_t
Greg Clayton514487e2011-02-15 21:59:32 +00003619Process::GetAddressByteSize () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003620{
Greg Clayton514487e2011-02-15 21:59:32 +00003621 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003622}
3623
Greg Clayton514487e2011-02-15 21:59:32 +00003624
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003625bool
3626Process::ShouldBroadcastEvent (Event *event_ptr)
3627{
3628 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
3629 bool return_value = true;
Greg Clayton5160ce52013-03-27 23:08:40 +00003630 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS));
Jim Ingham0161b492013-02-09 01:29:05 +00003631
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003632 switch (state)
3633 {
Greg Claytonb766a732011-02-04 01:58:07 +00003634 case eStateConnected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003635 case eStateAttaching:
3636 case eStateLaunching:
3637 case eStateDetached:
3638 case eStateExited:
3639 case eStateUnloaded:
3640 // These events indicate changes in the state of the debugging session, always report them.
3641 return_value = true;
3642 break;
3643 case eStateInvalid:
3644 // We stopped for no apparent reason, don't report it.
3645 return_value = false;
3646 break;
3647 case eStateRunning:
3648 case eStateStepping:
3649 // If we've started the target running, we handle the cases where we
3650 // are already running and where there is a transition from stopped to
3651 // running differently.
3652 // running -> running: Automatically suppress extra running events
3653 // stopped -> running: Report except when there is one or more no votes
3654 // and no yes votes.
3655 SynchronouslyNotifyStateChanged (state);
Jim Ingham0161b492013-02-09 01:29:05 +00003656 switch (m_last_broadcast_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003657 {
3658 case eStateRunning:
3659 case eStateStepping:
3660 // We always suppress multiple runnings with no PUBLIC stop in between.
3661 return_value = false;
3662 break;
3663 default:
3664 // TODO: make this work correctly. For now always report
3665 // run if we aren't running so we don't miss any runnning
3666 // events. If I run the lldb/test/thread/a.out file and
3667 // break at main.cpp:58, run and hit the breakpoints on
3668 // multiple threads, then somehow during the stepping over
3669 // of all breakpoints no run gets reported.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003670
3671 // This is a transition from stop to run.
3672 switch (m_thread_list.ShouldReportRun (event_ptr))
3673 {
3674 case eVoteYes:
3675 case eVoteNoOpinion:
3676 return_value = true;
3677 break;
3678 case eVoteNo:
3679 return_value = false;
3680 break;
3681 }
3682 break;
3683 }
3684 break;
3685 case eStateStopped:
3686 case eStateCrashed:
3687 case eStateSuspended:
3688 {
3689 // We've stopped. First see if we're going to restart the target.
3690 // If we are going to stop, then we always broadcast the event.
3691 // If we aren't going to stop, let the thread plans decide if we're going to report this event.
Jim Inghamb01e7422010-06-19 04:45:32 +00003692 // If no thread has an opinion, we don't report it.
Jim Ingham221d51c2013-05-08 00:35:16 +00003693
Jim Inghamcb4ca112012-05-16 01:32:14 +00003694 RefreshStateAfterStop ();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003695 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003696 {
Greg Clayton3af9ea52010-11-18 05:57:03 +00003697 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003698 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s",
3699 event_ptr,
3700 StateAsCString(state));
3701 return_value = true;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003702 }
3703 else
3704 {
Jim Ingham221d51c2013-05-08 00:35:16 +00003705 bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr);
3706 bool should_resume = false;
3707
Jim Ingham0161b492013-02-09 01:29:05 +00003708 // It makes no sense to ask "ShouldStop" if we've already been restarted...
3709 // Asking the thread list is also not likely to go well, since we are running again.
3710 // So in that case just report the event.
3711
Jim Ingham0161b492013-02-09 01:29:05 +00003712 if (!was_restarted)
3713 should_resume = m_thread_list.ShouldStop (event_ptr) == false;
Jim Ingham221d51c2013-05-08 00:35:16 +00003714
3715 if (was_restarted || should_resume || m_resume_requested)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003716 {
Jim Ingham0161b492013-02-09 01:29:05 +00003717 Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr);
3718 if (log)
3719 log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.",
3720 should_resume,
3721 StateAsCString(state),
3722 was_restarted,
3723 stop_vote);
3724
3725 switch (stop_vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003726 {
3727 case eVoteYes:
Jim Ingham0161b492013-02-09 01:29:05 +00003728 return_value = true;
3729 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003730 case eVoteNoOpinion:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003731 case eVoteNo:
3732 return_value = false;
3733 break;
3734 }
Jim Ingham0161b492013-02-09 01:29:05 +00003735
Jim Inghamcb95f342012-09-05 21:13:56 +00003736 if (!was_restarted)
Jim Ingham0161b492013-02-09 01:29:05 +00003737 {
3738 if (log)
3739 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state));
3740 ProcessEventData::SetRestartedInEvent(event_ptr, true);
Jim Inghamcb95f342012-09-05 21:13:56 +00003741 PrivateResume ();
Jim Ingham0161b492013-02-09 01:29:05 +00003742 }
3743
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003744 }
3745 else
3746 {
3747 return_value = true;
3748 SynchronouslyNotifyStateChanged (state);
3749 }
3750 }
3751 }
Jim Ingham0161b492013-02-09 01:29:05 +00003752 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003753 }
Jim Ingham0161b492013-02-09 01:29:05 +00003754
3755 // We do some coalescing of events (for instance two consecutive running events get coalesced.)
3756 // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state
3757 // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done,
3758 // because the PublicState reflects the last event pulled off the queue, and there may be several
3759 // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event
3760 // yet. m_last_broadcast_state gets updated here.
3761
3762 if (return_value)
3763 m_last_broadcast_state = state;
3764
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003765 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003766 log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s",
3767 event_ptr,
3768 StateAsCString(state),
3769 StateAsCString(m_last_broadcast_state),
3770 return_value ? "YES" : "NO");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003771 return return_value;
3772}
3773
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003774
3775bool
Jim Ingham372787f2012-04-07 00:00:41 +00003776Process::StartPrivateStateThread (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003777{
Greg Clayton5160ce52013-03-27 23:08:40 +00003778 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003779
Greg Clayton8b82f082011-04-12 05:54:46 +00003780 bool already_running = PrivateStateThreadIsValid ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003781 if (log)
Greg Clayton8b82f082011-04-12 05:54:46 +00003782 log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread");
3783
Jim Ingham372787f2012-04-07 00:00:41 +00003784 if (!force && already_running)
Greg Clayton8b82f082011-04-12 05:54:46 +00003785 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003786
3787 // Create a thread that watches our internal state and controls which
3788 // events make it to clients (into the DCProcess event queue).
Greg Clayton3e06bd92011-01-09 21:07:35 +00003789 char thread_name[1024];
Jim Ingham372787f2012-04-07 00:00:41 +00003790 if (already_running)
Daniel Malead01b2952012-11-29 21:49:15 +00003791 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
Jim Ingham372787f2012-04-07 00:00:41 +00003792 else
Daniel Malead01b2952012-11-29 21:49:15 +00003793 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
Jim Ingham076b3042012-04-10 01:21:57 +00003794
3795 // Create the private state thread, and start it running.
Greg Clayton3e06bd92011-01-09 21:07:35 +00003796 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Jim Ingham076b3042012-04-10 01:21:57 +00003797 bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
3798 if (success)
3799 {
3800 ResumePrivateStateThread();
3801 return true;
3802 }
3803 else
3804 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003805}
3806
3807void
3808Process::PausePrivateStateThread ()
3809{
3810 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
3811}
3812
3813void
3814Process::ResumePrivateStateThread ()
3815{
3816 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
3817}
3818
3819void
3820Process::StopPrivateStateThread ()
3821{
Greg Clayton8b82f082011-04-12 05:54:46 +00003822 if (PrivateStateThreadIsValid ())
3823 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
Jim Inghamb1e2e842012-04-12 18:49:31 +00003824 else
3825 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003826 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Jim Inghamb1e2e842012-04-12 18:49:31 +00003827 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003828 log->Printf ("Went to stop the private state thread, but it was already invalid.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00003829 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003830}
3831
3832void
3833Process::ControlPrivateStateThread (uint32_t signal)
3834{
Greg Clayton5160ce52013-03-27 23:08:40 +00003835 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003836
3837 assert (signal == eBroadcastInternalStateControlStop ||
3838 signal == eBroadcastInternalStateControlPause ||
3839 signal == eBroadcastInternalStateControlResume);
3840
3841 if (log)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003842 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003843
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003844 // Signal the private state thread. First we should copy this is case the
3845 // thread starts exiting since the private state thread will NULL this out
3846 // when it exits
3847 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton2da6d492011-02-08 01:34:25 +00003848 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003849 {
3850 TimeValue timeout_time;
3851 bool timed_out;
3852
3853 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
3854
3855 timeout_time = TimeValue::Now();
3856 timeout_time.OffsetWithSeconds(2);
Jim Inghamb1e2e842012-04-12 18:49:31 +00003857 if (log)
3858 log->Printf ("Sending control event of type: %d.", signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003859 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
3860 m_private_state_control_wait.SetValue (false, eBroadcastNever);
3861
3862 if (signal == eBroadcastInternalStateControlStop)
3863 {
3864 if (timed_out)
Jim Inghamb1e2e842012-04-12 18:49:31 +00003865 {
3866 Error error;
3867 Host::ThreadCancel (private_state_thread, &error);
3868 if (log)
3869 log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString());
3870 }
3871 else
3872 {
3873 if (log)
3874 log->Printf ("The control event killed the private state thread without having to cancel.");
3875 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003876
3877 thread_result_t result = NULL;
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003878 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Clayton49182ed2010-07-22 18:34:21 +00003879 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003880 }
3881 }
Jim Inghamb1e2e842012-04-12 18:49:31 +00003882 else
3883 {
3884 if (log)
3885 log->Printf ("Private state thread already dead, no need to signal it to stop.");
3886 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003887}
3888
3889void
Jim Inghamcfc09352012-07-27 23:57:19 +00003890Process::SendAsyncInterrupt ()
3891{
3892 if (PrivateStateThreadIsValid())
3893 m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
3894 else
3895 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
3896}
3897
3898void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003899Process::HandlePrivateEvent (EventSP &event_sp)
3900{
Greg Clayton5160ce52013-03-27 23:08:40 +00003901 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham221d51c2013-05-08 00:35:16 +00003902 m_resume_requested = false;
3903
Jim Inghamaacc3182012-06-06 00:29:30 +00003904 m_currently_handling_event.SetValue(true, eBroadcastNever);
Jim Inghambb3a2832011-01-29 01:49:25 +00003905
Greg Clayton414f5d32011-01-25 02:58:48 +00003906 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003907
3908 // First check to see if anybody wants a shot at this event:
Jim Ingham754ab982011-01-29 04:05:41 +00003909 if (m_next_event_action_ap.get() != NULL)
Jim Inghambb3a2832011-01-29 01:49:25 +00003910 {
Jim Ingham754ab982011-01-29 04:05:41 +00003911 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Ingham0161b492013-02-09 01:29:05 +00003912 if (log)
3913 log->Printf ("Ran next event action, result was %d.", action_result);
3914
Jim Inghambb3a2832011-01-29 01:49:25 +00003915 switch (action_result)
3916 {
3917 case NextEventAction::eEventActionSuccess:
3918 SetNextEventAction(NULL);
3919 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003920
Jim Inghambb3a2832011-01-29 01:49:25 +00003921 case NextEventAction::eEventActionRetry:
3922 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003923
Jim Inghambb3a2832011-01-29 01:49:25 +00003924 case NextEventAction::eEventActionExit:
Jim Ingham2a5fdd42011-01-29 01:57:31 +00003925 // Handle Exiting Here. If we already got an exited event,
3926 // we should just propagate it. Otherwise, swallow this event,
3927 // and set our state to exit so the next event will kill us.
3928 if (new_state != eStateExited)
3929 {
3930 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham754ab982011-01-29 04:05:41 +00003931 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham221d51c2013-05-08 00:35:16 +00003932 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Jim Ingham2a5fdd42011-01-29 01:57:31 +00003933 SetNextEventAction(NULL);
3934 return;
3935 }
3936 SetNextEventAction(NULL);
Jim Inghambb3a2832011-01-29 01:49:25 +00003937 break;
3938 }
3939 }
3940
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003941 // See if we should broadcast this state to external clients?
3942 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003943
3944 if (should_broadcast)
3945 {
3946 if (log)
3947 {
Daniel Malead01b2952012-11-29 21:49:15 +00003948 log->Printf ("Process::%s (pid = %" PRIu64 ") broadcasting new state %s (old state %s) to %s",
Greg Clayton414f5d32011-01-25 02:58:48 +00003949 __FUNCTION__,
3950 GetID(),
3951 StateAsCString(new_state),
3952 StateAsCString (GetState ()),
3953 IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003954 }
Jim Ingham9575d842011-03-11 03:53:59 +00003955 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton414f5d32011-01-25 02:58:48 +00003956 if (StateIsRunningState (new_state))
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003957 PushProcessInputReader ();
3958 else
3959 PopProcessInputReader ();
Jim Ingham9575d842011-03-11 03:53:59 +00003960
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003961 BroadcastEvent (event_sp);
3962 }
3963 else
3964 {
3965 if (log)
3966 {
Daniel Malead01b2952012-11-29 21:49:15 +00003967 log->Printf ("Process::%s (pid = %" PRIu64 ") suppressing state %s (old state %s): should_broadcast == false",
Greg Clayton414f5d32011-01-25 02:58:48 +00003968 __FUNCTION__,
3969 GetID(),
3970 StateAsCString(new_state),
Jason Molendafd54b362011-09-20 21:44:10 +00003971 StateAsCString (GetState ()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003972 }
3973 }
Jim Inghamaacc3182012-06-06 00:29:30 +00003974 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003975}
3976
3977void *
3978Process::PrivateStateThread (void *arg)
3979{
3980 Process *proc = static_cast<Process*> (arg);
3981 void *result = proc->RunPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003982 return result;
3983}
3984
3985void *
3986Process::RunPrivateStateThread ()
3987{
Jim Ingham076b3042012-04-10 01:21:57 +00003988 bool control_only = true;
Jim Inghamb1e2e842012-04-12 18:49:31 +00003989 m_private_state_control_wait.SetValue (false, eBroadcastNever);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003990
Greg Clayton5160ce52013-03-27 23:08:40 +00003991 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003992 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00003993 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, this, GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003994
3995 bool exit_now = false;
3996 while (!exit_now)
3997 {
3998 EventSP event_sp;
3999 WaitForEventsPrivate (NULL, event_sp, control_only);
4000 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
4001 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00004002 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00004003 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType());
Jim Inghamb1e2e842012-04-12 18:49:31 +00004004
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004005 switch (event_sp->GetType())
4006 {
4007 case eBroadcastInternalStateControlStop:
4008 exit_now = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004009 break; // doing any internal state managment below
4010
4011 case eBroadcastInternalStateControlPause:
4012 control_only = true;
4013 break;
4014
4015 case eBroadcastInternalStateControlResume:
4016 control_only = false;
4017 break;
4018 }
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004019
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004020 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004021 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004022 }
Jim Inghamcfc09352012-07-27 23:57:19 +00004023 else if (event_sp->GetType() == eBroadcastBitInterrupt)
4024 {
4025 if (m_public_state.GetValue() == eStateAttaching)
4026 {
4027 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00004028 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.", __FUNCTION__, this, GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004029 BroadcastEvent (eBroadcastBitInterrupt, NULL);
4030 }
4031 else
4032 {
4033 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00004034 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.", __FUNCTION__, this, GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004035 Halt();
4036 }
4037 continue;
4038 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004039
4040 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
4041
4042 if (internal_state != eStateInvalid)
4043 {
Greg Claytonf9b57b92013-05-10 23:48:10 +00004044 if (m_clear_thread_plans_on_stop &&
4045 StateIsStoppedState(internal_state, true))
4046 {
4047 m_clear_thread_plans_on_stop = false;
4048 m_thread_list.DiscardThreadPlans();
4049 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004050 HandlePrivateEvent (event_sp);
4051 }
4052
Greg Clayton58d1c9a2010-10-18 04:14:23 +00004053 if (internal_state == eStateInvalid ||
4054 internal_state == eStateExited ||
4055 internal_state == eStateDetached )
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004056 {
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004057 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00004058 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state));
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004059
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004060 break;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004061 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004062 }
4063
Caroline Tice20ad3c42010-10-29 21:48:37 +00004064 // Verify log is still enabled before attempting to write to it...
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004065 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00004066 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, this, GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004067
Greg Clayton96249852013-04-18 16:57:27 +00004068 m_public_run_lock.WriteUnlock();
Greg Clayton6ed95942011-01-22 07:12:45 +00004069 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
4070 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004071 return NULL;
4072}
4073
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004074//------------------------------------------------------------------
4075// Process Event Data
4076//------------------------------------------------------------------
4077
4078Process::ProcessEventData::ProcessEventData () :
4079 EventData (),
4080 m_process_sp (),
4081 m_state (eStateInvalid),
Greg Claytonc982c762010-07-09 20:39:50 +00004082 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004083 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004084 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004085{
4086}
4087
4088Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
4089 EventData (),
4090 m_process_sp (process_sp),
4091 m_state (state),
Greg Claytonc982c762010-07-09 20:39:50 +00004092 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004093 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004094 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004095{
4096}
4097
4098Process::ProcessEventData::~ProcessEventData()
4099{
4100}
4101
4102const ConstString &
4103Process::ProcessEventData::GetFlavorString ()
4104{
4105 static ConstString g_flavor ("Process::ProcessEventData");
4106 return g_flavor;
4107}
4108
4109const ConstString &
4110Process::ProcessEventData::GetFlavor () const
4111{
4112 return ProcessEventData::GetFlavorString ();
4113}
4114
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004115void
4116Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
4117{
4118 // This function gets called twice for each event, once when the event gets pulled
Jim Inghama8604692011-05-22 21:45:01 +00004119 // off of the private process event queue, and then any number of times, first when it gets pulled off of
4120 // the public event queue, then other times when we're pretending that this is where we stopped at the
4121 // end of expression evaluation. m_update_state is used to distinguish these
4122 // three cases; it is 0 when we're just pulling it off for private handling,
Jim Ingham221d51c2013-05-08 00:35:16 +00004123 // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then.
Jim Inghama8604692011-05-22 21:45:01 +00004124 if (m_update_state != 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004125 return;
Jim Ingham0161b492013-02-09 01:29:05 +00004126
Jim Ingham221d51c2013-05-08 00:35:16 +00004127 m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004128
4129 // If we're stopped and haven't restarted, then do the breakpoint commands here:
4130 if (m_state == eStateStopped && ! m_restarted)
Jim Ingham0faa43f2011-11-08 03:00:11 +00004131 {
4132 ThreadList &curr_thread_list = m_process_sp->GetThreadList();
Greg Clayton61e7a582011-12-01 23:28:38 +00004133 uint32_t num_threads = curr_thread_list.GetSize();
4134 uint32_t idx;
Greg Claytonf4b47e12010-08-04 01:40:35 +00004135
Jim Ingham4b536182011-08-09 02:12:22 +00004136 // The actions might change one of the thread's stop_info's opinions about whether we should
4137 // stop the process, so we need to query that as we go.
Jim Ingham0faa43f2011-11-08 03:00:11 +00004138
4139 // One other complication here, is that we try to catch any case where the target has run (except for expressions)
4140 // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and
4141 // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like
4142 // 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
4143 // against this list & bag out if anything differs.
Greg Clayton61e7a582011-12-01 23:28:38 +00004144 std::vector<uint32_t> thread_index_array(num_threads);
Jim Ingham0faa43f2011-11-08 03:00:11 +00004145 for (idx = 0; idx < num_threads; ++idx)
4146 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID();
4147
Jim Inghamc7078c22012-12-13 22:24:15 +00004148 // Use this to track whether we should continue from here. We will only continue the target running if
4149 // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running,
4150 // then it doesn't matter what the other threads say...
4151
4152 bool still_should_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +00004153
Jim Ingham0ad7e052013-04-25 02:04:59 +00004154 // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a
4155 // valid stop reason. In that case we should just stop, because we have no way of telling what the right
4156 // thing to do is, and it's better to let the user decide than continue behind their backs.
4157
4158 bool does_anybody_have_an_opinion = false;
4159
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004160 for (idx = 0; idx < num_threads; ++idx)
4161 {
Jim Ingham0faa43f2011-11-08 03:00:11 +00004162 curr_thread_list = m_process_sp->GetThreadList();
4163 if (curr_thread_list.GetSize() != num_threads)
4164 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004165 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004166 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004167 log->Printf("Number of threads changed from %u to %u while processing event.", num_threads, curr_thread_list.GetSize());
Jim Ingham0faa43f2011-11-08 03:00:11 +00004168 break;
4169 }
4170
4171 lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx);
4172
4173 if (thread_sp->GetIndexID() != thread_index_array[idx])
4174 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004175 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004176 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004177 log->Printf("The thread at position %u changed from %u to %u while processing event.",
Jim Ingham87c665f2011-12-01 20:26:15 +00004178 idx,
4179 thread_index_array[idx],
4180 thread_sp->GetIndexID());
Jim Ingham0faa43f2011-11-08 03:00:11 +00004181 break;
4182 }
4183
Jim Inghamb15bfc72010-10-20 00:39:53 +00004184 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00004185 if (stop_info_sp && stop_info_sp->IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004186 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004187 does_anybody_have_an_opinion = true;
Jim Ingham0161b492013-02-09 01:29:05 +00004188 bool this_thread_wants_to_stop;
4189 if (stop_info_sp->GetOverrideShouldStop())
Jim Ingham4b536182011-08-09 02:12:22 +00004190 {
Jim Ingham0161b492013-02-09 01:29:05 +00004191 this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue();
4192 }
4193 else
4194 {
4195 stop_info_sp->PerformAction(event_ptr);
4196 // The stop action might restart the target. If it does, then we want to mark that in the
4197 // event so that whoever is receiving it will know to wait for the running event and reflect
4198 // that state appropriately.
4199 // We also need to stop processing actions, since they aren't expecting the target to be running.
4200
4201 // FIXME: we might have run.
4202 if (stop_info_sp->HasTargetRunSinceMe())
4203 {
4204 SetRestarted (true);
4205 break;
4206 }
4207
4208 this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr);
Jim Ingham4b536182011-08-09 02:12:22 +00004209 }
Jim Inghamc7078c22012-12-13 22:24:15 +00004210
Jim Inghamc7078c22012-12-13 22:24:15 +00004211 if (still_should_stop == false)
4212 still_should_stop = this_thread_wants_to_stop;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004213 }
4214 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00004215
Ashok Thirumurthicf7c55e2013-04-18 14:38:20 +00004216
Jim Inghama8ca6e22013-05-03 23:04:37 +00004217 if (!GetRestarted())
Jim Ingham9575d842011-03-11 03:53:59 +00004218 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004219 if (!still_should_stop && does_anybody_have_an_opinion)
Jim Ingham4b536182011-08-09 02:12:22 +00004220 {
4221 // We've been asked to continue, so do that here.
Jim Ingham9575d842011-03-11 03:53:59 +00004222 SetRestarted(true);
Jim Ingham3b8285d2012-04-19 01:40:33 +00004223 // Use the public resume method here, since this is just
4224 // extending a public resume.
Jim Ingham0161b492013-02-09 01:29:05 +00004225 m_process_sp->PrivateResume();
Jim Ingham4b536182011-08-09 02:12:22 +00004226 }
4227 else
4228 {
4229 // If we didn't restart, run the Stop Hooks here:
4230 // They might also restart the target, so watch for that.
4231 m_process_sp->GetTarget().RunStopHooks();
4232 if (m_process_sp->GetPrivateState() == eStateRunning)
4233 SetRestarted(true);
4234 }
Jim Ingham9575d842011-03-11 03:53:59 +00004235 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004236 }
4237}
4238
4239void
4240Process::ProcessEventData::Dump (Stream *s) const
4241{
4242 if (m_process_sp)
Daniel Malead01b2952012-11-29 21:49:15 +00004243 s->Printf(" process = %p (pid = %" PRIu64 "), ", m_process_sp.get(), m_process_sp->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004244
Greg Clayton8b82f082011-04-12 05:54:46 +00004245 s->Printf("state = %s", StateAsCString(GetState()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004246}
4247
4248const Process::ProcessEventData *
4249Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
4250{
4251 if (event_ptr)
4252 {
4253 const EventData *event_data = event_ptr->GetData();
4254 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
4255 return static_cast <const ProcessEventData *> (event_ptr->GetData());
4256 }
4257 return NULL;
4258}
4259
4260ProcessSP
4261Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
4262{
4263 ProcessSP process_sp;
4264 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4265 if (data)
4266 process_sp = data->GetProcessSP();
4267 return process_sp;
4268}
4269
4270StateType
4271Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
4272{
4273 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4274 if (data == NULL)
4275 return eStateInvalid;
4276 else
4277 return data->GetState();
4278}
4279
4280bool
4281Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
4282{
4283 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4284 if (data == NULL)
4285 return false;
4286 else
4287 return data->GetRestarted();
4288}
4289
4290void
4291Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
4292{
4293 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4294 if (data != NULL)
4295 data->SetRestarted(new_value);
4296}
4297
Jim Ingham0161b492013-02-09 01:29:05 +00004298size_t
4299Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr)
4300{
4301 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4302 if (data != NULL)
4303 return data->GetNumRestartedReasons();
4304 else
4305 return 0;
4306}
4307
4308const char *
4309Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx)
4310{
4311 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4312 if (data != NULL)
4313 return data->GetRestartedReasonAtIndex(idx);
4314 else
4315 return NULL;
4316}
4317
4318void
4319Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason)
4320{
4321 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4322 if (data != NULL)
4323 data->AddRestartedReason(reason);
4324}
4325
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004326bool
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004327Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
4328{
4329 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4330 if (data == NULL)
4331 return false;
4332 else
4333 return data->GetInterrupted ();
4334}
4335
4336void
4337Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
4338{
4339 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4340 if (data != NULL)
4341 data->SetInterrupted(new_value);
4342}
4343
4344bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004345Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
4346{
4347 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4348 if (data)
4349 {
4350 data->SetUpdateStateOnRemoval();
4351 return true;
4352 }
4353 return false;
4354}
4355
Greg Claytond9e416c2012-02-18 05:35:26 +00004356lldb::TargetSP
4357Process::CalculateTarget ()
4358{
4359 return m_target.shared_from_this();
4360}
4361
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004362void
Greg Clayton0603aa92010-10-04 01:05:56 +00004363Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004364{
Greg Claytonc14ee322011-09-22 04:58:26 +00004365 exe_ctx.SetTargetPtr (&m_target);
4366 exe_ctx.SetProcessPtr (this);
4367 exe_ctx.SetThreadPtr(NULL);
4368 exe_ctx.SetFramePtr (NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004369}
4370
Greg Claytone996fd32011-03-08 22:40:15 +00004371//uint32_t
4372//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
4373//{
4374// return 0;
4375//}
4376//
4377//ArchSpec
4378//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
4379//{
4380// return Host::GetArchSpecForExistingProcess (pid);
4381//}
4382//
4383//ArchSpec
4384//Process::GetArchSpecForExistingProcess (const char *process_name)
4385//{
4386// return Host::GetArchSpecForExistingProcess (process_name);
4387//}
4388//
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004389void
4390Process::AppendSTDOUT (const char * s, size_t len)
4391{
Greg Clayton3af9ea52010-11-18 05:57:03 +00004392 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004393 m_stdout_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004394 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState()));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004395}
4396
4397void
Greg Clayton93e86192011-11-13 04:45:22 +00004398Process::AppendSTDERR (const char * s, size_t len)
4399{
4400 Mutex::Locker locker (m_stdio_communication_mutex);
4401 m_stderr_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004402 BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState()));
Greg Clayton93e86192011-11-13 04:45:22 +00004403}
4404
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004405void
4406Process::BroadcastAsyncProfileData(const char *s, size_t len)
4407{
4408 Mutex::Locker locker (m_profile_data_comm_mutex);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004409 m_profile_data.push_back(s);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004410 BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState()));
4411}
4412
4413size_t
4414Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error)
4415{
4416 Mutex::Locker locker(m_profile_data_comm_mutex);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004417 if (m_profile_data.empty())
4418 return 0;
4419
4420 size_t bytes_available = m_profile_data.front().size();
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004421 if (bytes_available > 0)
4422 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004423 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004424 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00004425 log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")", buf, (uint64_t)buf_size);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004426 if (bytes_available > buf_size)
4427 {
Han Ming Ong929a94f2012-11-29 22:14:45 +00004428 memcpy(buf, m_profile_data.front().data(), buf_size);
4429 m_profile_data.front().erase(0, buf_size);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004430 bytes_available = buf_size;
4431 }
4432 else
4433 {
Han Ming Ong929a94f2012-11-29 22:14:45 +00004434 memcpy(buf, m_profile_data.front().data(), bytes_available);
4435 m_profile_data.erase(m_profile_data.begin());
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004436 }
4437 }
4438 return bytes_available;
4439}
4440
4441
Greg Clayton93e86192011-11-13 04:45:22 +00004442//------------------------------------------------------------------
4443// Process STDIO
4444//------------------------------------------------------------------
4445
4446size_t
4447Process::GetSTDOUT (char *buf, size_t buf_size, Error &error)
4448{
4449 Mutex::Locker locker(m_stdio_communication_mutex);
4450 size_t bytes_available = m_stdout_data.size();
4451 if (bytes_available > 0)
4452 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004453 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004454 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00004455 log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")", buf, (uint64_t)buf_size);
Greg Clayton93e86192011-11-13 04:45:22 +00004456 if (bytes_available > buf_size)
4457 {
4458 memcpy(buf, m_stdout_data.c_str(), buf_size);
4459 m_stdout_data.erase(0, buf_size);
4460 bytes_available = buf_size;
4461 }
4462 else
4463 {
4464 memcpy(buf, m_stdout_data.c_str(), bytes_available);
4465 m_stdout_data.clear();
4466 }
4467 }
4468 return bytes_available;
4469}
4470
4471
4472size_t
4473Process::GetSTDERR (char *buf, size_t buf_size, Error &error)
4474{
4475 Mutex::Locker locker(m_stdio_communication_mutex);
4476 size_t bytes_available = m_stderr_data.size();
4477 if (bytes_available > 0)
4478 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004479 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004480 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00004481 log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")", buf, (uint64_t)buf_size);
Greg Clayton93e86192011-11-13 04:45:22 +00004482 if (bytes_available > buf_size)
4483 {
4484 memcpy(buf, m_stderr_data.c_str(), buf_size);
4485 m_stderr_data.erase(0, buf_size);
4486 bytes_available = buf_size;
4487 }
4488 else
4489 {
4490 memcpy(buf, m_stderr_data.c_str(), bytes_available);
4491 m_stderr_data.clear();
4492 }
4493 }
4494 return bytes_available;
4495}
4496
4497void
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004498Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
4499{
4500 Process *process = (Process *) baton;
4501 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
4502}
4503
4504size_t
4505Process::ProcessInputReaderCallback (void *baton,
4506 InputReader &reader,
4507 lldb::InputReaderAction notification,
4508 const char *bytes,
4509 size_t bytes_len)
4510{
4511 Process *process = (Process *) baton;
4512
4513 switch (notification)
4514 {
4515 case eInputReaderActivate:
4516 break;
4517
4518 case eInputReaderDeactivate:
4519 break;
4520
4521 case eInputReaderReactivate:
4522 break;
4523
Caroline Tice969ed3d2011-05-02 20:41:46 +00004524 case eInputReaderAsynchronousOutputWritten:
4525 break;
4526
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004527 case eInputReaderGotToken:
4528 {
4529 Error error;
4530 process->PutSTDIN (bytes, bytes_len, error);
4531 }
4532 break;
4533
Caroline Ticeefed6132010-11-19 20:47:54 +00004534 case eInputReaderInterrupt:
4535 process->Halt ();
4536 break;
4537
4538 case eInputReaderEndOfFile:
4539 process->AppendSTDOUT ("^D", 2);
4540 break;
4541
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004542 case eInputReaderDone:
4543 break;
4544
4545 }
4546
4547 return bytes_len;
4548}
4549
4550void
4551Process::ResetProcessInputReader ()
4552{
4553 m_process_input_reader.reset();
4554}
4555
4556void
Greg Claytonee95ed52011-11-17 22:14:31 +00004557Process::SetSTDIOFileDescriptor (int file_descriptor)
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004558{
4559 // First set up the Read Thread for reading/handling process I/O
4560
Greg Clayton7b0992d2013-04-18 22:45:39 +00004561 std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004562
4563 if (conn_ap.get())
4564 {
4565 m_stdio_communication.SetConnection (conn_ap.release());
4566 if (m_stdio_communication.IsConnected())
4567 {
4568 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
4569 m_stdio_communication.StartReadThread();
4570
4571 // Now read thread is set up, set up input reader.
4572
4573 if (!m_process_input_reader.get())
4574 {
4575 m_process_input_reader.reset (new InputReader(m_target.GetDebugger()));
4576 Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback,
4577 this,
4578 eInputReaderGranularityByte,
4579 NULL,
4580 NULL,
4581 false));
4582
4583 if (err.Fail())
4584 m_process_input_reader.reset();
4585 }
4586 }
4587 }
4588}
4589
4590void
4591Process::PushProcessInputReader ()
4592{
4593 if (m_process_input_reader && !m_process_input_reader->IsActive())
4594 m_target.GetDebugger().PushInputReader (m_process_input_reader);
4595}
4596
4597void
4598Process::PopProcessInputReader ()
4599{
4600 if (m_process_input_reader && m_process_input_reader->IsActive())
4601 m_target.GetDebugger().PopInputReader (m_process_input_reader);
4602}
4603
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004604// The process needs to know about installed plug-ins
Greg Clayton99d0faf2010-11-18 23:32:35 +00004605void
Caroline Tice20bd37f2011-03-10 22:14:10 +00004606Process::SettingsInitialize ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004607{
Greg Clayton67cc0632012-08-22 17:17:09 +00004608// static std::vector<OptionEnumValueElement> g_plugins;
4609//
4610// int i=0;
4611// const char *name;
4612// OptionEnumValueElement option_enum;
4613// while ((name = PluginManager::GetProcessPluginNameAtIndex (i)) != NULL)
4614// {
4615// if (name)
4616// {
4617// option_enum.value = i;
4618// option_enum.string_value = name;
4619// option_enum.usage = PluginManager::GetProcessPluginDescriptionAtIndex (i);
4620// g_plugins.push_back (option_enum);
4621// }
4622// ++i;
4623// }
4624// option_enum.value = 0;
4625// option_enum.string_value = NULL;
4626// option_enum.usage = NULL;
4627// g_plugins.push_back (option_enum);
4628//
4629// for (i=0; (name = SettingsController::instance_settings_table[i].var_name); ++i)
4630// {
4631// if (::strcmp (name, "plugin") == 0)
4632// {
4633// SettingsController::instance_settings_table[i].enum_values = &g_plugins[0];
4634// break;
4635// }
4636// }
Greg Clayton67cc0632012-08-22 17:17:09 +00004637//
Greg Clayton6920b522012-08-22 18:39:03 +00004638 Thread::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00004639}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004640
Greg Clayton99d0faf2010-11-18 23:32:35 +00004641void
Caroline Tice20bd37f2011-03-10 22:14:10 +00004642Process::SettingsTerminate ()
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004643{
Greg Clayton6920b522012-08-22 18:39:03 +00004644 Thread::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00004645}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004646
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00004647ExecutionResults
Jim Inghamf48169b2010-11-30 02:22:11 +00004648Process::RunThreadPlan (ExecutionContext &exe_ctx,
Jim Ingham372787f2012-04-07 00:00:41 +00004649 lldb::ThreadPlanSP &thread_plan_sp,
Jim Inghamf48169b2010-11-30 02:22:11 +00004650 bool stop_others,
Jim Ingham35e1bda2012-10-16 21:41:58 +00004651 bool run_others,
Jim Ingham184e9812013-01-15 02:47:48 +00004652 bool unwind_on_error,
4653 bool ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +00004654 uint32_t timeout_usec,
Jim Inghamf48169b2010-11-30 02:22:11 +00004655 Stream &errors)
4656{
4657 ExecutionResults return_value = eExecutionSetupError;
4658
Jim Ingham77787032011-01-20 02:03:18 +00004659 if (thread_plan_sp.get() == NULL)
4660 {
4661 errors.Printf("RunThreadPlan called with empty thread plan.");
Greg Claytone0d378b2011-03-24 21:19:54 +00004662 return eExecutionSetupError;
Jim Ingham77787032011-01-20 02:03:18 +00004663 }
Jim Ingham7d7931d2013-03-28 00:05:34 +00004664
4665 if (!thread_plan_sp->ValidatePlan(NULL))
4666 {
4667 errors.Printf ("RunThreadPlan called with an invalid thread plan.");
4668 return eExecutionSetupError;
4669 }
4670
Greg Claytonc14ee322011-09-22 04:58:26 +00004671 if (exe_ctx.GetProcessPtr() != this)
4672 {
4673 errors.Printf("RunThreadPlan called on wrong process.");
4674 return eExecutionSetupError;
4675 }
4676
4677 Thread *thread = exe_ctx.GetThreadPtr();
4678 if (thread == NULL)
4679 {
4680 errors.Printf("RunThreadPlan called with invalid thread.");
4681 return eExecutionSetupError;
4682 }
Jim Ingham77787032011-01-20 02:03:18 +00004683
Jim Ingham17e5c4e2011-05-17 22:24:54 +00004684 // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes.
4685 // For that to be true the plan can't be private - since private plans suppress themselves in the
4686 // GetCompletedPlan call.
4687
4688 bool orig_plan_private = thread_plan_sp->GetPrivate();
4689 thread_plan_sp->SetPrivate(false);
4690
Jim Ingham444586b2011-01-24 06:34:17 +00004691 if (m_private_state.GetValue() != eStateStopped)
4692 {
4693 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Greg Claytone0d378b2011-03-24 21:19:54 +00004694 return eExecutionSetupError;
Jim Ingham444586b2011-01-24 06:34:17 +00004695 }
4696
Jim Ingham66243842011-08-13 00:56:10 +00004697 // Save the thread & frame from the exe_ctx for restoration after we run
Greg Claytonc14ee322011-09-22 04:58:26 +00004698 const uint32_t thread_idx_id = thread->GetIndexID();
Jim Ingham11b0e052013-02-19 23:22:45 +00004699 StackFrameSP selected_frame_sp = thread->GetSelectedFrame();
4700 if (!selected_frame_sp)
4701 {
4702 thread->SetSelectedFrame(0);
4703 selected_frame_sp = thread->GetSelectedFrame();
4704 if (!selected_frame_sp)
4705 {
4706 errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id);
4707 return eExecutionSetupError;
4708 }
4709 }
4710
4711 StackID ctx_frame_id = selected_frame_sp->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00004712
4713 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
4714 // so we should arrange to reset them as well.
4715
Greg Claytonc14ee322011-09-22 04:58:26 +00004716 lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread();
Jim Inghamf48169b2010-11-30 02:22:11 +00004717
Jim Ingham66243842011-08-13 00:56:10 +00004718 uint32_t selected_tid;
4719 StackID selected_stack_id;
Greg Clayton762f7132011-09-18 18:59:15 +00004720 if (selected_thread_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00004721 {
4722 selected_tid = selected_thread_sp->GetIndexID();
Jim Ingham66243842011-08-13 00:56:10 +00004723 selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00004724 }
4725 else
4726 {
4727 selected_tid = LLDB_INVALID_THREAD_ID;
4728 }
4729
Jim Ingham372787f2012-04-07 00:00:41 +00004730 lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD;
Jim Ingham076b3042012-04-10 01:21:57 +00004731 lldb::StateType old_state;
4732 lldb::ThreadPlanSP stopper_base_plan_sp;
Jim Ingham372787f2012-04-07 00:00:41 +00004733
Greg Clayton5160ce52013-03-27 23:08:40 +00004734 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham372787f2012-04-07 00:00:41 +00004735 if (Host::GetCurrentThread() == m_private_state_thread)
4736 {
Jim Ingham076b3042012-04-10 01:21:57 +00004737 // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since
4738 // we are the thread that is generating public events.
Jim Ingham372787f2012-04-07 00:00:41 +00004739 // The simplest thing to do is to spin up a temporary thread to handle private state thread events while
Jim Ingham076b3042012-04-10 01:21:57 +00004740 // we are fielding public events here.
4741 if (log)
Jason Molendad251c9d2012-11-17 01:41:04 +00004742 log->Printf ("Running thread plan on private state thread, spinning up another state thread to handle the events.");
Jim Ingham076b3042012-04-10 01:21:57 +00004743
4744
Jim Ingham372787f2012-04-07 00:00:41 +00004745 backup_private_state_thread = m_private_state_thread;
Jim Ingham076b3042012-04-10 01:21:57 +00004746
4747 // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop,
4748 // returning control here.
4749 // But in the normal course of things, the plan above us on the stack would be given a shot at the stop
4750 // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack
4751 // before the plan we want to run. Since base plans always stop and return control to the user, that will
4752 // do just what we want.
4753 stopper_base_plan_sp.reset(new ThreadPlanBase (*thread));
4754 thread->QueueThreadPlan (stopper_base_plan_sp, false);
4755 // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly.
4756 old_state = m_public_state.GetValue();
4757 m_public_state.SetValueNoLock(eStateStopped);
4758
4759 // Now spin up the private state thread:
Jim Ingham372787f2012-04-07 00:00:41 +00004760 StartPrivateStateThread(true);
4761 }
4762
4763 thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense?
Jim Inghamf48169b2010-11-30 02:22:11 +00004764
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00004765 Listener listener("lldb.process.listener.run-thread-plan");
Jim Ingham0f16e732011-02-08 05:20:59 +00004766
Sean Callanana46ec452012-07-11 21:31:24 +00004767 lldb::EventSP event_to_broadcast_sp;
Jim Ingham0f16e732011-02-08 05:20:59 +00004768
Jim Ingham77787032011-01-20 02:03:18 +00004769 {
Sean Callanana46ec452012-07-11 21:31:24 +00004770 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
4771 // restored on exit to the function.
4772 //
4773 // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event
4774 // is put into event_to_broadcast_sp for rebroadcasting.
Jim Inghamf48169b2010-11-30 02:22:11 +00004775
Sean Callanana46ec452012-07-11 21:31:24 +00004776 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Jim Ingham0f16e732011-02-08 05:20:59 +00004777
Jim Inghamf48169b2010-11-30 02:22:11 +00004778 if (log)
Jim Ingham0f16e732011-02-08 05:20:59 +00004779 {
4780 StreamString s;
Sean Callanana46ec452012-07-11 21:31:24 +00004781 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Daniel Malead01b2952012-11-29 21:49:15 +00004782 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 " to run thread plan \"%s\".",
Sean Callanana46ec452012-07-11 21:31:24 +00004783 thread->GetIndexID(),
4784 thread->GetID(),
4785 s.GetData());
4786 }
4787
4788 bool got_event;
4789 lldb::EventSP event_sp;
4790 lldb::StateType stop_state = lldb::eStateInvalid;
4791
4792 TimeValue* timeout_ptr = NULL;
4793 TimeValue real_timeout;
4794
Jim Ingham0161b492013-02-09 01:29:05 +00004795 bool before_first_timeout = true; // This is set to false the first time that we have to halt the target.
Sean Callanana46ec452012-07-11 21:31:24 +00004796 bool do_resume = true;
Jim Ingham184e9812013-01-15 02:47:48 +00004797 bool handle_running_event = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +00004798 const uint64_t default_one_thread_timeout_usec = 250000;
Sean Callanana46ec452012-07-11 21:31:24 +00004799
Jim Ingham0161b492013-02-09 01:29:05 +00004800 // This is just for accounting:
4801 uint32_t num_resumes = 0;
4802
4803 TimeValue one_thread_timeout = TimeValue::Now();
4804 TimeValue final_timeout = one_thread_timeout;
4805
4806 if (run_others)
4807 {
4808 // If we are running all threads then we take half the time to run all threads, bounded by
4809 // .25 sec.
4810 if (timeout_usec == 0)
4811 one_thread_timeout.OffsetWithMicroSeconds(default_one_thread_timeout_usec);
4812 else
4813 {
Greg Clayton03da4cc2013-04-19 21:31:16 +00004814 uint64_t computed_timeout = timeout_usec / 2;
Jim Ingham0161b492013-02-09 01:29:05 +00004815 if (computed_timeout > default_one_thread_timeout_usec)
4816 computed_timeout = default_one_thread_timeout_usec;
4817 one_thread_timeout.OffsetWithMicroSeconds(computed_timeout);
4818 }
4819 final_timeout.OffsetWithMicroSeconds (timeout_usec);
4820 }
4821 else
4822 {
4823 if (timeout_usec != 0)
4824 final_timeout.OffsetWithMicroSeconds(timeout_usec);
4825 }
4826
Jim Ingham8559a352012-11-26 23:52:18 +00004827 // This while loop must exit out the bottom, there's cleanup that we need to do when we are done.
4828 // So don't call return anywhere within it.
4829
Sean Callanana46ec452012-07-11 21:31:24 +00004830 while (1)
4831 {
4832 // We usually want to resume the process if we get to the top of the loop.
4833 // The only exception is if we get two running events with no intervening
4834 // stop, which can happen, we will just wait for then next stop event.
Jim Ingham0161b492013-02-09 01:29:05 +00004835 if (log)
4836 log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.",
4837 do_resume,
4838 handle_running_event,
4839 before_first_timeout);
Sean Callanana46ec452012-07-11 21:31:24 +00004840
Jim Ingham184e9812013-01-15 02:47:48 +00004841 if (do_resume || handle_running_event)
Sean Callanana46ec452012-07-11 21:31:24 +00004842 {
4843 // Do the initial resume and wait for the running event before going further.
4844
Jim Ingham184e9812013-01-15 02:47:48 +00004845 if (do_resume)
Sean Callanana46ec452012-07-11 21:31:24 +00004846 {
Jim Ingham0161b492013-02-09 01:29:05 +00004847 num_resumes++;
Jim Ingham184e9812013-01-15 02:47:48 +00004848 Error resume_error = PrivateResume ();
4849 if (!resume_error.Success())
4850 {
Jim Ingham0161b492013-02-09 01:29:05 +00004851 errors.Printf("Error resuming inferior the %d time: \"%s\".\n",
4852 num_resumes,
4853 resume_error.AsCString());
Jim Ingham184e9812013-01-15 02:47:48 +00004854 return_value = eExecutionSetupError;
4855 break;
4856 }
Sean Callanana46ec452012-07-11 21:31:24 +00004857 }
Sean Callanana46ec452012-07-11 21:31:24 +00004858
Jim Ingham0161b492013-02-09 01:29:05 +00004859 TimeValue resume_timeout = TimeValue::Now();
4860 resume_timeout.OffsetWithMicroSeconds(500000);
4861
4862 got_event = listener.WaitForEvent(&resume_timeout, event_sp);
Sean Callanana46ec452012-07-11 21:31:24 +00004863 if (!got_event)
4864 {
4865 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00004866 log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.",
4867 num_resumes);
Sean Callanana46ec452012-07-11 21:31:24 +00004868
Jim Ingham0161b492013-02-09 01:29:05 +00004869 errors.Printf("Didn't get any event after resume %d, exiting.", num_resumes);
Sean Callanana46ec452012-07-11 21:31:24 +00004870 return_value = eExecutionSetupError;
4871 break;
4872 }
4873
4874 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Ingham0161b492013-02-09 01:29:05 +00004875
Sean Callanana46ec452012-07-11 21:31:24 +00004876 if (stop_state != eStateRunning)
4877 {
Jim Ingham0161b492013-02-09 01:29:05 +00004878 bool restarted = false;
4879
4880 if (stop_state == eStateStopped)
4881 {
4882 restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get());
4883 if (log)
4884 log->Printf("Process::RunThreadPlan(): didn't get running event after "
4885 "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).",
4886 num_resumes,
4887 StateAsCString(stop_state),
4888 restarted,
4889 do_resume,
4890 handle_running_event);
4891 }
4892
4893 if (restarted)
4894 {
4895 // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted
4896 // event here. But if I do, the best thing is to Halt and then get out of here.
4897 Halt();
4898 }
4899
Jim Ingham35e1bda2012-10-16 21:41:58 +00004900 errors.Printf("Didn't get running event after initial resume, got %s instead.",
4901 StateAsCString(stop_state));
Sean Callanana46ec452012-07-11 21:31:24 +00004902 return_value = eExecutionSetupError;
4903 break;
4904 }
4905
4906 if (log)
4907 log->PutCString ("Process::RunThreadPlan(): resuming succeeded.");
4908 // We need to call the function synchronously, so spin waiting for it to return.
4909 // If we get interrupted while executing, we're going to lose our context, and
4910 // won't be able to gather the result at this point.
4911 // We set the timeout AFTER the resume, since the resume takes some time and we
4912 // don't want to charge that to the timeout.
Sean Callanana46ec452012-07-11 21:31:24 +00004913 }
Jim Ingham0f16e732011-02-08 05:20:59 +00004914 else
4915 {
Sean Callanana46ec452012-07-11 21:31:24 +00004916 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00004917 log->PutCString ("Process::RunThreadPlan(): waiting for next event.");
Jim Ingham0f16e732011-02-08 05:20:59 +00004918 }
Jim Ingham0161b492013-02-09 01:29:05 +00004919
4920 if (before_first_timeout)
4921 {
4922 if (run_others)
4923 timeout_ptr = &one_thread_timeout;
4924 else
4925 {
4926 if (timeout_usec == 0)
4927 timeout_ptr = NULL;
4928 else
4929 timeout_ptr = &final_timeout;
4930 }
4931 }
4932 else
4933 {
4934 if (timeout_usec == 0)
4935 timeout_ptr = NULL;
4936 else
4937 timeout_ptr = &final_timeout;
4938 }
4939
4940 do_resume = true;
4941 handle_running_event = true;
Jim Ingham0f16e732011-02-08 05:20:59 +00004942
Sean Callanana46ec452012-07-11 21:31:24 +00004943 // Now wait for the process to stop again:
Sean Callanana46ec452012-07-11 21:31:24 +00004944 event_sp.reset();
Jim Ingham0f16e732011-02-08 05:20:59 +00004945
Jim Ingham0f16e732011-02-08 05:20:59 +00004946 if (log)
Jim Ingham20829ac2011-08-09 22:24:33 +00004947 {
Sean Callanana46ec452012-07-11 21:31:24 +00004948 if (timeout_ptr)
4949 {
Matt Kopec676a4872013-02-21 23:55:31 +00004950 log->Printf ("Process::RunThreadPlan(): about to wait - now is %" PRIu64 " - endpoint is %" PRIu64,
Jim Ingham0161b492013-02-09 01:29:05 +00004951 TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(),
4952 timeout_ptr->GetAsMicroSecondsSinceJan1_1970());
Sean Callanana46ec452012-07-11 21:31:24 +00004953 }
Jim Ingham20829ac2011-08-09 22:24:33 +00004954 else
Sean Callanana46ec452012-07-11 21:31:24 +00004955 {
4956 log->Printf ("Process::RunThreadPlan(): about to wait forever.");
4957 }
4958 }
4959
4960 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
4961
4962 if (got_event)
4963 {
4964 if (event_sp.get())
4965 {
4966 bool keep_going = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00004967 if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00004968 {
Jim Inghamcfc09352012-07-27 23:57:19 +00004969 Halt();
Jim Inghamcfc09352012-07-27 23:57:19 +00004970 return_value = eExecutionInterrupted;
4971 errors.Printf ("Execution halted by user interrupt.");
4972 if (log)
4973 log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting.");
Jim Ingham0161b492013-02-09 01:29:05 +00004974 break;
Jim Inghamcfc09352012-07-27 23:57:19 +00004975 }
4976 else
4977 {
4978 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
4979 if (log)
4980 log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state));
4981
4982 switch (stop_state)
Sean Callanana46ec452012-07-11 21:31:24 +00004983 {
Jim Inghamcfc09352012-07-27 23:57:19 +00004984 case lldb::eStateStopped:
Sean Callanana46ec452012-07-11 21:31:24 +00004985 {
Jim Ingham0161b492013-02-09 01:29:05 +00004986 // We stopped, figure out what we are going to do now.
Jim Inghamcfc09352012-07-27 23:57:19 +00004987 ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id);
4988 if (!thread_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00004989 {
Jim Inghamcfc09352012-07-27 23:57:19 +00004990 // Ooh, our thread has vanished. Unlikely that this was successful execution...
Sean Callanana46ec452012-07-11 21:31:24 +00004991 if (log)
Jim Inghamcfc09352012-07-27 23:57:19 +00004992 log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id);
4993 return_value = eExecutionInterrupted;
Sean Callanana46ec452012-07-11 21:31:24 +00004994 }
4995 else
4996 {
Jim Ingham0161b492013-02-09 01:29:05 +00004997 // If we were restarted, we just need to go back up to fetch another event.
4998 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
Jim Inghamcfc09352012-07-27 23:57:19 +00004999 {
5000 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005001 {
5002 log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting.");
5003 }
5004 keep_going = true;
5005 do_resume = false;
5006 handle_running_event = true;
5007
Jim Inghamcfc09352012-07-27 23:57:19 +00005008 }
5009 else
5010 {
Jim Ingham0161b492013-02-09 01:29:05 +00005011
5012 StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
5013 StopReason stop_reason = eStopReasonInvalid;
5014 if (stop_info_sp)
5015 stop_reason = stop_info_sp->GetStopReason();
5016
5017
5018 // FIXME: We only check if the stop reason is plan complete, should we make sure that
5019 // it is OUR plan that is complete?
5020 if (stop_reason == eStopReasonPlanComplete)
Jim Ingham184e9812013-01-15 02:47:48 +00005021 {
5022 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005023 log->PutCString ("Process::RunThreadPlan(): execution completed successfully.");
5024 // Now mark this plan as private so it doesn't get reported as the stop reason
5025 // after this point.
5026 if (thread_plan_sp)
5027 thread_plan_sp->SetPrivate (orig_plan_private);
5028 return_value = eExecutionCompleted;
Jim Ingham184e9812013-01-15 02:47:48 +00005029 }
5030 else
5031 {
Jim Ingham0161b492013-02-09 01:29:05 +00005032 // Something restarted the target, so just wait for it to stop for real.
Jim Ingham184e9812013-01-15 02:47:48 +00005033 if (stop_reason == eStopReasonBreakpoint)
Jim Ingham0161b492013-02-09 01:29:05 +00005034 {
5035 if (log)
5036 log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription());
Jim Ingham184e9812013-01-15 02:47:48 +00005037 return_value = eExecutionHitBreakpoint;
Jim Ingham0161b492013-02-09 01:29:05 +00005038 }
Jim Ingham184e9812013-01-15 02:47:48 +00005039 else
Jim Ingham0161b492013-02-09 01:29:05 +00005040 {
5041 if (log)
5042 log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete.");
Jim Ingham184e9812013-01-15 02:47:48 +00005043 return_value = eExecutionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005044 }
Jim Ingham184e9812013-01-15 02:47:48 +00005045 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005046 }
Sean Callanana46ec452012-07-11 21:31:24 +00005047 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005048 }
5049 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005050
Jim Inghamcfc09352012-07-27 23:57:19 +00005051 case lldb::eStateRunning:
Jim Ingham0161b492013-02-09 01:29:05 +00005052 // This shouldn't really happen, but sometimes we do get two running events without an
5053 // intervening stop, and in that case we should just go back to waiting for the stop.
Jim Inghamcfc09352012-07-27 23:57:19 +00005054 do_resume = false;
5055 keep_going = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005056 handle_running_event = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005057 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005058
Jim Inghamcfc09352012-07-27 23:57:19 +00005059 default:
5060 if (log)
5061 log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state));
5062
5063 if (stop_state == eStateExited)
5064 event_to_broadcast_sp = event_sp;
5065
Sean Callananbf154da2012-08-08 17:35:10 +00005066 errors.Printf ("Execution stopped with unexpected state.\n");
Jim Inghamcfc09352012-07-27 23:57:19 +00005067 return_value = eExecutionInterrupted;
5068 break;
5069 }
Sean Callanana46ec452012-07-11 21:31:24 +00005070 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005071
Sean Callanana46ec452012-07-11 21:31:24 +00005072 if (keep_going)
5073 continue;
5074 else
5075 break;
5076 }
5077 else
5078 {
5079 if (log)
5080 log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd...");
5081 return_value = eExecutionInterrupted;
5082 break;
5083 }
5084 }
5085 else
5086 {
5087 // If we didn't get an event that means we've timed out...
5088 // We will interrupt the process here. Depending on what we were asked to do we will
5089 // either exit, or try with all threads running for the same timeout.
Sean Callanana46ec452012-07-11 21:31:24 +00005090
5091 if (log) {
Jim Ingham35e1bda2012-10-16 21:41:58 +00005092 if (run_others)
Sean Callanana46ec452012-07-11 21:31:24 +00005093 {
Jim Ingham0161b492013-02-09 01:29:05 +00005094 uint64_t remaining_time = final_timeout - TimeValue::Now();
5095 if (before_first_timeout)
5096 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
5097 "running till for %" PRId64 " usec with all threads enabled.",
5098 remaining_time);
Sean Callanana46ec452012-07-11 21:31:24 +00005099 else
5100 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
Jim Ingham35e1bda2012-10-16 21:41:58 +00005101 "and timeout: %d timed out, abandoning execution.",
5102 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005103 }
5104 else
5105 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, "
Jim Ingham35e1bda2012-10-16 21:41:58 +00005106 "abandoning execution.",
5107 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005108 }
5109
Jim Ingham0161b492013-02-09 01:29:05 +00005110 // It is possible that between the time we issued the Halt, and we get around to calling Halt the target
5111 // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event.
5112 // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In
5113 // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's
5114 // stopped event. That's what this while loop does.
5115
5116 bool back_to_top = true;
5117 uint32_t try_halt_again = 0;
5118 bool do_halt = true;
5119 const uint32_t num_retries = 5;
5120 while (try_halt_again < num_retries)
Sean Callanana46ec452012-07-11 21:31:24 +00005121 {
Jim Ingham0161b492013-02-09 01:29:05 +00005122 Error halt_error;
5123 if (do_halt)
5124 {
5125 if (log)
5126 log->Printf ("Process::RunThreadPlan(): Running Halt.");
5127 halt_error = Halt();
5128 }
5129 if (halt_error.Success())
5130 {
5131 if (log)
5132 log->PutCString ("Process::RunThreadPlan(): Halt succeeded.");
5133
5134 real_timeout = TimeValue::Now();
5135 real_timeout.OffsetWithMicroSeconds(500000);
5136
5137 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Sean Callanana46ec452012-07-11 21:31:24 +00005138
Jim Ingham0161b492013-02-09 01:29:05 +00005139 if (got_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005140 {
Jim Ingham0161b492013-02-09 01:29:05 +00005141 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5142 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005143 {
Jim Ingham0161b492013-02-09 01:29:05 +00005144 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
5145 if (stop_state == lldb::eStateStopped
5146 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
5147 log->PutCString (" Event was the Halt interruption event.");
Sean Callanana46ec452012-07-11 21:31:24 +00005148 }
5149
Jim Ingham0161b492013-02-09 01:29:05 +00005150 if (stop_state == lldb::eStateStopped)
Sean Callanana46ec452012-07-11 21:31:24 +00005151 {
Jim Ingham0161b492013-02-09 01:29:05 +00005152 // Between the time we initiated the Halt and the time we delivered it, the process could have
5153 // already finished its job. Check that here:
5154
5155 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
5156 {
5157 if (log)
5158 log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
5159 "Exiting wait loop.");
5160 return_value = eExecutionCompleted;
5161 back_to_top = false;
5162 break;
5163 }
5164
5165 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
5166 {
5167 if (log)
5168 log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... "
5169 "Exiting wait loop.");
5170 try_halt_again++;
5171 do_halt = false;
5172 continue;
5173 }
Sean Callanana46ec452012-07-11 21:31:24 +00005174
Jim Ingham0161b492013-02-09 01:29:05 +00005175 if (!run_others)
5176 {
5177 if (log)
5178 log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting.");
5179 return_value = eExecutionInterrupted;
5180 back_to_top = false;
5181 break;
5182 }
5183
5184 if (before_first_timeout)
5185 {
5186 // Set all the other threads to run, and return to the top of the loop, which will continue;
5187 before_first_timeout = false;
5188 thread_plan_sp->SetStopOthers (false);
5189 if (log)
5190 log->PutCString ("Process::RunThreadPlan(): about to resume.");
Sean Callanana46ec452012-07-11 21:31:24 +00005191
Jim Ingham0161b492013-02-09 01:29:05 +00005192 back_to_top = true;
5193 break;
5194 }
5195 else
5196 {
5197 // Running all threads failed, so return Interrupted.
5198 if (log)
5199 log->PutCString("Process::RunThreadPlan(): running all threads timed out.");
5200 return_value = eExecutionInterrupted;
5201 back_to_top = false;
5202 break;
5203 }
Sean Callanana46ec452012-07-11 21:31:24 +00005204 }
5205 }
5206 else
Jim Ingham0161b492013-02-09 01:29:05 +00005207 { if (log)
5208 log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
5209 "I'm getting out of here passing Interrupted.");
Sean Callanana46ec452012-07-11 21:31:24 +00005210 return_value = eExecutionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005211 back_to_top = false;
5212 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005213 }
5214 }
Jim Ingham0161b492013-02-09 01:29:05 +00005215 else
5216 {
5217 try_halt_again++;
5218 continue;
5219 }
Sean Callanana46ec452012-07-11 21:31:24 +00005220 }
Jim Ingham0161b492013-02-09 01:29:05 +00005221
5222 if (!back_to_top || try_halt_again > num_retries)
5223 break;
5224 else
5225 continue;
Sean Callanana46ec452012-07-11 21:31:24 +00005226 }
Sean Callanana46ec452012-07-11 21:31:24 +00005227 } // END WAIT LOOP
5228
5229 // If we had to start up a temporary private state thread to run this thread plan, shut it down now.
5230 if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread))
5231 {
5232 StopPrivateStateThread();
5233 Error error;
5234 m_private_state_thread = backup_private_state_thread;
Sean Callanan9a028512012-08-09 00:50:26 +00005235 if (stopper_base_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005236 {
5237 thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp);
5238 }
5239 m_public_state.SetValueNoLock(old_state);
5240
5241 }
5242
Jim Ingham184e9812013-01-15 02:47:48 +00005243 // Restore the thread state if we are going to discard the plan execution. There are three cases where this
5244 // could happen:
5245 // 1) The execution successfully completed
5246 // 2) We hit a breakpoint, and ignore_breakpoints was true
5247 // 3) We got some other error, and discard_on_error was true
5248 bool should_unwind = (return_value == eExecutionInterrupted && unwind_on_error)
5249 || (return_value == eExecutionHitBreakpoint && ignore_breakpoints);
Jim Ingham8559a352012-11-26 23:52:18 +00005250
Jim Ingham184e9812013-01-15 02:47:48 +00005251 if (return_value == eExecutionCompleted
5252 || should_unwind)
Jim Ingham8559a352012-11-26 23:52:18 +00005253 {
5254 thread_plan_sp->RestoreThreadState();
5255 }
Sean Callanana46ec452012-07-11 21:31:24 +00005256
5257 // Now do some processing on the results of the run:
Jim Ingham184e9812013-01-15 02:47:48 +00005258 if (return_value == eExecutionInterrupted || return_value == eExecutionHitBreakpoint)
Sean Callanana46ec452012-07-11 21:31:24 +00005259 {
5260 if (log)
5261 {
5262 StreamString s;
5263 if (event_sp)
5264 event_sp->Dump (&s);
5265 else
5266 {
5267 log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
5268 }
5269
5270 StreamString ts;
5271
5272 const char *event_explanation = NULL;
5273
5274 do
5275 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005276 if (!event_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005277 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005278 event_explanation = "<no event>";
Sean Callanana46ec452012-07-11 21:31:24 +00005279 break;
5280 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005281 else if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005282 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005283 event_explanation = "<user interrupt>";
Sean Callanana46ec452012-07-11 21:31:24 +00005284 break;
5285 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005286 else
Sean Callanana46ec452012-07-11 21:31:24 +00005287 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005288 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
5289
5290 if (!event_data)
Sean Callanana46ec452012-07-11 21:31:24 +00005291 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005292 event_explanation = "<no event data>";
5293 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005294 }
5295
Jim Inghamcfc09352012-07-27 23:57:19 +00005296 Process *process = event_data->GetProcessSP().get();
5297
5298 if (!process)
Sean Callanana46ec452012-07-11 21:31:24 +00005299 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005300 event_explanation = "<no process>";
5301 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005302 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005303
5304 ThreadList &thread_list = process->GetThreadList();
5305
5306 uint32_t num_threads = thread_list.GetSize();
5307 uint32_t thread_index;
5308
5309 ts.Printf("<%u threads> ", num_threads);
5310
5311 for (thread_index = 0;
5312 thread_index < num_threads;
5313 ++thread_index)
5314 {
5315 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
5316
5317 if (!thread)
5318 {
5319 ts.Printf("<?> ");
5320 continue;
5321 }
5322
Daniel Malead01b2952012-11-29 21:49:15 +00005323 ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00005324 RegisterContext *register_context = thread->GetRegisterContext().get();
5325
5326 if (register_context)
Daniel Malead01b2952012-11-29 21:49:15 +00005327 ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC());
Jim Inghamcfc09352012-07-27 23:57:19 +00005328 else
5329 ts.Printf("[ip unknown] ");
5330
5331 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
5332 if (stop_info_sp)
5333 {
5334 const char *stop_desc = stop_info_sp->GetDescription();
5335 if (stop_desc)
5336 ts.PutCString (stop_desc);
5337 }
5338 ts.Printf(">");
5339 }
5340
5341 event_explanation = ts.GetData();
Sean Callanana46ec452012-07-11 21:31:24 +00005342 }
Sean Callanana46ec452012-07-11 21:31:24 +00005343 } while (0);
5344
Jim Inghamcfc09352012-07-27 23:57:19 +00005345 if (event_explanation)
5346 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
Sean Callanana46ec452012-07-11 21:31:24 +00005347 else
Jim Inghamcfc09352012-07-27 23:57:19 +00005348 log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData());
5349 }
5350
Jim Ingham184e9812013-01-15 02:47:48 +00005351 if (should_unwind && thread_plan_sp)
Jim Inghamcfc09352012-07-27 23:57:19 +00005352 {
5353 if (log)
5354 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.", thread_plan_sp.get());
5355 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5356 thread_plan_sp->SetPrivate (orig_plan_private);
5357 }
5358 else
5359 {
5360 if (log)
5361 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.", thread_plan_sp.get());
Sean Callanana46ec452012-07-11 21:31:24 +00005362 }
5363 }
5364 else if (return_value == eExecutionSetupError)
5365 {
5366 if (log)
5367 log->PutCString("Process::RunThreadPlan(): execution set up error.");
Jim Ingham0f16e732011-02-08 05:20:59 +00005368
Jim Ingham184e9812013-01-15 02:47:48 +00005369 if (unwind_on_error && thread_plan_sp)
Jim Ingham0f16e732011-02-08 05:20:59 +00005370 {
Greg Claytonc14ee322011-09-22 04:58:26 +00005371 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00005372 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham0f16e732011-02-08 05:20:59 +00005373 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005374 }
5375 else
5376 {
Sean Callanana46ec452012-07-11 21:31:24 +00005377 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
Jim Inghamf48169b2010-11-30 02:22:11 +00005378 {
Jim Ingham0f16e732011-02-08 05:20:59 +00005379 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005380 log->PutCString("Process::RunThreadPlan(): thread plan is done");
5381 return_value = eExecutionCompleted;
5382 }
5383 else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
5384 {
5385 if (log)
5386 log->PutCString("Process::RunThreadPlan(): thread plan was discarded");
5387 return_value = eExecutionDiscarded;
5388 }
5389 else
5390 {
5391 if (log)
5392 log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Ingham184e9812013-01-15 02:47:48 +00005393 if (unwind_on_error && thread_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005394 {
5395 if (log)
Jim Ingham184e9812013-01-15 02:47:48 +00005396 log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause unwind_on_error is set.");
Sean Callanana46ec452012-07-11 21:31:24 +00005397 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5398 thread_plan_sp->SetPrivate (orig_plan_private);
5399 }
5400 }
5401 }
5402
5403 // Thread we ran the function in may have gone away because we ran the target
5404 // Check that it's still there, and if it is put it back in the context. Also restore the
5405 // frame in the context if it is still present.
5406 thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get();
5407 if (thread)
5408 {
5409 exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id));
5410 }
5411
5412 // Also restore the current process'es selected frame & thread, since this function calling may
5413 // be done behind the user's back.
5414
5415 if (selected_tid != LLDB_INVALID_THREAD_ID)
5416 {
5417 if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid())
5418 {
5419 // We were able to restore the selected thread, now restore the frame:
5420 StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id);
5421 if (old_frame_sp)
5422 GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get());
Jim Inghamf48169b2010-11-30 02:22:11 +00005423 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005424 }
5425 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005426
Sean Callanana46ec452012-07-11 21:31:24 +00005427 // If the process exited during the run of the thread plan, notify everyone.
Jim Inghamf48169b2010-11-30 02:22:11 +00005428
Sean Callanana46ec452012-07-11 21:31:24 +00005429 if (event_to_broadcast_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00005430 {
Sean Callanana46ec452012-07-11 21:31:24 +00005431 if (log)
5432 log->PutCString("Process::RunThreadPlan(): rebroadcasting event.");
5433 BroadcastEvent(event_to_broadcast_sp);
Jim Inghamf48169b2010-11-30 02:22:11 +00005434 }
5435
5436 return return_value;
5437}
5438
5439const char *
5440Process::ExecutionResultAsCString (ExecutionResults result)
5441{
5442 const char *result_name;
5443
5444 switch (result)
5445 {
Greg Claytone0d378b2011-03-24 21:19:54 +00005446 case eExecutionCompleted:
Jim Inghamf48169b2010-11-30 02:22:11 +00005447 result_name = "eExecutionCompleted";
5448 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00005449 case eExecutionDiscarded:
Jim Inghamf48169b2010-11-30 02:22:11 +00005450 result_name = "eExecutionDiscarded";
5451 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00005452 case eExecutionInterrupted:
Jim Inghamf48169b2010-11-30 02:22:11 +00005453 result_name = "eExecutionInterrupted";
5454 break;
Jim Ingham184e9812013-01-15 02:47:48 +00005455 case eExecutionHitBreakpoint:
5456 result_name = "eExecutionHitBreakpoint";
5457 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00005458 case eExecutionSetupError:
Jim Inghamf48169b2010-11-30 02:22:11 +00005459 result_name = "eExecutionSetupError";
5460 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00005461 case eExecutionTimedOut:
Jim Inghamf48169b2010-11-30 02:22:11 +00005462 result_name = "eExecutionTimedOut";
5463 break;
5464 }
5465 return result_name;
5466}
5467
Greg Clayton7260f622011-04-18 08:33:37 +00005468void
5469Process::GetStatus (Stream &strm)
5470{
5471 const StateType state = GetState();
Greg Clayton2637f822011-11-17 01:23:07 +00005472 if (StateIsStoppedState(state, false))
Greg Clayton7260f622011-04-18 08:33:37 +00005473 {
5474 if (state == eStateExited)
5475 {
5476 int exit_status = GetExitStatus();
5477 const char *exit_description = GetExitDescription();
Daniel Malead01b2952012-11-29 21:49:15 +00005478 strm.Printf ("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n",
Greg Clayton7260f622011-04-18 08:33:37 +00005479 GetID(),
5480 exit_status,
5481 exit_status,
5482 exit_description ? exit_description : "");
5483 }
5484 else
5485 {
5486 if (state == eStateConnected)
5487 strm.Printf ("Connected to remote target.\n");
5488 else
Daniel Malead01b2952012-11-29 21:49:15 +00005489 strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state));
Greg Clayton7260f622011-04-18 08:33:37 +00005490 }
5491 }
5492 else
5493 {
Daniel Malead01b2952012-11-29 21:49:15 +00005494 strm.Printf ("Process %" PRIu64 " is running.\n", GetID());
Greg Clayton7260f622011-04-18 08:33:37 +00005495 }
5496}
5497
5498size_t
5499Process::GetThreadStatus (Stream &strm,
5500 bool only_threads_with_stop_reason,
5501 uint32_t start_frame,
5502 uint32_t num_frames,
5503 uint32_t num_frames_with_source)
5504{
5505 size_t num_thread_infos_dumped = 0;
5506
Jim Ingham41f2b942012-09-10 20:50:15 +00005507 Mutex::Locker locker (GetThreadList().GetMutex());
Greg Clayton7260f622011-04-18 08:33:37 +00005508 const size_t num_threads = GetThreadList().GetSize();
5509 for (uint32_t i = 0; i < num_threads; i++)
5510 {
5511 Thread *thread = GetThreadList().GetThreadAtIndex(i).get();
5512 if (thread)
5513 {
5514 if (only_threads_with_stop_reason)
5515 {
Jim Ingham5d88a062012-10-16 00:09:33 +00005516 StopInfoSP stop_info_sp = thread->GetStopInfo();
5517 if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid())
Greg Clayton7260f622011-04-18 08:33:37 +00005518 continue;
5519 }
5520 thread->GetStatus (strm,
5521 start_frame,
5522 num_frames,
5523 num_frames_with_source);
5524 ++num_thread_infos_dumped;
5525 }
5526 }
5527 return num_thread_infos_dumped;
5528}
5529
Greg Claytona9f40ad2012-02-22 04:37:26 +00005530void
5531Process::AddInvalidMemoryRegion (const LoadRange &region)
5532{
5533 m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize());
5534}
5535
5536bool
5537Process::RemoveInvalidMemoryRange (const LoadRange &region)
5538{
5539 return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize());
5540}
5541
Jim Ingham372787f2012-04-07 00:00:41 +00005542void
5543Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton)
5544{
5545 m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton));
5546}
5547
5548bool
5549Process::RunPreResumeActions ()
5550{
5551 bool result = true;
5552 while (!m_pre_resume_actions.empty())
5553 {
5554 struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back();
5555 m_pre_resume_actions.pop_back();
5556 bool this_result = action.callback (action.baton);
5557 if (result == true) result = this_result;
5558 }
5559 return result;
5560}
5561
5562void
5563Process::ClearPreResumeActions ()
5564{
5565 m_pre_resume_actions.clear();
5566}
Greg Claytona9f40ad2012-02-22 04:37:26 +00005567
Greg Claytonfa559e52012-05-18 02:38:05 +00005568void
5569Process::Flush ()
5570{
5571 m_thread_list.Flush();
5572}
Greg Clayton90ba8112012-12-05 00:16:59 +00005573
5574void
5575Process::DidExec ()
5576{
5577 Target &target = GetTarget();
5578 target.CleanupProcess ();
5579 ModuleList unloaded_modules (target.GetImages());
5580 target.ModulesDidUnload (unloaded_modules);
5581 target.GetSectionLoadList().Clear();
5582 m_dynamic_checkers_ap.reset();
5583 m_abi_sp.reset();
5584 m_os_ap.reset();
5585 m_dyld_ap.reset();
5586 m_image_tokens.clear();
5587 m_allocated_memory_cache.Clear();
5588 m_language_runtimes.clear();
5589 DoDidExec();
5590 CompleteAttach ();
5591}