blob: 02f96358433172bbb77f2d247dd75ec3bfb44512 [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." },
Jason Molendaf0340c92014-09-03 22:30:54 +0000111 { "memory-cache-line-size" , OptionValue::eTypeUInt64, false, 512, NULL, NULL, "The memory cache line size" },
Greg Clayton67cc0632012-08-22 17:17:09 +0000112 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
113};
114
115enum {
116 ePropertyDisableMemCache,
Greg Claytonc9d645d2012-10-18 22:40:37 +0000117 ePropertyExtraStartCommand,
Jim Ingham184e9812013-01-15 02:47:48 +0000118 ePropertyIgnoreBreakpointsInExpressions,
119 ePropertyUnwindOnErrorInExpressions,
Jim Ingham29950772013-01-26 02:19:28 +0000120 ePropertyPythonOSPluginPath,
Jim Inghamacff8952013-05-02 00:27:30 +0000121 ePropertyStopOnSharedLibraryEvents,
Jason Molendaf0340c92014-09-03 22:30:54 +0000122 ePropertyDetachKeepsStopped,
123 ePropertyMemCacheLineSize
Greg Clayton67cc0632012-08-22 17:17:09 +0000124};
125
126ProcessProperties::ProcessProperties (bool is_global) :
127 Properties ()
128{
129 if (is_global)
130 {
131 m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process")));
132 m_collection_sp->Initialize(g_properties);
133 m_collection_sp->AppendProperty(ConstString("thread"),
Jim Ingham29950772013-01-26 02:19:28 +0000134 ConstString("Settings specific to threads."),
Greg Clayton67cc0632012-08-22 17:17:09 +0000135 true,
136 Thread::GetGlobalProperties()->GetValueProperties());
137 }
138 else
139 m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get()));
140}
141
142ProcessProperties::~ProcessProperties()
143{
144}
145
146bool
147ProcessProperties::GetDisableMemoryCache() const
148{
149 const uint32_t idx = ePropertyDisableMemCache;
150 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
151}
152
Jason Molendaf0340c92014-09-03 22:30:54 +0000153uint64_t
154ProcessProperties::GetMemoryCacheLineSize() const
155{
156 const uint32_t idx = ePropertyMemCacheLineSize;
157 return m_collection_sp->GetPropertyAtIndexAsUInt64 (NULL, idx, g_properties[idx].default_uint_value);
158}
159
Greg Clayton67cc0632012-08-22 17:17:09 +0000160Args
161ProcessProperties::GetExtraStartupCommands () const
162{
163 Args args;
164 const uint32_t idx = ePropertyExtraStartCommand;
165 m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args);
166 return args;
167}
168
169void
170ProcessProperties::SetExtraStartupCommands (const Args &args)
171{
172 const uint32_t idx = ePropertyExtraStartCommand;
173 m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args);
174}
175
Greg Claytonc9d645d2012-10-18 22:40:37 +0000176FileSpec
177ProcessProperties::GetPythonOSPluginPath () const
178{
179 const uint32_t idx = ePropertyPythonOSPluginPath;
180 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
181}
182
183void
184ProcessProperties::SetPythonOSPluginPath (const FileSpec &file)
185{
186 const uint32_t idx = ePropertyPythonOSPluginPath;
187 m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file);
188}
189
Jim Ingham184e9812013-01-15 02:47:48 +0000190
191bool
192ProcessProperties::GetIgnoreBreakpointsInExpressions () const
193{
194 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
195 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
196}
197
198void
199ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore)
200{
201 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
202 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
203}
204
205bool
206ProcessProperties::GetUnwindOnErrorInExpressions () const
207{
208 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
209 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
210}
211
212void
213ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore)
214{
215 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
216 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
217}
218
Jim Ingham29950772013-01-26 02:19:28 +0000219bool
220ProcessProperties::GetStopOnSharedLibraryEvents () const
221{
222 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
223 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
224}
225
226void
227ProcessProperties::SetStopOnSharedLibraryEvents (bool stop)
228{
229 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
230 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
231}
232
Jim Inghamacff8952013-05-02 00:27:30 +0000233bool
234ProcessProperties::GetDetachKeepsStopped () const
235{
236 const uint32_t idx = ePropertyDetachKeepsStopped;
237 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
238}
239
240void
241ProcessProperties::SetDetachKeepsStopped (bool stop)
242{
243 const uint32_t idx = ePropertyDetachKeepsStopped;
244 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
245}
246
Greg Clayton32e0a752011-03-30 18:16:51 +0000247void
Greg Clayton8b82f082011-04-12 05:54:46 +0000248ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000249{
250 const char *cstr;
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000251 if (m_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000252 s.Printf (" pid = %" PRIu64 "\n", m_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000253
254 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000255 s.Printf (" parent = %" PRIu64 "\n", m_parent_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000256
257 if (m_executable)
258 {
259 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
260 s.PutCString (" file = ");
261 m_executable.Dump(&s);
262 s.EOL();
263 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000264 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000265 if (argc > 0)
266 {
267 for (uint32_t i=0; i<argc; i++)
268 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000269 const char *arg = m_arguments.GetArgumentAtIndex(i);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000270 if (i < 10)
Greg Clayton8b82f082011-04-12 05:54:46 +0000271 s.Printf (" arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000272 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000273 s.Printf ("arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000274 }
275 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000276
277 const uint32_t envc = m_environment.GetArgumentCount();
278 if (envc > 0)
279 {
280 for (uint32_t i=0; i<envc; i++)
281 {
282 const char *env = m_environment.GetArgumentAtIndex(i);
283 if (i < 10)
284 s.Printf (" env[%u] = %s\n", i, env);
285 else
286 s.Printf ("env[%u] = %s\n", i, env);
287 }
288 }
289
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000290 if (m_arch.IsValid())
291 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
292
Greg Clayton8b82f082011-04-12 05:54:46 +0000293 if (m_uid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000294 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000295 cstr = platform->GetUserName (m_uid);
296 s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000297 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000298 if (m_gid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000299 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000300 cstr = platform->GetGroupName (m_gid);
301 s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000302 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000303 if (m_euid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000304 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000305 cstr = platform->GetUserName (m_euid);
306 s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000307 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000308 if (m_egid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000309 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000310 cstr = platform->GetGroupName (m_egid);
311 s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000312 }
313}
314
315void
Greg Clayton8b82f082011-04-12 05:54:46 +0000316ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose)
Greg Clayton32e0a752011-03-30 18:16:51 +0000317{
Greg Clayton8b82f082011-04-12 05:54:46 +0000318 const char *label;
319 if (show_args || verbose)
320 label = "ARGUMENTS";
321 else
322 label = "NAME";
323
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000324 if (verbose)
325 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000326 s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000327 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
328 }
329 else
330 {
Jim Ingham368ac222014-08-15 17:05:27 +0000331 s.Printf ("PID PARENT USER TRIPLE %s\n", label);
332 s.PutCString ("====== ====== ========== ======================== ============================\n");
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000333 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000334}
335
336void
Greg Clayton8b82f082011-04-12 05:54:46 +0000337ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000338{
339 if (m_pid != LLDB_INVALID_PROCESS_ID)
340 {
341 const char *cstr;
Daniel Malead01b2952012-11-29 21:49:15 +0000342 s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000343
Greg Clayton32e0a752011-03-30 18:16:51 +0000344
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000345 if (verbose)
346 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000347 cstr = platform->GetUserName (m_uid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000348 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
349 s.Printf ("%-10s ", cstr);
350 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000351 s.Printf ("%-10u ", m_uid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000352
Greg Clayton8b82f082011-04-12 05:54:46 +0000353 cstr = platform->GetGroupName (m_gid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000354 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
355 s.Printf ("%-10s ", cstr);
356 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000357 s.Printf ("%-10u ", m_gid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000358
Greg Clayton8b82f082011-04-12 05:54:46 +0000359 cstr = platform->GetUserName (m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000360 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
361 s.Printf ("%-10s ", cstr);
362 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000363 s.Printf ("%-10u ", m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000364
Greg Clayton8b82f082011-04-12 05:54:46 +0000365 cstr = platform->GetGroupName (m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000366 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
367 s.Printf ("%-10s ", cstr);
368 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000369 s.Printf ("%-10u ", m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000370 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
371 }
372 else
373 {
Jim Ingham368ac222014-08-15 17:05:27 +0000374 s.Printf ("%-10s %-24s ",
Greg Clayton8b82f082011-04-12 05:54:46 +0000375 platform->GetUserName (m_euid),
Jim Ingham368ac222014-08-15 17:05:27 +0000376 m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000377 }
378
Greg Clayton8b82f082011-04-12 05:54:46 +0000379 if (verbose || show_args)
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000380 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000381 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000382 if (argc > 0)
383 {
384 for (uint32_t i=0; i<argc; i++)
385 {
386 if (i > 0)
387 s.PutChar (' ');
Greg Clayton8b82f082011-04-12 05:54:46 +0000388 s.PutCString (m_arguments.GetArgumentAtIndex(i));
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000389 }
390 }
391 }
392 else
393 {
394 s.PutCString (GetName());
395 }
396
397 s.EOL();
Greg Clayton32e0a752011-03-30 18:16:51 +0000398 }
399}
400
Greg Clayton8b82f082011-04-12 05:54:46 +0000401Error
Greg Claytonf6b8b582011-04-13 00:18:08 +0000402ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Clayton8b82f082011-04-12 05:54:46 +0000403{
404 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000405 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton8b82f082011-04-12 05:54:46 +0000406
407 switch (short_option)
408 {
409 case 's': // Stop at program entry point
410 launch_info.GetFlags().Set (eLaunchFlagStopAtEntry);
411 break;
412
Greg Clayton8b82f082011-04-12 05:54:46 +0000413 case 'i': // STDIN for read only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000414 {
415 FileAction action;
416 if (action.Open (STDIN_FILENO, option_arg, true, false))
417 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000418 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000419 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000420
421 case 'o': // Open STDOUT for write only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000422 {
423 FileAction action;
424 if (action.Open (STDOUT_FILENO, option_arg, false, true))
425 launch_info.AppendFileAction (action);
Greg Clayton9845a8d2012-03-06 04:01:04 +0000426 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000427 }
Greg Clayton9845a8d2012-03-06 04:01:04 +0000428
429 case 'e': // STDERR for write only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000430 {
431 FileAction action;
432 if (action.Open (STDERR_FILENO, option_arg, false, true))
433 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000434 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000435 }
Greg Clayton9845a8d2012-03-06 04:01:04 +0000436
Greg Clayton8b82f082011-04-12 05:54:46 +0000437 case 'p': // Process plug-in name
438 launch_info.SetProcessPluginName (option_arg);
439 break;
440
441 case 'n': // Disable STDIO
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000442 {
443 FileAction action;
444 if (action.Open (STDIN_FILENO, "/dev/null", true, false))
445 launch_info.AppendFileAction (action);
446 if (action.Open (STDOUT_FILENO, "/dev/null", false, true))
447 launch_info.AppendFileAction (action);
448 if (action.Open (STDERR_FILENO, "/dev/null", false, true))
449 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000450 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000451 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000452
453 case 'w':
454 launch_info.SetWorkingDirectory (option_arg);
455 break;
456
457 case 't': // Open process in new terminal window
458 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
459 break;
460
461 case 'a':
Greg Clayton70512312012-05-08 01:45:38 +0000462 if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get()))
463 launch_info.GetArchitecture().SetTriple (option_arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000464 break;
465
Todd Fiala51637922014-08-19 17:40:43 +0000466 case 'A': // Disable ASLR.
467 {
468 bool success;
469 const bool disable_aslr_arg = Args::StringToBoolean (option_arg, true, &success);
470 if (success)
471 disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo;
472 else
473 error.SetErrorStringWithFormat ("Invalid boolean value for disable-aslr option: '%s'", option_arg ? option_arg : "<null>");
Greg Clayton8b82f082011-04-12 05:54:46 +0000474 break;
Todd Fiala51637922014-08-19 17:40:43 +0000475 }
476
477 case 'c':
Greg Clayton144f3a92011-11-15 03:53:30 +0000478 if (option_arg && option_arg[0])
479 launch_info.SetShell (option_arg);
480 else
Ed Masteb8ca4a22013-09-03 23:04:53 +0000481 launch_info.SetShell (LLDB_DEFAULT_SHELL);
Greg Clayton982c9762011-11-03 21:22:33 +0000482 break;
483
Greg Clayton8b82f082011-04-12 05:54:46 +0000484 case 'v':
485 launch_info.GetEnvironmentEntries().AppendArgument(option_arg);
486 break;
487
488 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000489 error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option);
Greg Clayton8b82f082011-04-12 05:54:46 +0000490 break;
Greg Clayton8b82f082011-04-12 05:54:46 +0000491 }
492 return error;
493}
494
495OptionDefinition
496ProcessLaunchCommandOptions::g_option_table[] =
497{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000498{ 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 +0000499{ 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 +0000500{ LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
501{ 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."},
502{ LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."},
503{ 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."},
504{ 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 +0000505
Zachary Turnerd37221d2014-07-09 16:31:49 +0000506{ LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."},
507{ LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."},
508{ 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 +0000509
Zachary Turnerd37221d2014-07-09 16:31:49 +0000510{ 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 +0000511
Zachary Turnerd37221d2014-07-09 16:31:49 +0000512{ 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 +0000513
Zachary Turnerd37221d2014-07-09 16:31:49 +0000514{ 0 , false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Clayton8b82f082011-04-12 05:54:46 +0000515};
516
517
518
519bool
520ProcessInstanceInfoMatch::NameMatches (const char *process_name) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000521{
522 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
523 return true;
524 const char *match_name = m_match_info.GetName();
525 if (!match_name)
526 return true;
527
528 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
529}
530
531bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000532ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000533{
534 if (!NameMatches (proc_info.GetName()))
535 return false;
536
537 if (m_match_info.ProcessIDIsValid() &&
538 m_match_info.GetProcessID() != proc_info.GetProcessID())
539 return false;
540
541 if (m_match_info.ParentProcessIDIsValid() &&
542 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
543 return false;
544
Greg Clayton8b82f082011-04-12 05:54:46 +0000545 if (m_match_info.UserIDIsValid () &&
546 m_match_info.GetUserID() != proc_info.GetUserID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000547 return false;
548
Greg Clayton8b82f082011-04-12 05:54:46 +0000549 if (m_match_info.GroupIDIsValid () &&
550 m_match_info.GetGroupID() != proc_info.GetGroupID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000551 return false;
552
553 if (m_match_info.EffectiveUserIDIsValid () &&
554 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
555 return false;
556
557 if (m_match_info.EffectiveGroupIDIsValid () &&
558 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
559 return false;
560
561 if (m_match_info.GetArchitecture().IsValid() &&
Sean Callananbf4b7be2012-12-13 22:07:14 +0000562 !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture()))
Greg Clayton32e0a752011-03-30 18:16:51 +0000563 return false;
564 return true;
565}
566
567bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000568ProcessInstanceInfoMatch::MatchAllProcesses () const
Greg Clayton32e0a752011-03-30 18:16:51 +0000569{
570 if (m_name_match_type != eNameMatchIgnore)
571 return false;
572
573 if (m_match_info.ProcessIDIsValid())
574 return false;
575
576 if (m_match_info.ParentProcessIDIsValid())
577 return false;
578
Greg Clayton8b82f082011-04-12 05:54:46 +0000579 if (m_match_info.UserIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000580 return false;
581
Greg Clayton8b82f082011-04-12 05:54:46 +0000582 if (m_match_info.GroupIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000583 return false;
584
585 if (m_match_info.EffectiveUserIDIsValid ())
586 return false;
587
588 if (m_match_info.EffectiveGroupIDIsValid ())
589 return false;
590
591 if (m_match_info.GetArchitecture().IsValid())
592 return false;
593
594 if (m_match_all_users)
595 return false;
596
597 return true;
598
599}
600
601void
Greg Clayton8b82f082011-04-12 05:54:46 +0000602ProcessInstanceInfoMatch::Clear()
Greg Clayton32e0a752011-03-30 18:16:51 +0000603{
604 m_match_info.Clear();
605 m_name_match_type = eNameMatchIgnore;
606 m_match_all_users = false;
607}
Greg Clayton58be07b2011-01-07 06:08:19 +0000608
Greg Claytonc3776bf2012-02-09 06:16:32 +0000609ProcessSP
610Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611{
Greg Clayton949e8222013-01-16 17:29:04 +0000612 static uint32_t g_process_unique_id = 0;
613
Greg Claytonc3776bf2012-02-09 06:16:32 +0000614 ProcessSP process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 ProcessCreateInstance create_callback = NULL;
616 if (plugin_name)
617 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000618 ConstString const_plugin_name(plugin_name);
619 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 if (create_callback)
621 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000622 process_sp = create_callback(target, listener, crash_file_path);
623 if (process_sp)
624 {
Greg Clayton949e8222013-01-16 17:29:04 +0000625 if (process_sp->CanDebug(target, true))
626 {
627 process_sp->m_process_unique_id = ++g_process_unique_id;
628 }
629 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000630 process_sp.reset();
631 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 }
633 }
634 else
635 {
Greg Claytonc982c762010-07-09 20:39:50 +0000636 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000638 process_sp = create_callback(target, listener, crash_file_path);
639 if (process_sp)
640 {
Greg Clayton949e8222013-01-16 17:29:04 +0000641 if (process_sp->CanDebug(target, false))
642 {
643 process_sp->m_process_unique_id = ++g_process_unique_id;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000644 break;
Greg Clayton949e8222013-01-16 17:29:04 +0000645 }
646 else
647 process_sp.reset();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000648 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649 }
650 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000651 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652}
653
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000654ConstString &
655Process::GetStaticBroadcasterClass ()
656{
657 static ConstString class_name ("lldb.process");
658 return class_name;
659}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660
661//----------------------------------------------------------------------
662// Process constructor
663//----------------------------------------------------------------------
664Process::Process(Target &target, Listener &listener) :
Todd Fiala4ceced32014-08-29 17:35:57 +0000665 Process(target, listener, Host::GetUnixSignals ())
666{
667 // This constructor just delegates to the full Process constructor,
668 // defaulting to using the Host's UnixSignals.
669}
670
671Process::Process(Target &target, Listener &listener, const UnixSignalsSP &unix_signals_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000672 ProcessProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 UserID (LLDB_INVALID_PROCESS_ID),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000674 Broadcaster (&(target.GetDebugger()), "lldb.process"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 m_target (target),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676 m_public_state (eStateUnloaded),
677 m_private_state (eStateUnloaded),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000678 m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"),
679 m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 m_private_state_listener ("lldb.process.internal_state_listener"),
681 m_private_state_control_wait(),
682 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
Jim Ingham4b536182011-08-09 02:12:22 +0000683 m_mod_id (),
Greg Clayton949e8222013-01-16 17:29:04 +0000684 m_process_unique_id(0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 m_thread_index_id (0),
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000686 m_thread_id_to_index_id_map (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 m_exit_status (-1),
688 m_exit_string (),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000689 m_thread_mutex (Mutex::eMutexTypeRecursive),
690 m_thread_list_real (this),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691 m_thread_list (this),
Jason Molenda864f1cc2013-11-11 05:20:44 +0000692 m_extended_thread_list (this),
Jason Molenda4ff13262013-11-20 00:31:38 +0000693 m_extended_thread_stop_id (0),
Jason Molenda5e8dce42013-12-13 00:29:16 +0000694 m_queue_list (this),
695 m_queue_list_stop_id (0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 m_notifications (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000697 m_image_tokens (),
698 m_listener (listener),
699 m_breakpoint_site_list (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000700 m_dynamic_checkers_ap (),
Todd Fiala4ceced32014-08-29 17:35:57 +0000701 m_unix_signals_sp (unix_signals_sp),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000702 m_abi_sp (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +0000703 m_process_input_reader (),
Greg Clayton3e06bd92011-01-09 21:07:35 +0000704 m_stdio_communication ("process.stdio"),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000705 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton58be07b2011-01-07 06:08:19 +0000706 m_stdout_data (),
Greg Clayton93e86192011-11-13 04:45:22 +0000707 m_stderr_data (),
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000708 m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive),
709 m_profile_data (),
Todd Fialaa3b89e22014-08-12 14:33:19 +0000710 m_iohandler_sync (false),
Greg Claytond495c532011-05-17 03:37:42 +0000711 m_memory_cache (*this),
712 m_allocated_memory_cache (*this),
Greg Claytone24c4ac2011-11-17 04:46:02 +0000713 m_should_detach (false),
Sean Callanan90539452011-09-20 23:01:51 +0000714 m_next_event_action_ap(),
Greg Clayton96249852013-04-18 16:57:27 +0000715 m_public_run_lock (),
Greg Clayton96249852013-04-18 16:57:27 +0000716 m_private_run_lock (),
Jim Inghamaacc3182012-06-06 00:29:30 +0000717 m_currently_handling_event(false),
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000718 m_finalize_called(false),
Greg Claytonf9b57b92013-05-10 23:48:10 +0000719 m_clear_thread_plans_on_stop (false),
Jim Ingham1460e4b2014-01-10 23:46:59 +0000720 m_force_next_event_delivery(false),
Jim Ingham0161b492013-02-09 01:29:05 +0000721 m_last_broadcast_state (eStateInvalid),
Jason Molenda69b6b632013-03-05 03:33:59 +0000722 m_destroy_in_process (false),
723 m_can_jit(eCanJITDontKnow)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724{
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000725 CheckInWithManager ();
Caroline Tice1559a462010-09-27 00:30:10 +0000726
Greg Clayton5160ce52013-03-27 23:08:40 +0000727 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000729 log->Printf ("%p Process::Process()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730
Todd Fiala4ceced32014-08-29 17:35:57 +0000731 if (!m_unix_signals_sp)
732 m_unix_signals_sp.reset (new UnixSignals ());
733
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000734 SetEventName (eBroadcastBitStateChanged, "state-changed");
735 SetEventName (eBroadcastBitInterrupt, "interrupt");
736 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
737 SetEventName (eBroadcastBitSTDERR, "stderr-available");
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000738 SetEventName (eBroadcastBitProfileData, "profile-data-available");
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000739
Greg Clayton35a4cc52012-10-29 20:52:08 +0000740 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" );
741 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" );
742 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume");
743
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 listener.StartListeningForEvents (this,
745 eBroadcastBitStateChanged |
746 eBroadcastBitInterrupt |
747 eBroadcastBitSTDOUT |
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000748 eBroadcastBitSTDERR |
749 eBroadcastBitProfileData);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750
751 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +0000752 eBroadcastBitStateChanged |
753 eBroadcastBitInterrupt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754
755 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
756 eBroadcastInternalStateControlStop |
757 eBroadcastInternalStateControlPause |
758 eBroadcastInternalStateControlResume);
Todd Fiala4ceced32014-08-29 17:35:57 +0000759 // We need something valid here, even if just the default UnixSignalsSP.
760 assert (m_unix_signals_sp && "null m_unix_signals_sp after initialization");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761}
762
763//----------------------------------------------------------------------
764// Destructor
765//----------------------------------------------------------------------
766Process::~Process()
767{
Greg Clayton5160ce52013-03-27 23:08:40 +0000768 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000770 log->Printf ("%p Process::~Process()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771 StopPrivateStateThread();
772}
773
Greg Clayton67cc0632012-08-22 17:17:09 +0000774const ProcessPropertiesSP &
775Process::GetGlobalProperties()
776{
777 static ProcessPropertiesSP g_settings_sp;
778 if (!g_settings_sp)
779 g_settings_sp.reset (new ProcessProperties (true));
780 return g_settings_sp;
781}
782
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783void
784Process::Finalize()
785{
Greg Claytone24c4ac2011-11-17 04:46:02 +0000786 switch (GetPrivateState())
787 {
788 case eStateConnected:
789 case eStateAttaching:
790 case eStateLaunching:
791 case eStateStopped:
792 case eStateRunning:
793 case eStateStepping:
794 case eStateCrashed:
795 case eStateSuspended:
796 if (GetShouldDetach())
Jim Inghamacff8952013-05-02 00:27:30 +0000797 {
798 // FIXME: This will have to be a process setting:
799 bool keep_stopped = false;
800 Detach(keep_stopped);
801 }
Greg Claytone24c4ac2011-11-17 04:46:02 +0000802 else
803 Destroy();
804 break;
805
806 case eStateInvalid:
807 case eStateUnloaded:
808 case eStateDetached:
809 case eStateExited:
810 break;
811 }
812
Greg Clayton1ed54f52011-10-01 00:45:15 +0000813 // Clear our broadcaster before we proceed with destroying
814 Broadcaster::Clear();
815
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 // Do any cleanup needed prior to being destructed... Subclasses
817 // that override this method should call this superclass method as well.
Jim Inghamd0a3e122011-02-16 17:54:55 +0000818
819 // We need to destroy the loader before the derived Process class gets destroyed
820 // since it is very likely that undoing the loader will require access to the real process.
Greg Clayton894f82f2012-01-20 23:08:34 +0000821 m_dynamic_checkers_ap.reset();
822 m_abi_sp.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000823 m_os_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +0000824 m_system_runtime_ap.reset();
Greg Clayton894f82f2012-01-20 23:08:34 +0000825 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +0000826 m_jit_loaders_ap.reset();
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000827 m_thread_list_real.Destroy();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000828 m_thread_list.Destroy();
Jason Molenda864f1cc2013-11-11 05:20:44 +0000829 m_extended_thread_list.Destroy();
Jason Molenda5e8dce42013-12-13 00:29:16 +0000830 m_queue_list.Clear();
831 m_queue_list_stop_id = 0;
Greg Clayton894f82f2012-01-20 23:08:34 +0000832 std::vector<Notifications> empty_notifications;
833 m_notifications.swap(empty_notifications);
834 m_image_tokens.clear();
835 m_memory_cache.Clear();
836 m_allocated_memory_cache.Clear();
837 m_language_runtimes.clear();
838 m_next_event_action_ap.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000839//#ifdef LLDB_CONFIGURATION_DEBUG
840// StreamFile s(stdout, false);
841// EventSP event_sp;
842// while (m_private_state_listener.GetNextEvent(event_sp))
843// {
844// event_sp->Dump (&s);
845// s.EOL();
846// }
847//#endif
848 // We have to be very careful here as the m_private_state_listener might
849 // contain events that have ProcessSP values in them which can keep this
850 // process around forever. These events need to be cleared out.
851 m_private_state_listener.Clear();
Ed Maste64fad602013-07-29 20:58:06 +0000852 m_public_run_lock.TrySetRunning(); // This will do nothing if already locked
853 m_public_run_lock.SetStopped();
854 m_private_run_lock.TrySetRunning(); // This will do nothing if already locked
855 m_private_run_lock.SetStopped();
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000856 m_finalize_called = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857}
858
859void
860Process::RegisterNotificationCallbacks (const Notifications& callbacks)
861{
862 m_notifications.push_back(callbacks);
863 if (callbacks.initialize != NULL)
864 callbacks.initialize (callbacks.baton, this);
865}
866
867bool
868Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
869{
870 std::vector<Notifications>::iterator pos, end = m_notifications.end();
871 for (pos = m_notifications.begin(); pos != end; ++pos)
872 {
873 if (pos->baton == callbacks.baton &&
874 pos->initialize == callbacks.initialize &&
875 pos->process_state_changed == callbacks.process_state_changed)
876 {
877 m_notifications.erase(pos);
878 return true;
879 }
880 }
881 return false;
882}
883
884void
885Process::SynchronouslyNotifyStateChanged (StateType state)
886{
887 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
888 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
889 {
890 if (notification_pos->process_state_changed)
891 notification_pos->process_state_changed (notification_pos->baton, this, state);
892 }
893}
894
895// FIXME: We need to do some work on events before the general Listener sees them.
896// For instance if we are continuing from a breakpoint, we need to ensure that we do
897// the little "insert real insn, step & stop" trick. But we can't do that when the
898// event is delivered by the broadcaster - since that is done on the thread that is
899// waiting for new events, so if we needed more than one event for our handling, we would
900// stall. So instead we do it when we fetch the event off of the queue.
901//
902
903StateType
904Process::GetNextEvent (EventSP &event_sp)
905{
906 StateType state = eStateInvalid;
907
908 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
909 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
910
911 return state;
912}
913
Todd Fialaa3b89e22014-08-12 14:33:19 +0000914bool
915Process::SyncIOHandler (uint64_t timeout_msec)
916{
917 bool timed_out = false;
918
919 // don't sync (potentially context switch) in case where there is no process IO
920 if (m_process_input_reader)
921 {
922 TimeValue timeout = TimeValue::Now();
923 timeout.OffsetWithMicroSeconds(timeout_msec*1000);
924
925 m_iohandler_sync.WaitForValueEqualTo(true, &timeout, &timed_out);
926
927 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
928 if(log)
929 {
930 if(timed_out)
931 log->Printf ("Process::%s pid %" PRIu64 " (timeout=%" PRIu64 "ms): FAIL", __FUNCTION__, GetID (), timeout_msec);
932 else
933 log->Printf ("Process::%s pid %" PRIu64 ": SUCCESS", __FUNCTION__, GetID ());
934 }
935
936 // reset sync one-shot so it will be ready for next time
937 m_iohandler_sync.SetValue(false, eBroadcastNever);
938 }
939
940 return !timed_out;
941}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942
943StateType
Greg Clayton44d93782014-01-27 23:43:24 +0000944Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr, bool wait_always, Listener *hijack_listener)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945{
Jim Ingham4b536182011-08-09 02:12:22 +0000946 // We can't just wait for a "stopped" event, because the stopped event may have restarted the target.
947 // We have to actually check each event, and in the case of a stopped event check the restarted flag
948 // on the event.
Greg Clayton85fb1b92012-09-11 02:33:37 +0000949 if (event_sp_ptr)
950 event_sp_ptr->reset();
Jim Ingham4b536182011-08-09 02:12:22 +0000951 StateType state = GetState();
952 // If we are exited or detached, we won't ever get back to any
953 // other valid state...
954 if (state == eStateDetached || state == eStateExited)
955 return state;
956
Daniel Malea9e9919f2013-10-09 16:56:28 +0000957 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
958 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000959 log->Printf ("Process::%s (timeout = %p)", __FUNCTION__,
960 static_cast<const void*>(timeout));
Daniel Malea9e9919f2013-10-09 16:56:28 +0000961
962 if (!wait_always &&
963 StateIsStoppedState(state, true) &&
964 StateIsStoppedState(GetPrivateState(), true)) {
965 if (log)
966 log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.",
967 __FUNCTION__);
968 return state;
969 }
970
Jim Ingham4b536182011-08-09 02:12:22 +0000971 while (state != eStateInvalid)
972 {
Greg Clayton85fb1b92012-09-11 02:33:37 +0000973 EventSP event_sp;
Greg Clayton44d93782014-01-27 23:43:24 +0000974 state = WaitForStateChangedEvents (timeout, event_sp, hijack_listener);
Greg Clayton85fb1b92012-09-11 02:33:37 +0000975 if (event_sp_ptr && event_sp)
976 *event_sp_ptr = event_sp;
977
Jim Ingham4b536182011-08-09 02:12:22 +0000978 switch (state)
979 {
980 case eStateCrashed:
981 case eStateDetached:
982 case eStateExited:
983 case eStateUnloaded:
Greg Clayton44d93782014-01-27 23:43:24 +0000984 // We need to toggle the run lock as this won't get done in
985 // SetPublicState() if the process is hijacked.
986 if (hijack_listener)
987 m_public_run_lock.SetStopped();
Jim Ingham4b536182011-08-09 02:12:22 +0000988 return state;
989 case eStateStopped:
990 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
991 continue;
992 else
Greg Clayton44d93782014-01-27 23:43:24 +0000993 {
994 // We need to toggle the run lock as this won't get done in
995 // SetPublicState() if the process is hijacked.
996 if (hijack_listener)
997 m_public_run_lock.SetStopped();
Jim Ingham4b536182011-08-09 02:12:22 +0000998 return state;
Greg Clayton44d93782014-01-27 23:43:24 +0000999 }
Jim Ingham4b536182011-08-09 02:12:22 +00001000 default:
1001 continue;
1002 }
1003 }
1004 return state;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005}
1006
1007
1008StateType
1009Process::WaitForState
1010(
1011 const TimeValue *timeout,
Greg Clayton44d93782014-01-27 23:43:24 +00001012 const StateType *match_states,
1013 const uint32_t num_match_states
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014)
1015{
1016 EventSP event_sp;
1017 uint32_t i;
Greg Clayton05faeb72010-10-07 04:19:01 +00001018 StateType state = GetState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 while (state != eStateInvalid)
1020 {
Greg Clayton05faeb72010-10-07 04:19:01 +00001021 // If we are exited or detached, we won't ever get back to any
1022 // other valid state...
1023 if (state == eStateDetached || state == eStateExited)
1024 return state;
1025
Greg Clayton44d93782014-01-27 23:43:24 +00001026 state = WaitForStateChangedEvents (timeout, event_sp, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027
1028 for (i=0; i<num_match_states; ++i)
1029 {
1030 if (match_states[i] == state)
1031 return state;
1032 }
1033 }
1034 return state;
1035}
1036
Jim Ingham30f9b212010-10-11 23:53:14 +00001037bool
1038Process::HijackProcessEvents (Listener *listener)
1039{
1040 if (listener != NULL)
1041 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001042 return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham30f9b212010-10-11 23:53:14 +00001043 }
1044 else
1045 return false;
1046}
1047
1048void
1049Process::RestoreProcessEvents ()
1050{
1051 RestoreBroadcaster();
1052}
1053
Jim Ingham0f16e732011-02-08 05:20:59 +00001054bool
1055Process::HijackPrivateProcessEvents (Listener *listener)
1056{
1057 if (listener != NULL)
1058 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001059 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham0f16e732011-02-08 05:20:59 +00001060 }
1061 else
1062 return false;
1063}
1064
1065void
1066Process::RestorePrivateProcessEvents ()
1067{
1068 m_private_state_broadcaster.RestoreBroadcaster();
1069}
1070
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071StateType
Greg Clayton44d93782014-01-27 23:43:24 +00001072Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp, Listener *hijack_listener)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073{
Greg Clayton5160ce52013-03-27 23:08:40 +00001074 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075
1076 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001077 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1078 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079
Greg Clayton44d93782014-01-27 23:43:24 +00001080 Listener *listener = hijack_listener;
1081 if (listener == NULL)
1082 listener = &m_listener;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001083
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 StateType state = eStateInvalid;
Greg Clayton44d93782014-01-27 23:43:24 +00001085 if (listener->WaitForEventForBroadcasterWithType (timeout,
1086 this,
1087 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
1088 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001089 {
1090 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1091 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1092 else if (log)
1093 log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__);
1094 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095
1096 if (log)
1097 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001098 __FUNCTION__, static_cast<const void*>(timeout),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099 StateAsCString(state));
1100 return state;
1101}
1102
1103Event *
1104Process::PeekAtStateChangedEvents ()
1105{
Greg Clayton5160ce52013-03-27 23:08:40 +00001106 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107
1108 if (log)
1109 log->Printf ("Process::%s...", __FUNCTION__);
1110
1111 Event *event_ptr;
Greg Clayton3fcbed62010-10-19 03:25:40 +00001112 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
1113 eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 if (log)
1115 {
1116 if (event_ptr)
1117 {
1118 log->Printf ("Process::%s (event_ptr) => %s",
1119 __FUNCTION__,
1120 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
1121 }
1122 else
1123 {
1124 log->Printf ("Process::%s no events found",
1125 __FUNCTION__);
1126 }
1127 }
1128 return event_ptr;
1129}
1130
1131StateType
1132Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
1133{
Greg Clayton5160ce52013-03-27 23:08:40 +00001134 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135
1136 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001137 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1138 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139
1140 StateType state = eStateInvalid;
Greg Clayton6779606a2011-01-22 23:43:18 +00001141 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
1142 &m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +00001143 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
Greg Clayton6779606a2011-01-22 23:43:18 +00001144 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001145 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1146 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147
1148 // This is a bit of a hack, but when we wait here we could very well return
1149 // to the command-line, and that could disable the log, which would render the
1150 // log we got above invalid.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001152 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
1153 __FUNCTION__, static_cast<const void *>(timeout),
1154 state == eStateInvalid ? "TIMEOUT" : StateAsCString(state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 return state;
1156}
1157
1158bool
1159Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
1160{
Greg Clayton5160ce52013-03-27 23:08:40 +00001161 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162
1163 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001164 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1165 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166
1167 if (control_only)
1168 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
1169 else
1170 return m_private_state_listener.WaitForEvent(timeout, event_sp);
1171}
1172
1173bool
1174Process::IsRunning () const
1175{
1176 return StateIsRunningState (m_public_state.GetValue());
1177}
1178
1179int
1180Process::GetExitStatus ()
1181{
1182 if (m_public_state.GetValue() == eStateExited)
1183 return m_exit_status;
1184 return -1;
1185}
1186
Greg Clayton85851dd2010-12-04 00:10:17 +00001187
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188const char *
1189Process::GetExitDescription ()
1190{
1191 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
1192 return m_exit_string.c_str();
1193 return NULL;
1194}
1195
Greg Clayton6779606a2011-01-22 23:43:18 +00001196bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197Process::SetExitStatus (int status, const char *cstr)
1198{
Greg Clayton5160ce52013-03-27 23:08:40 +00001199 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Greg Clayton414f5d32011-01-25 02:58:48 +00001200 if (log)
1201 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
1202 status, status,
1203 cstr ? "\"" : "",
1204 cstr ? cstr : "NULL",
1205 cstr ? "\"" : "");
1206
Greg Clayton6779606a2011-01-22 23:43:18 +00001207 // We were already in the exited state
1208 if (m_private_state.GetValue() == eStateExited)
Greg Clayton414f5d32011-01-25 02:58:48 +00001209 {
Greg Clayton385d6032011-01-26 23:47:29 +00001210 if (log)
1211 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton6779606a2011-01-22 23:43:18 +00001212 return false;
Greg Clayton414f5d32011-01-25 02:58:48 +00001213 }
Greg Clayton6779606a2011-01-22 23:43:18 +00001214
1215 m_exit_status = status;
1216 if (cstr)
1217 m_exit_string = cstr;
1218 else
1219 m_exit_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220
Greg Clayton6779606a2011-01-22 23:43:18 +00001221 DidExit ();
Greg Clayton10177aa2010-12-08 05:08:21 +00001222
Greg Clayton6779606a2011-01-22 23:43:18 +00001223 SetPrivateState (eStateExited);
1224 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225}
1226
1227// This static callback can be used to watch for local child processes on
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001228// the current host. The child process exits, the process will be
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229// found in the global target list (we want to be completely sure that the
1230// lldb_private::Process doesn't go away before we can deliver the signal.
1231bool
Greg Claytone4e45922011-11-16 05:37:56 +00001232Process::SetProcessExitStatus (void *callback_baton,
1233 lldb::pid_t pid,
1234 bool exited,
1235 int signo, // Zero for no signal
1236 int exit_status // Exit value of process if signal is zero
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237)
1238{
Greg Clayton5160ce52013-03-27 23:08:40 +00001239 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytone4e45922011-11-16 05:37:56 +00001240 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001241 log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n",
Greg Claytone4e45922011-11-16 05:37:56 +00001242 callback_baton,
1243 pid,
1244 exited,
1245 signo,
1246 exit_status);
1247
1248 if (exited)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 {
Greg Clayton66111032010-06-23 01:19:29 +00001250 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 if (target_sp)
1252 {
1253 ProcessSP process_sp (target_sp->GetProcessSP());
1254 if (process_sp)
1255 {
1256 const char *signal_cstr = NULL;
1257 if (signo)
1258 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
1259
1260 process_sp->SetExitStatus (exit_status, signal_cstr);
1261 }
1262 }
1263 return true;
1264 }
1265 return false;
1266}
1267
1268
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001269void
1270Process::UpdateThreadListIfNeeded ()
1271{
1272 const uint32_t stop_id = GetStopID();
1273 if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID())
1274 {
Greg Clayton2637f822011-11-17 01:23:07 +00001275 const StateType state = GetPrivateState();
1276 if (StateIsStoppedState (state, true))
1277 {
1278 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytone24c4ac2011-11-17 04:46:02 +00001279 // m_thread_list does have its own mutex, but we need to
1280 // hold onto the mutex between the call to UpdateThreadList(...)
1281 // and the os->UpdateThreadList(...) so it doesn't change on us
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001282 ThreadList &old_thread_list = m_thread_list;
1283 ThreadList real_thread_list(this);
Greg Clayton2637f822011-11-17 01:23:07 +00001284 ThreadList new_thread_list(this);
1285 // Always update the thread list with the protocol specific
Greg Clayton9fc13552012-04-10 00:18:59 +00001286 // thread list, but only update if "true" is returned
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001287 if (UpdateThreadList (m_thread_list_real, real_thread_list))
Greg Clayton9fc13552012-04-10 00:18:59 +00001288 {
Jim Ingham09437922013-03-01 20:04:25 +00001289 // Don't call into the OperatingSystem to update the thread list if we are shutting down, since
1290 // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is
1291 // shutting us down, causing a deadlock.
1292 if (!m_destroy_in_process)
1293 {
1294 OperatingSystem *os = GetOperatingSystem ();
1295 if (os)
Greg Claytonb3ae8762013-04-12 20:07:46 +00001296 {
1297 // Clear any old backing threads where memory threads might have been
1298 // backed by actual threads from the lldb_private::Process subclass
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001299 size_t num_old_threads = old_thread_list.GetSize(false);
Greg Claytonb3ae8762013-04-12 20:07:46 +00001300 for (size_t i=0; i<num_old_threads; ++i)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001301 old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread();
Greg Claytonb3ae8762013-04-12 20:07:46 +00001302
1303 // Now let the OperatingSystem plug-in update the thread list
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001304 os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in
1305 real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass
1306 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 +00001307 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001308 else
1309 {
1310 // No OS plug-in, the new thread list is the same as the real thread list
1311 new_thread_list = real_thread_list;
1312 }
Jim Ingham09437922013-03-01 20:04:25 +00001313 }
Jim Ingham02ff8e02013-06-22 00:55:02 +00001314
1315 m_thread_list_real.Update(real_thread_list);
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001316 m_thread_list.Update (new_thread_list);
1317 m_thread_list.SetStopID (stop_id);
Jason Molendaa6e91302013-11-19 05:44:41 +00001318
Jason Molenda4ff13262013-11-20 00:31:38 +00001319 if (GetLastNaturalStopID () != m_extended_thread_stop_id)
1320 {
1321 // Clear any extended threads that we may have accumulated previously
1322 m_extended_thread_list.Clear();
1323 m_extended_thread_stop_id = GetLastNaturalStopID ();
Jason Molenda5e8dce42013-12-13 00:29:16 +00001324
1325 m_queue_list.Clear();
1326 m_queue_list_stop_id = GetLastNaturalStopID ();
Jason Molenda4ff13262013-11-20 00:31:38 +00001327 }
Greg Clayton9fc13552012-04-10 00:18:59 +00001328 }
Greg Clayton2637f822011-11-17 01:23:07 +00001329 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001330 }
1331}
1332
Jason Molenda5e8dce42013-12-13 00:29:16 +00001333void
1334Process::UpdateQueueListIfNeeded ()
1335{
1336 if (m_system_runtime_ap.get())
1337 {
1338 if (m_queue_list.GetSize() == 0 || m_queue_list_stop_id != GetLastNaturalStopID())
1339 {
1340 const StateType state = GetPrivateState();
1341 if (StateIsStoppedState (state, true))
1342 {
1343 m_system_runtime_ap->PopulateQueueList (m_queue_list);
1344 m_queue_list_stop_id = GetLastNaturalStopID();
1345 }
1346 }
1347 }
1348}
1349
Greg Claytona4d87472013-01-18 23:41:08 +00001350ThreadSP
1351Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context)
1352{
1353 OperatingSystem *os = GetOperatingSystem ();
1354 if (os)
1355 return os->CreateThread(tid, context);
1356 return ThreadSP();
1357}
1358
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001359uint32_t
1360Process::GetNextThreadIndexID (uint64_t thread_id)
1361{
1362 return AssignIndexIDToThread(thread_id);
1363}
1364
1365bool
1366Process::HasAssignedIndexIDToThread(uint64_t thread_id)
1367{
1368 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1369 if (iterator == m_thread_id_to_index_id_map.end())
1370 {
1371 return false;
1372 }
1373 else
1374 {
1375 return true;
1376 }
1377}
1378
1379uint32_t
1380Process::AssignIndexIDToThread(uint64_t thread_id)
1381{
1382 uint32_t result = 0;
1383 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1384 if (iterator == m_thread_id_to_index_id_map.end())
1385 {
1386 result = ++m_thread_index_id;
1387 m_thread_id_to_index_id_map[thread_id] = result;
1388 }
1389 else
1390 {
1391 result = iterator->second;
1392 }
1393
1394 return result;
1395}
1396
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397StateType
1398Process::GetState()
1399{
1400 // If any other threads access this we will need a mutex for it
1401 return m_public_state.GetValue ();
1402}
1403
1404void
Jim Ingham221d51c2013-05-08 00:35:16 +00001405Process::SetPublicState (StateType new_state, bool restarted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406{
Greg Clayton5160ce52013-03-27 23:08:40 +00001407 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +00001409 log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001410 const StateType old_state = m_public_state.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 m_public_state.SetValue (new_state);
Jim Ingham3b8285d2012-04-19 01:40:33 +00001412
1413 // On the transition from Run to Stopped, we unlock the writer end of the
1414 // run lock. The lock gets locked in Resume, which is the public API
1415 // to tell the program to run.
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001416 if (!IsHijackedForEvent(eBroadcastBitStateChanged))
1417 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001418 if (new_state == eStateDetached)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001419 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001420 if (log)
1421 log->Printf("Process::SetPublicState (%s) -- unlocking run lock for detach", StateAsCString(new_state));
Ed Maste64fad602013-07-29 20:58:06 +00001422 m_public_run_lock.SetStopped();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001423 }
1424 else
1425 {
1426 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1427 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
Jim Ingham221d51c2013-05-08 00:35:16 +00001428 if ((old_state_is_stopped != new_state_is_stopped))
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001429 {
Jim Ingham221d51c2013-05-08 00:35:16 +00001430 if (new_state_is_stopped && !restarted)
Sean Callanan8b0737f2012-06-02 01:16:20 +00001431 {
1432 if (log)
1433 log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state));
Ed Maste64fad602013-07-29 20:58:06 +00001434 m_public_run_lock.SetStopped();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001435 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001436 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001437 }
1438 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439}
1440
Jim Ingham3b8285d2012-04-19 01:40:33 +00001441Error
1442Process::Resume ()
1443{
Greg Clayton5160ce52013-03-27 23:08:40 +00001444 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Jim Ingham3b8285d2012-04-19 01:40:33 +00001445 if (log)
1446 log->Printf("Process::Resume -- locking run lock");
Ed Maste64fad602013-07-29 20:58:06 +00001447 if (!m_public_run_lock.TrySetRunning())
Jim Ingham3b8285d2012-04-19 01:40:33 +00001448 {
1449 Error error("Resume request failed - process still running.");
1450 if (log)
Ed Maste64fad602013-07-29 20:58:06 +00001451 log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming.");
Jim Ingham3b8285d2012-04-19 01:40:33 +00001452 return error;
1453 }
1454 return PrivateResume();
1455}
1456
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457StateType
1458Process::GetPrivateState ()
1459{
1460 return m_private_state.GetValue();
1461}
1462
1463void
1464Process::SetPrivateState (StateType new_state)
1465{
Greg Claytonfb8b37a2014-07-14 23:09:29 +00001466 if (m_finalize_called)
1467 return;
1468
Greg Clayton5160ce52013-03-27 23:08:40 +00001469 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470 bool state_changed = false;
1471
1472 if (log)
1473 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
1474
Andrew Kaylor29d65742013-05-10 17:19:04 +00001475 Mutex::Locker thread_locker(m_thread_list.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 Mutex::Locker locker(m_private_state.GetMutex());
1477
1478 const StateType old_state = m_private_state.GetValueNoLock ();
1479 state_changed = old_state != new_state;
Ed Mastec29693f2013-07-02 16:35:47 +00001480
Greg Claytonaa49c832013-05-03 22:25:56 +00001481 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1482 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
1483 if (old_state_is_stopped != new_state_is_stopped)
1484 {
1485 if (new_state_is_stopped)
Ed Maste64fad602013-07-29 20:58:06 +00001486 m_private_run_lock.SetStopped();
Greg Claytonaa49c832013-05-03 22:25:56 +00001487 else
Ed Maste64fad602013-07-29 20:58:06 +00001488 m_private_run_lock.SetRunning();
Greg Claytonaa49c832013-05-03 22:25:56 +00001489 }
Andrew Kaylor93132f52013-05-28 23:04:25 +00001490
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491 if (state_changed)
1492 {
1493 m_private_state.SetValueNoLock (new_state);
Greg Clayton2637f822011-11-17 01:23:07 +00001494 if (StateIsStoppedState(new_state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495 {
Andrew Kaylor29d65742013-05-10 17:19:04 +00001496 // Note, this currently assumes that all threads in the list
1497 // stop when the process stops. In the future we will want to
1498 // support a debugging model where some threads continue to run
1499 // while others are stopped. When that happens we will either need
1500 // a way for the thread list to identify which threads are stopping
1501 // or create a special thread list containing only threads which
1502 // actually stopped.
1503 //
1504 // The process plugin is responsible for managing the actual
1505 // behavior of the threads and should have stopped any threads
1506 // that are going to stop before we get here.
1507 m_thread_list.DidStop();
1508
Jim Ingham4b536182011-08-09 02:12:22 +00001509 m_mod_id.BumpStopID();
Greg Clayton58be07b2011-01-07 06:08:19 +00001510 m_memory_cache.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511 if (log)
Jim Ingham4b536182011-08-09 02:12:22 +00001512 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513 }
1514 // Use our target to get a shared pointer to ourselves...
Greg Clayton35a4cc52012-10-29 20:52:08 +00001515 if (m_finalize_called && PrivateStateThreadIsValid() == false)
1516 BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
1517 else
1518 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001519 }
1520 else
1521 {
1522 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001523 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524 }
1525}
1526
Jim Ingham0faa43f2011-11-08 03:00:11 +00001527void
1528Process::SetRunningUserExpression (bool on)
1529{
1530 m_mod_id.SetRunningUserExpression (on);
1531}
1532
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001533addr_t
1534Process::GetImageInfoAddress()
1535{
1536 return LLDB_INVALID_ADDRESS;
1537}
1538
Greg Clayton8f343b02010-11-04 01:54:29 +00001539//----------------------------------------------------------------------
1540// LoadImage
1541//
1542// This function provides a default implementation that works for most
1543// unix variants. Any Process subclasses that need to do shared library
1544// loading differently should override LoadImage and UnloadImage and
1545// do what is needed.
1546//----------------------------------------------------------------------
1547uint32_t
1548Process::LoadImage (const FileSpec &image_spec, Error &error)
1549{
Greg Claytonac7a3db2012-04-18 00:05:19 +00001550 char path[PATH_MAX];
1551 image_spec.GetPath(path, sizeof(path));
1552
Greg Clayton8f343b02010-11-04 01:54:29 +00001553 DynamicLoader *loader = GetDynamicLoader();
1554 if (loader)
1555 {
1556 error = loader->CanLoadImage();
1557 if (error.Fail())
1558 return LLDB_INVALID_IMAGE_TOKEN;
1559 }
1560
1561 if (error.Success())
1562 {
1563 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001564
1565 if (thread_sp)
1566 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001567 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
Greg Clayton8f343b02010-11-04 01:54:29 +00001568
1569 if (frame_sp)
1570 {
1571 ExecutionContext exe_ctx;
1572 frame_sp->CalculateExecutionContext (exe_ctx);
Greg Clayton62afb9f2013-11-04 19:35:17 +00001573 EvaluateExpressionOptions expr_options;
1574 expr_options.SetUnwindOnError(true);
1575 expr_options.SetIgnoreBreakpoints(true);
1576 expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
Jim Ingham4ac04432014-07-19 01:09:16 +00001577 expr_options.SetResultIsInternal(true);
1578
Greg Clayton8f343b02010-11-04 01:54:29 +00001579 StreamString expr;
Jim Ingham6971b862014-07-19 00:37:06 +00001580 expr.Printf(R"(
1581 struct __lldb_dlopen_result { void *image_ptr; const char *error_str; } the_result;
1582 the_result.image_ptr = dlopen ("%s", 2);
1583 if (the_result.image_ptr == (void *) 0x0)
1584 {
1585 the_result.error_str = dlerror();
1586 }
1587 else
1588 {
1589 the_result.error_str = (const char *) 0x0;
1590 }
1591 the_result;
1592 )",
1593 path);
1594 const char *prefix = R"(
1595 extern "C" void* dlopen (const char *path, int mode);
1596 extern "C" const char *dlerror (void);
1597 )";
Jim Inghamf48169b2010-11-30 02:22:11 +00001598 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +00001599 Error expr_error;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001600 ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001601 expr_options,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001602 expr.GetData(),
1603 prefix,
1604 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001605 expr_error);
1606 if (expr_error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001607 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001608 error = result_valobj_sp->GetError();
1609 if (error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001610 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001611 Scalar scalar;
Jim Ingham6971b862014-07-19 00:37:06 +00001612 ValueObjectSP image_ptr_sp = result_valobj_sp->GetChildAtIndex(0, true);
1613 if (image_ptr_sp && image_ptr_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001614 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001615 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1616 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
1617 {
1618 uint32_t image_token = m_image_tokens.size();
1619 m_image_tokens.push_back (image_ptr);
1620 return image_token;
1621 }
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001622 else if (image_ptr == 0)
1623 {
Jim Ingham6971b862014-07-19 00:37:06 +00001624 ValueObjectSP error_str_sp = result_valobj_sp->GetChildAtIndex(1, true);
1625 if (error_str_sp)
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001626 {
Jim Ingham6971b862014-07-19 00:37:06 +00001627 if (error_str_sp->IsCStringContainer(true))
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001628 {
Jim Inghamcf973792014-07-17 21:53:48 +00001629 StreamString s;
Jim Ingham6971b862014-07-19 00:37:06 +00001630 size_t num_chars = error_str_sp->ReadPointedString (s, error);
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001631 if (error.Success() && num_chars > 0)
1632 {
1633 error.Clear();
Jim Ingham6971b862014-07-19 00:37:06 +00001634 error.SetErrorStringWithFormat("dlopen error: %s", s.GetData());
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001635 }
1636 }
1637 }
1638 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001639 }
1640 }
1641 }
Jim Ingham6c9ed912014-04-03 01:26:14 +00001642 else
1643 error = expr_error;
Greg Clayton8f343b02010-11-04 01:54:29 +00001644 }
1645 }
1646 }
Greg Claytonac7a3db2012-04-18 00:05:19 +00001647 if (!error.AsCString())
1648 error.SetErrorStringWithFormat("unable to load '%s'", path);
Greg Clayton8f343b02010-11-04 01:54:29 +00001649 return LLDB_INVALID_IMAGE_TOKEN;
1650}
1651
1652//----------------------------------------------------------------------
1653// UnloadImage
1654//
1655// This function provides a default implementation that works for most
1656// unix variants. Any Process subclasses that need to do shared library
1657// loading differently should override LoadImage and UnloadImage and
1658// do what is needed.
1659//----------------------------------------------------------------------
1660Error
1661Process::UnloadImage (uint32_t image_token)
1662{
1663 Error error;
1664 if (image_token < m_image_tokens.size())
1665 {
1666 const addr_t image_addr = m_image_tokens[image_token];
1667 if (image_addr == LLDB_INVALID_ADDRESS)
1668 {
1669 error.SetErrorString("image already unloaded");
1670 }
1671 else
1672 {
1673 DynamicLoader *loader = GetDynamicLoader();
1674 if (loader)
1675 error = loader->CanLoadImage();
1676
1677 if (error.Success())
1678 {
1679 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001680
1681 if (thread_sp)
1682 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001683 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
Greg Clayton8f343b02010-11-04 01:54:29 +00001684
1685 if (frame_sp)
1686 {
1687 ExecutionContext exe_ctx;
1688 frame_sp->CalculateExecutionContext (exe_ctx);
Greg Clayton62afb9f2013-11-04 19:35:17 +00001689 EvaluateExpressionOptions expr_options;
1690 expr_options.SetUnwindOnError(true);
1691 expr_options.SetIgnoreBreakpoints(true);
1692 expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
Greg Clayton8f343b02010-11-04 01:54:29 +00001693 StreamString expr;
Daniel Malead01b2952012-11-29 21:49:15 +00001694 expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr);
Greg Clayton8f343b02010-11-04 01:54:29 +00001695 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001696 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +00001697 Error expr_error;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001698 ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001699 expr_options,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001700 expr.GetData(),
1701 prefix,
1702 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001703 expr_error);
Greg Clayton8f343b02010-11-04 01:54:29 +00001704 if (result_valobj_sp->GetError().Success())
1705 {
1706 Scalar scalar;
Jim Ingham6035b672011-03-31 00:19:25 +00001707 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001708 {
1709 if (scalar.UInt(1))
1710 {
1711 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1712 }
1713 else
1714 {
1715 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1716 }
1717 }
1718 }
1719 else
1720 {
1721 error = result_valobj_sp->GetError();
1722 }
1723 }
1724 }
1725 }
1726 }
1727 }
1728 else
1729 {
1730 error.SetErrorString("invalid image token");
1731 }
1732 return error;
1733}
1734
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001735const lldb::ABISP &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736Process::GetABI()
1737{
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001738 if (!m_abi_sp)
1739 m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture());
1740 return m_abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741}
1742
Jim Ingham22777012010-09-23 02:01:19 +00001743LanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001744Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001745{
1746 LanguageRuntimeCollection::iterator pos;
1747 pos = m_language_runtimes.find (language);
Jim Inghamab175242012-03-10 00:22:19 +00001748 if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second))
Jim Ingham22777012010-09-23 02:01:19 +00001749 {
Jim Inghamab175242012-03-10 00:22:19 +00001750 lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language));
Jim Ingham22777012010-09-23 02:01:19 +00001751
Jim Inghamab175242012-03-10 00:22:19 +00001752 m_language_runtimes[language] = runtime_sp;
1753 return runtime_sp.get();
Jim Ingham22777012010-09-23 02:01:19 +00001754 }
1755 else
1756 return (*pos).second.get();
1757}
1758
1759CPPLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001760Process::GetCPPLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001761{
Jim Inghamab175242012-03-10 00:22:19 +00001762 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00001763 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
1764 return static_cast<CPPLanguageRuntime *> (runtime);
1765 return NULL;
1766}
1767
1768ObjCLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001769Process::GetObjCLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001770{
Jim Inghamab175242012-03-10 00:22:19 +00001771 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00001772 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
1773 return static_cast<ObjCLanguageRuntime *> (runtime);
1774 return NULL;
1775}
1776
Enrico Granatafd4c84e2012-05-21 16:51:35 +00001777bool
1778Process::IsPossibleDynamicValue (ValueObject& in_value)
1779{
1780 if (in_value.IsDynamic())
1781 return false;
1782 LanguageType known_type = in_value.GetObjectRuntimeLanguage();
1783
1784 if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
1785 {
1786 LanguageRuntime *runtime = GetLanguageRuntime (known_type);
1787 return runtime ? runtime->CouldHaveDynamicValue(in_value) : false;
1788 }
1789
1790 LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus);
1791 if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value))
1792 return true;
1793
1794 LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC);
1795 return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false;
1796}
1797
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001798BreakpointSiteList &
1799Process::GetBreakpointSiteList()
1800{
1801 return m_breakpoint_site_list;
1802}
1803
1804const BreakpointSiteList &
1805Process::GetBreakpointSiteList() const
1806{
1807 return m_breakpoint_site_list;
1808}
1809
1810
1811void
1812Process::DisableAllBreakpointSites ()
1813{
Greg Claytond8cf1a12013-06-12 00:46:38 +00001814 m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void {
1815// bp_site->SetEnabled(true);
1816 DisableBreakpointSite(bp_site);
1817 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001818}
1819
1820Error
1821Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
1822{
1823 Error error (DisableBreakpointSiteByID (break_id));
1824
1825 if (error.Success())
1826 m_breakpoint_site_list.Remove(break_id);
1827
1828 return error;
1829}
1830
1831Error
1832Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
1833{
1834 Error error;
1835 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1836 if (bp_site_sp)
1837 {
1838 if (bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00001839 error = DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001840 }
1841 else
1842 {
Daniel Malead01b2952012-11-29 21:49:15 +00001843 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844 }
1845
1846 return error;
1847}
1848
1849Error
1850Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
1851{
1852 Error error;
1853 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1854 if (bp_site_sp)
1855 {
1856 if (!bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00001857 error = EnableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001858 }
1859 else
1860 {
Daniel Malead01b2952012-11-29 21:49:15 +00001861 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862 }
1863 return error;
1864}
1865
Stephen Wilson50bd94f2010-07-17 00:56:13 +00001866lldb::break_id_t
Greg Claytone1cd1be2012-01-29 20:56:30 +00001867Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001868{
Jim Ingham1460e4b2014-01-10 23:46:59 +00001869 addr_t load_addr = LLDB_INVALID_ADDRESS;
1870
1871 bool show_error = true;
1872 switch (GetState())
1873 {
1874 case eStateInvalid:
1875 case eStateUnloaded:
1876 case eStateConnected:
1877 case eStateAttaching:
1878 case eStateLaunching:
1879 case eStateDetached:
1880 case eStateExited:
1881 show_error = false;
1882 break;
1883
1884 case eStateStopped:
1885 case eStateRunning:
1886 case eStateStepping:
1887 case eStateCrashed:
1888 case eStateSuspended:
1889 show_error = IsAlive();
1890 break;
1891 }
1892
1893 // Reset the IsIndirect flag here, in case the location changes from
1894 // pointing to a indirect symbol to a regular symbol.
1895 owner->SetIsIndirect (false);
1896
1897 if (owner->ShouldResolveIndirectFunctions())
1898 {
1899 Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol();
1900 if (symbol && symbol->IsIndirect())
1901 {
1902 Error error;
1903 load_addr = ResolveIndirectFunction (&symbol->GetAddress(), error);
1904 if (!error.Success() && show_error)
1905 {
Greg Clayton44d93782014-01-27 23:43:24 +00001906 m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to resolve indirect function at 0x%" PRIx64 " for breakpoint %i.%i: %s\n",
1907 symbol->GetAddress().GetLoadAddress(&m_target),
1908 owner->GetBreakpoint().GetID(),
1909 owner->GetID(),
Sylvestre Ledruf6102892014-08-11 18:06:28 +00001910 error.AsCString() ? error.AsCString() : "unknown error");
Jim Ingham1460e4b2014-01-10 23:46:59 +00001911 return LLDB_INVALID_BREAK_ID;
1912 }
1913 Address resolved_address(load_addr);
1914 load_addr = resolved_address.GetOpcodeLoadAddress (&m_target);
1915 owner->SetIsIndirect(true);
1916 }
1917 else
1918 load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
1919 }
1920 else
1921 load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
1922
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001923 if (load_addr != LLDB_INVALID_ADDRESS)
1924 {
1925 BreakpointSiteSP bp_site_sp;
1926
1927 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
1928 // create a new breakpoint site and add it.
1929
1930 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
1931
1932 if (bp_site_sp)
1933 {
1934 bp_site_sp->AddOwner (owner);
1935 owner->SetBreakpointSite (bp_site_sp);
1936 return bp_site_sp->GetID();
1937 }
1938 else
1939 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001940 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, use_hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001941 if (bp_site_sp)
1942 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001943 Error error = EnableBreakpointSite (bp_site_sp.get());
1944 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001945 {
1946 owner->SetBreakpointSite (bp_site_sp);
1947 return m_breakpoint_site_list.Add (bp_site_sp);
1948 }
Greg Claytoneb023e72013-10-11 19:48:25 +00001949 else
1950 {
Greg Claytonfbb76342013-11-20 21:07:01 +00001951 if (show_error)
1952 {
1953 // Report error for setting breakpoint...
Greg Clayton44d93782014-01-27 23:43:24 +00001954 m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n",
1955 load_addr,
1956 owner->GetBreakpoint().GetID(),
1957 owner->GetID(),
Sylvestre Ledruf6102892014-08-11 18:06:28 +00001958 error.AsCString() ? error.AsCString() : "unknown error");
Greg Claytonfbb76342013-11-20 21:07:01 +00001959 }
Greg Claytoneb023e72013-10-11 19:48:25 +00001960 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001961 }
1962 }
1963 }
1964 // We failed to enable the breakpoint
1965 return LLDB_INVALID_BREAK_ID;
1966
1967}
1968
1969void
1970Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
1971{
1972 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
1973 if (num_owners == 0)
1974 {
Jim Inghamf1ff3bb2013-04-06 00:16:39 +00001975 // Don't try to disable the site if we don't have a live process anymore.
1976 if (IsAlive())
1977 DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001978 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1979 }
1980}
1981
1982
1983size_t
1984Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1985{
1986 size_t bytes_removed = 0;
Jim Ingham20c77192011-06-29 19:42:28 +00001987 BreakpointSiteList bp_sites_in_range;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988
Jim Ingham20c77192011-06-29 19:42:28 +00001989 if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00001991 bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void {
1992 if (bp_site->GetType() == BreakpointSite::eSoftware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00001994 addr_t intersect_addr;
1995 size_t intersect_size;
1996 size_t opcode_offset;
1997 if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
Jim Ingham20c77192011-06-29 19:42:28 +00001998 {
1999 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
2000 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
Greg Claytond8cf1a12013-06-12 00:46:38 +00002001 assert(opcode_offset + intersect_size <= bp_site->GetByteSize());
Jim Ingham20c77192011-06-29 19:42:28 +00002002 size_t buf_offset = intersect_addr - bp_addr;
Greg Claytond8cf1a12013-06-12 00:46:38 +00002003 ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
Jim Ingham20c77192011-06-29 19:42:28 +00002004 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002005 }
Greg Claytond8cf1a12013-06-12 00:46:38 +00002006 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002007 }
2008 return bytes_removed;
2009}
2010
2011
Greg Claytonded470d2011-03-19 01:12:21 +00002012
2013size_t
2014Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
2015{
2016 PlatformSP platform_sp (m_target.GetPlatform());
2017 if (platform_sp)
2018 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
2019 return 0;
2020}
2021
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002022Error
2023Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
2024{
2025 Error error;
2026 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002027 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028 const addr_t bp_addr = bp_site->GetLoadAddress();
2029 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002030 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, bp_site->GetID(), (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002031 if (bp_site->IsEnabled())
2032 {
2033 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002034 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 +00002035 return error;
2036 }
2037
2038 if (bp_addr == LLDB_INVALID_ADDRESS)
2039 {
2040 error.SetErrorString("BreakpointSite contains an invalid load address.");
2041 return error;
2042 }
2043 // Ask the lldb::Process subclass to fill in the correct software breakpoint
2044 // trap for the breakpoint site
2045 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
2046
2047 if (bp_opcode_size == 0)
2048 {
Daniel Malead01b2952012-11-29 21:49:15 +00002049 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%" PRIx64, bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002050 }
2051 else
2052 {
2053 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
2054
2055 if (bp_opcode_bytes == NULL)
2056 {
2057 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
2058 return error;
2059 }
2060
2061 // Save the original opcode by reading it
2062 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
2063 {
2064 // Write a software breakpoint in place of the original opcode
2065 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2066 {
2067 uint8_t verify_bp_opcode_bytes[64];
2068 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2069 {
2070 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
2071 {
2072 bp_site->SetEnabled(true);
2073 bp_site->SetType (BreakpointSite::eSoftware);
2074 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002075 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002076 bp_site->GetID(),
2077 (uint64_t)bp_addr);
2078 }
2079 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002080 error.SetErrorString("failed to verify the breakpoint trap in memory.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002081 }
2082 else
2083 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
2084 }
2085 else
2086 error.SetErrorString("Unable to write breakpoint trap to memory.");
2087 }
2088 else
2089 error.SetErrorString("Unable to read memory at breakpoint address.");
2090 }
Stephen Wilson78a4feb2011-01-12 04:20:03 +00002091 if (log && error.Fail())
Daniel Malead01b2952012-11-29 21:49:15 +00002092 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002093 bp_site->GetID(),
2094 (uint64_t)bp_addr,
2095 error.AsCString());
2096 return error;
2097}
2098
2099Error
2100Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
2101{
2102 Error error;
2103 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002104 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002105 addr_t bp_addr = bp_site->GetLoadAddress();
2106 lldb::user_id_t breakID = bp_site->GetID();
2107 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002108 log->Printf ("Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 ") addr = 0x%" PRIx64, breakID, (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002109
2110 if (bp_site->IsHardware())
2111 {
2112 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
2113 }
2114 else if (bp_site->IsEnabled())
2115 {
2116 const size_t break_op_size = bp_site->GetByteSize();
2117 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
2118 if (break_op_size > 0)
2119 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002120 // Clear a software breakpoint instruction
Greg Claytonc982c762010-07-09 20:39:50 +00002121 uint8_t curr_break_op[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002122 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002123 bool break_op_found = false;
2124
2125 // Read the breakpoint opcode
2126 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
2127 {
2128 bool verify = false;
2129 // Make sure we have the a breakpoint opcode exists at this address
2130 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
2131 {
2132 break_op_found = true;
2133 // We found a valid breakpoint opcode at this address, now restore
2134 // the saved opcode.
2135 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
2136 {
2137 verify = true;
2138 }
2139 else
2140 error.SetErrorString("Memory write failed when restoring original opcode.");
2141 }
2142 else
2143 {
2144 error.SetErrorString("Original breakpoint trap is no longer in memory.");
2145 // Set verify to true and so we can check if the original opcode has already been restored
2146 verify = true;
2147 }
2148
2149 if (verify)
2150 {
Greg Claytonc982c762010-07-09 20:39:50 +00002151 uint8_t verify_opcode[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002152 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153 // Verify that our original opcode made it back to the inferior
2154 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
2155 {
2156 // compare the memory we just read with the original opcode
2157 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
2158 {
2159 // SUCCESS
2160 bp_site->SetEnabled(false);
2161 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002162 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 +00002163 return error;
2164 }
2165 else
2166 {
2167 if (break_op_found)
2168 error.SetErrorString("Failed to restore original opcode.");
2169 }
2170 }
2171 else
2172 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
2173 }
2174 }
2175 else
2176 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
2177 }
2178 }
2179 else
2180 {
2181 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002182 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 +00002183 return error;
2184 }
2185
2186 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002187 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002188 bp_site->GetID(),
2189 (uint64_t)bp_addr,
2190 error.AsCString());
2191 return error;
2192
2193}
2194
Greg Clayton58be07b2011-01-07 06:08:19 +00002195// Uncomment to verify memory caching works after making changes to caching code
2196//#define VERIFY_MEMORY_READS
2197
Sean Callanan64c0cf22012-06-07 22:26:42 +00002198size_t
2199Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2200{
Jason Molendaa7b5afa2013-11-15 00:17:32 +00002201 error.Clear();
Sean Callanan64c0cf22012-06-07 22:26:42 +00002202 if (!GetDisableMemoryCache())
2203 {
Greg Clayton58be07b2011-01-07 06:08:19 +00002204#if defined (VERIFY_MEMORY_READS)
Sean Callanan64c0cf22012-06-07 22:26:42 +00002205 // Memory caching is enabled, with debug verification
2206
2207 if (buf && size)
2208 {
2209 // Uncomment the line below to make sure memory caching is working.
2210 // I ran this through the test suite and got no assertions, so I am
2211 // pretty confident this is working well. If any changes are made to
2212 // memory caching, uncomment the line below and test your changes!
2213
2214 // Verify all memory reads by using the cache first, then redundantly
2215 // reading the same memory from the inferior and comparing to make sure
2216 // everything is exactly the same.
2217 std::string verify_buf (size, '\0');
2218 assert (verify_buf.size() == size);
2219 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
2220 Error verify_error;
2221 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
2222 assert (cache_bytes_read == verify_bytes_read);
2223 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
2224 assert (verify_error.Success() == error.Success());
2225 return cache_bytes_read;
2226 }
2227 return 0;
2228#else // !defined(VERIFY_MEMORY_READS)
2229 // Memory caching is enabled, without debug verification
2230
2231 return m_memory_cache.Read (addr, buf, size, error);
2232#endif // defined (VERIFY_MEMORY_READS)
Greg Clayton58be07b2011-01-07 06:08:19 +00002233 }
Sean Callanan64c0cf22012-06-07 22:26:42 +00002234 else
2235 {
2236 // Memory caching is disabled
2237
2238 return ReadMemoryFromInferior (addr, buf, size, error);
2239 }
Greg Clayton58be07b2011-01-07 06:08:19 +00002240}
Greg Clayton58be07b2011-01-07 06:08:19 +00002241
Greg Clayton4c82d422012-05-18 23:20:01 +00002242size_t
2243Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error)
2244{
Greg Claytonde87c0f2012-05-19 00:18:00 +00002245 char buf[256];
Greg Clayton4c82d422012-05-18 23:20:01 +00002246 out_str.clear();
2247 addr_t curr_addr = addr;
2248 while (1)
2249 {
2250 size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error);
2251 if (length == 0)
2252 break;
2253 out_str.append(buf, length);
2254 // If we got "length - 1" bytes, we didn't get the whole C string, we
2255 // need to read some more characters
2256 if (length == sizeof(buf) - 1)
2257 curr_addr += length;
2258 else
2259 break;
2260 }
2261 return out_str.size();
2262}
2263
Greg Clayton58be07b2011-01-07 06:08:19 +00002264
2265size_t
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +00002266Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error,
2267 size_t type_width)
2268{
2269 size_t total_bytes_read = 0;
2270 if (dst && max_bytes && type_width && max_bytes >= type_width)
2271 {
2272 // Ensure a null terminator independent of the number of bytes that is read.
2273 memset (dst, 0, max_bytes);
2274 size_t bytes_left = max_bytes - type_width;
2275
2276 const char terminator[4] = {'\0', '\0', '\0', '\0'};
2277 assert(sizeof(terminator) >= type_width &&
2278 "Attempting to validate a string with more than 4 bytes per character!");
2279
2280 addr_t curr_addr = addr;
2281 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2282 char *curr_dst = dst;
2283
2284 error.Clear();
2285 while (bytes_left > 0 && error.Success())
2286 {
2287 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2288 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2289 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2290
2291 if (bytes_read == 0)
2292 break;
2293
2294 // Search for a null terminator of correct size and alignment in bytes_read
2295 size_t aligned_start = total_bytes_read - total_bytes_read % type_width;
2296 for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width)
2297 if (::strncmp(&dst[i], terminator, type_width) == 0)
2298 {
2299 error.Clear();
2300 return i;
2301 }
2302
2303 total_bytes_read += bytes_read;
2304 curr_dst += bytes_read;
2305 curr_addr += bytes_read;
2306 bytes_left -= bytes_read;
2307 }
2308 }
2309 else
2310 {
2311 if (max_bytes)
2312 error.SetErrorString("invalid arguments");
2313 }
2314 return total_bytes_read;
2315}
2316
2317// Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find
2318// null terminators.
2319size_t
Greg Claytone91b7952011-12-15 03:14:23 +00002320Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error)
Greg Clayton8b82f082011-04-12 05:54:46 +00002321{
2322 size_t total_cstr_len = 0;
2323 if (dst && dst_max_len)
2324 {
Greg Claytone91b7952011-12-15 03:14:23 +00002325 result_error.Clear();
Greg Clayton8b82f082011-04-12 05:54:46 +00002326 // NULL out everything just to be safe
2327 memset (dst, 0, dst_max_len);
2328 Error error;
2329 addr_t curr_addr = addr;
2330 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2331 size_t bytes_left = dst_max_len - 1;
2332 char *curr_dst = dst;
2333
2334 while (bytes_left > 0)
2335 {
2336 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2337 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2338 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2339
2340 if (bytes_read == 0)
2341 {
Greg Claytone91b7952011-12-15 03:14:23 +00002342 result_error = error;
Greg Clayton8b82f082011-04-12 05:54:46 +00002343 dst[total_cstr_len] = '\0';
2344 break;
2345 }
2346 const size_t len = strlen(curr_dst);
2347
2348 total_cstr_len += len;
2349
2350 if (len < bytes_to_read)
2351 break;
2352
2353 curr_dst += bytes_read;
2354 curr_addr += bytes_read;
2355 bytes_left -= bytes_read;
2356 }
2357 }
Greg Claytone91b7952011-12-15 03:14:23 +00002358 else
2359 {
2360 if (dst == NULL)
2361 result_error.SetErrorString("invalid arguments");
2362 else
2363 result_error.Clear();
2364 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002365 return total_cstr_len;
2366}
2367
2368size_t
Greg Clayton58be07b2011-01-07 06:08:19 +00002369Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
2370{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002371 if (buf == NULL || size == 0)
2372 return 0;
2373
2374 size_t bytes_read = 0;
2375 uint8_t *bytes = (uint8_t *)buf;
2376
2377 while (bytes_read < size)
2378 {
2379 const size_t curr_size = size - bytes_read;
2380 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
2381 bytes + bytes_read,
2382 curr_size,
2383 error);
2384 bytes_read += curr_bytes_read;
2385 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
2386 break;
2387 }
2388
2389 // Replace any software breakpoint opcodes that fall into this range back
2390 // into "buf" before we return
2391 if (bytes_read > 0)
2392 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
2393 return bytes_read;
2394}
2395
Greg Clayton58a4c462010-12-16 20:01:20 +00002396uint64_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002397Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error)
Greg Clayton58a4c462010-12-16 20:01:20 +00002398{
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002399 Scalar scalar;
2400 if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error))
2401 return scalar.ULongLong(fail_value);
2402 return fail_value;
2403}
2404
2405addr_t
2406Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error)
2407{
2408 Scalar scalar;
2409 if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error))
2410 return scalar.ULongLong(LLDB_INVALID_ADDRESS);
2411 return LLDB_INVALID_ADDRESS;
2412}
2413
2414
2415bool
2416Process::WritePointerToMemory (lldb::addr_t vm_addr,
2417 lldb::addr_t ptr_value,
2418 Error &error)
2419{
2420 Scalar scalar;
2421 const uint32_t addr_byte_size = GetAddressByteSize();
2422 if (addr_byte_size <= 4)
2423 scalar = (uint32_t)ptr_value;
Greg Clayton58a4c462010-12-16 20:01:20 +00002424 else
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002425 scalar = ptr_value;
2426 return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size;
Greg Clayton58a4c462010-12-16 20:01:20 +00002427}
2428
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002429size_t
2430Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
2431{
2432 size_t bytes_written = 0;
2433 const uint8_t *bytes = (const uint8_t *)buf;
2434
2435 while (bytes_written < size)
2436 {
2437 const size_t curr_size = size - bytes_written;
2438 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
2439 bytes + bytes_written,
2440 curr_size,
2441 error);
2442 bytes_written += curr_bytes_written;
2443 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
2444 break;
2445 }
2446 return bytes_written;
2447}
2448
2449size_t
2450Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2451{
Greg Clayton58be07b2011-01-07 06:08:19 +00002452#if defined (ENABLE_MEMORY_CACHING)
2453 m_memory_cache.Flush (addr, size);
2454#endif
2455
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002456 if (buf == NULL || size == 0)
2457 return 0;
Jim Ingham78a685a2011-04-16 00:01:13 +00002458
Jim Ingham4b536182011-08-09 02:12:22 +00002459 m_mod_id.BumpMemoryID();
Jim Ingham78a685a2011-04-16 00:01:13 +00002460
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002461 // We need to write any data that would go where any current software traps
2462 // (enabled software breakpoints) any software traps (breakpoints) that we
2463 // may have placed in our tasks memory.
2464
Greg Claytond8cf1a12013-06-12 00:46:38 +00002465 BreakpointSiteList bp_sites_in_range;
2466
2467 if (m_breakpoint_site_list.FindInRange (addr, addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002468 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002469 // No breakpoint sites overlap
2470 if (bp_sites_in_range.IsEmpty())
2471 return WriteMemoryPrivate (addr, buf, size, error);
2472 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002473 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002474 const uint8_t *ubuf = (const uint8_t *)buf;
2475 uint64_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002476
Greg Claytond8cf1a12013-06-12 00:46:38 +00002477 bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void {
2478
2479 if (error.Success())
2480 {
2481 addr_t intersect_addr;
2482 size_t intersect_size;
2483 size_t opcode_offset;
2484 const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset);
2485 assert(intersects);
2486 assert(addr <= intersect_addr && intersect_addr < addr + size);
2487 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
2488 assert(opcode_offset + intersect_size <= bp->GetByteSize());
2489
2490 // Check for bytes before this breakpoint
2491 const addr_t curr_addr = addr + bytes_written;
2492 if (intersect_addr > curr_addr)
2493 {
2494 // There are some bytes before this breakpoint that we need to
2495 // just write to memory
2496 size_t curr_size = intersect_addr - curr_addr;
2497 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
2498 ubuf + bytes_written,
2499 curr_size,
2500 error);
2501 bytes_written += curr_bytes_written;
2502 if (curr_bytes_written != curr_size)
2503 {
2504 // We weren't able to write all of the requested bytes, we
2505 // are done looping and will return the number of bytes that
2506 // we have written so far.
2507 if (error.Success())
2508 error.SetErrorToGenericError();
2509 }
2510 }
2511 // Now write any bytes that would cover up any software breakpoints
2512 // directly into the breakpoint opcode buffer
2513 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
2514 bytes_written += intersect_size;
2515 }
2516 });
2517
2518 if (bytes_written < size)
2519 bytes_written += WriteMemoryPrivate (addr + bytes_written,
2520 ubuf + bytes_written,
2521 size - bytes_written,
2522 error);
2523 }
2524 }
2525 else
2526 {
2527 return WriteMemoryPrivate (addr, buf, size, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002528 }
2529
2530 // Write any remaining bytes after the last breakpoint if we have any left
Greg Claytond8cf1a12013-06-12 00:46:38 +00002531 return 0; //bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002532}
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002533
2534size_t
Greg Claytonc7bece562013-01-25 18:06:21 +00002535Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, size_t byte_size, Error &error)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002536{
2537 if (byte_size == UINT32_MAX)
2538 byte_size = scalar.GetByteSize();
2539 if (byte_size > 0)
2540 {
2541 uint8_t buf[32];
2542 const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error);
2543 if (mem_size > 0)
2544 return WriteMemory(addr, buf, mem_size, error);
2545 else
2546 error.SetErrorString ("failed to get scalar as memory data");
2547 }
2548 else
2549 {
2550 error.SetErrorString ("invalid scalar value");
2551 }
2552 return 0;
2553}
2554
2555size_t
2556Process::ReadScalarIntegerFromMemory (addr_t addr,
2557 uint32_t byte_size,
2558 bool is_signed,
2559 Scalar &scalar,
2560 Error &error)
2561{
Greg Clayton7060f892013-05-01 23:41:30 +00002562 uint64_t uval = 0;
2563 if (byte_size == 0)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002564 {
Greg Clayton7060f892013-05-01 23:41:30 +00002565 error.SetErrorString ("byte size is zero");
2566 }
2567 else if (byte_size & (byte_size - 1))
2568 {
2569 error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size);
2570 }
2571 else if (byte_size <= sizeof(uval))
2572 {
2573 const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002574 if (bytes_read == byte_size)
2575 {
2576 DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00002577 lldb::offset_t offset = 0;
Greg Clayton7060f892013-05-01 23:41:30 +00002578 if (byte_size <= 4)
2579 scalar = data.GetMaxU32 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002580 else
Greg Clayton7060f892013-05-01 23:41:30 +00002581 scalar = data.GetMaxU64 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002582 if (is_signed)
2583 scalar.SignExtend(byte_size * 8);
2584 return bytes_read;
2585 }
2586 }
2587 else
2588 {
2589 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
2590 }
2591 return 0;
2592}
2593
Greg Claytond495c532011-05-17 03:37:42 +00002594#define USE_ALLOCATE_MEMORY_CACHE 1
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002595addr_t
2596Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
2597{
Jim Inghamf72ce3a2011-06-20 17:32:44 +00002598 if (GetPrivateState() != eStateStopped)
2599 return LLDB_INVALID_ADDRESS;
2600
Greg Claytond495c532011-05-17 03:37:42 +00002601#if defined (USE_ALLOCATE_MEMORY_CACHE)
2602 return m_allocated_memory_cache.AllocateMemory(size, permissions, error);
2603#else
Greg Claytonb2daec92011-01-23 19:58:49 +00002604 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
Greg Clayton5160ce52013-03-27 23:08:40 +00002605 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002606 if (log)
Greg Clayton45989072013-10-23 18:24:30 +00002607 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 +00002608 (uint64_t)size,
Greg Claytond495c532011-05-17 03:37:42 +00002609 GetPermissionsAsCString (permissions),
Greg Claytonb2daec92011-01-23 19:58:49 +00002610 (uint64_t)allocated_addr,
Jim Ingham4b536182011-08-09 02:12:22 +00002611 m_mod_id.GetStopID(),
2612 m_mod_id.GetMemoryID());
Greg Claytonb2daec92011-01-23 19:58:49 +00002613 return allocated_addr;
Greg Claytond495c532011-05-17 03:37:42 +00002614#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002615}
2616
Sean Callanan90539452011-09-20 23:01:51 +00002617bool
2618Process::CanJIT ()
2619{
Sean Callanana7b443a2012-02-14 22:50:38 +00002620 if (m_can_jit == eCanJITDontKnow)
2621 {
Todd Fialaaf245d12014-06-30 21:05:18 +00002622 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Sean Callanana7b443a2012-02-14 22:50:38 +00002623 Error err;
2624
2625 uint64_t allocated_memory = AllocateMemory(8,
2626 ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable,
2627 err);
2628
2629 if (err.Success())
Todd Fialaaf245d12014-06-30 21:05:18 +00002630 {
Sean Callanana7b443a2012-02-14 22:50:38 +00002631 m_can_jit = eCanJITYes;
Todd Fialaaf245d12014-06-30 21:05:18 +00002632 if (log)
2633 log->Printf ("Process::%s pid %" PRIu64 " allocation test passed, CanJIT () is true", __FUNCTION__, GetID ());
2634 }
Sean Callanana7b443a2012-02-14 22:50:38 +00002635 else
Todd Fialaaf245d12014-06-30 21:05:18 +00002636 {
Sean Callanana7b443a2012-02-14 22:50:38 +00002637 m_can_jit = eCanJITNo;
Todd Fialaaf245d12014-06-30 21:05:18 +00002638 if (log)
2639 log->Printf ("Process::%s pid %" PRIu64 " allocation test failed, CanJIT () is false: %s", __FUNCTION__, GetID (), err.AsCString ());
2640 }
Sean Callanana7b443a2012-02-14 22:50:38 +00002641
2642 DeallocateMemory (allocated_memory);
2643 }
2644
Sean Callanan90539452011-09-20 23:01:51 +00002645 return m_can_jit == eCanJITYes;
2646}
2647
2648void
2649Process::SetCanJIT (bool can_jit)
2650{
2651 m_can_jit = (can_jit ? eCanJITYes : eCanJITNo);
2652}
2653
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002654Error
2655Process::DeallocateMemory (addr_t ptr)
2656{
Greg Claytond495c532011-05-17 03:37:42 +00002657 Error error;
2658#if defined (USE_ALLOCATE_MEMORY_CACHE)
2659 if (!m_allocated_memory_cache.DeallocateMemory(ptr))
2660 {
Daniel Malead01b2952012-11-29 21:49:15 +00002661 error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr);
Greg Claytond495c532011-05-17 03:37:42 +00002662 }
2663#else
2664 error = DoDeallocateMemory (ptr);
Greg Claytonb2daec92011-01-23 19:58:49 +00002665
Greg Clayton5160ce52013-03-27 23:08:40 +00002666 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002667 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002668 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 +00002669 ptr,
2670 error.AsCString("SUCCESS"),
Jim Ingham4b536182011-08-09 02:12:22 +00002671 m_mod_id.GetStopID(),
2672 m_mod_id.GetMemoryID());
Greg Claytond495c532011-05-17 03:37:42 +00002673#endif
Greg Claytonb2daec92011-01-23 19:58:49 +00002674 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002675}
2676
Han Ming Ongc811d382012-11-17 00:33:14 +00002677
Greg Claytonc9660542012-02-05 02:38:54 +00002678ModuleSP
Greg Claytonc859e2d2012-02-13 23:10:39 +00002679Process::ReadModuleFromMemory (const FileSpec& file_spec,
Andrew MacPherson17220c12014-03-05 10:12:43 +00002680 lldb::addr_t header_addr,
2681 size_t size_to_read)
Greg Claytonc9660542012-02-05 02:38:54 +00002682{
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002683 ModuleSP module_sp (new Module (file_spec, ArchSpec()));
Greg Claytonc9660542012-02-05 02:38:54 +00002684 if (module_sp)
2685 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002686 Error error;
Andrew MacPherson17220c12014-03-05 10:12:43 +00002687 ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error, size_to_read);
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002688 if (objfile)
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002689 return module_sp;
Greg Claytonc9660542012-02-05 02:38:54 +00002690 }
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002691 return ModuleSP();
Greg Claytonc9660542012-02-05 02:38:54 +00002692}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002693
2694Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002695Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002696{
2697 Error error;
2698 error.SetErrorString("watchpoints are not supported");
2699 return error;
2700}
2701
2702Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002703Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002704{
2705 Error error;
2706 error.SetErrorString("watchpoints are not supported");
2707 return error;
2708}
2709
2710StateType
2711Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
2712{
2713 StateType state;
2714 // Now wait for the process to launch and return control to us, and then
2715 // call DidLaunch:
2716 while (1)
2717 {
Greg Clayton6779606a2011-01-22 23:43:18 +00002718 event_sp.reset();
2719 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
2720
Greg Clayton2637f822011-11-17 01:23:07 +00002721 if (StateIsStoppedState(state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002722 break;
Greg Clayton6779606a2011-01-22 23:43:18 +00002723
2724 // If state is invalid, then we timed out
2725 if (state == eStateInvalid)
2726 break;
2727
2728 if (event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002729 HandlePrivateEvent (event_sp);
2730 }
2731 return state;
2732}
2733
2734Error
Greg Claytonfbb76342013-11-20 21:07:01 +00002735Process::Launch (ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002736{
2737 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002738 m_abi_sp.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002739 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002740 m_jit_loaders_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00002741 m_system_runtime_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002742 m_os_ap.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002743 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002744
Greg Claytonaa149cb2011-08-11 02:48:45 +00002745 Module *exe_module = m_target.GetExecutableModulePointer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002746 if (exe_module)
2747 {
Greg Clayton2289fa42011-04-30 01:09:13 +00002748 char local_exec_file_path[PATH_MAX];
2749 char platform_exec_file_path[PATH_MAX];
2750 exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path));
2751 exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002752 if (exe_module->GetFileSpec().Exists())
2753 {
Greg Claytonfbb76342013-11-20 21:07:01 +00002754 // Install anything that might need to be installed prior to launching.
2755 // For host systems, this will do nothing, but if we are connected to a
2756 // remote platform it will install any needed binaries
2757 error = GetTarget().Install(&launch_info);
2758 if (error.Fail())
2759 return error;
2760
Greg Clayton71337622011-02-24 22:24:29 +00002761 if (PrivateStateThreadIsValid ())
2762 PausePrivateStateThread ();
2763
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002764 error = WillLaunch (exe_module);
2765 if (error.Success())
2766 {
Jim Ingham221d51c2013-05-08 00:35:16 +00002767 const bool restarted = false;
2768 SetPublicState (eStateLaunching, restarted);
Greg Claytone24c4ac2011-11-17 04:46:02 +00002769 m_should_detach = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002770
Ed Maste64fad602013-07-29 20:58:06 +00002771 if (m_public_run_lock.TrySetRunning())
Greg Clayton69fd4be2012-09-04 20:29:05 +00002772 {
2773 // Now launch using these arguments.
2774 error = DoLaunch (exe_module, launch_info);
2775 }
2776 else
2777 {
2778 // This shouldn't happen
2779 error.SetErrorString("failed to acquire process run lock");
2780 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002781
2782 if (error.Fail())
2783 {
2784 if (GetID() != LLDB_INVALID_PROCESS_ID)
2785 {
2786 SetID (LLDB_INVALID_PROCESS_ID);
2787 const char *error_string = error.AsCString();
2788 if (error_string == NULL)
2789 error_string = "launch failed";
2790 SetExitStatus (-1, error_string);
2791 }
2792 }
2793 else
2794 {
2795 EventSP event_sp;
Greg Clayton1a38ea72011-06-22 01:42:17 +00002796 TimeValue timeout_time;
2797 timeout_time = TimeValue::Now();
2798 timeout_time.OffsetWithSeconds(10);
2799 StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002800
Greg Clayton1a38ea72011-06-22 01:42:17 +00002801 if (state == eStateInvalid || event_sp.get() == NULL)
2802 {
2803 // We were able to launch the process, but we failed to
2804 // catch the initial stop.
2805 SetExitStatus (0, "failed to catch stop after launch");
2806 Destroy();
2807 }
2808 else if (state == eStateStopped || state == eStateCrashed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002809 {
Greg Clayton93d3c8332011-02-16 04:46:07 +00002810
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002811 DidLaunch ();
2812
Greg Claytonc859e2d2012-02-13 23:10:39 +00002813 DynamicLoader *dyld = GetDynamicLoader ();
2814 if (dyld)
2815 dyld->DidLaunch();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002816
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00002817 GetJITLoaders().DidLaunch();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002818
Jason Molendaeef51062013-11-05 03:57:19 +00002819 SystemRuntime *system_runtime = GetSystemRuntime ();
2820 if (system_runtime)
2821 system_runtime->DidLaunch();
2822
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002823 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002824 // This delays passing the stopped event to listeners till DidLaunch gets
2825 // a chance to complete...
2826 HandlePrivateEvent (event_sp);
Greg Clayton71337622011-02-24 22:24:29 +00002827
2828 if (PrivateStateThreadIsValid ())
2829 ResumePrivateStateThread ();
2830 else
2831 StartPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002832 }
2833 else if (state == eStateExited)
2834 {
2835 // We exited while trying to launch somehow. Don't call DidLaunch as that's
2836 // not likely to work, and return an invalid pid.
2837 HandlePrivateEvent (event_sp);
2838 }
2839 }
2840 }
2841 }
2842 else
2843 {
Greg Clayton86edbf42011-10-26 00:56:27 +00002844 error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002845 }
2846 }
2847 return error;
2848}
2849
Greg Claytonc3776bf2012-02-09 06:16:32 +00002850
2851Error
2852Process::LoadCore ()
2853{
2854 Error error = DoLoadCore();
2855 if (error.Success())
2856 {
2857 if (PrivateStateThreadIsValid ())
2858 ResumePrivateStateThread ();
2859 else
2860 StartPrivateStateThread ();
2861
Greg Claytonc859e2d2012-02-13 23:10:39 +00002862 DynamicLoader *dyld = GetDynamicLoader ();
2863 if (dyld)
2864 dyld->DidAttach();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002865
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00002866 GetJITLoaders().DidAttach();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002867
Jason Molendaeef51062013-11-05 03:57:19 +00002868 SystemRuntime *system_runtime = GetSystemRuntime ();
2869 if (system_runtime)
2870 system_runtime->DidAttach();
2871
Greg Claytonc859e2d2012-02-13 23:10:39 +00002872 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Claytonc3776bf2012-02-09 06:16:32 +00002873 // We successfully loaded a core file, now pretend we stopped so we can
2874 // show all of the threads in the core file and explore the crashed
2875 // state.
2876 SetPrivateState (eStateStopped);
2877
2878 }
2879 return error;
2880}
2881
Greg Claytonc859e2d2012-02-13 23:10:39 +00002882DynamicLoader *
2883Process::GetDynamicLoader ()
2884{
2885 if (m_dyld_ap.get() == NULL)
2886 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
2887 return m_dyld_ap.get();
2888}
Greg Claytonc3776bf2012-02-09 06:16:32 +00002889
Todd Fialaaf245d12014-06-30 21:05:18 +00002890const lldb::DataBufferSP
2891Process::GetAuxvData()
2892{
2893 return DataBufferSP ();
2894}
2895
Andrew MacPherson17220c12014-03-05 10:12:43 +00002896JITLoaderList &
2897Process::GetJITLoaders ()
2898{
2899 if (!m_jit_loaders_ap)
2900 {
2901 m_jit_loaders_ap.reset(new JITLoaderList());
2902 JITLoader::LoadPlugins(this, *m_jit_loaders_ap);
2903 }
2904 return *m_jit_loaders_ap;
2905}
2906
Jason Molendaeef51062013-11-05 03:57:19 +00002907SystemRuntime *
2908Process::GetSystemRuntime ()
2909{
2910 if (m_system_runtime_ap.get() == NULL)
2911 m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this));
2912 return m_system_runtime_ap.get();
2913}
2914
Todd Fiala76e0fc92014-08-27 22:58:26 +00002915Process::AttachCompletionHandler::AttachCompletionHandler (Process *process, uint32_t exec_count) :
2916 NextEventAction (process),
2917 m_exec_count (exec_count)
2918{
2919 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2920 if (log)
2921 log->Printf ("Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32, __FUNCTION__, static_cast<void*>(process), exec_count);
2922}
Greg Claytonc3776bf2012-02-09 06:16:32 +00002923
Jim Inghambb3a2832011-01-29 01:49:25 +00002924Process::NextEventAction::EventActionResult
2925Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002926{
Todd Fiala76e0fc92014-08-27 22:58:26 +00002927 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2928
Jim Inghambb3a2832011-01-29 01:49:25 +00002929 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
Todd Fiala76e0fc92014-08-27 22:58:26 +00002930 if (log)
2931 log->Printf ("Process::AttachCompletionHandler::%s called with state %s (%d)", __FUNCTION__, StateAsCString(state), static_cast<int> (state));
2932
2933 switch (state)
Greg Clayton19388cf2010-10-18 01:45:30 +00002934 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002935 case eStateRunning:
Greg Clayton71337622011-02-24 22:24:29 +00002936 case eStateConnected:
Greg Clayton513c26c2011-01-29 07:10:55 +00002937 return eEventActionRetry;
2938
2939 case eStateStopped:
2940 case eStateCrashed:
Greg Claytonc9ed4782011-11-12 02:10:56 +00002941 {
2942 // During attach, prior to sending the eStateStopped event,
Jim Inghamb1e2e842012-04-12 18:49:31 +00002943 // lldb_private::Process subclasses must set the new process ID.
Greg Claytonc9ed4782011-11-12 02:10:56 +00002944 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
Jim Ingham221d51c2013-05-08 00:35:16 +00002945 // We don't want these events to be reported, so go set the ShouldReportStop here:
2946 m_process->GetThreadList().SetShouldReportStop (eVoteNo);
2947
Greg Claytonc9ed4782011-11-12 02:10:56 +00002948 if (m_exec_count > 0)
2949 {
2950 --m_exec_count;
Todd Fiala76e0fc92014-08-27 22:58:26 +00002951
2952 if (log)
2953 log->Printf ("Process::AttachCompletionHandler::%s state %s: reduced remaining exec count to %" PRIu32 ", requesting resume", __FUNCTION__, StateAsCString(state), m_exec_count);
2954
Jim Ingham221d51c2013-05-08 00:35:16 +00002955 RequestResume();
Greg Claytonc9ed4782011-11-12 02:10:56 +00002956 return eEventActionRetry;
2957 }
2958 else
2959 {
Todd Fiala76e0fc92014-08-27 22:58:26 +00002960 if (log)
2961 log->Printf ("Process::AttachCompletionHandler::%s state %s: no more execs expected to start, continuing with attach", __FUNCTION__, StateAsCString(state));
2962
Greg Claytonc9ed4782011-11-12 02:10:56 +00002963 m_process->CompleteAttach ();
2964 return eEventActionSuccess;
2965 }
2966 }
Greg Clayton513c26c2011-01-29 07:10:55 +00002967 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00002968
Greg Clayton513c26c2011-01-29 07:10:55 +00002969 default:
2970 case eStateExited:
2971 case eStateInvalid:
Greg Clayton513c26c2011-01-29 07:10:55 +00002972 break;
Jim Inghambb3a2832011-01-29 01:49:25 +00002973 }
Greg Claytonc9ed4782011-11-12 02:10:56 +00002974
2975 m_exit_string.assign ("No valid Process");
2976 return eEventActionExit;
Jim Inghambb3a2832011-01-29 01:49:25 +00002977}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002978
Jim Inghambb3a2832011-01-29 01:49:25 +00002979Process::NextEventAction::EventActionResult
2980Process::AttachCompletionHandler::HandleBeingInterrupted()
2981{
2982 return eEventActionSuccess;
2983}
2984
2985const char *
2986Process::AttachCompletionHandler::GetExitString ()
2987{
2988 return m_exit_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002989}
2990
2991Error
Greg Clayton144f3a92011-11-15 03:53:30 +00002992Process::Attach (ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002993{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002994 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002995 m_process_input_reader.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002996 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002997 m_jit_loaders_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00002998 m_system_runtime_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002999 m_os_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00003000
Greg Clayton144f3a92011-11-15 03:53:30 +00003001 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Claytone996fd32011-03-08 22:40:15 +00003002 Error error;
Greg Clayton144f3a92011-11-15 03:53:30 +00003003 if (attach_pid == LLDB_INVALID_PROCESS_ID)
Jim Ingham5aee1622010-08-09 23:31:02 +00003004 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003005 char process_name[PATH_MAX];
Jim Ingham4299fdb2011-09-15 01:10:17 +00003006
Greg Clayton144f3a92011-11-15 03:53:30 +00003007 if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name)))
Jim Ingham2ecb7422010-08-17 21:54:19 +00003008 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003009 const bool wait_for_launch = attach_info.GetWaitForLaunch();
3010
3011 if (wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003012 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003013 error = WillAttachToProcessWithName(process_name, wait_for_launch);
3014 if (error.Success())
3015 {
Ed Maste64fad602013-07-29 20:58:06 +00003016 if (m_public_run_lock.TrySetRunning())
Greg Clayton926cce72012-10-12 16:10:12 +00003017 {
3018 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003019 const bool restarted = false;
3020 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003021 // Now attach using these arguments.
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +00003022 error = DoAttachToProcessWithName (process_name, attach_info);
Greg Clayton926cce72012-10-12 16:10:12 +00003023 }
3024 else
3025 {
3026 // This shouldn't happen
3027 error.SetErrorString("failed to acquire process run lock");
3028 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00003029
Greg Clayton144f3a92011-11-15 03:53:30 +00003030 if (error.Fail())
3031 {
3032 if (GetID() != LLDB_INVALID_PROCESS_ID)
3033 {
3034 SetID (LLDB_INVALID_PROCESS_ID);
3035 if (error.AsCString() == NULL)
3036 error.SetErrorString("attach failed");
3037
3038 SetExitStatus(-1, error.AsCString());
3039 }
3040 }
3041 else
3042 {
3043 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3044 StartPrivateStateThread();
3045 }
3046 return error;
3047 }
Greg Claytone996fd32011-03-08 22:40:15 +00003048 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003049 else
Greg Claytone996fd32011-03-08 22:40:15 +00003050 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003051 ProcessInstanceInfoList process_infos;
3052 PlatformSP platform_sp (m_target.GetPlatform ());
3053
3054 if (platform_sp)
3055 {
3056 ProcessInstanceInfoMatch match_info;
3057 match_info.GetProcessInfo() = attach_info;
3058 match_info.SetNameMatchType (eNameMatchEquals);
3059 platform_sp->FindProcesses (match_info, process_infos);
3060 const uint32_t num_matches = process_infos.GetSize();
3061 if (num_matches == 1)
3062 {
3063 attach_pid = process_infos.GetProcessIDAtIndex(0);
3064 // Fall through and attach using the above process ID
3065 }
3066 else
3067 {
3068 match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name));
3069 if (num_matches > 1)
Jim Ingham368ac222014-08-15 17:05:27 +00003070 {
3071 StreamString s;
3072 ProcessInstanceInfo::DumpTableHeader (s, platform_sp.get(), true, false);
3073 for (size_t i = 0; i < num_matches; i++)
3074 {
3075 process_infos.GetProcessInfoAtIndex(i).DumpAsTableRow(s, platform_sp.get(), true, false);
3076 }
3077 error.SetErrorStringWithFormat ("more than one process named %s:\n%s",
3078 process_name,
3079 s.GetData());
3080 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003081 else
3082 error.SetErrorStringWithFormat ("could not find a process named %s", process_name);
3083 }
3084 }
3085 else
3086 {
3087 error.SetErrorString ("invalid platform, can't find processes by name");
3088 return error;
3089 }
Greg Claytone996fd32011-03-08 22:40:15 +00003090 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003091 }
3092 else
Greg Clayton144f3a92011-11-15 03:53:30 +00003093 {
3094 error.SetErrorString ("invalid process name");
Greg Claytone996fd32011-03-08 22:40:15 +00003095 }
3096 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003097
3098 if (attach_pid != LLDB_INVALID_PROCESS_ID)
Greg Claytone996fd32011-03-08 22:40:15 +00003099 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003100 error = WillAttachToProcessWithID(attach_pid);
Greg Claytone996fd32011-03-08 22:40:15 +00003101 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003102 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003103
Ed Maste64fad602013-07-29 20:58:06 +00003104 if (m_public_run_lock.TrySetRunning())
Greg Clayton926cce72012-10-12 16:10:12 +00003105 {
3106 // Now attach using these arguments.
3107 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003108 const bool restarted = false;
3109 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003110 error = DoAttachToProcessWithID (attach_pid, attach_info);
3111 }
3112 else
3113 {
3114 // This shouldn't happen
3115 error.SetErrorString("failed to acquire process run lock");
3116 }
3117
Greg Clayton144f3a92011-11-15 03:53:30 +00003118 if (error.Success())
3119 {
3120
3121 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3122 StartPrivateStateThread();
3123 }
3124 else
Greg Claytone996fd32011-03-08 22:40:15 +00003125 {
3126 if (GetID() != LLDB_INVALID_PROCESS_ID)
3127 {
3128 SetID (LLDB_INVALID_PROCESS_ID);
3129 const char *error_string = error.AsCString();
3130 if (error_string == NULL)
3131 error_string = "attach failed";
3132
3133 SetExitStatus(-1, error_string);
3134 }
3135 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003136 }
3137 }
3138 return error;
3139}
3140
Greg Clayton93d3c8332011-02-16 04:46:07 +00003141void
3142Process::CompleteAttach ()
3143{
Todd Fiala76e0fc92014-08-27 22:58:26 +00003144 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3145 if (log)
3146 log->Printf ("Process::%s()", __FUNCTION__);
3147
Greg Clayton93d3c8332011-02-16 04:46:07 +00003148 // Let the process subclass figure out at much as it can about the process
3149 // before we go looking for a dynamic loader plug-in.
Jim Inghambb006ce2014-08-02 00:33:35 +00003150 ArchSpec process_arch;
3151 DidAttach(process_arch);
3152
3153 if (process_arch.IsValid())
Todd Fiala76e0fc92014-08-27 22:58:26 +00003154 {
Jim Inghambb006ce2014-08-02 00:33:35 +00003155 m_target.SetArchitecture(process_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003156 if (log)
3157 {
3158 const char *triple_str = process_arch.GetTriple().getTriple().c_str ();
3159 log->Printf ("Process::%s replacing process architecture with DidAttach() architecture: %s",
3160 __FUNCTION__,
3161 triple_str ? triple_str : "<null>");
3162 }
3163 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003164
Jim Ingham4299fdb2011-09-15 01:10:17 +00003165 // We just attached. If we have a platform, ask it for the process architecture, and if it isn't
3166 // the same as the one we've already set, switch architectures.
3167 PlatformSP platform_sp (m_target.GetPlatform ());
3168 assert (platform_sp.get());
3169 if (platform_sp)
3170 {
Greg Clayton70512312012-05-08 01:45:38 +00003171 const ArchSpec &target_arch = m_target.GetArchitecture();
Greg Clayton1e0c8842013-01-11 20:49:54 +00003172 if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL))
Greg Clayton70512312012-05-08 01:45:38 +00003173 {
3174 ArchSpec platform_arch;
3175 platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch);
3176 if (platform_sp)
3177 {
3178 m_target.SetPlatform (platform_sp);
3179 m_target.SetArchitecture(platform_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003180 if (log)
3181 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 +00003182 }
3183 }
Jim Inghambb006ce2014-08-02 00:33:35 +00003184 else if (!process_arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00003185 {
3186 ProcessInstanceInfo process_info;
3187 platform_sp->GetProcessInfo (GetID(), process_info);
3188 const ArchSpec &process_arch = process_info.GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +00003189 if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch))
Todd Fiala76e0fc92014-08-27 22:58:26 +00003190 {
Greg Clayton70512312012-05-08 01:45:38 +00003191 m_target.SetArchitecture (process_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003192 if (log)
3193 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 ());
3194 }
Greg Clayton70512312012-05-08 01:45:38 +00003195 }
Jim Ingham4299fdb2011-09-15 01:10:17 +00003196 }
3197
3198 // We have completed the attach, now it is time to find the dynamic loader
Greg Clayton93d3c8332011-02-16 04:46:07 +00003199 // plug-in
Greg Claytonc859e2d2012-02-13 23:10:39 +00003200 DynamicLoader *dyld = GetDynamicLoader ();
3201 if (dyld)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003202 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00003203 dyld->DidAttach();
Todd Fiala76e0fc92014-08-27 22:58:26 +00003204 if (log)
3205 {
3206 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3207 log->Printf ("Process::%s after DynamicLoader::DidAttach(), target executable is %s (using %s plugin)",
3208 __FUNCTION__,
3209 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>",
3210 dyld->GetPluginName().AsCString ("<unnamed>"));
3211 }
3212 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003213
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00003214 GetJITLoaders().DidAttach();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003215
Jason Molendaeef51062013-11-05 03:57:19 +00003216 SystemRuntime *system_runtime = GetSystemRuntime ();
3217 if (system_runtime)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003218 {
Jason Molendaeef51062013-11-05 03:57:19 +00003219 system_runtime->DidAttach();
Todd Fiala76e0fc92014-08-27 22:58:26 +00003220 if (log)
3221 {
3222 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3223 log->Printf ("Process::%s after SystemRuntime::DidAttach(), target executable is %s (using %s plugin)",
3224 __FUNCTION__,
3225 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>",
3226 system_runtime->GetPluginName().AsCString("<unnamed>"));
3227 }
3228 }
Jason Molendaeef51062013-11-05 03:57:19 +00003229
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003230 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Clayton93d3c8332011-02-16 04:46:07 +00003231 // Figure out which one is the executable, and set that in our target:
Enrico Granata17598482012-11-08 02:22:02 +00003232 const ModuleList &target_modules = m_target.GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003233 Mutex::Locker modules_locker(target_modules.GetMutex());
3234 size_t num_modules = target_modules.GetSize();
3235 ModuleSP new_executable_module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003236
Andy Gibbsa297a972013-06-19 19:04:53 +00003237 for (size_t i = 0; i < num_modules; i++)
Greg Clayton93d3c8332011-02-16 04:46:07 +00003238 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003239 ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i));
Greg Clayton8b82f082011-04-12 05:54:46 +00003240 if (module_sp && module_sp->IsExecutable())
Greg Clayton93d3c8332011-02-16 04:46:07 +00003241 {
Greg Claytonaa149cb2011-08-11 02:48:45 +00003242 if (m_target.GetExecutableModulePointer() != module_sp.get())
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003243 new_executable_module_sp = module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003244 break;
3245 }
3246 }
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003247 if (new_executable_module_sp)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003248 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003249 m_target.SetExecutableModule (new_executable_module_sp, false);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003250 if (log)
3251 {
3252 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3253 log->Printf ("Process::%s after looping through modules, target executable is %s",
3254 __FUNCTION__,
3255 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>");
3256 }
3257 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003258}
3259
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003260Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003261Process::ConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +00003262{
Greg Claytonb766a732011-02-04 01:58:07 +00003263 m_abi_sp.reset();
3264 m_process_input_reader.reset();
3265
3266 // Find the process and its architecture. Make sure it matches the architecture
3267 // of the current Target, and if not adjust it.
3268
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003269 Error error (DoConnectRemote (strm, remote_url));
Greg Claytonb766a732011-02-04 01:58:07 +00003270 if (error.Success())
3271 {
Greg Clayton71337622011-02-24 22:24:29 +00003272 if (GetID() != LLDB_INVALID_PROCESS_ID)
3273 {
Greg Clayton32e0a752011-03-30 18:16:51 +00003274 EventSP event_sp;
3275 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
3276
3277 if (state == eStateStopped || state == eStateCrashed)
3278 {
3279 // If we attached and actually have a process on the other end, then
3280 // this ended up being the equivalent of an attach.
3281 CompleteAttach ();
3282
3283 // This delays passing the stopped event to listeners till
3284 // CompleteAttach gets a chance to complete...
3285 HandlePrivateEvent (event_sp);
3286
3287 }
Greg Clayton71337622011-02-24 22:24:29 +00003288 }
Greg Clayton32e0a752011-03-30 18:16:51 +00003289
3290 if (PrivateStateThreadIsValid ())
3291 ResumePrivateStateThread ();
3292 else
3293 StartPrivateStateThread ();
Greg Claytonb766a732011-02-04 01:58:07 +00003294 }
3295 return error;
3296}
3297
3298
3299Error
Jim Ingham3b8285d2012-04-19 01:40:33 +00003300Process::PrivateResume ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003301{
Greg Clayton5160ce52013-03-27 23:08:40 +00003302 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003303 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003304 log->Printf("Process::PrivateResume() m_stop_id = %u, public state: %s private state: %s",
Jim Ingham4b536182011-08-09 02:12:22 +00003305 m_mod_id.GetStopID(),
Jim Ingham444586b2011-01-24 06:34:17 +00003306 StateAsCString(m_public_state.GetValue()),
3307 StateAsCString(m_private_state.GetValue()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003308
3309 Error error (WillResume());
3310 // Tell the process it is about to resume before the thread list
3311 if (error.Success())
3312 {
Johnny Chenc4221e42010-12-02 20:53:05 +00003313 // Now let the thread list know we are about to resume so it
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003314 // can let all of our threads know that they are about to be
3315 // resumed. Threads will each be called with
3316 // Thread::WillResume(StateType) where StateType contains the state
3317 // that they are supposed to have when the process is resumed
3318 // (suspended/running/stepping). Threads should also check
3319 // their resume signal in lldb::Thread::GetResumeSignal()
Jim Ingham221d51c2013-05-08 00:35:16 +00003320 // to see if they are supposed to start back up with a signal.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003321 if (m_thread_list.WillResume())
3322 {
Jim Ingham372787f2012-04-07 00:00:41 +00003323 // Last thing, do the PreResumeActions.
3324 if (!RunPreResumeActions())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003325 {
Jim Ingham0161b492013-02-09 01:29:05 +00003326 error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming.");
Jim Ingham372787f2012-04-07 00:00:41 +00003327 }
3328 else
3329 {
3330 m_mod_id.BumpResumeID();
3331 error = DoResume();
3332 if (error.Success())
3333 {
3334 DidResume();
3335 m_thread_list.DidResume();
3336 if (log)
3337 log->Printf ("Process thinks the process has resumed.");
3338 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003339 }
3340 }
3341 else
3342 {
Jim Ingham513c6bb2012-09-01 01:02:41 +00003343 // Somebody wanted to run without running. So generate a continue & a stopped event,
3344 // and let the world handle them.
3345 if (log)
3346 log->Printf ("Process::PrivateResume() asked to simulate a start & stop.");
3347
3348 SetPrivateState(eStateRunning);
3349 SetPrivateState(eStateStopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003350 }
3351 }
Jim Ingham444586b2011-01-24 06:34:17 +00003352 else if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003353 log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003354 return error;
3355}
3356
3357Error
Greg Claytonf9b57b92013-05-10 23:48:10 +00003358Process::Halt (bool clear_thread_plans)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003359{
Greg Claytonf9b57b92013-05-10 23:48:10 +00003360 // Don't clear the m_clear_thread_plans_on_stop, only set it to true if
3361 // in case it was already set and some thread plan logic calls halt on its
3362 // own.
3363 m_clear_thread_plans_on_stop |= clear_thread_plans;
3364
Jim Inghamaacc3182012-06-06 00:29:30 +00003365 // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since
3366 // we could just straightaway get another event. It just narrows the window...
3367 m_currently_handling_event.WaitForValueEqualTo(false);
3368
3369
Jim Inghambb3a2832011-01-29 01:49:25 +00003370 // Pause our private state thread so we can ensure no one else eats
3371 // the stop event out from under us.
Jim Ingham0f16e732011-02-08 05:20:59 +00003372 Listener halt_listener ("lldb.process.halt_listener");
3373 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton3af9ea52010-11-18 05:57:03 +00003374
Jim Inghambb3a2832011-01-29 01:49:25 +00003375 EventSP event_sp;
Greg Clayton513c26c2011-01-29 07:10:55 +00003376 Error error (WillHalt());
Jim Inghambb3a2832011-01-29 01:49:25 +00003377
Greg Clayton06357c92014-07-30 17:38:47 +00003378 bool restored_process_events = false;
Greg Clayton513c26c2011-01-29 07:10:55 +00003379 if (error.Success())
Jim Inghambb3a2832011-01-29 01:49:25 +00003380 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003381
Greg Clayton513c26c2011-01-29 07:10:55 +00003382 bool caused_stop = false;
3383
3384 // Ask the process subclass to actually halt our process
3385 error = DoHalt(caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003386 if (error.Success())
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003387 {
Greg Clayton513c26c2011-01-29 07:10:55 +00003388 if (m_public_state.GetValue() == eStateAttaching)
3389 {
Greg Clayton06357c92014-07-30 17:38:47 +00003390 // Don't hijack and eat the eStateExited as the code that was doing
3391 // the attach will be waiting for this event...
3392 RestorePrivateProcessEvents();
3393 restored_process_events = true;
Greg Clayton513c26c2011-01-29 07:10:55 +00003394 SetExitStatus(SIGKILL, "Cancelled async attach.");
3395 Destroy ();
3396 }
3397 else
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003398 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003399 // If "caused_stop" is true, then DoHalt stopped the process. If
3400 // "caused_stop" is false, the process was already stopped.
3401 // If the DoHalt caused the process to stop, then we want to catch
3402 // this event and set the interrupted bool to true before we pass
3403 // this along so clients know that the process was interrupted by
3404 // a halt command.
3405 if (caused_stop)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003406 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003407 // Wait for 1 second for the process to stop.
Jim Inghambb3a2832011-01-29 01:49:25 +00003408 TimeValue timeout_time;
3409 timeout_time = TimeValue::Now();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003410 timeout_time.OffsetWithSeconds(10);
Jim Ingham0f16e732011-02-08 05:20:59 +00003411 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
3412 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003413
Jim Ingham0f16e732011-02-08 05:20:59 +00003414 if (!got_event || state == eStateInvalid)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003415 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003416 // We timeout out and didn't get a stop event...
Jim Ingham0f16e732011-02-08 05:20:59 +00003417 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton3af9ea52010-11-18 05:57:03 +00003418 }
3419 else
3420 {
Greg Clayton2637f822011-11-17 01:23:07 +00003421 if (StateIsStoppedState (state, false))
Jim Inghambb3a2832011-01-29 01:49:25 +00003422 {
3423 // We caused the process to interrupt itself, so mark this
3424 // as such in the stop event so clients can tell an interrupted
3425 // process from a natural stop
3426 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
3427 }
3428 else
3429 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003430 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghambb3a2832011-01-29 01:49:25 +00003431 if (log)
3432 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
3433 error.SetErrorString ("Did not get stopped event after halt.");
3434 }
Greg Clayton3af9ea52010-11-18 05:57:03 +00003435 }
3436 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003437 DidHalt();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003438 }
3439 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003440 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003441 // Resume our private state thread before we post the event (if any)
Greg Clayton06357c92014-07-30 17:38:47 +00003442 if (!restored_process_events)
3443 RestorePrivateProcessEvents();
Jim Inghambb3a2832011-01-29 01:49:25 +00003444
3445 // Post any event we might have consumed. If all goes well, we will have
3446 // stopped the process, intercepted the event and set the interrupted
3447 // bool in the event. Post it to the private event queue and that will end up
3448 // correctly setting the state.
3449 if (event_sp)
3450 m_private_state_broadcaster.BroadcastEvent(event_sp);
3451
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003452 return error;
3453}
3454
3455Error
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003456Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp)
3457{
3458 Error error;
3459 if (m_public_state.GetValue() == eStateRunning)
3460 {
3461 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3462 if (log)
3463 log->Printf("Process::Destroy() About to halt.");
3464 error = Halt();
3465 if (error.Success())
3466 {
3467 // Consume the halt event.
3468 TimeValue timeout (TimeValue::Now());
3469 timeout.OffsetWithSeconds(1);
3470 StateType state = WaitForProcessToStop (&timeout, &exit_event_sp);
3471
3472 // If the process exited while we were waiting for it to stop, put the exited event into
3473 // the shared pointer passed in and return. Our caller doesn't need to do anything else, since
3474 // they don't have a process anymore...
3475
3476 if (state == eStateExited || m_private_state.GetValue() == eStateExited)
3477 {
3478 if (log)
3479 log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt.");
3480 return error;
3481 }
3482 else
3483 exit_event_sp.reset(); // It is ok to consume any non-exit stop events
3484
3485 if (state != eStateStopped)
3486 {
3487 if (log)
3488 log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state));
3489 // If we really couldn't stop the process then we should just error out here, but if the
3490 // lower levels just bobbled sending the event and we really are stopped, then continue on.
3491 StateType private_state = m_private_state.GetValue();
3492 if (private_state != eStateStopped)
3493 {
3494 return error;
3495 }
3496 }
3497 }
3498 else
3499 {
3500 if (log)
3501 log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString());
3502 }
3503 }
3504 return error;
3505}
3506
3507Error
Jim Inghamacff8952013-05-02 00:27:30 +00003508Process::Detach (bool keep_stopped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003509{
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003510 EventSP exit_event_sp;
3511 Error error;
3512 m_destroy_in_process = true;
3513
3514 error = WillDetach();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003515
3516 if (error.Success())
3517 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003518 if (DetachRequiresHalt())
3519 {
3520 error = HaltForDestroyOrDetach (exit_event_sp);
3521 if (!error.Success())
3522 {
3523 m_destroy_in_process = false;
3524 return error;
3525 }
3526 else if (exit_event_sp)
3527 {
3528 // We shouldn't need to do anything else here. There's no process left to detach from...
3529 StopPrivateStateThread();
3530 m_destroy_in_process = false;
3531 return error;
3532 }
3533 }
3534
Andrew MacPhersonc3826b52014-03-25 19:59:36 +00003535 m_thread_list.DiscardThreadPlans();
3536 DisableAllBreakpointSites();
3537
Jim Inghamacff8952013-05-02 00:27:30 +00003538 error = DoDetach(keep_stopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003539 if (error.Success())
3540 {
3541 DidDetach();
3542 StopPrivateStateThread();
3543 }
Jim Inghamacff8952013-05-02 00:27:30 +00003544 else
3545 {
3546 return error;
3547 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003548 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003549 m_destroy_in_process = false;
3550
3551 // If we exited when we were waiting for a process to stop, then
3552 // forward the event here so we don't lose the event
3553 if (exit_event_sp)
3554 {
3555 // Directly broadcast our exited event because we shut down our
3556 // private state thread above
3557 BroadcastEvent(exit_event_sp);
3558 }
3559
3560 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3561 // the last events through the event system, in which case we might strand the write lock. Unlock
3562 // it here so when we do to tear down the process we don't get an error destroying the lock.
3563
Ed Maste64fad602013-07-29 20:58:06 +00003564 m_public_run_lock.SetStopped();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003565 return error;
3566}
3567
3568Error
3569Process::Destroy ()
3570{
Jim Ingham09437922013-03-01 20:04:25 +00003571
3572 // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work
3573 // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt
3574 // failed and the process stays around for some reason it won't be in a confused state.
3575
3576 m_destroy_in_process = true;
3577
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003578 Error error (WillDestroy());
3579 if (error.Success())
3580 {
Greg Clayton85fb1b92012-09-11 02:33:37 +00003581 EventSP exit_event_sp;
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003582 if (DestroyRequiresHalt())
Jim Ingham04e0a222012-05-23 15:46:31 +00003583 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003584 error = HaltForDestroyOrDetach(exit_event_sp);
Jim Ingham04e0a222012-05-23 15:46:31 +00003585 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003586
Jim Inghamaacc3182012-06-06 00:29:30 +00003587 if (m_public_state.GetValue() != eStateRunning)
3588 {
3589 // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to
3590 // kill it, we don't want it hitting a breakpoint...
3591 // Only do this if we've stopped, however, since if we didn't manage to halt it above, then
3592 // we're not going to have much luck doing this now.
3593 m_thread_list.DiscardThreadPlans();
3594 DisableAllBreakpointSites();
3595 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003596
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003597 error = DoDestroy();
3598 if (error.Success())
3599 {
3600 DidDestroy();
3601 StopPrivateStateThread();
3602 }
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003603 m_stdio_communication.StopReadThread();
3604 m_stdio_communication.Disconnect();
Greg Claytonb4874f12014-02-28 18:22:24 +00003605
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003606 if (m_process_input_reader)
Greg Claytonb4874f12014-02-28 18:22:24 +00003607 {
3608 m_process_input_reader->SetIsDone(true);
3609 m_process_input_reader->Cancel();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003610 m_process_input_reader.reset();
Greg Claytonb4874f12014-02-28 18:22:24 +00003611 }
3612
Greg Clayton85fb1b92012-09-11 02:33:37 +00003613 // If we exited when we were waiting for a process to stop, then
3614 // forward the event here so we don't lose the event
3615 if (exit_event_sp)
3616 {
3617 // Directly broadcast our exited event because we shut down our
3618 // private state thread above
3619 BroadcastEvent(exit_event_sp);
3620 }
3621
Jim Inghamb1e2e842012-04-12 18:49:31 +00003622 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3623 // the last events through the event system, in which case we might strand the write lock. Unlock
3624 // 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 +00003625 m_public_run_lock.SetStopped();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003626 }
Jim Ingham09437922013-03-01 20:04:25 +00003627
3628 m_destroy_in_process = false;
3629
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003630 return error;
3631}
3632
3633Error
3634Process::Signal (int signal)
3635{
3636 Error error (WillSignal());
3637 if (error.Success())
3638 {
3639 error = DoSignal(signal);
3640 if (error.Success())
3641 DidSignal();
3642 }
3643 return error;
3644}
3645
Greg Clayton514487e2011-02-15 21:59:32 +00003646lldb::ByteOrder
3647Process::GetByteOrder () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003648{
Greg Clayton514487e2011-02-15 21:59:32 +00003649 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003650}
3651
3652uint32_t
Greg Clayton514487e2011-02-15 21:59:32 +00003653Process::GetAddressByteSize () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003654{
Greg Clayton514487e2011-02-15 21:59:32 +00003655 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003656}
3657
Greg Clayton514487e2011-02-15 21:59:32 +00003658
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003659bool
3660Process::ShouldBroadcastEvent (Event *event_ptr)
3661{
3662 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
3663 bool return_value = true;
Greg Clayton5160ce52013-03-27 23:08:40 +00003664 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS));
Jim Ingham0161b492013-02-09 01:29:05 +00003665
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003666 switch (state)
3667 {
Greg Claytonb766a732011-02-04 01:58:07 +00003668 case eStateConnected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003669 case eStateAttaching:
3670 case eStateLaunching:
3671 case eStateDetached:
3672 case eStateExited:
3673 case eStateUnloaded:
3674 // These events indicate changes in the state of the debugging session, always report them.
3675 return_value = true;
3676 break;
3677 case eStateInvalid:
3678 // We stopped for no apparent reason, don't report it.
3679 return_value = false;
3680 break;
3681 case eStateRunning:
3682 case eStateStepping:
3683 // If we've started the target running, we handle the cases where we
3684 // are already running and where there is a transition from stopped to
3685 // running differently.
3686 // running -> running: Automatically suppress extra running events
3687 // stopped -> running: Report except when there is one or more no votes
3688 // and no yes votes.
3689 SynchronouslyNotifyStateChanged (state);
Jim Ingham1460e4b2014-01-10 23:46:59 +00003690 if (m_force_next_event_delivery)
3691 return_value = true;
3692 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003693 {
Jim Ingham1460e4b2014-01-10 23:46:59 +00003694 switch (m_last_broadcast_state)
3695 {
3696 case eStateRunning:
3697 case eStateStepping:
3698 // We always suppress multiple runnings with no PUBLIC stop in between.
3699 return_value = false;
3700 break;
3701 default:
3702 // TODO: make this work correctly. For now always report
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003703 // run if we aren't running so we don't miss any running
Jim Ingham1460e4b2014-01-10 23:46:59 +00003704 // events. If I run the lldb/test/thread/a.out file and
3705 // break at main.cpp:58, run and hit the breakpoints on
3706 // multiple threads, then somehow during the stepping over
3707 // of all breakpoints no run gets reported.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003708
Jim Ingham1460e4b2014-01-10 23:46:59 +00003709 // This is a transition from stop to run.
3710 switch (m_thread_list.ShouldReportRun (event_ptr))
3711 {
3712 case eVoteYes:
3713 case eVoteNoOpinion:
3714 return_value = true;
3715 break;
3716 case eVoteNo:
3717 return_value = false;
3718 break;
3719 }
3720 break;
3721 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003722 }
3723 break;
3724 case eStateStopped:
3725 case eStateCrashed:
3726 case eStateSuspended:
3727 {
3728 // We've stopped. First see if we're going to restart the target.
3729 // If we are going to stop, then we always broadcast the event.
3730 // 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 +00003731 // If no thread has an opinion, we don't report it.
Jim Ingham221d51c2013-05-08 00:35:16 +00003732
Jim Inghamcb4ca112012-05-16 01:32:14 +00003733 RefreshStateAfterStop ();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003734 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003735 {
Greg Clayton3af9ea52010-11-18 05:57:03 +00003736 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003737 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003738 static_cast<void*>(event_ptr),
Jim Ingham0161b492013-02-09 01:29:05 +00003739 StateAsCString(state));
Jim Ingham35878c42014-04-08 21:33:21 +00003740 // Even though we know we are going to stop, we should let the threads have a look at the stop,
3741 // so they can properly set their state.
3742 m_thread_list.ShouldStop (event_ptr);
Jim Ingham0161b492013-02-09 01:29:05 +00003743 return_value = true;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003744 }
3745 else
3746 {
Jim Ingham221d51c2013-05-08 00:35:16 +00003747 bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr);
3748 bool should_resume = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003749
Jim Ingham0161b492013-02-09 01:29:05 +00003750 // It makes no sense to ask "ShouldStop" if we've already been restarted...
3751 // Asking the thread list is also not likely to go well, since we are running again.
3752 // So in that case just report the event.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003753
Jim Ingham0161b492013-02-09 01:29:05 +00003754 if (!was_restarted)
3755 should_resume = m_thread_list.ShouldStop (event_ptr) == false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003756
Jim Ingham221d51c2013-05-08 00:35:16 +00003757 if (was_restarted || should_resume || m_resume_requested)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003758 {
Jim Ingham0161b492013-02-09 01:29:05 +00003759 Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr);
3760 if (log)
3761 log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003762 should_resume, StateAsCString(state),
3763 was_restarted, stop_vote);
3764
Jim Ingham0161b492013-02-09 01:29:05 +00003765 switch (stop_vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003766 {
3767 case eVoteYes:
Jim Ingham0161b492013-02-09 01:29:05 +00003768 return_value = true;
3769 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003770 case eVoteNoOpinion:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003771 case eVoteNo:
3772 return_value = false;
3773 break;
3774 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003775
Jim Inghamcb95f342012-09-05 21:13:56 +00003776 if (!was_restarted)
Jim Ingham0161b492013-02-09 01:29:05 +00003777 {
3778 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003779 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s",
3780 static_cast<void*>(event_ptr),
3781 StateAsCString(state));
Jim Ingham0161b492013-02-09 01:29:05 +00003782 ProcessEventData::SetRestartedInEvent(event_ptr, true);
Jim Inghamcb95f342012-09-05 21:13:56 +00003783 PrivateResume ();
Jim Ingham0161b492013-02-09 01:29:05 +00003784 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003785
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003786 }
3787 else
3788 {
3789 return_value = true;
3790 SynchronouslyNotifyStateChanged (state);
3791 }
3792 }
3793 }
Jim Ingham0161b492013-02-09 01:29:05 +00003794 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003795 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003796
Jim Ingham1460e4b2014-01-10 23:46:59 +00003797 // Forcing the next event delivery is a one shot deal. So reset it here.
3798 m_force_next_event_delivery = false;
3799
Jim Ingham0161b492013-02-09 01:29:05 +00003800 // We do some coalescing of events (for instance two consecutive running events get coalesced.)
3801 // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state
3802 // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done,
3803 // because the PublicState reflects the last event pulled off the queue, and there may be several
3804 // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event
3805 // yet. m_last_broadcast_state gets updated here.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003806
Jim Ingham0161b492013-02-09 01:29:05 +00003807 if (return_value)
3808 m_last_broadcast_state = state;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003809
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003810 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003811 log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003812 static_cast<void*>(event_ptr), StateAsCString(state),
Jim Ingham0161b492013-02-09 01:29:05 +00003813 StateAsCString(m_last_broadcast_state),
3814 return_value ? "YES" : "NO");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003815 return return_value;
3816}
3817
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003818
3819bool
Jim Ingham372787f2012-04-07 00:00:41 +00003820Process::StartPrivateStateThread (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003821{
Greg Clayton5160ce52013-03-27 23:08:40 +00003822 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003823
Greg Clayton8b82f082011-04-12 05:54:46 +00003824 bool already_running = PrivateStateThreadIsValid ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003825 if (log)
Greg Clayton8b82f082011-04-12 05:54:46 +00003826 log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread");
3827
Jim Ingham372787f2012-04-07 00:00:41 +00003828 if (!force && already_running)
Greg Clayton8b82f082011-04-12 05:54:46 +00003829 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003830
3831 // Create a thread that watches our internal state and controls which
3832 // events make it to clients (into the DCProcess event queue).
Greg Clayton3e06bd92011-01-09 21:07:35 +00003833 char thread_name[1024];
Todd Fiala17096d72014-07-16 19:03:16 +00003834
3835 if (Host::MAX_THREAD_NAME_LENGTH <= 16)
3836 {
3837 // On platforms with abbreviated thread name lengths, choose thread names that fit within the limit.
3838 if (already_running)
3839 snprintf(thread_name, sizeof(thread_name), "intern-state-OV");
3840 else
3841 snprintf(thread_name, sizeof(thread_name), "intern-state");
3842 }
Jim Ingham372787f2012-04-07 00:00:41 +00003843 else
Todd Fiala17096d72014-07-16 19:03:16 +00003844 {
3845 if (already_running)
3846 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
3847 else
3848 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
3849 }
3850
Jim Ingham076b3042012-04-10 01:21:57 +00003851 // Create the private state thread, and start it running.
Greg Clayton3e06bd92011-01-09 21:07:35 +00003852 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Jim Ingham076b3042012-04-10 01:21:57 +00003853 bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
3854 if (success)
3855 {
3856 ResumePrivateStateThread();
3857 return true;
3858 }
3859 else
3860 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003861}
3862
3863void
3864Process::PausePrivateStateThread ()
3865{
3866 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
3867}
3868
3869void
3870Process::ResumePrivateStateThread ()
3871{
3872 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
3873}
3874
3875void
3876Process::StopPrivateStateThread ()
3877{
Greg Clayton8b82f082011-04-12 05:54:46 +00003878 if (PrivateStateThreadIsValid ())
3879 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
Jim Inghamb1e2e842012-04-12 18:49:31 +00003880 else
3881 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003882 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Jim Inghamb1e2e842012-04-12 18:49:31 +00003883 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003884 log->Printf ("Went to stop the private state thread, but it was already invalid.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00003885 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003886}
3887
3888void
3889Process::ControlPrivateStateThread (uint32_t signal)
3890{
Greg Clayton5160ce52013-03-27 23:08:40 +00003891 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892
3893 assert (signal == eBroadcastInternalStateControlStop ||
3894 signal == eBroadcastInternalStateControlPause ||
3895 signal == eBroadcastInternalStateControlResume);
3896
3897 if (log)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003898 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003899
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003900 // Signal the private state thread. First we should copy this is case the
3901 // thread starts exiting since the private state thread will NULL this out
3902 // when it exits
3903 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton2da6d492011-02-08 01:34:25 +00003904 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003905 {
3906 TimeValue timeout_time;
3907 bool timed_out;
3908
3909 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
3910
3911 timeout_time = TimeValue::Now();
3912 timeout_time.OffsetWithSeconds(2);
Jim Inghamb1e2e842012-04-12 18:49:31 +00003913 if (log)
3914 log->Printf ("Sending control event of type: %d.", signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003915 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
3916 m_private_state_control_wait.SetValue (false, eBroadcastNever);
3917
3918 if (signal == eBroadcastInternalStateControlStop)
3919 {
3920 if (timed_out)
Jim Inghamb1e2e842012-04-12 18:49:31 +00003921 {
3922 Error error;
3923 Host::ThreadCancel (private_state_thread, &error);
3924 if (log)
3925 log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString());
3926 }
3927 else
3928 {
3929 if (log)
3930 log->Printf ("The control event killed the private state thread without having to cancel.");
3931 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003932
3933 thread_result_t result = NULL;
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003934 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Clayton49182ed2010-07-22 18:34:21 +00003935 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003936 }
3937 }
Jim Inghamb1e2e842012-04-12 18:49:31 +00003938 else
3939 {
3940 if (log)
3941 log->Printf ("Private state thread already dead, no need to signal it to stop.");
3942 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003943}
3944
3945void
Jim Inghamcfc09352012-07-27 23:57:19 +00003946Process::SendAsyncInterrupt ()
3947{
3948 if (PrivateStateThreadIsValid())
3949 m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
3950 else
3951 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
3952}
3953
3954void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003955Process::HandlePrivateEvent (EventSP &event_sp)
3956{
Greg Clayton5160ce52013-03-27 23:08:40 +00003957 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham221d51c2013-05-08 00:35:16 +00003958 m_resume_requested = false;
3959
Jim Inghamaacc3182012-06-06 00:29:30 +00003960 m_currently_handling_event.SetValue(true, eBroadcastNever);
Jim Inghambb3a2832011-01-29 01:49:25 +00003961
Greg Clayton414f5d32011-01-25 02:58:48 +00003962 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003963
3964 // First check to see if anybody wants a shot at this event:
Jim Ingham754ab982011-01-29 04:05:41 +00003965 if (m_next_event_action_ap.get() != NULL)
Jim Inghambb3a2832011-01-29 01:49:25 +00003966 {
Jim Ingham754ab982011-01-29 04:05:41 +00003967 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Ingham0161b492013-02-09 01:29:05 +00003968 if (log)
3969 log->Printf ("Ran next event action, result was %d.", action_result);
3970
Jim Inghambb3a2832011-01-29 01:49:25 +00003971 switch (action_result)
3972 {
3973 case NextEventAction::eEventActionSuccess:
3974 SetNextEventAction(NULL);
3975 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003976
Jim Inghambb3a2832011-01-29 01:49:25 +00003977 case NextEventAction::eEventActionRetry:
3978 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003979
Jim Inghambb3a2832011-01-29 01:49:25 +00003980 case NextEventAction::eEventActionExit:
Jim Ingham2a5fdd42011-01-29 01:57:31 +00003981 // Handle Exiting Here. If we already got an exited event,
3982 // we should just propagate it. Otherwise, swallow this event,
3983 // and set our state to exit so the next event will kill us.
3984 if (new_state != eStateExited)
3985 {
3986 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham754ab982011-01-29 04:05:41 +00003987 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham221d51c2013-05-08 00:35:16 +00003988 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Jim Ingham2a5fdd42011-01-29 01:57:31 +00003989 SetNextEventAction(NULL);
3990 return;
3991 }
3992 SetNextEventAction(NULL);
Jim Inghambb3a2832011-01-29 01:49:25 +00003993 break;
3994 }
3995 }
3996
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003997 // See if we should broadcast this state to external clients?
3998 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003999
4000 if (should_broadcast)
4001 {
Greg Claytonb4874f12014-02-28 18:22:24 +00004002 const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004003 if (log)
4004 {
Daniel Malead01b2952012-11-29 21:49:15 +00004005 log->Printf ("Process::%s (pid = %" PRIu64 ") broadcasting new state %s (old state %s) to %s",
Greg Clayton414f5d32011-01-25 02:58:48 +00004006 __FUNCTION__,
4007 GetID(),
4008 StateAsCString(new_state),
4009 StateAsCString (GetState ()),
Greg Claytonb4874f12014-02-28 18:22:24 +00004010 is_hijacked ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004011 }
Jim Ingham9575d842011-03-11 03:53:59 +00004012 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton414f5d32011-01-25 02:58:48 +00004013 if (StateIsRunningState (new_state))
Greg Clayton44d93782014-01-27 23:43:24 +00004014 {
4015 // Only push the input handler if we aren't fowarding events,
4016 // as this means the curses GUI is in use...
4017 if (!GetTarget().GetDebugger().IsForwardingEvents())
4018 PushProcessIOHandler ();
Todd Fialaa3b89e22014-08-12 14:33:19 +00004019 m_iohandler_sync.SetValue(true, eBroadcastAlways);
Greg Clayton44d93782014-01-27 23:43:24 +00004020 }
Greg Claytonb4874f12014-02-28 18:22:24 +00004021 else if (StateIsStoppedState(new_state, false))
4022 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00004023 m_iohandler_sync.SetValue(false, eBroadcastNever);
Greg Claytonb4874f12014-02-28 18:22:24 +00004024 if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
4025 {
4026 // If the lldb_private::Debugger is handling the events, we don't
4027 // want to pop the process IOHandler here, we want to do it when
4028 // we receive the stopped event so we can carefully control when
4029 // the process IOHandler is popped because when we stop we want to
4030 // display some text stating how and why we stopped, then maybe some
4031 // process/thread/frame info, and then we want the "(lldb) " prompt
4032 // to show up. If we pop the process IOHandler here, then we will
4033 // cause the command interpreter to become the top IOHandler after
4034 // the process pops off and it will update its prompt right away...
4035 // See the Debugger.cpp file where it calls the function as
4036 // "process_sp->PopProcessIOHandler()" to see where I am talking about.
4037 // Otherwise we end up getting overlapping "(lldb) " prompts and
4038 // garbled output.
4039 //
4040 // If we aren't handling the events in the debugger (which is indicated
4041 // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or we
4042 // are hijacked, then we always pop the process IO handler manually.
4043 // Hijacking happens when the internal process state thread is running
4044 // thread plans, or when commands want to run in synchronous mode
4045 // and they call "process->WaitForProcessToStop()". An example of something
4046 // that will hijack the events is a simple expression:
4047 //
4048 // (lldb) expr (int)puts("hello")
4049 //
4050 // This will cause the internal process state thread to resume and halt
4051 // the process (and _it_ will hijack the eBroadcastBitStateChanged
4052 // events) and we do need the IO handler to be pushed and popped
4053 // correctly.
4054
4055 if (is_hijacked || m_target.GetDebugger().IsHandlingEvents() == false)
4056 PopProcessIOHandler ();
4057 }
4058 }
Jim Ingham9575d842011-03-11 03:53:59 +00004059
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004060 BroadcastEvent (event_sp);
4061 }
4062 else
4063 {
4064 if (log)
4065 {
Daniel Malead01b2952012-11-29 21:49:15 +00004066 log->Printf ("Process::%s (pid = %" PRIu64 ") suppressing state %s (old state %s): should_broadcast == false",
Greg Clayton414f5d32011-01-25 02:58:48 +00004067 __FUNCTION__,
4068 GetID(),
4069 StateAsCString(new_state),
Jason Molendafd54b362011-09-20 21:44:10 +00004070 StateAsCString (GetState ()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004071 }
4072 }
Jim Inghamaacc3182012-06-06 00:29:30 +00004073 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004074}
4075
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004076thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004077Process::PrivateStateThread (void *arg)
4078{
4079 Process *proc = static_cast<Process*> (arg);
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004080 thread_result_t result = proc->RunPrivateStateThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004081 return result;
4082}
4083
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004084thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004085Process::RunPrivateStateThread ()
4086{
Jim Ingham076b3042012-04-10 01:21:57 +00004087 bool control_only = true;
Jim Inghamb1e2e842012-04-12 18:49:31 +00004088 m_private_state_control_wait.SetValue (false, eBroadcastNever);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004089
Greg Clayton5160ce52013-03-27 23:08:40 +00004090 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004091 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004092 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...",
4093 __FUNCTION__, static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004094
4095 bool exit_now = false;
4096 while (!exit_now)
4097 {
4098 EventSP event_sp;
4099 WaitForEventsPrivate (NULL, event_sp, control_only);
4100 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
4101 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00004102 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004103 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d",
4104 __FUNCTION__, static_cast<void*>(this), GetID(),
4105 event_sp->GetType());
Jim Inghamb1e2e842012-04-12 18:49:31 +00004106
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004107 switch (event_sp->GetType())
4108 {
4109 case eBroadcastInternalStateControlStop:
4110 exit_now = true;
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004111 break; // doing any internal state management below
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004112
4113 case eBroadcastInternalStateControlPause:
4114 control_only = true;
4115 break;
4116
4117 case eBroadcastInternalStateControlResume:
4118 control_only = false;
4119 break;
4120 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004121
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004122 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004123 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004124 }
Jim Inghamcfc09352012-07-27 23:57:19 +00004125 else if (event_sp->GetType() == eBroadcastBitInterrupt)
4126 {
4127 if (m_public_state.GetValue() == eStateAttaching)
4128 {
4129 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004130 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.",
4131 __FUNCTION__, static_cast<void*>(this),
4132 GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004133 BroadcastEvent (eBroadcastBitInterrupt, NULL);
4134 }
4135 else
4136 {
4137 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004138 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.",
4139 __FUNCTION__, static_cast<void*>(this),
4140 GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004141 Halt();
4142 }
4143 continue;
4144 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004145
4146 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
4147
4148 if (internal_state != eStateInvalid)
4149 {
Greg Claytonf9b57b92013-05-10 23:48:10 +00004150 if (m_clear_thread_plans_on_stop &&
4151 StateIsStoppedState(internal_state, true))
4152 {
4153 m_clear_thread_plans_on_stop = false;
4154 m_thread_list.DiscardThreadPlans();
4155 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004156 HandlePrivateEvent (event_sp);
4157 }
4158
Greg Clayton58d1c9a2010-10-18 04:14:23 +00004159 if (internal_state == eStateInvalid ||
4160 internal_state == eStateExited ||
4161 internal_state == eStateDetached )
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004162 {
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004163 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004164 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...",
4165 __FUNCTION__, static_cast<void*>(this), GetID(),
4166 StateAsCString(internal_state));
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004167
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004168 break;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004169 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004170 }
4171
Caroline Tice20ad3c42010-10-29 21:48:37 +00004172 // Verify log is still enabled before attempting to write to it...
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004173 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004174 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...",
4175 __FUNCTION__, static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004176
Ed Maste64fad602013-07-29 20:58:06 +00004177 m_public_run_lock.SetStopped();
Greg Clayton6ed95942011-01-22 07:12:45 +00004178 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
4179 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004180 return NULL;
4181}
4182
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004183//------------------------------------------------------------------
4184// Process Event Data
4185//------------------------------------------------------------------
4186
4187Process::ProcessEventData::ProcessEventData () :
4188 EventData (),
4189 m_process_sp (),
4190 m_state (eStateInvalid),
Greg Claytonc982c762010-07-09 20:39:50 +00004191 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004192 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004193 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004194{
4195}
4196
4197Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
4198 EventData (),
4199 m_process_sp (process_sp),
4200 m_state (state),
Greg Claytonc982c762010-07-09 20:39:50 +00004201 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004202 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004203 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004204{
4205}
4206
4207Process::ProcessEventData::~ProcessEventData()
4208{
4209}
4210
4211const ConstString &
4212Process::ProcessEventData::GetFlavorString ()
4213{
4214 static ConstString g_flavor ("Process::ProcessEventData");
4215 return g_flavor;
4216}
4217
4218const ConstString &
4219Process::ProcessEventData::GetFlavor () const
4220{
4221 return ProcessEventData::GetFlavorString ();
4222}
4223
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004224void
4225Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
4226{
4227 // This function gets called twice for each event, once when the event gets pulled
Jim Inghama8604692011-05-22 21:45:01 +00004228 // off of the private process event queue, and then any number of times, first when it gets pulled off of
4229 // the public event queue, then other times when we're pretending that this is where we stopped at the
4230 // end of expression evaluation. m_update_state is used to distinguish these
4231 // three cases; it is 0 when we're just pulling it off for private handling,
Jim Ingham221d51c2013-05-08 00:35:16 +00004232 // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then.
Jim Inghama8604692011-05-22 21:45:01 +00004233 if (m_update_state != 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004234 return;
Jim Ingham0161b492013-02-09 01:29:05 +00004235
Jim Ingham221d51c2013-05-08 00:35:16 +00004236 m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr));
Jim Ingham35878c42014-04-08 21:33:21 +00004237
4238 // If this is a halt event, even if the halt stopped with some reason other than a plain interrupt (e.g. we had
4239 // already stopped for a breakpoint when the halt request came through) don't do the StopInfo actions, as they may
4240 // end up restarting the process.
4241 if (m_interrupted)
4242 return;
4243
4244 // If we're stopped and haven't restarted, then do the StopInfo actions here:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004245 if (m_state == eStateStopped && ! m_restarted)
Jim Ingham0faa43f2011-11-08 03:00:11 +00004246 {
4247 ThreadList &curr_thread_list = m_process_sp->GetThreadList();
Greg Clayton61e7a582011-12-01 23:28:38 +00004248 uint32_t num_threads = curr_thread_list.GetSize();
4249 uint32_t idx;
Greg Claytonf4b47e12010-08-04 01:40:35 +00004250
Jim Ingham4b536182011-08-09 02:12:22 +00004251 // The actions might change one of the thread's stop_info's opinions about whether we should
4252 // stop the process, so we need to query that as we go.
Jim Ingham0faa43f2011-11-08 03:00:11 +00004253
4254 // One other complication here, is that we try to catch any case where the target has run (except for expressions)
4255 // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and
4256 // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like
4257 // 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
4258 // against this list & bag out if anything differs.
Greg Clayton61e7a582011-12-01 23:28:38 +00004259 std::vector<uint32_t> thread_index_array(num_threads);
Jim Ingham0faa43f2011-11-08 03:00:11 +00004260 for (idx = 0; idx < num_threads; ++idx)
4261 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID();
4262
Jim Inghamc7078c22012-12-13 22:24:15 +00004263 // Use this to track whether we should continue from here. We will only continue the target running if
4264 // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running,
4265 // then it doesn't matter what the other threads say...
4266
4267 bool still_should_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +00004268
Jim Ingham0ad7e052013-04-25 02:04:59 +00004269 // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a
4270 // valid stop reason. In that case we should just stop, because we have no way of telling what the right
4271 // thing to do is, and it's better to let the user decide than continue behind their backs.
4272
4273 bool does_anybody_have_an_opinion = false;
4274
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004275 for (idx = 0; idx < num_threads; ++idx)
4276 {
Jim Ingham0faa43f2011-11-08 03:00:11 +00004277 curr_thread_list = m_process_sp->GetThreadList();
4278 if (curr_thread_list.GetSize() != num_threads)
4279 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004280 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004281 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004282 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 +00004283 break;
4284 }
4285
4286 lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx);
4287
4288 if (thread_sp->GetIndexID() != thread_index_array[idx])
4289 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004290 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004291 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004292 log->Printf("The thread at position %u changed from %u to %u while processing event.",
Jim Ingham87c665f2011-12-01 20:26:15 +00004293 idx,
4294 thread_index_array[idx],
4295 thread_sp->GetIndexID());
Jim Ingham0faa43f2011-11-08 03:00:11 +00004296 break;
4297 }
4298
Jim Inghamb15bfc72010-10-20 00:39:53 +00004299 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00004300 if (stop_info_sp && stop_info_sp->IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004301 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004302 does_anybody_have_an_opinion = true;
Jim Ingham0161b492013-02-09 01:29:05 +00004303 bool this_thread_wants_to_stop;
4304 if (stop_info_sp->GetOverrideShouldStop())
Jim Ingham4b536182011-08-09 02:12:22 +00004305 {
Jim Ingham0161b492013-02-09 01:29:05 +00004306 this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue();
4307 }
4308 else
4309 {
4310 stop_info_sp->PerformAction(event_ptr);
4311 // The stop action might restart the target. If it does, then we want to mark that in the
4312 // event so that whoever is receiving it will know to wait for the running event and reflect
4313 // that state appropriately.
4314 // We also need to stop processing actions, since they aren't expecting the target to be running.
4315
4316 // FIXME: we might have run.
4317 if (stop_info_sp->HasTargetRunSinceMe())
4318 {
4319 SetRestarted (true);
4320 break;
4321 }
4322
4323 this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr);
Jim Ingham4b536182011-08-09 02:12:22 +00004324 }
Jim Inghamc7078c22012-12-13 22:24:15 +00004325
Jim Inghamc7078c22012-12-13 22:24:15 +00004326 if (still_should_stop == false)
4327 still_should_stop = this_thread_wants_to_stop;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004328 }
4329 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00004330
Ashok Thirumurthicf7c55e2013-04-18 14:38:20 +00004331
Jim Inghama8ca6e22013-05-03 23:04:37 +00004332 if (!GetRestarted())
Jim Ingham9575d842011-03-11 03:53:59 +00004333 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004334 if (!still_should_stop && does_anybody_have_an_opinion)
Jim Ingham4b536182011-08-09 02:12:22 +00004335 {
4336 // We've been asked to continue, so do that here.
Jim Ingham9575d842011-03-11 03:53:59 +00004337 SetRestarted(true);
Jim Ingham3b8285d2012-04-19 01:40:33 +00004338 // Use the public resume method here, since this is just
4339 // extending a public resume.
Jim Ingham0161b492013-02-09 01:29:05 +00004340 m_process_sp->PrivateResume();
Jim Ingham4b536182011-08-09 02:12:22 +00004341 }
4342 else
4343 {
4344 // If we didn't restart, run the Stop Hooks here:
4345 // They might also restart the target, so watch for that.
4346 m_process_sp->GetTarget().RunStopHooks();
4347 if (m_process_sp->GetPrivateState() == eStateRunning)
4348 SetRestarted(true);
4349 }
Jim Ingham9575d842011-03-11 03:53:59 +00004350 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004351 }
4352}
4353
4354void
4355Process::ProcessEventData::Dump (Stream *s) const
4356{
4357 if (m_process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004358 s->Printf(" process = %p (pid = %" PRIu64 "), ",
4359 static_cast<void*>(m_process_sp.get()), m_process_sp->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004360
Greg Clayton8b82f082011-04-12 05:54:46 +00004361 s->Printf("state = %s", StateAsCString(GetState()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004362}
4363
4364const Process::ProcessEventData *
4365Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
4366{
4367 if (event_ptr)
4368 {
4369 const EventData *event_data = event_ptr->GetData();
4370 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
4371 return static_cast <const ProcessEventData *> (event_ptr->GetData());
4372 }
4373 return NULL;
4374}
4375
4376ProcessSP
4377Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
4378{
4379 ProcessSP process_sp;
4380 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4381 if (data)
4382 process_sp = data->GetProcessSP();
4383 return process_sp;
4384}
4385
4386StateType
4387Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
4388{
4389 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4390 if (data == NULL)
4391 return eStateInvalid;
4392 else
4393 return data->GetState();
4394}
4395
4396bool
4397Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
4398{
4399 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4400 if (data == NULL)
4401 return false;
4402 else
4403 return data->GetRestarted();
4404}
4405
4406void
4407Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
4408{
4409 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4410 if (data != NULL)
4411 data->SetRestarted(new_value);
4412}
4413
Jim Ingham0161b492013-02-09 01:29:05 +00004414size_t
4415Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr)
4416{
4417 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4418 if (data != NULL)
4419 return data->GetNumRestartedReasons();
4420 else
4421 return 0;
4422}
4423
4424const char *
4425Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx)
4426{
4427 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4428 if (data != NULL)
4429 return data->GetRestartedReasonAtIndex(idx);
4430 else
4431 return NULL;
4432}
4433
4434void
4435Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason)
4436{
4437 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4438 if (data != NULL)
4439 data->AddRestartedReason(reason);
4440}
4441
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004442bool
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004443Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
4444{
4445 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4446 if (data == NULL)
4447 return false;
4448 else
4449 return data->GetInterrupted ();
4450}
4451
4452void
4453Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
4454{
4455 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4456 if (data != NULL)
4457 data->SetInterrupted(new_value);
4458}
4459
4460bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004461Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
4462{
4463 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4464 if (data)
4465 {
4466 data->SetUpdateStateOnRemoval();
4467 return true;
4468 }
4469 return false;
4470}
4471
Greg Claytond9e416c2012-02-18 05:35:26 +00004472lldb::TargetSP
4473Process::CalculateTarget ()
4474{
4475 return m_target.shared_from_this();
4476}
4477
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004478void
Greg Clayton0603aa92010-10-04 01:05:56 +00004479Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004480{
Greg Claytonc14ee322011-09-22 04:58:26 +00004481 exe_ctx.SetTargetPtr (&m_target);
4482 exe_ctx.SetProcessPtr (this);
4483 exe_ctx.SetThreadPtr(NULL);
4484 exe_ctx.SetFramePtr (NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004485}
4486
Greg Claytone996fd32011-03-08 22:40:15 +00004487//uint32_t
4488//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
4489//{
4490// return 0;
4491//}
4492//
4493//ArchSpec
4494//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
4495//{
4496// return Host::GetArchSpecForExistingProcess (pid);
4497//}
4498//
4499//ArchSpec
4500//Process::GetArchSpecForExistingProcess (const char *process_name)
4501//{
4502// return Host::GetArchSpecForExistingProcess (process_name);
4503//}
4504//
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004505void
4506Process::AppendSTDOUT (const char * s, size_t len)
4507{
Greg Clayton3af9ea52010-11-18 05:57:03 +00004508 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004509 m_stdout_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004510 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState()));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004511}
4512
4513void
Greg Clayton93e86192011-11-13 04:45:22 +00004514Process::AppendSTDERR (const char * s, size_t len)
4515{
4516 Mutex::Locker locker (m_stdio_communication_mutex);
4517 m_stderr_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004518 BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState()));
Greg Clayton93e86192011-11-13 04:45:22 +00004519}
4520
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004521void
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004522Process::BroadcastAsyncProfileData(const std::string &one_profile_data)
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004523{
4524 Mutex::Locker locker (m_profile_data_comm_mutex);
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004525 m_profile_data.push_back(one_profile_data);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004526 BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState()));
4527}
4528
4529size_t
4530Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error)
4531{
4532 Mutex::Locker locker(m_profile_data_comm_mutex);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004533 if (m_profile_data.empty())
4534 return 0;
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004535
4536 std::string &one_profile_data = m_profile_data.front();
4537 size_t bytes_available = one_profile_data.size();
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004538 if (bytes_available > 0)
4539 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004540 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004541 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004542 log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")",
4543 static_cast<void*>(buf),
4544 static_cast<uint64_t>(buf_size));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004545 if (bytes_available > buf_size)
4546 {
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004547 memcpy(buf, one_profile_data.c_str(), buf_size);
4548 one_profile_data.erase(0, buf_size);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004549 bytes_available = buf_size;
4550 }
4551 else
4552 {
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004553 memcpy(buf, one_profile_data.c_str(), bytes_available);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004554 m_profile_data.erase(m_profile_data.begin());
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004555 }
4556 }
4557 return bytes_available;
4558}
4559
4560
Greg Clayton93e86192011-11-13 04:45:22 +00004561//------------------------------------------------------------------
4562// Process STDIO
4563//------------------------------------------------------------------
4564
4565size_t
4566Process::GetSTDOUT (char *buf, size_t buf_size, Error &error)
4567{
4568 Mutex::Locker locker(m_stdio_communication_mutex);
4569 size_t bytes_available = m_stdout_data.size();
4570 if (bytes_available > 0)
4571 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004572 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004573 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004574 log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")",
4575 static_cast<void*>(buf),
4576 static_cast<uint64_t>(buf_size));
Greg Clayton93e86192011-11-13 04:45:22 +00004577 if (bytes_available > buf_size)
4578 {
4579 memcpy(buf, m_stdout_data.c_str(), buf_size);
4580 m_stdout_data.erase(0, buf_size);
4581 bytes_available = buf_size;
4582 }
4583 else
4584 {
4585 memcpy(buf, m_stdout_data.c_str(), bytes_available);
4586 m_stdout_data.clear();
4587 }
4588 }
4589 return bytes_available;
4590}
4591
4592
4593size_t
4594Process::GetSTDERR (char *buf, size_t buf_size, Error &error)
4595{
4596 Mutex::Locker locker(m_stdio_communication_mutex);
4597 size_t bytes_available = m_stderr_data.size();
4598 if (bytes_available > 0)
4599 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004600 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004601 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004602 log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")",
4603 static_cast<void*>(buf),
4604 static_cast<uint64_t>(buf_size));
Greg Clayton93e86192011-11-13 04:45:22 +00004605 if (bytes_available > buf_size)
4606 {
4607 memcpy(buf, m_stderr_data.c_str(), buf_size);
4608 m_stderr_data.erase(0, buf_size);
4609 bytes_available = buf_size;
4610 }
4611 else
4612 {
4613 memcpy(buf, m_stderr_data.c_str(), bytes_available);
4614 m_stderr_data.clear();
4615 }
4616 }
4617 return bytes_available;
4618}
4619
4620void
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004621Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
4622{
4623 Process *process = (Process *) baton;
4624 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
4625}
4626
Greg Clayton44d93782014-01-27 23:43:24 +00004627class IOHandlerProcessSTDIO :
4628 public IOHandler
4629{
4630public:
4631 IOHandlerProcessSTDIO (Process *process,
4632 int write_fd) :
4633 IOHandler(process->GetTarget().GetDebugger()),
4634 m_process (process),
4635 m_read_file (),
4636 m_write_file (write_fd, false),
Greg Clayton100eb932014-07-02 21:10:39 +00004637 m_pipe ()
Greg Clayton44d93782014-01-27 23:43:24 +00004638 {
4639 m_read_file.SetDescriptor(GetInputFD(), false);
4640 }
4641
4642 virtual
4643 ~IOHandlerProcessSTDIO ()
4644 {
4645
4646 }
4647
4648 bool
4649 OpenPipes ()
4650 {
Greg Clayton100eb932014-07-02 21:10:39 +00004651 if (m_pipe.IsValid())
Greg Clayton44d93782014-01-27 23:43:24 +00004652 return true;
Greg Clayton100eb932014-07-02 21:10:39 +00004653 return m_pipe.Open();
Greg Clayton44d93782014-01-27 23:43:24 +00004654 }
4655
4656 void
4657 ClosePipes()
4658 {
Greg Clayton100eb932014-07-02 21:10:39 +00004659 m_pipe.Close();
Greg Clayton44d93782014-01-27 23:43:24 +00004660 }
4661
4662 // Each IOHandler gets to run until it is done. It should read data
4663 // from the "in" and place output into "out" and "err and return
4664 // when done.
4665 virtual void
4666 Run ()
4667 {
4668 if (m_read_file.IsValid() && m_write_file.IsValid())
4669 {
4670 SetIsDone(false);
4671 if (OpenPipes())
4672 {
4673 const int read_fd = m_read_file.GetDescriptor();
Greg Clayton100eb932014-07-02 21:10:39 +00004674 const int pipe_read_fd = m_pipe.GetReadFileDescriptor();
Greg Clayton44d93782014-01-27 23:43:24 +00004675 TerminalState terminal_state;
4676 terminal_state.Save (read_fd, false);
4677 Terminal terminal(read_fd);
4678 terminal.SetCanonical(false);
4679 terminal.SetEcho(false);
Deepak Panickal914b8d92014-01-31 18:48:46 +00004680// FD_ZERO, FD_SET are not supported on windows
Hafiz Abid Qadeer6eff1012014-03-12 10:45:23 +00004681#ifndef _WIN32
Greg Clayton44d93782014-01-27 23:43:24 +00004682 while (!GetIsDone())
4683 {
4684 fd_set read_fdset;
4685 FD_ZERO (&read_fdset);
4686 FD_SET (read_fd, &read_fdset);
4687 FD_SET (pipe_read_fd, &read_fdset);
4688 const int nfds = std::max<int>(read_fd, pipe_read_fd) + 1;
4689 int num_set_fds = select (nfds, &read_fdset, NULL, NULL, NULL);
4690 if (num_set_fds < 0)
4691 {
4692 const int select_errno = errno;
4693
4694 if (select_errno != EINTR)
4695 SetIsDone(true);
4696 }
4697 else if (num_set_fds > 0)
4698 {
4699 char ch = 0;
4700 size_t n;
4701 if (FD_ISSET (read_fd, &read_fdset))
4702 {
4703 n = 1;
4704 if (m_read_file.Read(&ch, n).Success() && n == 1)
4705 {
4706 if (m_write_file.Write(&ch, n).Fail() || n != 1)
4707 SetIsDone(true);
4708 }
4709 else
4710 SetIsDone(true);
4711 }
4712 if (FD_ISSET (pipe_read_fd, &read_fdset))
4713 {
4714 // Consume the interrupt byte
Greg Clayton100eb932014-07-02 21:10:39 +00004715 if (m_pipe.Read (&ch, 1) == 1)
Greg Clayton19e11352014-02-26 22:47:33 +00004716 {
Greg Clayton100eb932014-07-02 21:10:39 +00004717 switch (ch)
4718 {
4719 case 'q':
4720 SetIsDone(true);
4721 break;
4722 case 'i':
4723 if (StateIsRunningState(m_process->GetState()))
4724 m_process->Halt();
4725 break;
4726 }
Greg Clayton19e11352014-02-26 22:47:33 +00004727 }
Greg Clayton44d93782014-01-27 23:43:24 +00004728 }
4729 }
4730 }
Deepak Panickal914b8d92014-01-31 18:48:46 +00004731#endif
Greg Clayton44d93782014-01-27 23:43:24 +00004732 terminal_state.Restore();
4733
4734 }
4735 else
4736 SetIsDone(true);
4737 }
4738 else
4739 SetIsDone(true);
4740 }
4741
4742 // Hide any characters that have been displayed so far so async
4743 // output can be displayed. Refresh() will be called after the
4744 // output has been displayed.
4745 virtual void
4746 Hide ()
4747 {
4748
4749 }
4750 // Called when the async output has been received in order to update
4751 // the input reader (refresh the prompt and redisplay any current
4752 // line(s) that are being edited
4753 virtual void
4754 Refresh ()
4755 {
4756
4757 }
Greg Claytone68f5d62014-02-24 22:50:57 +00004758
Greg Clayton44d93782014-01-27 23:43:24 +00004759 virtual void
Greg Claytone68f5d62014-02-24 22:50:57 +00004760 Cancel ()
Greg Clayton44d93782014-01-27 23:43:24 +00004761 {
Greg Clayton19e11352014-02-26 22:47:33 +00004762 char ch = 'q'; // Send 'q' for quit
Greg Clayton100eb932014-07-02 21:10:39 +00004763 m_pipe.Write (&ch, 1);
Greg Clayton44d93782014-01-27 23:43:24 +00004764 }
Greg Claytone68f5d62014-02-24 22:50:57 +00004765
Greg Claytonf0066ad2014-05-02 00:45:31 +00004766 virtual bool
Greg Claytone68f5d62014-02-24 22:50:57 +00004767 Interrupt ()
4768 {
Greg Clayton19e11352014-02-26 22:47:33 +00004769 // Do only things that are safe to do in an interrupt context (like in
4770 // a SIGINT handler), like write 1 byte to a file descriptor. This will
4771 // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte
4772 // that was written to the pipe and then call m_process->Halt() from a
4773 // much safer location in code.
Greg Clayton0fdd3ae2014-07-16 21:05:41 +00004774 if (m_active)
4775 {
4776 char ch = 'i'; // Send 'i' for interrupt
4777 return m_pipe.Write (&ch, 1) == 1;
4778 }
4779 else
4780 {
4781 // This IOHandler might be pushed on the stack, but not being run currently
4782 // so do the right thing if we aren't actively watching for STDIN by sending
4783 // the interrupt to the process. Otherwise the write to the pipe above would
4784 // do nothing. This can happen when the command interpreter is running and
4785 // gets a "expression ...". It will be on the IOHandler thread and sending
4786 // the input is complete to the delegate which will cause the expression to
4787 // run, which will push the process IO handler, but not run it.
4788
4789 if (StateIsRunningState(m_process->GetState()))
4790 {
4791 m_process->SendAsyncInterrupt();
4792 return true;
4793 }
4794 }
4795 return false;
Greg Claytone68f5d62014-02-24 22:50:57 +00004796 }
Greg Clayton44d93782014-01-27 23:43:24 +00004797
4798 virtual void
4799 GotEOF()
4800 {
4801
4802 }
4803
4804protected:
4805 Process *m_process;
4806 File m_read_file; // Read from this file (usually actual STDIN for LLDB
4807 File m_write_file; // Write to this file (usually the master pty for getting io to debuggee)
Greg Clayton100eb932014-07-02 21:10:39 +00004808 Pipe m_pipe;
Greg Clayton44d93782014-01-27 23:43:24 +00004809};
4810
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004811void
Greg Clayton44d93782014-01-27 23:43:24 +00004812Process::SetSTDIOFileDescriptor (int fd)
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004813{
4814 // First set up the Read Thread for reading/handling process I/O
4815
Greg Clayton44d93782014-01-27 23:43:24 +00004816 std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (fd, true));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004817
4818 if (conn_ap.get())
4819 {
4820 m_stdio_communication.SetConnection (conn_ap.release());
4821 if (m_stdio_communication.IsConnected())
4822 {
4823 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
4824 m_stdio_communication.StartReadThread();
4825
4826 // Now read thread is set up, set up input reader.
4827
4828 if (!m_process_input_reader.get())
Greg Clayton44d93782014-01-27 23:43:24 +00004829 m_process_input_reader.reset (new IOHandlerProcessSTDIO (this, fd));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004830 }
4831 }
4832}
4833
Greg Claytonb4874f12014-02-28 18:22:24 +00004834bool
Greg Clayton6fea17e2014-03-03 19:15:20 +00004835Process::ProcessIOHandlerIsActive ()
4836{
4837 IOHandlerSP io_handler_sp (m_process_input_reader);
4838 if (io_handler_sp)
4839 return m_target.GetDebugger().IsTopIOHandler (io_handler_sp);
4840 return false;
4841}
4842bool
Greg Clayton44d93782014-01-27 23:43:24 +00004843Process::PushProcessIOHandler ()
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004844{
Greg Clayton44d93782014-01-27 23:43:24 +00004845 IOHandlerSP io_handler_sp (m_process_input_reader);
4846 if (io_handler_sp)
4847 {
4848 io_handler_sp->SetIsDone(false);
4849 m_target.GetDebugger().PushIOHandler (io_handler_sp);
Greg Claytonb4874f12014-02-28 18:22:24 +00004850 return true;
Greg Clayton44d93782014-01-27 23:43:24 +00004851 }
Greg Claytonb4874f12014-02-28 18:22:24 +00004852 return false;
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004853}
4854
Greg Claytonb4874f12014-02-28 18:22:24 +00004855bool
Greg Clayton44d93782014-01-27 23:43:24 +00004856Process::PopProcessIOHandler ()
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004857{
Greg Clayton44d93782014-01-27 23:43:24 +00004858 IOHandlerSP io_handler_sp (m_process_input_reader);
4859 if (io_handler_sp)
Greg Claytonb4874f12014-02-28 18:22:24 +00004860 return m_target.GetDebugger().PopIOHandler (io_handler_sp);
4861 return false;
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004862}
4863
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004864// The process needs to know about installed plug-ins
Greg Clayton99d0faf2010-11-18 23:32:35 +00004865void
Caroline Tice20bd37f2011-03-10 22:14:10 +00004866Process::SettingsInitialize ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004867{
Greg Clayton6920b522012-08-22 18:39:03 +00004868 Thread::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00004869}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004870
Greg Clayton99d0faf2010-11-18 23:32:35 +00004871void
Caroline Tice20bd37f2011-03-10 22:14:10 +00004872Process::SettingsTerminate ()
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004873{
Greg Clayton6920b522012-08-22 18:39:03 +00004874 Thread::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00004875}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004876
Jim Ingham1624a2d2014-05-05 02:26:40 +00004877ExpressionResults
Jim Inghamf48169b2010-11-30 02:22:11 +00004878Process::RunThreadPlan (ExecutionContext &exe_ctx,
Jim Ingham372787f2012-04-07 00:00:41 +00004879 lldb::ThreadPlanSP &thread_plan_sp,
Jim Ingham6fbc48b2013-11-07 00:11:47 +00004880 const EvaluateExpressionOptions &options,
Jim Inghamf48169b2010-11-30 02:22:11 +00004881 Stream &errors)
4882{
Jim Ingham8646d3c2014-05-05 02:47:44 +00004883 ExpressionResults return_value = eExpressionSetupError;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004884
Jim Ingham77787032011-01-20 02:03:18 +00004885 if (thread_plan_sp.get() == NULL)
4886 {
4887 errors.Printf("RunThreadPlan called with empty thread plan.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004888 return eExpressionSetupError;
Jim Ingham77787032011-01-20 02:03:18 +00004889 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004890
Jim Ingham7d7931d2013-03-28 00:05:34 +00004891 if (!thread_plan_sp->ValidatePlan(NULL))
4892 {
4893 errors.Printf ("RunThreadPlan called with an invalid thread plan.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004894 return eExpressionSetupError;
Jim Ingham7d7931d2013-03-28 00:05:34 +00004895 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004896
Greg Claytonc14ee322011-09-22 04:58:26 +00004897 if (exe_ctx.GetProcessPtr() != this)
4898 {
4899 errors.Printf("RunThreadPlan called on wrong process.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004900 return eExpressionSetupError;
Greg Claytonc14ee322011-09-22 04:58:26 +00004901 }
4902
4903 Thread *thread = exe_ctx.GetThreadPtr();
4904 if (thread == NULL)
4905 {
4906 errors.Printf("RunThreadPlan called with invalid thread.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004907 return eExpressionSetupError;
Greg Claytonc14ee322011-09-22 04:58:26 +00004908 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004909
Jim Ingham17e5c4e2011-05-17 22:24:54 +00004910 // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes.
4911 // For that to be true the plan can't be private - since private plans suppress themselves in the
4912 // GetCompletedPlan call.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004913
Jim Ingham17e5c4e2011-05-17 22:24:54 +00004914 bool orig_plan_private = thread_plan_sp->GetPrivate();
4915 thread_plan_sp->SetPrivate(false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004916
Jim Ingham444586b2011-01-24 06:34:17 +00004917 if (m_private_state.GetValue() != eStateStopped)
4918 {
4919 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004920 return eExpressionSetupError;
Jim Ingham444586b2011-01-24 06:34:17 +00004921 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004922
Jim Ingham66243842011-08-13 00:56:10 +00004923 // Save the thread & frame from the exe_ctx for restoration after we run
Greg Claytonc14ee322011-09-22 04:58:26 +00004924 const uint32_t thread_idx_id = thread->GetIndexID();
Jason Molendab57e4a12013-11-04 09:33:30 +00004925 StackFrameSP selected_frame_sp = thread->GetSelectedFrame();
Jim Ingham11b0e052013-02-19 23:22:45 +00004926 if (!selected_frame_sp)
4927 {
4928 thread->SetSelectedFrame(0);
4929 selected_frame_sp = thread->GetSelectedFrame();
4930 if (!selected_frame_sp)
4931 {
4932 errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id);
Jim Ingham8646d3c2014-05-05 02:47:44 +00004933 return eExpressionSetupError;
Jim Ingham11b0e052013-02-19 23:22:45 +00004934 }
4935 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004936
Jim Ingham11b0e052013-02-19 23:22:45 +00004937 StackID ctx_frame_id = selected_frame_sp->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00004938
4939 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
4940 // so we should arrange to reset them as well.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004941
Greg Claytonc14ee322011-09-22 04:58:26 +00004942 lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004943
Jim Ingham66243842011-08-13 00:56:10 +00004944 uint32_t selected_tid;
4945 StackID selected_stack_id;
Greg Clayton762f7132011-09-18 18:59:15 +00004946 if (selected_thread_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00004947 {
4948 selected_tid = selected_thread_sp->GetIndexID();
Jim Ingham66243842011-08-13 00:56:10 +00004949 selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00004950 }
4951 else
4952 {
4953 selected_tid = LLDB_INVALID_THREAD_ID;
4954 }
4955
Jim Ingham372787f2012-04-07 00:00:41 +00004956 lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD;
Jim Ingham076b3042012-04-10 01:21:57 +00004957 lldb::StateType old_state;
4958 lldb::ThreadPlanSP stopper_base_plan_sp;
Jim Ingham372787f2012-04-07 00:00:41 +00004959
Greg Clayton5160ce52013-03-27 23:08:40 +00004960 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham372787f2012-04-07 00:00:41 +00004961 if (Host::GetCurrentThread() == m_private_state_thread)
4962 {
Jim Ingham076b3042012-04-10 01:21:57 +00004963 // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since
4964 // we are the thread that is generating public events.
Jim Ingham372787f2012-04-07 00:00:41 +00004965 // 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 +00004966 // we are fielding public events here.
4967 if (log)
Jason Molendad251c9d2012-11-17 01:41:04 +00004968 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 +00004969
Jim Ingham372787f2012-04-07 00:00:41 +00004970 backup_private_state_thread = m_private_state_thread;
Jim Ingham076b3042012-04-10 01:21:57 +00004971
4972 // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop,
4973 // returning control here.
4974 // But in the normal course of things, the plan above us on the stack would be given a shot at the stop
4975 // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack
4976 // before the plan we want to run. Since base plans always stop and return control to the user, that will
4977 // do just what we want.
4978 stopper_base_plan_sp.reset(new ThreadPlanBase (*thread));
4979 thread->QueueThreadPlan (stopper_base_plan_sp, false);
4980 // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly.
4981 old_state = m_public_state.GetValue();
4982 m_public_state.SetValueNoLock(eStateStopped);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004983
Jim Ingham076b3042012-04-10 01:21:57 +00004984 // Now spin up the private state thread:
Jim Ingham372787f2012-04-07 00:00:41 +00004985 StartPrivateStateThread(true);
4986 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004987
Jim Ingham372787f2012-04-07 00:00:41 +00004988 thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense?
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004989
Jim Ingham6fbc48b2013-11-07 00:11:47 +00004990 if (options.GetDebug())
4991 {
4992 // In this case, we aren't actually going to run, we just want to stop right away.
4993 // Flush this thread so we will refetch the stacks and show the correct backtrace.
4994 // FIXME: To make this prettier we should invent some stop reason for this, but that
4995 // is only cosmetic, and this functionality is only of use to lldb developers who can
4996 // live with not pretty...
4997 thread->Flush();
Jim Ingham8646d3c2014-05-05 02:47:44 +00004998 return eExpressionStoppedForDebug;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00004999 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005000
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00005001 Listener listener("lldb.process.listener.run-thread-plan");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005002
Sean Callanana46ec452012-07-11 21:31:24 +00005003 lldb::EventSP event_to_broadcast_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005004
Jim Ingham77787032011-01-20 02:03:18 +00005005 {
Sean Callanana46ec452012-07-11 21:31:24 +00005006 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
5007 // restored on exit to the function.
5008 //
5009 // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event
5010 // is put into event_to_broadcast_sp for rebroadcasting.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005011
Sean Callanana46ec452012-07-11 21:31:24 +00005012 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005013
Jim Inghamf48169b2010-11-30 02:22:11 +00005014 if (log)
Jim Ingham0f16e732011-02-08 05:20:59 +00005015 {
5016 StreamString s;
Sean Callanana46ec452012-07-11 21:31:24 +00005017 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Daniel Malead01b2952012-11-29 21:49:15 +00005018 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 " to run thread plan \"%s\".",
Sean Callanana46ec452012-07-11 21:31:24 +00005019 thread->GetIndexID(),
5020 thread->GetID(),
5021 s.GetData());
5022 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005023
Sean Callanana46ec452012-07-11 21:31:24 +00005024 bool got_event;
5025 lldb::EventSP event_sp;
5026 lldb::StateType stop_state = lldb::eStateInvalid;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005027
Sean Callanana46ec452012-07-11 21:31:24 +00005028 TimeValue* timeout_ptr = NULL;
5029 TimeValue real_timeout;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005030
Jim Ingham0161b492013-02-09 01:29:05 +00005031 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 +00005032 bool do_resume = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005033 bool handle_running_event = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +00005034 const uint64_t default_one_thread_timeout_usec = 250000;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005035
Jim Ingham0161b492013-02-09 01:29:05 +00005036 // This is just for accounting:
5037 uint32_t num_resumes = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005038
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005039 uint32_t timeout_usec = options.GetTimeoutUsec();
Jim Inghamfd95f892014-04-22 01:41:52 +00005040 uint32_t one_thread_timeout_usec;
5041 uint32_t all_threads_timeout_usec = 0;
Jim Inghamfe1c3422014-04-16 02:24:48 +00005042
5043 // If we are going to run all threads the whole time, or if we are only going to run one thread,
5044 // then we don't need the first timeout. So we set the final timeout, and pretend we are after the
5045 // first timeout already.
5046
5047 if (!options.GetStopOthers() || !options.GetTryAllThreads())
Jim Ingham286fb1e2014-02-28 02:52:06 +00005048 {
5049 before_first_timeout = false;
Jim Inghamfd95f892014-04-22 01:41:52 +00005050 one_thread_timeout_usec = 0;
5051 all_threads_timeout_usec = timeout_usec;
Jim Ingham286fb1e2014-02-28 02:52:06 +00005052 }
Jim Inghamfe1c3422014-04-16 02:24:48 +00005053 else
Jim Ingham0161b492013-02-09 01:29:05 +00005054 {
Jim Inghamfd95f892014-04-22 01:41:52 +00005055 uint32_t option_one_thread_timeout = options.GetOneThreadTimeoutUsec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005056
Jim Ingham914f4e72014-03-28 21:58:28 +00005057 // If the overall wait is forever, then we only need to set the one thread timeout:
5058 if (timeout_usec == 0)
5059 {
Ed Maste801335c2014-03-31 19:28:14 +00005060 if (option_one_thread_timeout != 0)
Jim Inghamfd95f892014-04-22 01:41:52 +00005061 one_thread_timeout_usec = option_one_thread_timeout;
Jim Ingham914f4e72014-03-28 21:58:28 +00005062 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005063 one_thread_timeout_usec = default_one_thread_timeout_usec;
Jim Ingham914f4e72014-03-28 21:58:28 +00005064 }
Jim Ingham0161b492013-02-09 01:29:05 +00005065 else
5066 {
Jim Ingham914f4e72014-03-28 21:58:28 +00005067 // Otherwise, if the one thread timeout is set, make sure it isn't longer than the overall timeout,
5068 // and use it, otherwise use half the total timeout, bounded by the default_one_thread_timeout_usec.
5069 uint64_t computed_one_thread_timeout;
5070 if (option_one_thread_timeout != 0)
5071 {
5072 if (timeout_usec < option_one_thread_timeout)
5073 {
5074 errors.Printf("RunThreadPlan called without one thread timeout greater than total timeout");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005075 return eExpressionSetupError;
Jim Ingham914f4e72014-03-28 21:58:28 +00005076 }
5077 computed_one_thread_timeout = option_one_thread_timeout;
5078 }
5079 else
5080 {
5081 computed_one_thread_timeout = timeout_usec / 2;
5082 if (computed_one_thread_timeout > default_one_thread_timeout_usec)
5083 computed_one_thread_timeout = default_one_thread_timeout_usec;
5084 }
Jim Inghamfd95f892014-04-22 01:41:52 +00005085 one_thread_timeout_usec = computed_one_thread_timeout;
5086 all_threads_timeout_usec = timeout_usec - one_thread_timeout_usec;
5087
Jim Ingham0161b492013-02-09 01:29:05 +00005088 }
Jim Ingham0161b492013-02-09 01:29:05 +00005089 }
Jim Inghamfe1c3422014-04-16 02:24:48 +00005090
5091 if (log)
Jim Inghamfd95f892014-04-22 01:41:52 +00005092 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 +00005093 options.GetStopOthers(),
5094 options.GetTryAllThreads(),
Jim Inghamfd95f892014-04-22 01:41:52 +00005095 before_first_timeout,
5096 one_thread_timeout_usec,
5097 all_threads_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005098
Jim Ingham1460e4b2014-01-10 23:46:59 +00005099 // This isn't going to work if there are unfetched events on the queue.
5100 // Are there cases where we might want to run the remaining events here, and then try to
5101 // call the function? That's probably being too tricky for our own good.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005102
Jim Ingham1460e4b2014-01-10 23:46:59 +00005103 Event *other_events = listener.PeekAtNextEvent();
5104 if (other_events != NULL)
5105 {
5106 errors.Printf("Calling RunThreadPlan with pending events on the queue.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005107 return eExpressionSetupError;
Jim Ingham1460e4b2014-01-10 23:46:59 +00005108 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005109
Jim Ingham1460e4b2014-01-10 23:46:59 +00005110 // We also need to make sure that the next event is delivered. We might be calling a function as part of
5111 // a thread plan, in which case the last delivered event could be the running event, and we don't want
5112 // event coalescing to cause us to lose OUR running event...
5113 ForceNextEventDelivery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005114
Jim Ingham8559a352012-11-26 23:52:18 +00005115 // This while loop must exit out the bottom, there's cleanup that we need to do when we are done.
5116 // So don't call return anywhere within it.
Jim Ingham35878c42014-04-08 21:33:21 +00005117
5118#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
5119 // It's pretty much impossible to write test cases for things like:
5120 // One thread timeout expires, I go to halt, but the process already stopped
5121 // on the function call stop breakpoint. Turning on this define will make us not
5122 // fetch the first event till after the halt. So if you run a quick function, it will have
5123 // completed, and the completion event will be waiting, when you interrupt for halt.
5124 // The expression evaluation should still succeed.
5125 bool miss_first_event = true;
5126#endif
Jim Inghamfd95f892014-04-22 01:41:52 +00005127 TimeValue one_thread_timeout;
5128 TimeValue final_timeout;
5129
Jim Ingham35878c42014-04-08 21:33:21 +00005130
Sean Callanana46ec452012-07-11 21:31:24 +00005131 while (1)
5132 {
5133 // We usually want to resume the process if we get to the top of the loop.
5134 // The only exception is if we get two running events with no intervening
5135 // stop, which can happen, we will just wait for then next stop event.
Jim Ingham0161b492013-02-09 01:29:05 +00005136 if (log)
5137 log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.",
5138 do_resume,
5139 handle_running_event,
5140 before_first_timeout);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005141
Jim Ingham184e9812013-01-15 02:47:48 +00005142 if (do_resume || handle_running_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005143 {
5144 // Do the initial resume and wait for the running event before going further.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005145
Jim Ingham184e9812013-01-15 02:47:48 +00005146 if (do_resume)
Sean Callanana46ec452012-07-11 21:31:24 +00005147 {
Jim Ingham0161b492013-02-09 01:29:05 +00005148 num_resumes++;
Jim Ingham184e9812013-01-15 02:47:48 +00005149 Error resume_error = PrivateResume ();
5150 if (!resume_error.Success())
5151 {
Jim Ingham0161b492013-02-09 01:29:05 +00005152 errors.Printf("Error resuming inferior the %d time: \"%s\".\n",
5153 num_resumes,
5154 resume_error.AsCString());
Jim Ingham8646d3c2014-05-05 02:47:44 +00005155 return_value = eExpressionSetupError;
Jim Ingham184e9812013-01-15 02:47:48 +00005156 break;
5157 }
Sean Callanana46ec452012-07-11 21:31:24 +00005158 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005159
Jim Ingham0161b492013-02-09 01:29:05 +00005160 TimeValue resume_timeout = TimeValue::Now();
5161 resume_timeout.OffsetWithMicroSeconds(500000);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005162
Jim Ingham0161b492013-02-09 01:29:05 +00005163 got_event = listener.WaitForEvent(&resume_timeout, event_sp);
Sean Callanana46ec452012-07-11 21:31:24 +00005164 if (!got_event)
5165 {
5166 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005167 log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.",
5168 num_resumes);
Sean Callanana46ec452012-07-11 21:31:24 +00005169
Jim Ingham0161b492013-02-09 01:29:05 +00005170 errors.Printf("Didn't get any event after resume %d, exiting.", num_resumes);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005171 return_value = eExpressionSetupError;
Sean Callanana46ec452012-07-11 21:31:24 +00005172 break;
5173 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005174
Sean Callanana46ec452012-07-11 21:31:24 +00005175 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Ingham0161b492013-02-09 01:29:05 +00005176
Sean Callanana46ec452012-07-11 21:31:24 +00005177 if (stop_state != eStateRunning)
5178 {
Jim Ingham0161b492013-02-09 01:29:05 +00005179 bool restarted = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005180
Jim Ingham0161b492013-02-09 01:29:05 +00005181 if (stop_state == eStateStopped)
5182 {
5183 restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get());
5184 if (log)
5185 log->Printf("Process::RunThreadPlan(): didn't get running event after "
5186 "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).",
5187 num_resumes,
5188 StateAsCString(stop_state),
5189 restarted,
5190 do_resume,
5191 handle_running_event);
5192 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005193
Jim Ingham0161b492013-02-09 01:29:05 +00005194 if (restarted)
5195 {
5196 // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted
5197 // event here. But if I do, the best thing is to Halt and then get out of here.
5198 Halt();
5199 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005200
Jim Ingham35e1bda2012-10-16 21:41:58 +00005201 errors.Printf("Didn't get running event after initial resume, got %s instead.",
5202 StateAsCString(stop_state));
Jim Ingham8646d3c2014-05-05 02:47:44 +00005203 return_value = eExpressionSetupError;
Sean Callanana46ec452012-07-11 21:31:24 +00005204 break;
5205 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005206
Sean Callanana46ec452012-07-11 21:31:24 +00005207 if (log)
5208 log->PutCString ("Process::RunThreadPlan(): resuming succeeded.");
5209 // We need to call the function synchronously, so spin waiting for it to return.
5210 // If we get interrupted while executing, we're going to lose our context, and
5211 // won't be able to gather the result at this point.
5212 // We set the timeout AFTER the resume, since the resume takes some time and we
5213 // don't want to charge that to the timeout.
Sean Callanana46ec452012-07-11 21:31:24 +00005214 }
Jim Ingham0f16e732011-02-08 05:20:59 +00005215 else
5216 {
Sean Callanana46ec452012-07-11 21:31:24 +00005217 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005218 log->PutCString ("Process::RunThreadPlan(): waiting for next event.");
Jim Ingham0f16e732011-02-08 05:20:59 +00005219 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005220
Jim Ingham0161b492013-02-09 01:29:05 +00005221 if (before_first_timeout)
5222 {
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005223 if (options.GetTryAllThreads())
Jim Inghamfd95f892014-04-22 01:41:52 +00005224 {
5225 one_thread_timeout = TimeValue::Now();
5226 one_thread_timeout.OffsetWithMicroSeconds(one_thread_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005227 timeout_ptr = &one_thread_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005228 }
Jim Ingham0161b492013-02-09 01:29:05 +00005229 else
5230 {
5231 if (timeout_usec == 0)
5232 timeout_ptr = NULL;
5233 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005234 {
5235 final_timeout = TimeValue::Now();
5236 final_timeout.OffsetWithMicroSeconds (timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005237 timeout_ptr = &final_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005238 }
Jim Ingham0161b492013-02-09 01:29:05 +00005239 }
5240 }
5241 else
5242 {
5243 if (timeout_usec == 0)
5244 timeout_ptr = NULL;
5245 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005246 {
5247 final_timeout = TimeValue::Now();
5248 final_timeout.OffsetWithMicroSeconds (all_threads_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005249 timeout_ptr = &final_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005250 }
Jim Ingham0161b492013-02-09 01:29:05 +00005251 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005252
Jim Ingham0161b492013-02-09 01:29:05 +00005253 do_resume = true;
5254 handle_running_event = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005255
Sean Callanana46ec452012-07-11 21:31:24 +00005256 // Now wait for the process to stop again:
Sean Callanana46ec452012-07-11 21:31:24 +00005257 event_sp.reset();
Jim Ingham0f16e732011-02-08 05:20:59 +00005258
Jim Ingham0f16e732011-02-08 05:20:59 +00005259 if (log)
Jim Ingham20829ac2011-08-09 22:24:33 +00005260 {
Sean Callanana46ec452012-07-11 21:31:24 +00005261 if (timeout_ptr)
5262 {
Matt Kopec676a4872013-02-21 23:55:31 +00005263 log->Printf ("Process::RunThreadPlan(): about to wait - now is %" PRIu64 " - endpoint is %" PRIu64,
Jim Ingham0161b492013-02-09 01:29:05 +00005264 TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(),
5265 timeout_ptr->GetAsMicroSecondsSinceJan1_1970());
Sean Callanana46ec452012-07-11 21:31:24 +00005266 }
Jim Ingham20829ac2011-08-09 22:24:33 +00005267 else
Sean Callanana46ec452012-07-11 21:31:24 +00005268 {
5269 log->Printf ("Process::RunThreadPlan(): about to wait forever.");
5270 }
5271 }
Jim Ingham35878c42014-04-08 21:33:21 +00005272
5273#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
5274 // See comment above...
5275 if (miss_first_event)
5276 {
5277 usleep(1000);
5278 miss_first_event = false;
5279 got_event = false;
5280 }
5281 else
5282#endif
Sean Callanana46ec452012-07-11 21:31:24 +00005283 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005284
Sean Callanana46ec452012-07-11 21:31:24 +00005285 if (got_event)
5286 {
5287 if (event_sp.get())
5288 {
5289 bool keep_going = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005290 if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005291 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005292 Halt();
Jim Ingham8646d3c2014-05-05 02:47:44 +00005293 return_value = eExpressionInterrupted;
Jim Inghamcfc09352012-07-27 23:57:19 +00005294 errors.Printf ("Execution halted by user interrupt.");
5295 if (log)
5296 log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting.");
Jim Ingham0161b492013-02-09 01:29:05 +00005297 break;
Jim Inghamcfc09352012-07-27 23:57:19 +00005298 }
5299 else
5300 {
5301 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5302 if (log)
5303 log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005304
Jim Inghamcfc09352012-07-27 23:57:19 +00005305 switch (stop_state)
Sean Callanana46ec452012-07-11 21:31:24 +00005306 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005307 case lldb::eStateStopped:
Sean Callanana46ec452012-07-11 21:31:24 +00005308 {
Jim Ingham0161b492013-02-09 01:29:05 +00005309 // We stopped, figure out what we are going to do now.
Jim Inghamcfc09352012-07-27 23:57:19 +00005310 ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id);
5311 if (!thread_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005312 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005313 // Ooh, our thread has vanished. Unlikely that this was successful execution...
Sean Callanana46ec452012-07-11 21:31:24 +00005314 if (log)
Jim Inghamcfc09352012-07-27 23:57:19 +00005315 log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005316 return_value = eExpressionInterrupted;
Sean Callanana46ec452012-07-11 21:31:24 +00005317 }
5318 else
5319 {
Jim Ingham0161b492013-02-09 01:29:05 +00005320 // If we were restarted, we just need to go back up to fetch another event.
5321 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
Jim Inghamcfc09352012-07-27 23:57:19 +00005322 {
5323 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005324 {
5325 log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting.");
5326 }
5327 keep_going = true;
5328 do_resume = false;
5329 handle_running_event = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005330
Jim Inghamcfc09352012-07-27 23:57:19 +00005331 }
5332 else
5333 {
Jim Ingham0161b492013-02-09 01:29:05 +00005334 StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
5335 StopReason stop_reason = eStopReasonInvalid;
5336 if (stop_info_sp)
5337 stop_reason = stop_info_sp->GetStopReason();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005338
Jim Ingham0161b492013-02-09 01:29:05 +00005339 // FIXME: We only check if the stop reason is plan complete, should we make sure that
5340 // it is OUR plan that is complete?
5341 if (stop_reason == eStopReasonPlanComplete)
Jim Ingham184e9812013-01-15 02:47:48 +00005342 {
5343 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005344 log->PutCString ("Process::RunThreadPlan(): execution completed successfully.");
5345 // Now mark this plan as private so it doesn't get reported as the stop reason
5346 // after this point.
5347 if (thread_plan_sp)
5348 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005349 return_value = eExpressionCompleted;
Jim Ingham184e9812013-01-15 02:47:48 +00005350 }
5351 else
5352 {
Jim Ingham0161b492013-02-09 01:29:05 +00005353 // Something restarted the target, so just wait for it to stop for real.
Jim Ingham184e9812013-01-15 02:47:48 +00005354 if (stop_reason == eStopReasonBreakpoint)
Jim Ingham0161b492013-02-09 01:29:05 +00005355 {
5356 if (log)
5357 log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription());
Jim Ingham8646d3c2014-05-05 02:47:44 +00005358 return_value = eExpressionHitBreakpoint;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005359 if (!options.DoesIgnoreBreakpoints())
Sean Callanan4b388c92013-07-30 19:54:09 +00005360 {
5361 event_to_broadcast_sp = event_sp;
5362 }
Jim Ingham0161b492013-02-09 01:29:05 +00005363 }
Jim Ingham184e9812013-01-15 02:47:48 +00005364 else
Jim Ingham0161b492013-02-09 01:29:05 +00005365 {
5366 if (log)
5367 log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete.");
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005368 if (!options.DoesUnwindOnError())
Sean Callanan4b388c92013-07-30 19:54:09 +00005369 event_to_broadcast_sp = event_sp;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005370 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005371 }
Jim Ingham184e9812013-01-15 02:47:48 +00005372 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005373 }
Sean Callanana46ec452012-07-11 21:31:24 +00005374 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005375 }
5376 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005377
Jim Inghamcfc09352012-07-27 23:57:19 +00005378 case lldb::eStateRunning:
Jim Ingham0161b492013-02-09 01:29:05 +00005379 // This shouldn't really happen, but sometimes we do get two running events without an
5380 // intervening stop, and in that case we should just go back to waiting for the stop.
Jim Inghamcfc09352012-07-27 23:57:19 +00005381 do_resume = false;
5382 keep_going = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005383 handle_running_event = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005384 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005385
Jim Inghamcfc09352012-07-27 23:57:19 +00005386 default:
5387 if (log)
5388 log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005389
Jim Inghamcfc09352012-07-27 23:57:19 +00005390 if (stop_state == eStateExited)
5391 event_to_broadcast_sp = event_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005392
Sean Callananbf154da2012-08-08 17:35:10 +00005393 errors.Printf ("Execution stopped with unexpected state.\n");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005394 return_value = eExpressionInterrupted;
Jim Inghamcfc09352012-07-27 23:57:19 +00005395 break;
5396 }
Sean Callanana46ec452012-07-11 21:31:24 +00005397 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005398
Sean Callanana46ec452012-07-11 21:31:24 +00005399 if (keep_going)
5400 continue;
5401 else
5402 break;
5403 }
5404 else
5405 {
5406 if (log)
5407 log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd...");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005408 return_value = eExpressionInterrupted;
Sean Callanana46ec452012-07-11 21:31:24 +00005409 break;
5410 }
5411 }
5412 else
5413 {
5414 // If we didn't get an event that means we've timed out...
5415 // We will interrupt the process here. Depending on what we were asked to do we will
5416 // either exit, or try with all threads running for the same timeout.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005417
Sean Callanana46ec452012-07-11 21:31:24 +00005418 if (log) {
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005419 if (options.GetTryAllThreads())
Sean Callanana46ec452012-07-11 21:31:24 +00005420 {
Jim Ingham0161b492013-02-09 01:29:05 +00005421 if (before_first_timeout)
Jim Inghamfe1c3422014-04-16 02:24:48 +00005422 {
5423 if (timeout_usec != 0)
5424 {
Jim Inghamfe1c3422014-04-16 02:24:48 +00005425 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
Jim Inghamfd95f892014-04-22 01:41:52 +00005426 "running for %" PRIu32 " usec with all threads enabled.",
5427 all_threads_timeout_usec);
Jim Inghamfe1c3422014-04-16 02:24:48 +00005428 }
5429 else
5430 {
5431 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
Ed Mastee61c7b02014-04-29 17:48:06 +00005432 "running forever with all threads enabled.");
Jim Inghamfe1c3422014-04-16 02:24:48 +00005433 }
5434 }
Sean Callanana46ec452012-07-11 21:31:24 +00005435 else
5436 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
Jason Molenda6a8658a2013-10-27 02:32:23 +00005437 "and timeout: %u timed out, abandoning execution.",
Jim Ingham35e1bda2012-10-16 21:41:58 +00005438 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005439 }
5440 else
Jason Molenda6a8658a2013-10-27 02:32:23 +00005441 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, "
Jim Ingham35e1bda2012-10-16 21:41:58 +00005442 "abandoning execution.",
5443 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005444 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005445
Jim Ingham0161b492013-02-09 01:29:05 +00005446 // It is possible that between the time we issued the Halt, and we get around to calling Halt the target
5447 // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event.
5448 // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In
5449 // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's
5450 // stopped event. That's what this while loop does.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005451
Jim Ingham0161b492013-02-09 01:29:05 +00005452 bool back_to_top = true;
5453 uint32_t try_halt_again = 0;
5454 bool do_halt = true;
5455 const uint32_t num_retries = 5;
5456 while (try_halt_again < num_retries)
Sean Callanana46ec452012-07-11 21:31:24 +00005457 {
Jim Ingham0161b492013-02-09 01:29:05 +00005458 Error halt_error;
5459 if (do_halt)
5460 {
5461 if (log)
5462 log->Printf ("Process::RunThreadPlan(): Running Halt.");
5463 halt_error = Halt();
5464 }
5465 if (halt_error.Success())
5466 {
5467 if (log)
5468 log->PutCString ("Process::RunThreadPlan(): Halt succeeded.");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005469
Jim Ingham0161b492013-02-09 01:29:05 +00005470 real_timeout = TimeValue::Now();
5471 real_timeout.OffsetWithMicroSeconds(500000);
5472
5473 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005474
Jim Ingham0161b492013-02-09 01:29:05 +00005475 if (got_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005476 {
Jim Ingham0161b492013-02-09 01:29:05 +00005477 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5478 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005479 {
Jim Ingham0161b492013-02-09 01:29:05 +00005480 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
5481 if (stop_state == lldb::eStateStopped
5482 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
5483 log->PutCString (" Event was the Halt interruption event.");
Sean Callanana46ec452012-07-11 21:31:24 +00005484 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005485
Jim Ingham0161b492013-02-09 01:29:05 +00005486 if (stop_state == lldb::eStateStopped)
Sean Callanana46ec452012-07-11 21:31:24 +00005487 {
Jim Ingham0161b492013-02-09 01:29:05 +00005488 // Between the time we initiated the Halt and the time we delivered it, the process could have
5489 // already finished its job. Check that here:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005490
Jim Ingham0161b492013-02-09 01:29:05 +00005491 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
5492 {
5493 if (log)
5494 log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
5495 "Exiting wait loop.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005496 return_value = eExpressionCompleted;
Jim Ingham0161b492013-02-09 01:29:05 +00005497 back_to_top = false;
5498 break;
5499 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005500
Jim Ingham0161b492013-02-09 01:29:05 +00005501 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
5502 {
5503 if (log)
5504 log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... "
5505 "Exiting wait loop.");
5506 try_halt_again++;
5507 do_halt = false;
5508 continue;
5509 }
Sean Callanana46ec452012-07-11 21:31:24 +00005510
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005511 if (!options.GetTryAllThreads())
Jim Ingham0161b492013-02-09 01:29:05 +00005512 {
5513 if (log)
5514 log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005515 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005516 back_to_top = false;
5517 break;
5518 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005519
Jim Ingham0161b492013-02-09 01:29:05 +00005520 if (before_first_timeout)
5521 {
5522 // Set all the other threads to run, and return to the top of the loop, which will continue;
5523 before_first_timeout = false;
5524 thread_plan_sp->SetStopOthers (false);
5525 if (log)
5526 log->PutCString ("Process::RunThreadPlan(): about to resume.");
Sean Callanana46ec452012-07-11 21:31:24 +00005527
Jim Ingham0161b492013-02-09 01:29:05 +00005528 back_to_top = true;
5529 break;
5530 }
5531 else
5532 {
5533 // Running all threads failed, so return Interrupted.
5534 if (log)
5535 log->PutCString("Process::RunThreadPlan(): running all threads timed out.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005536 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005537 back_to_top = false;
5538 break;
5539 }
Sean Callanana46ec452012-07-11 21:31:24 +00005540 }
5541 }
5542 else
Jim Ingham0161b492013-02-09 01:29:05 +00005543 { if (log)
5544 log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
5545 "I'm getting out of here passing Interrupted.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005546 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005547 back_to_top = false;
5548 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005549 }
5550 }
Jim Ingham0161b492013-02-09 01:29:05 +00005551 else
5552 {
5553 try_halt_again++;
5554 continue;
5555 }
Sean Callanana46ec452012-07-11 21:31:24 +00005556 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005557
Jim Ingham0161b492013-02-09 01:29:05 +00005558 if (!back_to_top || try_halt_again > num_retries)
5559 break;
5560 else
5561 continue;
Sean Callanana46ec452012-07-11 21:31:24 +00005562 }
Sean Callanana46ec452012-07-11 21:31:24 +00005563 } // END WAIT LOOP
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005564
Sean Callanana46ec452012-07-11 21:31:24 +00005565 // If we had to start up a temporary private state thread to run this thread plan, shut it down now.
5566 if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread))
5567 {
5568 StopPrivateStateThread();
5569 Error error;
5570 m_private_state_thread = backup_private_state_thread;
Sean Callanan9a028512012-08-09 00:50:26 +00005571 if (stopper_base_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005572 {
5573 thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp);
5574 }
5575 m_public_state.SetValueNoLock(old_state);
5576
5577 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005578
Jim Ingham184e9812013-01-15 02:47:48 +00005579 // Restore the thread state if we are going to discard the plan execution. There are three cases where this
5580 // could happen:
5581 // 1) The execution successfully completed
5582 // 2) We hit a breakpoint, and ignore_breakpoints was true
5583 // 3) We got some other error, and discard_on_error was true
Jim Ingham8646d3c2014-05-05 02:47:44 +00005584 bool should_unwind = (return_value == eExpressionInterrupted && options.DoesUnwindOnError())
5585 || (return_value == eExpressionHitBreakpoint && options.DoesIgnoreBreakpoints());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005586
Jim Ingham8646d3c2014-05-05 02:47:44 +00005587 if (return_value == eExpressionCompleted
Jim Ingham184e9812013-01-15 02:47:48 +00005588 || should_unwind)
Jim Ingham8559a352012-11-26 23:52:18 +00005589 {
5590 thread_plan_sp->RestoreThreadState();
5591 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005592
Sean Callanana46ec452012-07-11 21:31:24 +00005593 // Now do some processing on the results of the run:
Jim Ingham8646d3c2014-05-05 02:47:44 +00005594 if (return_value == eExpressionInterrupted || return_value == eExpressionHitBreakpoint)
Sean Callanana46ec452012-07-11 21:31:24 +00005595 {
5596 if (log)
5597 {
5598 StreamString s;
5599 if (event_sp)
5600 event_sp->Dump (&s);
5601 else
5602 {
5603 log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
5604 }
5605
5606 StreamString ts;
5607
5608 const char *event_explanation = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005609
Sean Callanana46ec452012-07-11 21:31:24 +00005610 do
5611 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005612 if (!event_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005613 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005614 event_explanation = "<no event>";
Sean Callanana46ec452012-07-11 21:31:24 +00005615 break;
5616 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005617 else if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005618 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005619 event_explanation = "<user interrupt>";
Sean Callanana46ec452012-07-11 21:31:24 +00005620 break;
5621 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005622 else
Sean Callanana46ec452012-07-11 21:31:24 +00005623 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005624 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
5625
5626 if (!event_data)
Sean Callanana46ec452012-07-11 21:31:24 +00005627 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005628 event_explanation = "<no event data>";
5629 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005630 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005631
Jim Inghamcfc09352012-07-27 23:57:19 +00005632 Process *process = event_data->GetProcessSP().get();
5633
5634 if (!process)
Sean Callanana46ec452012-07-11 21:31:24 +00005635 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005636 event_explanation = "<no process>";
5637 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005638 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005639
Jim Inghamcfc09352012-07-27 23:57:19 +00005640 ThreadList &thread_list = process->GetThreadList();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005641
Jim Inghamcfc09352012-07-27 23:57:19 +00005642 uint32_t num_threads = thread_list.GetSize();
5643 uint32_t thread_index;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005644
Jim Inghamcfc09352012-07-27 23:57:19 +00005645 ts.Printf("<%u threads> ", num_threads);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005646
Jim Inghamcfc09352012-07-27 23:57:19 +00005647 for (thread_index = 0;
5648 thread_index < num_threads;
5649 ++thread_index)
5650 {
5651 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005652
Jim Inghamcfc09352012-07-27 23:57:19 +00005653 if (!thread)
5654 {
5655 ts.Printf("<?> ");
5656 continue;
5657 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005658
Daniel Malead01b2952012-11-29 21:49:15 +00005659 ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00005660 RegisterContext *register_context = thread->GetRegisterContext().get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005661
Jim Inghamcfc09352012-07-27 23:57:19 +00005662 if (register_context)
Daniel Malead01b2952012-11-29 21:49:15 +00005663 ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC());
Jim Inghamcfc09352012-07-27 23:57:19 +00005664 else
5665 ts.Printf("[ip unknown] ");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005666
Jim Inghamcfc09352012-07-27 23:57:19 +00005667 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
5668 if (stop_info_sp)
5669 {
5670 const char *stop_desc = stop_info_sp->GetDescription();
5671 if (stop_desc)
5672 ts.PutCString (stop_desc);
5673 }
5674 ts.Printf(">");
5675 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005676
Jim Inghamcfc09352012-07-27 23:57:19 +00005677 event_explanation = ts.GetData();
Sean Callanana46ec452012-07-11 21:31:24 +00005678 }
Sean Callanana46ec452012-07-11 21:31:24 +00005679 } while (0);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005680
Jim Inghamcfc09352012-07-27 23:57:19 +00005681 if (event_explanation)
5682 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
Sean Callanana46ec452012-07-11 21:31:24 +00005683 else
Jim Inghamcfc09352012-07-27 23:57:19 +00005684 log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData());
5685 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005686
Jim Inghame4483cf2013-09-27 01:13:01 +00005687 if (should_unwind)
Jim Inghamcfc09352012-07-27 23:57:19 +00005688 {
5689 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005690 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.",
5691 static_cast<void*>(thread_plan_sp.get()));
Jim Inghamcfc09352012-07-27 23:57:19 +00005692 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5693 thread_plan_sp->SetPrivate (orig_plan_private);
5694 }
5695 else
5696 {
5697 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005698 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.",
5699 static_cast<void*>(thread_plan_sp.get()));
Sean Callanana46ec452012-07-11 21:31:24 +00005700 }
5701 }
Jim Ingham8646d3c2014-05-05 02:47:44 +00005702 else if (return_value == eExpressionSetupError)
Sean Callanana46ec452012-07-11 21:31:24 +00005703 {
5704 if (log)
5705 log->PutCString("Process::RunThreadPlan(): execution set up error.");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005706
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005707 if (options.DoesUnwindOnError())
Jim Ingham0f16e732011-02-08 05:20:59 +00005708 {
Greg Claytonc14ee322011-09-22 04:58:26 +00005709 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00005710 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham0f16e732011-02-08 05:20:59 +00005711 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005712 }
5713 else
5714 {
Sean Callanana46ec452012-07-11 21:31:24 +00005715 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
Jim Inghamf48169b2010-11-30 02:22:11 +00005716 {
Jim Ingham0f16e732011-02-08 05:20:59 +00005717 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005718 log->PutCString("Process::RunThreadPlan(): thread plan is done");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005719 return_value = eExpressionCompleted;
Sean Callanana46ec452012-07-11 21:31:24 +00005720 }
5721 else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
5722 {
5723 if (log)
5724 log->PutCString("Process::RunThreadPlan(): thread plan was discarded");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005725 return_value = eExpressionDiscarded;
Sean Callanana46ec452012-07-11 21:31:24 +00005726 }
5727 else
5728 {
5729 if (log)
5730 log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005731 if (options.DoesUnwindOnError() && thread_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005732 {
5733 if (log)
Jim Ingham184e9812013-01-15 02:47:48 +00005734 log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause unwind_on_error is set.");
Sean Callanana46ec452012-07-11 21:31:24 +00005735 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5736 thread_plan_sp->SetPrivate (orig_plan_private);
5737 }
5738 }
5739 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005740
Sean Callanana46ec452012-07-11 21:31:24 +00005741 // Thread we ran the function in may have gone away because we ran the target
5742 // Check that it's still there, and if it is put it back in the context. Also restore the
5743 // frame in the context if it is still present.
5744 thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get();
5745 if (thread)
5746 {
5747 exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id));
5748 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005749
Sean Callanana46ec452012-07-11 21:31:24 +00005750 // Also restore the current process'es selected frame & thread, since this function calling may
5751 // be done behind the user's back.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005752
Sean Callanana46ec452012-07-11 21:31:24 +00005753 if (selected_tid != LLDB_INVALID_THREAD_ID)
5754 {
5755 if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid())
5756 {
5757 // We were able to restore the selected thread, now restore the frame:
Daniel Maleaa012d3a2013-07-31 20:21:20 +00005758 Mutex::Locker lock(GetThreadList().GetMutex());
Jason Molendab57e4a12013-11-04 09:33:30 +00005759 StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id);
Sean Callanana46ec452012-07-11 21:31:24 +00005760 if (old_frame_sp)
5761 GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get());
Jim Inghamf48169b2010-11-30 02:22:11 +00005762 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005763 }
5764 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005765
Sean Callanana46ec452012-07-11 21:31:24 +00005766 // If the process exited during the run of the thread plan, notify everyone.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005767
Sean Callanana46ec452012-07-11 21:31:24 +00005768 if (event_to_broadcast_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00005769 {
Sean Callanana46ec452012-07-11 21:31:24 +00005770 if (log)
5771 log->PutCString("Process::RunThreadPlan(): rebroadcasting event.");
5772 BroadcastEvent(event_to_broadcast_sp);
Jim Inghamf48169b2010-11-30 02:22:11 +00005773 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005774
Jim Inghamf48169b2010-11-30 02:22:11 +00005775 return return_value;
5776}
5777
5778const char *
Jim Ingham1624a2d2014-05-05 02:26:40 +00005779Process::ExecutionResultAsCString (ExpressionResults result)
Jim Inghamf48169b2010-11-30 02:22:11 +00005780{
5781 const char *result_name;
5782
5783 switch (result)
5784 {
Jim Ingham8646d3c2014-05-05 02:47:44 +00005785 case eExpressionCompleted:
5786 result_name = "eExpressionCompleted";
Jim Inghamf48169b2010-11-30 02:22:11 +00005787 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005788 case eExpressionDiscarded:
5789 result_name = "eExpressionDiscarded";
Jim Inghamf48169b2010-11-30 02:22:11 +00005790 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005791 case eExpressionInterrupted:
5792 result_name = "eExpressionInterrupted";
Jim Inghamf48169b2010-11-30 02:22:11 +00005793 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005794 case eExpressionHitBreakpoint:
5795 result_name = "eExpressionHitBreakpoint";
Jim Ingham184e9812013-01-15 02:47:48 +00005796 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005797 case eExpressionSetupError:
5798 result_name = "eExpressionSetupError";
Jim Inghamf48169b2010-11-30 02:22:11 +00005799 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005800 case eExpressionParseError:
5801 result_name = "eExpressionParseError";
Jim Ingham1624a2d2014-05-05 02:26:40 +00005802 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005803 case eExpressionResultUnavailable:
5804 result_name = "eExpressionResultUnavailable";
Jim Ingham1624a2d2014-05-05 02:26:40 +00005805 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005806 case eExpressionTimedOut:
5807 result_name = "eExpressionTimedOut";
Jim Inghamf48169b2010-11-30 02:22:11 +00005808 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005809 case eExpressionStoppedForDebug:
5810 result_name = "eExpressionStoppedForDebug";
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005811 break;
Jim Inghamf48169b2010-11-30 02:22:11 +00005812 }
5813 return result_name;
5814}
5815
Greg Clayton7260f622011-04-18 08:33:37 +00005816void
5817Process::GetStatus (Stream &strm)
5818{
5819 const StateType state = GetState();
Greg Clayton2637f822011-11-17 01:23:07 +00005820 if (StateIsStoppedState(state, false))
Greg Clayton7260f622011-04-18 08:33:37 +00005821 {
5822 if (state == eStateExited)
5823 {
5824 int exit_status = GetExitStatus();
5825 const char *exit_description = GetExitDescription();
Daniel Malead01b2952012-11-29 21:49:15 +00005826 strm.Printf ("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n",
Greg Clayton7260f622011-04-18 08:33:37 +00005827 GetID(),
5828 exit_status,
5829 exit_status,
5830 exit_description ? exit_description : "");
5831 }
5832 else
5833 {
5834 if (state == eStateConnected)
5835 strm.Printf ("Connected to remote target.\n");
5836 else
Daniel Malead01b2952012-11-29 21:49:15 +00005837 strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state));
Greg Clayton7260f622011-04-18 08:33:37 +00005838 }
5839 }
5840 else
5841 {
Daniel Malead01b2952012-11-29 21:49:15 +00005842 strm.Printf ("Process %" PRIu64 " is running.\n", GetID());
Greg Clayton7260f622011-04-18 08:33:37 +00005843 }
5844}
5845
5846size_t
5847Process::GetThreadStatus (Stream &strm,
5848 bool only_threads_with_stop_reason,
5849 uint32_t start_frame,
5850 uint32_t num_frames,
5851 uint32_t num_frames_with_source)
5852{
5853 size_t num_thread_infos_dumped = 0;
5854
Jim Ingham4a65fb12014-03-07 11:20:03 +00005855 // You can't hold the thread list lock while calling Thread::GetStatus. That very well might run code (e.g. if we need it
5856 // to get return values or arguments.) For that to work the process has to be able to acquire it. So instead copy the thread
5857 // ID's, and look them up one by one:
5858
5859 uint32_t num_threads;
5860 std::vector<uint32_t> thread_index_array;
5861 //Scope for thread list locker;
5862 {
5863 Mutex::Locker locker (GetThreadList().GetMutex());
5864 ThreadList &curr_thread_list = GetThreadList();
5865 num_threads = curr_thread_list.GetSize();
5866 uint32_t idx;
5867 thread_index_array.resize(num_threads);
5868 for (idx = 0; idx < num_threads; ++idx)
5869 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID();
5870 }
5871
Greg Clayton7260f622011-04-18 08:33:37 +00005872 for (uint32_t i = 0; i < num_threads; i++)
5873 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00005874 ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_index_array[i]));
5875 if (thread_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00005876 {
5877 if (only_threads_with_stop_reason)
5878 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00005879 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
Jim Ingham5d88a062012-10-16 00:09:33 +00005880 if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid())
Greg Clayton7260f622011-04-18 08:33:37 +00005881 continue;
5882 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00005883 thread_sp->GetStatus (strm,
Greg Clayton7260f622011-04-18 08:33:37 +00005884 start_frame,
5885 num_frames,
5886 num_frames_with_source);
5887 ++num_thread_infos_dumped;
5888 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00005889 else
5890 {
5891 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
5892 if (log)
5893 log->Printf("Process::GetThreadStatus - thread 0x" PRIu64 " vanished while running Thread::GetStatus.");
5894
5895 }
Greg Clayton7260f622011-04-18 08:33:37 +00005896 }
5897 return num_thread_infos_dumped;
5898}
5899
Greg Claytona9f40ad2012-02-22 04:37:26 +00005900void
5901Process::AddInvalidMemoryRegion (const LoadRange &region)
5902{
5903 m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize());
5904}
5905
5906bool
5907Process::RemoveInvalidMemoryRange (const LoadRange &region)
5908{
5909 return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize());
5910}
5911
Jim Ingham372787f2012-04-07 00:00:41 +00005912void
5913Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton)
5914{
5915 m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton));
5916}
5917
5918bool
5919Process::RunPreResumeActions ()
5920{
5921 bool result = true;
5922 while (!m_pre_resume_actions.empty())
5923 {
5924 struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back();
5925 m_pre_resume_actions.pop_back();
5926 bool this_result = action.callback (action.baton);
5927 if (result == true) result = this_result;
5928 }
5929 return result;
5930}
5931
5932void
5933Process::ClearPreResumeActions ()
5934{
5935 m_pre_resume_actions.clear();
5936}
Greg Claytona9f40ad2012-02-22 04:37:26 +00005937
Greg Claytonfa559e52012-05-18 02:38:05 +00005938void
5939Process::Flush ()
5940{
5941 m_thread_list.Flush();
Jason Molenda5e8dce42013-12-13 00:29:16 +00005942 m_extended_thread_list.Flush();
5943 m_extended_thread_stop_id = 0;
5944 m_queue_list.Clear();
5945 m_queue_list_stop_id = 0;
Greg Claytonfa559e52012-05-18 02:38:05 +00005946}
Greg Clayton90ba8112012-12-05 00:16:59 +00005947
5948void
5949Process::DidExec ()
5950{
Todd Fiala76e0fc92014-08-27 22:58:26 +00005951 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
5952 if (log)
5953 log->Printf ("Process::%s()", __FUNCTION__);
5954
Greg Clayton90ba8112012-12-05 00:16:59 +00005955 Target &target = GetTarget();
5956 target.CleanupProcess ();
Greg Claytonb35db632013-11-09 00:03:31 +00005957 target.ClearModules(false);
Greg Clayton90ba8112012-12-05 00:16:59 +00005958 m_dynamic_checkers_ap.reset();
5959 m_abi_sp.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00005960 m_system_runtime_ap.reset();
Greg Clayton90ba8112012-12-05 00:16:59 +00005961 m_os_ap.reset();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005962 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00005963 m_jit_loaders_ap.reset();
Greg Clayton90ba8112012-12-05 00:16:59 +00005964 m_image_tokens.clear();
5965 m_allocated_memory_cache.Clear();
5966 m_language_runtimes.clear();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005967 m_thread_list.DiscardThreadPlans();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005968 m_memory_cache.Clear(true);
Greg Clayton90ba8112012-12-05 00:16:59 +00005969 DoDidExec();
5970 CompleteAttach ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00005971 // Flush the process (threads and all stack frames) after running CompleteAttach()
5972 // in case the dynamic loader loaded things in new locations.
5973 Flush();
Greg Claytonb35db632013-11-09 00:03:31 +00005974
5975 // After we figure out what was loaded/unloaded in CompleteAttach,
5976 // we need to let the target know so it can do any cleanup it needs to.
5977 target.DidExec();
Greg Clayton90ba8112012-12-05 00:16:59 +00005978}
Greg Clayton095eeaa2013-11-05 23:28:00 +00005979
Jim Ingham1460e4b2014-01-10 23:46:59 +00005980addr_t
5981Process::ResolveIndirectFunction(const Address *address, Error &error)
5982{
5983 if (address == nullptr)
5984 {
Jean-Daniel Dupasef37711f2014-02-08 20:22:05 +00005985 error.SetErrorString("Invalid address argument");
Jim Ingham1460e4b2014-01-10 23:46:59 +00005986 return LLDB_INVALID_ADDRESS;
5987 }
5988
5989 addr_t function_addr = LLDB_INVALID_ADDRESS;
5990
5991 addr_t addr = address->GetLoadAddress(&GetTarget());
5992 std::map<addr_t,addr_t>::const_iterator iter = m_resolved_indirect_addresses.find(addr);
5993 if (iter != m_resolved_indirect_addresses.end())
5994 {
5995 function_addr = (*iter).second;
5996 }
5997 else
5998 {
5999 if (!InferiorCall(this, address, function_addr))
6000 {
6001 Symbol *symbol = address->CalculateSymbolContextSymbol();
6002 error.SetErrorStringWithFormat ("Unable to call resolver for indirect function %s",
6003 symbol ? symbol->GetName().AsCString() : "<UNKNOWN>");
6004 function_addr = LLDB_INVALID_ADDRESS;
6005 }
6006 else
6007 {
6008 m_resolved_indirect_addresses.insert(std::pair<addr_t, addr_t>(addr, function_addr));
6009 }
6010 }
6011 return function_addr;
6012}
6013
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00006014void
6015Process::ModulesDidLoad (ModuleList &module_list)
6016{
6017 SystemRuntime *sys_runtime = GetSystemRuntime();
6018 if (sys_runtime)
6019 {
6020 sys_runtime->ModulesDidLoad (module_list);
6021 }
6022
6023 GetJITLoaders().ModulesDidLoad (module_list);
6024}