blob: a1049787d82105d0efa393115b328a1302323c46 [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 Clayton100eb932014-07-02 21:10:39 +000030#include "lldb/Host/Pipe.h"
Greg Clayton44d93782014-01-27 23:43:24 +000031#include "lldb/Host/Terminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/ABI.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000033#include "lldb/Target/DynamicLoader.h"
Andrew MacPherson17220c12014-03-05 10:12:43 +000034#include "lldb/Target/JITLoader.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000035#include "lldb/Target/OperatingSystem.h"
Jim Ingham22777012010-09-23 02:01:19 +000036#include "lldb/Target/LanguageRuntime.h"
37#include "lldb/Target/CPPLanguageRuntime.h"
38#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytone996fd32011-03-08 22:40:15 +000039#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000041#include "lldb/Target/StopInfo.h"
Jason Molendaeef51062013-11-05 03:57:19 +000042#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Target/Target.h"
44#include "lldb/Target/TargetList.h"
45#include "lldb/Target/Thread.h"
46#include "lldb/Target/ThreadPlan.h"
Jim Ingham076b3042012-04-10 01:21:57 +000047#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham1460e4b2014-01-10 23:46:59 +000048#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
50using namespace lldb;
51using namespace lldb_private;
52
Greg Clayton67cc0632012-08-22 17:17:09 +000053
54// Comment out line below to disable memory caching, overriding the process setting
55// target.process.disable-memory-cache
56#define ENABLE_MEMORY_CACHING
57
58#ifdef ENABLE_MEMORY_CACHING
59#define DISABLE_MEM_CACHE_DEFAULT false
60#else
61#define DISABLE_MEM_CACHE_DEFAULT true
62#endif
63
64class ProcessOptionValueProperties : public OptionValueProperties
65{
66public:
67 ProcessOptionValueProperties (const ConstString &name) :
68 OptionValueProperties (name)
69 {
70 }
71
72 // This constructor is used when creating ProcessOptionValueProperties when it
73 // is part of a new lldb_private::Process instance. It will copy all current
74 // global property values as needed
75 ProcessOptionValueProperties (ProcessProperties *global_properties) :
76 OptionValueProperties(*global_properties->GetValueProperties())
77 {
78 }
79
80 virtual const Property *
81 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
82 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000083 // When getting the value for a key from the process options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +000084 // try and grab the setting from the current process if there is one. Else we just
85 // use the one from this instance.
86 if (exe_ctx)
87 {
88 Process *process = exe_ctx->GetProcessPtr();
89 if (process)
90 {
91 ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get());
92 if (this != instance_properties)
93 return instance_properties->ProtectedGetPropertyAtIndex (idx);
94 }
95 }
96 return ProtectedGetPropertyAtIndex (idx);
97 }
98};
99
100static PropertyDefinition
101g_properties[] =
102{
103 { "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 +0000104 { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. "
105 "For instance, to turn on debugserver logging set this to \"QSetLogging:bitmask=LOG_DEFAULT;\"" },
Jim Inghamafc1b122013-01-31 19:48:57 +0000106 { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." },
107 { "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 +0000108 { "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 +0000109 { "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 +0000110 { "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 +0000111 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
112};
113
114enum {
115 ePropertyDisableMemCache,
Greg Claytonc9d645d2012-10-18 22:40:37 +0000116 ePropertyExtraStartCommand,
Jim Ingham184e9812013-01-15 02:47:48 +0000117 ePropertyIgnoreBreakpointsInExpressions,
118 ePropertyUnwindOnErrorInExpressions,
Jim Ingham29950772013-01-26 02:19:28 +0000119 ePropertyPythonOSPluginPath,
Jim Inghamacff8952013-05-02 00:27:30 +0000120 ePropertyStopOnSharedLibraryEvents,
121 ePropertyDetachKeepsStopped
Greg Clayton67cc0632012-08-22 17:17:09 +0000122};
123
124ProcessProperties::ProcessProperties (bool is_global) :
125 Properties ()
126{
127 if (is_global)
128 {
129 m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process")));
130 m_collection_sp->Initialize(g_properties);
131 m_collection_sp->AppendProperty(ConstString("thread"),
Jim Ingham29950772013-01-26 02:19:28 +0000132 ConstString("Settings specific to threads."),
Greg Clayton67cc0632012-08-22 17:17:09 +0000133 true,
134 Thread::GetGlobalProperties()->GetValueProperties());
135 }
136 else
137 m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get()));
138}
139
140ProcessProperties::~ProcessProperties()
141{
142}
143
144bool
145ProcessProperties::GetDisableMemoryCache() const
146{
147 const uint32_t idx = ePropertyDisableMemCache;
148 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
149}
150
151Args
152ProcessProperties::GetExtraStartupCommands () const
153{
154 Args args;
155 const uint32_t idx = ePropertyExtraStartCommand;
156 m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args);
157 return args;
158}
159
160void
161ProcessProperties::SetExtraStartupCommands (const Args &args)
162{
163 const uint32_t idx = ePropertyExtraStartCommand;
164 m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args);
165}
166
Greg Claytonc9d645d2012-10-18 22:40:37 +0000167FileSpec
168ProcessProperties::GetPythonOSPluginPath () const
169{
170 const uint32_t idx = ePropertyPythonOSPluginPath;
171 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
172}
173
174void
175ProcessProperties::SetPythonOSPluginPath (const FileSpec &file)
176{
177 const uint32_t idx = ePropertyPythonOSPluginPath;
178 m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file);
179}
180
Jim Ingham184e9812013-01-15 02:47:48 +0000181
182bool
183ProcessProperties::GetIgnoreBreakpointsInExpressions () const
184{
185 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
186 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
187}
188
189void
190ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore)
191{
192 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
193 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
194}
195
196bool
197ProcessProperties::GetUnwindOnErrorInExpressions () const
198{
199 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
200 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
201}
202
203void
204ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore)
205{
206 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
207 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
208}
209
Jim Ingham29950772013-01-26 02:19:28 +0000210bool
211ProcessProperties::GetStopOnSharedLibraryEvents () const
212{
213 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
214 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
215}
216
217void
218ProcessProperties::SetStopOnSharedLibraryEvents (bool stop)
219{
220 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
221 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
222}
223
Jim Inghamacff8952013-05-02 00:27:30 +0000224bool
225ProcessProperties::GetDetachKeepsStopped () const
226{
227 const uint32_t idx = ePropertyDetachKeepsStopped;
228 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
229}
230
231void
232ProcessProperties::SetDetachKeepsStopped (bool stop)
233{
234 const uint32_t idx = ePropertyDetachKeepsStopped;
235 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
236}
237
Greg Clayton32e0a752011-03-30 18:16:51 +0000238void
Greg Clayton8b82f082011-04-12 05:54:46 +0000239ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000240{
241 const char *cstr;
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000242 if (m_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000243 s.Printf (" pid = %" PRIu64 "\n", m_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000244
245 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000246 s.Printf (" parent = %" PRIu64 "\n", m_parent_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000247
248 if (m_executable)
249 {
250 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
251 s.PutCString (" file = ");
252 m_executable.Dump(&s);
253 s.EOL();
254 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000255 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000256 if (argc > 0)
257 {
258 for (uint32_t i=0; i<argc; i++)
259 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000260 const char *arg = m_arguments.GetArgumentAtIndex(i);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000261 if (i < 10)
Greg Clayton8b82f082011-04-12 05:54:46 +0000262 s.Printf (" arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000263 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000264 s.Printf ("arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000265 }
266 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000267
268 const uint32_t envc = m_environment.GetArgumentCount();
269 if (envc > 0)
270 {
271 for (uint32_t i=0; i<envc; i++)
272 {
273 const char *env = m_environment.GetArgumentAtIndex(i);
274 if (i < 10)
275 s.Printf (" env[%u] = %s\n", i, env);
276 else
277 s.Printf ("env[%u] = %s\n", i, env);
278 }
279 }
280
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000281 if (m_arch.IsValid())
282 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
283
Greg Clayton8b82f082011-04-12 05:54:46 +0000284 if (m_uid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000285 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000286 cstr = platform->GetUserName (m_uid);
287 s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000288 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000289 if (m_gid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000290 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000291 cstr = platform->GetGroupName (m_gid);
292 s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000293 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000294 if (m_euid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000295 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000296 cstr = platform->GetUserName (m_euid);
297 s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000298 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000299 if (m_egid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000300 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000301 cstr = platform->GetGroupName (m_egid);
302 s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000303 }
304}
305
306void
Greg Clayton8b82f082011-04-12 05:54:46 +0000307ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose)
Greg Clayton32e0a752011-03-30 18:16:51 +0000308{
Greg Clayton8b82f082011-04-12 05:54:46 +0000309 const char *label;
310 if (show_args || verbose)
311 label = "ARGUMENTS";
312 else
313 label = "NAME";
314
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000315 if (verbose)
316 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000317 s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000318 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
319 }
320 else
321 {
Jim Ingham368ac222014-08-15 17:05:27 +0000322 s.Printf ("PID PARENT USER TRIPLE %s\n", label);
323 s.PutCString ("====== ====== ========== ======================== ============================\n");
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000324 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000325}
326
327void
Greg Clayton8b82f082011-04-12 05:54:46 +0000328ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000329{
330 if (m_pid != LLDB_INVALID_PROCESS_ID)
331 {
332 const char *cstr;
Daniel Malead01b2952012-11-29 21:49:15 +0000333 s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000334
Greg Clayton32e0a752011-03-30 18:16:51 +0000335
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000336 if (verbose)
337 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000338 cstr = platform->GetUserName (m_uid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000339 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
340 s.Printf ("%-10s ", cstr);
341 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000342 s.Printf ("%-10u ", m_uid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000343
Greg Clayton8b82f082011-04-12 05:54:46 +0000344 cstr = platform->GetGroupName (m_gid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000345 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
346 s.Printf ("%-10s ", cstr);
347 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000348 s.Printf ("%-10u ", m_gid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000349
Greg Clayton8b82f082011-04-12 05:54:46 +0000350 cstr = platform->GetUserName (m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000351 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
352 s.Printf ("%-10s ", cstr);
353 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000354 s.Printf ("%-10u ", m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000355
Greg Clayton8b82f082011-04-12 05:54:46 +0000356 cstr = platform->GetGroupName (m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000357 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
358 s.Printf ("%-10s ", cstr);
359 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000360 s.Printf ("%-10u ", m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000361 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
362 }
363 else
364 {
Jim Ingham368ac222014-08-15 17:05:27 +0000365 s.Printf ("%-10s %-24s ",
Greg Clayton8b82f082011-04-12 05:54:46 +0000366 platform->GetUserName (m_euid),
Jim Ingham368ac222014-08-15 17:05:27 +0000367 m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000368 }
369
Greg Clayton8b82f082011-04-12 05:54:46 +0000370 if (verbose || show_args)
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000371 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000372 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000373 if (argc > 0)
374 {
375 for (uint32_t i=0; i<argc; i++)
376 {
377 if (i > 0)
378 s.PutChar (' ');
Greg Clayton8b82f082011-04-12 05:54:46 +0000379 s.PutCString (m_arguments.GetArgumentAtIndex(i));
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000380 }
381 }
382 }
383 else
384 {
385 s.PutCString (GetName());
386 }
387
388 s.EOL();
Greg Clayton32e0a752011-03-30 18:16:51 +0000389 }
390}
391
Greg Clayton8b82f082011-04-12 05:54:46 +0000392Error
Greg Claytonf6b8b582011-04-13 00:18:08 +0000393ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Clayton8b82f082011-04-12 05:54:46 +0000394{
395 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000396 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton8b82f082011-04-12 05:54:46 +0000397
398 switch (short_option)
399 {
400 case 's': // Stop at program entry point
401 launch_info.GetFlags().Set (eLaunchFlagStopAtEntry);
402 break;
403
Greg Clayton8b82f082011-04-12 05:54:46 +0000404 case 'i': // STDIN for read only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000405 {
406 FileAction action;
407 if (action.Open (STDIN_FILENO, option_arg, true, false))
408 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000409 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000410 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000411
412 case 'o': // Open STDOUT for write only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000413 {
414 FileAction action;
415 if (action.Open (STDOUT_FILENO, option_arg, false, true))
416 launch_info.AppendFileAction (action);
Greg Clayton9845a8d2012-03-06 04:01:04 +0000417 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000418 }
Greg Clayton9845a8d2012-03-06 04:01:04 +0000419
420 case 'e': // STDERR for write only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000421 {
422 FileAction action;
423 if (action.Open (STDERR_FILENO, option_arg, false, true))
424 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000425 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000426 }
Greg Clayton9845a8d2012-03-06 04:01:04 +0000427
Greg Clayton8b82f082011-04-12 05:54:46 +0000428 case 'p': // Process plug-in name
429 launch_info.SetProcessPluginName (option_arg);
430 break;
431
432 case 'n': // Disable STDIO
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000433 {
434 FileAction action;
435 if (action.Open (STDIN_FILENO, "/dev/null", true, false))
436 launch_info.AppendFileAction (action);
437 if (action.Open (STDOUT_FILENO, "/dev/null", false, true))
438 launch_info.AppendFileAction (action);
439 if (action.Open (STDERR_FILENO, "/dev/null", false, true))
440 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000441 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000442 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000443
444 case 'w':
445 launch_info.SetWorkingDirectory (option_arg);
446 break;
447
448 case 't': // Open process in new terminal window
449 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
450 break;
451
452 case 'a':
Greg Clayton70512312012-05-08 01:45:38 +0000453 if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get()))
454 launch_info.GetArchitecture().SetTriple (option_arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000455 break;
456
Todd Fiala51637922014-08-19 17:40:43 +0000457 case 'A': // Disable ASLR.
458 {
459 bool success;
460 const bool disable_aslr_arg = Args::StringToBoolean (option_arg, true, &success);
461 if (success)
462 disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo;
463 else
464 error.SetErrorStringWithFormat ("Invalid boolean value for disable-aslr option: '%s'", option_arg ? option_arg : "<null>");
Greg Clayton8b82f082011-04-12 05:54:46 +0000465 break;
Todd Fiala51637922014-08-19 17:40:43 +0000466 }
467
468 case 'c':
Greg Clayton144f3a92011-11-15 03:53:30 +0000469 if (option_arg && option_arg[0])
470 launch_info.SetShell (option_arg);
471 else
Ed Masteb8ca4a22013-09-03 23:04:53 +0000472 launch_info.SetShell (LLDB_DEFAULT_SHELL);
Greg Clayton982c9762011-11-03 21:22:33 +0000473 break;
474
Greg Clayton8b82f082011-04-12 05:54:46 +0000475 case 'v':
476 launch_info.GetEnvironmentEntries().AppendArgument(option_arg);
477 break;
478
479 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000480 error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option);
Greg Clayton8b82f082011-04-12 05:54:46 +0000481 break;
Greg Clayton8b82f082011-04-12 05:54:46 +0000482 }
483 return error;
484}
485
486OptionDefinition
487ProcessLaunchCommandOptions::g_option_table[] =
488{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000489{ LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."},
Todd Fiala51637922014-08-19 17:40:43 +0000490{ LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Set whether to disable address space layout randomization when launching a process."},
Zachary Turnerd37221d2014-07-09 16:31:49 +0000491{ LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
492{ LLDB_OPT_SET_ALL, false, "working-dir", 'w', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."},
493{ LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."},
494{ LLDB_OPT_SET_ALL, false, "environment", 'v', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeNone, "Specify an environment variable name/value string (--environment NAME=VALUE). Can be specified multiple times for subsequent environment entries."},
495{ LLDB_OPT_SET_ALL, false, "shell", 'c', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeFilename, "Run the process in a shell (not supported on all platforms)."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000496
Zachary Turnerd37221d2014-07-09 16:31:49 +0000497{ LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."},
498{ LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."},
499{ LLDB_OPT_SET_1 , false, "stderr", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stderr for the process to <filename>."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000500
Zachary Turnerd37221d2014-07-09 16:31:49 +0000501{ LLDB_OPT_SET_2 , false, "tty", 't', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Start the process in a terminal (not supported on all platforms)."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000502
Zachary Turnerd37221d2014-07-09 16:31:49 +0000503{ LLDB_OPT_SET_3 , false, "no-stdio", 'n', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."},
Greg Clayton8b82f082011-04-12 05:54:46 +0000504
Zachary Turnerd37221d2014-07-09 16:31:49 +0000505{ 0 , false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Clayton8b82f082011-04-12 05:54:46 +0000506};
507
508
509
510bool
511ProcessInstanceInfoMatch::NameMatches (const char *process_name) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000512{
513 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
514 return true;
515 const char *match_name = m_match_info.GetName();
516 if (!match_name)
517 return true;
518
519 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
520}
521
522bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000523ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000524{
525 if (!NameMatches (proc_info.GetName()))
526 return false;
527
528 if (m_match_info.ProcessIDIsValid() &&
529 m_match_info.GetProcessID() != proc_info.GetProcessID())
530 return false;
531
532 if (m_match_info.ParentProcessIDIsValid() &&
533 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
534 return false;
535
Greg Clayton8b82f082011-04-12 05:54:46 +0000536 if (m_match_info.UserIDIsValid () &&
537 m_match_info.GetUserID() != proc_info.GetUserID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000538 return false;
539
Greg Clayton8b82f082011-04-12 05:54:46 +0000540 if (m_match_info.GroupIDIsValid () &&
541 m_match_info.GetGroupID() != proc_info.GetGroupID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000542 return false;
543
544 if (m_match_info.EffectiveUserIDIsValid () &&
545 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
546 return false;
547
548 if (m_match_info.EffectiveGroupIDIsValid () &&
549 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
550 return false;
551
552 if (m_match_info.GetArchitecture().IsValid() &&
Sean Callananbf4b7be2012-12-13 22:07:14 +0000553 !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture()))
Greg Clayton32e0a752011-03-30 18:16:51 +0000554 return false;
555 return true;
556}
557
558bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000559ProcessInstanceInfoMatch::MatchAllProcesses () const
Greg Clayton32e0a752011-03-30 18:16:51 +0000560{
561 if (m_name_match_type != eNameMatchIgnore)
562 return false;
563
564 if (m_match_info.ProcessIDIsValid())
565 return false;
566
567 if (m_match_info.ParentProcessIDIsValid())
568 return false;
569
Greg Clayton8b82f082011-04-12 05:54:46 +0000570 if (m_match_info.UserIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000571 return false;
572
Greg Clayton8b82f082011-04-12 05:54:46 +0000573 if (m_match_info.GroupIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000574 return false;
575
576 if (m_match_info.EffectiveUserIDIsValid ())
577 return false;
578
579 if (m_match_info.EffectiveGroupIDIsValid ())
580 return false;
581
582 if (m_match_info.GetArchitecture().IsValid())
583 return false;
584
585 if (m_match_all_users)
586 return false;
587
588 return true;
589
590}
591
592void
Greg Clayton8b82f082011-04-12 05:54:46 +0000593ProcessInstanceInfoMatch::Clear()
Greg Clayton32e0a752011-03-30 18:16:51 +0000594{
595 m_match_info.Clear();
596 m_name_match_type = eNameMatchIgnore;
597 m_match_all_users = false;
598}
Greg Clayton58be07b2011-01-07 06:08:19 +0000599
Greg Claytonc3776bf2012-02-09 06:16:32 +0000600ProcessSP
601Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602{
Greg Clayton949e8222013-01-16 17:29:04 +0000603 static uint32_t g_process_unique_id = 0;
604
Greg Claytonc3776bf2012-02-09 06:16:32 +0000605 ProcessSP process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 ProcessCreateInstance create_callback = NULL;
607 if (plugin_name)
608 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000609 ConstString const_plugin_name(plugin_name);
610 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 if (create_callback)
612 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000613 process_sp = create_callback(target, listener, crash_file_path);
614 if (process_sp)
615 {
Greg Clayton949e8222013-01-16 17:29:04 +0000616 if (process_sp->CanDebug(target, true))
617 {
618 process_sp->m_process_unique_id = ++g_process_unique_id;
619 }
620 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000621 process_sp.reset();
622 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 }
624 }
625 else
626 {
Greg Claytonc982c762010-07-09 20:39:50 +0000627 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000629 process_sp = create_callback(target, listener, crash_file_path);
630 if (process_sp)
631 {
Greg Clayton949e8222013-01-16 17:29:04 +0000632 if (process_sp->CanDebug(target, false))
633 {
634 process_sp->m_process_unique_id = ++g_process_unique_id;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000635 break;
Greg Clayton949e8222013-01-16 17:29:04 +0000636 }
637 else
638 process_sp.reset();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000639 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 }
641 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000642 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643}
644
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000645ConstString &
646Process::GetStaticBroadcasterClass ()
647{
648 static ConstString class_name ("lldb.process");
649 return class_name;
650}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651
652//----------------------------------------------------------------------
653// Process constructor
654//----------------------------------------------------------------------
655Process::Process(Target &target, Listener &listener) :
Todd Fiala4ceced32014-08-29 17:35:57 +0000656 Process(target, listener, Host::GetUnixSignals ())
657{
658 // This constructor just delegates to the full Process constructor,
659 // defaulting to using the Host's UnixSignals.
660}
661
662Process::Process(Target &target, Listener &listener, const UnixSignalsSP &unix_signals_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000663 ProcessProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 UserID (LLDB_INVALID_PROCESS_ID),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000665 Broadcaster (&(target.GetDebugger()), "lldb.process"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 m_target (target),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 m_public_state (eStateUnloaded),
668 m_private_state (eStateUnloaded),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000669 m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"),
670 m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671 m_private_state_listener ("lldb.process.internal_state_listener"),
672 m_private_state_control_wait(),
673 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
Jim Ingham4b536182011-08-09 02:12:22 +0000674 m_mod_id (),
Greg Clayton949e8222013-01-16 17:29:04 +0000675 m_process_unique_id(0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676 m_thread_index_id (0),
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000677 m_thread_id_to_index_id_map (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678 m_exit_status (-1),
679 m_exit_string (),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000680 m_thread_mutex (Mutex::eMutexTypeRecursive),
681 m_thread_list_real (this),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 m_thread_list (this),
Jason Molenda864f1cc2013-11-11 05:20:44 +0000683 m_extended_thread_list (this),
Jason Molenda4ff13262013-11-20 00:31:38 +0000684 m_extended_thread_stop_id (0),
Jason Molenda5e8dce42013-12-13 00:29:16 +0000685 m_queue_list (this),
686 m_queue_list_stop_id (0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 m_notifications (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000688 m_image_tokens (),
689 m_listener (listener),
690 m_breakpoint_site_list (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000691 m_dynamic_checkers_ap (),
Todd Fiala4ceced32014-08-29 17:35:57 +0000692 m_unix_signals_sp (unix_signals_sp),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000693 m_abi_sp (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +0000694 m_process_input_reader (),
Greg Clayton3e06bd92011-01-09 21:07:35 +0000695 m_stdio_communication ("process.stdio"),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000696 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton58be07b2011-01-07 06:08:19 +0000697 m_stdout_data (),
Greg Clayton93e86192011-11-13 04:45:22 +0000698 m_stderr_data (),
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000699 m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive),
700 m_profile_data (),
Todd Fialaa3b89e22014-08-12 14:33:19 +0000701 m_iohandler_sync (false),
Greg Claytond495c532011-05-17 03:37:42 +0000702 m_memory_cache (*this),
703 m_allocated_memory_cache (*this),
Greg Claytone24c4ac2011-11-17 04:46:02 +0000704 m_should_detach (false),
Sean Callanan90539452011-09-20 23:01:51 +0000705 m_next_event_action_ap(),
Greg Clayton96249852013-04-18 16:57:27 +0000706 m_public_run_lock (),
Greg Clayton96249852013-04-18 16:57:27 +0000707 m_private_run_lock (),
Jim Inghamaacc3182012-06-06 00:29:30 +0000708 m_currently_handling_event(false),
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000709 m_finalize_called(false),
Greg Claytonf9b57b92013-05-10 23:48:10 +0000710 m_clear_thread_plans_on_stop (false),
Jim Ingham1460e4b2014-01-10 23:46:59 +0000711 m_force_next_event_delivery(false),
Jim Ingham0161b492013-02-09 01:29:05 +0000712 m_last_broadcast_state (eStateInvalid),
Jason Molenda69b6b632013-03-05 03:33:59 +0000713 m_destroy_in_process (false),
714 m_can_jit(eCanJITDontKnow)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715{
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000716 CheckInWithManager ();
Caroline Tice1559a462010-09-27 00:30:10 +0000717
Greg Clayton5160ce52013-03-27 23:08:40 +0000718 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000720 log->Printf ("%p Process::Process()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721
Todd Fiala4ceced32014-08-29 17:35:57 +0000722 if (!m_unix_signals_sp)
723 m_unix_signals_sp.reset (new UnixSignals ());
724
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000725 SetEventName (eBroadcastBitStateChanged, "state-changed");
726 SetEventName (eBroadcastBitInterrupt, "interrupt");
727 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
728 SetEventName (eBroadcastBitSTDERR, "stderr-available");
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000729 SetEventName (eBroadcastBitProfileData, "profile-data-available");
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000730
Greg Clayton35a4cc52012-10-29 20:52:08 +0000731 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" );
732 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" );
733 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume");
734
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 listener.StartListeningForEvents (this,
736 eBroadcastBitStateChanged |
737 eBroadcastBitInterrupt |
738 eBroadcastBitSTDOUT |
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000739 eBroadcastBitSTDERR |
740 eBroadcastBitProfileData);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741
742 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +0000743 eBroadcastBitStateChanged |
744 eBroadcastBitInterrupt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745
746 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
747 eBroadcastInternalStateControlStop |
748 eBroadcastInternalStateControlPause |
749 eBroadcastInternalStateControlResume);
Todd Fiala4ceced32014-08-29 17:35:57 +0000750 // We need something valid here, even if just the default UnixSignalsSP.
751 assert (m_unix_signals_sp && "null m_unix_signals_sp after initialization");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752}
753
754//----------------------------------------------------------------------
755// Destructor
756//----------------------------------------------------------------------
757Process::~Process()
758{
Greg Clayton5160ce52013-03-27 23:08:40 +0000759 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000761 log->Printf ("%p Process::~Process()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762 StopPrivateStateThread();
763}
764
Greg Clayton67cc0632012-08-22 17:17:09 +0000765const ProcessPropertiesSP &
766Process::GetGlobalProperties()
767{
768 static ProcessPropertiesSP g_settings_sp;
769 if (!g_settings_sp)
770 g_settings_sp.reset (new ProcessProperties (true));
771 return g_settings_sp;
772}
773
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774void
775Process::Finalize()
776{
Greg Claytone24c4ac2011-11-17 04:46:02 +0000777 switch (GetPrivateState())
778 {
779 case eStateConnected:
780 case eStateAttaching:
781 case eStateLaunching:
782 case eStateStopped:
783 case eStateRunning:
784 case eStateStepping:
785 case eStateCrashed:
786 case eStateSuspended:
787 if (GetShouldDetach())
Jim Inghamacff8952013-05-02 00:27:30 +0000788 {
789 // FIXME: This will have to be a process setting:
790 bool keep_stopped = false;
791 Detach(keep_stopped);
792 }
Greg Claytone24c4ac2011-11-17 04:46:02 +0000793 else
794 Destroy();
795 break;
796
797 case eStateInvalid:
798 case eStateUnloaded:
799 case eStateDetached:
800 case eStateExited:
801 break;
802 }
803
Greg Clayton1ed54f52011-10-01 00:45:15 +0000804 // Clear our broadcaster before we proceed with destroying
805 Broadcaster::Clear();
806
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 // Do any cleanup needed prior to being destructed... Subclasses
808 // that override this method should call this superclass method as well.
Jim Inghamd0a3e122011-02-16 17:54:55 +0000809
810 // We need to destroy the loader before the derived Process class gets destroyed
811 // since it is very likely that undoing the loader will require access to the real process.
Greg Clayton894f82f2012-01-20 23:08:34 +0000812 m_dynamic_checkers_ap.reset();
813 m_abi_sp.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000814 m_os_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +0000815 m_system_runtime_ap.reset();
Greg Clayton894f82f2012-01-20 23:08:34 +0000816 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +0000817 m_jit_loaders_ap.reset();
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000818 m_thread_list_real.Destroy();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000819 m_thread_list.Destroy();
Jason Molenda864f1cc2013-11-11 05:20:44 +0000820 m_extended_thread_list.Destroy();
Jason Molenda5e8dce42013-12-13 00:29:16 +0000821 m_queue_list.Clear();
822 m_queue_list_stop_id = 0;
Greg Clayton894f82f2012-01-20 23:08:34 +0000823 std::vector<Notifications> empty_notifications;
824 m_notifications.swap(empty_notifications);
825 m_image_tokens.clear();
826 m_memory_cache.Clear();
827 m_allocated_memory_cache.Clear();
828 m_language_runtimes.clear();
829 m_next_event_action_ap.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000830//#ifdef LLDB_CONFIGURATION_DEBUG
831// StreamFile s(stdout, false);
832// EventSP event_sp;
833// while (m_private_state_listener.GetNextEvent(event_sp))
834// {
835// event_sp->Dump (&s);
836// s.EOL();
837// }
838//#endif
839 // We have to be very careful here as the m_private_state_listener might
840 // contain events that have ProcessSP values in them which can keep this
841 // process around forever. These events need to be cleared out.
842 m_private_state_listener.Clear();
Ed Maste64fad602013-07-29 20:58:06 +0000843 m_public_run_lock.TrySetRunning(); // This will do nothing if already locked
844 m_public_run_lock.SetStopped();
845 m_private_run_lock.TrySetRunning(); // This will do nothing if already locked
846 m_private_run_lock.SetStopped();
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000847 m_finalize_called = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848}
849
850void
851Process::RegisterNotificationCallbacks (const Notifications& callbacks)
852{
853 m_notifications.push_back(callbacks);
854 if (callbacks.initialize != NULL)
855 callbacks.initialize (callbacks.baton, this);
856}
857
858bool
859Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
860{
861 std::vector<Notifications>::iterator pos, end = m_notifications.end();
862 for (pos = m_notifications.begin(); pos != end; ++pos)
863 {
864 if (pos->baton == callbacks.baton &&
865 pos->initialize == callbacks.initialize &&
866 pos->process_state_changed == callbacks.process_state_changed)
867 {
868 m_notifications.erase(pos);
869 return true;
870 }
871 }
872 return false;
873}
874
875void
876Process::SynchronouslyNotifyStateChanged (StateType state)
877{
878 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
879 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
880 {
881 if (notification_pos->process_state_changed)
882 notification_pos->process_state_changed (notification_pos->baton, this, state);
883 }
884}
885
886// FIXME: We need to do some work on events before the general Listener sees them.
887// For instance if we are continuing from a breakpoint, we need to ensure that we do
888// the little "insert real insn, step & stop" trick. But we can't do that when the
889// event is delivered by the broadcaster - since that is done on the thread that is
890// waiting for new events, so if we needed more than one event for our handling, we would
891// stall. So instead we do it when we fetch the event off of the queue.
892//
893
894StateType
895Process::GetNextEvent (EventSP &event_sp)
896{
897 StateType state = eStateInvalid;
898
899 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
900 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
901
902 return state;
903}
904
Todd Fialaa3b89e22014-08-12 14:33:19 +0000905bool
906Process::SyncIOHandler (uint64_t timeout_msec)
907{
908 bool timed_out = false;
909
910 // don't sync (potentially context switch) in case where there is no process IO
911 if (m_process_input_reader)
912 {
913 TimeValue timeout = TimeValue::Now();
914 timeout.OffsetWithMicroSeconds(timeout_msec*1000);
915
916 m_iohandler_sync.WaitForValueEqualTo(true, &timeout, &timed_out);
917
918 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
919 if(log)
920 {
921 if(timed_out)
922 log->Printf ("Process::%s pid %" PRIu64 " (timeout=%" PRIu64 "ms): FAIL", __FUNCTION__, GetID (), timeout_msec);
923 else
924 log->Printf ("Process::%s pid %" PRIu64 ": SUCCESS", __FUNCTION__, GetID ());
925 }
926
927 // reset sync one-shot so it will be ready for next time
928 m_iohandler_sync.SetValue(false, eBroadcastNever);
929 }
930
931 return !timed_out;
932}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933
934StateType
Greg Clayton44d93782014-01-27 23:43:24 +0000935Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr, bool wait_always, Listener *hijack_listener)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936{
Jim Ingham4b536182011-08-09 02:12:22 +0000937 // We can't just wait for a "stopped" event, because the stopped event may have restarted the target.
938 // We have to actually check each event, and in the case of a stopped event check the restarted flag
939 // on the event.
Greg Clayton85fb1b92012-09-11 02:33:37 +0000940 if (event_sp_ptr)
941 event_sp_ptr->reset();
Jim Ingham4b536182011-08-09 02:12:22 +0000942 StateType state = GetState();
943 // If we are exited or detached, we won't ever get back to any
944 // other valid state...
945 if (state == eStateDetached || state == eStateExited)
946 return state;
947
Daniel Malea9e9919f2013-10-09 16:56:28 +0000948 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
949 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000950 log->Printf ("Process::%s (timeout = %p)", __FUNCTION__,
951 static_cast<const void*>(timeout));
Daniel Malea9e9919f2013-10-09 16:56:28 +0000952
953 if (!wait_always &&
954 StateIsStoppedState(state, true) &&
955 StateIsStoppedState(GetPrivateState(), true)) {
956 if (log)
957 log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.",
958 __FUNCTION__);
959 return state;
960 }
961
Jim Ingham4b536182011-08-09 02:12:22 +0000962 while (state != eStateInvalid)
963 {
Greg Clayton85fb1b92012-09-11 02:33:37 +0000964 EventSP event_sp;
Greg Clayton44d93782014-01-27 23:43:24 +0000965 state = WaitForStateChangedEvents (timeout, event_sp, hijack_listener);
Greg Clayton85fb1b92012-09-11 02:33:37 +0000966 if (event_sp_ptr && event_sp)
967 *event_sp_ptr = event_sp;
968
Jim Ingham4b536182011-08-09 02:12:22 +0000969 switch (state)
970 {
971 case eStateCrashed:
972 case eStateDetached:
973 case eStateExited:
974 case eStateUnloaded:
Greg Clayton44d93782014-01-27 23:43:24 +0000975 // We need to toggle the run lock as this won't get done in
976 // SetPublicState() if the process is hijacked.
977 if (hijack_listener)
978 m_public_run_lock.SetStopped();
Jim Ingham4b536182011-08-09 02:12:22 +0000979 return state;
980 case eStateStopped:
981 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
982 continue;
983 else
Greg Clayton44d93782014-01-27 23:43:24 +0000984 {
985 // We need to toggle the run lock as this won't get done in
986 // SetPublicState() if the process is hijacked.
987 if (hijack_listener)
988 m_public_run_lock.SetStopped();
Jim Ingham4b536182011-08-09 02:12:22 +0000989 return state;
Greg Clayton44d93782014-01-27 23:43:24 +0000990 }
Jim Ingham4b536182011-08-09 02:12:22 +0000991 default:
992 continue;
993 }
994 }
995 return state;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996}
997
998
999StateType
1000Process::WaitForState
1001(
1002 const TimeValue *timeout,
Greg Clayton44d93782014-01-27 23:43:24 +00001003 const StateType *match_states,
1004 const uint32_t num_match_states
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005)
1006{
1007 EventSP event_sp;
1008 uint32_t i;
Greg Clayton05faeb72010-10-07 04:19:01 +00001009 StateType state = GetState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010 while (state != eStateInvalid)
1011 {
Greg Clayton05faeb72010-10-07 04:19:01 +00001012 // If we are exited or detached, we won't ever get back to any
1013 // other valid state...
1014 if (state == eStateDetached || state == eStateExited)
1015 return state;
1016
Greg Clayton44d93782014-01-27 23:43:24 +00001017 state = WaitForStateChangedEvents (timeout, event_sp, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018
1019 for (i=0; i<num_match_states; ++i)
1020 {
1021 if (match_states[i] == state)
1022 return state;
1023 }
1024 }
1025 return state;
1026}
1027
Jim Ingham30f9b212010-10-11 23:53:14 +00001028bool
1029Process::HijackProcessEvents (Listener *listener)
1030{
1031 if (listener != NULL)
1032 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001033 return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham30f9b212010-10-11 23:53:14 +00001034 }
1035 else
1036 return false;
1037}
1038
1039void
1040Process::RestoreProcessEvents ()
1041{
1042 RestoreBroadcaster();
1043}
1044
Jim Ingham0f16e732011-02-08 05:20:59 +00001045bool
1046Process::HijackPrivateProcessEvents (Listener *listener)
1047{
1048 if (listener != NULL)
1049 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001050 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham0f16e732011-02-08 05:20:59 +00001051 }
1052 else
1053 return false;
1054}
1055
1056void
1057Process::RestorePrivateProcessEvents ()
1058{
1059 m_private_state_broadcaster.RestoreBroadcaster();
1060}
1061
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062StateType
Greg Clayton44d93782014-01-27 23:43:24 +00001063Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp, Listener *hijack_listener)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064{
Greg Clayton5160ce52013-03-27 23:08:40 +00001065 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066
1067 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001068 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1069 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070
Greg Clayton44d93782014-01-27 23:43:24 +00001071 Listener *listener = hijack_listener;
1072 if (listener == NULL)
1073 listener = &m_listener;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001074
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 StateType state = eStateInvalid;
Greg Clayton44d93782014-01-27 23:43:24 +00001076 if (listener->WaitForEventForBroadcasterWithType (timeout,
1077 this,
1078 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
1079 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001080 {
1081 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1082 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1083 else if (log)
1084 log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__);
1085 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086
1087 if (log)
1088 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001089 __FUNCTION__, static_cast<const void*>(timeout),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 StateAsCString(state));
1091 return state;
1092}
1093
1094Event *
1095Process::PeekAtStateChangedEvents ()
1096{
Greg Clayton5160ce52013-03-27 23:08:40 +00001097 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098
1099 if (log)
1100 log->Printf ("Process::%s...", __FUNCTION__);
1101
1102 Event *event_ptr;
Greg Clayton3fcbed62010-10-19 03:25:40 +00001103 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
1104 eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 if (log)
1106 {
1107 if (event_ptr)
1108 {
1109 log->Printf ("Process::%s (event_ptr) => %s",
1110 __FUNCTION__,
1111 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
1112 }
1113 else
1114 {
1115 log->Printf ("Process::%s no events found",
1116 __FUNCTION__);
1117 }
1118 }
1119 return event_ptr;
1120}
1121
1122StateType
1123Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
1124{
Greg Clayton5160ce52013-03-27 23:08:40 +00001125 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126
1127 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001128 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1129 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130
1131 StateType state = eStateInvalid;
Greg Clayton6779606a2011-01-22 23:43:18 +00001132 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
1133 &m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +00001134 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
Greg Clayton6779606a2011-01-22 23:43:18 +00001135 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001136 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1137 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138
1139 // This is a bit of a hack, but when we wait here we could very well return
1140 // to the command-line, and that could disable the log, which would render the
1141 // log we got above invalid.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001143 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
1144 __FUNCTION__, static_cast<const void *>(timeout),
1145 state == eStateInvalid ? "TIMEOUT" : StateAsCString(state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146 return state;
1147}
1148
1149bool
1150Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
1151{
Greg Clayton5160ce52013-03-27 23:08:40 +00001152 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153
1154 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001155 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1156 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157
1158 if (control_only)
1159 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
1160 else
1161 return m_private_state_listener.WaitForEvent(timeout, event_sp);
1162}
1163
1164bool
1165Process::IsRunning () const
1166{
1167 return StateIsRunningState (m_public_state.GetValue());
1168}
1169
1170int
1171Process::GetExitStatus ()
1172{
1173 if (m_public_state.GetValue() == eStateExited)
1174 return m_exit_status;
1175 return -1;
1176}
1177
Greg Clayton85851dd2010-12-04 00:10:17 +00001178
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179const char *
1180Process::GetExitDescription ()
1181{
1182 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
1183 return m_exit_string.c_str();
1184 return NULL;
1185}
1186
Greg Clayton6779606a2011-01-22 23:43:18 +00001187bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188Process::SetExitStatus (int status, const char *cstr)
1189{
Greg Clayton5160ce52013-03-27 23:08:40 +00001190 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Greg Clayton414f5d32011-01-25 02:58:48 +00001191 if (log)
1192 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
1193 status, status,
1194 cstr ? "\"" : "",
1195 cstr ? cstr : "NULL",
1196 cstr ? "\"" : "");
1197
Greg Clayton6779606a2011-01-22 23:43:18 +00001198 // We were already in the exited state
1199 if (m_private_state.GetValue() == eStateExited)
Greg Clayton414f5d32011-01-25 02:58:48 +00001200 {
Greg Clayton385d6032011-01-26 23:47:29 +00001201 if (log)
1202 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton6779606a2011-01-22 23:43:18 +00001203 return false;
Greg Clayton414f5d32011-01-25 02:58:48 +00001204 }
Greg Clayton6779606a2011-01-22 23:43:18 +00001205
1206 m_exit_status = status;
1207 if (cstr)
1208 m_exit_string = cstr;
1209 else
1210 m_exit_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211
Greg Clayton6779606a2011-01-22 23:43:18 +00001212 DidExit ();
Greg Clayton10177aa2010-12-08 05:08:21 +00001213
Greg Clayton6779606a2011-01-22 23:43:18 +00001214 SetPrivateState (eStateExited);
1215 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216}
1217
1218// This static callback can be used to watch for local child processes on
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001219// the current host. The child process exits, the process will be
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220// found in the global target list (we want to be completely sure that the
1221// lldb_private::Process doesn't go away before we can deliver the signal.
1222bool
Greg Claytone4e45922011-11-16 05:37:56 +00001223Process::SetProcessExitStatus (void *callback_baton,
1224 lldb::pid_t pid,
1225 bool exited,
1226 int signo, // Zero for no signal
1227 int exit_status // Exit value of process if signal is zero
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228)
1229{
Greg Clayton5160ce52013-03-27 23:08:40 +00001230 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytone4e45922011-11-16 05:37:56 +00001231 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001232 log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n",
Greg Claytone4e45922011-11-16 05:37:56 +00001233 callback_baton,
1234 pid,
1235 exited,
1236 signo,
1237 exit_status);
1238
1239 if (exited)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240 {
Greg Clayton66111032010-06-23 01:19:29 +00001241 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242 if (target_sp)
1243 {
1244 ProcessSP process_sp (target_sp->GetProcessSP());
1245 if (process_sp)
1246 {
1247 const char *signal_cstr = NULL;
1248 if (signo)
1249 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
1250
1251 process_sp->SetExitStatus (exit_status, signal_cstr);
1252 }
1253 }
1254 return true;
1255 }
1256 return false;
1257}
1258
1259
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001260void
1261Process::UpdateThreadListIfNeeded ()
1262{
1263 const uint32_t stop_id = GetStopID();
1264 if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID())
1265 {
Greg Clayton2637f822011-11-17 01:23:07 +00001266 const StateType state = GetPrivateState();
1267 if (StateIsStoppedState (state, true))
1268 {
1269 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytone24c4ac2011-11-17 04:46:02 +00001270 // m_thread_list does have its own mutex, but we need to
1271 // hold onto the mutex between the call to UpdateThreadList(...)
1272 // and the os->UpdateThreadList(...) so it doesn't change on us
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001273 ThreadList &old_thread_list = m_thread_list;
1274 ThreadList real_thread_list(this);
Greg Clayton2637f822011-11-17 01:23:07 +00001275 ThreadList new_thread_list(this);
1276 // Always update the thread list with the protocol specific
Greg Clayton9fc13552012-04-10 00:18:59 +00001277 // thread list, but only update if "true" is returned
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001278 if (UpdateThreadList (m_thread_list_real, real_thread_list))
Greg Clayton9fc13552012-04-10 00:18:59 +00001279 {
Jim Ingham09437922013-03-01 20:04:25 +00001280 // Don't call into the OperatingSystem to update the thread list if we are shutting down, since
1281 // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is
1282 // shutting us down, causing a deadlock.
1283 if (!m_destroy_in_process)
1284 {
1285 OperatingSystem *os = GetOperatingSystem ();
1286 if (os)
Greg Claytonb3ae8762013-04-12 20:07:46 +00001287 {
1288 // Clear any old backing threads where memory threads might have been
1289 // backed by actual threads from the lldb_private::Process subclass
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001290 size_t num_old_threads = old_thread_list.GetSize(false);
Greg Claytonb3ae8762013-04-12 20:07:46 +00001291 for (size_t i=0; i<num_old_threads; ++i)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001292 old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread();
Greg Claytonb3ae8762013-04-12 20:07:46 +00001293
1294 // Now let the OperatingSystem plug-in update the thread list
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001295 os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in
1296 real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass
1297 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 +00001298 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001299 else
1300 {
1301 // No OS plug-in, the new thread list is the same as the real thread list
1302 new_thread_list = real_thread_list;
1303 }
Jim Ingham09437922013-03-01 20:04:25 +00001304 }
Jim Ingham02ff8e02013-06-22 00:55:02 +00001305
1306 m_thread_list_real.Update(real_thread_list);
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001307 m_thread_list.Update (new_thread_list);
1308 m_thread_list.SetStopID (stop_id);
Jason Molendaa6e91302013-11-19 05:44:41 +00001309
Jason Molenda4ff13262013-11-20 00:31:38 +00001310 if (GetLastNaturalStopID () != m_extended_thread_stop_id)
1311 {
1312 // Clear any extended threads that we may have accumulated previously
1313 m_extended_thread_list.Clear();
1314 m_extended_thread_stop_id = GetLastNaturalStopID ();
Jason Molenda5e8dce42013-12-13 00:29:16 +00001315
1316 m_queue_list.Clear();
1317 m_queue_list_stop_id = GetLastNaturalStopID ();
Jason Molenda4ff13262013-11-20 00:31:38 +00001318 }
Greg Clayton9fc13552012-04-10 00:18:59 +00001319 }
Greg Clayton2637f822011-11-17 01:23:07 +00001320 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001321 }
1322}
1323
Jason Molenda5e8dce42013-12-13 00:29:16 +00001324void
1325Process::UpdateQueueListIfNeeded ()
1326{
1327 if (m_system_runtime_ap.get())
1328 {
1329 if (m_queue_list.GetSize() == 0 || m_queue_list_stop_id != GetLastNaturalStopID())
1330 {
1331 const StateType state = GetPrivateState();
1332 if (StateIsStoppedState (state, true))
1333 {
1334 m_system_runtime_ap->PopulateQueueList (m_queue_list);
1335 m_queue_list_stop_id = GetLastNaturalStopID();
1336 }
1337 }
1338 }
1339}
1340
Greg Claytona4d87472013-01-18 23:41:08 +00001341ThreadSP
1342Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context)
1343{
1344 OperatingSystem *os = GetOperatingSystem ();
1345 if (os)
1346 return os->CreateThread(tid, context);
1347 return ThreadSP();
1348}
1349
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001350uint32_t
1351Process::GetNextThreadIndexID (uint64_t thread_id)
1352{
1353 return AssignIndexIDToThread(thread_id);
1354}
1355
1356bool
1357Process::HasAssignedIndexIDToThread(uint64_t thread_id)
1358{
1359 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1360 if (iterator == m_thread_id_to_index_id_map.end())
1361 {
1362 return false;
1363 }
1364 else
1365 {
1366 return true;
1367 }
1368}
1369
1370uint32_t
1371Process::AssignIndexIDToThread(uint64_t thread_id)
1372{
1373 uint32_t result = 0;
1374 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1375 if (iterator == m_thread_id_to_index_id_map.end())
1376 {
1377 result = ++m_thread_index_id;
1378 m_thread_id_to_index_id_map[thread_id] = result;
1379 }
1380 else
1381 {
1382 result = iterator->second;
1383 }
1384
1385 return result;
1386}
1387
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388StateType
1389Process::GetState()
1390{
1391 // If any other threads access this we will need a mutex for it
1392 return m_public_state.GetValue ();
1393}
1394
1395void
Jim Ingham221d51c2013-05-08 00:35:16 +00001396Process::SetPublicState (StateType new_state, bool restarted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397{
Greg Clayton5160ce52013-03-27 23:08:40 +00001398 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +00001400 log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001401 const StateType old_state = m_public_state.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 m_public_state.SetValue (new_state);
Jim Ingham3b8285d2012-04-19 01:40:33 +00001403
1404 // On the transition from Run to Stopped, we unlock the writer end of the
1405 // run lock. The lock gets locked in Resume, which is the public API
1406 // to tell the program to run.
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001407 if (!IsHijackedForEvent(eBroadcastBitStateChanged))
1408 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001409 if (new_state == eStateDetached)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001410 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001411 if (log)
1412 log->Printf("Process::SetPublicState (%s) -- unlocking run lock for detach", StateAsCString(new_state));
Ed Maste64fad602013-07-29 20:58:06 +00001413 m_public_run_lock.SetStopped();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001414 }
1415 else
1416 {
1417 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1418 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
Jim Ingham221d51c2013-05-08 00:35:16 +00001419 if ((old_state_is_stopped != new_state_is_stopped))
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001420 {
Jim Ingham221d51c2013-05-08 00:35:16 +00001421 if (new_state_is_stopped && !restarted)
Sean Callanan8b0737f2012-06-02 01:16:20 +00001422 {
1423 if (log)
1424 log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state));
Ed Maste64fad602013-07-29 20:58:06 +00001425 m_public_run_lock.SetStopped();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001426 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001427 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001428 }
1429 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430}
1431
Jim Ingham3b8285d2012-04-19 01:40:33 +00001432Error
1433Process::Resume ()
1434{
Greg Clayton5160ce52013-03-27 23:08:40 +00001435 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Jim Ingham3b8285d2012-04-19 01:40:33 +00001436 if (log)
1437 log->Printf("Process::Resume -- locking run lock");
Ed Maste64fad602013-07-29 20:58:06 +00001438 if (!m_public_run_lock.TrySetRunning())
Jim Ingham3b8285d2012-04-19 01:40:33 +00001439 {
1440 Error error("Resume request failed - process still running.");
1441 if (log)
Ed Maste64fad602013-07-29 20:58:06 +00001442 log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming.");
Jim Ingham3b8285d2012-04-19 01:40:33 +00001443 return error;
1444 }
1445 return PrivateResume();
1446}
1447
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001448StateType
1449Process::GetPrivateState ()
1450{
1451 return m_private_state.GetValue();
1452}
1453
1454void
1455Process::SetPrivateState (StateType new_state)
1456{
Greg Claytonfb8b37a2014-07-14 23:09:29 +00001457 if (m_finalize_called)
1458 return;
1459
Greg Clayton5160ce52013-03-27 23:08:40 +00001460 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 bool state_changed = false;
1462
1463 if (log)
1464 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
1465
Andrew Kaylor29d65742013-05-10 17:19:04 +00001466 Mutex::Locker thread_locker(m_thread_list.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467 Mutex::Locker locker(m_private_state.GetMutex());
1468
1469 const StateType old_state = m_private_state.GetValueNoLock ();
1470 state_changed = old_state != new_state;
Ed Mastec29693f2013-07-02 16:35:47 +00001471
Greg Claytonaa49c832013-05-03 22:25:56 +00001472 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1473 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
1474 if (old_state_is_stopped != new_state_is_stopped)
1475 {
1476 if (new_state_is_stopped)
Ed Maste64fad602013-07-29 20:58:06 +00001477 m_private_run_lock.SetStopped();
Greg Claytonaa49c832013-05-03 22:25:56 +00001478 else
Ed Maste64fad602013-07-29 20:58:06 +00001479 m_private_run_lock.SetRunning();
Greg Claytonaa49c832013-05-03 22:25:56 +00001480 }
Andrew Kaylor93132f52013-05-28 23:04:25 +00001481
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001482 if (state_changed)
1483 {
1484 m_private_state.SetValueNoLock (new_state);
Greg Clayton2637f822011-11-17 01:23:07 +00001485 if (StateIsStoppedState(new_state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001486 {
Andrew Kaylor29d65742013-05-10 17:19:04 +00001487 // Note, this currently assumes that all threads in the list
1488 // stop when the process stops. In the future we will want to
1489 // support a debugging model where some threads continue to run
1490 // while others are stopped. When that happens we will either need
1491 // a way for the thread list to identify which threads are stopping
1492 // or create a special thread list containing only threads which
1493 // actually stopped.
1494 //
1495 // The process plugin is responsible for managing the actual
1496 // behavior of the threads and should have stopped any threads
1497 // that are going to stop before we get here.
1498 m_thread_list.DidStop();
1499
Jim Ingham4b536182011-08-09 02:12:22 +00001500 m_mod_id.BumpStopID();
Greg Clayton58be07b2011-01-07 06:08:19 +00001501 m_memory_cache.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502 if (log)
Jim Ingham4b536182011-08-09 02:12:22 +00001503 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504 }
1505 // Use our target to get a shared pointer to ourselves...
Greg Clayton35a4cc52012-10-29 20:52:08 +00001506 if (m_finalize_called && PrivateStateThreadIsValid() == false)
1507 BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
1508 else
1509 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510 }
1511 else
1512 {
1513 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001514 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515 }
1516}
1517
Jim Ingham0faa43f2011-11-08 03:00:11 +00001518void
1519Process::SetRunningUserExpression (bool on)
1520{
1521 m_mod_id.SetRunningUserExpression (on);
1522}
1523
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524addr_t
1525Process::GetImageInfoAddress()
1526{
1527 return LLDB_INVALID_ADDRESS;
1528}
1529
Greg Clayton8f343b02010-11-04 01:54:29 +00001530//----------------------------------------------------------------------
1531// LoadImage
1532//
1533// This function provides a default implementation that works for most
1534// unix variants. Any Process subclasses that need to do shared library
1535// loading differently should override LoadImage and UnloadImage and
1536// do what is needed.
1537//----------------------------------------------------------------------
1538uint32_t
1539Process::LoadImage (const FileSpec &image_spec, Error &error)
1540{
Greg Claytonac7a3db2012-04-18 00:05:19 +00001541 char path[PATH_MAX];
1542 image_spec.GetPath(path, sizeof(path));
1543
Greg Clayton8f343b02010-11-04 01:54:29 +00001544 DynamicLoader *loader = GetDynamicLoader();
1545 if (loader)
1546 {
1547 error = loader->CanLoadImage();
1548 if (error.Fail())
1549 return LLDB_INVALID_IMAGE_TOKEN;
1550 }
1551
1552 if (error.Success())
1553 {
1554 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001555
1556 if (thread_sp)
1557 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001558 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
Greg Clayton8f343b02010-11-04 01:54:29 +00001559
1560 if (frame_sp)
1561 {
1562 ExecutionContext exe_ctx;
1563 frame_sp->CalculateExecutionContext (exe_ctx);
Greg Clayton62afb9f2013-11-04 19:35:17 +00001564 EvaluateExpressionOptions expr_options;
1565 expr_options.SetUnwindOnError(true);
1566 expr_options.SetIgnoreBreakpoints(true);
1567 expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
Jim Ingham4ac04432014-07-19 01:09:16 +00001568 expr_options.SetResultIsInternal(true);
1569
Greg Clayton8f343b02010-11-04 01:54:29 +00001570 StreamString expr;
Jim Ingham6971b862014-07-19 00:37:06 +00001571 expr.Printf(R"(
1572 struct __lldb_dlopen_result { void *image_ptr; const char *error_str; } the_result;
1573 the_result.image_ptr = dlopen ("%s", 2);
1574 if (the_result.image_ptr == (void *) 0x0)
1575 {
1576 the_result.error_str = dlerror();
1577 }
1578 else
1579 {
1580 the_result.error_str = (const char *) 0x0;
1581 }
1582 the_result;
1583 )",
1584 path);
1585 const char *prefix = R"(
1586 extern "C" void* dlopen (const char *path, int mode);
1587 extern "C" const char *dlerror (void);
1588 )";
Jim Inghamf48169b2010-11-30 02:22:11 +00001589 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +00001590 Error expr_error;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001591 ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001592 expr_options,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001593 expr.GetData(),
1594 prefix,
1595 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001596 expr_error);
1597 if (expr_error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001598 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001599 error = result_valobj_sp->GetError();
1600 if (error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001601 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001602 Scalar scalar;
Jim Ingham6971b862014-07-19 00:37:06 +00001603 ValueObjectSP image_ptr_sp = result_valobj_sp->GetChildAtIndex(0, true);
1604 if (image_ptr_sp && image_ptr_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001605 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001606 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1607 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
1608 {
1609 uint32_t image_token = m_image_tokens.size();
1610 m_image_tokens.push_back (image_ptr);
1611 return image_token;
1612 }
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001613 else if (image_ptr == 0)
1614 {
Jim Ingham6971b862014-07-19 00:37:06 +00001615 ValueObjectSP error_str_sp = result_valobj_sp->GetChildAtIndex(1, true);
1616 if (error_str_sp)
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001617 {
Jim Ingham6971b862014-07-19 00:37:06 +00001618 if (error_str_sp->IsCStringContainer(true))
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001619 {
Jim Inghamcf973792014-07-17 21:53:48 +00001620 StreamString s;
Jim Ingham6971b862014-07-19 00:37:06 +00001621 size_t num_chars = error_str_sp->ReadPointedString (s, error);
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001622 if (error.Success() && num_chars > 0)
1623 {
1624 error.Clear();
Jim Ingham6971b862014-07-19 00:37:06 +00001625 error.SetErrorStringWithFormat("dlopen error: %s", s.GetData());
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001626 }
1627 }
1628 }
1629 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001630 }
1631 }
1632 }
Jim Ingham6c9ed912014-04-03 01:26:14 +00001633 else
1634 error = expr_error;
Greg Clayton8f343b02010-11-04 01:54:29 +00001635 }
1636 }
1637 }
Greg Claytonac7a3db2012-04-18 00:05:19 +00001638 if (!error.AsCString())
1639 error.SetErrorStringWithFormat("unable to load '%s'", path);
Greg Clayton8f343b02010-11-04 01:54:29 +00001640 return LLDB_INVALID_IMAGE_TOKEN;
1641}
1642
1643//----------------------------------------------------------------------
1644// UnloadImage
1645//
1646// This function provides a default implementation that works for most
1647// unix variants. Any Process subclasses that need to do shared library
1648// loading differently should override LoadImage and UnloadImage and
1649// do what is needed.
1650//----------------------------------------------------------------------
1651Error
1652Process::UnloadImage (uint32_t image_token)
1653{
1654 Error error;
1655 if (image_token < m_image_tokens.size())
1656 {
1657 const addr_t image_addr = m_image_tokens[image_token];
1658 if (image_addr == LLDB_INVALID_ADDRESS)
1659 {
1660 error.SetErrorString("image already unloaded");
1661 }
1662 else
1663 {
1664 DynamicLoader *loader = GetDynamicLoader();
1665 if (loader)
1666 error = loader->CanLoadImage();
1667
1668 if (error.Success())
1669 {
1670 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001671
1672 if (thread_sp)
1673 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001674 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
Greg Clayton8f343b02010-11-04 01:54:29 +00001675
1676 if (frame_sp)
1677 {
1678 ExecutionContext exe_ctx;
1679 frame_sp->CalculateExecutionContext (exe_ctx);
Greg Clayton62afb9f2013-11-04 19:35:17 +00001680 EvaluateExpressionOptions expr_options;
1681 expr_options.SetUnwindOnError(true);
1682 expr_options.SetIgnoreBreakpoints(true);
1683 expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
Greg Clayton8f343b02010-11-04 01:54:29 +00001684 StreamString expr;
Daniel Malead01b2952012-11-29 21:49:15 +00001685 expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr);
Greg Clayton8f343b02010-11-04 01:54:29 +00001686 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001687 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +00001688 Error expr_error;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001689 ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001690 expr_options,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001691 expr.GetData(),
1692 prefix,
1693 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001694 expr_error);
Greg Clayton8f343b02010-11-04 01:54:29 +00001695 if (result_valobj_sp->GetError().Success())
1696 {
1697 Scalar scalar;
Jim Ingham6035b672011-03-31 00:19:25 +00001698 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001699 {
1700 if (scalar.UInt(1))
1701 {
1702 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1703 }
1704 else
1705 {
1706 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1707 }
1708 }
1709 }
1710 else
1711 {
1712 error = result_valobj_sp->GetError();
1713 }
1714 }
1715 }
1716 }
1717 }
1718 }
1719 else
1720 {
1721 error.SetErrorString("invalid image token");
1722 }
1723 return error;
1724}
1725
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001726const lldb::ABISP &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727Process::GetABI()
1728{
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001729 if (!m_abi_sp)
1730 m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture());
1731 return m_abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001732}
1733
Jim Ingham22777012010-09-23 02:01:19 +00001734LanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001735Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001736{
1737 LanguageRuntimeCollection::iterator pos;
1738 pos = m_language_runtimes.find (language);
Jim Inghamab175242012-03-10 00:22:19 +00001739 if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second))
Jim Ingham22777012010-09-23 02:01:19 +00001740 {
Jim Inghamab175242012-03-10 00:22:19 +00001741 lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language));
Jim Ingham22777012010-09-23 02:01:19 +00001742
Jim Inghamab175242012-03-10 00:22:19 +00001743 m_language_runtimes[language] = runtime_sp;
1744 return runtime_sp.get();
Jim Ingham22777012010-09-23 02:01:19 +00001745 }
1746 else
1747 return (*pos).second.get();
1748}
1749
1750CPPLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001751Process::GetCPPLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001752{
Jim Inghamab175242012-03-10 00:22:19 +00001753 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00001754 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
1755 return static_cast<CPPLanguageRuntime *> (runtime);
1756 return NULL;
1757}
1758
1759ObjCLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001760Process::GetObjCLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001761{
Jim Inghamab175242012-03-10 00:22:19 +00001762 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00001763 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
1764 return static_cast<ObjCLanguageRuntime *> (runtime);
1765 return NULL;
1766}
1767
Enrico Granatafd4c84e2012-05-21 16:51:35 +00001768bool
1769Process::IsPossibleDynamicValue (ValueObject& in_value)
1770{
1771 if (in_value.IsDynamic())
1772 return false;
1773 LanguageType known_type = in_value.GetObjectRuntimeLanguage();
1774
1775 if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
1776 {
1777 LanguageRuntime *runtime = GetLanguageRuntime (known_type);
1778 return runtime ? runtime->CouldHaveDynamicValue(in_value) : false;
1779 }
1780
1781 LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus);
1782 if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value))
1783 return true;
1784
1785 LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC);
1786 return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false;
1787}
1788
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789BreakpointSiteList &
1790Process::GetBreakpointSiteList()
1791{
1792 return m_breakpoint_site_list;
1793}
1794
1795const BreakpointSiteList &
1796Process::GetBreakpointSiteList() const
1797{
1798 return m_breakpoint_site_list;
1799}
1800
1801
1802void
1803Process::DisableAllBreakpointSites ()
1804{
Greg Claytond8cf1a12013-06-12 00:46:38 +00001805 m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void {
1806// bp_site->SetEnabled(true);
1807 DisableBreakpointSite(bp_site);
1808 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001809}
1810
1811Error
1812Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
1813{
1814 Error error (DisableBreakpointSiteByID (break_id));
1815
1816 if (error.Success())
1817 m_breakpoint_site_list.Remove(break_id);
1818
1819 return error;
1820}
1821
1822Error
1823Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
1824{
1825 Error error;
1826 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1827 if (bp_site_sp)
1828 {
1829 if (bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00001830 error = DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001831 }
1832 else
1833 {
Daniel Malead01b2952012-11-29 21:49:15 +00001834 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001835 }
1836
1837 return error;
1838}
1839
1840Error
1841Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
1842{
1843 Error error;
1844 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1845 if (bp_site_sp)
1846 {
1847 if (!bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00001848 error = EnableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001849 }
1850 else
1851 {
Daniel Malead01b2952012-11-29 21:49:15 +00001852 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853 }
1854 return error;
1855}
1856
Stephen Wilson50bd94f2010-07-17 00:56:13 +00001857lldb::break_id_t
Greg Claytone1cd1be2012-01-29 20:56:30 +00001858Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001859{
Jim Ingham1460e4b2014-01-10 23:46:59 +00001860 addr_t load_addr = LLDB_INVALID_ADDRESS;
1861
1862 bool show_error = true;
1863 switch (GetState())
1864 {
1865 case eStateInvalid:
1866 case eStateUnloaded:
1867 case eStateConnected:
1868 case eStateAttaching:
1869 case eStateLaunching:
1870 case eStateDetached:
1871 case eStateExited:
1872 show_error = false;
1873 break;
1874
1875 case eStateStopped:
1876 case eStateRunning:
1877 case eStateStepping:
1878 case eStateCrashed:
1879 case eStateSuspended:
1880 show_error = IsAlive();
1881 break;
1882 }
1883
1884 // Reset the IsIndirect flag here, in case the location changes from
1885 // pointing to a indirect symbol to a regular symbol.
1886 owner->SetIsIndirect (false);
1887
1888 if (owner->ShouldResolveIndirectFunctions())
1889 {
1890 Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol();
1891 if (symbol && symbol->IsIndirect())
1892 {
1893 Error error;
1894 load_addr = ResolveIndirectFunction (&symbol->GetAddress(), error);
1895 if (!error.Success() && show_error)
1896 {
Greg Clayton44d93782014-01-27 23:43:24 +00001897 m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to resolve indirect function at 0x%" PRIx64 " for breakpoint %i.%i: %s\n",
1898 symbol->GetAddress().GetLoadAddress(&m_target),
1899 owner->GetBreakpoint().GetID(),
1900 owner->GetID(),
Sylvestre Ledruf6102892014-08-11 18:06:28 +00001901 error.AsCString() ? error.AsCString() : "unknown error");
Jim Ingham1460e4b2014-01-10 23:46:59 +00001902 return LLDB_INVALID_BREAK_ID;
1903 }
1904 Address resolved_address(load_addr);
1905 load_addr = resolved_address.GetOpcodeLoadAddress (&m_target);
1906 owner->SetIsIndirect(true);
1907 }
1908 else
1909 load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
1910 }
1911 else
1912 load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
1913
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001914 if (load_addr != LLDB_INVALID_ADDRESS)
1915 {
1916 BreakpointSiteSP bp_site_sp;
1917
1918 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
1919 // create a new breakpoint site and add it.
1920
1921 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
1922
1923 if (bp_site_sp)
1924 {
1925 bp_site_sp->AddOwner (owner);
1926 owner->SetBreakpointSite (bp_site_sp);
1927 return bp_site_sp->GetID();
1928 }
1929 else
1930 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001931 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, use_hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001932 if (bp_site_sp)
1933 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001934 Error error = EnableBreakpointSite (bp_site_sp.get());
1935 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936 {
1937 owner->SetBreakpointSite (bp_site_sp);
1938 return m_breakpoint_site_list.Add (bp_site_sp);
1939 }
Greg Claytoneb023e72013-10-11 19:48:25 +00001940 else
1941 {
Greg Claytonfbb76342013-11-20 21:07:01 +00001942 if (show_error)
1943 {
1944 // Report error for setting breakpoint...
Greg Clayton44d93782014-01-27 23:43:24 +00001945 m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n",
1946 load_addr,
1947 owner->GetBreakpoint().GetID(),
1948 owner->GetID(),
Sylvestre Ledruf6102892014-08-11 18:06:28 +00001949 error.AsCString() ? error.AsCString() : "unknown error");
Greg Claytonfbb76342013-11-20 21:07:01 +00001950 }
Greg Claytoneb023e72013-10-11 19:48:25 +00001951 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952 }
1953 }
1954 }
1955 // We failed to enable the breakpoint
1956 return LLDB_INVALID_BREAK_ID;
1957
1958}
1959
1960void
1961Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
1962{
1963 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
1964 if (num_owners == 0)
1965 {
Jim Inghamf1ff3bb2013-04-06 00:16:39 +00001966 // Don't try to disable the site if we don't have a live process anymore.
1967 if (IsAlive())
1968 DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1970 }
1971}
1972
1973
1974size_t
1975Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1976{
1977 size_t bytes_removed = 0;
Jim Ingham20c77192011-06-29 19:42:28 +00001978 BreakpointSiteList bp_sites_in_range;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979
Jim Ingham20c77192011-06-29 19:42:28 +00001980 if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00001982 bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void {
1983 if (bp_site->GetType() == BreakpointSite::eSoftware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001984 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00001985 addr_t intersect_addr;
1986 size_t intersect_size;
1987 size_t opcode_offset;
1988 if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
Jim Ingham20c77192011-06-29 19:42:28 +00001989 {
1990 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
1991 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
Greg Claytond8cf1a12013-06-12 00:46:38 +00001992 assert(opcode_offset + intersect_size <= bp_site->GetByteSize());
Jim Ingham20c77192011-06-29 19:42:28 +00001993 size_t buf_offset = intersect_addr - bp_addr;
Greg Claytond8cf1a12013-06-12 00:46:38 +00001994 ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
Jim Ingham20c77192011-06-29 19:42:28 +00001995 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996 }
Greg Claytond8cf1a12013-06-12 00:46:38 +00001997 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001998 }
1999 return bytes_removed;
2000}
2001
2002
Greg Claytonded470d2011-03-19 01:12:21 +00002003
2004size_t
2005Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
2006{
2007 PlatformSP platform_sp (m_target.GetPlatform());
2008 if (platform_sp)
2009 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
2010 return 0;
2011}
2012
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002013Error
2014Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
2015{
2016 Error error;
2017 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002018 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019 const addr_t bp_addr = bp_site->GetLoadAddress();
2020 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002021 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, bp_site->GetID(), (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002022 if (bp_site->IsEnabled())
2023 {
2024 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002025 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 +00002026 return error;
2027 }
2028
2029 if (bp_addr == LLDB_INVALID_ADDRESS)
2030 {
2031 error.SetErrorString("BreakpointSite contains an invalid load address.");
2032 return error;
2033 }
2034 // Ask the lldb::Process subclass to fill in the correct software breakpoint
2035 // trap for the breakpoint site
2036 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
2037
2038 if (bp_opcode_size == 0)
2039 {
Daniel Malead01b2952012-11-29 21:49:15 +00002040 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%" PRIx64, bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002041 }
2042 else
2043 {
2044 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
2045
2046 if (bp_opcode_bytes == NULL)
2047 {
2048 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
2049 return error;
2050 }
2051
2052 // Save the original opcode by reading it
2053 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
2054 {
2055 // Write a software breakpoint in place of the original opcode
2056 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2057 {
2058 uint8_t verify_bp_opcode_bytes[64];
2059 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2060 {
2061 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
2062 {
2063 bp_site->SetEnabled(true);
2064 bp_site->SetType (BreakpointSite::eSoftware);
2065 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002066 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002067 bp_site->GetID(),
2068 (uint64_t)bp_addr);
2069 }
2070 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002071 error.SetErrorString("failed to verify the breakpoint trap in memory.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002072 }
2073 else
2074 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
2075 }
2076 else
2077 error.SetErrorString("Unable to write breakpoint trap to memory.");
2078 }
2079 else
2080 error.SetErrorString("Unable to read memory at breakpoint address.");
2081 }
Stephen Wilson78a4feb2011-01-12 04:20:03 +00002082 if (log && error.Fail())
Daniel Malead01b2952012-11-29 21:49:15 +00002083 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002084 bp_site->GetID(),
2085 (uint64_t)bp_addr,
2086 error.AsCString());
2087 return error;
2088}
2089
2090Error
2091Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
2092{
2093 Error error;
2094 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002095 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002096 addr_t bp_addr = bp_site->GetLoadAddress();
2097 lldb::user_id_t breakID = bp_site->GetID();
2098 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002099 log->Printf ("Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 ") addr = 0x%" PRIx64, breakID, (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002100
2101 if (bp_site->IsHardware())
2102 {
2103 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
2104 }
2105 else if (bp_site->IsEnabled())
2106 {
2107 const size_t break_op_size = bp_site->GetByteSize();
2108 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
2109 if (break_op_size > 0)
2110 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002111 // Clear a software breakpoint instruction
Greg Claytonc982c762010-07-09 20:39:50 +00002112 uint8_t curr_break_op[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002113 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002114 bool break_op_found = false;
2115
2116 // Read the breakpoint opcode
2117 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
2118 {
2119 bool verify = false;
2120 // Make sure we have the a breakpoint opcode exists at this address
2121 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
2122 {
2123 break_op_found = true;
2124 // We found a valid breakpoint opcode at this address, now restore
2125 // the saved opcode.
2126 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
2127 {
2128 verify = true;
2129 }
2130 else
2131 error.SetErrorString("Memory write failed when restoring original opcode.");
2132 }
2133 else
2134 {
2135 error.SetErrorString("Original breakpoint trap is no longer in memory.");
2136 // Set verify to true and so we can check if the original opcode has already been restored
2137 verify = true;
2138 }
2139
2140 if (verify)
2141 {
Greg Claytonc982c762010-07-09 20:39:50 +00002142 uint8_t verify_opcode[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002143 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002144 // Verify that our original opcode made it back to the inferior
2145 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
2146 {
2147 // compare the memory we just read with the original opcode
2148 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
2149 {
2150 // SUCCESS
2151 bp_site->SetEnabled(false);
2152 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002153 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 +00002154 return error;
2155 }
2156 else
2157 {
2158 if (break_op_found)
2159 error.SetErrorString("Failed to restore original opcode.");
2160 }
2161 }
2162 else
2163 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
2164 }
2165 }
2166 else
2167 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
2168 }
2169 }
2170 else
2171 {
2172 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002173 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 +00002174 return error;
2175 }
2176
2177 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002178 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002179 bp_site->GetID(),
2180 (uint64_t)bp_addr,
2181 error.AsCString());
2182 return error;
2183
2184}
2185
Greg Clayton58be07b2011-01-07 06:08:19 +00002186// Uncomment to verify memory caching works after making changes to caching code
2187//#define VERIFY_MEMORY_READS
2188
Sean Callanan64c0cf22012-06-07 22:26:42 +00002189size_t
2190Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2191{
Jason Molendaa7b5afa2013-11-15 00:17:32 +00002192 error.Clear();
Sean Callanan64c0cf22012-06-07 22:26:42 +00002193 if (!GetDisableMemoryCache())
2194 {
Greg Clayton58be07b2011-01-07 06:08:19 +00002195#if defined (VERIFY_MEMORY_READS)
Sean Callanan64c0cf22012-06-07 22:26:42 +00002196 // Memory caching is enabled, with debug verification
2197
2198 if (buf && size)
2199 {
2200 // Uncomment the line below to make sure memory caching is working.
2201 // I ran this through the test suite and got no assertions, so I am
2202 // pretty confident this is working well. If any changes are made to
2203 // memory caching, uncomment the line below and test your changes!
2204
2205 // Verify all memory reads by using the cache first, then redundantly
2206 // reading the same memory from the inferior and comparing to make sure
2207 // everything is exactly the same.
2208 std::string verify_buf (size, '\0');
2209 assert (verify_buf.size() == size);
2210 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
2211 Error verify_error;
2212 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
2213 assert (cache_bytes_read == verify_bytes_read);
2214 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
2215 assert (verify_error.Success() == error.Success());
2216 return cache_bytes_read;
2217 }
2218 return 0;
2219#else // !defined(VERIFY_MEMORY_READS)
2220 // Memory caching is enabled, without debug verification
2221
2222 return m_memory_cache.Read (addr, buf, size, error);
2223#endif // defined (VERIFY_MEMORY_READS)
Greg Clayton58be07b2011-01-07 06:08:19 +00002224 }
Sean Callanan64c0cf22012-06-07 22:26:42 +00002225 else
2226 {
2227 // Memory caching is disabled
2228
2229 return ReadMemoryFromInferior (addr, buf, size, error);
2230 }
Greg Clayton58be07b2011-01-07 06:08:19 +00002231}
Greg Clayton58be07b2011-01-07 06:08:19 +00002232
Greg Clayton4c82d422012-05-18 23:20:01 +00002233size_t
2234Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error)
2235{
Greg Claytonde87c0f2012-05-19 00:18:00 +00002236 char buf[256];
Greg Clayton4c82d422012-05-18 23:20:01 +00002237 out_str.clear();
2238 addr_t curr_addr = addr;
2239 while (1)
2240 {
2241 size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error);
2242 if (length == 0)
2243 break;
2244 out_str.append(buf, length);
2245 // If we got "length - 1" bytes, we didn't get the whole C string, we
2246 // need to read some more characters
2247 if (length == sizeof(buf) - 1)
2248 curr_addr += length;
2249 else
2250 break;
2251 }
2252 return out_str.size();
2253}
2254
Greg Clayton58be07b2011-01-07 06:08:19 +00002255
2256size_t
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +00002257Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error,
2258 size_t type_width)
2259{
2260 size_t total_bytes_read = 0;
2261 if (dst && max_bytes && type_width && max_bytes >= type_width)
2262 {
2263 // Ensure a null terminator independent of the number of bytes that is read.
2264 memset (dst, 0, max_bytes);
2265 size_t bytes_left = max_bytes - type_width;
2266
2267 const char terminator[4] = {'\0', '\0', '\0', '\0'};
2268 assert(sizeof(terminator) >= type_width &&
2269 "Attempting to validate a string with more than 4 bytes per character!");
2270
2271 addr_t curr_addr = addr;
2272 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2273 char *curr_dst = dst;
2274
2275 error.Clear();
2276 while (bytes_left > 0 && error.Success())
2277 {
2278 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2279 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2280 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2281
2282 if (bytes_read == 0)
2283 break;
2284
2285 // Search for a null terminator of correct size and alignment in bytes_read
2286 size_t aligned_start = total_bytes_read - total_bytes_read % type_width;
2287 for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width)
2288 if (::strncmp(&dst[i], terminator, type_width) == 0)
2289 {
2290 error.Clear();
2291 return i;
2292 }
2293
2294 total_bytes_read += bytes_read;
2295 curr_dst += bytes_read;
2296 curr_addr += bytes_read;
2297 bytes_left -= bytes_read;
2298 }
2299 }
2300 else
2301 {
2302 if (max_bytes)
2303 error.SetErrorString("invalid arguments");
2304 }
2305 return total_bytes_read;
2306}
2307
2308// Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find
2309// null terminators.
2310size_t
Greg Claytone91b7952011-12-15 03:14:23 +00002311Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error)
Greg Clayton8b82f082011-04-12 05:54:46 +00002312{
2313 size_t total_cstr_len = 0;
2314 if (dst && dst_max_len)
2315 {
Greg Claytone91b7952011-12-15 03:14:23 +00002316 result_error.Clear();
Greg Clayton8b82f082011-04-12 05:54:46 +00002317 // NULL out everything just to be safe
2318 memset (dst, 0, dst_max_len);
2319 Error error;
2320 addr_t curr_addr = addr;
2321 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2322 size_t bytes_left = dst_max_len - 1;
2323 char *curr_dst = dst;
2324
2325 while (bytes_left > 0)
2326 {
2327 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2328 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2329 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2330
2331 if (bytes_read == 0)
2332 {
Greg Claytone91b7952011-12-15 03:14:23 +00002333 result_error = error;
Greg Clayton8b82f082011-04-12 05:54:46 +00002334 dst[total_cstr_len] = '\0';
2335 break;
2336 }
2337 const size_t len = strlen(curr_dst);
2338
2339 total_cstr_len += len;
2340
2341 if (len < bytes_to_read)
2342 break;
2343
2344 curr_dst += bytes_read;
2345 curr_addr += bytes_read;
2346 bytes_left -= bytes_read;
2347 }
2348 }
Greg Claytone91b7952011-12-15 03:14:23 +00002349 else
2350 {
2351 if (dst == NULL)
2352 result_error.SetErrorString("invalid arguments");
2353 else
2354 result_error.Clear();
2355 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002356 return total_cstr_len;
2357}
2358
2359size_t
Greg Clayton58be07b2011-01-07 06:08:19 +00002360Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
2361{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002362 if (buf == NULL || size == 0)
2363 return 0;
2364
2365 size_t bytes_read = 0;
2366 uint8_t *bytes = (uint8_t *)buf;
2367
2368 while (bytes_read < size)
2369 {
2370 const size_t curr_size = size - bytes_read;
2371 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
2372 bytes + bytes_read,
2373 curr_size,
2374 error);
2375 bytes_read += curr_bytes_read;
2376 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
2377 break;
2378 }
2379
2380 // Replace any software breakpoint opcodes that fall into this range back
2381 // into "buf" before we return
2382 if (bytes_read > 0)
2383 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
2384 return bytes_read;
2385}
2386
Greg Clayton58a4c462010-12-16 20:01:20 +00002387uint64_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002388Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error)
Greg Clayton58a4c462010-12-16 20:01:20 +00002389{
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002390 Scalar scalar;
2391 if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error))
2392 return scalar.ULongLong(fail_value);
2393 return fail_value;
2394}
2395
2396addr_t
2397Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error)
2398{
2399 Scalar scalar;
2400 if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error))
2401 return scalar.ULongLong(LLDB_INVALID_ADDRESS);
2402 return LLDB_INVALID_ADDRESS;
2403}
2404
2405
2406bool
2407Process::WritePointerToMemory (lldb::addr_t vm_addr,
2408 lldb::addr_t ptr_value,
2409 Error &error)
2410{
2411 Scalar scalar;
2412 const uint32_t addr_byte_size = GetAddressByteSize();
2413 if (addr_byte_size <= 4)
2414 scalar = (uint32_t)ptr_value;
Greg Clayton58a4c462010-12-16 20:01:20 +00002415 else
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002416 scalar = ptr_value;
2417 return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size;
Greg Clayton58a4c462010-12-16 20:01:20 +00002418}
2419
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002420size_t
2421Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
2422{
2423 size_t bytes_written = 0;
2424 const uint8_t *bytes = (const uint8_t *)buf;
2425
2426 while (bytes_written < size)
2427 {
2428 const size_t curr_size = size - bytes_written;
2429 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
2430 bytes + bytes_written,
2431 curr_size,
2432 error);
2433 bytes_written += curr_bytes_written;
2434 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
2435 break;
2436 }
2437 return bytes_written;
2438}
2439
2440size_t
2441Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2442{
Greg Clayton58be07b2011-01-07 06:08:19 +00002443#if defined (ENABLE_MEMORY_CACHING)
2444 m_memory_cache.Flush (addr, size);
2445#endif
2446
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002447 if (buf == NULL || size == 0)
2448 return 0;
Jim Ingham78a685a2011-04-16 00:01:13 +00002449
Jim Ingham4b536182011-08-09 02:12:22 +00002450 m_mod_id.BumpMemoryID();
Jim Ingham78a685a2011-04-16 00:01:13 +00002451
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002452 // We need to write any data that would go where any current software traps
2453 // (enabled software breakpoints) any software traps (breakpoints) that we
2454 // may have placed in our tasks memory.
2455
Greg Claytond8cf1a12013-06-12 00:46:38 +00002456 BreakpointSiteList bp_sites_in_range;
2457
2458 if (m_breakpoint_site_list.FindInRange (addr, addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002459 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002460 // No breakpoint sites overlap
2461 if (bp_sites_in_range.IsEmpty())
2462 return WriteMemoryPrivate (addr, buf, size, error);
2463 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002464 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002465 const uint8_t *ubuf = (const uint8_t *)buf;
2466 uint64_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002467
Greg Claytond8cf1a12013-06-12 00:46:38 +00002468 bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void {
2469
2470 if (error.Success())
2471 {
2472 addr_t intersect_addr;
2473 size_t intersect_size;
2474 size_t opcode_offset;
2475 const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset);
2476 assert(intersects);
2477 assert(addr <= intersect_addr && intersect_addr < addr + size);
2478 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
2479 assert(opcode_offset + intersect_size <= bp->GetByteSize());
2480
2481 // Check for bytes before this breakpoint
2482 const addr_t curr_addr = addr + bytes_written;
2483 if (intersect_addr > curr_addr)
2484 {
2485 // There are some bytes before this breakpoint that we need to
2486 // just write to memory
2487 size_t curr_size = intersect_addr - curr_addr;
2488 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
2489 ubuf + bytes_written,
2490 curr_size,
2491 error);
2492 bytes_written += curr_bytes_written;
2493 if (curr_bytes_written != curr_size)
2494 {
2495 // We weren't able to write all of the requested bytes, we
2496 // are done looping and will return the number of bytes that
2497 // we have written so far.
2498 if (error.Success())
2499 error.SetErrorToGenericError();
2500 }
2501 }
2502 // Now write any bytes that would cover up any software breakpoints
2503 // directly into the breakpoint opcode buffer
2504 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
2505 bytes_written += intersect_size;
2506 }
2507 });
2508
2509 if (bytes_written < size)
2510 bytes_written += WriteMemoryPrivate (addr + bytes_written,
2511 ubuf + bytes_written,
2512 size - bytes_written,
2513 error);
2514 }
2515 }
2516 else
2517 {
2518 return WriteMemoryPrivate (addr, buf, size, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002519 }
2520
2521 // Write any remaining bytes after the last breakpoint if we have any left
Greg Claytond8cf1a12013-06-12 00:46:38 +00002522 return 0; //bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002523}
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002524
2525size_t
Greg Claytonc7bece562013-01-25 18:06:21 +00002526Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, size_t byte_size, Error &error)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002527{
2528 if (byte_size == UINT32_MAX)
2529 byte_size = scalar.GetByteSize();
2530 if (byte_size > 0)
2531 {
2532 uint8_t buf[32];
2533 const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error);
2534 if (mem_size > 0)
2535 return WriteMemory(addr, buf, mem_size, error);
2536 else
2537 error.SetErrorString ("failed to get scalar as memory data");
2538 }
2539 else
2540 {
2541 error.SetErrorString ("invalid scalar value");
2542 }
2543 return 0;
2544}
2545
2546size_t
2547Process::ReadScalarIntegerFromMemory (addr_t addr,
2548 uint32_t byte_size,
2549 bool is_signed,
2550 Scalar &scalar,
2551 Error &error)
2552{
Greg Clayton7060f892013-05-01 23:41:30 +00002553 uint64_t uval = 0;
2554 if (byte_size == 0)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002555 {
Greg Clayton7060f892013-05-01 23:41:30 +00002556 error.SetErrorString ("byte size is zero");
2557 }
2558 else if (byte_size & (byte_size - 1))
2559 {
2560 error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size);
2561 }
2562 else if (byte_size <= sizeof(uval))
2563 {
2564 const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002565 if (bytes_read == byte_size)
2566 {
2567 DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00002568 lldb::offset_t offset = 0;
Greg Clayton7060f892013-05-01 23:41:30 +00002569 if (byte_size <= 4)
2570 scalar = data.GetMaxU32 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002571 else
Greg Clayton7060f892013-05-01 23:41:30 +00002572 scalar = data.GetMaxU64 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002573 if (is_signed)
2574 scalar.SignExtend(byte_size * 8);
2575 return bytes_read;
2576 }
2577 }
2578 else
2579 {
2580 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
2581 }
2582 return 0;
2583}
2584
Greg Claytond495c532011-05-17 03:37:42 +00002585#define USE_ALLOCATE_MEMORY_CACHE 1
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002586addr_t
2587Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
2588{
Jim Inghamf72ce3a2011-06-20 17:32:44 +00002589 if (GetPrivateState() != eStateStopped)
2590 return LLDB_INVALID_ADDRESS;
2591
Greg Claytond495c532011-05-17 03:37:42 +00002592#if defined (USE_ALLOCATE_MEMORY_CACHE)
2593 return m_allocated_memory_cache.AllocateMemory(size, permissions, error);
2594#else
Greg Claytonb2daec92011-01-23 19:58:49 +00002595 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
Greg Clayton5160ce52013-03-27 23:08:40 +00002596 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002597 if (log)
Greg Clayton45989072013-10-23 18:24:30 +00002598 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 +00002599 (uint64_t)size,
Greg Claytond495c532011-05-17 03:37:42 +00002600 GetPermissionsAsCString (permissions),
Greg Claytonb2daec92011-01-23 19:58:49 +00002601 (uint64_t)allocated_addr,
Jim Ingham4b536182011-08-09 02:12:22 +00002602 m_mod_id.GetStopID(),
2603 m_mod_id.GetMemoryID());
Greg Claytonb2daec92011-01-23 19:58:49 +00002604 return allocated_addr;
Greg Claytond495c532011-05-17 03:37:42 +00002605#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002606}
2607
Sean Callanan90539452011-09-20 23:01:51 +00002608bool
2609Process::CanJIT ()
2610{
Sean Callanana7b443a2012-02-14 22:50:38 +00002611 if (m_can_jit == eCanJITDontKnow)
2612 {
Todd Fialaaf245d12014-06-30 21:05:18 +00002613 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Sean Callanana7b443a2012-02-14 22:50:38 +00002614 Error err;
2615
2616 uint64_t allocated_memory = AllocateMemory(8,
2617 ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable,
2618 err);
2619
2620 if (err.Success())
Todd Fialaaf245d12014-06-30 21:05:18 +00002621 {
Sean Callanana7b443a2012-02-14 22:50:38 +00002622 m_can_jit = eCanJITYes;
Todd Fialaaf245d12014-06-30 21:05:18 +00002623 if (log)
2624 log->Printf ("Process::%s pid %" PRIu64 " allocation test passed, CanJIT () is true", __FUNCTION__, GetID ());
2625 }
Sean Callanana7b443a2012-02-14 22:50:38 +00002626 else
Todd Fialaaf245d12014-06-30 21:05:18 +00002627 {
Sean Callanana7b443a2012-02-14 22:50:38 +00002628 m_can_jit = eCanJITNo;
Todd Fialaaf245d12014-06-30 21:05:18 +00002629 if (log)
2630 log->Printf ("Process::%s pid %" PRIu64 " allocation test failed, CanJIT () is false: %s", __FUNCTION__, GetID (), err.AsCString ());
2631 }
Sean Callanana7b443a2012-02-14 22:50:38 +00002632
2633 DeallocateMemory (allocated_memory);
2634 }
2635
Sean Callanan90539452011-09-20 23:01:51 +00002636 return m_can_jit == eCanJITYes;
2637}
2638
2639void
2640Process::SetCanJIT (bool can_jit)
2641{
2642 m_can_jit = (can_jit ? eCanJITYes : eCanJITNo);
2643}
2644
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002645Error
2646Process::DeallocateMemory (addr_t ptr)
2647{
Greg Claytond495c532011-05-17 03:37:42 +00002648 Error error;
2649#if defined (USE_ALLOCATE_MEMORY_CACHE)
2650 if (!m_allocated_memory_cache.DeallocateMemory(ptr))
2651 {
Daniel Malead01b2952012-11-29 21:49:15 +00002652 error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr);
Greg Claytond495c532011-05-17 03:37:42 +00002653 }
2654#else
2655 error = DoDeallocateMemory (ptr);
Greg Claytonb2daec92011-01-23 19:58:49 +00002656
Greg Clayton5160ce52013-03-27 23:08:40 +00002657 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002658 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002659 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 +00002660 ptr,
2661 error.AsCString("SUCCESS"),
Jim Ingham4b536182011-08-09 02:12:22 +00002662 m_mod_id.GetStopID(),
2663 m_mod_id.GetMemoryID());
Greg Claytond495c532011-05-17 03:37:42 +00002664#endif
Greg Claytonb2daec92011-01-23 19:58:49 +00002665 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002666}
2667
Han Ming Ongc811d382012-11-17 00:33:14 +00002668
Greg Claytonc9660542012-02-05 02:38:54 +00002669ModuleSP
Greg Claytonc859e2d2012-02-13 23:10:39 +00002670Process::ReadModuleFromMemory (const FileSpec& file_spec,
Andrew MacPherson17220c12014-03-05 10:12:43 +00002671 lldb::addr_t header_addr,
2672 size_t size_to_read)
Greg Claytonc9660542012-02-05 02:38:54 +00002673{
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002674 ModuleSP module_sp (new Module (file_spec, ArchSpec()));
Greg Claytonc9660542012-02-05 02:38:54 +00002675 if (module_sp)
2676 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002677 Error error;
Andrew MacPherson17220c12014-03-05 10:12:43 +00002678 ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error, size_to_read);
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002679 if (objfile)
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002680 return module_sp;
Greg Claytonc9660542012-02-05 02:38:54 +00002681 }
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002682 return ModuleSP();
Greg Claytonc9660542012-02-05 02:38:54 +00002683}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002684
2685Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002686Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002687{
2688 Error error;
2689 error.SetErrorString("watchpoints are not supported");
2690 return error;
2691}
2692
2693Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002694Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002695{
2696 Error error;
2697 error.SetErrorString("watchpoints are not supported");
2698 return error;
2699}
2700
2701StateType
2702Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
2703{
2704 StateType state;
2705 // Now wait for the process to launch and return control to us, and then
2706 // call DidLaunch:
2707 while (1)
2708 {
Greg Clayton6779606a2011-01-22 23:43:18 +00002709 event_sp.reset();
2710 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
2711
Greg Clayton2637f822011-11-17 01:23:07 +00002712 if (StateIsStoppedState(state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002713 break;
Greg Clayton6779606a2011-01-22 23:43:18 +00002714
2715 // If state is invalid, then we timed out
2716 if (state == eStateInvalid)
2717 break;
2718
2719 if (event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002720 HandlePrivateEvent (event_sp);
2721 }
2722 return state;
2723}
2724
2725Error
Greg Claytonfbb76342013-11-20 21:07:01 +00002726Process::Launch (ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002727{
2728 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002729 m_abi_sp.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002730 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002731 m_jit_loaders_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00002732 m_system_runtime_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002733 m_os_ap.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002734 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002735
Greg Claytonaa149cb2011-08-11 02:48:45 +00002736 Module *exe_module = m_target.GetExecutableModulePointer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002737 if (exe_module)
2738 {
Greg Clayton2289fa42011-04-30 01:09:13 +00002739 char local_exec_file_path[PATH_MAX];
2740 char platform_exec_file_path[PATH_MAX];
2741 exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path));
2742 exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002743 if (exe_module->GetFileSpec().Exists())
2744 {
Greg Claytonfbb76342013-11-20 21:07:01 +00002745 // Install anything that might need to be installed prior to launching.
2746 // For host systems, this will do nothing, but if we are connected to a
2747 // remote platform it will install any needed binaries
2748 error = GetTarget().Install(&launch_info);
2749 if (error.Fail())
2750 return error;
2751
Greg Clayton71337622011-02-24 22:24:29 +00002752 if (PrivateStateThreadIsValid ())
2753 PausePrivateStateThread ();
2754
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002755 error = WillLaunch (exe_module);
2756 if (error.Success())
2757 {
Jim Ingham221d51c2013-05-08 00:35:16 +00002758 const bool restarted = false;
2759 SetPublicState (eStateLaunching, restarted);
Greg Claytone24c4ac2011-11-17 04:46:02 +00002760 m_should_detach = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002761
Ed Maste64fad602013-07-29 20:58:06 +00002762 if (m_public_run_lock.TrySetRunning())
Greg Clayton69fd4be2012-09-04 20:29:05 +00002763 {
2764 // Now launch using these arguments.
2765 error = DoLaunch (exe_module, launch_info);
2766 }
2767 else
2768 {
2769 // This shouldn't happen
2770 error.SetErrorString("failed to acquire process run lock");
2771 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002772
2773 if (error.Fail())
2774 {
2775 if (GetID() != LLDB_INVALID_PROCESS_ID)
2776 {
2777 SetID (LLDB_INVALID_PROCESS_ID);
2778 const char *error_string = error.AsCString();
2779 if (error_string == NULL)
2780 error_string = "launch failed";
2781 SetExitStatus (-1, error_string);
2782 }
2783 }
2784 else
2785 {
2786 EventSP event_sp;
Greg Clayton1a38ea72011-06-22 01:42:17 +00002787 TimeValue timeout_time;
2788 timeout_time = TimeValue::Now();
2789 timeout_time.OffsetWithSeconds(10);
2790 StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002791
Greg Clayton1a38ea72011-06-22 01:42:17 +00002792 if (state == eStateInvalid || event_sp.get() == NULL)
2793 {
2794 // We were able to launch the process, but we failed to
2795 // catch the initial stop.
2796 SetExitStatus (0, "failed to catch stop after launch");
2797 Destroy();
2798 }
2799 else if (state == eStateStopped || state == eStateCrashed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002800 {
Greg Clayton93d3c8332011-02-16 04:46:07 +00002801
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002802 DidLaunch ();
2803
Greg Claytonc859e2d2012-02-13 23:10:39 +00002804 DynamicLoader *dyld = GetDynamicLoader ();
2805 if (dyld)
2806 dyld->DidLaunch();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002807
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00002808 GetJITLoaders().DidLaunch();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002809
Jason Molendaeef51062013-11-05 03:57:19 +00002810 SystemRuntime *system_runtime = GetSystemRuntime ();
2811 if (system_runtime)
2812 system_runtime->DidLaunch();
2813
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002814 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002815 // This delays passing the stopped event to listeners till DidLaunch gets
2816 // a chance to complete...
2817 HandlePrivateEvent (event_sp);
Greg Clayton71337622011-02-24 22:24:29 +00002818
2819 if (PrivateStateThreadIsValid ())
2820 ResumePrivateStateThread ();
2821 else
2822 StartPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002823 }
2824 else if (state == eStateExited)
2825 {
2826 // We exited while trying to launch somehow. Don't call DidLaunch as that's
2827 // not likely to work, and return an invalid pid.
2828 HandlePrivateEvent (event_sp);
2829 }
2830 }
2831 }
2832 }
2833 else
2834 {
Greg Clayton86edbf42011-10-26 00:56:27 +00002835 error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002836 }
2837 }
2838 return error;
2839}
2840
Greg Claytonc3776bf2012-02-09 06:16:32 +00002841
2842Error
2843Process::LoadCore ()
2844{
2845 Error error = DoLoadCore();
2846 if (error.Success())
2847 {
2848 if (PrivateStateThreadIsValid ())
2849 ResumePrivateStateThread ();
2850 else
2851 StartPrivateStateThread ();
2852
Greg Claytonc859e2d2012-02-13 23:10:39 +00002853 DynamicLoader *dyld = GetDynamicLoader ();
2854 if (dyld)
2855 dyld->DidAttach();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002856
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00002857 GetJITLoaders().DidAttach();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002858
Jason Molendaeef51062013-11-05 03:57:19 +00002859 SystemRuntime *system_runtime = GetSystemRuntime ();
2860 if (system_runtime)
2861 system_runtime->DidAttach();
2862
Greg Claytonc859e2d2012-02-13 23:10:39 +00002863 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Claytonc3776bf2012-02-09 06:16:32 +00002864 // We successfully loaded a core file, now pretend we stopped so we can
2865 // show all of the threads in the core file and explore the crashed
2866 // state.
2867 SetPrivateState (eStateStopped);
2868
2869 }
2870 return error;
2871}
2872
Greg Claytonc859e2d2012-02-13 23:10:39 +00002873DynamicLoader *
2874Process::GetDynamicLoader ()
2875{
2876 if (m_dyld_ap.get() == NULL)
2877 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
2878 return m_dyld_ap.get();
2879}
Greg Claytonc3776bf2012-02-09 06:16:32 +00002880
Todd Fialaaf245d12014-06-30 21:05:18 +00002881const lldb::DataBufferSP
2882Process::GetAuxvData()
2883{
2884 return DataBufferSP ();
2885}
2886
Andrew MacPherson17220c12014-03-05 10:12:43 +00002887JITLoaderList &
2888Process::GetJITLoaders ()
2889{
2890 if (!m_jit_loaders_ap)
2891 {
2892 m_jit_loaders_ap.reset(new JITLoaderList());
2893 JITLoader::LoadPlugins(this, *m_jit_loaders_ap);
2894 }
2895 return *m_jit_loaders_ap;
2896}
2897
Jason Molendaeef51062013-11-05 03:57:19 +00002898SystemRuntime *
2899Process::GetSystemRuntime ()
2900{
2901 if (m_system_runtime_ap.get() == NULL)
2902 m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this));
2903 return m_system_runtime_ap.get();
2904}
2905
Todd Fiala76e0fc92014-08-27 22:58:26 +00002906Process::AttachCompletionHandler::AttachCompletionHandler (Process *process, uint32_t exec_count) :
2907 NextEventAction (process),
2908 m_exec_count (exec_count)
2909{
2910 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2911 if (log)
2912 log->Printf ("Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32, __FUNCTION__, static_cast<void*>(process), exec_count);
2913}
Greg Claytonc3776bf2012-02-09 06:16:32 +00002914
Jim Inghambb3a2832011-01-29 01:49:25 +00002915Process::NextEventAction::EventActionResult
2916Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002917{
Todd Fiala76e0fc92014-08-27 22:58:26 +00002918 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2919
Jim Inghambb3a2832011-01-29 01:49:25 +00002920 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
Todd Fiala76e0fc92014-08-27 22:58:26 +00002921 if (log)
2922 log->Printf ("Process::AttachCompletionHandler::%s called with state %s (%d)", __FUNCTION__, StateAsCString(state), static_cast<int> (state));
2923
2924 switch (state)
Greg Clayton19388cf2010-10-18 01:45:30 +00002925 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002926 case eStateRunning:
Greg Clayton71337622011-02-24 22:24:29 +00002927 case eStateConnected:
Greg Clayton513c26c2011-01-29 07:10:55 +00002928 return eEventActionRetry;
2929
2930 case eStateStopped:
2931 case eStateCrashed:
Greg Claytonc9ed4782011-11-12 02:10:56 +00002932 {
2933 // During attach, prior to sending the eStateStopped event,
Jim Inghamb1e2e842012-04-12 18:49:31 +00002934 // lldb_private::Process subclasses must set the new process ID.
Greg Claytonc9ed4782011-11-12 02:10:56 +00002935 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
Jim Ingham221d51c2013-05-08 00:35:16 +00002936 // We don't want these events to be reported, so go set the ShouldReportStop here:
2937 m_process->GetThreadList().SetShouldReportStop (eVoteNo);
2938
Greg Claytonc9ed4782011-11-12 02:10:56 +00002939 if (m_exec_count > 0)
2940 {
2941 --m_exec_count;
Todd Fiala76e0fc92014-08-27 22:58:26 +00002942
2943 if (log)
2944 log->Printf ("Process::AttachCompletionHandler::%s state %s: reduced remaining exec count to %" PRIu32 ", requesting resume", __FUNCTION__, StateAsCString(state), m_exec_count);
2945
Jim Ingham221d51c2013-05-08 00:35:16 +00002946 RequestResume();
Greg Claytonc9ed4782011-11-12 02:10:56 +00002947 return eEventActionRetry;
2948 }
2949 else
2950 {
Todd Fiala76e0fc92014-08-27 22:58:26 +00002951 if (log)
2952 log->Printf ("Process::AttachCompletionHandler::%s state %s: no more execs expected to start, continuing with attach", __FUNCTION__, StateAsCString(state));
2953
Greg Claytonc9ed4782011-11-12 02:10:56 +00002954 m_process->CompleteAttach ();
2955 return eEventActionSuccess;
2956 }
2957 }
Greg Clayton513c26c2011-01-29 07:10:55 +00002958 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00002959
Greg Clayton513c26c2011-01-29 07:10:55 +00002960 default:
2961 case eStateExited:
2962 case eStateInvalid:
Greg Clayton513c26c2011-01-29 07:10:55 +00002963 break;
Jim Inghambb3a2832011-01-29 01:49:25 +00002964 }
Greg Claytonc9ed4782011-11-12 02:10:56 +00002965
2966 m_exit_string.assign ("No valid Process");
2967 return eEventActionExit;
Jim Inghambb3a2832011-01-29 01:49:25 +00002968}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002969
Jim Inghambb3a2832011-01-29 01:49:25 +00002970Process::NextEventAction::EventActionResult
2971Process::AttachCompletionHandler::HandleBeingInterrupted()
2972{
2973 return eEventActionSuccess;
2974}
2975
2976const char *
2977Process::AttachCompletionHandler::GetExitString ()
2978{
2979 return m_exit_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002980}
2981
2982Error
Greg Clayton144f3a92011-11-15 03:53:30 +00002983Process::Attach (ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002984{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002985 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002986 m_process_input_reader.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002987 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002988 m_jit_loaders_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00002989 m_system_runtime_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002990 m_os_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00002991
Greg Clayton144f3a92011-11-15 03:53:30 +00002992 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Claytone996fd32011-03-08 22:40:15 +00002993 Error error;
Greg Clayton144f3a92011-11-15 03:53:30 +00002994 if (attach_pid == LLDB_INVALID_PROCESS_ID)
Jim Ingham5aee1622010-08-09 23:31:02 +00002995 {
Greg Clayton144f3a92011-11-15 03:53:30 +00002996 char process_name[PATH_MAX];
Jim Ingham4299fdb2011-09-15 01:10:17 +00002997
Greg Clayton144f3a92011-11-15 03:53:30 +00002998 if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name)))
Jim Ingham2ecb7422010-08-17 21:54:19 +00002999 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003000 const bool wait_for_launch = attach_info.GetWaitForLaunch();
3001
3002 if (wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003003 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003004 error = WillAttachToProcessWithName(process_name, wait_for_launch);
3005 if (error.Success())
3006 {
Ed Maste64fad602013-07-29 20:58:06 +00003007 if (m_public_run_lock.TrySetRunning())
Greg Clayton926cce72012-10-12 16:10:12 +00003008 {
3009 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003010 const bool restarted = false;
3011 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003012 // Now attach using these arguments.
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +00003013 error = DoAttachToProcessWithName (process_name, attach_info);
Greg Clayton926cce72012-10-12 16:10:12 +00003014 }
3015 else
3016 {
3017 // This shouldn't happen
3018 error.SetErrorString("failed to acquire process run lock");
3019 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00003020
Greg Clayton144f3a92011-11-15 03:53:30 +00003021 if (error.Fail())
3022 {
3023 if (GetID() != LLDB_INVALID_PROCESS_ID)
3024 {
3025 SetID (LLDB_INVALID_PROCESS_ID);
3026 if (error.AsCString() == NULL)
3027 error.SetErrorString("attach failed");
3028
3029 SetExitStatus(-1, error.AsCString());
3030 }
3031 }
3032 else
3033 {
3034 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3035 StartPrivateStateThread();
3036 }
3037 return error;
3038 }
Greg Claytone996fd32011-03-08 22:40:15 +00003039 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003040 else
Greg Claytone996fd32011-03-08 22:40:15 +00003041 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003042 ProcessInstanceInfoList process_infos;
3043 PlatformSP platform_sp (m_target.GetPlatform ());
3044
3045 if (platform_sp)
3046 {
3047 ProcessInstanceInfoMatch match_info;
3048 match_info.GetProcessInfo() = attach_info;
3049 match_info.SetNameMatchType (eNameMatchEquals);
3050 platform_sp->FindProcesses (match_info, process_infos);
3051 const uint32_t num_matches = process_infos.GetSize();
3052 if (num_matches == 1)
3053 {
3054 attach_pid = process_infos.GetProcessIDAtIndex(0);
3055 // Fall through and attach using the above process ID
3056 }
3057 else
3058 {
3059 match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name));
3060 if (num_matches > 1)
Jim Ingham368ac222014-08-15 17:05:27 +00003061 {
3062 StreamString s;
3063 ProcessInstanceInfo::DumpTableHeader (s, platform_sp.get(), true, false);
3064 for (size_t i = 0; i < num_matches; i++)
3065 {
3066 process_infos.GetProcessInfoAtIndex(i).DumpAsTableRow(s, platform_sp.get(), true, false);
3067 }
3068 error.SetErrorStringWithFormat ("more than one process named %s:\n%s",
3069 process_name,
3070 s.GetData());
3071 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003072 else
3073 error.SetErrorStringWithFormat ("could not find a process named %s", process_name);
3074 }
3075 }
3076 else
3077 {
3078 error.SetErrorString ("invalid platform, can't find processes by name");
3079 return error;
3080 }
Greg Claytone996fd32011-03-08 22:40:15 +00003081 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003082 }
3083 else
Greg Clayton144f3a92011-11-15 03:53:30 +00003084 {
3085 error.SetErrorString ("invalid process name");
Greg Claytone996fd32011-03-08 22:40:15 +00003086 }
3087 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003088
3089 if (attach_pid != LLDB_INVALID_PROCESS_ID)
Greg Claytone996fd32011-03-08 22:40:15 +00003090 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003091 error = WillAttachToProcessWithID(attach_pid);
Greg Claytone996fd32011-03-08 22:40:15 +00003092 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003093 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003094
Ed Maste64fad602013-07-29 20:58:06 +00003095 if (m_public_run_lock.TrySetRunning())
Greg Clayton926cce72012-10-12 16:10:12 +00003096 {
3097 // Now attach using these arguments.
3098 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003099 const bool restarted = false;
3100 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003101 error = DoAttachToProcessWithID (attach_pid, attach_info);
3102 }
3103 else
3104 {
3105 // This shouldn't happen
3106 error.SetErrorString("failed to acquire process run lock");
3107 }
3108
Greg Clayton144f3a92011-11-15 03:53:30 +00003109 if (error.Success())
3110 {
3111
3112 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3113 StartPrivateStateThread();
3114 }
3115 else
Greg Claytone996fd32011-03-08 22:40:15 +00003116 {
3117 if (GetID() != LLDB_INVALID_PROCESS_ID)
3118 {
3119 SetID (LLDB_INVALID_PROCESS_ID);
3120 const char *error_string = error.AsCString();
3121 if (error_string == NULL)
3122 error_string = "attach failed";
3123
3124 SetExitStatus(-1, error_string);
3125 }
3126 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003127 }
3128 }
3129 return error;
3130}
3131
Greg Clayton93d3c8332011-02-16 04:46:07 +00003132void
3133Process::CompleteAttach ()
3134{
Todd Fiala76e0fc92014-08-27 22:58:26 +00003135 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3136 if (log)
3137 log->Printf ("Process::%s()", __FUNCTION__);
3138
Greg Clayton93d3c8332011-02-16 04:46:07 +00003139 // Let the process subclass figure out at much as it can about the process
3140 // before we go looking for a dynamic loader plug-in.
Jim Inghambb006ce2014-08-02 00:33:35 +00003141 ArchSpec process_arch;
3142 DidAttach(process_arch);
3143
3144 if (process_arch.IsValid())
Todd Fiala76e0fc92014-08-27 22:58:26 +00003145 {
Jim Inghambb006ce2014-08-02 00:33:35 +00003146 m_target.SetArchitecture(process_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003147 if (log)
3148 {
3149 const char *triple_str = process_arch.GetTriple().getTriple().c_str ();
3150 log->Printf ("Process::%s replacing process architecture with DidAttach() architecture: %s",
3151 __FUNCTION__,
3152 triple_str ? triple_str : "<null>");
3153 }
3154 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003155
Jim Ingham4299fdb2011-09-15 01:10:17 +00003156 // We just attached. If we have a platform, ask it for the process architecture, and if it isn't
3157 // the same as the one we've already set, switch architectures.
3158 PlatformSP platform_sp (m_target.GetPlatform ());
3159 assert (platform_sp.get());
3160 if (platform_sp)
3161 {
Greg Clayton70512312012-05-08 01:45:38 +00003162 const ArchSpec &target_arch = m_target.GetArchitecture();
Greg Clayton1e0c8842013-01-11 20:49:54 +00003163 if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL))
Greg Clayton70512312012-05-08 01:45:38 +00003164 {
3165 ArchSpec platform_arch;
3166 platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch);
3167 if (platform_sp)
3168 {
3169 m_target.SetPlatform (platform_sp);
3170 m_target.SetArchitecture(platform_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003171 if (log)
3172 log->Printf ("Process::%s switching platform to %s and architecture to %s based on info from attach", __FUNCTION__, platform_sp->GetName().AsCString (""), platform_arch.GetTriple().getTriple().c_str ());
Greg Clayton70512312012-05-08 01:45:38 +00003173 }
3174 }
Jim Inghambb006ce2014-08-02 00:33:35 +00003175 else if (!process_arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00003176 {
3177 ProcessInstanceInfo process_info;
3178 platform_sp->GetProcessInfo (GetID(), process_info);
3179 const ArchSpec &process_arch = process_info.GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +00003180 if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch))
Todd Fiala76e0fc92014-08-27 22:58:26 +00003181 {
Greg Clayton70512312012-05-08 01:45:38 +00003182 m_target.SetArchitecture (process_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003183 if (log)
3184 log->Printf ("Process::%s switching architecture to %s based on info the platform retrieved for pid %" PRIu64, __FUNCTION__, process_arch.GetTriple().getTriple().c_str (), GetID ());
3185 }
Greg Clayton70512312012-05-08 01:45:38 +00003186 }
Jim Ingham4299fdb2011-09-15 01:10:17 +00003187 }
3188
3189 // We have completed the attach, now it is time to find the dynamic loader
Greg Clayton93d3c8332011-02-16 04:46:07 +00003190 // plug-in
Greg Claytonc859e2d2012-02-13 23:10:39 +00003191 DynamicLoader *dyld = GetDynamicLoader ();
3192 if (dyld)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003193 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00003194 dyld->DidAttach();
Todd Fiala76e0fc92014-08-27 22:58:26 +00003195 if (log)
3196 {
3197 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3198 log->Printf ("Process::%s after DynamicLoader::DidAttach(), target executable is %s (using %s plugin)",
3199 __FUNCTION__,
3200 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>",
3201 dyld->GetPluginName().AsCString ("<unnamed>"));
3202 }
3203 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003204
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00003205 GetJITLoaders().DidAttach();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003206
Jason Molendaeef51062013-11-05 03:57:19 +00003207 SystemRuntime *system_runtime = GetSystemRuntime ();
3208 if (system_runtime)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003209 {
Jason Molendaeef51062013-11-05 03:57:19 +00003210 system_runtime->DidAttach();
Todd Fiala76e0fc92014-08-27 22:58:26 +00003211 if (log)
3212 {
3213 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3214 log->Printf ("Process::%s after SystemRuntime::DidAttach(), target executable is %s (using %s plugin)",
3215 __FUNCTION__,
3216 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>",
3217 system_runtime->GetPluginName().AsCString("<unnamed>"));
3218 }
3219 }
Jason Molendaeef51062013-11-05 03:57:19 +00003220
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003221 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Clayton93d3c8332011-02-16 04:46:07 +00003222 // Figure out which one is the executable, and set that in our target:
Enrico Granata17598482012-11-08 02:22:02 +00003223 const ModuleList &target_modules = m_target.GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003224 Mutex::Locker modules_locker(target_modules.GetMutex());
3225 size_t num_modules = target_modules.GetSize();
3226 ModuleSP new_executable_module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003227
Andy Gibbsa297a972013-06-19 19:04:53 +00003228 for (size_t i = 0; i < num_modules; i++)
Greg Clayton93d3c8332011-02-16 04:46:07 +00003229 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003230 ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i));
Greg Clayton8b82f082011-04-12 05:54:46 +00003231 if (module_sp && module_sp->IsExecutable())
Greg Clayton93d3c8332011-02-16 04:46:07 +00003232 {
Greg Claytonaa149cb2011-08-11 02:48:45 +00003233 if (m_target.GetExecutableModulePointer() != module_sp.get())
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003234 new_executable_module_sp = module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003235 break;
3236 }
3237 }
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003238 if (new_executable_module_sp)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003239 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003240 m_target.SetExecutableModule (new_executable_module_sp, false);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003241 if (log)
3242 {
3243 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3244 log->Printf ("Process::%s after looping through modules, target executable is %s",
3245 __FUNCTION__,
3246 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>");
3247 }
3248 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003249}
3250
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003251Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003252Process::ConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +00003253{
Greg Claytonb766a732011-02-04 01:58:07 +00003254 m_abi_sp.reset();
3255 m_process_input_reader.reset();
3256
3257 // Find the process and its architecture. Make sure it matches the architecture
3258 // of the current Target, and if not adjust it.
3259
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003260 Error error (DoConnectRemote (strm, remote_url));
Greg Claytonb766a732011-02-04 01:58:07 +00003261 if (error.Success())
3262 {
Greg Clayton71337622011-02-24 22:24:29 +00003263 if (GetID() != LLDB_INVALID_PROCESS_ID)
3264 {
Greg Clayton32e0a752011-03-30 18:16:51 +00003265 EventSP event_sp;
3266 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
3267
3268 if (state == eStateStopped || state == eStateCrashed)
3269 {
3270 // If we attached and actually have a process on the other end, then
3271 // this ended up being the equivalent of an attach.
3272 CompleteAttach ();
3273
3274 // This delays passing the stopped event to listeners till
3275 // CompleteAttach gets a chance to complete...
3276 HandlePrivateEvent (event_sp);
3277
3278 }
Greg Clayton71337622011-02-24 22:24:29 +00003279 }
Greg Clayton32e0a752011-03-30 18:16:51 +00003280
3281 if (PrivateStateThreadIsValid ())
3282 ResumePrivateStateThread ();
3283 else
3284 StartPrivateStateThread ();
Greg Claytonb766a732011-02-04 01:58:07 +00003285 }
3286 return error;
3287}
3288
3289
3290Error
Jim Ingham3b8285d2012-04-19 01:40:33 +00003291Process::PrivateResume ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003292{
Greg Clayton5160ce52013-03-27 23:08:40 +00003293 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003294 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003295 log->Printf("Process::PrivateResume() m_stop_id = %u, public state: %s private state: %s",
Jim Ingham4b536182011-08-09 02:12:22 +00003296 m_mod_id.GetStopID(),
Jim Ingham444586b2011-01-24 06:34:17 +00003297 StateAsCString(m_public_state.GetValue()),
3298 StateAsCString(m_private_state.GetValue()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003299
3300 Error error (WillResume());
3301 // Tell the process it is about to resume before the thread list
3302 if (error.Success())
3303 {
Johnny Chenc4221e42010-12-02 20:53:05 +00003304 // Now let the thread list know we are about to resume so it
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003305 // can let all of our threads know that they are about to be
3306 // resumed. Threads will each be called with
3307 // Thread::WillResume(StateType) where StateType contains the state
3308 // that they are supposed to have when the process is resumed
3309 // (suspended/running/stepping). Threads should also check
3310 // their resume signal in lldb::Thread::GetResumeSignal()
Jim Ingham221d51c2013-05-08 00:35:16 +00003311 // to see if they are supposed to start back up with a signal.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003312 if (m_thread_list.WillResume())
3313 {
Jim Ingham372787f2012-04-07 00:00:41 +00003314 // Last thing, do the PreResumeActions.
3315 if (!RunPreResumeActions())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003316 {
Jim Ingham0161b492013-02-09 01:29:05 +00003317 error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming.");
Jim Ingham372787f2012-04-07 00:00:41 +00003318 }
3319 else
3320 {
3321 m_mod_id.BumpResumeID();
3322 error = DoResume();
3323 if (error.Success())
3324 {
3325 DidResume();
3326 m_thread_list.DidResume();
3327 if (log)
3328 log->Printf ("Process thinks the process has resumed.");
3329 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003330 }
3331 }
3332 else
3333 {
Jim Ingham513c6bb2012-09-01 01:02:41 +00003334 // Somebody wanted to run without running. So generate a continue & a stopped event,
3335 // and let the world handle them.
3336 if (log)
3337 log->Printf ("Process::PrivateResume() asked to simulate a start & stop.");
3338
3339 SetPrivateState(eStateRunning);
3340 SetPrivateState(eStateStopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003341 }
3342 }
Jim Ingham444586b2011-01-24 06:34:17 +00003343 else if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003344 log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003345 return error;
3346}
3347
3348Error
Greg Claytonf9b57b92013-05-10 23:48:10 +00003349Process::Halt (bool clear_thread_plans)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003350{
Greg Claytonf9b57b92013-05-10 23:48:10 +00003351 // Don't clear the m_clear_thread_plans_on_stop, only set it to true if
3352 // in case it was already set and some thread plan logic calls halt on its
3353 // own.
3354 m_clear_thread_plans_on_stop |= clear_thread_plans;
3355
Jim Inghamaacc3182012-06-06 00:29:30 +00003356 // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since
3357 // we could just straightaway get another event. It just narrows the window...
3358 m_currently_handling_event.WaitForValueEqualTo(false);
3359
3360
Jim Inghambb3a2832011-01-29 01:49:25 +00003361 // Pause our private state thread so we can ensure no one else eats
3362 // the stop event out from under us.
Jim Ingham0f16e732011-02-08 05:20:59 +00003363 Listener halt_listener ("lldb.process.halt_listener");
3364 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton3af9ea52010-11-18 05:57:03 +00003365
Jim Inghambb3a2832011-01-29 01:49:25 +00003366 EventSP event_sp;
Greg Clayton513c26c2011-01-29 07:10:55 +00003367 Error error (WillHalt());
Jim Inghambb3a2832011-01-29 01:49:25 +00003368
Greg Clayton06357c92014-07-30 17:38:47 +00003369 bool restored_process_events = false;
Greg Clayton513c26c2011-01-29 07:10:55 +00003370 if (error.Success())
Jim Inghambb3a2832011-01-29 01:49:25 +00003371 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003372
Greg Clayton513c26c2011-01-29 07:10:55 +00003373 bool caused_stop = false;
3374
3375 // Ask the process subclass to actually halt our process
3376 error = DoHalt(caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003377 if (error.Success())
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003378 {
Greg Clayton513c26c2011-01-29 07:10:55 +00003379 if (m_public_state.GetValue() == eStateAttaching)
3380 {
Greg Clayton06357c92014-07-30 17:38:47 +00003381 // Don't hijack and eat the eStateExited as the code that was doing
3382 // the attach will be waiting for this event...
3383 RestorePrivateProcessEvents();
3384 restored_process_events = true;
Greg Clayton513c26c2011-01-29 07:10:55 +00003385 SetExitStatus(SIGKILL, "Cancelled async attach.");
3386 Destroy ();
3387 }
3388 else
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003389 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003390 // If "caused_stop" is true, then DoHalt stopped the process. If
3391 // "caused_stop" is false, the process was already stopped.
3392 // If the DoHalt caused the process to stop, then we want to catch
3393 // this event and set the interrupted bool to true before we pass
3394 // this along so clients know that the process was interrupted by
3395 // a halt command.
3396 if (caused_stop)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003397 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003398 // Wait for 1 second for the process to stop.
Jim Inghambb3a2832011-01-29 01:49:25 +00003399 TimeValue timeout_time;
3400 timeout_time = TimeValue::Now();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003401 timeout_time.OffsetWithSeconds(10);
Jim Ingham0f16e732011-02-08 05:20:59 +00003402 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
3403 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003404
Jim Ingham0f16e732011-02-08 05:20:59 +00003405 if (!got_event || state == eStateInvalid)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003406 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003407 // We timeout out and didn't get a stop event...
Jim Ingham0f16e732011-02-08 05:20:59 +00003408 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton3af9ea52010-11-18 05:57:03 +00003409 }
3410 else
3411 {
Greg Clayton2637f822011-11-17 01:23:07 +00003412 if (StateIsStoppedState (state, false))
Jim Inghambb3a2832011-01-29 01:49:25 +00003413 {
3414 // We caused the process to interrupt itself, so mark this
3415 // as such in the stop event so clients can tell an interrupted
3416 // process from a natural stop
3417 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
3418 }
3419 else
3420 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003421 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghambb3a2832011-01-29 01:49:25 +00003422 if (log)
3423 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
3424 error.SetErrorString ("Did not get stopped event after halt.");
3425 }
Greg Clayton3af9ea52010-11-18 05:57:03 +00003426 }
3427 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003428 DidHalt();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003429 }
3430 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003431 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003432 // Resume our private state thread before we post the event (if any)
Greg Clayton06357c92014-07-30 17:38:47 +00003433 if (!restored_process_events)
3434 RestorePrivateProcessEvents();
Jim Inghambb3a2832011-01-29 01:49:25 +00003435
3436 // Post any event we might have consumed. If all goes well, we will have
3437 // stopped the process, intercepted the event and set the interrupted
3438 // bool in the event. Post it to the private event queue and that will end up
3439 // correctly setting the state.
3440 if (event_sp)
3441 m_private_state_broadcaster.BroadcastEvent(event_sp);
3442
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003443 return error;
3444}
3445
3446Error
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003447Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp)
3448{
3449 Error error;
3450 if (m_public_state.GetValue() == eStateRunning)
3451 {
3452 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3453 if (log)
3454 log->Printf("Process::Destroy() About to halt.");
3455 error = Halt();
3456 if (error.Success())
3457 {
3458 // Consume the halt event.
3459 TimeValue timeout (TimeValue::Now());
3460 timeout.OffsetWithSeconds(1);
3461 StateType state = WaitForProcessToStop (&timeout, &exit_event_sp);
3462
3463 // If the process exited while we were waiting for it to stop, put the exited event into
3464 // the shared pointer passed in and return. Our caller doesn't need to do anything else, since
3465 // they don't have a process anymore...
3466
3467 if (state == eStateExited || m_private_state.GetValue() == eStateExited)
3468 {
3469 if (log)
3470 log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt.");
3471 return error;
3472 }
3473 else
3474 exit_event_sp.reset(); // It is ok to consume any non-exit stop events
3475
3476 if (state != eStateStopped)
3477 {
3478 if (log)
3479 log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state));
3480 // If we really couldn't stop the process then we should just error out here, but if the
3481 // lower levels just bobbled sending the event and we really are stopped, then continue on.
3482 StateType private_state = m_private_state.GetValue();
3483 if (private_state != eStateStopped)
3484 {
3485 return error;
3486 }
3487 }
3488 }
3489 else
3490 {
3491 if (log)
3492 log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString());
3493 }
3494 }
3495 return error;
3496}
3497
3498Error
Jim Inghamacff8952013-05-02 00:27:30 +00003499Process::Detach (bool keep_stopped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003500{
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003501 EventSP exit_event_sp;
3502 Error error;
3503 m_destroy_in_process = true;
3504
3505 error = WillDetach();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003506
3507 if (error.Success())
3508 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003509 if (DetachRequiresHalt())
3510 {
3511 error = HaltForDestroyOrDetach (exit_event_sp);
3512 if (!error.Success())
3513 {
3514 m_destroy_in_process = false;
3515 return error;
3516 }
3517 else if (exit_event_sp)
3518 {
3519 // We shouldn't need to do anything else here. There's no process left to detach from...
3520 StopPrivateStateThread();
3521 m_destroy_in_process = false;
3522 return error;
3523 }
3524 }
3525
Andrew MacPhersonc3826b52014-03-25 19:59:36 +00003526 m_thread_list.DiscardThreadPlans();
3527 DisableAllBreakpointSites();
3528
Jim Inghamacff8952013-05-02 00:27:30 +00003529 error = DoDetach(keep_stopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003530 if (error.Success())
3531 {
3532 DidDetach();
3533 StopPrivateStateThread();
3534 }
Jim Inghamacff8952013-05-02 00:27:30 +00003535 else
3536 {
3537 return error;
3538 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003539 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003540 m_destroy_in_process = false;
3541
3542 // If we exited when we were waiting for a process to stop, then
3543 // forward the event here so we don't lose the event
3544 if (exit_event_sp)
3545 {
3546 // Directly broadcast our exited event because we shut down our
3547 // private state thread above
3548 BroadcastEvent(exit_event_sp);
3549 }
3550
3551 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3552 // the last events through the event system, in which case we might strand the write lock. Unlock
3553 // it here so when we do to tear down the process we don't get an error destroying the lock.
3554
Ed Maste64fad602013-07-29 20:58:06 +00003555 m_public_run_lock.SetStopped();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003556 return error;
3557}
3558
3559Error
3560Process::Destroy ()
3561{
Jim Ingham09437922013-03-01 20:04:25 +00003562
3563 // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work
3564 // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt
3565 // failed and the process stays around for some reason it won't be in a confused state.
3566
3567 m_destroy_in_process = true;
3568
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003569 Error error (WillDestroy());
3570 if (error.Success())
3571 {
Greg Clayton85fb1b92012-09-11 02:33:37 +00003572 EventSP exit_event_sp;
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003573 if (DestroyRequiresHalt())
Jim Ingham04e0a222012-05-23 15:46:31 +00003574 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003575 error = HaltForDestroyOrDetach(exit_event_sp);
Jim Ingham04e0a222012-05-23 15:46:31 +00003576 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003577
Jim Inghamaacc3182012-06-06 00:29:30 +00003578 if (m_public_state.GetValue() != eStateRunning)
3579 {
3580 // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to
3581 // kill it, we don't want it hitting a breakpoint...
3582 // Only do this if we've stopped, however, since if we didn't manage to halt it above, then
3583 // we're not going to have much luck doing this now.
3584 m_thread_list.DiscardThreadPlans();
3585 DisableAllBreakpointSites();
3586 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003587
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003588 error = DoDestroy();
3589 if (error.Success())
3590 {
3591 DidDestroy();
3592 StopPrivateStateThread();
3593 }
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003594 m_stdio_communication.StopReadThread();
3595 m_stdio_communication.Disconnect();
Greg Claytonb4874f12014-02-28 18:22:24 +00003596
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003597 if (m_process_input_reader)
Greg Claytonb4874f12014-02-28 18:22:24 +00003598 {
3599 m_process_input_reader->SetIsDone(true);
3600 m_process_input_reader->Cancel();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003601 m_process_input_reader.reset();
Greg Claytonb4874f12014-02-28 18:22:24 +00003602 }
3603
Greg Clayton85fb1b92012-09-11 02:33:37 +00003604 // If we exited when we were waiting for a process to stop, then
3605 // forward the event here so we don't lose the event
3606 if (exit_event_sp)
3607 {
3608 // Directly broadcast our exited event because we shut down our
3609 // private state thread above
3610 BroadcastEvent(exit_event_sp);
3611 }
3612
Jim Inghamb1e2e842012-04-12 18:49:31 +00003613 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3614 // the last events through the event system, in which case we might strand the write lock. Unlock
3615 // 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 +00003616 m_public_run_lock.SetStopped();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003617 }
Jim Ingham09437922013-03-01 20:04:25 +00003618
3619 m_destroy_in_process = false;
3620
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003621 return error;
3622}
3623
3624Error
3625Process::Signal (int signal)
3626{
3627 Error error (WillSignal());
3628 if (error.Success())
3629 {
3630 error = DoSignal(signal);
3631 if (error.Success())
3632 DidSignal();
3633 }
3634 return error;
3635}
3636
Greg Clayton514487e2011-02-15 21:59:32 +00003637lldb::ByteOrder
3638Process::GetByteOrder () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003639{
Greg Clayton514487e2011-02-15 21:59:32 +00003640 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003641}
3642
3643uint32_t
Greg Clayton514487e2011-02-15 21:59:32 +00003644Process::GetAddressByteSize () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003645{
Greg Clayton514487e2011-02-15 21:59:32 +00003646 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003647}
3648
Greg Clayton514487e2011-02-15 21:59:32 +00003649
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003650bool
3651Process::ShouldBroadcastEvent (Event *event_ptr)
3652{
3653 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
3654 bool return_value = true;
Greg Clayton5160ce52013-03-27 23:08:40 +00003655 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS));
Jim Ingham0161b492013-02-09 01:29:05 +00003656
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003657 switch (state)
3658 {
Greg Claytonb766a732011-02-04 01:58:07 +00003659 case eStateConnected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003660 case eStateAttaching:
3661 case eStateLaunching:
3662 case eStateDetached:
3663 case eStateExited:
3664 case eStateUnloaded:
3665 // These events indicate changes in the state of the debugging session, always report them.
3666 return_value = true;
3667 break;
3668 case eStateInvalid:
3669 // We stopped for no apparent reason, don't report it.
3670 return_value = false;
3671 break;
3672 case eStateRunning:
3673 case eStateStepping:
3674 // If we've started the target running, we handle the cases where we
3675 // are already running and where there is a transition from stopped to
3676 // running differently.
3677 // running -> running: Automatically suppress extra running events
3678 // stopped -> running: Report except when there is one or more no votes
3679 // and no yes votes.
3680 SynchronouslyNotifyStateChanged (state);
Jim Ingham1460e4b2014-01-10 23:46:59 +00003681 if (m_force_next_event_delivery)
3682 return_value = true;
3683 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003684 {
Jim Ingham1460e4b2014-01-10 23:46:59 +00003685 switch (m_last_broadcast_state)
3686 {
3687 case eStateRunning:
3688 case eStateStepping:
3689 // We always suppress multiple runnings with no PUBLIC stop in between.
3690 return_value = false;
3691 break;
3692 default:
3693 // TODO: make this work correctly. For now always report
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003694 // run if we aren't running so we don't miss any running
Jim Ingham1460e4b2014-01-10 23:46:59 +00003695 // events. If I run the lldb/test/thread/a.out file and
3696 // break at main.cpp:58, run and hit the breakpoints on
3697 // multiple threads, then somehow during the stepping over
3698 // of all breakpoints no run gets reported.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003699
Jim Ingham1460e4b2014-01-10 23:46:59 +00003700 // This is a transition from stop to run.
3701 switch (m_thread_list.ShouldReportRun (event_ptr))
3702 {
3703 case eVoteYes:
3704 case eVoteNoOpinion:
3705 return_value = true;
3706 break;
3707 case eVoteNo:
3708 return_value = false;
3709 break;
3710 }
3711 break;
3712 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003713 }
3714 break;
3715 case eStateStopped:
3716 case eStateCrashed:
3717 case eStateSuspended:
3718 {
3719 // We've stopped. First see if we're going to restart the target.
3720 // If we are going to stop, then we always broadcast the event.
3721 // 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 +00003722 // If no thread has an opinion, we don't report it.
Jim Ingham221d51c2013-05-08 00:35:16 +00003723
Jim Inghamcb4ca112012-05-16 01:32:14 +00003724 RefreshStateAfterStop ();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003725 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003726 {
Greg Clayton3af9ea52010-11-18 05:57:03 +00003727 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003728 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003729 static_cast<void*>(event_ptr),
Jim Ingham0161b492013-02-09 01:29:05 +00003730 StateAsCString(state));
Jim Ingham35878c42014-04-08 21:33:21 +00003731 // Even though we know we are going to stop, we should let the threads have a look at the stop,
3732 // so they can properly set their state.
3733 m_thread_list.ShouldStop (event_ptr);
Jim Ingham0161b492013-02-09 01:29:05 +00003734 return_value = true;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003735 }
3736 else
3737 {
Jim Ingham221d51c2013-05-08 00:35:16 +00003738 bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr);
3739 bool should_resume = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003740
Jim Ingham0161b492013-02-09 01:29:05 +00003741 // It makes no sense to ask "ShouldStop" if we've already been restarted...
3742 // Asking the thread list is also not likely to go well, since we are running again.
3743 // So in that case just report the event.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003744
Jim Ingham0161b492013-02-09 01:29:05 +00003745 if (!was_restarted)
3746 should_resume = m_thread_list.ShouldStop (event_ptr) == false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003747
Jim Ingham221d51c2013-05-08 00:35:16 +00003748 if (was_restarted || should_resume || m_resume_requested)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003749 {
Jim Ingham0161b492013-02-09 01:29:05 +00003750 Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr);
3751 if (log)
3752 log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003753 should_resume, StateAsCString(state),
3754 was_restarted, stop_vote);
3755
Jim Ingham0161b492013-02-09 01:29:05 +00003756 switch (stop_vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003757 {
3758 case eVoteYes:
Jim Ingham0161b492013-02-09 01:29:05 +00003759 return_value = true;
3760 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003761 case eVoteNoOpinion:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003762 case eVoteNo:
3763 return_value = false;
3764 break;
3765 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003766
Jim Inghamcb95f342012-09-05 21:13:56 +00003767 if (!was_restarted)
Jim Ingham0161b492013-02-09 01:29:05 +00003768 {
3769 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003770 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s",
3771 static_cast<void*>(event_ptr),
3772 StateAsCString(state));
Jim Ingham0161b492013-02-09 01:29:05 +00003773 ProcessEventData::SetRestartedInEvent(event_ptr, true);
Jim Inghamcb95f342012-09-05 21:13:56 +00003774 PrivateResume ();
Jim Ingham0161b492013-02-09 01:29:05 +00003775 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003776
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003777 }
3778 else
3779 {
3780 return_value = true;
3781 SynchronouslyNotifyStateChanged (state);
3782 }
3783 }
3784 }
Jim Ingham0161b492013-02-09 01:29:05 +00003785 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003786 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003787
Jim Ingham1460e4b2014-01-10 23:46:59 +00003788 // Forcing the next event delivery is a one shot deal. So reset it here.
3789 m_force_next_event_delivery = false;
3790
Jim Ingham0161b492013-02-09 01:29:05 +00003791 // We do some coalescing of events (for instance two consecutive running events get coalesced.)
3792 // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state
3793 // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done,
3794 // because the PublicState reflects the last event pulled off the queue, and there may be several
3795 // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event
3796 // yet. m_last_broadcast_state gets updated here.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003797
Jim Ingham0161b492013-02-09 01:29:05 +00003798 if (return_value)
3799 m_last_broadcast_state = state;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003800
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003801 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003802 log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003803 static_cast<void*>(event_ptr), StateAsCString(state),
Jim Ingham0161b492013-02-09 01:29:05 +00003804 StateAsCString(m_last_broadcast_state),
3805 return_value ? "YES" : "NO");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003806 return return_value;
3807}
3808
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003809
3810bool
Jim Ingham372787f2012-04-07 00:00:41 +00003811Process::StartPrivateStateThread (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003812{
Greg Clayton5160ce52013-03-27 23:08:40 +00003813 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003814
Greg Clayton8b82f082011-04-12 05:54:46 +00003815 bool already_running = PrivateStateThreadIsValid ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003816 if (log)
Greg Clayton8b82f082011-04-12 05:54:46 +00003817 log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread");
3818
Jim Ingham372787f2012-04-07 00:00:41 +00003819 if (!force && already_running)
Greg Clayton8b82f082011-04-12 05:54:46 +00003820 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003821
3822 // Create a thread that watches our internal state and controls which
3823 // events make it to clients (into the DCProcess event queue).
Greg Clayton3e06bd92011-01-09 21:07:35 +00003824 char thread_name[1024];
Todd Fiala17096d72014-07-16 19:03:16 +00003825
3826 if (Host::MAX_THREAD_NAME_LENGTH <= 16)
3827 {
3828 // On platforms with abbreviated thread name lengths, choose thread names that fit within the limit.
3829 if (already_running)
3830 snprintf(thread_name, sizeof(thread_name), "intern-state-OV");
3831 else
3832 snprintf(thread_name, sizeof(thread_name), "intern-state");
3833 }
Jim Ingham372787f2012-04-07 00:00:41 +00003834 else
Todd Fiala17096d72014-07-16 19:03:16 +00003835 {
3836 if (already_running)
3837 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
3838 else
3839 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
3840 }
3841
Jim Ingham076b3042012-04-10 01:21:57 +00003842 // Create the private state thread, and start it running.
Greg Clayton3e06bd92011-01-09 21:07:35 +00003843 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Jim Ingham076b3042012-04-10 01:21:57 +00003844 bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
3845 if (success)
3846 {
3847 ResumePrivateStateThread();
3848 return true;
3849 }
3850 else
3851 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003852}
3853
3854void
3855Process::PausePrivateStateThread ()
3856{
3857 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
3858}
3859
3860void
3861Process::ResumePrivateStateThread ()
3862{
3863 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
3864}
3865
3866void
3867Process::StopPrivateStateThread ()
3868{
Greg Clayton8b82f082011-04-12 05:54:46 +00003869 if (PrivateStateThreadIsValid ())
3870 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
Jim Inghamb1e2e842012-04-12 18:49:31 +00003871 else
3872 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003873 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Jim Inghamb1e2e842012-04-12 18:49:31 +00003874 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003875 log->Printf ("Went to stop the private state thread, but it was already invalid.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00003876 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003877}
3878
3879void
3880Process::ControlPrivateStateThread (uint32_t signal)
3881{
Greg Clayton5160ce52013-03-27 23:08:40 +00003882 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003883
3884 assert (signal == eBroadcastInternalStateControlStop ||
3885 signal == eBroadcastInternalStateControlPause ||
3886 signal == eBroadcastInternalStateControlResume);
3887
3888 if (log)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003889 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003890
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003891 // Signal the private state thread. First we should copy this is case the
3892 // thread starts exiting since the private state thread will NULL this out
3893 // when it exits
3894 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton2da6d492011-02-08 01:34:25 +00003895 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003896 {
3897 TimeValue timeout_time;
3898 bool timed_out;
3899
3900 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
3901
3902 timeout_time = TimeValue::Now();
3903 timeout_time.OffsetWithSeconds(2);
Jim Inghamb1e2e842012-04-12 18:49:31 +00003904 if (log)
3905 log->Printf ("Sending control event of type: %d.", signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003906 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
3907 m_private_state_control_wait.SetValue (false, eBroadcastNever);
3908
3909 if (signal == eBroadcastInternalStateControlStop)
3910 {
3911 if (timed_out)
Jim Inghamb1e2e842012-04-12 18:49:31 +00003912 {
3913 Error error;
3914 Host::ThreadCancel (private_state_thread, &error);
3915 if (log)
3916 log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString());
3917 }
3918 else
3919 {
3920 if (log)
3921 log->Printf ("The control event killed the private state thread without having to cancel.");
3922 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003923
3924 thread_result_t result = NULL;
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003925 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Clayton49182ed2010-07-22 18:34:21 +00003926 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003927 }
3928 }
Jim Inghamb1e2e842012-04-12 18:49:31 +00003929 else
3930 {
3931 if (log)
3932 log->Printf ("Private state thread already dead, no need to signal it to stop.");
3933 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003934}
3935
3936void
Jim Inghamcfc09352012-07-27 23:57:19 +00003937Process::SendAsyncInterrupt ()
3938{
3939 if (PrivateStateThreadIsValid())
3940 m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
3941 else
3942 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
3943}
3944
3945void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003946Process::HandlePrivateEvent (EventSP &event_sp)
3947{
Greg Clayton5160ce52013-03-27 23:08:40 +00003948 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham221d51c2013-05-08 00:35:16 +00003949 m_resume_requested = false;
3950
Jim Inghamaacc3182012-06-06 00:29:30 +00003951 m_currently_handling_event.SetValue(true, eBroadcastNever);
Jim Inghambb3a2832011-01-29 01:49:25 +00003952
Greg Clayton414f5d32011-01-25 02:58:48 +00003953 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003954
3955 // First check to see if anybody wants a shot at this event:
Jim Ingham754ab982011-01-29 04:05:41 +00003956 if (m_next_event_action_ap.get() != NULL)
Jim Inghambb3a2832011-01-29 01:49:25 +00003957 {
Jim Ingham754ab982011-01-29 04:05:41 +00003958 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Ingham0161b492013-02-09 01:29:05 +00003959 if (log)
3960 log->Printf ("Ran next event action, result was %d.", action_result);
3961
Jim Inghambb3a2832011-01-29 01:49:25 +00003962 switch (action_result)
3963 {
3964 case NextEventAction::eEventActionSuccess:
3965 SetNextEventAction(NULL);
3966 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003967
Jim Inghambb3a2832011-01-29 01:49:25 +00003968 case NextEventAction::eEventActionRetry:
3969 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003970
Jim Inghambb3a2832011-01-29 01:49:25 +00003971 case NextEventAction::eEventActionExit:
Jim Ingham2a5fdd42011-01-29 01:57:31 +00003972 // Handle Exiting Here. If we already got an exited event,
3973 // we should just propagate it. Otherwise, swallow this event,
3974 // and set our state to exit so the next event will kill us.
3975 if (new_state != eStateExited)
3976 {
3977 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham754ab982011-01-29 04:05:41 +00003978 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham221d51c2013-05-08 00:35:16 +00003979 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Jim Ingham2a5fdd42011-01-29 01:57:31 +00003980 SetNextEventAction(NULL);
3981 return;
3982 }
3983 SetNextEventAction(NULL);
Jim Inghambb3a2832011-01-29 01:49:25 +00003984 break;
3985 }
3986 }
3987
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003988 // See if we should broadcast this state to external clients?
3989 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003990
3991 if (should_broadcast)
3992 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003993 const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003994 if (log)
3995 {
Daniel Malead01b2952012-11-29 21:49:15 +00003996 log->Printf ("Process::%s (pid = %" PRIu64 ") broadcasting new state %s (old state %s) to %s",
Greg Clayton414f5d32011-01-25 02:58:48 +00003997 __FUNCTION__,
3998 GetID(),
3999 StateAsCString(new_state),
4000 StateAsCString (GetState ()),
Greg Claytonb4874f12014-02-28 18:22:24 +00004001 is_hijacked ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004002 }
Jim Ingham9575d842011-03-11 03:53:59 +00004003 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton414f5d32011-01-25 02:58:48 +00004004 if (StateIsRunningState (new_state))
Greg Clayton44d93782014-01-27 23:43:24 +00004005 {
4006 // Only push the input handler if we aren't fowarding events,
4007 // as this means the curses GUI is in use...
4008 if (!GetTarget().GetDebugger().IsForwardingEvents())
4009 PushProcessIOHandler ();
Todd Fialaa3b89e22014-08-12 14:33:19 +00004010 m_iohandler_sync.SetValue(true, eBroadcastAlways);
Greg Clayton44d93782014-01-27 23:43:24 +00004011 }
Greg Claytonb4874f12014-02-28 18:22:24 +00004012 else if (StateIsStoppedState(new_state, false))
4013 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00004014 m_iohandler_sync.SetValue(false, eBroadcastNever);
Greg Claytonb4874f12014-02-28 18:22:24 +00004015 if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
4016 {
4017 // If the lldb_private::Debugger is handling the events, we don't
4018 // want to pop the process IOHandler here, we want to do it when
4019 // we receive the stopped event so we can carefully control when
4020 // the process IOHandler is popped because when we stop we want to
4021 // display some text stating how and why we stopped, then maybe some
4022 // process/thread/frame info, and then we want the "(lldb) " prompt
4023 // to show up. If we pop the process IOHandler here, then we will
4024 // cause the command interpreter to become the top IOHandler after
4025 // the process pops off and it will update its prompt right away...
4026 // See the Debugger.cpp file where it calls the function as
4027 // "process_sp->PopProcessIOHandler()" to see where I am talking about.
4028 // Otherwise we end up getting overlapping "(lldb) " prompts and
4029 // garbled output.
4030 //
4031 // If we aren't handling the events in the debugger (which is indicated
4032 // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or we
4033 // are hijacked, then we always pop the process IO handler manually.
4034 // Hijacking happens when the internal process state thread is running
4035 // thread plans, or when commands want to run in synchronous mode
4036 // and they call "process->WaitForProcessToStop()". An example of something
4037 // that will hijack the events is a simple expression:
4038 //
4039 // (lldb) expr (int)puts("hello")
4040 //
4041 // This will cause the internal process state thread to resume and halt
4042 // the process (and _it_ will hijack the eBroadcastBitStateChanged
4043 // events) and we do need the IO handler to be pushed and popped
4044 // correctly.
4045
4046 if (is_hijacked || m_target.GetDebugger().IsHandlingEvents() == false)
4047 PopProcessIOHandler ();
4048 }
4049 }
Jim Ingham9575d842011-03-11 03:53:59 +00004050
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004051 BroadcastEvent (event_sp);
4052 }
4053 else
4054 {
4055 if (log)
4056 {
Daniel Malead01b2952012-11-29 21:49:15 +00004057 log->Printf ("Process::%s (pid = %" PRIu64 ") suppressing state %s (old state %s): should_broadcast == false",
Greg Clayton414f5d32011-01-25 02:58:48 +00004058 __FUNCTION__,
4059 GetID(),
4060 StateAsCString(new_state),
Jason Molendafd54b362011-09-20 21:44:10 +00004061 StateAsCString (GetState ()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004062 }
4063 }
Jim Inghamaacc3182012-06-06 00:29:30 +00004064 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004065}
4066
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004067thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004068Process::PrivateStateThread (void *arg)
4069{
4070 Process *proc = static_cast<Process*> (arg);
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004071 thread_result_t result = proc->RunPrivateStateThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004072 return result;
4073}
4074
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004075thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004076Process::RunPrivateStateThread ()
4077{
Jim Ingham076b3042012-04-10 01:21:57 +00004078 bool control_only = true;
Jim Inghamb1e2e842012-04-12 18:49:31 +00004079 m_private_state_control_wait.SetValue (false, eBroadcastNever);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004080
Greg Clayton5160ce52013-03-27 23:08:40 +00004081 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004082 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004083 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...",
4084 __FUNCTION__, static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004085
4086 bool exit_now = false;
4087 while (!exit_now)
4088 {
4089 EventSP event_sp;
4090 WaitForEventsPrivate (NULL, event_sp, control_only);
4091 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
4092 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00004093 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004094 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d",
4095 __FUNCTION__, static_cast<void*>(this), GetID(),
4096 event_sp->GetType());
Jim Inghamb1e2e842012-04-12 18:49:31 +00004097
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004098 switch (event_sp->GetType())
4099 {
4100 case eBroadcastInternalStateControlStop:
4101 exit_now = true;
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004102 break; // doing any internal state management below
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004103
4104 case eBroadcastInternalStateControlPause:
4105 control_only = true;
4106 break;
4107
4108 case eBroadcastInternalStateControlResume:
4109 control_only = false;
4110 break;
4111 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004112
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004113 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004114 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004115 }
Jim Inghamcfc09352012-07-27 23:57:19 +00004116 else if (event_sp->GetType() == eBroadcastBitInterrupt)
4117 {
4118 if (m_public_state.GetValue() == eStateAttaching)
4119 {
4120 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004121 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.",
4122 __FUNCTION__, static_cast<void*>(this),
4123 GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004124 BroadcastEvent (eBroadcastBitInterrupt, NULL);
4125 }
4126 else
4127 {
4128 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004129 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.",
4130 __FUNCTION__, static_cast<void*>(this),
4131 GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004132 Halt();
4133 }
4134 continue;
4135 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004136
4137 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
4138
4139 if (internal_state != eStateInvalid)
4140 {
Greg Claytonf9b57b92013-05-10 23:48:10 +00004141 if (m_clear_thread_plans_on_stop &&
4142 StateIsStoppedState(internal_state, true))
4143 {
4144 m_clear_thread_plans_on_stop = false;
4145 m_thread_list.DiscardThreadPlans();
4146 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004147 HandlePrivateEvent (event_sp);
4148 }
4149
Greg Clayton58d1c9a2010-10-18 04:14:23 +00004150 if (internal_state == eStateInvalid ||
4151 internal_state == eStateExited ||
4152 internal_state == eStateDetached )
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004153 {
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004154 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004155 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...",
4156 __FUNCTION__, static_cast<void*>(this), GetID(),
4157 StateAsCString(internal_state));
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004158
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004159 break;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004160 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004161 }
4162
Caroline Tice20ad3c42010-10-29 21:48:37 +00004163 // Verify log is still enabled before attempting to write to it...
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004164 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004165 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...",
4166 __FUNCTION__, static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004167
Ed Maste64fad602013-07-29 20:58:06 +00004168 m_public_run_lock.SetStopped();
Greg Clayton6ed95942011-01-22 07:12:45 +00004169 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
4170 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004171 return NULL;
4172}
4173
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004174//------------------------------------------------------------------
4175// Process Event Data
4176//------------------------------------------------------------------
4177
4178Process::ProcessEventData::ProcessEventData () :
4179 EventData (),
4180 m_process_sp (),
4181 m_state (eStateInvalid),
Greg Claytonc982c762010-07-09 20:39:50 +00004182 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004183 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004184 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004185{
4186}
4187
4188Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
4189 EventData (),
4190 m_process_sp (process_sp),
4191 m_state (state),
Greg Claytonc982c762010-07-09 20:39:50 +00004192 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004193 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004194 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004195{
4196}
4197
4198Process::ProcessEventData::~ProcessEventData()
4199{
4200}
4201
4202const ConstString &
4203Process::ProcessEventData::GetFlavorString ()
4204{
4205 static ConstString g_flavor ("Process::ProcessEventData");
4206 return g_flavor;
4207}
4208
4209const ConstString &
4210Process::ProcessEventData::GetFlavor () const
4211{
4212 return ProcessEventData::GetFlavorString ();
4213}
4214
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004215void
4216Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
4217{
4218 // This function gets called twice for each event, once when the event gets pulled
Jim Inghama8604692011-05-22 21:45:01 +00004219 // off of the private process event queue, and then any number of times, first when it gets pulled off of
4220 // the public event queue, then other times when we're pretending that this is where we stopped at the
4221 // end of expression evaluation. m_update_state is used to distinguish these
4222 // three cases; it is 0 when we're just pulling it off for private handling,
Jim Ingham221d51c2013-05-08 00:35:16 +00004223 // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then.
Jim Inghama8604692011-05-22 21:45:01 +00004224 if (m_update_state != 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004225 return;
Jim Ingham0161b492013-02-09 01:29:05 +00004226
Jim Ingham221d51c2013-05-08 00:35:16 +00004227 m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr));
Jim Ingham35878c42014-04-08 21:33:21 +00004228
4229 // If this is a halt event, even if the halt stopped with some reason other than a plain interrupt (e.g. we had
4230 // already stopped for a breakpoint when the halt request came through) don't do the StopInfo actions, as they may
4231 // end up restarting the process.
4232 if (m_interrupted)
4233 return;
4234
4235 // If we're stopped and haven't restarted, then do the StopInfo actions here:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004236 if (m_state == eStateStopped && ! m_restarted)
Jim Ingham0faa43f2011-11-08 03:00:11 +00004237 {
4238 ThreadList &curr_thread_list = m_process_sp->GetThreadList();
Greg Clayton61e7a582011-12-01 23:28:38 +00004239 uint32_t num_threads = curr_thread_list.GetSize();
4240 uint32_t idx;
Greg Claytonf4b47e12010-08-04 01:40:35 +00004241
Jim Ingham4b536182011-08-09 02:12:22 +00004242 // The actions might change one of the thread's stop_info's opinions about whether we should
4243 // stop the process, so we need to query that as we go.
Jim Ingham0faa43f2011-11-08 03:00:11 +00004244
4245 // One other complication here, is that we try to catch any case where the target has run (except for expressions)
4246 // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and
4247 // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like
4248 // 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
4249 // against this list & bag out if anything differs.
Greg Clayton61e7a582011-12-01 23:28:38 +00004250 std::vector<uint32_t> thread_index_array(num_threads);
Jim Ingham0faa43f2011-11-08 03:00:11 +00004251 for (idx = 0; idx < num_threads; ++idx)
4252 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID();
4253
Jim Inghamc7078c22012-12-13 22:24:15 +00004254 // Use this to track whether we should continue from here. We will only continue the target running if
4255 // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running,
4256 // then it doesn't matter what the other threads say...
4257
4258 bool still_should_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +00004259
Jim Ingham0ad7e052013-04-25 02:04:59 +00004260 // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a
4261 // valid stop reason. In that case we should just stop, because we have no way of telling what the right
4262 // thing to do is, and it's better to let the user decide than continue behind their backs.
4263
4264 bool does_anybody_have_an_opinion = false;
4265
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004266 for (idx = 0; idx < num_threads; ++idx)
4267 {
Jim Ingham0faa43f2011-11-08 03:00:11 +00004268 curr_thread_list = m_process_sp->GetThreadList();
4269 if (curr_thread_list.GetSize() != num_threads)
4270 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004271 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004272 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004273 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 +00004274 break;
4275 }
4276
4277 lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx);
4278
4279 if (thread_sp->GetIndexID() != thread_index_array[idx])
4280 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004281 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004282 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004283 log->Printf("The thread at position %u changed from %u to %u while processing event.",
Jim Ingham87c665f2011-12-01 20:26:15 +00004284 idx,
4285 thread_index_array[idx],
4286 thread_sp->GetIndexID());
Jim Ingham0faa43f2011-11-08 03:00:11 +00004287 break;
4288 }
4289
Jim Inghamb15bfc72010-10-20 00:39:53 +00004290 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00004291 if (stop_info_sp && stop_info_sp->IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004292 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004293 does_anybody_have_an_opinion = true;
Jim Ingham0161b492013-02-09 01:29:05 +00004294 bool this_thread_wants_to_stop;
4295 if (stop_info_sp->GetOverrideShouldStop())
Jim Ingham4b536182011-08-09 02:12:22 +00004296 {
Jim Ingham0161b492013-02-09 01:29:05 +00004297 this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue();
4298 }
4299 else
4300 {
4301 stop_info_sp->PerformAction(event_ptr);
4302 // The stop action might restart the target. If it does, then we want to mark that in the
4303 // event so that whoever is receiving it will know to wait for the running event and reflect
4304 // that state appropriately.
4305 // We also need to stop processing actions, since they aren't expecting the target to be running.
4306
4307 // FIXME: we might have run.
4308 if (stop_info_sp->HasTargetRunSinceMe())
4309 {
4310 SetRestarted (true);
4311 break;
4312 }
4313
4314 this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr);
Jim Ingham4b536182011-08-09 02:12:22 +00004315 }
Jim Inghamc7078c22012-12-13 22:24:15 +00004316
Jim Inghamc7078c22012-12-13 22:24:15 +00004317 if (still_should_stop == false)
4318 still_should_stop = this_thread_wants_to_stop;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004319 }
4320 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00004321
Ashok Thirumurthicf7c55e2013-04-18 14:38:20 +00004322
Jim Inghama8ca6e22013-05-03 23:04:37 +00004323 if (!GetRestarted())
Jim Ingham9575d842011-03-11 03:53:59 +00004324 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004325 if (!still_should_stop && does_anybody_have_an_opinion)
Jim Ingham4b536182011-08-09 02:12:22 +00004326 {
4327 // We've been asked to continue, so do that here.
Jim Ingham9575d842011-03-11 03:53:59 +00004328 SetRestarted(true);
Jim Ingham3b8285d2012-04-19 01:40:33 +00004329 // Use the public resume method here, since this is just
4330 // extending a public resume.
Jim Ingham0161b492013-02-09 01:29:05 +00004331 m_process_sp->PrivateResume();
Jim Ingham4b536182011-08-09 02:12:22 +00004332 }
4333 else
4334 {
4335 // If we didn't restart, run the Stop Hooks here:
4336 // They might also restart the target, so watch for that.
4337 m_process_sp->GetTarget().RunStopHooks();
4338 if (m_process_sp->GetPrivateState() == eStateRunning)
4339 SetRestarted(true);
4340 }
Jim Ingham9575d842011-03-11 03:53:59 +00004341 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004342 }
4343}
4344
4345void
4346Process::ProcessEventData::Dump (Stream *s) const
4347{
4348 if (m_process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004349 s->Printf(" process = %p (pid = %" PRIu64 "), ",
4350 static_cast<void*>(m_process_sp.get()), m_process_sp->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004351
Greg Clayton8b82f082011-04-12 05:54:46 +00004352 s->Printf("state = %s", StateAsCString(GetState()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004353}
4354
4355const Process::ProcessEventData *
4356Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
4357{
4358 if (event_ptr)
4359 {
4360 const EventData *event_data = event_ptr->GetData();
4361 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
4362 return static_cast <const ProcessEventData *> (event_ptr->GetData());
4363 }
4364 return NULL;
4365}
4366
4367ProcessSP
4368Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
4369{
4370 ProcessSP process_sp;
4371 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4372 if (data)
4373 process_sp = data->GetProcessSP();
4374 return process_sp;
4375}
4376
4377StateType
4378Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
4379{
4380 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4381 if (data == NULL)
4382 return eStateInvalid;
4383 else
4384 return data->GetState();
4385}
4386
4387bool
4388Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
4389{
4390 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4391 if (data == NULL)
4392 return false;
4393 else
4394 return data->GetRestarted();
4395}
4396
4397void
4398Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
4399{
4400 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4401 if (data != NULL)
4402 data->SetRestarted(new_value);
4403}
4404
Jim Ingham0161b492013-02-09 01:29:05 +00004405size_t
4406Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr)
4407{
4408 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4409 if (data != NULL)
4410 return data->GetNumRestartedReasons();
4411 else
4412 return 0;
4413}
4414
4415const char *
4416Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx)
4417{
4418 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4419 if (data != NULL)
4420 return data->GetRestartedReasonAtIndex(idx);
4421 else
4422 return NULL;
4423}
4424
4425void
4426Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason)
4427{
4428 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4429 if (data != NULL)
4430 data->AddRestartedReason(reason);
4431}
4432
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004433bool
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004434Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
4435{
4436 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4437 if (data == NULL)
4438 return false;
4439 else
4440 return data->GetInterrupted ();
4441}
4442
4443void
4444Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
4445{
4446 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4447 if (data != NULL)
4448 data->SetInterrupted(new_value);
4449}
4450
4451bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004452Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
4453{
4454 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4455 if (data)
4456 {
4457 data->SetUpdateStateOnRemoval();
4458 return true;
4459 }
4460 return false;
4461}
4462
Greg Claytond9e416c2012-02-18 05:35:26 +00004463lldb::TargetSP
4464Process::CalculateTarget ()
4465{
4466 return m_target.shared_from_this();
4467}
4468
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004469void
Greg Clayton0603aa92010-10-04 01:05:56 +00004470Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004471{
Greg Claytonc14ee322011-09-22 04:58:26 +00004472 exe_ctx.SetTargetPtr (&m_target);
4473 exe_ctx.SetProcessPtr (this);
4474 exe_ctx.SetThreadPtr(NULL);
4475 exe_ctx.SetFramePtr (NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004476}
4477
Greg Claytone996fd32011-03-08 22:40:15 +00004478//uint32_t
4479//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
4480//{
4481// return 0;
4482//}
4483//
4484//ArchSpec
4485//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
4486//{
4487// return Host::GetArchSpecForExistingProcess (pid);
4488//}
4489//
4490//ArchSpec
4491//Process::GetArchSpecForExistingProcess (const char *process_name)
4492//{
4493// return Host::GetArchSpecForExistingProcess (process_name);
4494//}
4495//
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004496void
4497Process::AppendSTDOUT (const char * s, size_t len)
4498{
Greg Clayton3af9ea52010-11-18 05:57:03 +00004499 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004500 m_stdout_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004501 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState()));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004502}
4503
4504void
Greg Clayton93e86192011-11-13 04:45:22 +00004505Process::AppendSTDERR (const char * s, size_t len)
4506{
4507 Mutex::Locker locker (m_stdio_communication_mutex);
4508 m_stderr_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004509 BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState()));
Greg Clayton93e86192011-11-13 04:45:22 +00004510}
4511
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004512void
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004513Process::BroadcastAsyncProfileData(const std::string &one_profile_data)
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004514{
4515 Mutex::Locker locker (m_profile_data_comm_mutex);
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004516 m_profile_data.push_back(one_profile_data);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004517 BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState()));
4518}
4519
4520size_t
4521Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error)
4522{
4523 Mutex::Locker locker(m_profile_data_comm_mutex);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004524 if (m_profile_data.empty())
4525 return 0;
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004526
4527 std::string &one_profile_data = m_profile_data.front();
4528 size_t bytes_available = one_profile_data.size();
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004529 if (bytes_available > 0)
4530 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004531 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004532 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004533 log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")",
4534 static_cast<void*>(buf),
4535 static_cast<uint64_t>(buf_size));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004536 if (bytes_available > buf_size)
4537 {
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004538 memcpy(buf, one_profile_data.c_str(), buf_size);
4539 one_profile_data.erase(0, buf_size);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004540 bytes_available = buf_size;
4541 }
4542 else
4543 {
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004544 memcpy(buf, one_profile_data.c_str(), bytes_available);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004545 m_profile_data.erase(m_profile_data.begin());
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004546 }
4547 }
4548 return bytes_available;
4549}
4550
4551
Greg Clayton93e86192011-11-13 04:45:22 +00004552//------------------------------------------------------------------
4553// Process STDIO
4554//------------------------------------------------------------------
4555
4556size_t
4557Process::GetSTDOUT (char *buf, size_t buf_size, Error &error)
4558{
4559 Mutex::Locker locker(m_stdio_communication_mutex);
4560 size_t bytes_available = m_stdout_data.size();
4561 if (bytes_available > 0)
4562 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004563 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004564 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004565 log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")",
4566 static_cast<void*>(buf),
4567 static_cast<uint64_t>(buf_size));
Greg Clayton93e86192011-11-13 04:45:22 +00004568 if (bytes_available > buf_size)
4569 {
4570 memcpy(buf, m_stdout_data.c_str(), buf_size);
4571 m_stdout_data.erase(0, buf_size);
4572 bytes_available = buf_size;
4573 }
4574 else
4575 {
4576 memcpy(buf, m_stdout_data.c_str(), bytes_available);
4577 m_stdout_data.clear();
4578 }
4579 }
4580 return bytes_available;
4581}
4582
4583
4584size_t
4585Process::GetSTDERR (char *buf, size_t buf_size, Error &error)
4586{
4587 Mutex::Locker locker(m_stdio_communication_mutex);
4588 size_t bytes_available = m_stderr_data.size();
4589 if (bytes_available > 0)
4590 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004591 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004592 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004593 log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")",
4594 static_cast<void*>(buf),
4595 static_cast<uint64_t>(buf_size));
Greg Clayton93e86192011-11-13 04:45:22 +00004596 if (bytes_available > buf_size)
4597 {
4598 memcpy(buf, m_stderr_data.c_str(), buf_size);
4599 m_stderr_data.erase(0, buf_size);
4600 bytes_available = buf_size;
4601 }
4602 else
4603 {
4604 memcpy(buf, m_stderr_data.c_str(), bytes_available);
4605 m_stderr_data.clear();
4606 }
4607 }
4608 return bytes_available;
4609}
4610
4611void
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004612Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
4613{
4614 Process *process = (Process *) baton;
4615 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
4616}
4617
Greg Clayton44d93782014-01-27 23:43:24 +00004618class IOHandlerProcessSTDIO :
4619 public IOHandler
4620{
4621public:
4622 IOHandlerProcessSTDIO (Process *process,
4623 int write_fd) :
4624 IOHandler(process->GetTarget().GetDebugger()),
4625 m_process (process),
4626 m_read_file (),
4627 m_write_file (write_fd, false),
Greg Clayton100eb932014-07-02 21:10:39 +00004628 m_pipe ()
Greg Clayton44d93782014-01-27 23:43:24 +00004629 {
4630 m_read_file.SetDescriptor(GetInputFD(), false);
4631 }
4632
4633 virtual
4634 ~IOHandlerProcessSTDIO ()
4635 {
4636
4637 }
4638
4639 bool
4640 OpenPipes ()
4641 {
Greg Clayton100eb932014-07-02 21:10:39 +00004642 if (m_pipe.IsValid())
Greg Clayton44d93782014-01-27 23:43:24 +00004643 return true;
Greg Clayton100eb932014-07-02 21:10:39 +00004644 return m_pipe.Open();
Greg Clayton44d93782014-01-27 23:43:24 +00004645 }
4646
4647 void
4648 ClosePipes()
4649 {
Greg Clayton100eb932014-07-02 21:10:39 +00004650 m_pipe.Close();
Greg Clayton44d93782014-01-27 23:43:24 +00004651 }
4652
4653 // Each IOHandler gets to run until it is done. It should read data
4654 // from the "in" and place output into "out" and "err and return
4655 // when done.
4656 virtual void
4657 Run ()
4658 {
4659 if (m_read_file.IsValid() && m_write_file.IsValid())
4660 {
4661 SetIsDone(false);
4662 if (OpenPipes())
4663 {
4664 const int read_fd = m_read_file.GetDescriptor();
Greg Clayton100eb932014-07-02 21:10:39 +00004665 const int pipe_read_fd = m_pipe.GetReadFileDescriptor();
Greg Clayton44d93782014-01-27 23:43:24 +00004666 TerminalState terminal_state;
4667 terminal_state.Save (read_fd, false);
4668 Terminal terminal(read_fd);
4669 terminal.SetCanonical(false);
4670 terminal.SetEcho(false);
Deepak Panickal914b8d92014-01-31 18:48:46 +00004671// FD_ZERO, FD_SET are not supported on windows
Hafiz Abid Qadeer6eff1012014-03-12 10:45:23 +00004672#ifndef _WIN32
Greg Clayton44d93782014-01-27 23:43:24 +00004673 while (!GetIsDone())
4674 {
4675 fd_set read_fdset;
4676 FD_ZERO (&read_fdset);
4677 FD_SET (read_fd, &read_fdset);
4678 FD_SET (pipe_read_fd, &read_fdset);
4679 const int nfds = std::max<int>(read_fd, pipe_read_fd) + 1;
4680 int num_set_fds = select (nfds, &read_fdset, NULL, NULL, NULL);
4681 if (num_set_fds < 0)
4682 {
4683 const int select_errno = errno;
4684
4685 if (select_errno != EINTR)
4686 SetIsDone(true);
4687 }
4688 else if (num_set_fds > 0)
4689 {
4690 char ch = 0;
4691 size_t n;
4692 if (FD_ISSET (read_fd, &read_fdset))
4693 {
4694 n = 1;
4695 if (m_read_file.Read(&ch, n).Success() && n == 1)
4696 {
4697 if (m_write_file.Write(&ch, n).Fail() || n != 1)
4698 SetIsDone(true);
4699 }
4700 else
4701 SetIsDone(true);
4702 }
4703 if (FD_ISSET (pipe_read_fd, &read_fdset))
4704 {
4705 // Consume the interrupt byte
Greg Clayton100eb932014-07-02 21:10:39 +00004706 if (m_pipe.Read (&ch, 1) == 1)
Greg Clayton19e11352014-02-26 22:47:33 +00004707 {
Greg Clayton100eb932014-07-02 21:10:39 +00004708 switch (ch)
4709 {
4710 case 'q':
4711 SetIsDone(true);
4712 break;
4713 case 'i':
4714 if (StateIsRunningState(m_process->GetState()))
4715 m_process->Halt();
4716 break;
4717 }
Greg Clayton19e11352014-02-26 22:47:33 +00004718 }
Greg Clayton44d93782014-01-27 23:43:24 +00004719 }
4720 }
4721 }
Deepak Panickal914b8d92014-01-31 18:48:46 +00004722#endif
Greg Clayton44d93782014-01-27 23:43:24 +00004723 terminal_state.Restore();
4724
4725 }
4726 else
4727 SetIsDone(true);
4728 }
4729 else
4730 SetIsDone(true);
4731 }
4732
4733 // Hide any characters that have been displayed so far so async
4734 // output can be displayed. Refresh() will be called after the
4735 // output has been displayed.
4736 virtual void
4737 Hide ()
4738 {
4739
4740 }
4741 // Called when the async output has been received in order to update
4742 // the input reader (refresh the prompt and redisplay any current
4743 // line(s) that are being edited
4744 virtual void
4745 Refresh ()
4746 {
4747
4748 }
Greg Claytone68f5d62014-02-24 22:50:57 +00004749
Greg Clayton44d93782014-01-27 23:43:24 +00004750 virtual void
Greg Claytone68f5d62014-02-24 22:50:57 +00004751 Cancel ()
Greg Clayton44d93782014-01-27 23:43:24 +00004752 {
Greg Clayton19e11352014-02-26 22:47:33 +00004753 char ch = 'q'; // Send 'q' for quit
Greg Clayton100eb932014-07-02 21:10:39 +00004754 m_pipe.Write (&ch, 1);
Greg Clayton44d93782014-01-27 23:43:24 +00004755 }
Greg Claytone68f5d62014-02-24 22:50:57 +00004756
Greg Claytonf0066ad2014-05-02 00:45:31 +00004757 virtual bool
Greg Claytone68f5d62014-02-24 22:50:57 +00004758 Interrupt ()
4759 {
Greg Clayton19e11352014-02-26 22:47:33 +00004760 // Do only things that are safe to do in an interrupt context (like in
4761 // a SIGINT handler), like write 1 byte to a file descriptor. This will
4762 // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte
4763 // that was written to the pipe and then call m_process->Halt() from a
4764 // much safer location in code.
Greg Clayton0fdd3ae2014-07-16 21:05:41 +00004765 if (m_active)
4766 {
4767 char ch = 'i'; // Send 'i' for interrupt
4768 return m_pipe.Write (&ch, 1) == 1;
4769 }
4770 else
4771 {
4772 // This IOHandler might be pushed on the stack, but not being run currently
4773 // so do the right thing if we aren't actively watching for STDIN by sending
4774 // the interrupt to the process. Otherwise the write to the pipe above would
4775 // do nothing. This can happen when the command interpreter is running and
4776 // gets a "expression ...". It will be on the IOHandler thread and sending
4777 // the input is complete to the delegate which will cause the expression to
4778 // run, which will push the process IO handler, but not run it.
4779
4780 if (StateIsRunningState(m_process->GetState()))
4781 {
4782 m_process->SendAsyncInterrupt();
4783 return true;
4784 }
4785 }
4786 return false;
Greg Claytone68f5d62014-02-24 22:50:57 +00004787 }
Greg Clayton44d93782014-01-27 23:43:24 +00004788
4789 virtual void
4790 GotEOF()
4791 {
4792
4793 }
4794
4795protected:
4796 Process *m_process;
4797 File m_read_file; // Read from this file (usually actual STDIN for LLDB
4798 File m_write_file; // Write to this file (usually the master pty for getting io to debuggee)
Greg Clayton100eb932014-07-02 21:10:39 +00004799 Pipe m_pipe;
Greg Clayton44d93782014-01-27 23:43:24 +00004800};
4801
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004802void
Greg Clayton44d93782014-01-27 23:43:24 +00004803Process::SetSTDIOFileDescriptor (int fd)
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004804{
4805 // First set up the Read Thread for reading/handling process I/O
4806
Greg Clayton44d93782014-01-27 23:43:24 +00004807 std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (fd, true));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004808
4809 if (conn_ap.get())
4810 {
4811 m_stdio_communication.SetConnection (conn_ap.release());
4812 if (m_stdio_communication.IsConnected())
4813 {
4814 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
4815 m_stdio_communication.StartReadThread();
4816
4817 // Now read thread is set up, set up input reader.
4818
4819 if (!m_process_input_reader.get())
Greg Clayton44d93782014-01-27 23:43:24 +00004820 m_process_input_reader.reset (new IOHandlerProcessSTDIO (this, fd));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004821 }
4822 }
4823}
4824
Greg Claytonb4874f12014-02-28 18:22:24 +00004825bool
Greg Clayton6fea17e2014-03-03 19:15:20 +00004826Process::ProcessIOHandlerIsActive ()
4827{
4828 IOHandlerSP io_handler_sp (m_process_input_reader);
4829 if (io_handler_sp)
4830 return m_target.GetDebugger().IsTopIOHandler (io_handler_sp);
4831 return false;
4832}
4833bool
Greg Clayton44d93782014-01-27 23:43:24 +00004834Process::PushProcessIOHandler ()
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004835{
Greg Clayton44d93782014-01-27 23:43:24 +00004836 IOHandlerSP io_handler_sp (m_process_input_reader);
4837 if (io_handler_sp)
4838 {
4839 io_handler_sp->SetIsDone(false);
4840 m_target.GetDebugger().PushIOHandler (io_handler_sp);
Greg Claytonb4874f12014-02-28 18:22:24 +00004841 return true;
Greg Clayton44d93782014-01-27 23:43:24 +00004842 }
Greg Claytonb4874f12014-02-28 18:22:24 +00004843 return false;
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004844}
4845
Greg Claytonb4874f12014-02-28 18:22:24 +00004846bool
Greg Clayton44d93782014-01-27 23:43:24 +00004847Process::PopProcessIOHandler ()
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004848{
Greg Clayton44d93782014-01-27 23:43:24 +00004849 IOHandlerSP io_handler_sp (m_process_input_reader);
4850 if (io_handler_sp)
Greg Claytonb4874f12014-02-28 18:22:24 +00004851 return m_target.GetDebugger().PopIOHandler (io_handler_sp);
4852 return false;
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004853}
4854
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004855// The process needs to know about installed plug-ins
Greg Clayton99d0faf2010-11-18 23:32:35 +00004856void
Caroline Tice20bd37f2011-03-10 22:14:10 +00004857Process::SettingsInitialize ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004858{
Greg Clayton6920b522012-08-22 18:39:03 +00004859 Thread::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00004860}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004861
Greg Clayton99d0faf2010-11-18 23:32:35 +00004862void
Caroline Tice20bd37f2011-03-10 22:14:10 +00004863Process::SettingsTerminate ()
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004864{
Greg Clayton6920b522012-08-22 18:39:03 +00004865 Thread::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00004866}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004867
Jim Ingham1624a2d2014-05-05 02:26:40 +00004868ExpressionResults
Jim Inghamf48169b2010-11-30 02:22:11 +00004869Process::RunThreadPlan (ExecutionContext &exe_ctx,
Jim Ingham372787f2012-04-07 00:00:41 +00004870 lldb::ThreadPlanSP &thread_plan_sp,
Jim Ingham6fbc48b2013-11-07 00:11:47 +00004871 const EvaluateExpressionOptions &options,
Jim Inghamf48169b2010-11-30 02:22:11 +00004872 Stream &errors)
4873{
Jim Ingham8646d3c2014-05-05 02:47:44 +00004874 ExpressionResults return_value = eExpressionSetupError;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004875
Jim Ingham77787032011-01-20 02:03:18 +00004876 if (thread_plan_sp.get() == NULL)
4877 {
4878 errors.Printf("RunThreadPlan called with empty thread plan.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004879 return eExpressionSetupError;
Jim Ingham77787032011-01-20 02:03:18 +00004880 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004881
Jim Ingham7d7931d2013-03-28 00:05:34 +00004882 if (!thread_plan_sp->ValidatePlan(NULL))
4883 {
4884 errors.Printf ("RunThreadPlan called with an invalid thread plan.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004885 return eExpressionSetupError;
Jim Ingham7d7931d2013-03-28 00:05:34 +00004886 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004887
Greg Claytonc14ee322011-09-22 04:58:26 +00004888 if (exe_ctx.GetProcessPtr() != this)
4889 {
4890 errors.Printf("RunThreadPlan called on wrong process.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004891 return eExpressionSetupError;
Greg Claytonc14ee322011-09-22 04:58:26 +00004892 }
4893
4894 Thread *thread = exe_ctx.GetThreadPtr();
4895 if (thread == NULL)
4896 {
4897 errors.Printf("RunThreadPlan called with invalid thread.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004898 return eExpressionSetupError;
Greg Claytonc14ee322011-09-22 04:58:26 +00004899 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004900
Jim Ingham17e5c4e2011-05-17 22:24:54 +00004901 // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes.
4902 // For that to be true the plan can't be private - since private plans suppress themselves in the
4903 // GetCompletedPlan call.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004904
Jim Ingham17e5c4e2011-05-17 22:24:54 +00004905 bool orig_plan_private = thread_plan_sp->GetPrivate();
4906 thread_plan_sp->SetPrivate(false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004907
Jim Ingham444586b2011-01-24 06:34:17 +00004908 if (m_private_state.GetValue() != eStateStopped)
4909 {
4910 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004911 return eExpressionSetupError;
Jim Ingham444586b2011-01-24 06:34:17 +00004912 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004913
Jim Ingham66243842011-08-13 00:56:10 +00004914 // Save the thread & frame from the exe_ctx for restoration after we run
Greg Claytonc14ee322011-09-22 04:58:26 +00004915 const uint32_t thread_idx_id = thread->GetIndexID();
Jason Molendab57e4a12013-11-04 09:33:30 +00004916 StackFrameSP selected_frame_sp = thread->GetSelectedFrame();
Jim Ingham11b0e052013-02-19 23:22:45 +00004917 if (!selected_frame_sp)
4918 {
4919 thread->SetSelectedFrame(0);
4920 selected_frame_sp = thread->GetSelectedFrame();
4921 if (!selected_frame_sp)
4922 {
4923 errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id);
Jim Ingham8646d3c2014-05-05 02:47:44 +00004924 return eExpressionSetupError;
Jim Ingham11b0e052013-02-19 23:22:45 +00004925 }
4926 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004927
Jim Ingham11b0e052013-02-19 23:22:45 +00004928 StackID ctx_frame_id = selected_frame_sp->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00004929
4930 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
4931 // so we should arrange to reset them as well.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004932
Greg Claytonc14ee322011-09-22 04:58:26 +00004933 lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004934
Jim Ingham66243842011-08-13 00:56:10 +00004935 uint32_t selected_tid;
4936 StackID selected_stack_id;
Greg Clayton762f7132011-09-18 18:59:15 +00004937 if (selected_thread_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00004938 {
4939 selected_tid = selected_thread_sp->GetIndexID();
Jim Ingham66243842011-08-13 00:56:10 +00004940 selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00004941 }
4942 else
4943 {
4944 selected_tid = LLDB_INVALID_THREAD_ID;
4945 }
4946
Jim Ingham372787f2012-04-07 00:00:41 +00004947 lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD;
Jim Ingham076b3042012-04-10 01:21:57 +00004948 lldb::StateType old_state;
4949 lldb::ThreadPlanSP stopper_base_plan_sp;
Jim Ingham372787f2012-04-07 00:00:41 +00004950
Greg Clayton5160ce52013-03-27 23:08:40 +00004951 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham372787f2012-04-07 00:00:41 +00004952 if (Host::GetCurrentThread() == m_private_state_thread)
4953 {
Jim Ingham076b3042012-04-10 01:21:57 +00004954 // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since
4955 // we are the thread that is generating public events.
Jim Ingham372787f2012-04-07 00:00:41 +00004956 // 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 +00004957 // we are fielding public events here.
4958 if (log)
Jason Molendad251c9d2012-11-17 01:41:04 +00004959 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 +00004960
Jim Ingham372787f2012-04-07 00:00:41 +00004961 backup_private_state_thread = m_private_state_thread;
Jim Ingham076b3042012-04-10 01:21:57 +00004962
4963 // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop,
4964 // returning control here.
4965 // But in the normal course of things, the plan above us on the stack would be given a shot at the stop
4966 // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack
4967 // before the plan we want to run. Since base plans always stop and return control to the user, that will
4968 // do just what we want.
4969 stopper_base_plan_sp.reset(new ThreadPlanBase (*thread));
4970 thread->QueueThreadPlan (stopper_base_plan_sp, false);
4971 // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly.
4972 old_state = m_public_state.GetValue();
4973 m_public_state.SetValueNoLock(eStateStopped);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004974
Jim Ingham076b3042012-04-10 01:21:57 +00004975 // Now spin up the private state thread:
Jim Ingham372787f2012-04-07 00:00:41 +00004976 StartPrivateStateThread(true);
4977 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004978
Jim Ingham372787f2012-04-07 00:00:41 +00004979 thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense?
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004980
Jim Ingham6fbc48b2013-11-07 00:11:47 +00004981 if (options.GetDebug())
4982 {
4983 // In this case, we aren't actually going to run, we just want to stop right away.
4984 // Flush this thread so we will refetch the stacks and show the correct backtrace.
4985 // FIXME: To make this prettier we should invent some stop reason for this, but that
4986 // is only cosmetic, and this functionality is only of use to lldb developers who can
4987 // live with not pretty...
4988 thread->Flush();
Jim Ingham8646d3c2014-05-05 02:47:44 +00004989 return eExpressionStoppedForDebug;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00004990 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004991
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00004992 Listener listener("lldb.process.listener.run-thread-plan");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004993
Sean Callanana46ec452012-07-11 21:31:24 +00004994 lldb::EventSP event_to_broadcast_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004995
Jim Ingham77787032011-01-20 02:03:18 +00004996 {
Sean Callanana46ec452012-07-11 21:31:24 +00004997 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
4998 // restored on exit to the function.
4999 //
5000 // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event
5001 // is put into event_to_broadcast_sp for rebroadcasting.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005002
Sean Callanana46ec452012-07-11 21:31:24 +00005003 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005004
Jim Inghamf48169b2010-11-30 02:22:11 +00005005 if (log)
Jim Ingham0f16e732011-02-08 05:20:59 +00005006 {
5007 StreamString s;
Sean Callanana46ec452012-07-11 21:31:24 +00005008 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Daniel Malead01b2952012-11-29 21:49:15 +00005009 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 " to run thread plan \"%s\".",
Sean Callanana46ec452012-07-11 21:31:24 +00005010 thread->GetIndexID(),
5011 thread->GetID(),
5012 s.GetData());
5013 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005014
Sean Callanana46ec452012-07-11 21:31:24 +00005015 bool got_event;
5016 lldb::EventSP event_sp;
5017 lldb::StateType stop_state = lldb::eStateInvalid;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005018
Sean Callanana46ec452012-07-11 21:31:24 +00005019 TimeValue* timeout_ptr = NULL;
5020 TimeValue real_timeout;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005021
Jim Ingham0161b492013-02-09 01:29:05 +00005022 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 +00005023 bool do_resume = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005024 bool handle_running_event = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +00005025 const uint64_t default_one_thread_timeout_usec = 250000;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005026
Jim Ingham0161b492013-02-09 01:29:05 +00005027 // This is just for accounting:
5028 uint32_t num_resumes = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005029
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005030 uint32_t timeout_usec = options.GetTimeoutUsec();
Jim Inghamfd95f892014-04-22 01:41:52 +00005031 uint32_t one_thread_timeout_usec;
5032 uint32_t all_threads_timeout_usec = 0;
Jim Inghamfe1c3422014-04-16 02:24:48 +00005033
5034 // If we are going to run all threads the whole time, or if we are only going to run one thread,
5035 // then we don't need the first timeout. So we set the final timeout, and pretend we are after the
5036 // first timeout already.
5037
5038 if (!options.GetStopOthers() || !options.GetTryAllThreads())
Jim Ingham286fb1e2014-02-28 02:52:06 +00005039 {
5040 before_first_timeout = false;
Jim Inghamfd95f892014-04-22 01:41:52 +00005041 one_thread_timeout_usec = 0;
5042 all_threads_timeout_usec = timeout_usec;
Jim Ingham286fb1e2014-02-28 02:52:06 +00005043 }
Jim Inghamfe1c3422014-04-16 02:24:48 +00005044 else
Jim Ingham0161b492013-02-09 01:29:05 +00005045 {
Jim Inghamfd95f892014-04-22 01:41:52 +00005046 uint32_t option_one_thread_timeout = options.GetOneThreadTimeoutUsec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005047
Jim Ingham914f4e72014-03-28 21:58:28 +00005048 // If the overall wait is forever, then we only need to set the one thread timeout:
5049 if (timeout_usec == 0)
5050 {
Ed Maste801335c2014-03-31 19:28:14 +00005051 if (option_one_thread_timeout != 0)
Jim Inghamfd95f892014-04-22 01:41:52 +00005052 one_thread_timeout_usec = option_one_thread_timeout;
Jim Ingham914f4e72014-03-28 21:58:28 +00005053 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005054 one_thread_timeout_usec = default_one_thread_timeout_usec;
Jim Ingham914f4e72014-03-28 21:58:28 +00005055 }
Jim Ingham0161b492013-02-09 01:29:05 +00005056 else
5057 {
Jim Ingham914f4e72014-03-28 21:58:28 +00005058 // Otherwise, if the one thread timeout is set, make sure it isn't longer than the overall timeout,
5059 // and use it, otherwise use half the total timeout, bounded by the default_one_thread_timeout_usec.
5060 uint64_t computed_one_thread_timeout;
5061 if (option_one_thread_timeout != 0)
5062 {
5063 if (timeout_usec < option_one_thread_timeout)
5064 {
5065 errors.Printf("RunThreadPlan called without one thread timeout greater than total timeout");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005066 return eExpressionSetupError;
Jim Ingham914f4e72014-03-28 21:58:28 +00005067 }
5068 computed_one_thread_timeout = option_one_thread_timeout;
5069 }
5070 else
5071 {
5072 computed_one_thread_timeout = timeout_usec / 2;
5073 if (computed_one_thread_timeout > default_one_thread_timeout_usec)
5074 computed_one_thread_timeout = default_one_thread_timeout_usec;
5075 }
Jim Inghamfd95f892014-04-22 01:41:52 +00005076 one_thread_timeout_usec = computed_one_thread_timeout;
5077 all_threads_timeout_usec = timeout_usec - one_thread_timeout_usec;
5078
Jim Ingham0161b492013-02-09 01:29:05 +00005079 }
Jim Ingham0161b492013-02-09 01:29:05 +00005080 }
Jim Inghamfe1c3422014-04-16 02:24:48 +00005081
5082 if (log)
Jim Inghamfd95f892014-04-22 01:41:52 +00005083 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 +00005084 options.GetStopOthers(),
5085 options.GetTryAllThreads(),
Jim Inghamfd95f892014-04-22 01:41:52 +00005086 before_first_timeout,
5087 one_thread_timeout_usec,
5088 all_threads_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005089
Jim Ingham1460e4b2014-01-10 23:46:59 +00005090 // This isn't going to work if there are unfetched events on the queue.
5091 // Are there cases where we might want to run the remaining events here, and then try to
5092 // call the function? That's probably being too tricky for our own good.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005093
Jim Ingham1460e4b2014-01-10 23:46:59 +00005094 Event *other_events = listener.PeekAtNextEvent();
5095 if (other_events != NULL)
5096 {
5097 errors.Printf("Calling RunThreadPlan with pending events on the queue.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005098 return eExpressionSetupError;
Jim Ingham1460e4b2014-01-10 23:46:59 +00005099 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005100
Jim Ingham1460e4b2014-01-10 23:46:59 +00005101 // We also need to make sure that the next event is delivered. We might be calling a function as part of
5102 // a thread plan, in which case the last delivered event could be the running event, and we don't want
5103 // event coalescing to cause us to lose OUR running event...
5104 ForceNextEventDelivery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005105
Jim Ingham8559a352012-11-26 23:52:18 +00005106 // This while loop must exit out the bottom, there's cleanup that we need to do when we are done.
5107 // So don't call return anywhere within it.
Jim Ingham35878c42014-04-08 21:33:21 +00005108
5109#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
5110 // It's pretty much impossible to write test cases for things like:
5111 // One thread timeout expires, I go to halt, but the process already stopped
5112 // on the function call stop breakpoint. Turning on this define will make us not
5113 // fetch the first event till after the halt. So if you run a quick function, it will have
5114 // completed, and the completion event will be waiting, when you interrupt for halt.
5115 // The expression evaluation should still succeed.
5116 bool miss_first_event = true;
5117#endif
Jim Inghamfd95f892014-04-22 01:41:52 +00005118 TimeValue one_thread_timeout;
5119 TimeValue final_timeout;
5120
Jim Ingham35878c42014-04-08 21:33:21 +00005121
Sean Callanana46ec452012-07-11 21:31:24 +00005122 while (1)
5123 {
5124 // We usually want to resume the process if we get to the top of the loop.
5125 // The only exception is if we get two running events with no intervening
5126 // stop, which can happen, we will just wait for then next stop event.
Jim Ingham0161b492013-02-09 01:29:05 +00005127 if (log)
5128 log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.",
5129 do_resume,
5130 handle_running_event,
5131 before_first_timeout);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005132
Jim Ingham184e9812013-01-15 02:47:48 +00005133 if (do_resume || handle_running_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005134 {
5135 // Do the initial resume and wait for the running event before going further.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005136
Jim Ingham184e9812013-01-15 02:47:48 +00005137 if (do_resume)
Sean Callanana46ec452012-07-11 21:31:24 +00005138 {
Jim Ingham0161b492013-02-09 01:29:05 +00005139 num_resumes++;
Jim Ingham184e9812013-01-15 02:47:48 +00005140 Error resume_error = PrivateResume ();
5141 if (!resume_error.Success())
5142 {
Jim Ingham0161b492013-02-09 01:29:05 +00005143 errors.Printf("Error resuming inferior the %d time: \"%s\".\n",
5144 num_resumes,
5145 resume_error.AsCString());
Jim Ingham8646d3c2014-05-05 02:47:44 +00005146 return_value = eExpressionSetupError;
Jim Ingham184e9812013-01-15 02:47:48 +00005147 break;
5148 }
Sean Callanana46ec452012-07-11 21:31:24 +00005149 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005150
Jim Ingham0161b492013-02-09 01:29:05 +00005151 TimeValue resume_timeout = TimeValue::Now();
5152 resume_timeout.OffsetWithMicroSeconds(500000);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005153
Jim Ingham0161b492013-02-09 01:29:05 +00005154 got_event = listener.WaitForEvent(&resume_timeout, event_sp);
Sean Callanana46ec452012-07-11 21:31:24 +00005155 if (!got_event)
5156 {
5157 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005158 log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.",
5159 num_resumes);
Sean Callanana46ec452012-07-11 21:31:24 +00005160
Jim Ingham0161b492013-02-09 01:29:05 +00005161 errors.Printf("Didn't get any event after resume %d, exiting.", num_resumes);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005162 return_value = eExpressionSetupError;
Sean Callanana46ec452012-07-11 21:31:24 +00005163 break;
5164 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005165
Sean Callanana46ec452012-07-11 21:31:24 +00005166 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Ingham0161b492013-02-09 01:29:05 +00005167
Sean Callanana46ec452012-07-11 21:31:24 +00005168 if (stop_state != eStateRunning)
5169 {
Jim Ingham0161b492013-02-09 01:29:05 +00005170 bool restarted = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005171
Jim Ingham0161b492013-02-09 01:29:05 +00005172 if (stop_state == eStateStopped)
5173 {
5174 restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get());
5175 if (log)
5176 log->Printf("Process::RunThreadPlan(): didn't get running event after "
5177 "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).",
5178 num_resumes,
5179 StateAsCString(stop_state),
5180 restarted,
5181 do_resume,
5182 handle_running_event);
5183 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005184
Jim Ingham0161b492013-02-09 01:29:05 +00005185 if (restarted)
5186 {
5187 // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted
5188 // event here. But if I do, the best thing is to Halt and then get out of here.
5189 Halt();
5190 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005191
Jim Ingham35e1bda2012-10-16 21:41:58 +00005192 errors.Printf("Didn't get running event after initial resume, got %s instead.",
5193 StateAsCString(stop_state));
Jim Ingham8646d3c2014-05-05 02:47:44 +00005194 return_value = eExpressionSetupError;
Sean Callanana46ec452012-07-11 21:31:24 +00005195 break;
5196 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005197
Sean Callanana46ec452012-07-11 21:31:24 +00005198 if (log)
5199 log->PutCString ("Process::RunThreadPlan(): resuming succeeded.");
5200 // We need to call the function synchronously, so spin waiting for it to return.
5201 // If we get interrupted while executing, we're going to lose our context, and
5202 // won't be able to gather the result at this point.
5203 // We set the timeout AFTER the resume, since the resume takes some time and we
5204 // don't want to charge that to the timeout.
Sean Callanana46ec452012-07-11 21:31:24 +00005205 }
Jim Ingham0f16e732011-02-08 05:20:59 +00005206 else
5207 {
Sean Callanana46ec452012-07-11 21:31:24 +00005208 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005209 log->PutCString ("Process::RunThreadPlan(): waiting for next event.");
Jim Ingham0f16e732011-02-08 05:20:59 +00005210 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005211
Jim Ingham0161b492013-02-09 01:29:05 +00005212 if (before_first_timeout)
5213 {
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005214 if (options.GetTryAllThreads())
Jim Inghamfd95f892014-04-22 01:41:52 +00005215 {
5216 one_thread_timeout = TimeValue::Now();
5217 one_thread_timeout.OffsetWithMicroSeconds(one_thread_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005218 timeout_ptr = &one_thread_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005219 }
Jim Ingham0161b492013-02-09 01:29:05 +00005220 else
5221 {
5222 if (timeout_usec == 0)
5223 timeout_ptr = NULL;
5224 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005225 {
5226 final_timeout = TimeValue::Now();
5227 final_timeout.OffsetWithMicroSeconds (timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005228 timeout_ptr = &final_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005229 }
Jim Ingham0161b492013-02-09 01:29:05 +00005230 }
5231 }
5232 else
5233 {
5234 if (timeout_usec == 0)
5235 timeout_ptr = NULL;
5236 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005237 {
5238 final_timeout = TimeValue::Now();
5239 final_timeout.OffsetWithMicroSeconds (all_threads_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005240 timeout_ptr = &final_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005241 }
Jim Ingham0161b492013-02-09 01:29:05 +00005242 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005243
Jim Ingham0161b492013-02-09 01:29:05 +00005244 do_resume = true;
5245 handle_running_event = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005246
Sean Callanana46ec452012-07-11 21:31:24 +00005247 // Now wait for the process to stop again:
Sean Callanana46ec452012-07-11 21:31:24 +00005248 event_sp.reset();
Jim Ingham0f16e732011-02-08 05:20:59 +00005249
Jim Ingham0f16e732011-02-08 05:20:59 +00005250 if (log)
Jim Ingham20829ac2011-08-09 22:24:33 +00005251 {
Sean Callanana46ec452012-07-11 21:31:24 +00005252 if (timeout_ptr)
5253 {
Matt Kopec676a4872013-02-21 23:55:31 +00005254 log->Printf ("Process::RunThreadPlan(): about to wait - now is %" PRIu64 " - endpoint is %" PRIu64,
Jim Ingham0161b492013-02-09 01:29:05 +00005255 TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(),
5256 timeout_ptr->GetAsMicroSecondsSinceJan1_1970());
Sean Callanana46ec452012-07-11 21:31:24 +00005257 }
Jim Ingham20829ac2011-08-09 22:24:33 +00005258 else
Sean Callanana46ec452012-07-11 21:31:24 +00005259 {
5260 log->Printf ("Process::RunThreadPlan(): about to wait forever.");
5261 }
5262 }
Jim Ingham35878c42014-04-08 21:33:21 +00005263
5264#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
5265 // See comment above...
5266 if (miss_first_event)
5267 {
5268 usleep(1000);
5269 miss_first_event = false;
5270 got_event = false;
5271 }
5272 else
5273#endif
Sean Callanana46ec452012-07-11 21:31:24 +00005274 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005275
Sean Callanana46ec452012-07-11 21:31:24 +00005276 if (got_event)
5277 {
5278 if (event_sp.get())
5279 {
5280 bool keep_going = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005281 if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005282 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005283 Halt();
Jim Ingham8646d3c2014-05-05 02:47:44 +00005284 return_value = eExpressionInterrupted;
Jim Inghamcfc09352012-07-27 23:57:19 +00005285 errors.Printf ("Execution halted by user interrupt.");
5286 if (log)
5287 log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting.");
Jim Ingham0161b492013-02-09 01:29:05 +00005288 break;
Jim Inghamcfc09352012-07-27 23:57:19 +00005289 }
5290 else
5291 {
5292 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5293 if (log)
5294 log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005295
Jim Inghamcfc09352012-07-27 23:57:19 +00005296 switch (stop_state)
Sean Callanana46ec452012-07-11 21:31:24 +00005297 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005298 case lldb::eStateStopped:
Sean Callanana46ec452012-07-11 21:31:24 +00005299 {
Jim Ingham0161b492013-02-09 01:29:05 +00005300 // We stopped, figure out what we are going to do now.
Jim Inghamcfc09352012-07-27 23:57:19 +00005301 ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id);
5302 if (!thread_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005303 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005304 // Ooh, our thread has vanished. Unlikely that this was successful execution...
Sean Callanana46ec452012-07-11 21:31:24 +00005305 if (log)
Jim Inghamcfc09352012-07-27 23:57:19 +00005306 log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005307 return_value = eExpressionInterrupted;
Sean Callanana46ec452012-07-11 21:31:24 +00005308 }
5309 else
5310 {
Jim Ingham0161b492013-02-09 01:29:05 +00005311 // If we were restarted, we just need to go back up to fetch another event.
5312 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
Jim Inghamcfc09352012-07-27 23:57:19 +00005313 {
5314 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005315 {
5316 log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting.");
5317 }
5318 keep_going = true;
5319 do_resume = false;
5320 handle_running_event = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005321
Jim Inghamcfc09352012-07-27 23:57:19 +00005322 }
5323 else
5324 {
Jim Ingham0161b492013-02-09 01:29:05 +00005325 StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
5326 StopReason stop_reason = eStopReasonInvalid;
5327 if (stop_info_sp)
5328 stop_reason = stop_info_sp->GetStopReason();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005329
Jim Ingham0161b492013-02-09 01:29:05 +00005330 // FIXME: We only check if the stop reason is plan complete, should we make sure that
5331 // it is OUR plan that is complete?
5332 if (stop_reason == eStopReasonPlanComplete)
Jim Ingham184e9812013-01-15 02:47:48 +00005333 {
5334 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005335 log->PutCString ("Process::RunThreadPlan(): execution completed successfully.");
5336 // Now mark this plan as private so it doesn't get reported as the stop reason
5337 // after this point.
5338 if (thread_plan_sp)
5339 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005340 return_value = eExpressionCompleted;
Jim Ingham184e9812013-01-15 02:47:48 +00005341 }
5342 else
5343 {
Jim Ingham0161b492013-02-09 01:29:05 +00005344 // Something restarted the target, so just wait for it to stop for real.
Jim Ingham184e9812013-01-15 02:47:48 +00005345 if (stop_reason == eStopReasonBreakpoint)
Jim Ingham0161b492013-02-09 01:29:05 +00005346 {
5347 if (log)
5348 log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription());
Jim Ingham8646d3c2014-05-05 02:47:44 +00005349 return_value = eExpressionHitBreakpoint;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005350 if (!options.DoesIgnoreBreakpoints())
Sean Callanan4b388c92013-07-30 19:54:09 +00005351 {
5352 event_to_broadcast_sp = event_sp;
5353 }
Jim Ingham0161b492013-02-09 01:29:05 +00005354 }
Jim Ingham184e9812013-01-15 02:47:48 +00005355 else
Jim Ingham0161b492013-02-09 01:29:05 +00005356 {
5357 if (log)
5358 log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete.");
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005359 if (!options.DoesUnwindOnError())
Sean Callanan4b388c92013-07-30 19:54:09 +00005360 event_to_broadcast_sp = event_sp;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005361 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005362 }
Jim Ingham184e9812013-01-15 02:47:48 +00005363 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005364 }
Sean Callanana46ec452012-07-11 21:31:24 +00005365 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005366 }
5367 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005368
Jim Inghamcfc09352012-07-27 23:57:19 +00005369 case lldb::eStateRunning:
Jim Ingham0161b492013-02-09 01:29:05 +00005370 // This shouldn't really happen, but sometimes we do get two running events without an
5371 // intervening stop, and in that case we should just go back to waiting for the stop.
Jim Inghamcfc09352012-07-27 23:57:19 +00005372 do_resume = false;
5373 keep_going = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005374 handle_running_event = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005375 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005376
Jim Inghamcfc09352012-07-27 23:57:19 +00005377 default:
5378 if (log)
5379 log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005380
Jim Inghamcfc09352012-07-27 23:57:19 +00005381 if (stop_state == eStateExited)
5382 event_to_broadcast_sp = event_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005383
Sean Callananbf154da2012-08-08 17:35:10 +00005384 errors.Printf ("Execution stopped with unexpected state.\n");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005385 return_value = eExpressionInterrupted;
Jim Inghamcfc09352012-07-27 23:57:19 +00005386 break;
5387 }
Sean Callanana46ec452012-07-11 21:31:24 +00005388 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005389
Sean Callanana46ec452012-07-11 21:31:24 +00005390 if (keep_going)
5391 continue;
5392 else
5393 break;
5394 }
5395 else
5396 {
5397 if (log)
5398 log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd...");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005399 return_value = eExpressionInterrupted;
Sean Callanana46ec452012-07-11 21:31:24 +00005400 break;
5401 }
5402 }
5403 else
5404 {
5405 // If we didn't get an event that means we've timed out...
5406 // We will interrupt the process here. Depending on what we were asked to do we will
5407 // either exit, or try with all threads running for the same timeout.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005408
Sean Callanana46ec452012-07-11 21:31:24 +00005409 if (log) {
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005410 if (options.GetTryAllThreads())
Sean Callanana46ec452012-07-11 21:31:24 +00005411 {
Jim Ingham0161b492013-02-09 01:29:05 +00005412 if (before_first_timeout)
Jim Inghamfe1c3422014-04-16 02:24:48 +00005413 {
5414 if (timeout_usec != 0)
5415 {
Jim Inghamfe1c3422014-04-16 02:24:48 +00005416 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
Jim Inghamfd95f892014-04-22 01:41:52 +00005417 "running for %" PRIu32 " usec with all threads enabled.",
5418 all_threads_timeout_usec);
Jim Inghamfe1c3422014-04-16 02:24:48 +00005419 }
5420 else
5421 {
5422 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
Ed Mastee61c7b02014-04-29 17:48:06 +00005423 "running forever with all threads enabled.");
Jim Inghamfe1c3422014-04-16 02:24:48 +00005424 }
5425 }
Sean Callanana46ec452012-07-11 21:31:24 +00005426 else
5427 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
Jason Molenda6a8658a2013-10-27 02:32:23 +00005428 "and timeout: %u timed out, abandoning execution.",
Jim Ingham35e1bda2012-10-16 21:41:58 +00005429 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005430 }
5431 else
Jason Molenda6a8658a2013-10-27 02:32:23 +00005432 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, "
Jim Ingham35e1bda2012-10-16 21:41:58 +00005433 "abandoning execution.",
5434 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005435 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005436
Jim Ingham0161b492013-02-09 01:29:05 +00005437 // It is possible that between the time we issued the Halt, and we get around to calling Halt the target
5438 // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event.
5439 // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In
5440 // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's
5441 // stopped event. That's what this while loop does.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005442
Jim Ingham0161b492013-02-09 01:29:05 +00005443 bool back_to_top = true;
5444 uint32_t try_halt_again = 0;
5445 bool do_halt = true;
5446 const uint32_t num_retries = 5;
5447 while (try_halt_again < num_retries)
Sean Callanana46ec452012-07-11 21:31:24 +00005448 {
Jim Ingham0161b492013-02-09 01:29:05 +00005449 Error halt_error;
5450 if (do_halt)
5451 {
5452 if (log)
5453 log->Printf ("Process::RunThreadPlan(): Running Halt.");
5454 halt_error = Halt();
5455 }
5456 if (halt_error.Success())
5457 {
5458 if (log)
5459 log->PutCString ("Process::RunThreadPlan(): Halt succeeded.");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005460
Jim Ingham0161b492013-02-09 01:29:05 +00005461 real_timeout = TimeValue::Now();
5462 real_timeout.OffsetWithMicroSeconds(500000);
5463
5464 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005465
Jim Ingham0161b492013-02-09 01:29:05 +00005466 if (got_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005467 {
Jim Ingham0161b492013-02-09 01:29:05 +00005468 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5469 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005470 {
Jim Ingham0161b492013-02-09 01:29:05 +00005471 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
5472 if (stop_state == lldb::eStateStopped
5473 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
5474 log->PutCString (" Event was the Halt interruption event.");
Sean Callanana46ec452012-07-11 21:31:24 +00005475 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005476
Jim Ingham0161b492013-02-09 01:29:05 +00005477 if (stop_state == lldb::eStateStopped)
Sean Callanana46ec452012-07-11 21:31:24 +00005478 {
Jim Ingham0161b492013-02-09 01:29:05 +00005479 // Between the time we initiated the Halt and the time we delivered it, the process could have
5480 // already finished its job. Check that here:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005481
Jim Ingham0161b492013-02-09 01:29:05 +00005482 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
5483 {
5484 if (log)
5485 log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
5486 "Exiting wait loop.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005487 return_value = eExpressionCompleted;
Jim Ingham0161b492013-02-09 01:29:05 +00005488 back_to_top = false;
5489 break;
5490 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005491
Jim Ingham0161b492013-02-09 01:29:05 +00005492 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
5493 {
5494 if (log)
5495 log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... "
5496 "Exiting wait loop.");
5497 try_halt_again++;
5498 do_halt = false;
5499 continue;
5500 }
Sean Callanana46ec452012-07-11 21:31:24 +00005501
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005502 if (!options.GetTryAllThreads())
Jim Ingham0161b492013-02-09 01:29:05 +00005503 {
5504 if (log)
5505 log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005506 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005507 back_to_top = false;
5508 break;
5509 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005510
Jim Ingham0161b492013-02-09 01:29:05 +00005511 if (before_first_timeout)
5512 {
5513 // Set all the other threads to run, and return to the top of the loop, which will continue;
5514 before_first_timeout = false;
5515 thread_plan_sp->SetStopOthers (false);
5516 if (log)
5517 log->PutCString ("Process::RunThreadPlan(): about to resume.");
Sean Callanana46ec452012-07-11 21:31:24 +00005518
Jim Ingham0161b492013-02-09 01:29:05 +00005519 back_to_top = true;
5520 break;
5521 }
5522 else
5523 {
5524 // Running all threads failed, so return Interrupted.
5525 if (log)
5526 log->PutCString("Process::RunThreadPlan(): running all threads timed out.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005527 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005528 back_to_top = false;
5529 break;
5530 }
Sean Callanana46ec452012-07-11 21:31:24 +00005531 }
5532 }
5533 else
Jim Ingham0161b492013-02-09 01:29:05 +00005534 { if (log)
5535 log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
5536 "I'm getting out of here passing Interrupted.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005537 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005538 back_to_top = false;
5539 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005540 }
5541 }
Jim Ingham0161b492013-02-09 01:29:05 +00005542 else
5543 {
5544 try_halt_again++;
5545 continue;
5546 }
Sean Callanana46ec452012-07-11 21:31:24 +00005547 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005548
Jim Ingham0161b492013-02-09 01:29:05 +00005549 if (!back_to_top || try_halt_again > num_retries)
5550 break;
5551 else
5552 continue;
Sean Callanana46ec452012-07-11 21:31:24 +00005553 }
Sean Callanana46ec452012-07-11 21:31:24 +00005554 } // END WAIT LOOP
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005555
Sean Callanana46ec452012-07-11 21:31:24 +00005556 // If we had to start up a temporary private state thread to run this thread plan, shut it down now.
5557 if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread))
5558 {
5559 StopPrivateStateThread();
5560 Error error;
5561 m_private_state_thread = backup_private_state_thread;
Sean Callanan9a028512012-08-09 00:50:26 +00005562 if (stopper_base_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005563 {
5564 thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp);
5565 }
5566 m_public_state.SetValueNoLock(old_state);
5567
5568 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005569
Jim Ingham184e9812013-01-15 02:47:48 +00005570 // Restore the thread state if we are going to discard the plan execution. There are three cases where this
5571 // could happen:
5572 // 1) The execution successfully completed
5573 // 2) We hit a breakpoint, and ignore_breakpoints was true
5574 // 3) We got some other error, and discard_on_error was true
Jim Ingham8646d3c2014-05-05 02:47:44 +00005575 bool should_unwind = (return_value == eExpressionInterrupted && options.DoesUnwindOnError())
5576 || (return_value == eExpressionHitBreakpoint && options.DoesIgnoreBreakpoints());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005577
Jim Ingham8646d3c2014-05-05 02:47:44 +00005578 if (return_value == eExpressionCompleted
Jim Ingham184e9812013-01-15 02:47:48 +00005579 || should_unwind)
Jim Ingham8559a352012-11-26 23:52:18 +00005580 {
5581 thread_plan_sp->RestoreThreadState();
5582 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005583
Sean Callanana46ec452012-07-11 21:31:24 +00005584 // Now do some processing on the results of the run:
Jim Ingham8646d3c2014-05-05 02:47:44 +00005585 if (return_value == eExpressionInterrupted || return_value == eExpressionHitBreakpoint)
Sean Callanana46ec452012-07-11 21:31:24 +00005586 {
5587 if (log)
5588 {
5589 StreamString s;
5590 if (event_sp)
5591 event_sp->Dump (&s);
5592 else
5593 {
5594 log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
5595 }
5596
5597 StreamString ts;
5598
5599 const char *event_explanation = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005600
Sean Callanana46ec452012-07-11 21:31:24 +00005601 do
5602 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005603 if (!event_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005604 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005605 event_explanation = "<no event>";
Sean Callanana46ec452012-07-11 21:31:24 +00005606 break;
5607 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005608 else if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005609 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005610 event_explanation = "<user interrupt>";
Sean Callanana46ec452012-07-11 21:31:24 +00005611 break;
5612 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005613 else
Sean Callanana46ec452012-07-11 21:31:24 +00005614 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005615 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
5616
5617 if (!event_data)
Sean Callanana46ec452012-07-11 21:31:24 +00005618 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005619 event_explanation = "<no event data>";
5620 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005621 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005622
Jim Inghamcfc09352012-07-27 23:57:19 +00005623 Process *process = event_data->GetProcessSP().get();
5624
5625 if (!process)
Sean Callanana46ec452012-07-11 21:31:24 +00005626 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005627 event_explanation = "<no process>";
5628 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005629 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005630
Jim Inghamcfc09352012-07-27 23:57:19 +00005631 ThreadList &thread_list = process->GetThreadList();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005632
Jim Inghamcfc09352012-07-27 23:57:19 +00005633 uint32_t num_threads = thread_list.GetSize();
5634 uint32_t thread_index;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005635
Jim Inghamcfc09352012-07-27 23:57:19 +00005636 ts.Printf("<%u threads> ", num_threads);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005637
Jim Inghamcfc09352012-07-27 23:57:19 +00005638 for (thread_index = 0;
5639 thread_index < num_threads;
5640 ++thread_index)
5641 {
5642 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005643
Jim Inghamcfc09352012-07-27 23:57:19 +00005644 if (!thread)
5645 {
5646 ts.Printf("<?> ");
5647 continue;
5648 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005649
Daniel Malead01b2952012-11-29 21:49:15 +00005650 ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00005651 RegisterContext *register_context = thread->GetRegisterContext().get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005652
Jim Inghamcfc09352012-07-27 23:57:19 +00005653 if (register_context)
Daniel Malead01b2952012-11-29 21:49:15 +00005654 ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC());
Jim Inghamcfc09352012-07-27 23:57:19 +00005655 else
5656 ts.Printf("[ip unknown] ");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005657
Jim Inghamcfc09352012-07-27 23:57:19 +00005658 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
5659 if (stop_info_sp)
5660 {
5661 const char *stop_desc = stop_info_sp->GetDescription();
5662 if (stop_desc)
5663 ts.PutCString (stop_desc);
5664 }
5665 ts.Printf(">");
5666 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005667
Jim Inghamcfc09352012-07-27 23:57:19 +00005668 event_explanation = ts.GetData();
Sean Callanana46ec452012-07-11 21:31:24 +00005669 }
Sean Callanana46ec452012-07-11 21:31:24 +00005670 } while (0);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005671
Jim Inghamcfc09352012-07-27 23:57:19 +00005672 if (event_explanation)
5673 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
Sean Callanana46ec452012-07-11 21:31:24 +00005674 else
Jim Inghamcfc09352012-07-27 23:57:19 +00005675 log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData());
5676 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005677
Jim Inghame4483cf2013-09-27 01:13:01 +00005678 if (should_unwind)
Jim Inghamcfc09352012-07-27 23:57:19 +00005679 {
5680 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005681 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.",
5682 static_cast<void*>(thread_plan_sp.get()));
Jim Inghamcfc09352012-07-27 23:57:19 +00005683 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5684 thread_plan_sp->SetPrivate (orig_plan_private);
5685 }
5686 else
5687 {
5688 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005689 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.",
5690 static_cast<void*>(thread_plan_sp.get()));
Sean Callanana46ec452012-07-11 21:31:24 +00005691 }
5692 }
Jim Ingham8646d3c2014-05-05 02:47:44 +00005693 else if (return_value == eExpressionSetupError)
Sean Callanana46ec452012-07-11 21:31:24 +00005694 {
5695 if (log)
5696 log->PutCString("Process::RunThreadPlan(): execution set up error.");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005697
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005698 if (options.DoesUnwindOnError())
Jim Ingham0f16e732011-02-08 05:20:59 +00005699 {
Greg Claytonc14ee322011-09-22 04:58:26 +00005700 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00005701 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham0f16e732011-02-08 05:20:59 +00005702 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005703 }
5704 else
5705 {
Sean Callanana46ec452012-07-11 21:31:24 +00005706 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
Jim Inghamf48169b2010-11-30 02:22:11 +00005707 {
Jim Ingham0f16e732011-02-08 05:20:59 +00005708 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005709 log->PutCString("Process::RunThreadPlan(): thread plan is done");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005710 return_value = eExpressionCompleted;
Sean Callanana46ec452012-07-11 21:31:24 +00005711 }
5712 else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
5713 {
5714 if (log)
5715 log->PutCString("Process::RunThreadPlan(): thread plan was discarded");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005716 return_value = eExpressionDiscarded;
Sean Callanana46ec452012-07-11 21:31:24 +00005717 }
5718 else
5719 {
5720 if (log)
5721 log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005722 if (options.DoesUnwindOnError() && thread_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005723 {
5724 if (log)
Jim Ingham184e9812013-01-15 02:47:48 +00005725 log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause unwind_on_error is set.");
Sean Callanana46ec452012-07-11 21:31:24 +00005726 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5727 thread_plan_sp->SetPrivate (orig_plan_private);
5728 }
5729 }
5730 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005731
Sean Callanana46ec452012-07-11 21:31:24 +00005732 // Thread we ran the function in may have gone away because we ran the target
5733 // Check that it's still there, and if it is put it back in the context. Also restore the
5734 // frame in the context if it is still present.
5735 thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get();
5736 if (thread)
5737 {
5738 exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id));
5739 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005740
Sean Callanana46ec452012-07-11 21:31:24 +00005741 // Also restore the current process'es selected frame & thread, since this function calling may
5742 // be done behind the user's back.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005743
Sean Callanana46ec452012-07-11 21:31:24 +00005744 if (selected_tid != LLDB_INVALID_THREAD_ID)
5745 {
5746 if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid())
5747 {
5748 // We were able to restore the selected thread, now restore the frame:
Daniel Maleaa012d3a2013-07-31 20:21:20 +00005749 Mutex::Locker lock(GetThreadList().GetMutex());
Jason Molendab57e4a12013-11-04 09:33:30 +00005750 StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id);
Sean Callanana46ec452012-07-11 21:31:24 +00005751 if (old_frame_sp)
5752 GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get());
Jim Inghamf48169b2010-11-30 02:22:11 +00005753 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005754 }
5755 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005756
Sean Callanana46ec452012-07-11 21:31:24 +00005757 // If the process exited during the run of the thread plan, notify everyone.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005758
Sean Callanana46ec452012-07-11 21:31:24 +00005759 if (event_to_broadcast_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00005760 {
Sean Callanana46ec452012-07-11 21:31:24 +00005761 if (log)
5762 log->PutCString("Process::RunThreadPlan(): rebroadcasting event.");
5763 BroadcastEvent(event_to_broadcast_sp);
Jim Inghamf48169b2010-11-30 02:22:11 +00005764 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005765
Jim Inghamf48169b2010-11-30 02:22:11 +00005766 return return_value;
5767}
5768
5769const char *
Jim Ingham1624a2d2014-05-05 02:26:40 +00005770Process::ExecutionResultAsCString (ExpressionResults result)
Jim Inghamf48169b2010-11-30 02:22:11 +00005771{
5772 const char *result_name;
5773
5774 switch (result)
5775 {
Jim Ingham8646d3c2014-05-05 02:47:44 +00005776 case eExpressionCompleted:
5777 result_name = "eExpressionCompleted";
Jim Inghamf48169b2010-11-30 02:22:11 +00005778 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005779 case eExpressionDiscarded:
5780 result_name = "eExpressionDiscarded";
Jim Inghamf48169b2010-11-30 02:22:11 +00005781 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005782 case eExpressionInterrupted:
5783 result_name = "eExpressionInterrupted";
Jim Inghamf48169b2010-11-30 02:22:11 +00005784 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005785 case eExpressionHitBreakpoint:
5786 result_name = "eExpressionHitBreakpoint";
Jim Ingham184e9812013-01-15 02:47:48 +00005787 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005788 case eExpressionSetupError:
5789 result_name = "eExpressionSetupError";
Jim Inghamf48169b2010-11-30 02:22:11 +00005790 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005791 case eExpressionParseError:
5792 result_name = "eExpressionParseError";
Jim Ingham1624a2d2014-05-05 02:26:40 +00005793 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005794 case eExpressionResultUnavailable:
5795 result_name = "eExpressionResultUnavailable";
Jim Ingham1624a2d2014-05-05 02:26:40 +00005796 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005797 case eExpressionTimedOut:
5798 result_name = "eExpressionTimedOut";
Jim Inghamf48169b2010-11-30 02:22:11 +00005799 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005800 case eExpressionStoppedForDebug:
5801 result_name = "eExpressionStoppedForDebug";
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005802 break;
Jim Inghamf48169b2010-11-30 02:22:11 +00005803 }
5804 return result_name;
5805}
5806
Greg Clayton7260f622011-04-18 08:33:37 +00005807void
5808Process::GetStatus (Stream &strm)
5809{
5810 const StateType state = GetState();
Greg Clayton2637f822011-11-17 01:23:07 +00005811 if (StateIsStoppedState(state, false))
Greg Clayton7260f622011-04-18 08:33:37 +00005812 {
5813 if (state == eStateExited)
5814 {
5815 int exit_status = GetExitStatus();
5816 const char *exit_description = GetExitDescription();
Daniel Malead01b2952012-11-29 21:49:15 +00005817 strm.Printf ("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n",
Greg Clayton7260f622011-04-18 08:33:37 +00005818 GetID(),
5819 exit_status,
5820 exit_status,
5821 exit_description ? exit_description : "");
5822 }
5823 else
5824 {
5825 if (state == eStateConnected)
5826 strm.Printf ("Connected to remote target.\n");
5827 else
Daniel Malead01b2952012-11-29 21:49:15 +00005828 strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state));
Greg Clayton7260f622011-04-18 08:33:37 +00005829 }
5830 }
5831 else
5832 {
Daniel Malead01b2952012-11-29 21:49:15 +00005833 strm.Printf ("Process %" PRIu64 " is running.\n", GetID());
Greg Clayton7260f622011-04-18 08:33:37 +00005834 }
5835}
5836
5837size_t
5838Process::GetThreadStatus (Stream &strm,
5839 bool only_threads_with_stop_reason,
5840 uint32_t start_frame,
5841 uint32_t num_frames,
5842 uint32_t num_frames_with_source)
5843{
5844 size_t num_thread_infos_dumped = 0;
5845
Jim Ingham4a65fb12014-03-07 11:20:03 +00005846 // You can't hold the thread list lock while calling Thread::GetStatus. That very well might run code (e.g. if we need it
5847 // to get return values or arguments.) For that to work the process has to be able to acquire it. So instead copy the thread
5848 // ID's, and look them up one by one:
5849
5850 uint32_t num_threads;
5851 std::vector<uint32_t> thread_index_array;
5852 //Scope for thread list locker;
5853 {
5854 Mutex::Locker locker (GetThreadList().GetMutex());
5855 ThreadList &curr_thread_list = GetThreadList();
5856 num_threads = curr_thread_list.GetSize();
5857 uint32_t idx;
5858 thread_index_array.resize(num_threads);
5859 for (idx = 0; idx < num_threads; ++idx)
5860 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID();
5861 }
5862
Greg Clayton7260f622011-04-18 08:33:37 +00005863 for (uint32_t i = 0; i < num_threads; i++)
5864 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00005865 ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_index_array[i]));
5866 if (thread_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00005867 {
5868 if (only_threads_with_stop_reason)
5869 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00005870 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
Jim Ingham5d88a062012-10-16 00:09:33 +00005871 if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid())
Greg Clayton7260f622011-04-18 08:33:37 +00005872 continue;
5873 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00005874 thread_sp->GetStatus (strm,
Greg Clayton7260f622011-04-18 08:33:37 +00005875 start_frame,
5876 num_frames,
5877 num_frames_with_source);
5878 ++num_thread_infos_dumped;
5879 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00005880 else
5881 {
5882 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
5883 if (log)
5884 log->Printf("Process::GetThreadStatus - thread 0x" PRIu64 " vanished while running Thread::GetStatus.");
5885
5886 }
Greg Clayton7260f622011-04-18 08:33:37 +00005887 }
5888 return num_thread_infos_dumped;
5889}
5890
Greg Claytona9f40ad2012-02-22 04:37:26 +00005891void
5892Process::AddInvalidMemoryRegion (const LoadRange &region)
5893{
5894 m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize());
5895}
5896
5897bool
5898Process::RemoveInvalidMemoryRange (const LoadRange &region)
5899{
5900 return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize());
5901}
5902
Jim Ingham372787f2012-04-07 00:00:41 +00005903void
5904Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton)
5905{
5906 m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton));
5907}
5908
5909bool
5910Process::RunPreResumeActions ()
5911{
5912 bool result = true;
5913 while (!m_pre_resume_actions.empty())
5914 {
5915 struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back();
5916 m_pre_resume_actions.pop_back();
5917 bool this_result = action.callback (action.baton);
5918 if (result == true) result = this_result;
5919 }
5920 return result;
5921}
5922
5923void
5924Process::ClearPreResumeActions ()
5925{
5926 m_pre_resume_actions.clear();
5927}
Greg Claytona9f40ad2012-02-22 04:37:26 +00005928
Greg Claytonfa559e52012-05-18 02:38:05 +00005929void
5930Process::Flush ()
5931{
5932 m_thread_list.Flush();
Jason Molenda5e8dce42013-12-13 00:29:16 +00005933 m_extended_thread_list.Flush();
5934 m_extended_thread_stop_id = 0;
5935 m_queue_list.Clear();
5936 m_queue_list_stop_id = 0;
Greg Claytonfa559e52012-05-18 02:38:05 +00005937}
Greg Clayton90ba8112012-12-05 00:16:59 +00005938
5939void
5940Process::DidExec ()
5941{
Todd Fiala76e0fc92014-08-27 22:58:26 +00005942 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
5943 if (log)
5944 log->Printf ("Process::%s()", __FUNCTION__);
5945
Greg Clayton90ba8112012-12-05 00:16:59 +00005946 Target &target = GetTarget();
5947 target.CleanupProcess ();
Greg Claytonb35db632013-11-09 00:03:31 +00005948 target.ClearModules(false);
Greg Clayton90ba8112012-12-05 00:16:59 +00005949 m_dynamic_checkers_ap.reset();
5950 m_abi_sp.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00005951 m_system_runtime_ap.reset();
Greg Clayton90ba8112012-12-05 00:16:59 +00005952 m_os_ap.reset();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005953 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00005954 m_jit_loaders_ap.reset();
Greg Clayton90ba8112012-12-05 00:16:59 +00005955 m_image_tokens.clear();
5956 m_allocated_memory_cache.Clear();
5957 m_language_runtimes.clear();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005958 m_thread_list.DiscardThreadPlans();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005959 m_memory_cache.Clear(true);
Greg Clayton90ba8112012-12-05 00:16:59 +00005960 DoDidExec();
5961 CompleteAttach ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00005962 // Flush the process (threads and all stack frames) after running CompleteAttach()
5963 // in case the dynamic loader loaded things in new locations.
5964 Flush();
Greg Claytonb35db632013-11-09 00:03:31 +00005965
5966 // After we figure out what was loaded/unloaded in CompleteAttach,
5967 // we need to let the target know so it can do any cleanup it needs to.
5968 target.DidExec();
Greg Clayton90ba8112012-12-05 00:16:59 +00005969}
Greg Clayton095eeaa2013-11-05 23:28:00 +00005970
Jim Ingham1460e4b2014-01-10 23:46:59 +00005971addr_t
5972Process::ResolveIndirectFunction(const Address *address, Error &error)
5973{
5974 if (address == nullptr)
5975 {
Jean-Daniel Dupasef37711f2014-02-08 20:22:05 +00005976 error.SetErrorString("Invalid address argument");
Jim Ingham1460e4b2014-01-10 23:46:59 +00005977 return LLDB_INVALID_ADDRESS;
5978 }
5979
5980 addr_t function_addr = LLDB_INVALID_ADDRESS;
5981
5982 addr_t addr = address->GetLoadAddress(&GetTarget());
5983 std::map<addr_t,addr_t>::const_iterator iter = m_resolved_indirect_addresses.find(addr);
5984 if (iter != m_resolved_indirect_addresses.end())
5985 {
5986 function_addr = (*iter).second;
5987 }
5988 else
5989 {
5990 if (!InferiorCall(this, address, function_addr))
5991 {
5992 Symbol *symbol = address->CalculateSymbolContextSymbol();
5993 error.SetErrorStringWithFormat ("Unable to call resolver for indirect function %s",
5994 symbol ? symbol->GetName().AsCString() : "<UNKNOWN>");
5995 function_addr = LLDB_INVALID_ADDRESS;
5996 }
5997 else
5998 {
5999 m_resolved_indirect_addresses.insert(std::pair<addr_t, addr_t>(addr, function_addr));
6000 }
6001 }
6002 return function_addr;
6003}
6004
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00006005void
6006Process::ModulesDidLoad (ModuleList &module_list)
6007{
6008 SystemRuntime *sys_runtime = GetSystemRuntime();
6009 if (sys_runtime)
6010 {
6011 sys_runtime->ModulesDidLoad (module_list);
6012 }
6013
6014 GetJITLoaders().ModulesDidLoad (module_list);
6015}