blob: e6804459ccea60ca9e471c9c72ddddedf4c428f9 [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"
21#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Jim Ingham1460e4b2014-01-10 23:46:59 +000023#include "lldb/Symbol/Symbol.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/PluginManager.h"
25#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000026#include "lldb/Core/StreamFile.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000027#include "lldb/Expression/ClangUserExpression.h"
Caroline Tice3df9a8d2010-09-04 00:03:46 +000028#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Host/Host.h"
Greg Clayton44d93782014-01-27 23:43:24 +000030#include "lldb/Host/Terminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/ABI.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000032#include "lldb/Target/DynamicLoader.h"
Andrew MacPherson17220c12014-03-05 10:12:43 +000033#include "lldb/Target/JITLoader.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000034#include "lldb/Target/OperatingSystem.h"
Jim Ingham22777012010-09-23 02:01:19 +000035#include "lldb/Target/LanguageRuntime.h"
36#include "lldb/Target/CPPLanguageRuntime.h"
37#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytone996fd32011-03-08 22:40:15 +000038#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000040#include "lldb/Target/StopInfo.h"
Jason Molendaeef51062013-11-05 03:57:19 +000041#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Target/Target.h"
43#include "lldb/Target/TargetList.h"
44#include "lldb/Target/Thread.h"
45#include "lldb/Target/ThreadPlan.h"
Jim Ingham076b3042012-04-10 01:21:57 +000046#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham1460e4b2014-01-10 23:46:59 +000047#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Charles Davis510938e2013-08-27 05:04:57 +000049#ifndef LLDB_DISABLE_POSIX
50#include <spawn.h>
51#endif
52
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053using namespace lldb;
54using namespace lldb_private;
55
Greg Clayton67cc0632012-08-22 17:17:09 +000056
57// Comment out line below to disable memory caching, overriding the process setting
58// target.process.disable-memory-cache
59#define ENABLE_MEMORY_CACHING
60
61#ifdef ENABLE_MEMORY_CACHING
62#define DISABLE_MEM_CACHE_DEFAULT false
63#else
64#define DISABLE_MEM_CACHE_DEFAULT true
65#endif
66
67class ProcessOptionValueProperties : public OptionValueProperties
68{
69public:
70 ProcessOptionValueProperties (const ConstString &name) :
71 OptionValueProperties (name)
72 {
73 }
74
75 // This constructor is used when creating ProcessOptionValueProperties when it
76 // is part of a new lldb_private::Process instance. It will copy all current
77 // global property values as needed
78 ProcessOptionValueProperties (ProcessProperties *global_properties) :
79 OptionValueProperties(*global_properties->GetValueProperties())
80 {
81 }
82
83 virtual const Property *
84 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
85 {
86 // When gettings the value for a key from the process options, we will always
87 // try and grab the setting from the current process if there is one. Else we just
88 // use the one from this instance.
89 if (exe_ctx)
90 {
91 Process *process = exe_ctx->GetProcessPtr();
92 if (process)
93 {
94 ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get());
95 if (this != instance_properties)
96 return instance_properties->ProtectedGetPropertyAtIndex (idx);
97 }
98 }
99 return ProtectedGetPropertyAtIndex (idx);
100 }
101};
102
103static PropertyDefinition
104g_properties[] =
105{
106 { "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 +0000107 { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. "
108 "For instance, to turn on debugserver logging set this to \"QSetLogging:bitmask=LOG_DEFAULT;\"" },
Jim Inghamafc1b122013-01-31 19:48:57 +0000109 { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." },
110 { "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 +0000111 { "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 +0000112 { "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 +0000113 { "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 +0000114 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
115};
116
117enum {
118 ePropertyDisableMemCache,
Greg Claytonc9d645d2012-10-18 22:40:37 +0000119 ePropertyExtraStartCommand,
Jim Ingham184e9812013-01-15 02:47:48 +0000120 ePropertyIgnoreBreakpointsInExpressions,
121 ePropertyUnwindOnErrorInExpressions,
Jim Ingham29950772013-01-26 02:19:28 +0000122 ePropertyPythonOSPluginPath,
Jim Inghamacff8952013-05-02 00:27:30 +0000123 ePropertyStopOnSharedLibraryEvents,
124 ePropertyDetachKeepsStopped
Greg Clayton67cc0632012-08-22 17:17:09 +0000125};
126
127ProcessProperties::ProcessProperties (bool is_global) :
128 Properties ()
129{
130 if (is_global)
131 {
132 m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process")));
133 m_collection_sp->Initialize(g_properties);
134 m_collection_sp->AppendProperty(ConstString("thread"),
Jim Ingham29950772013-01-26 02:19:28 +0000135 ConstString("Settings specific to threads."),
Greg Clayton67cc0632012-08-22 17:17:09 +0000136 true,
137 Thread::GetGlobalProperties()->GetValueProperties());
138 }
139 else
140 m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get()));
141}
142
143ProcessProperties::~ProcessProperties()
144{
145}
146
147bool
148ProcessProperties::GetDisableMemoryCache() const
149{
150 const uint32_t idx = ePropertyDisableMemCache;
151 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
152}
153
154Args
155ProcessProperties::GetExtraStartupCommands () const
156{
157 Args args;
158 const uint32_t idx = ePropertyExtraStartCommand;
159 m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args);
160 return args;
161}
162
163void
164ProcessProperties::SetExtraStartupCommands (const Args &args)
165{
166 const uint32_t idx = ePropertyExtraStartCommand;
167 m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args);
168}
169
Greg Claytonc9d645d2012-10-18 22:40:37 +0000170FileSpec
171ProcessProperties::GetPythonOSPluginPath () const
172{
173 const uint32_t idx = ePropertyPythonOSPluginPath;
174 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
175}
176
177void
178ProcessProperties::SetPythonOSPluginPath (const FileSpec &file)
179{
180 const uint32_t idx = ePropertyPythonOSPluginPath;
181 m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file);
182}
183
Jim Ingham184e9812013-01-15 02:47:48 +0000184
185bool
186ProcessProperties::GetIgnoreBreakpointsInExpressions () const
187{
188 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
189 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
190}
191
192void
193ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore)
194{
195 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
196 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
197}
198
199bool
200ProcessProperties::GetUnwindOnErrorInExpressions () const
201{
202 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
203 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
204}
205
206void
207ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore)
208{
209 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
210 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
211}
212
Jim Ingham29950772013-01-26 02:19:28 +0000213bool
214ProcessProperties::GetStopOnSharedLibraryEvents () const
215{
216 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
217 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
218}
219
220void
221ProcessProperties::SetStopOnSharedLibraryEvents (bool stop)
222{
223 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
224 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
225}
226
Jim Inghamacff8952013-05-02 00:27:30 +0000227bool
228ProcessProperties::GetDetachKeepsStopped () const
229{
230 const uint32_t idx = ePropertyDetachKeepsStopped;
231 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
232}
233
234void
235ProcessProperties::SetDetachKeepsStopped (bool stop)
236{
237 const uint32_t idx = ePropertyDetachKeepsStopped;
238 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
239}
240
Greg Clayton32e0a752011-03-30 18:16:51 +0000241void
Greg Clayton8b82f082011-04-12 05:54:46 +0000242ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000243{
244 const char *cstr;
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000245 if (m_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000246 s.Printf (" pid = %" PRIu64 "\n", m_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000247
248 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000249 s.Printf (" parent = %" PRIu64 "\n", m_parent_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000250
251 if (m_executable)
252 {
253 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
254 s.PutCString (" file = ");
255 m_executable.Dump(&s);
256 s.EOL();
257 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000258 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000259 if (argc > 0)
260 {
261 for (uint32_t i=0; i<argc; i++)
262 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000263 const char *arg = m_arguments.GetArgumentAtIndex(i);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000264 if (i < 10)
Greg Clayton8b82f082011-04-12 05:54:46 +0000265 s.Printf (" arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000266 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000267 s.Printf ("arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000268 }
269 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000270
271 const uint32_t envc = m_environment.GetArgumentCount();
272 if (envc > 0)
273 {
274 for (uint32_t i=0; i<envc; i++)
275 {
276 const char *env = m_environment.GetArgumentAtIndex(i);
277 if (i < 10)
278 s.Printf (" env[%u] = %s\n", i, env);
279 else
280 s.Printf ("env[%u] = %s\n", i, env);
281 }
282 }
283
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000284 if (m_arch.IsValid())
285 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
286
Greg Clayton8b82f082011-04-12 05:54:46 +0000287 if (m_uid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000288 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000289 cstr = platform->GetUserName (m_uid);
290 s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000291 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000292 if (m_gid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000293 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000294 cstr = platform->GetGroupName (m_gid);
295 s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000296 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000297 if (m_euid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000298 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000299 cstr = platform->GetUserName (m_euid);
300 s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000301 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000302 if (m_egid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000303 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000304 cstr = platform->GetGroupName (m_egid);
305 s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000306 }
307}
308
309void
Greg Clayton8b82f082011-04-12 05:54:46 +0000310ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose)
Greg Clayton32e0a752011-03-30 18:16:51 +0000311{
Greg Clayton8b82f082011-04-12 05:54:46 +0000312 const char *label;
313 if (show_args || verbose)
314 label = "ARGUMENTS";
315 else
316 label = "NAME";
317
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000318 if (verbose)
319 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000320 s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000321 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
322 }
323 else
324 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000325 s.Printf ("PID PARENT USER ARCH %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000326 s.PutCString ("====== ====== ========== ======= ============================\n");
327 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000328}
329
330void
Greg Clayton8b82f082011-04-12 05:54:46 +0000331ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000332{
333 if (m_pid != LLDB_INVALID_PROCESS_ID)
334 {
335 const char *cstr;
Daniel Malead01b2952012-11-29 21:49:15 +0000336 s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000337
Greg Clayton32e0a752011-03-30 18:16:51 +0000338
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000339 if (verbose)
340 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000341 cstr = platform->GetUserName (m_uid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000342 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
343 s.Printf ("%-10s ", cstr);
344 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000345 s.Printf ("%-10u ", m_uid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000346
Greg Clayton8b82f082011-04-12 05:54:46 +0000347 cstr = platform->GetGroupName (m_gid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000348 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
349 s.Printf ("%-10s ", cstr);
350 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000351 s.Printf ("%-10u ", m_gid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000352
Greg Clayton8b82f082011-04-12 05:54:46 +0000353 cstr = platform->GetUserName (m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000354 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
355 s.Printf ("%-10s ", cstr);
356 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000357 s.Printf ("%-10u ", m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000358
Greg Clayton8b82f082011-04-12 05:54:46 +0000359 cstr = platform->GetGroupName (m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000360 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
361 s.Printf ("%-10s ", cstr);
362 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000363 s.Printf ("%-10u ", m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000364 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
365 }
366 else
367 {
Jason Molendafd54b362011-09-20 21:44:10 +0000368 s.Printf ("%-10s %-7d %s ",
Greg Clayton8b82f082011-04-12 05:54:46 +0000369 platform->GetUserName (m_euid),
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000370 (int)m_arch.GetTriple().getArchName().size(),
371 m_arch.GetTriple().getArchName().data());
372 }
373
Greg Clayton8b82f082011-04-12 05:54:46 +0000374 if (verbose || show_args)
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000375 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000376 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000377 if (argc > 0)
378 {
379 for (uint32_t i=0; i<argc; i++)
380 {
381 if (i > 0)
382 s.PutChar (' ');
Greg Clayton8b82f082011-04-12 05:54:46 +0000383 s.PutCString (m_arguments.GetArgumentAtIndex(i));
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000384 }
385 }
386 }
387 else
388 {
389 s.PutCString (GetName());
390 }
391
392 s.EOL();
Greg Clayton32e0a752011-03-30 18:16:51 +0000393 }
394}
395
Greg Clayton8b82f082011-04-12 05:54:46 +0000396
397void
Greg Clayton45392552012-10-17 22:57:12 +0000398ProcessInfo::SetArguments (char const **argv, bool first_arg_is_executable)
Greg Clayton982c9762011-11-03 21:22:33 +0000399{
400 m_arguments.SetArguments (argv);
401
402 // Is the first argument the executable?
403 if (first_arg_is_executable)
404 {
405 const char *first_arg = m_arguments.GetArgumentAtIndex (0);
406 if (first_arg)
407 {
408 // Yes the first argument is an executable, set it as the executable
409 // in the launch options. Don't resolve the file path as the path
410 // could be a remote platform path
411 const bool resolve = false;
412 m_executable.SetFile(first_arg, resolve);
Greg Clayton982c9762011-11-03 21:22:33 +0000413 }
414 }
415}
416void
Greg Clayton45392552012-10-17 22:57:12 +0000417ProcessInfo::SetArguments (const Args& args, bool first_arg_is_executable)
Greg Clayton8b82f082011-04-12 05:54:46 +0000418{
419 // Copy all arguments
420 m_arguments = args;
421
422 // Is the first argument the executable?
423 if (first_arg_is_executable)
424 {
Greg Clayton982c9762011-11-03 21:22:33 +0000425 const char *first_arg = m_arguments.GetArgumentAtIndex (0);
Greg Clayton8b82f082011-04-12 05:54:46 +0000426 if (first_arg)
427 {
428 // Yes the first argument is an executable, set it as the executable
429 // in the launch options. Don't resolve the file path as the path
430 // could be a remote platform path
431 const bool resolve = false;
432 m_executable.SetFile(first_arg, resolve);
Greg Clayton8b82f082011-04-12 05:54:46 +0000433 }
434 }
435}
436
Greg Clayton1d885962011-11-08 02:43:13 +0000437void
Greg Claytonee95ed52011-11-17 22:14:31 +0000438ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty)
Greg Clayton1d885962011-11-08 02:43:13 +0000439{
Greg Claytonc71e7bc2014-04-17 17:27:28 +0000440 // If nothing for stdin or stdout or stderr was specified, then check the process for any default
Greg Clayton1d885962011-11-08 02:43:13 +0000441 // settings that were set with "settings set"
Greg Claytonc71e7bc2014-04-17 17:27:28 +0000442 if (GetFileActionForFD(STDIN_FILENO) == NULL || GetFileActionForFD(STDOUT_FILENO) == NULL ||
443 GetFileActionForFD(STDERR_FILENO) == NULL)
Greg Clayton1d885962011-11-08 02:43:13 +0000444 {
Greg Clayton1d885962011-11-08 02:43:13 +0000445 if (m_flags.Test(eLaunchFlagDisableSTDIO))
446 {
Greg Clayton9845a8d2012-03-06 04:01:04 +0000447 AppendSuppressFileAction (STDIN_FILENO , true, false);
448 AppendSuppressFileAction (STDOUT_FILENO, false, true);
449 AppendSuppressFileAction (STDERR_FILENO, false, true);
Greg Clayton1d885962011-11-08 02:43:13 +0000450 }
451 else
452 {
453 // Check for any values that might have gotten set with any of:
454 // (lldb) settings set target.input-path
455 // (lldb) settings set target.output-path
456 // (lldb) settings set target.error-path
Greg Clayton67cc0632012-08-22 17:17:09 +0000457 FileSpec in_path;
458 FileSpec out_path;
459 FileSpec err_path;
Greg Clayton1d885962011-11-08 02:43:13 +0000460 if (target)
461 {
Greg Clayton9845a8d2012-03-06 04:01:04 +0000462 in_path = target->GetStandardInputPath();
463 out_path = target->GetStandardOutputPath();
464 err_path = target->GetStandardErrorPath();
Greg Claytonee95ed52011-11-17 22:14:31 +0000465 }
Greg Claytonc71e7bc2014-04-17 17:27:28 +0000466
467 char path[PATH_MAX];
468 if (in_path && in_path.GetPath(path, sizeof(path)))
469 AppendOpenFileAction(STDIN_FILENO, path, true, false);
470
471 if (out_path && out_path.GetPath(path, sizeof(path)))
472 AppendOpenFileAction(STDOUT_FILENO, path, false, true);
473
474 if (err_path && err_path.GetPath(path, sizeof(path)))
475 AppendOpenFileAction(STDERR_FILENO, path, false, true);
476
477 if (default_to_use_pty && (!in_path || !out_path || !err_path)) {
478 if (m_pty.OpenFirstAvailableMaster(O_RDWR| O_NOCTTY, NULL, 0)) {
479 const char *slave_path = m_pty.GetSlaveName(NULL, 0);
480
481 if (!in_path) {
482 AppendOpenFileAction(STDIN_FILENO, slave_path, true, false);
483 }
484
485 if (!out_path) {
486 AppendOpenFileAction(STDOUT_FILENO, slave_path, false, true);
487 }
488
489 if (!err_path) {
490 AppendOpenFileAction(STDERR_FILENO, slave_path, false, true);
491 }
Greg Clayton1d885962011-11-08 02:43:13 +0000492 }
493 }
Greg Clayton1d885962011-11-08 02:43:13 +0000494 }
495 }
496}
497
Greg Clayton144f3a92011-11-15 03:53:30 +0000498
499bool
Greg Claytond1cf11a2012-04-14 01:42:46 +0000500ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error,
501 bool localhost,
502 bool will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +0000503 bool first_arg_is_full_shell_command,
504 int32_t num_resumes)
Greg Clayton144f3a92011-11-15 03:53:30 +0000505{
506 error.Clear();
507
508 if (GetFlags().Test (eLaunchFlagLaunchInShell))
509 {
510 const char *shell_executable = GetShell();
511 if (shell_executable)
512 {
513 char shell_resolved_path[PATH_MAX];
514
515 if (localhost)
516 {
517 FileSpec shell_filespec (shell_executable, true);
518
519 if (!shell_filespec.Exists())
520 {
521 // Resolve the path in case we just got "bash", "sh" or "tcsh"
522 if (!shell_filespec.ResolveExecutableLocation ())
523 {
524 error.SetErrorStringWithFormat("invalid shell path '%s'", shell_executable);
525 return false;
526 }
527 }
528 shell_filespec.GetPath (shell_resolved_path, sizeof(shell_resolved_path));
529 shell_executable = shell_resolved_path;
530 }
531
Greg Clayton45392552012-10-17 22:57:12 +0000532 const char **argv = GetArguments().GetConstArgumentVector ();
533 if (argv == NULL || argv[0] == NULL)
534 return false;
Greg Clayton144f3a92011-11-15 03:53:30 +0000535 Args shell_arguments;
536 std::string safe_arg;
537 shell_arguments.AppendArgument (shell_executable);
Greg Clayton144f3a92011-11-15 03:53:30 +0000538 shell_arguments.AppendArgument ("-c");
Greg Claytond1cf11a2012-04-14 01:42:46 +0000539 StreamString shell_command;
540 if (will_debug)
Greg Clayton144f3a92011-11-15 03:53:30 +0000541 {
Greg Clayton45392552012-10-17 22:57:12 +0000542 // Add a modified PATH environment variable in case argv[0]
543 // is a relative path
544 const char *argv0 = argv[0];
545 if (argv0 && (argv0[0] != '/' && argv0[0] != '~'))
546 {
547 // We have a relative path to our executable which may not work if
548 // we just try to run "a.out" (without it being converted to "./a.out")
549 const char *working_dir = GetWorkingDirectory();
Greg Clayton8938f8d2013-02-14 03:54:39 +0000550 // Be sure to put quotes around PATH's value in case any paths have spaces...
551 std::string new_path("PATH=\"");
Greg Clayton45392552012-10-17 22:57:12 +0000552 const size_t empty_path_len = new_path.size();
553
554 if (working_dir && working_dir[0])
555 {
556 new_path += working_dir;
557 }
558 else
559 {
560 char current_working_dir[PATH_MAX];
561 const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
562 if (cwd && cwd[0])
563 new_path += cwd;
564 }
565 const char *curr_path = getenv("PATH");
566 if (curr_path)
567 {
568 if (new_path.size() > empty_path_len)
569 new_path += ':';
570 new_path += curr_path;
571 }
Greg Clayton8938f8d2013-02-14 03:54:39 +0000572 new_path += "\" ";
Greg Clayton45392552012-10-17 22:57:12 +0000573 shell_command.PutCString(new_path.c_str());
574 }
575
Greg Claytond1cf11a2012-04-14 01:42:46 +0000576 shell_command.PutCString ("exec");
Greg Clayton45392552012-10-17 22:57:12 +0000577
Greg Clayton45392552012-10-17 22:57:12 +0000578 // Only Apple supports /usr/bin/arch being able to specify the architecture
Greg Claytond1cf11a2012-04-14 01:42:46 +0000579 if (GetArchitecture().IsValid())
580 {
581 shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
Greg Clayton45392552012-10-17 22:57:12 +0000582 // Set the resume count to 2:
Greg Claytond1cf11a2012-04-14 01:42:46 +0000583 // 1 - stop in shell
584 // 2 - stop in /usr/bin/arch
585 // 3 - then we will stop in our program
Jim Inghamdf0ae222013-09-10 02:09:47 +0000586 SetResumeCount(num_resumes + 1);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000587 }
588 else
589 {
Greg Clayton45392552012-10-17 22:57:12 +0000590 // Set the resume count to 1:
Greg Claytond1cf11a2012-04-14 01:42:46 +0000591 // 1 - stop in shell
592 // 2 - then we will stop in our program
Jim Inghamdf0ae222013-09-10 02:09:47 +0000593 SetResumeCount(num_resumes);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000594 }
Greg Clayton144f3a92011-11-15 03:53:30 +0000595 }
Greg Clayton45392552012-10-17 22:57:12 +0000596
597 if (first_arg_is_full_shell_command)
Greg Clayton144f3a92011-11-15 03:53:30 +0000598 {
Greg Clayton45392552012-10-17 22:57:12 +0000599 // There should only be one argument that is the shell command itself to be used as is
600 if (argv[0] && !argv[1])
601 shell_command.Printf("%s", argv[0]);
Greg Claytond1cf11a2012-04-14 01:42:46 +0000602 else
Greg Clayton45392552012-10-17 22:57:12 +0000603 return false;
Greg Clayton144f3a92011-11-15 03:53:30 +0000604 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000605 else
606 {
Greg Clayton45392552012-10-17 22:57:12 +0000607 for (size_t i=0; argv[i] != NULL; ++i)
608 {
609 const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
610 shell_command.Printf(" %s", arg);
611 }
Greg Claytond1cf11a2012-04-14 01:42:46 +0000612 }
Greg Clayton45392552012-10-17 22:57:12 +0000613 shell_arguments.AppendArgument (shell_command.GetString().c_str());
Greg Clayton144f3a92011-11-15 03:53:30 +0000614 m_executable.SetFile(shell_executable, false);
615 m_arguments = shell_arguments;
616 return true;
617 }
618 else
619 {
620 error.SetErrorString ("invalid shell path");
621 }
622 }
623 else
624 {
625 error.SetErrorString ("not launching in shell");
626 }
627 return false;
628}
629
630
Greg Clayton32e0a752011-03-30 18:16:51 +0000631bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000632ProcessLaunchInfo::FileAction::Open (int fd, const char *path, bool read, bool write)
633{
634 if ((read || write) && fd >= 0 && path && path[0])
635 {
636 m_action = eFileActionOpen;
637 m_fd = fd;
638 if (read && write)
Greg Clayton144f3a92011-11-15 03:53:30 +0000639 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
Greg Clayton8b82f082011-04-12 05:54:46 +0000640 else if (read)
Greg Clayton144f3a92011-11-15 03:53:30 +0000641 m_arg = O_NOCTTY | O_RDONLY;
Greg Clayton8b82f082011-04-12 05:54:46 +0000642 else
Greg Clayton144f3a92011-11-15 03:53:30 +0000643 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
Greg Clayton8b82f082011-04-12 05:54:46 +0000644 m_path.assign (path);
645 return true;
646 }
647 else
648 {
649 Clear();
650 }
651 return false;
652}
653
654bool
655ProcessLaunchInfo::FileAction::Close (int fd)
656{
657 Clear();
658 if (fd >= 0)
659 {
660 m_action = eFileActionClose;
661 m_fd = fd;
662 }
663 return m_fd >= 0;
664}
665
666
667bool
668ProcessLaunchInfo::FileAction::Duplicate (int fd, int dup_fd)
669{
670 Clear();
671 if (fd >= 0 && dup_fd >= 0)
672 {
673 m_action = eFileActionDuplicate;
674 m_fd = fd;
675 m_arg = dup_fd;
676 }
677 return m_fd >= 0;
678}
679
680
681
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000682#ifndef LLDB_DISABLE_POSIX
Greg Clayton8b82f082011-04-12 05:54:46 +0000683bool
Charles Davis510938e2013-08-27 05:04:57 +0000684ProcessLaunchInfo::FileAction::AddPosixSpawnFileAction (void *_file_actions,
Greg Clayton8b82f082011-04-12 05:54:46 +0000685 const FileAction *info,
686 Log *log,
687 Error& error)
688{
689 if (info == NULL)
690 return false;
691
Charles Davis510938e2013-08-27 05:04:57 +0000692 posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions);
693
Greg Clayton8b82f082011-04-12 05:54:46 +0000694 switch (info->m_action)
695 {
696 case eFileActionNone:
697 error.Clear();
698 break;
699
700 case eFileActionClose:
701 if (info->m_fd == -1)
702 error.SetErrorString ("invalid fd for posix_spawn_file_actions_addclose(...)");
703 else
704 {
705 error.SetError (::posix_spawn_file_actions_addclose (file_actions, info->m_fd),
706 eErrorTypePOSIX);
707 if (log && (error.Fail() || log))
708 error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000709 static_cast<void*>(file_actions), info->m_fd);
Greg Clayton8b82f082011-04-12 05:54:46 +0000710 }
711 break;
712
713 case eFileActionDuplicate:
714 if (info->m_fd == -1)
715 error.SetErrorString ("invalid fd for posix_spawn_file_actions_adddup2(...)");
716 else if (info->m_arg == -1)
717 error.SetErrorString ("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
718 else
719 {
720 error.SetError (::posix_spawn_file_actions_adddup2 (file_actions, info->m_fd, info->m_arg),
721 eErrorTypePOSIX);
722 if (log && (error.Fail() || log))
723 error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000724 static_cast<void*>(file_actions), info->m_fd,
725 info->m_arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000726 }
727 break;
728
729 case eFileActionOpen:
730 if (info->m_fd == -1)
731 error.SetErrorString ("invalid fd in posix_spawn_file_actions_addopen(...)");
732 else
733 {
734 int oflag = info->m_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000735
Greg Clayton8b82f082011-04-12 05:54:46 +0000736 mode_t mode = 0;
737
Greg Clayton144f3a92011-11-15 03:53:30 +0000738 if (oflag & O_CREAT)
739 mode = 0640;
740
Greg Clayton8b82f082011-04-12 05:54:46 +0000741 error.SetError (::posix_spawn_file_actions_addopen (file_actions,
742 info->m_fd,
743 info->m_path.c_str(),
744 oflag,
745 mode),
746 eErrorTypePOSIX);
747 if (error.Fail() || log)
748 error.PutToLog(log,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000749 "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)",
750 static_cast<void*>(file_actions), info->m_fd,
751 info->m_path.c_str(), oflag, mode);
Greg Clayton8b82f082011-04-12 05:54:46 +0000752 }
753 break;
Greg Clayton8b82f082011-04-12 05:54:46 +0000754 }
755 return error.Success();
756}
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000757#endif
Greg Clayton8b82f082011-04-12 05:54:46 +0000758
759Error
Greg Claytonf6b8b582011-04-13 00:18:08 +0000760ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Clayton8b82f082011-04-12 05:54:46 +0000761{
762 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000763 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton8b82f082011-04-12 05:54:46 +0000764
765 switch (short_option)
766 {
767 case 's': // Stop at program entry point
768 launch_info.GetFlags().Set (eLaunchFlagStopAtEntry);
769 break;
770
Greg Clayton8b82f082011-04-12 05:54:46 +0000771 case 'i': // STDIN for read only
772 {
773 ProcessLaunchInfo::FileAction action;
Greg Clayton9845a8d2012-03-06 04:01:04 +0000774 if (action.Open (STDIN_FILENO, option_arg, true, false))
Greg Clayton8b82f082011-04-12 05:54:46 +0000775 launch_info.AppendFileAction (action);
776 }
777 break;
778
779 case 'o': // Open STDOUT for write only
780 {
781 ProcessLaunchInfo::FileAction action;
Greg Clayton9845a8d2012-03-06 04:01:04 +0000782 if (action.Open (STDOUT_FILENO, option_arg, false, true))
783 launch_info.AppendFileAction (action);
784 }
785 break;
786
787 case 'e': // STDERR for write only
788 {
789 ProcessLaunchInfo::FileAction action;
790 if (action.Open (STDERR_FILENO, option_arg, false, true))
Greg Clayton8b82f082011-04-12 05:54:46 +0000791 launch_info.AppendFileAction (action);
792 }
793 break;
794
Greg Clayton9845a8d2012-03-06 04:01:04 +0000795
Greg Clayton8b82f082011-04-12 05:54:46 +0000796 case 'p': // Process plug-in name
797 launch_info.SetProcessPluginName (option_arg);
798 break;
799
800 case 'n': // Disable STDIO
801 {
802 ProcessLaunchInfo::FileAction action;
Greg Clayton9845a8d2012-03-06 04:01:04 +0000803 if (action.Open (STDIN_FILENO, "/dev/null", true, false))
Greg Clayton8b82f082011-04-12 05:54:46 +0000804 launch_info.AppendFileAction (action);
Greg Clayton9845a8d2012-03-06 04:01:04 +0000805 if (action.Open (STDOUT_FILENO, "/dev/null", false, true))
Greg Clayton8b82f082011-04-12 05:54:46 +0000806 launch_info.AppendFileAction (action);
Greg Clayton9845a8d2012-03-06 04:01:04 +0000807 if (action.Open (STDERR_FILENO, "/dev/null", false, true))
Greg Clayton8b82f082011-04-12 05:54:46 +0000808 launch_info.AppendFileAction (action);
809 }
810 break;
811
812 case 'w':
813 launch_info.SetWorkingDirectory (option_arg);
814 break;
815
816 case 't': // Open process in new terminal window
817 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
818 break;
819
820 case 'a':
Greg Clayton70512312012-05-08 01:45:38 +0000821 if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get()))
822 launch_info.GetArchitecture().SetTriple (option_arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000823 break;
824
825 case 'A':
826 launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
827 break;
828
Greg Clayton982c9762011-11-03 21:22:33 +0000829 case 'c':
Greg Clayton144f3a92011-11-15 03:53:30 +0000830 if (option_arg && option_arg[0])
831 launch_info.SetShell (option_arg);
832 else
Ed Masteb8ca4a22013-09-03 23:04:53 +0000833 launch_info.SetShell (LLDB_DEFAULT_SHELL);
Greg Clayton982c9762011-11-03 21:22:33 +0000834 break;
835
Greg Clayton8b82f082011-04-12 05:54:46 +0000836 case 'v':
837 launch_info.GetEnvironmentEntries().AppendArgument(option_arg);
838 break;
839
840 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000841 error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option);
Greg Clayton8b82f082011-04-12 05:54:46 +0000842 break;
843
844 }
845 return error;
846}
847
848OptionDefinition
849ProcessLaunchCommandOptions::g_option_table[] =
850{
Virgile Belloe2607b52013-09-05 16:42:23 +0000851{ LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."},
852{ LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Disable address space layout randomization when launching a process."},
853{ LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
854{ LLDB_OPT_SET_ALL, false, "working-dir", 'w', OptionParser::eRequiredArgument, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."},
855{ LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."},
856{ LLDB_OPT_SET_ALL, false, "environment", 'v', OptionParser::eRequiredArgument, NULL, 0, eArgTypeNone, "Specify an environment variable name/value string (--environment NAME=VALUE). Can be specified multiple times for subsequent environment entries."},
857{ LLDB_OPT_SET_ALL, false, "shell", 'c', OptionParser::eOptionalArgument, NULL, 0, eArgTypeFilename, "Run the process in a shell (not supported on all platforms)."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000858
Virgile Belloe2607b52013-09-05 16:42:23 +0000859{ LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."},
860{ LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."},
861{ LLDB_OPT_SET_1 , false, "stderr", 'e', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stderr for the process to <filename>."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000862
Virgile Belloe2607b52013-09-05 16:42:23 +0000863{ LLDB_OPT_SET_2 , false, "tty", 't', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Start the process in a terminal (not supported on all platforms)."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000864
Virgile Belloe2607b52013-09-05 16:42:23 +0000865{ LLDB_OPT_SET_3 , false, "no-stdio", 'n', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000866
867{ 0 , false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
868};
869
870
871
872bool
873ProcessInstanceInfoMatch::NameMatches (const char *process_name) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000874{
875 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
876 return true;
877 const char *match_name = m_match_info.GetName();
878 if (!match_name)
879 return true;
880
881 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
882}
883
884bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000885ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000886{
887 if (!NameMatches (proc_info.GetName()))
888 return false;
889
890 if (m_match_info.ProcessIDIsValid() &&
891 m_match_info.GetProcessID() != proc_info.GetProcessID())
892 return false;
893
894 if (m_match_info.ParentProcessIDIsValid() &&
895 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
896 return false;
897
Greg Clayton8b82f082011-04-12 05:54:46 +0000898 if (m_match_info.UserIDIsValid () &&
899 m_match_info.GetUserID() != proc_info.GetUserID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000900 return false;
901
Greg Clayton8b82f082011-04-12 05:54:46 +0000902 if (m_match_info.GroupIDIsValid () &&
903 m_match_info.GetGroupID() != proc_info.GetGroupID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000904 return false;
905
906 if (m_match_info.EffectiveUserIDIsValid () &&
907 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
908 return false;
909
910 if (m_match_info.EffectiveGroupIDIsValid () &&
911 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
912 return false;
913
914 if (m_match_info.GetArchitecture().IsValid() &&
Sean Callananbf4b7be2012-12-13 22:07:14 +0000915 !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture()))
Greg Clayton32e0a752011-03-30 18:16:51 +0000916 return false;
917 return true;
918}
919
920bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000921ProcessInstanceInfoMatch::MatchAllProcesses () const
Greg Clayton32e0a752011-03-30 18:16:51 +0000922{
923 if (m_name_match_type != eNameMatchIgnore)
924 return false;
925
926 if (m_match_info.ProcessIDIsValid())
927 return false;
928
929 if (m_match_info.ParentProcessIDIsValid())
930 return false;
931
Greg Clayton8b82f082011-04-12 05:54:46 +0000932 if (m_match_info.UserIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000933 return false;
934
Greg Clayton8b82f082011-04-12 05:54:46 +0000935 if (m_match_info.GroupIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000936 return false;
937
938 if (m_match_info.EffectiveUserIDIsValid ())
939 return false;
940
941 if (m_match_info.EffectiveGroupIDIsValid ())
942 return false;
943
944 if (m_match_info.GetArchitecture().IsValid())
945 return false;
946
947 if (m_match_all_users)
948 return false;
949
950 return true;
951
952}
953
954void
Greg Clayton8b82f082011-04-12 05:54:46 +0000955ProcessInstanceInfoMatch::Clear()
Greg Clayton32e0a752011-03-30 18:16:51 +0000956{
957 m_match_info.Clear();
958 m_name_match_type = eNameMatchIgnore;
959 m_match_all_users = false;
960}
Greg Clayton58be07b2011-01-07 06:08:19 +0000961
Greg Claytonc3776bf2012-02-09 06:16:32 +0000962ProcessSP
963Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964{
Greg Clayton949e8222013-01-16 17:29:04 +0000965 static uint32_t g_process_unique_id = 0;
966
Greg Claytonc3776bf2012-02-09 06:16:32 +0000967 ProcessSP process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968 ProcessCreateInstance create_callback = NULL;
969 if (plugin_name)
970 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000971 ConstString const_plugin_name(plugin_name);
972 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973 if (create_callback)
974 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000975 process_sp = create_callback(target, listener, crash_file_path);
976 if (process_sp)
977 {
Greg Clayton949e8222013-01-16 17:29:04 +0000978 if (process_sp->CanDebug(target, true))
979 {
980 process_sp->m_process_unique_id = ++g_process_unique_id;
981 }
982 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000983 process_sp.reset();
984 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 }
986 }
987 else
988 {
Greg Claytonc982c762010-07-09 20:39:50 +0000989 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000991 process_sp = create_callback(target, listener, crash_file_path);
992 if (process_sp)
993 {
Greg Clayton949e8222013-01-16 17:29:04 +0000994 if (process_sp->CanDebug(target, false))
995 {
996 process_sp->m_process_unique_id = ++g_process_unique_id;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000997 break;
Greg Clayton949e8222013-01-16 17:29:04 +0000998 }
999 else
1000 process_sp.reset();
Greg Claytonc3776bf2012-02-09 06:16:32 +00001001 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002 }
1003 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00001004 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005}
1006
Jim Ingham4bddaeb2012-02-16 06:50:00 +00001007ConstString &
1008Process::GetStaticBroadcasterClass ()
1009{
1010 static ConstString class_name ("lldb.process");
1011 return class_name;
1012}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013
1014//----------------------------------------------------------------------
1015// Process constructor
1016//----------------------------------------------------------------------
1017Process::Process(Target &target, Listener &listener) :
Greg Clayton67cc0632012-08-22 17:17:09 +00001018 ProcessProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 UserID (LLDB_INVALID_PROCESS_ID),
Jim Ingham4bddaeb2012-02-16 06:50:00 +00001020 Broadcaster (&(target.GetDebugger()), "lldb.process"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021 m_target (target),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 m_public_state (eStateUnloaded),
1023 m_private_state (eStateUnloaded),
Jim Ingham4bddaeb2012-02-16 06:50:00 +00001024 m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"),
1025 m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 m_private_state_listener ("lldb.process.internal_state_listener"),
1027 m_private_state_control_wait(),
1028 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
Jim Ingham4b536182011-08-09 02:12:22 +00001029 m_mod_id (),
Greg Clayton949e8222013-01-16 17:29:04 +00001030 m_process_unique_id(0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031 m_thread_index_id (0),
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001032 m_thread_id_to_index_id_map (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001033 m_exit_status (-1),
1034 m_exit_string (),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001035 m_thread_mutex (Mutex::eMutexTypeRecursive),
1036 m_thread_list_real (this),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037 m_thread_list (this),
Jason Molenda864f1cc2013-11-11 05:20:44 +00001038 m_extended_thread_list (this),
Jason Molenda4ff13262013-11-20 00:31:38 +00001039 m_extended_thread_stop_id (0),
Jason Molenda5e8dce42013-12-13 00:29:16 +00001040 m_queue_list (this),
1041 m_queue_list_stop_id (0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042 m_notifications (),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001043 m_image_tokens (),
1044 m_listener (listener),
1045 m_breakpoint_site_list (),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001046 m_dynamic_checkers_ap (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001047 m_unix_signals (),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001048 m_abi_sp (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +00001049 m_process_input_reader (),
Greg Clayton3e06bd92011-01-09 21:07:35 +00001050 m_stdio_communication ("process.stdio"),
Greg Clayton3af9ea52010-11-18 05:57:03 +00001051 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton58be07b2011-01-07 06:08:19 +00001052 m_stdout_data (),
Greg Clayton93e86192011-11-13 04:45:22 +00001053 m_stderr_data (),
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001054 m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive),
1055 m_profile_data (),
Greg Claytond495c532011-05-17 03:37:42 +00001056 m_memory_cache (*this),
1057 m_allocated_memory_cache (*this),
Greg Claytone24c4ac2011-11-17 04:46:02 +00001058 m_should_detach (false),
Sean Callanan90539452011-09-20 23:01:51 +00001059 m_next_event_action_ap(),
Greg Clayton96249852013-04-18 16:57:27 +00001060 m_public_run_lock (),
Greg Clayton96249852013-04-18 16:57:27 +00001061 m_private_run_lock (),
Jim Inghamaacc3182012-06-06 00:29:30 +00001062 m_currently_handling_event(false),
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001063 m_finalize_called(false),
Greg Claytonf9b57b92013-05-10 23:48:10 +00001064 m_clear_thread_plans_on_stop (false),
Jim Ingham1460e4b2014-01-10 23:46:59 +00001065 m_force_next_event_delivery(false),
Jim Ingham0161b492013-02-09 01:29:05 +00001066 m_last_broadcast_state (eStateInvalid),
Jason Molenda69b6b632013-03-05 03:33:59 +00001067 m_destroy_in_process (false),
1068 m_can_jit(eCanJITDontKnow)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069{
Jim Ingham4bddaeb2012-02-16 06:50:00 +00001070 CheckInWithManager ();
Caroline Tice1559a462010-09-27 00:30:10 +00001071
Greg Clayton5160ce52013-03-27 23:08:40 +00001072 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001074 log->Printf ("%p Process::Process()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001076 SetEventName (eBroadcastBitStateChanged, "state-changed");
1077 SetEventName (eBroadcastBitInterrupt, "interrupt");
1078 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
1079 SetEventName (eBroadcastBitSTDERR, "stderr-available");
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001080 SetEventName (eBroadcastBitProfileData, "profile-data-available");
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001081
Greg Clayton35a4cc52012-10-29 20:52:08 +00001082 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" );
1083 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" );
1084 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume");
1085
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086 listener.StartListeningForEvents (this,
1087 eBroadcastBitStateChanged |
1088 eBroadcastBitInterrupt |
1089 eBroadcastBitSTDOUT |
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001090 eBroadcastBitSTDERR |
1091 eBroadcastBitProfileData);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092
1093 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +00001094 eBroadcastBitStateChanged |
1095 eBroadcastBitInterrupt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096
1097 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
1098 eBroadcastInternalStateControlStop |
1099 eBroadcastInternalStateControlPause |
1100 eBroadcastInternalStateControlResume);
1101}
1102
1103//----------------------------------------------------------------------
1104// Destructor
1105//----------------------------------------------------------------------
1106Process::~Process()
1107{
Greg Clayton5160ce52013-03-27 23:08:40 +00001108 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001110 log->Printf ("%p Process::~Process()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 StopPrivateStateThread();
1112}
1113
Greg Clayton67cc0632012-08-22 17:17:09 +00001114const ProcessPropertiesSP &
1115Process::GetGlobalProperties()
1116{
1117 static ProcessPropertiesSP g_settings_sp;
1118 if (!g_settings_sp)
1119 g_settings_sp.reset (new ProcessProperties (true));
1120 return g_settings_sp;
1121}
1122
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123void
1124Process::Finalize()
1125{
Greg Claytone24c4ac2011-11-17 04:46:02 +00001126 switch (GetPrivateState())
1127 {
1128 case eStateConnected:
1129 case eStateAttaching:
1130 case eStateLaunching:
1131 case eStateStopped:
1132 case eStateRunning:
1133 case eStateStepping:
1134 case eStateCrashed:
1135 case eStateSuspended:
1136 if (GetShouldDetach())
Jim Inghamacff8952013-05-02 00:27:30 +00001137 {
1138 // FIXME: This will have to be a process setting:
1139 bool keep_stopped = false;
1140 Detach(keep_stopped);
1141 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001142 else
1143 Destroy();
1144 break;
1145
1146 case eStateInvalid:
1147 case eStateUnloaded:
1148 case eStateDetached:
1149 case eStateExited:
1150 break;
1151 }
1152
Greg Clayton1ed54f52011-10-01 00:45:15 +00001153 // Clear our broadcaster before we proceed with destroying
1154 Broadcaster::Clear();
1155
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 // Do any cleanup needed prior to being destructed... Subclasses
1157 // that override this method should call this superclass method as well.
Jim Inghamd0a3e122011-02-16 17:54:55 +00001158
1159 // We need to destroy the loader before the derived Process class gets destroyed
1160 // since it is very likely that undoing the loader will require access to the real process.
Greg Clayton894f82f2012-01-20 23:08:34 +00001161 m_dynamic_checkers_ap.reset();
1162 m_abi_sp.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001163 m_os_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00001164 m_system_runtime_ap.reset();
Greg Clayton894f82f2012-01-20 23:08:34 +00001165 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00001166 m_jit_loaders_ap.reset();
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001167 m_thread_list_real.Destroy();
Greg Claytone1cd1be2012-01-29 20:56:30 +00001168 m_thread_list.Destroy();
Jason Molenda864f1cc2013-11-11 05:20:44 +00001169 m_extended_thread_list.Destroy();
Jason Molenda5e8dce42013-12-13 00:29:16 +00001170 m_queue_list.Clear();
1171 m_queue_list_stop_id = 0;
Greg Clayton894f82f2012-01-20 23:08:34 +00001172 std::vector<Notifications> empty_notifications;
1173 m_notifications.swap(empty_notifications);
1174 m_image_tokens.clear();
1175 m_memory_cache.Clear();
1176 m_allocated_memory_cache.Clear();
1177 m_language_runtimes.clear();
1178 m_next_event_action_ap.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +00001179//#ifdef LLDB_CONFIGURATION_DEBUG
1180// StreamFile s(stdout, false);
1181// EventSP event_sp;
1182// while (m_private_state_listener.GetNextEvent(event_sp))
1183// {
1184// event_sp->Dump (&s);
1185// s.EOL();
1186// }
1187//#endif
1188 // We have to be very careful here as the m_private_state_listener might
1189 // contain events that have ProcessSP values in them which can keep this
1190 // process around forever. These events need to be cleared out.
1191 m_private_state_listener.Clear();
Ed Maste64fad602013-07-29 20:58:06 +00001192 m_public_run_lock.TrySetRunning(); // This will do nothing if already locked
1193 m_public_run_lock.SetStopped();
1194 m_private_run_lock.TrySetRunning(); // This will do nothing if already locked
1195 m_private_run_lock.SetStopped();
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001196 m_finalize_called = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197}
1198
1199void
1200Process::RegisterNotificationCallbacks (const Notifications& callbacks)
1201{
1202 m_notifications.push_back(callbacks);
1203 if (callbacks.initialize != NULL)
1204 callbacks.initialize (callbacks.baton, this);
1205}
1206
1207bool
1208Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
1209{
1210 std::vector<Notifications>::iterator pos, end = m_notifications.end();
1211 for (pos = m_notifications.begin(); pos != end; ++pos)
1212 {
1213 if (pos->baton == callbacks.baton &&
1214 pos->initialize == callbacks.initialize &&
1215 pos->process_state_changed == callbacks.process_state_changed)
1216 {
1217 m_notifications.erase(pos);
1218 return true;
1219 }
1220 }
1221 return false;
1222}
1223
1224void
1225Process::SynchronouslyNotifyStateChanged (StateType state)
1226{
1227 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
1228 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
1229 {
1230 if (notification_pos->process_state_changed)
1231 notification_pos->process_state_changed (notification_pos->baton, this, state);
1232 }
1233}
1234
1235// FIXME: We need to do some work on events before the general Listener sees them.
1236// For instance if we are continuing from a breakpoint, we need to ensure that we do
1237// the little "insert real insn, step & stop" trick. But we can't do that when the
1238// event is delivered by the broadcaster - since that is done on the thread that is
1239// waiting for new events, so if we needed more than one event for our handling, we would
1240// stall. So instead we do it when we fetch the event off of the queue.
1241//
1242
1243StateType
1244Process::GetNextEvent (EventSP &event_sp)
1245{
1246 StateType state = eStateInvalid;
1247
1248 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
1249 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
1250
1251 return state;
1252}
1253
1254
1255StateType
Greg Clayton44d93782014-01-27 23:43:24 +00001256Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr, bool wait_always, Listener *hijack_listener)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257{
Jim Ingham4b536182011-08-09 02:12:22 +00001258 // We can't just wait for a "stopped" event, because the stopped event may have restarted the target.
1259 // We have to actually check each event, and in the case of a stopped event check the restarted flag
1260 // on the event.
Greg Clayton85fb1b92012-09-11 02:33:37 +00001261 if (event_sp_ptr)
1262 event_sp_ptr->reset();
Jim Ingham4b536182011-08-09 02:12:22 +00001263 StateType state = GetState();
1264 // If we are exited or detached, we won't ever get back to any
1265 // other valid state...
1266 if (state == eStateDetached || state == eStateExited)
1267 return state;
1268
Daniel Malea9e9919f2013-10-09 16:56:28 +00001269 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1270 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001271 log->Printf ("Process::%s (timeout = %p)", __FUNCTION__,
1272 static_cast<const void*>(timeout));
Daniel Malea9e9919f2013-10-09 16:56:28 +00001273
1274 if (!wait_always &&
1275 StateIsStoppedState(state, true) &&
1276 StateIsStoppedState(GetPrivateState(), true)) {
1277 if (log)
1278 log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.",
1279 __FUNCTION__);
1280 return state;
1281 }
1282
Jim Ingham4b536182011-08-09 02:12:22 +00001283 while (state != eStateInvalid)
1284 {
Greg Clayton85fb1b92012-09-11 02:33:37 +00001285 EventSP event_sp;
Greg Clayton44d93782014-01-27 23:43:24 +00001286 state = WaitForStateChangedEvents (timeout, event_sp, hijack_listener);
Greg Clayton85fb1b92012-09-11 02:33:37 +00001287 if (event_sp_ptr && event_sp)
1288 *event_sp_ptr = event_sp;
1289
Jim Ingham4b536182011-08-09 02:12:22 +00001290 switch (state)
1291 {
1292 case eStateCrashed:
1293 case eStateDetached:
1294 case eStateExited:
1295 case eStateUnloaded:
Greg Clayton44d93782014-01-27 23:43:24 +00001296 // We need to toggle the run lock as this won't get done in
1297 // SetPublicState() if the process is hijacked.
1298 if (hijack_listener)
1299 m_public_run_lock.SetStopped();
Jim Ingham4b536182011-08-09 02:12:22 +00001300 return state;
1301 case eStateStopped:
1302 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
1303 continue;
1304 else
Greg Clayton44d93782014-01-27 23:43:24 +00001305 {
1306 // We need to toggle the run lock as this won't get done in
1307 // SetPublicState() if the process is hijacked.
1308 if (hijack_listener)
1309 m_public_run_lock.SetStopped();
Jim Ingham4b536182011-08-09 02:12:22 +00001310 return state;
Greg Clayton44d93782014-01-27 23:43:24 +00001311 }
Jim Ingham4b536182011-08-09 02:12:22 +00001312 default:
1313 continue;
1314 }
1315 }
1316 return state;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317}
1318
1319
1320StateType
1321Process::WaitForState
1322(
1323 const TimeValue *timeout,
Greg Clayton44d93782014-01-27 23:43:24 +00001324 const StateType *match_states,
1325 const uint32_t num_match_states
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326)
1327{
1328 EventSP event_sp;
1329 uint32_t i;
Greg Clayton05faeb72010-10-07 04:19:01 +00001330 StateType state = GetState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331 while (state != eStateInvalid)
1332 {
Greg Clayton05faeb72010-10-07 04:19:01 +00001333 // If we are exited or detached, we won't ever get back to any
1334 // other valid state...
1335 if (state == eStateDetached || state == eStateExited)
1336 return state;
1337
Greg Clayton44d93782014-01-27 23:43:24 +00001338 state = WaitForStateChangedEvents (timeout, event_sp, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339
1340 for (i=0; i<num_match_states; ++i)
1341 {
1342 if (match_states[i] == state)
1343 return state;
1344 }
1345 }
1346 return state;
1347}
1348
Jim Ingham30f9b212010-10-11 23:53:14 +00001349bool
1350Process::HijackProcessEvents (Listener *listener)
1351{
1352 if (listener != NULL)
1353 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001354 return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham30f9b212010-10-11 23:53:14 +00001355 }
1356 else
1357 return false;
1358}
1359
1360void
1361Process::RestoreProcessEvents ()
1362{
1363 RestoreBroadcaster();
1364}
1365
Jim Ingham0f16e732011-02-08 05:20:59 +00001366bool
1367Process::HijackPrivateProcessEvents (Listener *listener)
1368{
1369 if (listener != NULL)
1370 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001371 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham0f16e732011-02-08 05:20:59 +00001372 }
1373 else
1374 return false;
1375}
1376
1377void
1378Process::RestorePrivateProcessEvents ()
1379{
1380 m_private_state_broadcaster.RestoreBroadcaster();
1381}
1382
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383StateType
Greg Clayton44d93782014-01-27 23:43:24 +00001384Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp, Listener *hijack_listener)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385{
Greg Clayton5160ce52013-03-27 23:08:40 +00001386 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387
1388 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001389 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1390 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391
Greg Clayton44d93782014-01-27 23:43:24 +00001392 Listener *listener = hijack_listener;
1393 if (listener == NULL)
1394 listener = &m_listener;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001395
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001396 StateType state = eStateInvalid;
Greg Clayton44d93782014-01-27 23:43:24 +00001397 if (listener->WaitForEventForBroadcasterWithType (timeout,
1398 this,
1399 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
1400 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001401 {
1402 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1403 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1404 else if (log)
1405 log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__);
1406 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407
1408 if (log)
1409 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001410 __FUNCTION__, static_cast<const void*>(timeout),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 StateAsCString(state));
1412 return state;
1413}
1414
1415Event *
1416Process::PeekAtStateChangedEvents ()
1417{
Greg Clayton5160ce52013-03-27 23:08:40 +00001418 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419
1420 if (log)
1421 log->Printf ("Process::%s...", __FUNCTION__);
1422
1423 Event *event_ptr;
Greg Clayton3fcbed62010-10-19 03:25:40 +00001424 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
1425 eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426 if (log)
1427 {
1428 if (event_ptr)
1429 {
1430 log->Printf ("Process::%s (event_ptr) => %s",
1431 __FUNCTION__,
1432 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
1433 }
1434 else
1435 {
1436 log->Printf ("Process::%s no events found",
1437 __FUNCTION__);
1438 }
1439 }
1440 return event_ptr;
1441}
1442
1443StateType
1444Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
1445{
Greg Clayton5160ce52013-03-27 23:08:40 +00001446 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447
1448 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001449 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1450 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451
1452 StateType state = eStateInvalid;
Greg Clayton6779606a2011-01-22 23:43:18 +00001453 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
1454 &m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +00001455 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
Greg Clayton6779606a2011-01-22 23:43:18 +00001456 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001457 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1458 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459
1460 // This is a bit of a hack, but when we wait here we could very well return
1461 // to the command-line, and that could disable the log, which would render the
1462 // log we got above invalid.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001464 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
1465 __FUNCTION__, static_cast<const void *>(timeout),
1466 state == eStateInvalid ? "TIMEOUT" : StateAsCString(state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467 return state;
1468}
1469
1470bool
1471Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
1472{
Greg Clayton5160ce52013-03-27 23:08:40 +00001473 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474
1475 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001476 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1477 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478
1479 if (control_only)
1480 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
1481 else
1482 return m_private_state_listener.WaitForEvent(timeout, event_sp);
1483}
1484
1485bool
1486Process::IsRunning () const
1487{
1488 return StateIsRunningState (m_public_state.GetValue());
1489}
1490
1491int
1492Process::GetExitStatus ()
1493{
1494 if (m_public_state.GetValue() == eStateExited)
1495 return m_exit_status;
1496 return -1;
1497}
1498
Greg Clayton85851dd2010-12-04 00:10:17 +00001499
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500const char *
1501Process::GetExitDescription ()
1502{
1503 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
1504 return m_exit_string.c_str();
1505 return NULL;
1506}
1507
Greg Clayton6779606a2011-01-22 23:43:18 +00001508bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509Process::SetExitStatus (int status, const char *cstr)
1510{
Greg Clayton5160ce52013-03-27 23:08:40 +00001511 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Greg Clayton414f5d32011-01-25 02:58:48 +00001512 if (log)
1513 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
1514 status, status,
1515 cstr ? "\"" : "",
1516 cstr ? cstr : "NULL",
1517 cstr ? "\"" : "");
1518
Greg Clayton6779606a2011-01-22 23:43:18 +00001519 // We were already in the exited state
1520 if (m_private_state.GetValue() == eStateExited)
Greg Clayton414f5d32011-01-25 02:58:48 +00001521 {
Greg Clayton385d6032011-01-26 23:47:29 +00001522 if (log)
1523 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton6779606a2011-01-22 23:43:18 +00001524 return false;
Greg Clayton414f5d32011-01-25 02:58:48 +00001525 }
Greg Clayton6779606a2011-01-22 23:43:18 +00001526
1527 m_exit_status = status;
1528 if (cstr)
1529 m_exit_string = cstr;
1530 else
1531 m_exit_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001532
Greg Clayton6779606a2011-01-22 23:43:18 +00001533 DidExit ();
Greg Clayton10177aa2010-12-08 05:08:21 +00001534
Greg Clayton6779606a2011-01-22 23:43:18 +00001535 SetPrivateState (eStateExited);
1536 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001537}
1538
1539// This static callback can be used to watch for local child processes on
1540// the current host. The the child process exits, the process will be
1541// found in the global target list (we want to be completely sure that the
1542// lldb_private::Process doesn't go away before we can deliver the signal.
1543bool
Greg Claytone4e45922011-11-16 05:37:56 +00001544Process::SetProcessExitStatus (void *callback_baton,
1545 lldb::pid_t pid,
1546 bool exited,
1547 int signo, // Zero for no signal
1548 int exit_status // Exit value of process if signal is zero
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549)
1550{
Greg Clayton5160ce52013-03-27 23:08:40 +00001551 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytone4e45922011-11-16 05:37:56 +00001552 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001553 log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n",
Greg Claytone4e45922011-11-16 05:37:56 +00001554 callback_baton,
1555 pid,
1556 exited,
1557 signo,
1558 exit_status);
1559
1560 if (exited)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561 {
Greg Clayton66111032010-06-23 01:19:29 +00001562 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563 if (target_sp)
1564 {
1565 ProcessSP process_sp (target_sp->GetProcessSP());
1566 if (process_sp)
1567 {
1568 const char *signal_cstr = NULL;
1569 if (signo)
1570 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
1571
1572 process_sp->SetExitStatus (exit_status, signal_cstr);
1573 }
1574 }
1575 return true;
1576 }
1577 return false;
1578}
1579
1580
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001581void
1582Process::UpdateThreadListIfNeeded ()
1583{
1584 const uint32_t stop_id = GetStopID();
1585 if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID())
1586 {
Greg Clayton2637f822011-11-17 01:23:07 +00001587 const StateType state = GetPrivateState();
1588 if (StateIsStoppedState (state, true))
1589 {
1590 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytone24c4ac2011-11-17 04:46:02 +00001591 // m_thread_list does have its own mutex, but we need to
1592 // hold onto the mutex between the call to UpdateThreadList(...)
1593 // and the os->UpdateThreadList(...) so it doesn't change on us
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001594 ThreadList &old_thread_list = m_thread_list;
1595 ThreadList real_thread_list(this);
Greg Clayton2637f822011-11-17 01:23:07 +00001596 ThreadList new_thread_list(this);
1597 // Always update the thread list with the protocol specific
Greg Clayton9fc13552012-04-10 00:18:59 +00001598 // thread list, but only update if "true" is returned
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001599 if (UpdateThreadList (m_thread_list_real, real_thread_list))
Greg Clayton9fc13552012-04-10 00:18:59 +00001600 {
Jim Ingham09437922013-03-01 20:04:25 +00001601 // Don't call into the OperatingSystem to update the thread list if we are shutting down, since
1602 // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is
1603 // shutting us down, causing a deadlock.
1604 if (!m_destroy_in_process)
1605 {
1606 OperatingSystem *os = GetOperatingSystem ();
1607 if (os)
Greg Claytonb3ae8762013-04-12 20:07:46 +00001608 {
1609 // Clear any old backing threads where memory threads might have been
1610 // backed by actual threads from the lldb_private::Process subclass
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001611 size_t num_old_threads = old_thread_list.GetSize(false);
Greg Claytonb3ae8762013-04-12 20:07:46 +00001612 for (size_t i=0; i<num_old_threads; ++i)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001613 old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread();
Greg Claytonb3ae8762013-04-12 20:07:46 +00001614
1615 // Now let the OperatingSystem plug-in update the thread list
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001616 os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in
1617 real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass
1618 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 +00001619 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001620 else
1621 {
1622 // No OS plug-in, the new thread list is the same as the real thread list
1623 new_thread_list = real_thread_list;
1624 }
Jim Ingham09437922013-03-01 20:04:25 +00001625 }
Jim Ingham02ff8e02013-06-22 00:55:02 +00001626
1627 m_thread_list_real.Update(real_thread_list);
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001628 m_thread_list.Update (new_thread_list);
1629 m_thread_list.SetStopID (stop_id);
Jason Molendaa6e91302013-11-19 05:44:41 +00001630
Jason Molenda4ff13262013-11-20 00:31:38 +00001631 if (GetLastNaturalStopID () != m_extended_thread_stop_id)
1632 {
1633 // Clear any extended threads that we may have accumulated previously
1634 m_extended_thread_list.Clear();
1635 m_extended_thread_stop_id = GetLastNaturalStopID ();
Jason Molenda5e8dce42013-12-13 00:29:16 +00001636
1637 m_queue_list.Clear();
1638 m_queue_list_stop_id = GetLastNaturalStopID ();
Jason Molenda4ff13262013-11-20 00:31:38 +00001639 }
Greg Clayton9fc13552012-04-10 00:18:59 +00001640 }
Greg Clayton2637f822011-11-17 01:23:07 +00001641 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001642 }
1643}
1644
Jason Molenda5e8dce42013-12-13 00:29:16 +00001645void
1646Process::UpdateQueueListIfNeeded ()
1647{
1648 if (m_system_runtime_ap.get())
1649 {
1650 if (m_queue_list.GetSize() == 0 || m_queue_list_stop_id != GetLastNaturalStopID())
1651 {
1652 const StateType state = GetPrivateState();
1653 if (StateIsStoppedState (state, true))
1654 {
1655 m_system_runtime_ap->PopulateQueueList (m_queue_list);
1656 m_queue_list_stop_id = GetLastNaturalStopID();
1657 }
1658 }
1659 }
1660}
1661
Greg Claytona4d87472013-01-18 23:41:08 +00001662ThreadSP
1663Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context)
1664{
1665 OperatingSystem *os = GetOperatingSystem ();
1666 if (os)
1667 return os->CreateThread(tid, context);
1668 return ThreadSP();
1669}
1670
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001671uint32_t
1672Process::GetNextThreadIndexID (uint64_t thread_id)
1673{
1674 return AssignIndexIDToThread(thread_id);
1675}
1676
1677bool
1678Process::HasAssignedIndexIDToThread(uint64_t thread_id)
1679{
1680 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1681 if (iterator == m_thread_id_to_index_id_map.end())
1682 {
1683 return false;
1684 }
1685 else
1686 {
1687 return true;
1688 }
1689}
1690
1691uint32_t
1692Process::AssignIndexIDToThread(uint64_t thread_id)
1693{
1694 uint32_t result = 0;
1695 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1696 if (iterator == m_thread_id_to_index_id_map.end())
1697 {
1698 result = ++m_thread_index_id;
1699 m_thread_id_to_index_id_map[thread_id] = result;
1700 }
1701 else
1702 {
1703 result = iterator->second;
1704 }
1705
1706 return result;
1707}
1708
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001709StateType
1710Process::GetState()
1711{
1712 // If any other threads access this we will need a mutex for it
1713 return m_public_state.GetValue ();
1714}
1715
1716void
Jim Ingham221d51c2013-05-08 00:35:16 +00001717Process::SetPublicState (StateType new_state, bool restarted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001718{
Greg Clayton5160ce52013-03-27 23:08:40 +00001719 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +00001721 log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001722 const StateType old_state = m_public_state.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723 m_public_state.SetValue (new_state);
Jim Ingham3b8285d2012-04-19 01:40:33 +00001724
1725 // On the transition from Run to Stopped, we unlock the writer end of the
1726 // run lock. The lock gets locked in Resume, which is the public API
1727 // to tell the program to run.
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001728 if (!IsHijackedForEvent(eBroadcastBitStateChanged))
1729 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001730 if (new_state == eStateDetached)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001731 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001732 if (log)
1733 log->Printf("Process::SetPublicState (%s) -- unlocking run lock for detach", StateAsCString(new_state));
Ed Maste64fad602013-07-29 20:58:06 +00001734 m_public_run_lock.SetStopped();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001735 }
1736 else
1737 {
1738 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1739 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
Jim Ingham221d51c2013-05-08 00:35:16 +00001740 if ((old_state_is_stopped != new_state_is_stopped))
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001741 {
Jim Ingham221d51c2013-05-08 00:35:16 +00001742 if (new_state_is_stopped && !restarted)
Sean Callanan8b0737f2012-06-02 01:16:20 +00001743 {
1744 if (log)
1745 log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state));
Ed Maste64fad602013-07-29 20:58:06 +00001746 m_public_run_lock.SetStopped();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001747 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001748 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001749 }
1750 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751}
1752
Jim Ingham3b8285d2012-04-19 01:40:33 +00001753Error
1754Process::Resume ()
1755{
Greg Clayton5160ce52013-03-27 23:08:40 +00001756 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Jim Ingham3b8285d2012-04-19 01:40:33 +00001757 if (log)
1758 log->Printf("Process::Resume -- locking run lock");
Ed Maste64fad602013-07-29 20:58:06 +00001759 if (!m_public_run_lock.TrySetRunning())
Jim Ingham3b8285d2012-04-19 01:40:33 +00001760 {
1761 Error error("Resume request failed - process still running.");
1762 if (log)
Ed Maste64fad602013-07-29 20:58:06 +00001763 log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming.");
Jim Ingham3b8285d2012-04-19 01:40:33 +00001764 return error;
1765 }
1766 return PrivateResume();
1767}
1768
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769StateType
1770Process::GetPrivateState ()
1771{
1772 return m_private_state.GetValue();
1773}
1774
1775void
1776Process::SetPrivateState (StateType new_state)
1777{
Greg Clayton5160ce52013-03-27 23:08:40 +00001778 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001779 bool state_changed = false;
1780
1781 if (log)
1782 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
1783
Andrew Kaylor29d65742013-05-10 17:19:04 +00001784 Mutex::Locker thread_locker(m_thread_list.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001785 Mutex::Locker locker(m_private_state.GetMutex());
1786
1787 const StateType old_state = m_private_state.GetValueNoLock ();
1788 state_changed = old_state != new_state;
Ed Mastec29693f2013-07-02 16:35:47 +00001789
Greg Claytonaa49c832013-05-03 22:25:56 +00001790 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1791 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
1792 if (old_state_is_stopped != new_state_is_stopped)
1793 {
1794 if (new_state_is_stopped)
Ed Maste64fad602013-07-29 20:58:06 +00001795 m_private_run_lock.SetStopped();
Greg Claytonaa49c832013-05-03 22:25:56 +00001796 else
Ed Maste64fad602013-07-29 20:58:06 +00001797 m_private_run_lock.SetRunning();
Greg Claytonaa49c832013-05-03 22:25:56 +00001798 }
Andrew Kaylor93132f52013-05-28 23:04:25 +00001799
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800 if (state_changed)
1801 {
1802 m_private_state.SetValueNoLock (new_state);
Greg Clayton2637f822011-11-17 01:23:07 +00001803 if (StateIsStoppedState(new_state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804 {
Andrew Kaylor29d65742013-05-10 17:19:04 +00001805 // Note, this currently assumes that all threads in the list
1806 // stop when the process stops. In the future we will want to
1807 // support a debugging model where some threads continue to run
1808 // while others are stopped. When that happens we will either need
1809 // a way for the thread list to identify which threads are stopping
1810 // or create a special thread list containing only threads which
1811 // actually stopped.
1812 //
1813 // The process plugin is responsible for managing the actual
1814 // behavior of the threads and should have stopped any threads
1815 // that are going to stop before we get here.
1816 m_thread_list.DidStop();
1817
Jim Ingham4b536182011-08-09 02:12:22 +00001818 m_mod_id.BumpStopID();
Greg Clayton58be07b2011-01-07 06:08:19 +00001819 m_memory_cache.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001820 if (log)
Jim Ingham4b536182011-08-09 02:12:22 +00001821 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001822 }
1823 // Use our target to get a shared pointer to ourselves...
Greg Clayton35a4cc52012-10-29 20:52:08 +00001824 if (m_finalize_called && PrivateStateThreadIsValid() == false)
1825 BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
1826 else
1827 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828 }
1829 else
1830 {
1831 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001832 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001833 }
1834}
1835
Jim Ingham0faa43f2011-11-08 03:00:11 +00001836void
1837Process::SetRunningUserExpression (bool on)
1838{
1839 m_mod_id.SetRunningUserExpression (on);
1840}
1841
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001842addr_t
1843Process::GetImageInfoAddress()
1844{
1845 return LLDB_INVALID_ADDRESS;
1846}
1847
Greg Clayton8f343b02010-11-04 01:54:29 +00001848//----------------------------------------------------------------------
1849// LoadImage
1850//
1851// This function provides a default implementation that works for most
1852// unix variants. Any Process subclasses that need to do shared library
1853// loading differently should override LoadImage and UnloadImage and
1854// do what is needed.
1855//----------------------------------------------------------------------
1856uint32_t
1857Process::LoadImage (const FileSpec &image_spec, Error &error)
1858{
Greg Claytonac7a3db2012-04-18 00:05:19 +00001859 char path[PATH_MAX];
1860 image_spec.GetPath(path, sizeof(path));
1861
Greg Clayton8f343b02010-11-04 01:54:29 +00001862 DynamicLoader *loader = GetDynamicLoader();
1863 if (loader)
1864 {
1865 error = loader->CanLoadImage();
1866 if (error.Fail())
1867 return LLDB_INVALID_IMAGE_TOKEN;
1868 }
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 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001876 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
Greg Clayton8f343b02010-11-04 01:54:29 +00001877
1878 if (frame_sp)
1879 {
1880 ExecutionContext exe_ctx;
1881 frame_sp->CalculateExecutionContext (exe_ctx);
Greg Clayton62afb9f2013-11-04 19:35:17 +00001882 EvaluateExpressionOptions expr_options;
1883 expr_options.SetUnwindOnError(true);
1884 expr_options.SetIgnoreBreakpoints(true);
1885 expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
Greg Clayton8f343b02010-11-04 01:54:29 +00001886 StreamString expr;
Greg Clayton8f343b02010-11-04 01:54:29 +00001887 expr.Printf("dlopen (\"%s\", 2)", path);
1888 const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001889 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +00001890 Error expr_error;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001891 ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001892 expr_options,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001893 expr.GetData(),
1894 prefix,
1895 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001896 expr_error);
1897 if (expr_error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001898 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001899 error = result_valobj_sp->GetError();
1900 if (error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001901 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001902 Scalar scalar;
1903 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001904 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001905 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1906 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
1907 {
1908 uint32_t image_token = m_image_tokens.size();
1909 m_image_tokens.push_back (image_ptr);
1910 return image_token;
1911 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001912 }
1913 }
1914 }
Jim Ingham6c9ed912014-04-03 01:26:14 +00001915 else
1916 error = expr_error;
Greg Clayton8f343b02010-11-04 01:54:29 +00001917 }
1918 }
1919 }
Greg Claytonac7a3db2012-04-18 00:05:19 +00001920 if (!error.AsCString())
1921 error.SetErrorStringWithFormat("unable to load '%s'", path);
Greg Clayton8f343b02010-11-04 01:54:29 +00001922 return LLDB_INVALID_IMAGE_TOKEN;
1923}
1924
1925//----------------------------------------------------------------------
1926// UnloadImage
1927//
1928// This function provides a default implementation that works for most
1929// unix variants. Any Process subclasses that need to do shared library
1930// loading differently should override LoadImage and UnloadImage and
1931// do what is needed.
1932//----------------------------------------------------------------------
1933Error
1934Process::UnloadImage (uint32_t image_token)
1935{
1936 Error error;
1937 if (image_token < m_image_tokens.size())
1938 {
1939 const addr_t image_addr = m_image_tokens[image_token];
1940 if (image_addr == LLDB_INVALID_ADDRESS)
1941 {
1942 error.SetErrorString("image already unloaded");
1943 }
1944 else
1945 {
1946 DynamicLoader *loader = GetDynamicLoader();
1947 if (loader)
1948 error = loader->CanLoadImage();
1949
1950 if (error.Success())
1951 {
1952 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001953
1954 if (thread_sp)
1955 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001956 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
Greg Clayton8f343b02010-11-04 01:54:29 +00001957
1958 if (frame_sp)
1959 {
1960 ExecutionContext exe_ctx;
1961 frame_sp->CalculateExecutionContext (exe_ctx);
Greg Clayton62afb9f2013-11-04 19:35:17 +00001962 EvaluateExpressionOptions expr_options;
1963 expr_options.SetUnwindOnError(true);
1964 expr_options.SetIgnoreBreakpoints(true);
1965 expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
Greg Clayton8f343b02010-11-04 01:54:29 +00001966 StreamString expr;
Daniel Malead01b2952012-11-29 21:49:15 +00001967 expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr);
Greg Clayton8f343b02010-11-04 01:54:29 +00001968 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001969 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +00001970 Error expr_error;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001971 ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001972 expr_options,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001973 expr.GetData(),
1974 prefix,
1975 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001976 expr_error);
Greg Clayton8f343b02010-11-04 01:54:29 +00001977 if (result_valobj_sp->GetError().Success())
1978 {
1979 Scalar scalar;
Jim Ingham6035b672011-03-31 00:19:25 +00001980 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001981 {
1982 if (scalar.UInt(1))
1983 {
1984 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1985 }
1986 else
1987 {
1988 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1989 }
1990 }
1991 }
1992 else
1993 {
1994 error = result_valobj_sp->GetError();
1995 }
1996 }
1997 }
1998 }
1999 }
2000 }
2001 else
2002 {
2003 error.SetErrorString("invalid image token");
2004 }
2005 return error;
2006}
2007
Greg Clayton31f1d2f2011-05-11 18:39:18 +00002008const lldb::ABISP &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002009Process::GetABI()
2010{
Greg Clayton31f1d2f2011-05-11 18:39:18 +00002011 if (!m_abi_sp)
2012 m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture());
2013 return m_abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014}
2015
Jim Ingham22777012010-09-23 02:01:19 +00002016LanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00002017Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00002018{
2019 LanguageRuntimeCollection::iterator pos;
2020 pos = m_language_runtimes.find (language);
Jim Inghamab175242012-03-10 00:22:19 +00002021 if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second))
Jim Ingham22777012010-09-23 02:01:19 +00002022 {
Jim Inghamab175242012-03-10 00:22:19 +00002023 lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language));
Jim Ingham22777012010-09-23 02:01:19 +00002024
Jim Inghamab175242012-03-10 00:22:19 +00002025 m_language_runtimes[language] = runtime_sp;
2026 return runtime_sp.get();
Jim Ingham22777012010-09-23 02:01:19 +00002027 }
2028 else
2029 return (*pos).second.get();
2030}
2031
2032CPPLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00002033Process::GetCPPLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00002034{
Jim Inghamab175242012-03-10 00:22:19 +00002035 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00002036 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
2037 return static_cast<CPPLanguageRuntime *> (runtime);
2038 return NULL;
2039}
2040
2041ObjCLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00002042Process::GetObjCLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00002043{
Jim Inghamab175242012-03-10 00:22:19 +00002044 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00002045 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
2046 return static_cast<ObjCLanguageRuntime *> (runtime);
2047 return NULL;
2048}
2049
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002050bool
2051Process::IsPossibleDynamicValue (ValueObject& in_value)
2052{
2053 if (in_value.IsDynamic())
2054 return false;
2055 LanguageType known_type = in_value.GetObjectRuntimeLanguage();
2056
2057 if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
2058 {
2059 LanguageRuntime *runtime = GetLanguageRuntime (known_type);
2060 return runtime ? runtime->CouldHaveDynamicValue(in_value) : false;
2061 }
2062
2063 LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus);
2064 if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value))
2065 return true;
2066
2067 LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC);
2068 return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false;
2069}
2070
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002071BreakpointSiteList &
2072Process::GetBreakpointSiteList()
2073{
2074 return m_breakpoint_site_list;
2075}
2076
2077const BreakpointSiteList &
2078Process::GetBreakpointSiteList() const
2079{
2080 return m_breakpoint_site_list;
2081}
2082
2083
2084void
2085Process::DisableAllBreakpointSites ()
2086{
Greg Claytond8cf1a12013-06-12 00:46:38 +00002087 m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void {
2088// bp_site->SetEnabled(true);
2089 DisableBreakpointSite(bp_site);
2090 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002091}
2092
2093Error
2094Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
2095{
2096 Error error (DisableBreakpointSiteByID (break_id));
2097
2098 if (error.Success())
2099 m_breakpoint_site_list.Remove(break_id);
2100
2101 return error;
2102}
2103
2104Error
2105Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
2106{
2107 Error error;
2108 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
2109 if (bp_site_sp)
2110 {
2111 if (bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00002112 error = DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002113 }
2114 else
2115 {
Daniel Malead01b2952012-11-29 21:49:15 +00002116 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002117 }
2118
2119 return error;
2120}
2121
2122Error
2123Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
2124{
2125 Error error;
2126 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
2127 if (bp_site_sp)
2128 {
2129 if (!bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00002130 error = EnableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131 }
2132 else
2133 {
Daniel Malead01b2952012-11-29 21:49:15 +00002134 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002135 }
2136 return error;
2137}
2138
Stephen Wilson50bd94f2010-07-17 00:56:13 +00002139lldb::break_id_t
Greg Claytone1cd1be2012-01-29 20:56:30 +00002140Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002141{
Jim Ingham1460e4b2014-01-10 23:46:59 +00002142 addr_t load_addr = LLDB_INVALID_ADDRESS;
2143
2144 bool show_error = true;
2145 switch (GetState())
2146 {
2147 case eStateInvalid:
2148 case eStateUnloaded:
2149 case eStateConnected:
2150 case eStateAttaching:
2151 case eStateLaunching:
2152 case eStateDetached:
2153 case eStateExited:
2154 show_error = false;
2155 break;
2156
2157 case eStateStopped:
2158 case eStateRunning:
2159 case eStateStepping:
2160 case eStateCrashed:
2161 case eStateSuspended:
2162 show_error = IsAlive();
2163 break;
2164 }
2165
2166 // Reset the IsIndirect flag here, in case the location changes from
2167 // pointing to a indirect symbol to a regular symbol.
2168 owner->SetIsIndirect (false);
2169
2170 if (owner->ShouldResolveIndirectFunctions())
2171 {
2172 Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol();
2173 if (symbol && symbol->IsIndirect())
2174 {
2175 Error error;
2176 load_addr = ResolveIndirectFunction (&symbol->GetAddress(), error);
2177 if (!error.Success() && show_error)
2178 {
Greg Clayton44d93782014-01-27 23:43:24 +00002179 m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to resolve indirect function at 0x%" PRIx64 " for breakpoint %i.%i: %s\n",
2180 symbol->GetAddress().GetLoadAddress(&m_target),
2181 owner->GetBreakpoint().GetID(),
2182 owner->GetID(),
2183 error.AsCString() ? error.AsCString() : "unkown error");
Jim Ingham1460e4b2014-01-10 23:46:59 +00002184 return LLDB_INVALID_BREAK_ID;
2185 }
2186 Address resolved_address(load_addr);
2187 load_addr = resolved_address.GetOpcodeLoadAddress (&m_target);
2188 owner->SetIsIndirect(true);
2189 }
2190 else
2191 load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
2192 }
2193 else
2194 load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
2195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002196 if (load_addr != LLDB_INVALID_ADDRESS)
2197 {
2198 BreakpointSiteSP bp_site_sp;
2199
2200 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
2201 // create a new breakpoint site and add it.
2202
2203 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
2204
2205 if (bp_site_sp)
2206 {
2207 bp_site_sp->AddOwner (owner);
2208 owner->SetBreakpointSite (bp_site_sp);
2209 return bp_site_sp->GetID();
2210 }
2211 else
2212 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002213 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, use_hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002214 if (bp_site_sp)
2215 {
Greg Claytoneb023e72013-10-11 19:48:25 +00002216 Error error = EnableBreakpointSite (bp_site_sp.get());
2217 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002218 {
2219 owner->SetBreakpointSite (bp_site_sp);
2220 return m_breakpoint_site_list.Add (bp_site_sp);
2221 }
Greg Claytoneb023e72013-10-11 19:48:25 +00002222 else
2223 {
Greg Claytonfbb76342013-11-20 21:07:01 +00002224 if (show_error)
2225 {
2226 // Report error for setting breakpoint...
Greg Clayton44d93782014-01-27 23:43:24 +00002227 m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n",
2228 load_addr,
2229 owner->GetBreakpoint().GetID(),
2230 owner->GetID(),
2231 error.AsCString() ? error.AsCString() : "unkown error");
Greg Claytonfbb76342013-11-20 21:07:01 +00002232 }
Greg Claytoneb023e72013-10-11 19:48:25 +00002233 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002234 }
2235 }
2236 }
2237 // We failed to enable the breakpoint
2238 return LLDB_INVALID_BREAK_ID;
2239
2240}
2241
2242void
2243Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
2244{
2245 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
2246 if (num_owners == 0)
2247 {
Jim Inghamf1ff3bb2013-04-06 00:16:39 +00002248 // Don't try to disable the site if we don't have a live process anymore.
2249 if (IsAlive())
2250 DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002251 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
2252 }
2253}
2254
2255
2256size_t
2257Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
2258{
2259 size_t bytes_removed = 0;
Jim Ingham20c77192011-06-29 19:42:28 +00002260 BreakpointSiteList bp_sites_in_range;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002261
Jim Ingham20c77192011-06-29 19:42:28 +00002262 if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002263 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002264 bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void {
2265 if (bp_site->GetType() == BreakpointSite::eSoftware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002266 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002267 addr_t intersect_addr;
2268 size_t intersect_size;
2269 size_t opcode_offset;
2270 if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
Jim Ingham20c77192011-06-29 19:42:28 +00002271 {
2272 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
2273 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
Greg Claytond8cf1a12013-06-12 00:46:38 +00002274 assert(opcode_offset + intersect_size <= bp_site->GetByteSize());
Jim Ingham20c77192011-06-29 19:42:28 +00002275 size_t buf_offset = intersect_addr - bp_addr;
Greg Claytond8cf1a12013-06-12 00:46:38 +00002276 ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
Jim Ingham20c77192011-06-29 19:42:28 +00002277 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002278 }
Greg Claytond8cf1a12013-06-12 00:46:38 +00002279 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002280 }
2281 return bytes_removed;
2282}
2283
2284
Greg Claytonded470d2011-03-19 01:12:21 +00002285
2286size_t
2287Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
2288{
2289 PlatformSP platform_sp (m_target.GetPlatform());
2290 if (platform_sp)
2291 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
2292 return 0;
2293}
2294
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002295Error
2296Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
2297{
2298 Error error;
2299 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002300 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002301 const addr_t bp_addr = bp_site->GetLoadAddress();
2302 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002303 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, bp_site->GetID(), (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002304 if (bp_site->IsEnabled())
2305 {
2306 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002307 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 +00002308 return error;
2309 }
2310
2311 if (bp_addr == LLDB_INVALID_ADDRESS)
2312 {
2313 error.SetErrorString("BreakpointSite contains an invalid load address.");
2314 return error;
2315 }
2316 // Ask the lldb::Process subclass to fill in the correct software breakpoint
2317 // trap for the breakpoint site
2318 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
2319
2320 if (bp_opcode_size == 0)
2321 {
Daniel Malead01b2952012-11-29 21:49:15 +00002322 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%" PRIx64, bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002323 }
2324 else
2325 {
2326 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
2327
2328 if (bp_opcode_bytes == NULL)
2329 {
2330 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
2331 return error;
2332 }
2333
2334 // Save the original opcode by reading it
2335 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
2336 {
2337 // Write a software breakpoint in place of the original opcode
2338 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2339 {
2340 uint8_t verify_bp_opcode_bytes[64];
2341 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2342 {
2343 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
2344 {
2345 bp_site->SetEnabled(true);
2346 bp_site->SetType (BreakpointSite::eSoftware);
2347 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002348 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002349 bp_site->GetID(),
2350 (uint64_t)bp_addr);
2351 }
2352 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002353 error.SetErrorString("failed to verify the breakpoint trap in memory.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002354 }
2355 else
2356 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
2357 }
2358 else
2359 error.SetErrorString("Unable to write breakpoint trap to memory.");
2360 }
2361 else
2362 error.SetErrorString("Unable to read memory at breakpoint address.");
2363 }
Stephen Wilson78a4feb2011-01-12 04:20:03 +00002364 if (log && error.Fail())
Daniel Malead01b2952012-11-29 21:49:15 +00002365 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002366 bp_site->GetID(),
2367 (uint64_t)bp_addr,
2368 error.AsCString());
2369 return error;
2370}
2371
2372Error
2373Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
2374{
2375 Error error;
2376 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002377 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002378 addr_t bp_addr = bp_site->GetLoadAddress();
2379 lldb::user_id_t breakID = bp_site->GetID();
2380 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002381 log->Printf ("Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 ") addr = 0x%" PRIx64, breakID, (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002382
2383 if (bp_site->IsHardware())
2384 {
2385 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
2386 }
2387 else if (bp_site->IsEnabled())
2388 {
2389 const size_t break_op_size = bp_site->GetByteSize();
2390 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
2391 if (break_op_size > 0)
2392 {
2393 // Clear a software breakoint instruction
Greg Claytonc982c762010-07-09 20:39:50 +00002394 uint8_t curr_break_op[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002395 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002396 bool break_op_found = false;
2397
2398 // Read the breakpoint opcode
2399 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
2400 {
2401 bool verify = false;
2402 // Make sure we have the a breakpoint opcode exists at this address
2403 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
2404 {
2405 break_op_found = true;
2406 // We found a valid breakpoint opcode at this address, now restore
2407 // the saved opcode.
2408 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
2409 {
2410 verify = true;
2411 }
2412 else
2413 error.SetErrorString("Memory write failed when restoring original opcode.");
2414 }
2415 else
2416 {
2417 error.SetErrorString("Original breakpoint trap is no longer in memory.");
2418 // Set verify to true and so we can check if the original opcode has already been restored
2419 verify = true;
2420 }
2421
2422 if (verify)
2423 {
Greg Claytonc982c762010-07-09 20:39:50 +00002424 uint8_t verify_opcode[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002425 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002426 // Verify that our original opcode made it back to the inferior
2427 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
2428 {
2429 // compare the memory we just read with the original opcode
2430 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
2431 {
2432 // SUCCESS
2433 bp_site->SetEnabled(false);
2434 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002435 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 +00002436 return error;
2437 }
2438 else
2439 {
2440 if (break_op_found)
2441 error.SetErrorString("Failed to restore original opcode.");
2442 }
2443 }
2444 else
2445 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
2446 }
2447 }
2448 else
2449 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
2450 }
2451 }
2452 else
2453 {
2454 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002455 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 +00002456 return error;
2457 }
2458
2459 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002460 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002461 bp_site->GetID(),
2462 (uint64_t)bp_addr,
2463 error.AsCString());
2464 return error;
2465
2466}
2467
Greg Clayton58be07b2011-01-07 06:08:19 +00002468// Uncomment to verify memory caching works after making changes to caching code
2469//#define VERIFY_MEMORY_READS
2470
Sean Callanan64c0cf22012-06-07 22:26:42 +00002471size_t
2472Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2473{
Jason Molendaa7b5afa2013-11-15 00:17:32 +00002474 error.Clear();
Sean Callanan64c0cf22012-06-07 22:26:42 +00002475 if (!GetDisableMemoryCache())
2476 {
Greg Clayton58be07b2011-01-07 06:08:19 +00002477#if defined (VERIFY_MEMORY_READS)
Sean Callanan64c0cf22012-06-07 22:26:42 +00002478 // Memory caching is enabled, with debug verification
2479
2480 if (buf && size)
2481 {
2482 // Uncomment the line below to make sure memory caching is working.
2483 // I ran this through the test suite and got no assertions, so I am
2484 // pretty confident this is working well. If any changes are made to
2485 // memory caching, uncomment the line below and test your changes!
2486
2487 // Verify all memory reads by using the cache first, then redundantly
2488 // reading the same memory from the inferior and comparing to make sure
2489 // everything is exactly the same.
2490 std::string verify_buf (size, '\0');
2491 assert (verify_buf.size() == size);
2492 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
2493 Error verify_error;
2494 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
2495 assert (cache_bytes_read == verify_bytes_read);
2496 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
2497 assert (verify_error.Success() == error.Success());
2498 return cache_bytes_read;
2499 }
2500 return 0;
2501#else // !defined(VERIFY_MEMORY_READS)
2502 // Memory caching is enabled, without debug verification
2503
2504 return m_memory_cache.Read (addr, buf, size, error);
2505#endif // defined (VERIFY_MEMORY_READS)
Greg Clayton58be07b2011-01-07 06:08:19 +00002506 }
Sean Callanan64c0cf22012-06-07 22:26:42 +00002507 else
2508 {
2509 // Memory caching is disabled
2510
2511 return ReadMemoryFromInferior (addr, buf, size, error);
2512 }
Greg Clayton58be07b2011-01-07 06:08:19 +00002513}
Greg Clayton58be07b2011-01-07 06:08:19 +00002514
Greg Clayton4c82d422012-05-18 23:20:01 +00002515size_t
2516Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error)
2517{
Greg Claytonde87c0f2012-05-19 00:18:00 +00002518 char buf[256];
Greg Clayton4c82d422012-05-18 23:20:01 +00002519 out_str.clear();
2520 addr_t curr_addr = addr;
2521 while (1)
2522 {
2523 size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error);
2524 if (length == 0)
2525 break;
2526 out_str.append(buf, length);
2527 // If we got "length - 1" bytes, we didn't get the whole C string, we
2528 // need to read some more characters
2529 if (length == sizeof(buf) - 1)
2530 curr_addr += length;
2531 else
2532 break;
2533 }
2534 return out_str.size();
2535}
2536
Greg Clayton58be07b2011-01-07 06:08:19 +00002537
2538size_t
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +00002539Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error,
2540 size_t type_width)
2541{
2542 size_t total_bytes_read = 0;
2543 if (dst && max_bytes && type_width && max_bytes >= type_width)
2544 {
2545 // Ensure a null terminator independent of the number of bytes that is read.
2546 memset (dst, 0, max_bytes);
2547 size_t bytes_left = max_bytes - type_width;
2548
2549 const char terminator[4] = {'\0', '\0', '\0', '\0'};
2550 assert(sizeof(terminator) >= type_width &&
2551 "Attempting to validate a string with more than 4 bytes per character!");
2552
2553 addr_t curr_addr = addr;
2554 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2555 char *curr_dst = dst;
2556
2557 error.Clear();
2558 while (bytes_left > 0 && error.Success())
2559 {
2560 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2561 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2562 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2563
2564 if (bytes_read == 0)
2565 break;
2566
2567 // Search for a null terminator of correct size and alignment in bytes_read
2568 size_t aligned_start = total_bytes_read - total_bytes_read % type_width;
2569 for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width)
2570 if (::strncmp(&dst[i], terminator, type_width) == 0)
2571 {
2572 error.Clear();
2573 return i;
2574 }
2575
2576 total_bytes_read += bytes_read;
2577 curr_dst += bytes_read;
2578 curr_addr += bytes_read;
2579 bytes_left -= bytes_read;
2580 }
2581 }
2582 else
2583 {
2584 if (max_bytes)
2585 error.SetErrorString("invalid arguments");
2586 }
2587 return total_bytes_read;
2588}
2589
2590// Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find
2591// null terminators.
2592size_t
Greg Claytone91b7952011-12-15 03:14:23 +00002593Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error)
Greg Clayton8b82f082011-04-12 05:54:46 +00002594{
2595 size_t total_cstr_len = 0;
2596 if (dst && dst_max_len)
2597 {
Greg Claytone91b7952011-12-15 03:14:23 +00002598 result_error.Clear();
Greg Clayton8b82f082011-04-12 05:54:46 +00002599 // NULL out everything just to be safe
2600 memset (dst, 0, dst_max_len);
2601 Error error;
2602 addr_t curr_addr = addr;
2603 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2604 size_t bytes_left = dst_max_len - 1;
2605 char *curr_dst = dst;
2606
2607 while (bytes_left > 0)
2608 {
2609 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2610 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2611 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2612
2613 if (bytes_read == 0)
2614 {
Greg Claytone91b7952011-12-15 03:14:23 +00002615 result_error = error;
Greg Clayton8b82f082011-04-12 05:54:46 +00002616 dst[total_cstr_len] = '\0';
2617 break;
2618 }
2619 const size_t len = strlen(curr_dst);
2620
2621 total_cstr_len += len;
2622
2623 if (len < bytes_to_read)
2624 break;
2625
2626 curr_dst += bytes_read;
2627 curr_addr += bytes_read;
2628 bytes_left -= bytes_read;
2629 }
2630 }
Greg Claytone91b7952011-12-15 03:14:23 +00002631 else
2632 {
2633 if (dst == NULL)
2634 result_error.SetErrorString("invalid arguments");
2635 else
2636 result_error.Clear();
2637 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002638 return total_cstr_len;
2639}
2640
2641size_t
Greg Clayton58be07b2011-01-07 06:08:19 +00002642Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
2643{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002644 if (buf == NULL || size == 0)
2645 return 0;
2646
2647 size_t bytes_read = 0;
2648 uint8_t *bytes = (uint8_t *)buf;
2649
2650 while (bytes_read < size)
2651 {
2652 const size_t curr_size = size - bytes_read;
2653 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
2654 bytes + bytes_read,
2655 curr_size,
2656 error);
2657 bytes_read += curr_bytes_read;
2658 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
2659 break;
2660 }
2661
2662 // Replace any software breakpoint opcodes that fall into this range back
2663 // into "buf" before we return
2664 if (bytes_read > 0)
2665 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
2666 return bytes_read;
2667}
2668
Greg Clayton58a4c462010-12-16 20:01:20 +00002669uint64_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002670Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error)
Greg Clayton58a4c462010-12-16 20:01:20 +00002671{
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002672 Scalar scalar;
2673 if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error))
2674 return scalar.ULongLong(fail_value);
2675 return fail_value;
2676}
2677
2678addr_t
2679Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error)
2680{
2681 Scalar scalar;
2682 if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error))
2683 return scalar.ULongLong(LLDB_INVALID_ADDRESS);
2684 return LLDB_INVALID_ADDRESS;
2685}
2686
2687
2688bool
2689Process::WritePointerToMemory (lldb::addr_t vm_addr,
2690 lldb::addr_t ptr_value,
2691 Error &error)
2692{
2693 Scalar scalar;
2694 const uint32_t addr_byte_size = GetAddressByteSize();
2695 if (addr_byte_size <= 4)
2696 scalar = (uint32_t)ptr_value;
Greg Clayton58a4c462010-12-16 20:01:20 +00002697 else
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002698 scalar = ptr_value;
2699 return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size;
Greg Clayton58a4c462010-12-16 20:01:20 +00002700}
2701
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002702size_t
2703Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
2704{
2705 size_t bytes_written = 0;
2706 const uint8_t *bytes = (const uint8_t *)buf;
2707
2708 while (bytes_written < size)
2709 {
2710 const size_t curr_size = size - bytes_written;
2711 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
2712 bytes + bytes_written,
2713 curr_size,
2714 error);
2715 bytes_written += curr_bytes_written;
2716 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
2717 break;
2718 }
2719 return bytes_written;
2720}
2721
2722size_t
2723Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2724{
Greg Clayton58be07b2011-01-07 06:08:19 +00002725#if defined (ENABLE_MEMORY_CACHING)
2726 m_memory_cache.Flush (addr, size);
2727#endif
2728
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002729 if (buf == NULL || size == 0)
2730 return 0;
Jim Ingham78a685a2011-04-16 00:01:13 +00002731
Jim Ingham4b536182011-08-09 02:12:22 +00002732 m_mod_id.BumpMemoryID();
Jim Ingham78a685a2011-04-16 00:01:13 +00002733
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002734 // We need to write any data that would go where any current software traps
2735 // (enabled software breakpoints) any software traps (breakpoints) that we
2736 // may have placed in our tasks memory.
2737
Greg Claytond8cf1a12013-06-12 00:46:38 +00002738 BreakpointSiteList bp_sites_in_range;
2739
2740 if (m_breakpoint_site_list.FindInRange (addr, addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002741 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002742 // No breakpoint sites overlap
2743 if (bp_sites_in_range.IsEmpty())
2744 return WriteMemoryPrivate (addr, buf, size, error);
2745 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002746 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002747 const uint8_t *ubuf = (const uint8_t *)buf;
2748 uint64_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002749
Greg Claytond8cf1a12013-06-12 00:46:38 +00002750 bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void {
2751
2752 if (error.Success())
2753 {
2754 addr_t intersect_addr;
2755 size_t intersect_size;
2756 size_t opcode_offset;
2757 const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset);
2758 assert(intersects);
2759 assert(addr <= intersect_addr && intersect_addr < addr + size);
2760 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
2761 assert(opcode_offset + intersect_size <= bp->GetByteSize());
2762
2763 // Check for bytes before this breakpoint
2764 const addr_t curr_addr = addr + bytes_written;
2765 if (intersect_addr > curr_addr)
2766 {
2767 // There are some bytes before this breakpoint that we need to
2768 // just write to memory
2769 size_t curr_size = intersect_addr - curr_addr;
2770 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
2771 ubuf + bytes_written,
2772 curr_size,
2773 error);
2774 bytes_written += curr_bytes_written;
2775 if (curr_bytes_written != curr_size)
2776 {
2777 // We weren't able to write all of the requested bytes, we
2778 // are done looping and will return the number of bytes that
2779 // we have written so far.
2780 if (error.Success())
2781 error.SetErrorToGenericError();
2782 }
2783 }
2784 // Now write any bytes that would cover up any software breakpoints
2785 // directly into the breakpoint opcode buffer
2786 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
2787 bytes_written += intersect_size;
2788 }
2789 });
2790
2791 if (bytes_written < size)
2792 bytes_written += WriteMemoryPrivate (addr + bytes_written,
2793 ubuf + bytes_written,
2794 size - bytes_written,
2795 error);
2796 }
2797 }
2798 else
2799 {
2800 return WriteMemoryPrivate (addr, buf, size, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002801 }
2802
2803 // Write any remaining bytes after the last breakpoint if we have any left
Greg Claytond8cf1a12013-06-12 00:46:38 +00002804 return 0; //bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002805}
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002806
2807size_t
Greg Claytonc7bece562013-01-25 18:06:21 +00002808Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, size_t byte_size, Error &error)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002809{
2810 if (byte_size == UINT32_MAX)
2811 byte_size = scalar.GetByteSize();
2812 if (byte_size > 0)
2813 {
2814 uint8_t buf[32];
2815 const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error);
2816 if (mem_size > 0)
2817 return WriteMemory(addr, buf, mem_size, error);
2818 else
2819 error.SetErrorString ("failed to get scalar as memory data");
2820 }
2821 else
2822 {
2823 error.SetErrorString ("invalid scalar value");
2824 }
2825 return 0;
2826}
2827
2828size_t
2829Process::ReadScalarIntegerFromMemory (addr_t addr,
2830 uint32_t byte_size,
2831 bool is_signed,
2832 Scalar &scalar,
2833 Error &error)
2834{
Greg Clayton7060f892013-05-01 23:41:30 +00002835 uint64_t uval = 0;
2836 if (byte_size == 0)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002837 {
Greg Clayton7060f892013-05-01 23:41:30 +00002838 error.SetErrorString ("byte size is zero");
2839 }
2840 else if (byte_size & (byte_size - 1))
2841 {
2842 error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size);
2843 }
2844 else if (byte_size <= sizeof(uval))
2845 {
2846 const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002847 if (bytes_read == byte_size)
2848 {
2849 DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00002850 lldb::offset_t offset = 0;
Greg Clayton7060f892013-05-01 23:41:30 +00002851 if (byte_size <= 4)
2852 scalar = data.GetMaxU32 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002853 else
Greg Clayton7060f892013-05-01 23:41:30 +00002854 scalar = data.GetMaxU64 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002855 if (is_signed)
2856 scalar.SignExtend(byte_size * 8);
2857 return bytes_read;
2858 }
2859 }
2860 else
2861 {
2862 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
2863 }
2864 return 0;
2865}
2866
Greg Claytond495c532011-05-17 03:37:42 +00002867#define USE_ALLOCATE_MEMORY_CACHE 1
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002868addr_t
2869Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
2870{
Jim Inghamf72ce3a2011-06-20 17:32:44 +00002871 if (GetPrivateState() != eStateStopped)
2872 return LLDB_INVALID_ADDRESS;
2873
Greg Claytond495c532011-05-17 03:37:42 +00002874#if defined (USE_ALLOCATE_MEMORY_CACHE)
2875 return m_allocated_memory_cache.AllocateMemory(size, permissions, error);
2876#else
Greg Claytonb2daec92011-01-23 19:58:49 +00002877 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
Greg Clayton5160ce52013-03-27 23:08:40 +00002878 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002879 if (log)
Greg Clayton45989072013-10-23 18:24:30 +00002880 log->Printf("Process::AllocateMemory(size=%" PRIu64 ", permissions=%s) => 0x%16.16" PRIx64 " (m_stop_id = %u m_memory_id = %u)",
Deepak Panickald66b50c2013-10-22 12:27:43 +00002881 (uint64_t)size,
Greg Claytond495c532011-05-17 03:37:42 +00002882 GetPermissionsAsCString (permissions),
Greg Claytonb2daec92011-01-23 19:58:49 +00002883 (uint64_t)allocated_addr,
Jim Ingham4b536182011-08-09 02:12:22 +00002884 m_mod_id.GetStopID(),
2885 m_mod_id.GetMemoryID());
Greg Claytonb2daec92011-01-23 19:58:49 +00002886 return allocated_addr;
Greg Claytond495c532011-05-17 03:37:42 +00002887#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002888}
2889
Sean Callanan90539452011-09-20 23:01:51 +00002890bool
2891Process::CanJIT ()
2892{
Sean Callanana7b443a2012-02-14 22:50:38 +00002893 if (m_can_jit == eCanJITDontKnow)
2894 {
2895 Error err;
2896
2897 uint64_t allocated_memory = AllocateMemory(8,
2898 ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable,
2899 err);
2900
2901 if (err.Success())
2902 m_can_jit = eCanJITYes;
2903 else
2904 m_can_jit = eCanJITNo;
2905
2906 DeallocateMemory (allocated_memory);
2907 }
2908
Sean Callanan90539452011-09-20 23:01:51 +00002909 return m_can_jit == eCanJITYes;
2910}
2911
2912void
2913Process::SetCanJIT (bool can_jit)
2914{
2915 m_can_jit = (can_jit ? eCanJITYes : eCanJITNo);
2916}
2917
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002918Error
2919Process::DeallocateMemory (addr_t ptr)
2920{
Greg Claytond495c532011-05-17 03:37:42 +00002921 Error error;
2922#if defined (USE_ALLOCATE_MEMORY_CACHE)
2923 if (!m_allocated_memory_cache.DeallocateMemory(ptr))
2924 {
Daniel Malead01b2952012-11-29 21:49:15 +00002925 error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr);
Greg Claytond495c532011-05-17 03:37:42 +00002926 }
2927#else
2928 error = DoDeallocateMemory (ptr);
Greg Claytonb2daec92011-01-23 19:58:49 +00002929
Greg Clayton5160ce52013-03-27 23:08:40 +00002930 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002931 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002932 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 +00002933 ptr,
2934 error.AsCString("SUCCESS"),
Jim Ingham4b536182011-08-09 02:12:22 +00002935 m_mod_id.GetStopID(),
2936 m_mod_id.GetMemoryID());
Greg Claytond495c532011-05-17 03:37:42 +00002937#endif
Greg Claytonb2daec92011-01-23 19:58:49 +00002938 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002939}
2940
Han Ming Ongc811d382012-11-17 00:33:14 +00002941
Greg Claytonc9660542012-02-05 02:38:54 +00002942ModuleSP
Greg Claytonc859e2d2012-02-13 23:10:39 +00002943Process::ReadModuleFromMemory (const FileSpec& file_spec,
Andrew MacPherson17220c12014-03-05 10:12:43 +00002944 lldb::addr_t header_addr,
2945 size_t size_to_read)
Greg Claytonc9660542012-02-05 02:38:54 +00002946{
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002947 ModuleSP module_sp (new Module (file_spec, ArchSpec()));
Greg Claytonc9660542012-02-05 02:38:54 +00002948 if (module_sp)
2949 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002950 Error error;
Andrew MacPherson17220c12014-03-05 10:12:43 +00002951 ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error, size_to_read);
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002952 if (objfile)
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002953 return module_sp;
Greg Claytonc9660542012-02-05 02:38:54 +00002954 }
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002955 return ModuleSP();
Greg Claytonc9660542012-02-05 02:38:54 +00002956}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002957
2958Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002959Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002960{
2961 Error error;
2962 error.SetErrorString("watchpoints are not supported");
2963 return error;
2964}
2965
2966Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002967Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002968{
2969 Error error;
2970 error.SetErrorString("watchpoints are not supported");
2971 return error;
2972}
2973
2974StateType
2975Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
2976{
2977 StateType state;
2978 // Now wait for the process to launch and return control to us, and then
2979 // call DidLaunch:
2980 while (1)
2981 {
Greg Clayton6779606a2011-01-22 23:43:18 +00002982 event_sp.reset();
2983 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
2984
Greg Clayton2637f822011-11-17 01:23:07 +00002985 if (StateIsStoppedState(state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002986 break;
Greg Clayton6779606a2011-01-22 23:43:18 +00002987
2988 // If state is invalid, then we timed out
2989 if (state == eStateInvalid)
2990 break;
2991
2992 if (event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002993 HandlePrivateEvent (event_sp);
2994 }
2995 return state;
2996}
2997
2998Error
Greg Claytonfbb76342013-11-20 21:07:01 +00002999Process::Launch (ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003000{
3001 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003002 m_abi_sp.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00003003 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003004 m_jit_loaders_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00003005 m_system_runtime_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003006 m_os_ap.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003007 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003008
Greg Claytonaa149cb2011-08-11 02:48:45 +00003009 Module *exe_module = m_target.GetExecutableModulePointer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003010 if (exe_module)
3011 {
Greg Clayton2289fa42011-04-30 01:09:13 +00003012 char local_exec_file_path[PATH_MAX];
3013 char platform_exec_file_path[PATH_MAX];
3014 exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path));
3015 exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003016 if (exe_module->GetFileSpec().Exists())
3017 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003018 // Install anything that might need to be installed prior to launching.
3019 // For host systems, this will do nothing, but if we are connected to a
3020 // remote platform it will install any needed binaries
3021 error = GetTarget().Install(&launch_info);
3022 if (error.Fail())
3023 return error;
3024
Greg Clayton71337622011-02-24 22:24:29 +00003025 if (PrivateStateThreadIsValid ())
3026 PausePrivateStateThread ();
3027
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003028 error = WillLaunch (exe_module);
3029 if (error.Success())
3030 {
Jim Ingham221d51c2013-05-08 00:35:16 +00003031 const bool restarted = false;
3032 SetPublicState (eStateLaunching, restarted);
Greg Claytone24c4ac2011-11-17 04:46:02 +00003033 m_should_detach = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003034
Ed Maste64fad602013-07-29 20:58:06 +00003035 if (m_public_run_lock.TrySetRunning())
Greg Clayton69fd4be2012-09-04 20:29:05 +00003036 {
3037 // Now launch using these arguments.
3038 error = DoLaunch (exe_module, launch_info);
3039 }
3040 else
3041 {
3042 // This shouldn't happen
3043 error.SetErrorString("failed to acquire process run lock");
3044 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003045
3046 if (error.Fail())
3047 {
3048 if (GetID() != LLDB_INVALID_PROCESS_ID)
3049 {
3050 SetID (LLDB_INVALID_PROCESS_ID);
3051 const char *error_string = error.AsCString();
3052 if (error_string == NULL)
3053 error_string = "launch failed";
3054 SetExitStatus (-1, error_string);
3055 }
3056 }
3057 else
3058 {
3059 EventSP event_sp;
Greg Clayton1a38ea72011-06-22 01:42:17 +00003060 TimeValue timeout_time;
3061 timeout_time = TimeValue::Now();
3062 timeout_time.OffsetWithSeconds(10);
3063 StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003064
Greg Clayton1a38ea72011-06-22 01:42:17 +00003065 if (state == eStateInvalid || event_sp.get() == NULL)
3066 {
3067 // We were able to launch the process, but we failed to
3068 // catch the initial stop.
3069 SetExitStatus (0, "failed to catch stop after launch");
3070 Destroy();
3071 }
3072 else if (state == eStateStopped || state == eStateCrashed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003073 {
Greg Clayton93d3c8332011-02-16 04:46:07 +00003074
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003075 DidLaunch ();
3076
Greg Claytonc859e2d2012-02-13 23:10:39 +00003077 DynamicLoader *dyld = GetDynamicLoader ();
3078 if (dyld)
3079 dyld->DidLaunch();
Greg Clayton93d3c8332011-02-16 04:46:07 +00003080
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00003081 GetJITLoaders().DidLaunch();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003082
Jason Molendaeef51062013-11-05 03:57:19 +00003083 SystemRuntime *system_runtime = GetSystemRuntime ();
3084 if (system_runtime)
3085 system_runtime->DidLaunch();
3086
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003087 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003088 // This delays passing the stopped event to listeners till DidLaunch gets
3089 // a chance to complete...
3090 HandlePrivateEvent (event_sp);
Greg Clayton71337622011-02-24 22:24:29 +00003091
3092 if (PrivateStateThreadIsValid ())
3093 ResumePrivateStateThread ();
3094 else
3095 StartPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003096 }
3097 else if (state == eStateExited)
3098 {
3099 // We exited while trying to launch somehow. Don't call DidLaunch as that's
3100 // not likely to work, and return an invalid pid.
3101 HandlePrivateEvent (event_sp);
3102 }
3103 }
3104 }
3105 }
3106 else
3107 {
Greg Clayton86edbf42011-10-26 00:56:27 +00003108 error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003109 }
3110 }
3111 return error;
3112}
3113
Greg Claytonc3776bf2012-02-09 06:16:32 +00003114
3115Error
3116Process::LoadCore ()
3117{
3118 Error error = DoLoadCore();
3119 if (error.Success())
3120 {
3121 if (PrivateStateThreadIsValid ())
3122 ResumePrivateStateThread ();
3123 else
3124 StartPrivateStateThread ();
3125
Greg Claytonc859e2d2012-02-13 23:10:39 +00003126 DynamicLoader *dyld = GetDynamicLoader ();
3127 if (dyld)
3128 dyld->DidAttach();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003129
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00003130 GetJITLoaders().DidAttach();
Greg Claytonc859e2d2012-02-13 23:10:39 +00003131
Jason Molendaeef51062013-11-05 03:57:19 +00003132 SystemRuntime *system_runtime = GetSystemRuntime ();
3133 if (system_runtime)
3134 system_runtime->DidAttach();
3135
Greg Claytonc859e2d2012-02-13 23:10:39 +00003136 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Claytonc3776bf2012-02-09 06:16:32 +00003137 // We successfully loaded a core file, now pretend we stopped so we can
3138 // show all of the threads in the core file and explore the crashed
3139 // state.
3140 SetPrivateState (eStateStopped);
3141
3142 }
3143 return error;
3144}
3145
Greg Claytonc859e2d2012-02-13 23:10:39 +00003146DynamicLoader *
3147Process::GetDynamicLoader ()
3148{
3149 if (m_dyld_ap.get() == NULL)
3150 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
3151 return m_dyld_ap.get();
3152}
Greg Claytonc3776bf2012-02-09 06:16:32 +00003153
Andrew MacPherson17220c12014-03-05 10:12:43 +00003154JITLoaderList &
3155Process::GetJITLoaders ()
3156{
3157 if (!m_jit_loaders_ap)
3158 {
3159 m_jit_loaders_ap.reset(new JITLoaderList());
3160 JITLoader::LoadPlugins(this, *m_jit_loaders_ap);
3161 }
3162 return *m_jit_loaders_ap;
3163}
3164
Jason Molendaeef51062013-11-05 03:57:19 +00003165SystemRuntime *
3166Process::GetSystemRuntime ()
3167{
3168 if (m_system_runtime_ap.get() == NULL)
3169 m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this));
3170 return m_system_runtime_ap.get();
3171}
3172
Greg Claytonc3776bf2012-02-09 06:16:32 +00003173
Jim Inghambb3a2832011-01-29 01:49:25 +00003174Process::NextEventAction::EventActionResult
3175Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003176{
Jim Inghambb3a2832011-01-29 01:49:25 +00003177 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
3178 switch (state)
Greg Clayton19388cf2010-10-18 01:45:30 +00003179 {
Greg Clayton513c26c2011-01-29 07:10:55 +00003180 case eStateRunning:
Greg Clayton71337622011-02-24 22:24:29 +00003181 case eStateConnected:
Greg Clayton513c26c2011-01-29 07:10:55 +00003182 return eEventActionRetry;
3183
3184 case eStateStopped:
3185 case eStateCrashed:
Greg Claytonc9ed4782011-11-12 02:10:56 +00003186 {
3187 // During attach, prior to sending the eStateStopped event,
Jim Inghamb1e2e842012-04-12 18:49:31 +00003188 // lldb_private::Process subclasses must set the new process ID.
Greg Claytonc9ed4782011-11-12 02:10:56 +00003189 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
Jim Ingham221d51c2013-05-08 00:35:16 +00003190 // We don't want these events to be reported, so go set the ShouldReportStop here:
3191 m_process->GetThreadList().SetShouldReportStop (eVoteNo);
3192
Greg Claytonc9ed4782011-11-12 02:10:56 +00003193 if (m_exec_count > 0)
3194 {
3195 --m_exec_count;
Jim Ingham221d51c2013-05-08 00:35:16 +00003196 RequestResume();
Greg Claytonc9ed4782011-11-12 02:10:56 +00003197 return eEventActionRetry;
3198 }
3199 else
3200 {
3201 m_process->CompleteAttach ();
3202 return eEventActionSuccess;
3203 }
3204 }
Greg Clayton513c26c2011-01-29 07:10:55 +00003205 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003206
Greg Clayton513c26c2011-01-29 07:10:55 +00003207 default:
3208 case eStateExited:
3209 case eStateInvalid:
Greg Clayton513c26c2011-01-29 07:10:55 +00003210 break;
Jim Inghambb3a2832011-01-29 01:49:25 +00003211 }
Greg Claytonc9ed4782011-11-12 02:10:56 +00003212
3213 m_exit_string.assign ("No valid Process");
3214 return eEventActionExit;
Jim Inghambb3a2832011-01-29 01:49:25 +00003215}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003216
Jim Inghambb3a2832011-01-29 01:49:25 +00003217Process::NextEventAction::EventActionResult
3218Process::AttachCompletionHandler::HandleBeingInterrupted()
3219{
3220 return eEventActionSuccess;
3221}
3222
3223const char *
3224Process::AttachCompletionHandler::GetExitString ()
3225{
3226 return m_exit_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003227}
3228
3229Error
Greg Clayton144f3a92011-11-15 03:53:30 +00003230Process::Attach (ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003231{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003232 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003233 m_process_input_reader.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00003234 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003235 m_jit_loaders_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00003236 m_system_runtime_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003237 m_os_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00003238
Greg Clayton144f3a92011-11-15 03:53:30 +00003239 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Claytone996fd32011-03-08 22:40:15 +00003240 Error error;
Greg Clayton144f3a92011-11-15 03:53:30 +00003241 if (attach_pid == LLDB_INVALID_PROCESS_ID)
Jim Ingham5aee1622010-08-09 23:31:02 +00003242 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003243 char process_name[PATH_MAX];
Jim Ingham4299fdb2011-09-15 01:10:17 +00003244
Greg Clayton144f3a92011-11-15 03:53:30 +00003245 if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name)))
Jim Ingham2ecb7422010-08-17 21:54:19 +00003246 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003247 const bool wait_for_launch = attach_info.GetWaitForLaunch();
3248
3249 if (wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003250 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003251 error = WillAttachToProcessWithName(process_name, wait_for_launch);
3252 if (error.Success())
3253 {
Ed Maste64fad602013-07-29 20:58:06 +00003254 if (m_public_run_lock.TrySetRunning())
Greg Clayton926cce72012-10-12 16:10:12 +00003255 {
3256 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003257 const bool restarted = false;
3258 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003259 // Now attach using these arguments.
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +00003260 error = DoAttachToProcessWithName (process_name, attach_info);
Greg Clayton926cce72012-10-12 16:10:12 +00003261 }
3262 else
3263 {
3264 // This shouldn't happen
3265 error.SetErrorString("failed to acquire process run lock");
3266 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00003267
Greg Clayton144f3a92011-11-15 03:53:30 +00003268 if (error.Fail())
3269 {
3270 if (GetID() != LLDB_INVALID_PROCESS_ID)
3271 {
3272 SetID (LLDB_INVALID_PROCESS_ID);
3273 if (error.AsCString() == NULL)
3274 error.SetErrorString("attach failed");
3275
3276 SetExitStatus(-1, error.AsCString());
3277 }
3278 }
3279 else
3280 {
3281 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3282 StartPrivateStateThread();
3283 }
3284 return error;
3285 }
Greg Claytone996fd32011-03-08 22:40:15 +00003286 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003287 else
Greg Claytone996fd32011-03-08 22:40:15 +00003288 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003289 ProcessInstanceInfoList process_infos;
3290 PlatformSP platform_sp (m_target.GetPlatform ());
3291
3292 if (platform_sp)
3293 {
3294 ProcessInstanceInfoMatch match_info;
3295 match_info.GetProcessInfo() = attach_info;
3296 match_info.SetNameMatchType (eNameMatchEquals);
3297 platform_sp->FindProcesses (match_info, process_infos);
3298 const uint32_t num_matches = process_infos.GetSize();
3299 if (num_matches == 1)
3300 {
3301 attach_pid = process_infos.GetProcessIDAtIndex(0);
3302 // Fall through and attach using the above process ID
3303 }
3304 else
3305 {
3306 match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name));
3307 if (num_matches > 1)
3308 error.SetErrorStringWithFormat ("more than one process named %s", process_name);
3309 else
3310 error.SetErrorStringWithFormat ("could not find a process named %s", process_name);
3311 }
3312 }
3313 else
3314 {
3315 error.SetErrorString ("invalid platform, can't find processes by name");
3316 return error;
3317 }
Greg Claytone996fd32011-03-08 22:40:15 +00003318 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003319 }
3320 else
Greg Clayton144f3a92011-11-15 03:53:30 +00003321 {
3322 error.SetErrorString ("invalid process name");
Greg Claytone996fd32011-03-08 22:40:15 +00003323 }
3324 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003325
3326 if (attach_pid != LLDB_INVALID_PROCESS_ID)
Greg Claytone996fd32011-03-08 22:40:15 +00003327 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003328 error = WillAttachToProcessWithID(attach_pid);
Greg Claytone996fd32011-03-08 22:40:15 +00003329 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003330 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003331
Ed Maste64fad602013-07-29 20:58:06 +00003332 if (m_public_run_lock.TrySetRunning())
Greg Clayton926cce72012-10-12 16:10:12 +00003333 {
3334 // Now attach using these arguments.
3335 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003336 const bool restarted = false;
3337 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003338 error = DoAttachToProcessWithID (attach_pid, attach_info);
3339 }
3340 else
3341 {
3342 // This shouldn't happen
3343 error.SetErrorString("failed to acquire process run lock");
3344 }
3345
Greg Clayton144f3a92011-11-15 03:53:30 +00003346 if (error.Success())
3347 {
3348
3349 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3350 StartPrivateStateThread();
3351 }
3352 else
Greg Claytone996fd32011-03-08 22:40:15 +00003353 {
3354 if (GetID() != LLDB_INVALID_PROCESS_ID)
3355 {
3356 SetID (LLDB_INVALID_PROCESS_ID);
3357 const char *error_string = error.AsCString();
3358 if (error_string == NULL)
3359 error_string = "attach failed";
3360
3361 SetExitStatus(-1, error_string);
3362 }
3363 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003364 }
3365 }
3366 return error;
3367}
3368
Greg Clayton93d3c8332011-02-16 04:46:07 +00003369void
3370Process::CompleteAttach ()
3371{
3372 // Let the process subclass figure out at much as it can about the process
3373 // before we go looking for a dynamic loader plug-in.
3374 DidAttach();
3375
Jim Ingham4299fdb2011-09-15 01:10:17 +00003376 // We just attached. If we have a platform, ask it for the process architecture, and if it isn't
3377 // the same as the one we've already set, switch architectures.
3378 PlatformSP platform_sp (m_target.GetPlatform ());
3379 assert (platform_sp.get());
3380 if (platform_sp)
3381 {
Greg Clayton70512312012-05-08 01:45:38 +00003382 const ArchSpec &target_arch = m_target.GetArchitecture();
Greg Clayton1e0c8842013-01-11 20:49:54 +00003383 if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL))
Greg Clayton70512312012-05-08 01:45:38 +00003384 {
3385 ArchSpec platform_arch;
3386 platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch);
3387 if (platform_sp)
3388 {
3389 m_target.SetPlatform (platform_sp);
3390 m_target.SetArchitecture(platform_arch);
3391 }
3392 }
3393 else
3394 {
3395 ProcessInstanceInfo process_info;
3396 platform_sp->GetProcessInfo (GetID(), process_info);
3397 const ArchSpec &process_arch = process_info.GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +00003398 if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch))
Greg Clayton70512312012-05-08 01:45:38 +00003399 m_target.SetArchitecture (process_arch);
3400 }
Jim Ingham4299fdb2011-09-15 01:10:17 +00003401 }
3402
3403 // We have completed the attach, now it is time to find the dynamic loader
Greg Clayton93d3c8332011-02-16 04:46:07 +00003404 // plug-in
Greg Claytonc859e2d2012-02-13 23:10:39 +00003405 DynamicLoader *dyld = GetDynamicLoader ();
3406 if (dyld)
3407 dyld->DidAttach();
Greg Clayton93d3c8332011-02-16 04:46:07 +00003408
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00003409 GetJITLoaders().DidAttach();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003410
Jason Molendaeef51062013-11-05 03:57:19 +00003411 SystemRuntime *system_runtime = GetSystemRuntime ();
3412 if (system_runtime)
3413 system_runtime->DidAttach();
3414
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003415 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Clayton93d3c8332011-02-16 04:46:07 +00003416 // Figure out which one is the executable, and set that in our target:
Enrico Granata17598482012-11-08 02:22:02 +00003417 const ModuleList &target_modules = m_target.GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003418 Mutex::Locker modules_locker(target_modules.GetMutex());
3419 size_t num_modules = target_modules.GetSize();
3420 ModuleSP new_executable_module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003421
Andy Gibbsa297a972013-06-19 19:04:53 +00003422 for (size_t i = 0; i < num_modules; i++)
Greg Clayton93d3c8332011-02-16 04:46:07 +00003423 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003424 ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i));
Greg Clayton8b82f082011-04-12 05:54:46 +00003425 if (module_sp && module_sp->IsExecutable())
Greg Clayton93d3c8332011-02-16 04:46:07 +00003426 {
Greg Claytonaa149cb2011-08-11 02:48:45 +00003427 if (m_target.GetExecutableModulePointer() != module_sp.get())
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003428 new_executable_module_sp = module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003429 break;
3430 }
3431 }
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003432 if (new_executable_module_sp)
3433 m_target.SetExecutableModule (new_executable_module_sp, false);
Greg Clayton93d3c8332011-02-16 04:46:07 +00003434}
3435
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003436Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003437Process::ConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +00003438{
Greg Claytonb766a732011-02-04 01:58:07 +00003439 m_abi_sp.reset();
3440 m_process_input_reader.reset();
3441
3442 // Find the process and its architecture. Make sure it matches the architecture
3443 // of the current Target, and if not adjust it.
3444
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003445 Error error (DoConnectRemote (strm, remote_url));
Greg Claytonb766a732011-02-04 01:58:07 +00003446 if (error.Success())
3447 {
Greg Clayton71337622011-02-24 22:24:29 +00003448 if (GetID() != LLDB_INVALID_PROCESS_ID)
3449 {
Greg Clayton32e0a752011-03-30 18:16:51 +00003450 EventSP event_sp;
3451 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
3452
3453 if (state == eStateStopped || state == eStateCrashed)
3454 {
3455 // If we attached and actually have a process on the other end, then
3456 // this ended up being the equivalent of an attach.
3457 CompleteAttach ();
3458
3459 // This delays passing the stopped event to listeners till
3460 // CompleteAttach gets a chance to complete...
3461 HandlePrivateEvent (event_sp);
3462
3463 }
Greg Clayton71337622011-02-24 22:24:29 +00003464 }
Greg Clayton32e0a752011-03-30 18:16:51 +00003465
3466 if (PrivateStateThreadIsValid ())
3467 ResumePrivateStateThread ();
3468 else
3469 StartPrivateStateThread ();
Greg Claytonb766a732011-02-04 01:58:07 +00003470 }
3471 return error;
3472}
3473
3474
3475Error
Jim Ingham3b8285d2012-04-19 01:40:33 +00003476Process::PrivateResume ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003477{
Greg Clayton5160ce52013-03-27 23:08:40 +00003478 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003479 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003480 log->Printf("Process::PrivateResume() m_stop_id = %u, public state: %s private state: %s",
Jim Ingham4b536182011-08-09 02:12:22 +00003481 m_mod_id.GetStopID(),
Jim Ingham444586b2011-01-24 06:34:17 +00003482 StateAsCString(m_public_state.GetValue()),
3483 StateAsCString(m_private_state.GetValue()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003484
3485 Error error (WillResume());
3486 // Tell the process it is about to resume before the thread list
3487 if (error.Success())
3488 {
Johnny Chenc4221e42010-12-02 20:53:05 +00003489 // Now let the thread list know we are about to resume so it
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003490 // can let all of our threads know that they are about to be
3491 // resumed. Threads will each be called with
3492 // Thread::WillResume(StateType) where StateType contains the state
3493 // that they are supposed to have when the process is resumed
3494 // (suspended/running/stepping). Threads should also check
3495 // their resume signal in lldb::Thread::GetResumeSignal()
Jim Ingham221d51c2013-05-08 00:35:16 +00003496 // to see if they are supposed to start back up with a signal.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003497 if (m_thread_list.WillResume())
3498 {
Jim Ingham372787f2012-04-07 00:00:41 +00003499 // Last thing, do the PreResumeActions.
3500 if (!RunPreResumeActions())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003501 {
Jim Ingham0161b492013-02-09 01:29:05 +00003502 error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming.");
Jim Ingham372787f2012-04-07 00:00:41 +00003503 }
3504 else
3505 {
3506 m_mod_id.BumpResumeID();
3507 error = DoResume();
3508 if (error.Success())
3509 {
3510 DidResume();
3511 m_thread_list.DidResume();
3512 if (log)
3513 log->Printf ("Process thinks the process has resumed.");
3514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003515 }
3516 }
3517 else
3518 {
Jim Ingham513c6bb2012-09-01 01:02:41 +00003519 // Somebody wanted to run without running. So generate a continue & a stopped event,
3520 // and let the world handle them.
3521 if (log)
3522 log->Printf ("Process::PrivateResume() asked to simulate a start & stop.");
3523
3524 SetPrivateState(eStateRunning);
3525 SetPrivateState(eStateStopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003526 }
3527 }
Jim Ingham444586b2011-01-24 06:34:17 +00003528 else if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003529 log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003530 return error;
3531}
3532
3533Error
Greg Claytonf9b57b92013-05-10 23:48:10 +00003534Process::Halt (bool clear_thread_plans)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003535{
Greg Claytonf9b57b92013-05-10 23:48:10 +00003536 // Don't clear the m_clear_thread_plans_on_stop, only set it to true if
3537 // in case it was already set and some thread plan logic calls halt on its
3538 // own.
3539 m_clear_thread_plans_on_stop |= clear_thread_plans;
3540
Jim Inghamaacc3182012-06-06 00:29:30 +00003541 // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since
3542 // we could just straightaway get another event. It just narrows the window...
3543 m_currently_handling_event.WaitForValueEqualTo(false);
3544
3545
Jim Inghambb3a2832011-01-29 01:49:25 +00003546 // Pause our private state thread so we can ensure no one else eats
3547 // the stop event out from under us.
Jim Ingham0f16e732011-02-08 05:20:59 +00003548 Listener halt_listener ("lldb.process.halt_listener");
3549 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton3af9ea52010-11-18 05:57:03 +00003550
Jim Inghambb3a2832011-01-29 01:49:25 +00003551 EventSP event_sp;
Greg Clayton513c26c2011-01-29 07:10:55 +00003552 Error error (WillHalt());
Jim Inghambb3a2832011-01-29 01:49:25 +00003553
Greg Clayton513c26c2011-01-29 07:10:55 +00003554 if (error.Success())
Jim Inghambb3a2832011-01-29 01:49:25 +00003555 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003556
Greg Clayton513c26c2011-01-29 07:10:55 +00003557 bool caused_stop = false;
3558
3559 // Ask the process subclass to actually halt our process
3560 error = DoHalt(caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003561 if (error.Success())
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003562 {
Greg Clayton513c26c2011-01-29 07:10:55 +00003563 if (m_public_state.GetValue() == eStateAttaching)
3564 {
3565 SetExitStatus(SIGKILL, "Cancelled async attach.");
3566 Destroy ();
3567 }
3568 else
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003569 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003570 // If "caused_stop" is true, then DoHalt stopped the process. If
3571 // "caused_stop" is false, the process was already stopped.
3572 // If the DoHalt caused the process to stop, then we want to catch
3573 // this event and set the interrupted bool to true before we pass
3574 // this along so clients know that the process was interrupted by
3575 // a halt command.
3576 if (caused_stop)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003577 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003578 // Wait for 1 second for the process to stop.
Jim Inghambb3a2832011-01-29 01:49:25 +00003579 TimeValue timeout_time;
3580 timeout_time = TimeValue::Now();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003581 timeout_time.OffsetWithSeconds(10);
Jim Ingham0f16e732011-02-08 05:20:59 +00003582 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
3583 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003584
Jim Ingham0f16e732011-02-08 05:20:59 +00003585 if (!got_event || state == eStateInvalid)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003586 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003587 // We timeout out and didn't get a stop event...
Jim Ingham0f16e732011-02-08 05:20:59 +00003588 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton3af9ea52010-11-18 05:57:03 +00003589 }
3590 else
3591 {
Greg Clayton2637f822011-11-17 01:23:07 +00003592 if (StateIsStoppedState (state, false))
Jim Inghambb3a2832011-01-29 01:49:25 +00003593 {
3594 // We caused the process to interrupt itself, so mark this
3595 // as such in the stop event so clients can tell an interrupted
3596 // process from a natural stop
3597 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
3598 }
3599 else
3600 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003601 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghambb3a2832011-01-29 01:49:25 +00003602 if (log)
3603 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
3604 error.SetErrorString ("Did not get stopped event after halt.");
3605 }
Greg Clayton3af9ea52010-11-18 05:57:03 +00003606 }
3607 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003608 DidHalt();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003609 }
3610 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003611 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003612 // Resume our private state thread before we post the event (if any)
Jim Ingham0f16e732011-02-08 05:20:59 +00003613 RestorePrivateProcessEvents();
Jim Inghambb3a2832011-01-29 01:49:25 +00003614
3615 // Post any event we might have consumed. If all goes well, we will have
3616 // stopped the process, intercepted the event and set the interrupted
3617 // bool in the event. Post it to the private event queue and that will end up
3618 // correctly setting the state.
3619 if (event_sp)
3620 m_private_state_broadcaster.BroadcastEvent(event_sp);
3621
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003622 return error;
3623}
3624
3625Error
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003626Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp)
3627{
3628 Error error;
3629 if (m_public_state.GetValue() == eStateRunning)
3630 {
3631 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3632 if (log)
3633 log->Printf("Process::Destroy() About to halt.");
3634 error = Halt();
3635 if (error.Success())
3636 {
3637 // Consume the halt event.
3638 TimeValue timeout (TimeValue::Now());
3639 timeout.OffsetWithSeconds(1);
3640 StateType state = WaitForProcessToStop (&timeout, &exit_event_sp);
3641
3642 // If the process exited while we were waiting for it to stop, put the exited event into
3643 // the shared pointer passed in and return. Our caller doesn't need to do anything else, since
3644 // they don't have a process anymore...
3645
3646 if (state == eStateExited || m_private_state.GetValue() == eStateExited)
3647 {
3648 if (log)
3649 log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt.");
3650 return error;
3651 }
3652 else
3653 exit_event_sp.reset(); // It is ok to consume any non-exit stop events
3654
3655 if (state != eStateStopped)
3656 {
3657 if (log)
3658 log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state));
3659 // If we really couldn't stop the process then we should just error out here, but if the
3660 // lower levels just bobbled sending the event and we really are stopped, then continue on.
3661 StateType private_state = m_private_state.GetValue();
3662 if (private_state != eStateStopped)
3663 {
3664 return error;
3665 }
3666 }
3667 }
3668 else
3669 {
3670 if (log)
3671 log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString());
3672 }
3673 }
3674 return error;
3675}
3676
3677Error
Jim Inghamacff8952013-05-02 00:27:30 +00003678Process::Detach (bool keep_stopped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003679{
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003680 EventSP exit_event_sp;
3681 Error error;
3682 m_destroy_in_process = true;
3683
3684 error = WillDetach();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003685
3686 if (error.Success())
3687 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003688 if (DetachRequiresHalt())
3689 {
3690 error = HaltForDestroyOrDetach (exit_event_sp);
3691 if (!error.Success())
3692 {
3693 m_destroy_in_process = false;
3694 return error;
3695 }
3696 else if (exit_event_sp)
3697 {
3698 // We shouldn't need to do anything else here. There's no process left to detach from...
3699 StopPrivateStateThread();
3700 m_destroy_in_process = false;
3701 return error;
3702 }
3703 }
3704
Andrew MacPhersonc3826b52014-03-25 19:59:36 +00003705 m_thread_list.DiscardThreadPlans();
3706 DisableAllBreakpointSites();
3707
Jim Inghamacff8952013-05-02 00:27:30 +00003708 error = DoDetach(keep_stopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003709 if (error.Success())
3710 {
3711 DidDetach();
3712 StopPrivateStateThread();
3713 }
Jim Inghamacff8952013-05-02 00:27:30 +00003714 else
3715 {
3716 return error;
3717 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003718 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003719 m_destroy_in_process = false;
3720
3721 // If we exited when we were waiting for a process to stop, then
3722 // forward the event here so we don't lose the event
3723 if (exit_event_sp)
3724 {
3725 // Directly broadcast our exited event because we shut down our
3726 // private state thread above
3727 BroadcastEvent(exit_event_sp);
3728 }
3729
3730 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3731 // the last events through the event system, in which case we might strand the write lock. Unlock
3732 // it here so when we do to tear down the process we don't get an error destroying the lock.
3733
Ed Maste64fad602013-07-29 20:58:06 +00003734 m_public_run_lock.SetStopped();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003735 return error;
3736}
3737
3738Error
3739Process::Destroy ()
3740{
Jim Ingham09437922013-03-01 20:04:25 +00003741
3742 // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work
3743 // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt
3744 // failed and the process stays around for some reason it won't be in a confused state.
3745
3746 m_destroy_in_process = true;
3747
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003748 Error error (WillDestroy());
3749 if (error.Success())
3750 {
Greg Clayton85fb1b92012-09-11 02:33:37 +00003751 EventSP exit_event_sp;
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003752 if (DestroyRequiresHalt())
Jim Ingham04e0a222012-05-23 15:46:31 +00003753 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003754 error = HaltForDestroyOrDetach(exit_event_sp);
Jim Ingham04e0a222012-05-23 15:46:31 +00003755 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003756
Jim Inghamaacc3182012-06-06 00:29:30 +00003757 if (m_public_state.GetValue() != eStateRunning)
3758 {
3759 // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to
3760 // kill it, we don't want it hitting a breakpoint...
3761 // Only do this if we've stopped, however, since if we didn't manage to halt it above, then
3762 // we're not going to have much luck doing this now.
3763 m_thread_list.DiscardThreadPlans();
3764 DisableAllBreakpointSites();
3765 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003766
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003767 error = DoDestroy();
3768 if (error.Success())
3769 {
3770 DidDestroy();
3771 StopPrivateStateThread();
3772 }
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003773 m_stdio_communication.StopReadThread();
3774 m_stdio_communication.Disconnect();
Greg Claytonb4874f12014-02-28 18:22:24 +00003775
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003776 if (m_process_input_reader)
Greg Claytonb4874f12014-02-28 18:22:24 +00003777 {
3778 m_process_input_reader->SetIsDone(true);
3779 m_process_input_reader->Cancel();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003780 m_process_input_reader.reset();
Greg Claytonb4874f12014-02-28 18:22:24 +00003781 }
3782
Greg Clayton85fb1b92012-09-11 02:33:37 +00003783 // If we exited when we were waiting for a process to stop, then
3784 // forward the event here so we don't lose the event
3785 if (exit_event_sp)
3786 {
3787 // Directly broadcast our exited event because we shut down our
3788 // private state thread above
3789 BroadcastEvent(exit_event_sp);
3790 }
3791
Jim Inghamb1e2e842012-04-12 18:49:31 +00003792 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3793 // the last events through the event system, in which case we might strand the write lock. Unlock
3794 // it here so when we do to tear down the process we don't get an error destroying the lock.
Ed Maste64fad602013-07-29 20:58:06 +00003795 m_public_run_lock.SetStopped();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003796 }
Jim Ingham09437922013-03-01 20:04:25 +00003797
3798 m_destroy_in_process = false;
3799
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003800 return error;
3801}
3802
3803Error
3804Process::Signal (int signal)
3805{
3806 Error error (WillSignal());
3807 if (error.Success())
3808 {
3809 error = DoSignal(signal);
3810 if (error.Success())
3811 DidSignal();
3812 }
3813 return error;
3814}
3815
Greg Clayton514487e2011-02-15 21:59:32 +00003816lldb::ByteOrder
3817Process::GetByteOrder () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003818{
Greg Clayton514487e2011-02-15 21:59:32 +00003819 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003820}
3821
3822uint32_t
Greg Clayton514487e2011-02-15 21:59:32 +00003823Process::GetAddressByteSize () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003824{
Greg Clayton514487e2011-02-15 21:59:32 +00003825 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003826}
3827
Greg Clayton514487e2011-02-15 21:59:32 +00003828
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003829bool
3830Process::ShouldBroadcastEvent (Event *event_ptr)
3831{
3832 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
3833 bool return_value = true;
Greg Clayton5160ce52013-03-27 23:08:40 +00003834 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS));
Jim Ingham0161b492013-02-09 01:29:05 +00003835
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003836 switch (state)
3837 {
Greg Claytonb766a732011-02-04 01:58:07 +00003838 case eStateConnected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003839 case eStateAttaching:
3840 case eStateLaunching:
3841 case eStateDetached:
3842 case eStateExited:
3843 case eStateUnloaded:
3844 // These events indicate changes in the state of the debugging session, always report them.
3845 return_value = true;
3846 break;
3847 case eStateInvalid:
3848 // We stopped for no apparent reason, don't report it.
3849 return_value = false;
3850 break;
3851 case eStateRunning:
3852 case eStateStepping:
3853 // If we've started the target running, we handle the cases where we
3854 // are already running and where there is a transition from stopped to
3855 // running differently.
3856 // running -> running: Automatically suppress extra running events
3857 // stopped -> running: Report except when there is one or more no votes
3858 // and no yes votes.
3859 SynchronouslyNotifyStateChanged (state);
Jim Ingham1460e4b2014-01-10 23:46:59 +00003860 if (m_force_next_event_delivery)
3861 return_value = true;
3862 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003863 {
Jim Ingham1460e4b2014-01-10 23:46:59 +00003864 switch (m_last_broadcast_state)
3865 {
3866 case eStateRunning:
3867 case eStateStepping:
3868 // We always suppress multiple runnings with no PUBLIC stop in between.
3869 return_value = false;
3870 break;
3871 default:
3872 // TODO: make this work correctly. For now always report
3873 // run if we aren't running so we don't miss any runnning
3874 // events. If I run the lldb/test/thread/a.out file and
3875 // break at main.cpp:58, run and hit the breakpoints on
3876 // multiple threads, then somehow during the stepping over
3877 // of all breakpoints no run gets reported.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003878
Jim Ingham1460e4b2014-01-10 23:46:59 +00003879 // This is a transition from stop to run.
3880 switch (m_thread_list.ShouldReportRun (event_ptr))
3881 {
3882 case eVoteYes:
3883 case eVoteNoOpinion:
3884 return_value = true;
3885 break;
3886 case eVoteNo:
3887 return_value = false;
3888 break;
3889 }
3890 break;
3891 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892 }
3893 break;
3894 case eStateStopped:
3895 case eStateCrashed:
3896 case eStateSuspended:
3897 {
3898 // We've stopped. First see if we're going to restart the target.
3899 // If we are going to stop, then we always broadcast the event.
3900 // 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 +00003901 // If no thread has an opinion, we don't report it.
Jim Ingham221d51c2013-05-08 00:35:16 +00003902
Jim Inghamcb4ca112012-05-16 01:32:14 +00003903 RefreshStateAfterStop ();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003904 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003905 {
Greg Clayton3af9ea52010-11-18 05:57:03 +00003906 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003907 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003908 static_cast<void*>(event_ptr),
Jim Ingham0161b492013-02-09 01:29:05 +00003909 StateAsCString(state));
Jim Ingham35878c42014-04-08 21:33:21 +00003910 // Even though we know we are going to stop, we should let the threads have a look at the stop,
3911 // so they can properly set their state.
3912 m_thread_list.ShouldStop (event_ptr);
Jim Ingham0161b492013-02-09 01:29:05 +00003913 return_value = true;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003914 }
3915 else
3916 {
Jim Ingham221d51c2013-05-08 00:35:16 +00003917 bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr);
3918 bool should_resume = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003919
Jim Ingham0161b492013-02-09 01:29:05 +00003920 // It makes no sense to ask "ShouldStop" if we've already been restarted...
3921 // Asking the thread list is also not likely to go well, since we are running again.
3922 // So in that case just report the event.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003923
Jim Ingham0161b492013-02-09 01:29:05 +00003924 if (!was_restarted)
3925 should_resume = m_thread_list.ShouldStop (event_ptr) == false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003926
Jim Ingham221d51c2013-05-08 00:35:16 +00003927 if (was_restarted || should_resume || m_resume_requested)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003928 {
Jim Ingham0161b492013-02-09 01:29:05 +00003929 Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr);
3930 if (log)
3931 log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003932 should_resume, StateAsCString(state),
3933 was_restarted, stop_vote);
3934
Jim Ingham0161b492013-02-09 01:29:05 +00003935 switch (stop_vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003936 {
3937 case eVoteYes:
Jim Ingham0161b492013-02-09 01:29:05 +00003938 return_value = true;
3939 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003940 case eVoteNoOpinion:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003941 case eVoteNo:
3942 return_value = false;
3943 break;
3944 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003945
Jim Inghamcb95f342012-09-05 21:13:56 +00003946 if (!was_restarted)
Jim Ingham0161b492013-02-09 01:29:05 +00003947 {
3948 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003949 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s",
3950 static_cast<void*>(event_ptr),
3951 StateAsCString(state));
Jim Ingham0161b492013-02-09 01:29:05 +00003952 ProcessEventData::SetRestartedInEvent(event_ptr, true);
Jim Inghamcb95f342012-09-05 21:13:56 +00003953 PrivateResume ();
Jim Ingham0161b492013-02-09 01:29:05 +00003954 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003955
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003956 }
3957 else
3958 {
3959 return_value = true;
3960 SynchronouslyNotifyStateChanged (state);
3961 }
3962 }
3963 }
Jim Ingham0161b492013-02-09 01:29:05 +00003964 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003965 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003966
Jim Ingham1460e4b2014-01-10 23:46:59 +00003967 // Forcing the next event delivery is a one shot deal. So reset it here.
3968 m_force_next_event_delivery = false;
3969
Jim Ingham0161b492013-02-09 01:29:05 +00003970 // We do some coalescing of events (for instance two consecutive running events get coalesced.)
3971 // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state
3972 // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done,
3973 // because the PublicState reflects the last event pulled off the queue, and there may be several
3974 // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event
3975 // yet. m_last_broadcast_state gets updated here.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003976
Jim Ingham0161b492013-02-09 01:29:05 +00003977 if (return_value)
3978 m_last_broadcast_state = state;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003979
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003980 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003981 log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003982 static_cast<void*>(event_ptr), StateAsCString(state),
Jim Ingham0161b492013-02-09 01:29:05 +00003983 StateAsCString(m_last_broadcast_state),
3984 return_value ? "YES" : "NO");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003985 return return_value;
3986}
3987
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003988
3989bool
Jim Ingham372787f2012-04-07 00:00:41 +00003990Process::StartPrivateStateThread (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003991{
Greg Clayton5160ce52013-03-27 23:08:40 +00003992 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003993
Greg Clayton8b82f082011-04-12 05:54:46 +00003994 bool already_running = PrivateStateThreadIsValid ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003995 if (log)
Greg Clayton8b82f082011-04-12 05:54:46 +00003996 log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread");
3997
Jim Ingham372787f2012-04-07 00:00:41 +00003998 if (!force && already_running)
Greg Clayton8b82f082011-04-12 05:54:46 +00003999 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004000
4001 // Create a thread that watches our internal state and controls which
4002 // events make it to clients (into the DCProcess event queue).
Greg Clayton3e06bd92011-01-09 21:07:35 +00004003 char thread_name[1024];
Jim Ingham372787f2012-04-07 00:00:41 +00004004 if (already_running)
Daniel Malead01b2952012-11-29 21:49:15 +00004005 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
Jim Ingham372787f2012-04-07 00:00:41 +00004006 else
Daniel Malead01b2952012-11-29 21:49:15 +00004007 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
Jim Ingham076b3042012-04-10 01:21:57 +00004008
4009 // Create the private state thread, and start it running.
Greg Clayton3e06bd92011-01-09 21:07:35 +00004010 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Jim Ingham076b3042012-04-10 01:21:57 +00004011 bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
4012 if (success)
4013 {
4014 ResumePrivateStateThread();
4015 return true;
4016 }
4017 else
4018 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004019}
4020
4021void
4022Process::PausePrivateStateThread ()
4023{
4024 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
4025}
4026
4027void
4028Process::ResumePrivateStateThread ()
4029{
4030 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
4031}
4032
4033void
4034Process::StopPrivateStateThread ()
4035{
Greg Clayton8b82f082011-04-12 05:54:46 +00004036 if (PrivateStateThreadIsValid ())
4037 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
Jim Inghamb1e2e842012-04-12 18:49:31 +00004038 else
4039 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004040 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Jim Inghamb1e2e842012-04-12 18:49:31 +00004041 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00004042 log->Printf ("Went to stop the private state thread, but it was already invalid.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00004043 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004044}
4045
4046void
4047Process::ControlPrivateStateThread (uint32_t signal)
4048{
Greg Clayton5160ce52013-03-27 23:08:40 +00004049 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004050
4051 assert (signal == eBroadcastInternalStateControlStop ||
4052 signal == eBroadcastInternalStateControlPause ||
4053 signal == eBroadcastInternalStateControlResume);
4054
4055 if (log)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00004056 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004057
Greg Clayton7ecb3a02011-01-22 17:43:17 +00004058 // Signal the private state thread. First we should copy this is case the
4059 // thread starts exiting since the private state thread will NULL this out
4060 // when it exits
4061 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton2da6d492011-02-08 01:34:25 +00004062 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004063 {
4064 TimeValue timeout_time;
4065 bool timed_out;
4066
4067 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
4068
4069 timeout_time = TimeValue::Now();
4070 timeout_time.OffsetWithSeconds(2);
Jim Inghamb1e2e842012-04-12 18:49:31 +00004071 if (log)
4072 log->Printf ("Sending control event of type: %d.", signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004073 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
4074 m_private_state_control_wait.SetValue (false, eBroadcastNever);
4075
4076 if (signal == eBroadcastInternalStateControlStop)
4077 {
4078 if (timed_out)
Jim Inghamb1e2e842012-04-12 18:49:31 +00004079 {
4080 Error error;
4081 Host::ThreadCancel (private_state_thread, &error);
4082 if (log)
4083 log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString());
4084 }
4085 else
4086 {
4087 if (log)
4088 log->Printf ("The control event killed the private state thread without having to cancel.");
4089 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004090
4091 thread_result_t result = NULL;
Greg Clayton7ecb3a02011-01-22 17:43:17 +00004092 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Clayton49182ed2010-07-22 18:34:21 +00004093 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004094 }
4095 }
Jim Inghamb1e2e842012-04-12 18:49:31 +00004096 else
4097 {
4098 if (log)
4099 log->Printf ("Private state thread already dead, no need to signal it to stop.");
4100 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004101}
4102
4103void
Jim Inghamcfc09352012-07-27 23:57:19 +00004104Process::SendAsyncInterrupt ()
4105{
4106 if (PrivateStateThreadIsValid())
4107 m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
4108 else
4109 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
4110}
4111
4112void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004113Process::HandlePrivateEvent (EventSP &event_sp)
4114{
Greg Clayton5160ce52013-03-27 23:08:40 +00004115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham221d51c2013-05-08 00:35:16 +00004116 m_resume_requested = false;
4117
Jim Inghamaacc3182012-06-06 00:29:30 +00004118 m_currently_handling_event.SetValue(true, eBroadcastNever);
Jim Inghambb3a2832011-01-29 01:49:25 +00004119
Greg Clayton414f5d32011-01-25 02:58:48 +00004120 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00004121
4122 // First check to see if anybody wants a shot at this event:
Jim Ingham754ab982011-01-29 04:05:41 +00004123 if (m_next_event_action_ap.get() != NULL)
Jim Inghambb3a2832011-01-29 01:49:25 +00004124 {
Jim Ingham754ab982011-01-29 04:05:41 +00004125 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Ingham0161b492013-02-09 01:29:05 +00004126 if (log)
4127 log->Printf ("Ran next event action, result was %d.", action_result);
4128
Jim Inghambb3a2832011-01-29 01:49:25 +00004129 switch (action_result)
4130 {
4131 case NextEventAction::eEventActionSuccess:
4132 SetNextEventAction(NULL);
4133 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00004134
Jim Inghambb3a2832011-01-29 01:49:25 +00004135 case NextEventAction::eEventActionRetry:
4136 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00004137
Jim Inghambb3a2832011-01-29 01:49:25 +00004138 case NextEventAction::eEventActionExit:
Jim Ingham2a5fdd42011-01-29 01:57:31 +00004139 // Handle Exiting Here. If we already got an exited event,
4140 // we should just propagate it. Otherwise, swallow this event,
4141 // and set our state to exit so the next event will kill us.
4142 if (new_state != eStateExited)
4143 {
4144 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham754ab982011-01-29 04:05:41 +00004145 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham221d51c2013-05-08 00:35:16 +00004146 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Jim Ingham2a5fdd42011-01-29 01:57:31 +00004147 SetNextEventAction(NULL);
4148 return;
4149 }
4150 SetNextEventAction(NULL);
Jim Inghambb3a2832011-01-29 01:49:25 +00004151 break;
4152 }
4153 }
4154
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004155 // See if we should broadcast this state to external clients?
4156 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004157
4158 if (should_broadcast)
4159 {
Greg Claytonb4874f12014-02-28 18:22:24 +00004160 const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004161 if (log)
4162 {
Daniel Malead01b2952012-11-29 21:49:15 +00004163 log->Printf ("Process::%s (pid = %" PRIu64 ") broadcasting new state %s (old state %s) to %s",
Greg Clayton414f5d32011-01-25 02:58:48 +00004164 __FUNCTION__,
4165 GetID(),
4166 StateAsCString(new_state),
4167 StateAsCString (GetState ()),
Greg Claytonb4874f12014-02-28 18:22:24 +00004168 is_hijacked ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004169 }
Jim Ingham9575d842011-03-11 03:53:59 +00004170 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton414f5d32011-01-25 02:58:48 +00004171 if (StateIsRunningState (new_state))
Greg Clayton44d93782014-01-27 23:43:24 +00004172 {
4173 // Only push the input handler if we aren't fowarding events,
4174 // as this means the curses GUI is in use...
4175 if (!GetTarget().GetDebugger().IsForwardingEvents())
4176 PushProcessIOHandler ();
4177 }
Greg Claytonb4874f12014-02-28 18:22:24 +00004178 else if (StateIsStoppedState(new_state, false))
4179 {
4180 if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
4181 {
4182 // If the lldb_private::Debugger is handling the events, we don't
4183 // want to pop the process IOHandler here, we want to do it when
4184 // we receive the stopped event so we can carefully control when
4185 // the process IOHandler is popped because when we stop we want to
4186 // display some text stating how and why we stopped, then maybe some
4187 // process/thread/frame info, and then we want the "(lldb) " prompt
4188 // to show up. If we pop the process IOHandler here, then we will
4189 // cause the command interpreter to become the top IOHandler after
4190 // the process pops off and it will update its prompt right away...
4191 // See the Debugger.cpp file where it calls the function as
4192 // "process_sp->PopProcessIOHandler()" to see where I am talking about.
4193 // Otherwise we end up getting overlapping "(lldb) " prompts and
4194 // garbled output.
4195 //
4196 // If we aren't handling the events in the debugger (which is indicated
4197 // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or we
4198 // are hijacked, then we always pop the process IO handler manually.
4199 // Hijacking happens when the internal process state thread is running
4200 // thread plans, or when commands want to run in synchronous mode
4201 // and they call "process->WaitForProcessToStop()". An example of something
4202 // that will hijack the events is a simple expression:
4203 //
4204 // (lldb) expr (int)puts("hello")
4205 //
4206 // This will cause the internal process state thread to resume and halt
4207 // the process (and _it_ will hijack the eBroadcastBitStateChanged
4208 // events) and we do need the IO handler to be pushed and popped
4209 // correctly.
4210
4211 if (is_hijacked || m_target.GetDebugger().IsHandlingEvents() == false)
4212 PopProcessIOHandler ();
4213 }
4214 }
Jim Ingham9575d842011-03-11 03:53:59 +00004215
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004216 BroadcastEvent (event_sp);
4217 }
4218 else
4219 {
4220 if (log)
4221 {
Daniel Malead01b2952012-11-29 21:49:15 +00004222 log->Printf ("Process::%s (pid = %" PRIu64 ") suppressing state %s (old state %s): should_broadcast == false",
Greg Clayton414f5d32011-01-25 02:58:48 +00004223 __FUNCTION__,
4224 GetID(),
4225 StateAsCString(new_state),
Jason Molendafd54b362011-09-20 21:44:10 +00004226 StateAsCString (GetState ()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004227 }
4228 }
Jim Inghamaacc3182012-06-06 00:29:30 +00004229 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004230}
4231
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004232thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004233Process::PrivateStateThread (void *arg)
4234{
4235 Process *proc = static_cast<Process*> (arg);
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004236 thread_result_t result = proc->RunPrivateStateThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004237 return result;
4238}
4239
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004240thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004241Process::RunPrivateStateThread ()
4242{
Jim Ingham076b3042012-04-10 01:21:57 +00004243 bool control_only = true;
Jim Inghamb1e2e842012-04-12 18:49:31 +00004244 m_private_state_control_wait.SetValue (false, eBroadcastNever);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004245
Greg Clayton5160ce52013-03-27 23:08:40 +00004246 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004247 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004248 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...",
4249 __FUNCTION__, static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004250
4251 bool exit_now = false;
4252 while (!exit_now)
4253 {
4254 EventSP event_sp;
4255 WaitForEventsPrivate (NULL, event_sp, control_only);
4256 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
4257 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00004258 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004259 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d",
4260 __FUNCTION__, static_cast<void*>(this), GetID(),
4261 event_sp->GetType());
Jim Inghamb1e2e842012-04-12 18:49:31 +00004262
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004263 switch (event_sp->GetType())
4264 {
4265 case eBroadcastInternalStateControlStop:
4266 exit_now = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004267 break; // doing any internal state managment below
4268
4269 case eBroadcastInternalStateControlPause:
4270 control_only = true;
4271 break;
4272
4273 case eBroadcastInternalStateControlResume:
4274 control_only = false;
4275 break;
4276 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004277
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004278 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004279 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004280 }
Jim Inghamcfc09352012-07-27 23:57:19 +00004281 else if (event_sp->GetType() == eBroadcastBitInterrupt)
4282 {
4283 if (m_public_state.GetValue() == eStateAttaching)
4284 {
4285 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004286 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.",
4287 __FUNCTION__, static_cast<void*>(this),
4288 GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004289 BroadcastEvent (eBroadcastBitInterrupt, NULL);
4290 }
4291 else
4292 {
4293 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004294 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.",
4295 __FUNCTION__, static_cast<void*>(this),
4296 GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004297 Halt();
4298 }
4299 continue;
4300 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004301
4302 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
4303
4304 if (internal_state != eStateInvalid)
4305 {
Greg Claytonf9b57b92013-05-10 23:48:10 +00004306 if (m_clear_thread_plans_on_stop &&
4307 StateIsStoppedState(internal_state, true))
4308 {
4309 m_clear_thread_plans_on_stop = false;
4310 m_thread_list.DiscardThreadPlans();
4311 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004312 HandlePrivateEvent (event_sp);
4313 }
4314
Greg Clayton58d1c9a2010-10-18 04:14:23 +00004315 if (internal_state == eStateInvalid ||
4316 internal_state == eStateExited ||
4317 internal_state == eStateDetached )
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004318 {
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004319 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004320 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...",
4321 __FUNCTION__, static_cast<void*>(this), GetID(),
4322 StateAsCString(internal_state));
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004323
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004324 break;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004325 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004326 }
4327
Caroline Tice20ad3c42010-10-29 21:48:37 +00004328 // Verify log is still enabled before attempting to write to it...
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004329 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004330 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...",
4331 __FUNCTION__, static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004332
Ed Maste64fad602013-07-29 20:58:06 +00004333 m_public_run_lock.SetStopped();
Greg Clayton6ed95942011-01-22 07:12:45 +00004334 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
4335 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004336 return NULL;
4337}
4338
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004339//------------------------------------------------------------------
4340// Process Event Data
4341//------------------------------------------------------------------
4342
4343Process::ProcessEventData::ProcessEventData () :
4344 EventData (),
4345 m_process_sp (),
4346 m_state (eStateInvalid),
Greg Claytonc982c762010-07-09 20:39:50 +00004347 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004348 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004349 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004350{
4351}
4352
4353Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
4354 EventData (),
4355 m_process_sp (process_sp),
4356 m_state (state),
Greg Claytonc982c762010-07-09 20:39:50 +00004357 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004358 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004359 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004360{
4361}
4362
4363Process::ProcessEventData::~ProcessEventData()
4364{
4365}
4366
4367const ConstString &
4368Process::ProcessEventData::GetFlavorString ()
4369{
4370 static ConstString g_flavor ("Process::ProcessEventData");
4371 return g_flavor;
4372}
4373
4374const ConstString &
4375Process::ProcessEventData::GetFlavor () const
4376{
4377 return ProcessEventData::GetFlavorString ();
4378}
4379
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004380void
4381Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
4382{
4383 // This function gets called twice for each event, once when the event gets pulled
Jim Inghama8604692011-05-22 21:45:01 +00004384 // off of the private process event queue, and then any number of times, first when it gets pulled off of
4385 // the public event queue, then other times when we're pretending that this is where we stopped at the
4386 // end of expression evaluation. m_update_state is used to distinguish these
4387 // three cases; it is 0 when we're just pulling it off for private handling,
Jim Ingham221d51c2013-05-08 00:35:16 +00004388 // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then.
Jim Inghama8604692011-05-22 21:45:01 +00004389 if (m_update_state != 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004390 return;
Jim Ingham0161b492013-02-09 01:29:05 +00004391
Jim Ingham221d51c2013-05-08 00:35:16 +00004392 m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr));
Jim Ingham35878c42014-04-08 21:33:21 +00004393
4394 // If this is a halt event, even if the halt stopped with some reason other than a plain interrupt (e.g. we had
4395 // already stopped for a breakpoint when the halt request came through) don't do the StopInfo actions, as they may
4396 // end up restarting the process.
4397 if (m_interrupted)
4398 return;
4399
4400 // If we're stopped and haven't restarted, then do the StopInfo actions here:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004401 if (m_state == eStateStopped && ! m_restarted)
Jim Ingham0faa43f2011-11-08 03:00:11 +00004402 {
4403 ThreadList &curr_thread_list = m_process_sp->GetThreadList();
Greg Clayton61e7a582011-12-01 23:28:38 +00004404 uint32_t num_threads = curr_thread_list.GetSize();
4405 uint32_t idx;
Greg Claytonf4b47e12010-08-04 01:40:35 +00004406
Jim Ingham4b536182011-08-09 02:12:22 +00004407 // The actions might change one of the thread's stop_info's opinions about whether we should
4408 // stop the process, so we need to query that as we go.
Jim Ingham0faa43f2011-11-08 03:00:11 +00004409
4410 // One other complication here, is that we try to catch any case where the target has run (except for expressions)
4411 // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and
4412 // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like
4413 // 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
4414 // against this list & bag out if anything differs.
Greg Clayton61e7a582011-12-01 23:28:38 +00004415 std::vector<uint32_t> thread_index_array(num_threads);
Jim Ingham0faa43f2011-11-08 03:00:11 +00004416 for (idx = 0; idx < num_threads; ++idx)
4417 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID();
4418
Jim Inghamc7078c22012-12-13 22:24:15 +00004419 // Use this to track whether we should continue from here. We will only continue the target running if
4420 // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running,
4421 // then it doesn't matter what the other threads say...
4422
4423 bool still_should_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +00004424
Jim Ingham0ad7e052013-04-25 02:04:59 +00004425 // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a
4426 // valid stop reason. In that case we should just stop, because we have no way of telling what the right
4427 // thing to do is, and it's better to let the user decide than continue behind their backs.
4428
4429 bool does_anybody_have_an_opinion = false;
4430
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004431 for (idx = 0; idx < num_threads; ++idx)
4432 {
Jim Ingham0faa43f2011-11-08 03:00:11 +00004433 curr_thread_list = m_process_sp->GetThreadList();
4434 if (curr_thread_list.GetSize() != num_threads)
4435 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004436 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004437 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004438 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 +00004439 break;
4440 }
4441
4442 lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx);
4443
4444 if (thread_sp->GetIndexID() != thread_index_array[idx])
4445 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004446 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004447 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004448 log->Printf("The thread at position %u changed from %u to %u while processing event.",
Jim Ingham87c665f2011-12-01 20:26:15 +00004449 idx,
4450 thread_index_array[idx],
4451 thread_sp->GetIndexID());
Jim Ingham0faa43f2011-11-08 03:00:11 +00004452 break;
4453 }
4454
Jim Inghamb15bfc72010-10-20 00:39:53 +00004455 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00004456 if (stop_info_sp && stop_info_sp->IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004457 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004458 does_anybody_have_an_opinion = true;
Jim Ingham0161b492013-02-09 01:29:05 +00004459 bool this_thread_wants_to_stop;
4460 if (stop_info_sp->GetOverrideShouldStop())
Jim Ingham4b536182011-08-09 02:12:22 +00004461 {
Jim Ingham0161b492013-02-09 01:29:05 +00004462 this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue();
4463 }
4464 else
4465 {
4466 stop_info_sp->PerformAction(event_ptr);
4467 // The stop action might restart the target. If it does, then we want to mark that in the
4468 // event so that whoever is receiving it will know to wait for the running event and reflect
4469 // that state appropriately.
4470 // We also need to stop processing actions, since they aren't expecting the target to be running.
4471
4472 // FIXME: we might have run.
4473 if (stop_info_sp->HasTargetRunSinceMe())
4474 {
4475 SetRestarted (true);
4476 break;
4477 }
4478
4479 this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr);
Jim Ingham4b536182011-08-09 02:12:22 +00004480 }
Jim Inghamc7078c22012-12-13 22:24:15 +00004481
Jim Inghamc7078c22012-12-13 22:24:15 +00004482 if (still_should_stop == false)
4483 still_should_stop = this_thread_wants_to_stop;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004484 }
4485 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00004486
Ashok Thirumurthicf7c55e2013-04-18 14:38:20 +00004487
Jim Inghama8ca6e22013-05-03 23:04:37 +00004488 if (!GetRestarted())
Jim Ingham9575d842011-03-11 03:53:59 +00004489 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004490 if (!still_should_stop && does_anybody_have_an_opinion)
Jim Ingham4b536182011-08-09 02:12:22 +00004491 {
4492 // We've been asked to continue, so do that here.
Jim Ingham9575d842011-03-11 03:53:59 +00004493 SetRestarted(true);
Jim Ingham3b8285d2012-04-19 01:40:33 +00004494 // Use the public resume method here, since this is just
4495 // extending a public resume.
Jim Ingham0161b492013-02-09 01:29:05 +00004496 m_process_sp->PrivateResume();
Jim Ingham4b536182011-08-09 02:12:22 +00004497 }
4498 else
4499 {
4500 // If we didn't restart, run the Stop Hooks here:
4501 // They might also restart the target, so watch for that.
4502 m_process_sp->GetTarget().RunStopHooks();
4503 if (m_process_sp->GetPrivateState() == eStateRunning)
4504 SetRestarted(true);
4505 }
Jim Ingham9575d842011-03-11 03:53:59 +00004506 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004507 }
4508}
4509
4510void
4511Process::ProcessEventData::Dump (Stream *s) const
4512{
4513 if (m_process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004514 s->Printf(" process = %p (pid = %" PRIu64 "), ",
4515 static_cast<void*>(m_process_sp.get()), m_process_sp->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004516
Greg Clayton8b82f082011-04-12 05:54:46 +00004517 s->Printf("state = %s", StateAsCString(GetState()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004518}
4519
4520const Process::ProcessEventData *
4521Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
4522{
4523 if (event_ptr)
4524 {
4525 const EventData *event_data = event_ptr->GetData();
4526 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
4527 return static_cast <const ProcessEventData *> (event_ptr->GetData());
4528 }
4529 return NULL;
4530}
4531
4532ProcessSP
4533Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
4534{
4535 ProcessSP process_sp;
4536 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4537 if (data)
4538 process_sp = data->GetProcessSP();
4539 return process_sp;
4540}
4541
4542StateType
4543Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
4544{
4545 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4546 if (data == NULL)
4547 return eStateInvalid;
4548 else
4549 return data->GetState();
4550}
4551
4552bool
4553Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
4554{
4555 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4556 if (data == NULL)
4557 return false;
4558 else
4559 return data->GetRestarted();
4560}
4561
4562void
4563Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
4564{
4565 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4566 if (data != NULL)
4567 data->SetRestarted(new_value);
4568}
4569
Jim Ingham0161b492013-02-09 01:29:05 +00004570size_t
4571Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr)
4572{
4573 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4574 if (data != NULL)
4575 return data->GetNumRestartedReasons();
4576 else
4577 return 0;
4578}
4579
4580const char *
4581Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx)
4582{
4583 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4584 if (data != NULL)
4585 return data->GetRestartedReasonAtIndex(idx);
4586 else
4587 return NULL;
4588}
4589
4590void
4591Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason)
4592{
4593 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4594 if (data != NULL)
4595 data->AddRestartedReason(reason);
4596}
4597
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004598bool
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004599Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
4600{
4601 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4602 if (data == NULL)
4603 return false;
4604 else
4605 return data->GetInterrupted ();
4606}
4607
4608void
4609Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
4610{
4611 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4612 if (data != NULL)
4613 data->SetInterrupted(new_value);
4614}
4615
4616bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004617Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
4618{
4619 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4620 if (data)
4621 {
4622 data->SetUpdateStateOnRemoval();
4623 return true;
4624 }
4625 return false;
4626}
4627
Greg Claytond9e416c2012-02-18 05:35:26 +00004628lldb::TargetSP
4629Process::CalculateTarget ()
4630{
4631 return m_target.shared_from_this();
4632}
4633
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004634void
Greg Clayton0603aa92010-10-04 01:05:56 +00004635Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004636{
Greg Claytonc14ee322011-09-22 04:58:26 +00004637 exe_ctx.SetTargetPtr (&m_target);
4638 exe_ctx.SetProcessPtr (this);
4639 exe_ctx.SetThreadPtr(NULL);
4640 exe_ctx.SetFramePtr (NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004641}
4642
Greg Claytone996fd32011-03-08 22:40:15 +00004643//uint32_t
4644//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
4645//{
4646// return 0;
4647//}
4648//
4649//ArchSpec
4650//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
4651//{
4652// return Host::GetArchSpecForExistingProcess (pid);
4653//}
4654//
4655//ArchSpec
4656//Process::GetArchSpecForExistingProcess (const char *process_name)
4657//{
4658// return Host::GetArchSpecForExistingProcess (process_name);
4659//}
4660//
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004661void
4662Process::AppendSTDOUT (const char * s, size_t len)
4663{
Greg Clayton3af9ea52010-11-18 05:57:03 +00004664 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004665 m_stdout_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004666 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState()));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004667}
4668
4669void
Greg Clayton93e86192011-11-13 04:45:22 +00004670Process::AppendSTDERR (const char * s, size_t len)
4671{
4672 Mutex::Locker locker (m_stdio_communication_mutex);
4673 m_stderr_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004674 BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState()));
Greg Clayton93e86192011-11-13 04:45:22 +00004675}
4676
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004677void
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004678Process::BroadcastAsyncProfileData(const std::string &one_profile_data)
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004679{
4680 Mutex::Locker locker (m_profile_data_comm_mutex);
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004681 m_profile_data.push_back(one_profile_data);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004682 BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState()));
4683}
4684
4685size_t
4686Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error)
4687{
4688 Mutex::Locker locker(m_profile_data_comm_mutex);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004689 if (m_profile_data.empty())
4690 return 0;
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004691
4692 std::string &one_profile_data = m_profile_data.front();
4693 size_t bytes_available = one_profile_data.size();
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004694 if (bytes_available > 0)
4695 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004696 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004697 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004698 log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")",
4699 static_cast<void*>(buf),
4700 static_cast<uint64_t>(buf_size));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004701 if (bytes_available > buf_size)
4702 {
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004703 memcpy(buf, one_profile_data.c_str(), buf_size);
4704 one_profile_data.erase(0, buf_size);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004705 bytes_available = buf_size;
4706 }
4707 else
4708 {
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004709 memcpy(buf, one_profile_data.c_str(), bytes_available);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004710 m_profile_data.erase(m_profile_data.begin());
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004711 }
4712 }
4713 return bytes_available;
4714}
4715
4716
Greg Clayton93e86192011-11-13 04:45:22 +00004717//------------------------------------------------------------------
4718// Process STDIO
4719//------------------------------------------------------------------
4720
4721size_t
4722Process::GetSTDOUT (char *buf, size_t buf_size, Error &error)
4723{
4724 Mutex::Locker locker(m_stdio_communication_mutex);
4725 size_t bytes_available = m_stdout_data.size();
4726 if (bytes_available > 0)
4727 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004728 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004729 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004730 log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")",
4731 static_cast<void*>(buf),
4732 static_cast<uint64_t>(buf_size));
Greg Clayton93e86192011-11-13 04:45:22 +00004733 if (bytes_available > buf_size)
4734 {
4735 memcpy(buf, m_stdout_data.c_str(), buf_size);
4736 m_stdout_data.erase(0, buf_size);
4737 bytes_available = buf_size;
4738 }
4739 else
4740 {
4741 memcpy(buf, m_stdout_data.c_str(), bytes_available);
4742 m_stdout_data.clear();
4743 }
4744 }
4745 return bytes_available;
4746}
4747
4748
4749size_t
4750Process::GetSTDERR (char *buf, size_t buf_size, Error &error)
4751{
4752 Mutex::Locker locker(m_stdio_communication_mutex);
4753 size_t bytes_available = m_stderr_data.size();
4754 if (bytes_available > 0)
4755 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004756 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004757 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004758 log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")",
4759 static_cast<void*>(buf),
4760 static_cast<uint64_t>(buf_size));
Greg Clayton93e86192011-11-13 04:45:22 +00004761 if (bytes_available > buf_size)
4762 {
4763 memcpy(buf, m_stderr_data.c_str(), buf_size);
4764 m_stderr_data.erase(0, buf_size);
4765 bytes_available = buf_size;
4766 }
4767 else
4768 {
4769 memcpy(buf, m_stderr_data.c_str(), bytes_available);
4770 m_stderr_data.clear();
4771 }
4772 }
4773 return bytes_available;
4774}
4775
4776void
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004777Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
4778{
4779 Process *process = (Process *) baton;
4780 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
4781}
4782
Greg Clayton44d93782014-01-27 23:43:24 +00004783class IOHandlerProcessSTDIO :
4784 public IOHandler
4785{
4786public:
4787 IOHandlerProcessSTDIO (Process *process,
4788 int write_fd) :
4789 IOHandler(process->GetTarget().GetDebugger()),
4790 m_process (process),
4791 m_read_file (),
4792 m_write_file (write_fd, false),
4793 m_pipe_read(),
4794 m_pipe_write()
4795 {
4796 m_read_file.SetDescriptor(GetInputFD(), false);
4797 }
4798
4799 virtual
4800 ~IOHandlerProcessSTDIO ()
4801 {
4802
4803 }
4804
4805 bool
4806 OpenPipes ()
4807 {
4808 if (m_pipe_read.IsValid() && m_pipe_write.IsValid())
4809 return true;
4810
4811 int fds[2];
Hafiz Abid Qadeer6eff1012014-03-12 10:45:23 +00004812#ifdef _WIN32
Deepak Panickal914b8d92014-01-31 18:48:46 +00004813 // pipe is not supported on windows so default to a fail condition
4814 int err = 1;
4815#else
Greg Clayton44d93782014-01-27 23:43:24 +00004816 int err = pipe(fds);
Deepak Panickal914b8d92014-01-31 18:48:46 +00004817#endif
Greg Clayton44d93782014-01-27 23:43:24 +00004818 if (err == 0)
4819 {
4820 m_pipe_read.SetDescriptor(fds[0], true);
4821 m_pipe_write.SetDescriptor(fds[1], true);
4822 return true;
4823 }
4824 return false;
4825 }
4826
4827 void
4828 ClosePipes()
4829 {
4830 m_pipe_read.Close();
4831 m_pipe_write.Close();
4832 }
4833
4834 // Each IOHandler gets to run until it is done. It should read data
4835 // from the "in" and place output into "out" and "err and return
4836 // when done.
4837 virtual void
4838 Run ()
4839 {
4840 if (m_read_file.IsValid() && m_write_file.IsValid())
4841 {
4842 SetIsDone(false);
4843 if (OpenPipes())
4844 {
4845 const int read_fd = m_read_file.GetDescriptor();
4846 const int pipe_read_fd = m_pipe_read.GetDescriptor();
4847 TerminalState terminal_state;
4848 terminal_state.Save (read_fd, false);
4849 Terminal terminal(read_fd);
4850 terminal.SetCanonical(false);
4851 terminal.SetEcho(false);
Deepak Panickal914b8d92014-01-31 18:48:46 +00004852// FD_ZERO, FD_SET are not supported on windows
Hafiz Abid Qadeer6eff1012014-03-12 10:45:23 +00004853#ifndef _WIN32
Greg Clayton44d93782014-01-27 23:43:24 +00004854 while (!GetIsDone())
4855 {
4856 fd_set read_fdset;
4857 FD_ZERO (&read_fdset);
4858 FD_SET (read_fd, &read_fdset);
4859 FD_SET (pipe_read_fd, &read_fdset);
4860 const int nfds = std::max<int>(read_fd, pipe_read_fd) + 1;
4861 int num_set_fds = select (nfds, &read_fdset, NULL, NULL, NULL);
4862 if (num_set_fds < 0)
4863 {
4864 const int select_errno = errno;
4865
4866 if (select_errno != EINTR)
4867 SetIsDone(true);
4868 }
4869 else if (num_set_fds > 0)
4870 {
4871 char ch = 0;
4872 size_t n;
4873 if (FD_ISSET (read_fd, &read_fdset))
4874 {
4875 n = 1;
4876 if (m_read_file.Read(&ch, n).Success() && n == 1)
4877 {
4878 if (m_write_file.Write(&ch, n).Fail() || n != 1)
4879 SetIsDone(true);
4880 }
4881 else
4882 SetIsDone(true);
4883 }
4884 if (FD_ISSET (pipe_read_fd, &read_fdset))
4885 {
4886 // Consume the interrupt byte
4887 n = 1;
4888 m_pipe_read.Read (&ch, n);
Greg Clayton19e11352014-02-26 22:47:33 +00004889 switch (ch)
4890 {
4891 case 'q':
4892 SetIsDone(true);
4893 break;
4894 case 'i':
4895 if (StateIsRunningState(m_process->GetState()))
4896 m_process->Halt();
4897 break;
4898 }
Greg Clayton44d93782014-01-27 23:43:24 +00004899 }
4900 }
4901 }
Deepak Panickal914b8d92014-01-31 18:48:46 +00004902#endif
Greg Clayton44d93782014-01-27 23:43:24 +00004903 terminal_state.Restore();
4904
4905 }
4906 else
4907 SetIsDone(true);
4908 }
4909 else
4910 SetIsDone(true);
4911 }
4912
4913 // Hide any characters that have been displayed so far so async
4914 // output can be displayed. Refresh() will be called after the
4915 // output has been displayed.
4916 virtual void
4917 Hide ()
4918 {
4919
4920 }
4921 // Called when the async output has been received in order to update
4922 // the input reader (refresh the prompt and redisplay any current
4923 // line(s) that are being edited
4924 virtual void
4925 Refresh ()
4926 {
4927
4928 }
Greg Claytone68f5d62014-02-24 22:50:57 +00004929
Greg Clayton44d93782014-01-27 23:43:24 +00004930 virtual void
Greg Claytone68f5d62014-02-24 22:50:57 +00004931 Cancel ()
Greg Clayton44d93782014-01-27 23:43:24 +00004932 {
4933 size_t n = 1;
Greg Clayton19e11352014-02-26 22:47:33 +00004934 char ch = 'q'; // Send 'q' for quit
Greg Clayton44d93782014-01-27 23:43:24 +00004935 m_pipe_write.Write (&ch, n);
4936 }
Greg Claytone68f5d62014-02-24 22:50:57 +00004937
Greg Claytonf0066ad2014-05-02 00:45:31 +00004938 virtual bool
Greg Claytone68f5d62014-02-24 22:50:57 +00004939 Interrupt ()
4940 {
Greg Clayton19e11352014-02-26 22:47:33 +00004941#ifdef _MSC_VER
4942 // Windows doesn't support pipes, so we will send an async interrupt
4943 // event to stop the process
Greg Claytone68f5d62014-02-24 22:50:57 +00004944 if (StateIsRunningState(m_process->GetState()))
Ed Maste96e51b82014-02-25 14:20:14 +00004945 m_process->SendAsyncInterrupt();
Greg Clayton19e11352014-02-26 22:47:33 +00004946#else
4947 // Do only things that are safe to do in an interrupt context (like in
4948 // a SIGINT handler), like write 1 byte to a file descriptor. This will
4949 // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte
4950 // that was written to the pipe and then call m_process->Halt() from a
4951 // much safer location in code.
4952 size_t n = 1;
4953 char ch = 'i'; // Send 'i' for interrupt
4954 m_pipe_write.Write (&ch, n);
4955#endif
Greg Claytonf0066ad2014-05-02 00:45:31 +00004956 return true;
Greg Claytone68f5d62014-02-24 22:50:57 +00004957 }
Greg Clayton44d93782014-01-27 23:43:24 +00004958
4959 virtual void
4960 GotEOF()
4961 {
4962
4963 }
4964
4965protected:
4966 Process *m_process;
4967 File m_read_file; // Read from this file (usually actual STDIN for LLDB
4968 File m_write_file; // Write to this file (usually the master pty for getting io to debuggee)
4969 File m_pipe_read;
4970 File m_pipe_write;
4971
4972};
4973
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004974void
Greg Clayton44d93782014-01-27 23:43:24 +00004975Process::SetSTDIOFileDescriptor (int fd)
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004976{
4977 // First set up the Read Thread for reading/handling process I/O
4978
Greg Clayton44d93782014-01-27 23:43:24 +00004979 std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (fd, true));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004980
4981 if (conn_ap.get())
4982 {
4983 m_stdio_communication.SetConnection (conn_ap.release());
4984 if (m_stdio_communication.IsConnected())
4985 {
4986 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
4987 m_stdio_communication.StartReadThread();
4988
4989 // Now read thread is set up, set up input reader.
4990
4991 if (!m_process_input_reader.get())
Greg Clayton44d93782014-01-27 23:43:24 +00004992 m_process_input_reader.reset (new IOHandlerProcessSTDIO (this, fd));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004993 }
4994 }
4995}
4996
Greg Claytonb4874f12014-02-28 18:22:24 +00004997bool
Greg Clayton6fea17e2014-03-03 19:15:20 +00004998Process::ProcessIOHandlerIsActive ()
4999{
5000 IOHandlerSP io_handler_sp (m_process_input_reader);
5001 if (io_handler_sp)
5002 return m_target.GetDebugger().IsTopIOHandler (io_handler_sp);
5003 return false;
5004}
5005bool
Greg Clayton44d93782014-01-27 23:43:24 +00005006Process::PushProcessIOHandler ()
Caroline Ticeef5c6d02010-11-16 05:07:41 +00005007{
Greg Clayton44d93782014-01-27 23:43:24 +00005008 IOHandlerSP io_handler_sp (m_process_input_reader);
5009 if (io_handler_sp)
5010 {
5011 io_handler_sp->SetIsDone(false);
5012 m_target.GetDebugger().PushIOHandler (io_handler_sp);
Greg Claytonb4874f12014-02-28 18:22:24 +00005013 return true;
Greg Clayton44d93782014-01-27 23:43:24 +00005014 }
Greg Claytonb4874f12014-02-28 18:22:24 +00005015 return false;
Caroline Ticeef5c6d02010-11-16 05:07:41 +00005016}
5017
Greg Claytonb4874f12014-02-28 18:22:24 +00005018bool
Greg Clayton44d93782014-01-27 23:43:24 +00005019Process::PopProcessIOHandler ()
Caroline Ticeef5c6d02010-11-16 05:07:41 +00005020{
Greg Clayton44d93782014-01-27 23:43:24 +00005021 IOHandlerSP io_handler_sp (m_process_input_reader);
5022 if (io_handler_sp)
Greg Claytonb4874f12014-02-28 18:22:24 +00005023 return m_target.GetDebugger().PopIOHandler (io_handler_sp);
5024 return false;
Caroline Ticeef5c6d02010-11-16 05:07:41 +00005025}
5026
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00005027// The process needs to know about installed plug-ins
Greg Clayton99d0faf2010-11-18 23:32:35 +00005028void
Caroline Tice20bd37f2011-03-10 22:14:10 +00005029Process::SettingsInitialize ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00005030{
Greg Clayton6920b522012-08-22 18:39:03 +00005031 Thread::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00005032}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00005033
Greg Clayton99d0faf2010-11-18 23:32:35 +00005034void
Caroline Tice20bd37f2011-03-10 22:14:10 +00005035Process::SettingsTerminate ()
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00005036{
Greg Clayton6920b522012-08-22 18:39:03 +00005037 Thread::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00005038}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00005039
Jim Ingham1624a2d2014-05-05 02:26:40 +00005040ExpressionResults
Jim Inghamf48169b2010-11-30 02:22:11 +00005041Process::RunThreadPlan (ExecutionContext &exe_ctx,
Jim Ingham372787f2012-04-07 00:00:41 +00005042 lldb::ThreadPlanSP &thread_plan_sp,
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005043 const EvaluateExpressionOptions &options,
Jim Inghamf48169b2010-11-30 02:22:11 +00005044 Stream &errors)
5045{
Jim Ingham1624a2d2014-05-05 02:26:40 +00005046 ExpressionResults return_value = eExecutionSetupError;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005047
Jim Ingham77787032011-01-20 02:03:18 +00005048 if (thread_plan_sp.get() == NULL)
5049 {
5050 errors.Printf("RunThreadPlan called with empty thread plan.");
Greg Claytone0d378b2011-03-24 21:19:54 +00005051 return eExecutionSetupError;
Jim Ingham77787032011-01-20 02:03:18 +00005052 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005053
Jim Ingham7d7931d2013-03-28 00:05:34 +00005054 if (!thread_plan_sp->ValidatePlan(NULL))
5055 {
5056 errors.Printf ("RunThreadPlan called with an invalid thread plan.");
5057 return eExecutionSetupError;
5058 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005059
Greg Claytonc14ee322011-09-22 04:58:26 +00005060 if (exe_ctx.GetProcessPtr() != this)
5061 {
5062 errors.Printf("RunThreadPlan called on wrong process.");
5063 return eExecutionSetupError;
5064 }
5065
5066 Thread *thread = exe_ctx.GetThreadPtr();
5067 if (thread == NULL)
5068 {
5069 errors.Printf("RunThreadPlan called with invalid thread.");
5070 return eExecutionSetupError;
5071 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005072
Jim Ingham17e5c4e2011-05-17 22:24:54 +00005073 // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes.
5074 // For that to be true the plan can't be private - since private plans suppress themselves in the
5075 // GetCompletedPlan call.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005076
Jim Ingham17e5c4e2011-05-17 22:24:54 +00005077 bool orig_plan_private = thread_plan_sp->GetPrivate();
5078 thread_plan_sp->SetPrivate(false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005079
Jim Ingham444586b2011-01-24 06:34:17 +00005080 if (m_private_state.GetValue() != eStateStopped)
5081 {
5082 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Greg Claytone0d378b2011-03-24 21:19:54 +00005083 return eExecutionSetupError;
Jim Ingham444586b2011-01-24 06:34:17 +00005084 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005085
Jim Ingham66243842011-08-13 00:56:10 +00005086 // Save the thread & frame from the exe_ctx for restoration after we run
Greg Claytonc14ee322011-09-22 04:58:26 +00005087 const uint32_t thread_idx_id = thread->GetIndexID();
Jason Molendab57e4a12013-11-04 09:33:30 +00005088 StackFrameSP selected_frame_sp = thread->GetSelectedFrame();
Jim Ingham11b0e052013-02-19 23:22:45 +00005089 if (!selected_frame_sp)
5090 {
5091 thread->SetSelectedFrame(0);
5092 selected_frame_sp = thread->GetSelectedFrame();
5093 if (!selected_frame_sp)
5094 {
5095 errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id);
5096 return eExecutionSetupError;
5097 }
5098 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005099
Jim Ingham11b0e052013-02-19 23:22:45 +00005100 StackID ctx_frame_id = selected_frame_sp->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00005101
5102 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
5103 // so we should arrange to reset them as well.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005104
Greg Claytonc14ee322011-09-22 04:58:26 +00005105 lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005106
Jim Ingham66243842011-08-13 00:56:10 +00005107 uint32_t selected_tid;
5108 StackID selected_stack_id;
Greg Clayton762f7132011-09-18 18:59:15 +00005109 if (selected_thread_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00005110 {
5111 selected_tid = selected_thread_sp->GetIndexID();
Jim Ingham66243842011-08-13 00:56:10 +00005112 selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00005113 }
5114 else
5115 {
5116 selected_tid = LLDB_INVALID_THREAD_ID;
5117 }
5118
Jim Ingham372787f2012-04-07 00:00:41 +00005119 lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD;
Jim Ingham076b3042012-04-10 01:21:57 +00005120 lldb::StateType old_state;
5121 lldb::ThreadPlanSP stopper_base_plan_sp;
Jim Ingham372787f2012-04-07 00:00:41 +00005122
Greg Clayton5160ce52013-03-27 23:08:40 +00005123 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham372787f2012-04-07 00:00:41 +00005124 if (Host::GetCurrentThread() == m_private_state_thread)
5125 {
Jim Ingham076b3042012-04-10 01:21:57 +00005126 // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since
5127 // we are the thread that is generating public events.
Jim Ingham372787f2012-04-07 00:00:41 +00005128 // 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 +00005129 // we are fielding public events here.
5130 if (log)
Jason Molendad251c9d2012-11-17 01:41:04 +00005131 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 +00005132
Jim Ingham372787f2012-04-07 00:00:41 +00005133 backup_private_state_thread = m_private_state_thread;
Jim Ingham076b3042012-04-10 01:21:57 +00005134
5135 // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop,
5136 // returning control here.
5137 // But in the normal course of things, the plan above us on the stack would be given a shot at the stop
5138 // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack
5139 // before the plan we want to run. Since base plans always stop and return control to the user, that will
5140 // do just what we want.
5141 stopper_base_plan_sp.reset(new ThreadPlanBase (*thread));
5142 thread->QueueThreadPlan (stopper_base_plan_sp, false);
5143 // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly.
5144 old_state = m_public_state.GetValue();
5145 m_public_state.SetValueNoLock(eStateStopped);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005146
Jim Ingham076b3042012-04-10 01:21:57 +00005147 // Now spin up the private state thread:
Jim Ingham372787f2012-04-07 00:00:41 +00005148 StartPrivateStateThread(true);
5149 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005150
Jim Ingham372787f2012-04-07 00:00:41 +00005151 thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense?
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005152
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005153 if (options.GetDebug())
5154 {
5155 // In this case, we aren't actually going to run, we just want to stop right away.
5156 // Flush this thread so we will refetch the stacks and show the correct backtrace.
5157 // FIXME: To make this prettier we should invent some stop reason for this, but that
5158 // is only cosmetic, and this functionality is only of use to lldb developers who can
5159 // live with not pretty...
5160 thread->Flush();
5161 return eExecutionStoppedForDebug;
5162 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005163
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00005164 Listener listener("lldb.process.listener.run-thread-plan");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005165
Sean Callanana46ec452012-07-11 21:31:24 +00005166 lldb::EventSP event_to_broadcast_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005167
Jim Ingham77787032011-01-20 02:03:18 +00005168 {
Sean Callanana46ec452012-07-11 21:31:24 +00005169 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
5170 // restored on exit to the function.
5171 //
5172 // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event
5173 // is put into event_to_broadcast_sp for rebroadcasting.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005174
Sean Callanana46ec452012-07-11 21:31:24 +00005175 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005176
Jim Inghamf48169b2010-11-30 02:22:11 +00005177 if (log)
Jim Ingham0f16e732011-02-08 05:20:59 +00005178 {
5179 StreamString s;
Sean Callanana46ec452012-07-11 21:31:24 +00005180 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Daniel Malead01b2952012-11-29 21:49:15 +00005181 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 " to run thread plan \"%s\".",
Sean Callanana46ec452012-07-11 21:31:24 +00005182 thread->GetIndexID(),
5183 thread->GetID(),
5184 s.GetData());
5185 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005186
Sean Callanana46ec452012-07-11 21:31:24 +00005187 bool got_event;
5188 lldb::EventSP event_sp;
5189 lldb::StateType stop_state = lldb::eStateInvalid;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005190
Sean Callanana46ec452012-07-11 21:31:24 +00005191 TimeValue* timeout_ptr = NULL;
5192 TimeValue real_timeout;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005193
Jim Ingham0161b492013-02-09 01:29:05 +00005194 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 +00005195 bool do_resume = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005196 bool handle_running_event = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +00005197 const uint64_t default_one_thread_timeout_usec = 250000;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005198
Jim Ingham0161b492013-02-09 01:29:05 +00005199 // This is just for accounting:
5200 uint32_t num_resumes = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005201
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005202 uint32_t timeout_usec = options.GetTimeoutUsec();
Jim Inghamfd95f892014-04-22 01:41:52 +00005203 uint32_t one_thread_timeout_usec;
5204 uint32_t all_threads_timeout_usec = 0;
Jim Inghamfe1c3422014-04-16 02:24:48 +00005205
5206 // If we are going to run all threads the whole time, or if we are only going to run one thread,
5207 // then we don't need the first timeout. So we set the final timeout, and pretend we are after the
5208 // first timeout already.
5209
5210 if (!options.GetStopOthers() || !options.GetTryAllThreads())
Jim Ingham286fb1e2014-02-28 02:52:06 +00005211 {
5212 before_first_timeout = false;
Jim Inghamfd95f892014-04-22 01:41:52 +00005213 one_thread_timeout_usec = 0;
5214 all_threads_timeout_usec = timeout_usec;
Jim Ingham286fb1e2014-02-28 02:52:06 +00005215 }
Jim Inghamfe1c3422014-04-16 02:24:48 +00005216 else
Jim Ingham0161b492013-02-09 01:29:05 +00005217 {
Jim Inghamfd95f892014-04-22 01:41:52 +00005218 uint32_t option_one_thread_timeout = options.GetOneThreadTimeoutUsec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005219
Jim Ingham914f4e72014-03-28 21:58:28 +00005220 // If the overall wait is forever, then we only need to set the one thread timeout:
5221 if (timeout_usec == 0)
5222 {
Ed Maste801335c2014-03-31 19:28:14 +00005223 if (option_one_thread_timeout != 0)
Jim Inghamfd95f892014-04-22 01:41:52 +00005224 one_thread_timeout_usec = option_one_thread_timeout;
Jim Ingham914f4e72014-03-28 21:58:28 +00005225 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005226 one_thread_timeout_usec = default_one_thread_timeout_usec;
Jim Ingham914f4e72014-03-28 21:58:28 +00005227 }
Jim Ingham0161b492013-02-09 01:29:05 +00005228 else
5229 {
Jim Ingham914f4e72014-03-28 21:58:28 +00005230 // Otherwise, if the one thread timeout is set, make sure it isn't longer than the overall timeout,
5231 // and use it, otherwise use half the total timeout, bounded by the default_one_thread_timeout_usec.
5232 uint64_t computed_one_thread_timeout;
5233 if (option_one_thread_timeout != 0)
5234 {
5235 if (timeout_usec < option_one_thread_timeout)
5236 {
5237 errors.Printf("RunThreadPlan called without one thread timeout greater than total timeout");
5238 return eExecutionSetupError;
5239 }
5240 computed_one_thread_timeout = option_one_thread_timeout;
5241 }
5242 else
5243 {
5244 computed_one_thread_timeout = timeout_usec / 2;
5245 if (computed_one_thread_timeout > default_one_thread_timeout_usec)
5246 computed_one_thread_timeout = default_one_thread_timeout_usec;
5247 }
Jim Inghamfd95f892014-04-22 01:41:52 +00005248 one_thread_timeout_usec = computed_one_thread_timeout;
5249 all_threads_timeout_usec = timeout_usec - one_thread_timeout_usec;
5250
Jim Ingham0161b492013-02-09 01:29:05 +00005251 }
Jim Ingham0161b492013-02-09 01:29:05 +00005252 }
Jim Inghamfe1c3422014-04-16 02:24:48 +00005253
5254 if (log)
Jim Inghamfd95f892014-04-22 01:41:52 +00005255 log->Printf ("Stop others: %u, try all: %u, before_first: %u, one thread: %" PRIu32 " - all threads: %" PRIu32 ".\n",
Jim Inghamfe1c3422014-04-16 02:24:48 +00005256 options.GetStopOthers(),
5257 options.GetTryAllThreads(),
Jim Inghamfd95f892014-04-22 01:41:52 +00005258 before_first_timeout,
5259 one_thread_timeout_usec,
5260 all_threads_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005261
Jim Ingham1460e4b2014-01-10 23:46:59 +00005262 // This isn't going to work if there are unfetched events on the queue.
5263 // Are there cases where we might want to run the remaining events here, and then try to
5264 // call the function? That's probably being too tricky for our own good.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005265
Jim Ingham1460e4b2014-01-10 23:46:59 +00005266 Event *other_events = listener.PeekAtNextEvent();
5267 if (other_events != NULL)
5268 {
5269 errors.Printf("Calling RunThreadPlan with pending events on the queue.");
5270 return eExecutionSetupError;
5271 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005272
Jim Ingham1460e4b2014-01-10 23:46:59 +00005273 // We also need to make sure that the next event is delivered. We might be calling a function as part of
5274 // a thread plan, in which case the last delivered event could be the running event, and we don't want
5275 // event coalescing to cause us to lose OUR running event...
5276 ForceNextEventDelivery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005277
Jim Ingham8559a352012-11-26 23:52:18 +00005278 // This while loop must exit out the bottom, there's cleanup that we need to do when we are done.
5279 // So don't call return anywhere within it.
Jim Ingham35878c42014-04-08 21:33:21 +00005280
5281#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
5282 // It's pretty much impossible to write test cases for things like:
5283 // One thread timeout expires, I go to halt, but the process already stopped
5284 // on the function call stop breakpoint. Turning on this define will make us not
5285 // fetch the first event till after the halt. So if you run a quick function, it will have
5286 // completed, and the completion event will be waiting, when you interrupt for halt.
5287 // The expression evaluation should still succeed.
5288 bool miss_first_event = true;
5289#endif
Jim Inghamfd95f892014-04-22 01:41:52 +00005290 TimeValue one_thread_timeout;
5291 TimeValue final_timeout;
5292
Jim Ingham35878c42014-04-08 21:33:21 +00005293
Sean Callanana46ec452012-07-11 21:31:24 +00005294 while (1)
5295 {
5296 // We usually want to resume the process if we get to the top of the loop.
5297 // The only exception is if we get two running events with no intervening
5298 // stop, which can happen, we will just wait for then next stop event.
Jim Ingham0161b492013-02-09 01:29:05 +00005299 if (log)
5300 log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.",
5301 do_resume,
5302 handle_running_event,
5303 before_first_timeout);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005304
Jim Ingham184e9812013-01-15 02:47:48 +00005305 if (do_resume || handle_running_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005306 {
5307 // Do the initial resume and wait for the running event before going further.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005308
Jim Ingham184e9812013-01-15 02:47:48 +00005309 if (do_resume)
Sean Callanana46ec452012-07-11 21:31:24 +00005310 {
Jim Ingham0161b492013-02-09 01:29:05 +00005311 num_resumes++;
Jim Ingham184e9812013-01-15 02:47:48 +00005312 Error resume_error = PrivateResume ();
5313 if (!resume_error.Success())
5314 {
Jim Ingham0161b492013-02-09 01:29:05 +00005315 errors.Printf("Error resuming inferior the %d time: \"%s\".\n",
5316 num_resumes,
5317 resume_error.AsCString());
Jim Ingham184e9812013-01-15 02:47:48 +00005318 return_value = eExecutionSetupError;
5319 break;
5320 }
Sean Callanana46ec452012-07-11 21:31:24 +00005321 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005322
Jim Ingham0161b492013-02-09 01:29:05 +00005323 TimeValue resume_timeout = TimeValue::Now();
5324 resume_timeout.OffsetWithMicroSeconds(500000);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005325
Jim Ingham0161b492013-02-09 01:29:05 +00005326 got_event = listener.WaitForEvent(&resume_timeout, event_sp);
Sean Callanana46ec452012-07-11 21:31:24 +00005327 if (!got_event)
5328 {
5329 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005330 log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.",
5331 num_resumes);
Sean Callanana46ec452012-07-11 21:31:24 +00005332
Jim Ingham0161b492013-02-09 01:29:05 +00005333 errors.Printf("Didn't get any event after resume %d, exiting.", num_resumes);
Sean Callanana46ec452012-07-11 21:31:24 +00005334 return_value = eExecutionSetupError;
5335 break;
5336 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005337
Sean Callanana46ec452012-07-11 21:31:24 +00005338 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Ingham0161b492013-02-09 01:29:05 +00005339
Sean Callanana46ec452012-07-11 21:31:24 +00005340 if (stop_state != eStateRunning)
5341 {
Jim Ingham0161b492013-02-09 01:29:05 +00005342 bool restarted = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005343
Jim Ingham0161b492013-02-09 01:29:05 +00005344 if (stop_state == eStateStopped)
5345 {
5346 restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get());
5347 if (log)
5348 log->Printf("Process::RunThreadPlan(): didn't get running event after "
5349 "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).",
5350 num_resumes,
5351 StateAsCString(stop_state),
5352 restarted,
5353 do_resume,
5354 handle_running_event);
5355 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005356
Jim Ingham0161b492013-02-09 01:29:05 +00005357 if (restarted)
5358 {
5359 // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted
5360 // event here. But if I do, the best thing is to Halt and then get out of here.
5361 Halt();
5362 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005363
Jim Ingham35e1bda2012-10-16 21:41:58 +00005364 errors.Printf("Didn't get running event after initial resume, got %s instead.",
5365 StateAsCString(stop_state));
Sean Callanana46ec452012-07-11 21:31:24 +00005366 return_value = eExecutionSetupError;
5367 break;
5368 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005369
Sean Callanana46ec452012-07-11 21:31:24 +00005370 if (log)
5371 log->PutCString ("Process::RunThreadPlan(): resuming succeeded.");
5372 // We need to call the function synchronously, so spin waiting for it to return.
5373 // If we get interrupted while executing, we're going to lose our context, and
5374 // won't be able to gather the result at this point.
5375 // We set the timeout AFTER the resume, since the resume takes some time and we
5376 // don't want to charge that to the timeout.
Sean Callanana46ec452012-07-11 21:31:24 +00005377 }
Jim Ingham0f16e732011-02-08 05:20:59 +00005378 else
5379 {
Sean Callanana46ec452012-07-11 21:31:24 +00005380 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005381 log->PutCString ("Process::RunThreadPlan(): waiting for next event.");
Jim Ingham0f16e732011-02-08 05:20:59 +00005382 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005383
Jim Ingham0161b492013-02-09 01:29:05 +00005384 if (before_first_timeout)
5385 {
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005386 if (options.GetTryAllThreads())
Jim Inghamfd95f892014-04-22 01:41:52 +00005387 {
5388 one_thread_timeout = TimeValue::Now();
5389 one_thread_timeout.OffsetWithMicroSeconds(one_thread_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005390 timeout_ptr = &one_thread_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005391 }
Jim Ingham0161b492013-02-09 01:29:05 +00005392 else
5393 {
5394 if (timeout_usec == 0)
5395 timeout_ptr = NULL;
5396 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005397 {
5398 final_timeout = TimeValue::Now();
5399 final_timeout.OffsetWithMicroSeconds (timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005400 timeout_ptr = &final_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005401 }
Jim Ingham0161b492013-02-09 01:29:05 +00005402 }
5403 }
5404 else
5405 {
5406 if (timeout_usec == 0)
5407 timeout_ptr = NULL;
5408 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005409 {
5410 final_timeout = TimeValue::Now();
5411 final_timeout.OffsetWithMicroSeconds (all_threads_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005412 timeout_ptr = &final_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005413 }
Jim Ingham0161b492013-02-09 01:29:05 +00005414 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005415
Jim Ingham0161b492013-02-09 01:29:05 +00005416 do_resume = true;
5417 handle_running_event = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005418
Sean Callanana46ec452012-07-11 21:31:24 +00005419 // Now wait for the process to stop again:
Sean Callanana46ec452012-07-11 21:31:24 +00005420 event_sp.reset();
Jim Ingham0f16e732011-02-08 05:20:59 +00005421
Jim Ingham0f16e732011-02-08 05:20:59 +00005422 if (log)
Jim Ingham20829ac2011-08-09 22:24:33 +00005423 {
Sean Callanana46ec452012-07-11 21:31:24 +00005424 if (timeout_ptr)
5425 {
Matt Kopec676a4872013-02-21 23:55:31 +00005426 log->Printf ("Process::RunThreadPlan(): about to wait - now is %" PRIu64 " - endpoint is %" PRIu64,
Jim Ingham0161b492013-02-09 01:29:05 +00005427 TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(),
5428 timeout_ptr->GetAsMicroSecondsSinceJan1_1970());
Sean Callanana46ec452012-07-11 21:31:24 +00005429 }
Jim Ingham20829ac2011-08-09 22:24:33 +00005430 else
Sean Callanana46ec452012-07-11 21:31:24 +00005431 {
5432 log->Printf ("Process::RunThreadPlan(): about to wait forever.");
5433 }
5434 }
Jim Ingham35878c42014-04-08 21:33:21 +00005435
5436#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
5437 // See comment above...
5438 if (miss_first_event)
5439 {
5440 usleep(1000);
5441 miss_first_event = false;
5442 got_event = false;
5443 }
5444 else
5445#endif
Sean Callanana46ec452012-07-11 21:31:24 +00005446 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005447
Sean Callanana46ec452012-07-11 21:31:24 +00005448 if (got_event)
5449 {
5450 if (event_sp.get())
5451 {
5452 bool keep_going = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005453 if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005454 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005455 Halt();
Jim Inghamcfc09352012-07-27 23:57:19 +00005456 return_value = eExecutionInterrupted;
5457 errors.Printf ("Execution halted by user interrupt.");
5458 if (log)
5459 log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting.");
Jim Ingham0161b492013-02-09 01:29:05 +00005460 break;
Jim Inghamcfc09352012-07-27 23:57:19 +00005461 }
5462 else
5463 {
5464 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5465 if (log)
5466 log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005467
Jim Inghamcfc09352012-07-27 23:57:19 +00005468 switch (stop_state)
Sean Callanana46ec452012-07-11 21:31:24 +00005469 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005470 case lldb::eStateStopped:
Sean Callanana46ec452012-07-11 21:31:24 +00005471 {
Jim Ingham0161b492013-02-09 01:29:05 +00005472 // We stopped, figure out what we are going to do now.
Jim Inghamcfc09352012-07-27 23:57:19 +00005473 ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id);
5474 if (!thread_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005475 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005476 // Ooh, our thread has vanished. Unlikely that this was successful execution...
Sean Callanana46ec452012-07-11 21:31:24 +00005477 if (log)
Jim Inghamcfc09352012-07-27 23:57:19 +00005478 log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id);
5479 return_value = eExecutionInterrupted;
Sean Callanana46ec452012-07-11 21:31:24 +00005480 }
5481 else
5482 {
Jim Ingham0161b492013-02-09 01:29:05 +00005483 // If we were restarted, we just need to go back up to fetch another event.
5484 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
Jim Inghamcfc09352012-07-27 23:57:19 +00005485 {
5486 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005487 {
5488 log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting.");
5489 }
5490 keep_going = true;
5491 do_resume = false;
5492 handle_running_event = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005493
Jim Inghamcfc09352012-07-27 23:57:19 +00005494 }
5495 else
5496 {
Jim Ingham0161b492013-02-09 01:29:05 +00005497 StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
5498 StopReason stop_reason = eStopReasonInvalid;
5499 if (stop_info_sp)
5500 stop_reason = stop_info_sp->GetStopReason();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005501
Jim Ingham0161b492013-02-09 01:29:05 +00005502 // FIXME: We only check if the stop reason is plan complete, should we make sure that
5503 // it is OUR plan that is complete?
5504 if (stop_reason == eStopReasonPlanComplete)
Jim Ingham184e9812013-01-15 02:47:48 +00005505 {
5506 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005507 log->PutCString ("Process::RunThreadPlan(): execution completed successfully.");
5508 // Now mark this plan as private so it doesn't get reported as the stop reason
5509 // after this point.
5510 if (thread_plan_sp)
5511 thread_plan_sp->SetPrivate (orig_plan_private);
5512 return_value = eExecutionCompleted;
Jim Ingham184e9812013-01-15 02:47:48 +00005513 }
5514 else
5515 {
Jim Ingham0161b492013-02-09 01:29:05 +00005516 // Something restarted the target, so just wait for it to stop for real.
Jim Ingham184e9812013-01-15 02:47:48 +00005517 if (stop_reason == eStopReasonBreakpoint)
Jim Ingham0161b492013-02-09 01:29:05 +00005518 {
5519 if (log)
5520 log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription());
Jim Ingham184e9812013-01-15 02:47:48 +00005521 return_value = eExecutionHitBreakpoint;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005522 if (!options.DoesIgnoreBreakpoints())
Sean Callanan4b388c92013-07-30 19:54:09 +00005523 {
5524 event_to_broadcast_sp = event_sp;
5525 }
Jim Ingham0161b492013-02-09 01:29:05 +00005526 }
Jim Ingham184e9812013-01-15 02:47:48 +00005527 else
Jim Ingham0161b492013-02-09 01:29:05 +00005528 {
5529 if (log)
5530 log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete.");
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005531 if (!options.DoesUnwindOnError())
Sean Callanan4b388c92013-07-30 19:54:09 +00005532 event_to_broadcast_sp = event_sp;
Jim Ingham184e9812013-01-15 02:47:48 +00005533 return_value = eExecutionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005534 }
Jim Ingham184e9812013-01-15 02:47:48 +00005535 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005536 }
Sean Callanana46ec452012-07-11 21:31:24 +00005537 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005538 }
5539 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005540
Jim Inghamcfc09352012-07-27 23:57:19 +00005541 case lldb::eStateRunning:
Jim Ingham0161b492013-02-09 01:29:05 +00005542 // This shouldn't really happen, but sometimes we do get two running events without an
5543 // intervening stop, and in that case we should just go back to waiting for the stop.
Jim Inghamcfc09352012-07-27 23:57:19 +00005544 do_resume = false;
5545 keep_going = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005546 handle_running_event = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005547 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005548
Jim Inghamcfc09352012-07-27 23:57:19 +00005549 default:
5550 if (log)
5551 log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005552
Jim Inghamcfc09352012-07-27 23:57:19 +00005553 if (stop_state == eStateExited)
5554 event_to_broadcast_sp = event_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005555
Sean Callananbf154da2012-08-08 17:35:10 +00005556 errors.Printf ("Execution stopped with unexpected state.\n");
Jim Inghamcfc09352012-07-27 23:57:19 +00005557 return_value = eExecutionInterrupted;
5558 break;
5559 }
Sean Callanana46ec452012-07-11 21:31:24 +00005560 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005561
Sean Callanana46ec452012-07-11 21:31:24 +00005562 if (keep_going)
5563 continue;
5564 else
5565 break;
5566 }
5567 else
5568 {
5569 if (log)
5570 log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd...");
5571 return_value = eExecutionInterrupted;
5572 break;
5573 }
5574 }
5575 else
5576 {
5577 // If we didn't get an event that means we've timed out...
5578 // We will interrupt the process here. Depending on what we were asked to do we will
5579 // either exit, or try with all threads running for the same timeout.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005580
Sean Callanana46ec452012-07-11 21:31:24 +00005581 if (log) {
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005582 if (options.GetTryAllThreads())
Sean Callanana46ec452012-07-11 21:31:24 +00005583 {
Jim Ingham0161b492013-02-09 01:29:05 +00005584 if (before_first_timeout)
Jim Inghamfe1c3422014-04-16 02:24:48 +00005585 {
5586 if (timeout_usec != 0)
5587 {
Jim Inghamfe1c3422014-04-16 02:24:48 +00005588 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
Jim Inghamfd95f892014-04-22 01:41:52 +00005589 "running for %" PRIu32 " usec with all threads enabled.",
5590 all_threads_timeout_usec);
Jim Inghamfe1c3422014-04-16 02:24:48 +00005591 }
5592 else
5593 {
5594 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
Ed Mastee61c7b02014-04-29 17:48:06 +00005595 "running forever with all threads enabled.");
Jim Inghamfe1c3422014-04-16 02:24:48 +00005596 }
5597 }
Sean Callanana46ec452012-07-11 21:31:24 +00005598 else
5599 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
Jason Molenda6a8658a2013-10-27 02:32:23 +00005600 "and timeout: %u timed out, abandoning execution.",
Jim Ingham35e1bda2012-10-16 21:41:58 +00005601 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005602 }
5603 else
Jason Molenda6a8658a2013-10-27 02:32:23 +00005604 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, "
Jim Ingham35e1bda2012-10-16 21:41:58 +00005605 "abandoning execution.",
5606 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005607 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005608
Jim Ingham0161b492013-02-09 01:29:05 +00005609 // It is possible that between the time we issued the Halt, and we get around to calling Halt the target
5610 // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event.
5611 // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In
5612 // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's
5613 // stopped event. That's what this while loop does.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005614
Jim Ingham0161b492013-02-09 01:29:05 +00005615 bool back_to_top = true;
5616 uint32_t try_halt_again = 0;
5617 bool do_halt = true;
5618 const uint32_t num_retries = 5;
5619 while (try_halt_again < num_retries)
Sean Callanana46ec452012-07-11 21:31:24 +00005620 {
Jim Ingham0161b492013-02-09 01:29:05 +00005621 Error halt_error;
5622 if (do_halt)
5623 {
5624 if (log)
5625 log->Printf ("Process::RunThreadPlan(): Running Halt.");
5626 halt_error = Halt();
5627 }
5628 if (halt_error.Success())
5629 {
5630 if (log)
5631 log->PutCString ("Process::RunThreadPlan(): Halt succeeded.");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005632
Jim Ingham0161b492013-02-09 01:29:05 +00005633 real_timeout = TimeValue::Now();
5634 real_timeout.OffsetWithMicroSeconds(500000);
5635
5636 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005637
Jim Ingham0161b492013-02-09 01:29:05 +00005638 if (got_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005639 {
Jim Ingham0161b492013-02-09 01:29:05 +00005640 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5641 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005642 {
Jim Ingham0161b492013-02-09 01:29:05 +00005643 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
5644 if (stop_state == lldb::eStateStopped
5645 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
5646 log->PutCString (" Event was the Halt interruption event.");
Sean Callanana46ec452012-07-11 21:31:24 +00005647 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005648
Jim Ingham0161b492013-02-09 01:29:05 +00005649 if (stop_state == lldb::eStateStopped)
Sean Callanana46ec452012-07-11 21:31:24 +00005650 {
Jim Ingham0161b492013-02-09 01:29:05 +00005651 // Between the time we initiated the Halt and the time we delivered it, the process could have
5652 // already finished its job. Check that here:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005653
Jim Ingham0161b492013-02-09 01:29:05 +00005654 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
5655 {
5656 if (log)
5657 log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
5658 "Exiting wait loop.");
5659 return_value = eExecutionCompleted;
5660 back_to_top = false;
5661 break;
5662 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005663
Jim Ingham0161b492013-02-09 01:29:05 +00005664 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
5665 {
5666 if (log)
5667 log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... "
5668 "Exiting wait loop.");
5669 try_halt_again++;
5670 do_halt = false;
5671 continue;
5672 }
Sean Callanana46ec452012-07-11 21:31:24 +00005673
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005674 if (!options.GetTryAllThreads())
Jim Ingham0161b492013-02-09 01:29:05 +00005675 {
5676 if (log)
5677 log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting.");
5678 return_value = eExecutionInterrupted;
5679 back_to_top = false;
5680 break;
5681 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005682
Jim Ingham0161b492013-02-09 01:29:05 +00005683 if (before_first_timeout)
5684 {
5685 // Set all the other threads to run, and return to the top of the loop, which will continue;
5686 before_first_timeout = false;
5687 thread_plan_sp->SetStopOthers (false);
5688 if (log)
5689 log->PutCString ("Process::RunThreadPlan(): about to resume.");
Sean Callanana46ec452012-07-11 21:31:24 +00005690
Jim Ingham0161b492013-02-09 01:29:05 +00005691 back_to_top = true;
5692 break;
5693 }
5694 else
5695 {
5696 // Running all threads failed, so return Interrupted.
5697 if (log)
5698 log->PutCString("Process::RunThreadPlan(): running all threads timed out.");
5699 return_value = eExecutionInterrupted;
5700 back_to_top = false;
5701 break;
5702 }
Sean Callanana46ec452012-07-11 21:31:24 +00005703 }
5704 }
5705 else
Jim Ingham0161b492013-02-09 01:29:05 +00005706 { if (log)
5707 log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
5708 "I'm getting out of here passing Interrupted.");
Sean Callanana46ec452012-07-11 21:31:24 +00005709 return_value = eExecutionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005710 back_to_top = false;
5711 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005712 }
5713 }
Jim Ingham0161b492013-02-09 01:29:05 +00005714 else
5715 {
5716 try_halt_again++;
5717 continue;
5718 }
Sean Callanana46ec452012-07-11 21:31:24 +00005719 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005720
Jim Ingham0161b492013-02-09 01:29:05 +00005721 if (!back_to_top || try_halt_again > num_retries)
5722 break;
5723 else
5724 continue;
Sean Callanana46ec452012-07-11 21:31:24 +00005725 }
Sean Callanana46ec452012-07-11 21:31:24 +00005726 } // END WAIT LOOP
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005727
Sean Callanana46ec452012-07-11 21:31:24 +00005728 // If we had to start up a temporary private state thread to run this thread plan, shut it down now.
5729 if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread))
5730 {
5731 StopPrivateStateThread();
5732 Error error;
5733 m_private_state_thread = backup_private_state_thread;
Sean Callanan9a028512012-08-09 00:50:26 +00005734 if (stopper_base_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005735 {
5736 thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp);
5737 }
5738 m_public_state.SetValueNoLock(old_state);
5739
5740 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005741
Jim Ingham184e9812013-01-15 02:47:48 +00005742 // Restore the thread state if we are going to discard the plan execution. There are three cases where this
5743 // could happen:
5744 // 1) The execution successfully completed
5745 // 2) We hit a breakpoint, and ignore_breakpoints was true
5746 // 3) We got some other error, and discard_on_error was true
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005747 bool should_unwind = (return_value == eExecutionInterrupted && options.DoesUnwindOnError())
5748 || (return_value == eExecutionHitBreakpoint && options.DoesIgnoreBreakpoints());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005749
Jim Ingham184e9812013-01-15 02:47:48 +00005750 if (return_value == eExecutionCompleted
5751 || should_unwind)
Jim Ingham8559a352012-11-26 23:52:18 +00005752 {
5753 thread_plan_sp->RestoreThreadState();
5754 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005755
Sean Callanana46ec452012-07-11 21:31:24 +00005756 // Now do some processing on the results of the run:
Jim Ingham184e9812013-01-15 02:47:48 +00005757 if (return_value == eExecutionInterrupted || return_value == eExecutionHitBreakpoint)
Sean Callanana46ec452012-07-11 21:31:24 +00005758 {
5759 if (log)
5760 {
5761 StreamString s;
5762 if (event_sp)
5763 event_sp->Dump (&s);
5764 else
5765 {
5766 log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
5767 }
5768
5769 StreamString ts;
5770
5771 const char *event_explanation = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005772
Sean Callanana46ec452012-07-11 21:31:24 +00005773 do
5774 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005775 if (!event_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005776 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005777 event_explanation = "<no event>";
Sean Callanana46ec452012-07-11 21:31:24 +00005778 break;
5779 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005780 else if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005781 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005782 event_explanation = "<user interrupt>";
Sean Callanana46ec452012-07-11 21:31:24 +00005783 break;
5784 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005785 else
Sean Callanana46ec452012-07-11 21:31:24 +00005786 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005787 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
5788
5789 if (!event_data)
Sean Callanana46ec452012-07-11 21:31:24 +00005790 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005791 event_explanation = "<no event data>";
5792 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005793 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005794
Jim Inghamcfc09352012-07-27 23:57:19 +00005795 Process *process = event_data->GetProcessSP().get();
5796
5797 if (!process)
Sean Callanana46ec452012-07-11 21:31:24 +00005798 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005799 event_explanation = "<no process>";
5800 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005801 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005802
Jim Inghamcfc09352012-07-27 23:57:19 +00005803 ThreadList &thread_list = process->GetThreadList();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005804
Jim Inghamcfc09352012-07-27 23:57:19 +00005805 uint32_t num_threads = thread_list.GetSize();
5806 uint32_t thread_index;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005807
Jim Inghamcfc09352012-07-27 23:57:19 +00005808 ts.Printf("<%u threads> ", num_threads);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005809
Jim Inghamcfc09352012-07-27 23:57:19 +00005810 for (thread_index = 0;
5811 thread_index < num_threads;
5812 ++thread_index)
5813 {
5814 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005815
Jim Inghamcfc09352012-07-27 23:57:19 +00005816 if (!thread)
5817 {
5818 ts.Printf("<?> ");
5819 continue;
5820 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005821
Daniel Malead01b2952012-11-29 21:49:15 +00005822 ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00005823 RegisterContext *register_context = thread->GetRegisterContext().get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005824
Jim Inghamcfc09352012-07-27 23:57:19 +00005825 if (register_context)
Daniel Malead01b2952012-11-29 21:49:15 +00005826 ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC());
Jim Inghamcfc09352012-07-27 23:57:19 +00005827 else
5828 ts.Printf("[ip unknown] ");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005829
Jim Inghamcfc09352012-07-27 23:57:19 +00005830 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
5831 if (stop_info_sp)
5832 {
5833 const char *stop_desc = stop_info_sp->GetDescription();
5834 if (stop_desc)
5835 ts.PutCString (stop_desc);
5836 }
5837 ts.Printf(">");
5838 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005839
Jim Inghamcfc09352012-07-27 23:57:19 +00005840 event_explanation = ts.GetData();
Sean Callanana46ec452012-07-11 21:31:24 +00005841 }
Sean Callanana46ec452012-07-11 21:31:24 +00005842 } while (0);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005843
Jim Inghamcfc09352012-07-27 23:57:19 +00005844 if (event_explanation)
5845 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
Sean Callanana46ec452012-07-11 21:31:24 +00005846 else
Jim Inghamcfc09352012-07-27 23:57:19 +00005847 log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData());
5848 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005849
Jim Inghame4483cf2013-09-27 01:13:01 +00005850 if (should_unwind)
Jim Inghamcfc09352012-07-27 23:57:19 +00005851 {
5852 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005853 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.",
5854 static_cast<void*>(thread_plan_sp.get()));
Jim Inghamcfc09352012-07-27 23:57:19 +00005855 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5856 thread_plan_sp->SetPrivate (orig_plan_private);
5857 }
5858 else
5859 {
5860 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005861 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.",
5862 static_cast<void*>(thread_plan_sp.get()));
Sean Callanana46ec452012-07-11 21:31:24 +00005863 }
5864 }
5865 else if (return_value == eExecutionSetupError)
5866 {
5867 if (log)
5868 log->PutCString("Process::RunThreadPlan(): execution set up error.");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005869
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005870 if (options.DoesUnwindOnError())
Jim Ingham0f16e732011-02-08 05:20:59 +00005871 {
Greg Claytonc14ee322011-09-22 04:58:26 +00005872 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00005873 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham0f16e732011-02-08 05:20:59 +00005874 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005875 }
5876 else
5877 {
Sean Callanana46ec452012-07-11 21:31:24 +00005878 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
Jim Inghamf48169b2010-11-30 02:22:11 +00005879 {
Jim Ingham0f16e732011-02-08 05:20:59 +00005880 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005881 log->PutCString("Process::RunThreadPlan(): thread plan is done");
5882 return_value = eExecutionCompleted;
5883 }
5884 else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
5885 {
5886 if (log)
5887 log->PutCString("Process::RunThreadPlan(): thread plan was discarded");
5888 return_value = eExecutionDiscarded;
5889 }
5890 else
5891 {
5892 if (log)
5893 log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005894 if (options.DoesUnwindOnError() && thread_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005895 {
5896 if (log)
Jim Ingham184e9812013-01-15 02:47:48 +00005897 log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause unwind_on_error is set.");
Sean Callanana46ec452012-07-11 21:31:24 +00005898 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5899 thread_plan_sp->SetPrivate (orig_plan_private);
5900 }
5901 }
5902 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005903
Sean Callanana46ec452012-07-11 21:31:24 +00005904 // Thread we ran the function in may have gone away because we ran the target
5905 // Check that it's still there, and if it is put it back in the context. Also restore the
5906 // frame in the context if it is still present.
5907 thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get();
5908 if (thread)
5909 {
5910 exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id));
5911 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005912
Sean Callanana46ec452012-07-11 21:31:24 +00005913 // Also restore the current process'es selected frame & thread, since this function calling may
5914 // be done behind the user's back.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005915
Sean Callanana46ec452012-07-11 21:31:24 +00005916 if (selected_tid != LLDB_INVALID_THREAD_ID)
5917 {
5918 if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid())
5919 {
5920 // We were able to restore the selected thread, now restore the frame:
Daniel Maleaa012d3a2013-07-31 20:21:20 +00005921 Mutex::Locker lock(GetThreadList().GetMutex());
Jason Molendab57e4a12013-11-04 09:33:30 +00005922 StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id);
Sean Callanana46ec452012-07-11 21:31:24 +00005923 if (old_frame_sp)
5924 GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get());
Jim Inghamf48169b2010-11-30 02:22:11 +00005925 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005926 }
5927 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005928
Sean Callanana46ec452012-07-11 21:31:24 +00005929 // If the process exited during the run of the thread plan, notify everyone.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005930
Sean Callanana46ec452012-07-11 21:31:24 +00005931 if (event_to_broadcast_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00005932 {
Sean Callanana46ec452012-07-11 21:31:24 +00005933 if (log)
5934 log->PutCString("Process::RunThreadPlan(): rebroadcasting event.");
5935 BroadcastEvent(event_to_broadcast_sp);
Jim Inghamf48169b2010-11-30 02:22:11 +00005936 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005937
Jim Inghamf48169b2010-11-30 02:22:11 +00005938 return return_value;
5939}
5940
5941const char *
Jim Ingham1624a2d2014-05-05 02:26:40 +00005942Process::ExecutionResultAsCString (ExpressionResults result)
Jim Inghamf48169b2010-11-30 02:22:11 +00005943{
5944 const char *result_name;
5945
5946 switch (result)
5947 {
Greg Claytone0d378b2011-03-24 21:19:54 +00005948 case eExecutionCompleted:
Jim Inghamf48169b2010-11-30 02:22:11 +00005949 result_name = "eExecutionCompleted";
5950 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00005951 case eExecutionDiscarded:
Jim Inghamf48169b2010-11-30 02:22:11 +00005952 result_name = "eExecutionDiscarded";
5953 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00005954 case eExecutionInterrupted:
Jim Inghamf48169b2010-11-30 02:22:11 +00005955 result_name = "eExecutionInterrupted";
5956 break;
Jim Ingham184e9812013-01-15 02:47:48 +00005957 case eExecutionHitBreakpoint:
5958 result_name = "eExecutionHitBreakpoint";
5959 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00005960 case eExecutionSetupError:
Jim Inghamf48169b2010-11-30 02:22:11 +00005961 result_name = "eExecutionSetupError";
5962 break;
Jim Ingham1624a2d2014-05-05 02:26:40 +00005963 case eExecutionParseError:
5964 result_name = "eExecutionParseError";
5965 break;
5966 case eExecutionResultUnavailable:
5967 result_name = "eExecutionResultUnavailable";
5968 break;
Greg Claytone0d378b2011-03-24 21:19:54 +00005969 case eExecutionTimedOut:
Jim Inghamf48169b2010-11-30 02:22:11 +00005970 result_name = "eExecutionTimedOut";
5971 break;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005972 case eExecutionStoppedForDebug:
5973 result_name = "eExecutionStoppedForDebug";
5974 break;
Jim Inghamf48169b2010-11-30 02:22:11 +00005975 }
5976 return result_name;
5977}
5978
Greg Clayton7260f622011-04-18 08:33:37 +00005979void
5980Process::GetStatus (Stream &strm)
5981{
5982 const StateType state = GetState();
Greg Clayton2637f822011-11-17 01:23:07 +00005983 if (StateIsStoppedState(state, false))
Greg Clayton7260f622011-04-18 08:33:37 +00005984 {
5985 if (state == eStateExited)
5986 {
5987 int exit_status = GetExitStatus();
5988 const char *exit_description = GetExitDescription();
Daniel Malead01b2952012-11-29 21:49:15 +00005989 strm.Printf ("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n",
Greg Clayton7260f622011-04-18 08:33:37 +00005990 GetID(),
5991 exit_status,
5992 exit_status,
5993 exit_description ? exit_description : "");
5994 }
5995 else
5996 {
5997 if (state == eStateConnected)
5998 strm.Printf ("Connected to remote target.\n");
5999 else
Daniel Malead01b2952012-11-29 21:49:15 +00006000 strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state));
Greg Clayton7260f622011-04-18 08:33:37 +00006001 }
6002 }
6003 else
6004 {
Daniel Malead01b2952012-11-29 21:49:15 +00006005 strm.Printf ("Process %" PRIu64 " is running.\n", GetID());
Greg Clayton7260f622011-04-18 08:33:37 +00006006 }
6007}
6008
6009size_t
6010Process::GetThreadStatus (Stream &strm,
6011 bool only_threads_with_stop_reason,
6012 uint32_t start_frame,
6013 uint32_t num_frames,
6014 uint32_t num_frames_with_source)
6015{
6016 size_t num_thread_infos_dumped = 0;
6017
Jim Ingham4a65fb12014-03-07 11:20:03 +00006018 // You can't hold the thread list lock while calling Thread::GetStatus. That very well might run code (e.g. if we need it
6019 // to get return values or arguments.) For that to work the process has to be able to acquire it. So instead copy the thread
6020 // ID's, and look them up one by one:
6021
6022 uint32_t num_threads;
6023 std::vector<uint32_t> thread_index_array;
6024 //Scope for thread list locker;
6025 {
6026 Mutex::Locker locker (GetThreadList().GetMutex());
6027 ThreadList &curr_thread_list = GetThreadList();
6028 num_threads = curr_thread_list.GetSize();
6029 uint32_t idx;
6030 thread_index_array.resize(num_threads);
6031 for (idx = 0; idx < num_threads; ++idx)
6032 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID();
6033 }
6034
Greg Clayton7260f622011-04-18 08:33:37 +00006035 for (uint32_t i = 0; i < num_threads; i++)
6036 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00006037 ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_index_array[i]));
6038 if (thread_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00006039 {
6040 if (only_threads_with_stop_reason)
6041 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00006042 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
Jim Ingham5d88a062012-10-16 00:09:33 +00006043 if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid())
Greg Clayton7260f622011-04-18 08:33:37 +00006044 continue;
6045 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00006046 thread_sp->GetStatus (strm,
Greg Clayton7260f622011-04-18 08:33:37 +00006047 start_frame,
6048 num_frames,
6049 num_frames_with_source);
6050 ++num_thread_infos_dumped;
6051 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00006052 else
6053 {
6054 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
6055 if (log)
6056 log->Printf("Process::GetThreadStatus - thread 0x" PRIu64 " vanished while running Thread::GetStatus.");
6057
6058 }
Greg Clayton7260f622011-04-18 08:33:37 +00006059 }
6060 return num_thread_infos_dumped;
6061}
6062
Greg Claytona9f40ad2012-02-22 04:37:26 +00006063void
6064Process::AddInvalidMemoryRegion (const LoadRange &region)
6065{
6066 m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize());
6067}
6068
6069bool
6070Process::RemoveInvalidMemoryRange (const LoadRange &region)
6071{
6072 return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize());
6073}
6074
Jim Ingham372787f2012-04-07 00:00:41 +00006075void
6076Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton)
6077{
6078 m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton));
6079}
6080
6081bool
6082Process::RunPreResumeActions ()
6083{
6084 bool result = true;
6085 while (!m_pre_resume_actions.empty())
6086 {
6087 struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back();
6088 m_pre_resume_actions.pop_back();
6089 bool this_result = action.callback (action.baton);
6090 if (result == true) result = this_result;
6091 }
6092 return result;
6093}
6094
6095void
6096Process::ClearPreResumeActions ()
6097{
6098 m_pre_resume_actions.clear();
6099}
Greg Claytona9f40ad2012-02-22 04:37:26 +00006100
Greg Claytonfa559e52012-05-18 02:38:05 +00006101void
6102Process::Flush ()
6103{
6104 m_thread_list.Flush();
Jason Molenda5e8dce42013-12-13 00:29:16 +00006105 m_extended_thread_list.Flush();
6106 m_extended_thread_stop_id = 0;
6107 m_queue_list.Clear();
6108 m_queue_list_stop_id = 0;
Greg Claytonfa559e52012-05-18 02:38:05 +00006109}
Greg Clayton90ba8112012-12-05 00:16:59 +00006110
6111void
6112Process::DidExec ()
6113{
6114 Target &target = GetTarget();
6115 target.CleanupProcess ();
Greg Claytonb35db632013-11-09 00:03:31 +00006116 target.ClearModules(false);
Greg Clayton90ba8112012-12-05 00:16:59 +00006117 m_dynamic_checkers_ap.reset();
6118 m_abi_sp.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00006119 m_system_runtime_ap.reset();
Greg Clayton90ba8112012-12-05 00:16:59 +00006120 m_os_ap.reset();
Greg Clayton15fc2be2013-05-21 01:00:52 +00006121 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00006122 m_jit_loaders_ap.reset();
Greg Clayton90ba8112012-12-05 00:16:59 +00006123 m_image_tokens.clear();
6124 m_allocated_memory_cache.Clear();
6125 m_language_runtimes.clear();
Greg Clayton15fc2be2013-05-21 01:00:52 +00006126 m_thread_list.DiscardThreadPlans();
Greg Clayton15fc2be2013-05-21 01:00:52 +00006127 m_memory_cache.Clear(true);
Greg Clayton90ba8112012-12-05 00:16:59 +00006128 DoDidExec();
6129 CompleteAttach ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00006130 // Flush the process (threads and all stack frames) after running CompleteAttach()
6131 // in case the dynamic loader loaded things in new locations.
6132 Flush();
Greg Claytonb35db632013-11-09 00:03:31 +00006133
6134 // After we figure out what was loaded/unloaded in CompleteAttach,
6135 // we need to let the target know so it can do any cleanup it needs to.
6136 target.DidExec();
Greg Clayton90ba8112012-12-05 00:16:59 +00006137}
Greg Clayton095eeaa2013-11-05 23:28:00 +00006138
Jim Ingham1460e4b2014-01-10 23:46:59 +00006139addr_t
6140Process::ResolveIndirectFunction(const Address *address, Error &error)
6141{
6142 if (address == nullptr)
6143 {
Jean-Daniel Dupasef37711f2014-02-08 20:22:05 +00006144 error.SetErrorString("Invalid address argument");
Jim Ingham1460e4b2014-01-10 23:46:59 +00006145 return LLDB_INVALID_ADDRESS;
6146 }
6147
6148 addr_t function_addr = LLDB_INVALID_ADDRESS;
6149
6150 addr_t addr = address->GetLoadAddress(&GetTarget());
6151 std::map<addr_t,addr_t>::const_iterator iter = m_resolved_indirect_addresses.find(addr);
6152 if (iter != m_resolved_indirect_addresses.end())
6153 {
6154 function_addr = (*iter).second;
6155 }
6156 else
6157 {
6158 if (!InferiorCall(this, address, function_addr))
6159 {
6160 Symbol *symbol = address->CalculateSymbolContextSymbol();
6161 error.SetErrorStringWithFormat ("Unable to call resolver for indirect function %s",
6162 symbol ? symbol->GetName().AsCString() : "<UNKNOWN>");
6163 function_addr = LLDB_INVALID_ADDRESS;
6164 }
6165 else
6166 {
6167 m_resolved_indirect_addresses.insert(std::pair<addr_t, addr_t>(addr, function_addr));
6168 }
6169 }
6170 return function_addr;
6171}
6172
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00006173void
6174Process::ModulesDidLoad (ModuleList &module_list)
6175{
6176 SystemRuntime *sys_runtime = GetSystemRuntime();
6177 if (sys_runtime)
6178 {
6179 sys_runtime->ModulesDidLoad (module_list);
6180 }
6181
6182 GetJITLoaders().ModulesDidLoad (module_list);
6183}