blob: 28f02359b744c4edc8f3e9e0c8dc99c1fe361501 [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"
Kuba Breckaa51ea382014-09-06 01:33:13 +000035#include "lldb/Target/MemoryHistory.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000036#include "lldb/Target/OperatingSystem.h"
Jim Ingham22777012010-09-23 02:01:19 +000037#include "lldb/Target/LanguageRuntime.h"
38#include "lldb/Target/CPPLanguageRuntime.h"
39#include "lldb/Target/ObjCLanguageRuntime.h"
Greg Claytone996fd32011-03-08 22:40:15 +000040#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000042#include "lldb/Target/StopInfo.h"
Jason Molendaeef51062013-11-05 03:57:19 +000043#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/Target/Target.h"
45#include "lldb/Target/TargetList.h"
46#include "lldb/Target/Thread.h"
47#include "lldb/Target/ThreadPlan.h"
Jim Ingham076b3042012-04-10 01:21:57 +000048#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham1460e4b2014-01-10 23:46:59 +000049#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
51using namespace lldb;
52using namespace lldb_private;
53
Greg Clayton67cc0632012-08-22 17:17:09 +000054
55// Comment out line below to disable memory caching, overriding the process setting
56// target.process.disable-memory-cache
57#define ENABLE_MEMORY_CACHING
58
59#ifdef ENABLE_MEMORY_CACHING
60#define DISABLE_MEM_CACHE_DEFAULT false
61#else
62#define DISABLE_MEM_CACHE_DEFAULT true
63#endif
64
65class ProcessOptionValueProperties : public OptionValueProperties
66{
67public:
68 ProcessOptionValueProperties (const ConstString &name) :
69 OptionValueProperties (name)
70 {
71 }
72
73 // This constructor is used when creating ProcessOptionValueProperties when it
74 // is part of a new lldb_private::Process instance. It will copy all current
75 // global property values as needed
76 ProcessOptionValueProperties (ProcessProperties *global_properties) :
77 OptionValueProperties(*global_properties->GetValueProperties())
78 {
79 }
80
81 virtual const Property *
82 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
83 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000084 // When getting the value for a key from the process options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +000085 // try and grab the setting from the current process if there is one. Else we just
86 // use the one from this instance.
87 if (exe_ctx)
88 {
89 Process *process = exe_ctx->GetProcessPtr();
90 if (process)
91 {
92 ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get());
93 if (this != instance_properties)
94 return instance_properties->ProtectedGetPropertyAtIndex (idx);
95 }
96 }
97 return ProtectedGetPropertyAtIndex (idx);
98 }
99};
100
101static PropertyDefinition
102g_properties[] =
103{
104 { "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 +0000105 { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. "
106 "For instance, to turn on debugserver logging set this to \"QSetLogging:bitmask=LOG_DEFAULT;\"" },
Jim Inghamafc1b122013-01-31 19:48:57 +0000107 { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." },
108 { "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 +0000109 { "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 +0000110 { "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 +0000111 { "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 +0000112 { "memory-cache-line-size" , OptionValue::eTypeUInt64, false, 512, NULL, NULL, "The memory cache line size" },
Greg Clayton67cc0632012-08-22 17:17:09 +0000113 { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL }
114};
115
116enum {
117 ePropertyDisableMemCache,
Greg Claytonc9d645d2012-10-18 22:40:37 +0000118 ePropertyExtraStartCommand,
Jim Ingham184e9812013-01-15 02:47:48 +0000119 ePropertyIgnoreBreakpointsInExpressions,
120 ePropertyUnwindOnErrorInExpressions,
Jim Ingham29950772013-01-26 02:19:28 +0000121 ePropertyPythonOSPluginPath,
Jim Inghamacff8952013-05-02 00:27:30 +0000122 ePropertyStopOnSharedLibraryEvents,
Jason Molendaf0340c92014-09-03 22:30:54 +0000123 ePropertyDetachKeepsStopped,
124 ePropertyMemCacheLineSize
Greg Clayton67cc0632012-08-22 17:17:09 +0000125};
126
127ProcessProperties::ProcessProperties (bool is_global) :
128 Properties ()
129{
130 if (is_global)
131 {
132 m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process")));
133 m_collection_sp->Initialize(g_properties);
134 m_collection_sp->AppendProperty(ConstString("thread"),
Jim Ingham29950772013-01-26 02:19:28 +0000135 ConstString("Settings specific to threads."),
Greg Clayton67cc0632012-08-22 17:17:09 +0000136 true,
137 Thread::GetGlobalProperties()->GetValueProperties());
138 }
139 else
140 m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get()));
141}
142
143ProcessProperties::~ProcessProperties()
144{
145}
146
147bool
148ProcessProperties::GetDisableMemoryCache() const
149{
150 const uint32_t idx = ePropertyDisableMemCache;
151 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
152}
153
Jason Molendaf0340c92014-09-03 22:30:54 +0000154uint64_t
155ProcessProperties::GetMemoryCacheLineSize() const
156{
157 const uint32_t idx = ePropertyMemCacheLineSize;
158 return m_collection_sp->GetPropertyAtIndexAsUInt64 (NULL, idx, g_properties[idx].default_uint_value);
159}
160
Greg Clayton67cc0632012-08-22 17:17:09 +0000161Args
162ProcessProperties::GetExtraStartupCommands () const
163{
164 Args args;
165 const uint32_t idx = ePropertyExtraStartCommand;
166 m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args);
167 return args;
168}
169
170void
171ProcessProperties::SetExtraStartupCommands (const Args &args)
172{
173 const uint32_t idx = ePropertyExtraStartCommand;
174 m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args);
175}
176
Greg Claytonc9d645d2012-10-18 22:40:37 +0000177FileSpec
178ProcessProperties::GetPythonOSPluginPath () const
179{
180 const uint32_t idx = ePropertyPythonOSPluginPath;
181 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
182}
183
184void
185ProcessProperties::SetPythonOSPluginPath (const FileSpec &file)
186{
187 const uint32_t idx = ePropertyPythonOSPluginPath;
188 m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file);
189}
190
Jim Ingham184e9812013-01-15 02:47:48 +0000191
192bool
193ProcessProperties::GetIgnoreBreakpointsInExpressions () const
194{
195 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
196 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
197}
198
199void
200ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore)
201{
202 const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions;
203 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
204}
205
206bool
207ProcessProperties::GetUnwindOnErrorInExpressions () const
208{
209 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
210 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
211}
212
213void
214ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore)
215{
216 const uint32_t idx = ePropertyUnwindOnErrorInExpressions;
217 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore);
218}
219
Jim Ingham29950772013-01-26 02:19:28 +0000220bool
221ProcessProperties::GetStopOnSharedLibraryEvents () const
222{
223 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
224 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
225}
226
227void
228ProcessProperties::SetStopOnSharedLibraryEvents (bool stop)
229{
230 const uint32_t idx = ePropertyStopOnSharedLibraryEvents;
231 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
232}
233
Jim Inghamacff8952013-05-02 00:27:30 +0000234bool
235ProcessProperties::GetDetachKeepsStopped () const
236{
237 const uint32_t idx = ePropertyDetachKeepsStopped;
238 return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0);
239}
240
241void
242ProcessProperties::SetDetachKeepsStopped (bool stop)
243{
244 const uint32_t idx = ePropertyDetachKeepsStopped;
245 m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop);
246}
247
Greg Clayton32e0a752011-03-30 18:16:51 +0000248void
Greg Clayton8b82f082011-04-12 05:54:46 +0000249ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000250{
251 const char *cstr;
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000252 if (m_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000253 s.Printf (" pid = %" PRIu64 "\n", m_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000254
255 if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000256 s.Printf (" parent = %" PRIu64 "\n", m_parent_pid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000257
258 if (m_executable)
259 {
260 s.Printf (" name = %s\n", m_executable.GetFilename().GetCString());
261 s.PutCString (" file = ");
262 m_executable.Dump(&s);
263 s.EOL();
264 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000265 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000266 if (argc > 0)
267 {
268 for (uint32_t i=0; i<argc; i++)
269 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000270 const char *arg = m_arguments.GetArgumentAtIndex(i);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000271 if (i < 10)
Greg Clayton8b82f082011-04-12 05:54:46 +0000272 s.Printf (" arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000273 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000274 s.Printf ("arg[%u] = %s\n", i, arg);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000275 }
276 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000277
278 const uint32_t envc = m_environment.GetArgumentCount();
279 if (envc > 0)
280 {
281 for (uint32_t i=0; i<envc; i++)
282 {
283 const char *env = m_environment.GetArgumentAtIndex(i);
284 if (i < 10)
285 s.Printf (" env[%u] = %s\n", i, env);
286 else
287 s.Printf ("env[%u] = %s\n", i, env);
288 }
289 }
290
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000291 if (m_arch.IsValid())
292 s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str());
293
Greg Clayton8b82f082011-04-12 05:54:46 +0000294 if (m_uid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000295 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000296 cstr = platform->GetUserName (m_uid);
297 s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000298 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000299 if (m_gid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000300 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000301 cstr = platform->GetGroupName (m_gid);
302 s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000303 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000304 if (m_euid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000305 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000306 cstr = platform->GetUserName (m_euid);
307 s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000308 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000309 if (m_egid != UINT32_MAX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000310 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000311 cstr = platform->GetGroupName (m_egid);
312 s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : "");
Greg Clayton32e0a752011-03-30 18:16:51 +0000313 }
314}
315
316void
Greg Clayton8b82f082011-04-12 05:54:46 +0000317ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose)
Greg Clayton32e0a752011-03-30 18:16:51 +0000318{
Greg Clayton8b82f082011-04-12 05:54:46 +0000319 const char *label;
320 if (show_args || verbose)
321 label = "ARGUMENTS";
322 else
323 label = "NAME";
324
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000325 if (verbose)
326 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000327 s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000328 s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n");
329 }
330 else
331 {
Jim Ingham368ac222014-08-15 17:05:27 +0000332 s.Printf ("PID PARENT USER TRIPLE %s\n", label);
333 s.PutCString ("====== ====== ========== ======================== ============================\n");
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000334 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000335}
336
337void
Greg Clayton8b82f082011-04-12 05:54:46 +0000338ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000339{
340 if (m_pid != LLDB_INVALID_PROCESS_ID)
341 {
342 const char *cstr;
Daniel Malead01b2952012-11-29 21:49:15 +0000343 s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000344
Greg Clayton32e0a752011-03-30 18:16:51 +0000345
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000346 if (verbose)
347 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000348 cstr = platform->GetUserName (m_uid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000349 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
350 s.Printf ("%-10s ", cstr);
351 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000352 s.Printf ("%-10u ", m_uid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000353
Greg Clayton8b82f082011-04-12 05:54:46 +0000354 cstr = platform->GetGroupName (m_gid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000355 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
356 s.Printf ("%-10s ", cstr);
357 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000358 s.Printf ("%-10u ", m_gid);
Greg Clayton32e0a752011-03-30 18:16:51 +0000359
Greg Clayton8b82f082011-04-12 05:54:46 +0000360 cstr = platform->GetUserName (m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000361 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
362 s.Printf ("%-10s ", cstr);
363 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000364 s.Printf ("%-10u ", m_euid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000365
Greg Clayton8b82f082011-04-12 05:54:46 +0000366 cstr = platform->GetGroupName (m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000367 if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed
368 s.Printf ("%-10s ", cstr);
369 else
Greg Clayton8b82f082011-04-12 05:54:46 +0000370 s.Printf ("%-10u ", m_egid);
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000371 s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
372 }
373 else
374 {
Jim Ingham368ac222014-08-15 17:05:27 +0000375 s.Printf ("%-10s %-24s ",
Greg Clayton8b82f082011-04-12 05:54:46 +0000376 platform->GetUserName (m_euid),
Jim Ingham368ac222014-08-15 17:05:27 +0000377 m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : "");
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000378 }
379
Greg Clayton8b82f082011-04-12 05:54:46 +0000380 if (verbose || show_args)
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000381 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000382 const uint32_t argc = m_arguments.GetArgumentCount();
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000383 if (argc > 0)
384 {
385 for (uint32_t i=0; i<argc; i++)
386 {
387 if (i > 0)
388 s.PutChar (' ');
Greg Clayton8b82f082011-04-12 05:54:46 +0000389 s.PutCString (m_arguments.GetArgumentAtIndex(i));
Greg Clayton95bf0fd2011-04-01 00:29:43 +0000390 }
391 }
392 }
393 else
394 {
395 s.PutCString (GetName());
396 }
397
398 s.EOL();
Greg Clayton32e0a752011-03-30 18:16:51 +0000399 }
400}
401
Greg Clayton8b82f082011-04-12 05:54:46 +0000402Error
Greg Claytonf6b8b582011-04-13 00:18:08 +0000403ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Greg Clayton8b82f082011-04-12 05:54:46 +0000404{
405 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000406 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton8b82f082011-04-12 05:54:46 +0000407
408 switch (short_option)
409 {
410 case 's': // Stop at program entry point
411 launch_info.GetFlags().Set (eLaunchFlagStopAtEntry);
412 break;
413
Greg Clayton8b82f082011-04-12 05:54:46 +0000414 case 'i': // STDIN for read only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000415 {
416 FileAction action;
417 if (action.Open (STDIN_FILENO, option_arg, true, false))
418 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000419 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000420 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000421
422 case 'o': // Open STDOUT for write only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000423 {
424 FileAction action;
425 if (action.Open (STDOUT_FILENO, option_arg, false, true))
426 launch_info.AppendFileAction (action);
Greg Clayton9845a8d2012-03-06 04:01:04 +0000427 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000428 }
Greg Clayton9845a8d2012-03-06 04:01:04 +0000429
430 case 'e': // STDERR for write only
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000431 {
432 FileAction action;
433 if (action.Open (STDERR_FILENO, option_arg, false, true))
434 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000435 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000436 }
Greg Clayton9845a8d2012-03-06 04:01:04 +0000437
Greg Clayton8b82f082011-04-12 05:54:46 +0000438 case 'p': // Process plug-in name
439 launch_info.SetProcessPluginName (option_arg);
440 break;
441
442 case 'n': // Disable STDIO
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000443 {
444 FileAction action;
445 if (action.Open (STDIN_FILENO, "/dev/null", true, false))
446 launch_info.AppendFileAction (action);
447 if (action.Open (STDOUT_FILENO, "/dev/null", false, true))
448 launch_info.AppendFileAction (action);
449 if (action.Open (STDERR_FILENO, "/dev/null", false, true))
450 launch_info.AppendFileAction (action);
Greg Clayton8b82f082011-04-12 05:54:46 +0000451 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000452 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000453
454 case 'w':
455 launch_info.SetWorkingDirectory (option_arg);
456 break;
457
458 case 't': // Open process in new terminal window
459 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
460 break;
461
462 case 'a':
Greg Clayton70512312012-05-08 01:45:38 +0000463 if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get()))
464 launch_info.GetArchitecture().SetTriple (option_arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000465 break;
466
Todd Fiala51637922014-08-19 17:40:43 +0000467 case 'A': // Disable ASLR.
468 {
469 bool success;
470 const bool disable_aslr_arg = Args::StringToBoolean (option_arg, true, &success);
471 if (success)
472 disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo;
473 else
474 error.SetErrorStringWithFormat ("Invalid boolean value for disable-aslr option: '%s'", option_arg ? option_arg : "<null>");
Greg Clayton8b82f082011-04-12 05:54:46 +0000475 break;
Todd Fiala51637922014-08-19 17:40:43 +0000476 }
477
478 case 'c':
Greg Clayton144f3a92011-11-15 03:53:30 +0000479 if (option_arg && option_arg[0])
480 launch_info.SetShell (option_arg);
481 else
Ed Masteb8ca4a22013-09-03 23:04:53 +0000482 launch_info.SetShell (LLDB_DEFAULT_SHELL);
Greg Clayton982c9762011-11-03 21:22:33 +0000483 break;
484
Greg Clayton8b82f082011-04-12 05:54:46 +0000485 case 'v':
486 launch_info.GetEnvironmentEntries().AppendArgument(option_arg);
487 break;
488
489 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000490 error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option);
Greg Clayton8b82f082011-04-12 05:54:46 +0000491 break;
Greg Clayton8b82f082011-04-12 05:54:46 +0000492 }
493 return error;
494}
495
496OptionDefinition
497ProcessLaunchCommandOptions::g_option_table[] =
498{
Zachary Turnerd37221d2014-07-09 16:31:49 +0000499{ 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 +0000500{ 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 +0000501{ LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."},
502{ 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."},
503{ LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."},
504{ 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."},
505{ 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 +0000506
Zachary Turnerd37221d2014-07-09 16:31:49 +0000507{ LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."},
508{ LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."},
509{ 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 +0000510
Zachary Turnerd37221d2014-07-09 16:31:49 +0000511{ 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 +0000512
Zachary Turnerd37221d2014-07-09 16:31:49 +0000513{ 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 +0000514
Zachary Turnerd37221d2014-07-09 16:31:49 +0000515{ 0 , false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Clayton8b82f082011-04-12 05:54:46 +0000516};
517
518
519
520bool
521ProcessInstanceInfoMatch::NameMatches (const char *process_name) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000522{
523 if (m_name_match_type == eNameMatchIgnore || process_name == NULL)
524 return true;
525 const char *match_name = m_match_info.GetName();
526 if (!match_name)
527 return true;
528
529 return lldb_private::NameMatches (process_name, m_name_match_type, match_name);
530}
531
532bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000533ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const
Greg Clayton32e0a752011-03-30 18:16:51 +0000534{
535 if (!NameMatches (proc_info.GetName()))
536 return false;
537
538 if (m_match_info.ProcessIDIsValid() &&
539 m_match_info.GetProcessID() != proc_info.GetProcessID())
540 return false;
541
542 if (m_match_info.ParentProcessIDIsValid() &&
543 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
544 return false;
545
Greg Clayton8b82f082011-04-12 05:54:46 +0000546 if (m_match_info.UserIDIsValid () &&
547 m_match_info.GetUserID() != proc_info.GetUserID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000548 return false;
549
Greg Clayton8b82f082011-04-12 05:54:46 +0000550 if (m_match_info.GroupIDIsValid () &&
551 m_match_info.GetGroupID() != proc_info.GetGroupID())
Greg Clayton32e0a752011-03-30 18:16:51 +0000552 return false;
553
554 if (m_match_info.EffectiveUserIDIsValid () &&
555 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
556 return false;
557
558 if (m_match_info.EffectiveGroupIDIsValid () &&
559 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
560 return false;
561
562 if (m_match_info.GetArchitecture().IsValid() &&
Sean Callananbf4b7be2012-12-13 22:07:14 +0000563 !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture()))
Greg Clayton32e0a752011-03-30 18:16:51 +0000564 return false;
565 return true;
566}
567
568bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000569ProcessInstanceInfoMatch::MatchAllProcesses () const
Greg Clayton32e0a752011-03-30 18:16:51 +0000570{
571 if (m_name_match_type != eNameMatchIgnore)
572 return false;
573
574 if (m_match_info.ProcessIDIsValid())
575 return false;
576
577 if (m_match_info.ParentProcessIDIsValid())
578 return false;
579
Greg Clayton8b82f082011-04-12 05:54:46 +0000580 if (m_match_info.UserIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000581 return false;
582
Greg Clayton8b82f082011-04-12 05:54:46 +0000583 if (m_match_info.GroupIDIsValid ())
Greg Clayton32e0a752011-03-30 18:16:51 +0000584 return false;
585
586 if (m_match_info.EffectiveUserIDIsValid ())
587 return false;
588
589 if (m_match_info.EffectiveGroupIDIsValid ())
590 return false;
591
592 if (m_match_info.GetArchitecture().IsValid())
593 return false;
594
595 if (m_match_all_users)
596 return false;
597
598 return true;
599
600}
601
602void
Greg Clayton8b82f082011-04-12 05:54:46 +0000603ProcessInstanceInfoMatch::Clear()
Greg Clayton32e0a752011-03-30 18:16:51 +0000604{
605 m_match_info.Clear();
606 m_name_match_type = eNameMatchIgnore;
607 m_match_all_users = false;
608}
Greg Clayton58be07b2011-01-07 06:08:19 +0000609
Greg Claytonc3776bf2012-02-09 06:16:32 +0000610ProcessSP
611Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener, const FileSpec *crash_file_path)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612{
Greg Clayton949e8222013-01-16 17:29:04 +0000613 static uint32_t g_process_unique_id = 0;
614
Greg Claytonc3776bf2012-02-09 06:16:32 +0000615 ProcessSP process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 ProcessCreateInstance create_callback = NULL;
617 if (plugin_name)
618 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000619 ConstString const_plugin_name(plugin_name);
620 create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 if (create_callback)
622 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000623 process_sp = create_callback(target, listener, crash_file_path);
624 if (process_sp)
625 {
Greg Clayton949e8222013-01-16 17:29:04 +0000626 if (process_sp->CanDebug(target, true))
627 {
628 process_sp->m_process_unique_id = ++g_process_unique_id;
629 }
630 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000631 process_sp.reset();
632 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 }
634 }
635 else
636 {
Greg Claytonc982c762010-07-09 20:39:50 +0000637 for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000639 process_sp = create_callback(target, listener, crash_file_path);
640 if (process_sp)
641 {
Greg Clayton949e8222013-01-16 17:29:04 +0000642 if (process_sp->CanDebug(target, false))
643 {
644 process_sp->m_process_unique_id = ++g_process_unique_id;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000645 break;
Greg Clayton949e8222013-01-16 17:29:04 +0000646 }
647 else
648 process_sp.reset();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000649 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 }
651 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000652 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653}
654
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000655ConstString &
656Process::GetStaticBroadcasterClass ()
657{
658 static ConstString class_name ("lldb.process");
659 return class_name;
660}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661
662//----------------------------------------------------------------------
663// Process constructor
664//----------------------------------------------------------------------
665Process::Process(Target &target, Listener &listener) :
Todd Fiala4ceced32014-08-29 17:35:57 +0000666 Process(target, listener, Host::GetUnixSignals ())
667{
668 // This constructor just delegates to the full Process constructor,
669 // defaulting to using the Host's UnixSignals.
670}
671
672Process::Process(Target &target, Listener &listener, const UnixSignalsSP &unix_signals_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000673 ProcessProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 UserID (LLDB_INVALID_PROCESS_ID),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000675 Broadcaster (&(target.GetDebugger()), "lldb.process"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676 m_target (target),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677 m_public_state (eStateUnloaded),
678 m_private_state (eStateUnloaded),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000679 m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"),
680 m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 m_private_state_listener ("lldb.process.internal_state_listener"),
682 m_private_state_control_wait(),
683 m_private_state_thread (LLDB_INVALID_HOST_THREAD),
Jim Ingham4b536182011-08-09 02:12:22 +0000684 m_mod_id (),
Greg Clayton949e8222013-01-16 17:29:04 +0000685 m_process_unique_id(0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686 m_thread_index_id (0),
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000687 m_thread_id_to_index_id_map (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 m_exit_status (-1),
689 m_exit_string (),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000690 m_thread_mutex (Mutex::eMutexTypeRecursive),
691 m_thread_list_real (this),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 m_thread_list (this),
Jason Molenda864f1cc2013-11-11 05:20:44 +0000693 m_extended_thread_list (this),
Jason Molenda4ff13262013-11-20 00:31:38 +0000694 m_extended_thread_stop_id (0),
Jason Molenda5e8dce42013-12-13 00:29:16 +0000695 m_queue_list (this),
696 m_queue_list_stop_id (0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697 m_notifications (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000698 m_image_tokens (),
699 m_listener (listener),
700 m_breakpoint_site_list (),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000701 m_dynamic_checkers_ap (),
Todd Fiala4ceced32014-08-29 17:35:57 +0000702 m_unix_signals_sp (unix_signals_sp),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000703 m_abi_sp (),
Caroline Ticeef5c6d02010-11-16 05:07:41 +0000704 m_process_input_reader (),
Greg Clayton3e06bd92011-01-09 21:07:35 +0000705 m_stdio_communication ("process.stdio"),
Greg Clayton3af9ea52010-11-18 05:57:03 +0000706 m_stdio_communication_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton58be07b2011-01-07 06:08:19 +0000707 m_stdout_data (),
Greg Clayton93e86192011-11-13 04:45:22 +0000708 m_stderr_data (),
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000709 m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive),
710 m_profile_data (),
Todd Fialaa3b89e22014-08-12 14:33:19 +0000711 m_iohandler_sync (false),
Greg Claytond495c532011-05-17 03:37:42 +0000712 m_memory_cache (*this),
713 m_allocated_memory_cache (*this),
Greg Claytone24c4ac2011-11-17 04:46:02 +0000714 m_should_detach (false),
Sean Callanan90539452011-09-20 23:01:51 +0000715 m_next_event_action_ap(),
Greg Clayton96249852013-04-18 16:57:27 +0000716 m_public_run_lock (),
Greg Clayton96249852013-04-18 16:57:27 +0000717 m_private_run_lock (),
Jim Inghamaacc3182012-06-06 00:29:30 +0000718 m_currently_handling_event(false),
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000719 m_finalize_called(false),
Greg Claytonf9b57b92013-05-10 23:48:10 +0000720 m_clear_thread_plans_on_stop (false),
Jim Ingham1460e4b2014-01-10 23:46:59 +0000721 m_force_next_event_delivery(false),
Jim Ingham0161b492013-02-09 01:29:05 +0000722 m_last_broadcast_state (eStateInvalid),
Jason Molenda69b6b632013-03-05 03:33:59 +0000723 m_destroy_in_process (false),
724 m_can_jit(eCanJITDontKnow)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725{
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000726 CheckInWithManager ();
Caroline Tice1559a462010-09-27 00:30:10 +0000727
Greg Clayton5160ce52013-03-27 23:08:40 +0000728 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000730 log->Printf ("%p Process::Process()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731
Todd Fiala4ceced32014-08-29 17:35:57 +0000732 if (!m_unix_signals_sp)
733 m_unix_signals_sp.reset (new UnixSignals ());
734
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000735 SetEventName (eBroadcastBitStateChanged, "state-changed");
736 SetEventName (eBroadcastBitInterrupt, "interrupt");
737 SetEventName (eBroadcastBitSTDOUT, "stdout-available");
738 SetEventName (eBroadcastBitSTDERR, "stderr-available");
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000739 SetEventName (eBroadcastBitProfileData, "profile-data-available");
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000740
Greg Clayton35a4cc52012-10-29 20:52:08 +0000741 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" );
742 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" );
743 m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume");
744
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 listener.StartListeningForEvents (this,
746 eBroadcastBitStateChanged |
747 eBroadcastBitInterrupt |
748 eBroadcastBitSTDOUT |
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000749 eBroadcastBitSTDERR |
750 eBroadcastBitProfileData);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751
752 m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +0000753 eBroadcastBitStateChanged |
754 eBroadcastBitInterrupt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755
756 m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster,
757 eBroadcastInternalStateControlStop |
758 eBroadcastInternalStateControlPause |
759 eBroadcastInternalStateControlResume);
Todd Fiala4ceced32014-08-29 17:35:57 +0000760 // We need something valid here, even if just the default UnixSignalsSP.
761 assert (m_unix_signals_sp && "null m_unix_signals_sp after initialization");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
764//----------------------------------------------------------------------
765// Destructor
766//----------------------------------------------------------------------
767Process::~Process()
768{
Greg Clayton5160ce52013-03-27 23:08:40 +0000769 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000771 log->Printf ("%p Process::~Process()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772 StopPrivateStateThread();
773}
774
Greg Clayton67cc0632012-08-22 17:17:09 +0000775const ProcessPropertiesSP &
776Process::GetGlobalProperties()
777{
778 static ProcessPropertiesSP g_settings_sp;
779 if (!g_settings_sp)
780 g_settings_sp.reset (new ProcessProperties (true));
781 return g_settings_sp;
782}
783
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784void
785Process::Finalize()
786{
Greg Claytone24c4ac2011-11-17 04:46:02 +0000787 switch (GetPrivateState())
788 {
789 case eStateConnected:
790 case eStateAttaching:
791 case eStateLaunching:
792 case eStateStopped:
793 case eStateRunning:
794 case eStateStepping:
795 case eStateCrashed:
796 case eStateSuspended:
797 if (GetShouldDetach())
Jim Inghamacff8952013-05-02 00:27:30 +0000798 {
799 // FIXME: This will have to be a process setting:
800 bool keep_stopped = false;
801 Detach(keep_stopped);
802 }
Greg Claytone24c4ac2011-11-17 04:46:02 +0000803 else
804 Destroy();
805 break;
806
807 case eStateInvalid:
808 case eStateUnloaded:
809 case eStateDetached:
810 case eStateExited:
811 break;
812 }
813
Greg Clayton1ed54f52011-10-01 00:45:15 +0000814 // Clear our broadcaster before we proceed with destroying
815 Broadcaster::Clear();
816
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817 // Do any cleanup needed prior to being destructed... Subclasses
818 // that override this method should call this superclass method as well.
Jim Inghamd0a3e122011-02-16 17:54:55 +0000819
820 // We need to destroy the loader before the derived Process class gets destroyed
821 // since it is very likely that undoing the loader will require access to the real process.
Greg Clayton894f82f2012-01-20 23:08:34 +0000822 m_dynamic_checkers_ap.reset();
823 m_abi_sp.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000824 m_os_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +0000825 m_system_runtime_ap.reset();
Greg Clayton894f82f2012-01-20 23:08:34 +0000826 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +0000827 m_jit_loaders_ap.reset();
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000828 m_thread_list_real.Destroy();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000829 m_thread_list.Destroy();
Jason Molenda864f1cc2013-11-11 05:20:44 +0000830 m_extended_thread_list.Destroy();
Jason Molenda5e8dce42013-12-13 00:29:16 +0000831 m_queue_list.Clear();
832 m_queue_list_stop_id = 0;
Greg Clayton894f82f2012-01-20 23:08:34 +0000833 std::vector<Notifications> empty_notifications;
834 m_notifications.swap(empty_notifications);
835 m_image_tokens.clear();
836 m_memory_cache.Clear();
837 m_allocated_memory_cache.Clear();
838 m_language_runtimes.clear();
839 m_next_event_action_ap.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000840//#ifdef LLDB_CONFIGURATION_DEBUG
841// StreamFile s(stdout, false);
842// EventSP event_sp;
843// while (m_private_state_listener.GetNextEvent(event_sp))
844// {
845// event_sp->Dump (&s);
846// s.EOL();
847// }
848//#endif
849 // We have to be very careful here as the m_private_state_listener might
850 // contain events that have ProcessSP values in them which can keep this
851 // process around forever. These events need to be cleared out.
852 m_private_state_listener.Clear();
Ed Maste64fad602013-07-29 20:58:06 +0000853 m_public_run_lock.TrySetRunning(); // This will do nothing if already locked
854 m_public_run_lock.SetStopped();
855 m_private_run_lock.TrySetRunning(); // This will do nothing if already locked
856 m_private_run_lock.SetStopped();
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000857 m_finalize_called = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858}
859
860void
861Process::RegisterNotificationCallbacks (const Notifications& callbacks)
862{
863 m_notifications.push_back(callbacks);
864 if (callbacks.initialize != NULL)
865 callbacks.initialize (callbacks.baton, this);
866}
867
868bool
869Process::UnregisterNotificationCallbacks(const Notifications& callbacks)
870{
871 std::vector<Notifications>::iterator pos, end = m_notifications.end();
872 for (pos = m_notifications.begin(); pos != end; ++pos)
873 {
874 if (pos->baton == callbacks.baton &&
875 pos->initialize == callbacks.initialize &&
876 pos->process_state_changed == callbacks.process_state_changed)
877 {
878 m_notifications.erase(pos);
879 return true;
880 }
881 }
882 return false;
883}
884
885void
886Process::SynchronouslyNotifyStateChanged (StateType state)
887{
888 std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end();
889 for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos)
890 {
891 if (notification_pos->process_state_changed)
892 notification_pos->process_state_changed (notification_pos->baton, this, state);
893 }
894}
895
896// FIXME: We need to do some work on events before the general Listener sees them.
897// For instance if we are continuing from a breakpoint, we need to ensure that we do
898// the little "insert real insn, step & stop" trick. But we can't do that when the
899// event is delivered by the broadcaster - since that is done on the thread that is
900// waiting for new events, so if we needed more than one event for our handling, we would
901// stall. So instead we do it when we fetch the event off of the queue.
902//
903
904StateType
905Process::GetNextEvent (EventSP &event_sp)
906{
907 StateType state = eStateInvalid;
908
909 if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp)
910 state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
911
912 return state;
913}
914
Todd Fialaa3b89e22014-08-12 14:33:19 +0000915bool
916Process::SyncIOHandler (uint64_t timeout_msec)
917{
918 bool timed_out = false;
919
920 // don't sync (potentially context switch) in case where there is no process IO
921 if (m_process_input_reader)
922 {
923 TimeValue timeout = TimeValue::Now();
924 timeout.OffsetWithMicroSeconds(timeout_msec*1000);
925
926 m_iohandler_sync.WaitForValueEqualTo(true, &timeout, &timed_out);
927
928 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
929 if(log)
930 {
931 if(timed_out)
932 log->Printf ("Process::%s pid %" PRIu64 " (timeout=%" PRIu64 "ms): FAIL", __FUNCTION__, GetID (), timeout_msec);
933 else
934 log->Printf ("Process::%s pid %" PRIu64 ": SUCCESS", __FUNCTION__, GetID ());
935 }
936
937 // reset sync one-shot so it will be ready for next time
938 m_iohandler_sync.SetValue(false, eBroadcastNever);
939 }
940
941 return !timed_out;
942}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943
944StateType
Greg Clayton44d93782014-01-27 23:43:24 +0000945Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr, bool wait_always, Listener *hijack_listener)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946{
Jim Ingham4b536182011-08-09 02:12:22 +0000947 // We can't just wait for a "stopped" event, because the stopped event may have restarted the target.
948 // We have to actually check each event, and in the case of a stopped event check the restarted flag
949 // on the event.
Greg Clayton85fb1b92012-09-11 02:33:37 +0000950 if (event_sp_ptr)
951 event_sp_ptr->reset();
Jim Ingham4b536182011-08-09 02:12:22 +0000952 StateType state = GetState();
953 // If we are exited or detached, we won't ever get back to any
954 // other valid state...
955 if (state == eStateDetached || state == eStateExited)
956 return state;
957
Daniel Malea9e9919f2013-10-09 16:56:28 +0000958 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
959 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000960 log->Printf ("Process::%s (timeout = %p)", __FUNCTION__,
961 static_cast<const void*>(timeout));
Daniel Malea9e9919f2013-10-09 16:56:28 +0000962
963 if (!wait_always &&
964 StateIsStoppedState(state, true) &&
965 StateIsStoppedState(GetPrivateState(), true)) {
966 if (log)
967 log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.",
968 __FUNCTION__);
969 return state;
970 }
971
Jim Ingham4b536182011-08-09 02:12:22 +0000972 while (state != eStateInvalid)
973 {
Greg Clayton85fb1b92012-09-11 02:33:37 +0000974 EventSP event_sp;
Greg Clayton44d93782014-01-27 23:43:24 +0000975 state = WaitForStateChangedEvents (timeout, event_sp, hijack_listener);
Greg Clayton85fb1b92012-09-11 02:33:37 +0000976 if (event_sp_ptr && event_sp)
977 *event_sp_ptr = event_sp;
978
Jim Ingham4b536182011-08-09 02:12:22 +0000979 switch (state)
980 {
981 case eStateCrashed:
982 case eStateDetached:
983 case eStateExited:
984 case eStateUnloaded:
Greg Clayton44d93782014-01-27 23:43:24 +0000985 // We need to toggle the run lock as this won't get done in
986 // SetPublicState() if the process is hijacked.
987 if (hijack_listener)
988 m_public_run_lock.SetStopped();
Jim Ingham4b536182011-08-09 02:12:22 +0000989 return state;
990 case eStateStopped:
991 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
992 continue;
993 else
Greg Clayton44d93782014-01-27 23:43:24 +0000994 {
995 // We need to toggle the run lock as this won't get done in
996 // SetPublicState() if the process is hijacked.
997 if (hijack_listener)
998 m_public_run_lock.SetStopped();
Jim Ingham4b536182011-08-09 02:12:22 +0000999 return state;
Greg Clayton44d93782014-01-27 23:43:24 +00001000 }
Jim Ingham4b536182011-08-09 02:12:22 +00001001 default:
1002 continue;
1003 }
1004 }
1005 return state;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001006}
1007
1008
1009StateType
1010Process::WaitForState
1011(
1012 const TimeValue *timeout,
Greg Clayton44d93782014-01-27 23:43:24 +00001013 const StateType *match_states,
1014 const uint32_t num_match_states
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015)
1016{
1017 EventSP event_sp;
1018 uint32_t i;
Greg Clayton05faeb72010-10-07 04:19:01 +00001019 StateType state = GetState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020 while (state != eStateInvalid)
1021 {
Greg Clayton05faeb72010-10-07 04:19:01 +00001022 // If we are exited or detached, we won't ever get back to any
1023 // other valid state...
1024 if (state == eStateDetached || state == eStateExited)
1025 return state;
1026
Greg Clayton44d93782014-01-27 23:43:24 +00001027 state = WaitForStateChangedEvents (timeout, event_sp, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028
1029 for (i=0; i<num_match_states; ++i)
1030 {
1031 if (match_states[i] == state)
1032 return state;
1033 }
1034 }
1035 return state;
1036}
1037
Jim Ingham30f9b212010-10-11 23:53:14 +00001038bool
1039Process::HijackProcessEvents (Listener *listener)
1040{
1041 if (listener != NULL)
1042 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001043 return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham30f9b212010-10-11 23:53:14 +00001044 }
1045 else
1046 return false;
1047}
1048
1049void
1050Process::RestoreProcessEvents ()
1051{
1052 RestoreBroadcaster();
1053}
1054
Jim Ingham0f16e732011-02-08 05:20:59 +00001055bool
1056Process::HijackPrivateProcessEvents (Listener *listener)
1057{
1058 if (listener != NULL)
1059 {
Jim Inghamcfc09352012-07-27 23:57:19 +00001060 return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt);
Jim Ingham0f16e732011-02-08 05:20:59 +00001061 }
1062 else
1063 return false;
1064}
1065
1066void
1067Process::RestorePrivateProcessEvents ()
1068{
1069 m_private_state_broadcaster.RestoreBroadcaster();
1070}
1071
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072StateType
Greg Clayton44d93782014-01-27 23:43:24 +00001073Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp, Listener *hijack_listener)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074{
Greg Clayton5160ce52013-03-27 23:08:40 +00001075 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076
1077 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001078 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1079 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080
Greg Clayton44d93782014-01-27 23:43:24 +00001081 Listener *listener = hijack_listener;
1082 if (listener == NULL)
1083 listener = &m_listener;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001084
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001085 StateType state = eStateInvalid;
Greg Clayton44d93782014-01-27 23:43:24 +00001086 if (listener->WaitForEventForBroadcasterWithType (timeout,
1087 this,
1088 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
1089 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001090 {
1091 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1092 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
1093 else if (log)
1094 log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__);
1095 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096
1097 if (log)
1098 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001099 __FUNCTION__, static_cast<const void*>(timeout),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100 StateAsCString(state));
1101 return state;
1102}
1103
1104Event *
1105Process::PeekAtStateChangedEvents ()
1106{
Greg Clayton5160ce52013-03-27 23:08:40 +00001107 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108
1109 if (log)
1110 log->Printf ("Process::%s...", __FUNCTION__);
1111
1112 Event *event_ptr;
Greg Clayton3fcbed62010-10-19 03:25:40 +00001113 event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this,
1114 eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115 if (log)
1116 {
1117 if (event_ptr)
1118 {
1119 log->Printf ("Process::%s (event_ptr) => %s",
1120 __FUNCTION__,
1121 StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr)));
1122 }
1123 else
1124 {
1125 log->Printf ("Process::%s no events found",
1126 __FUNCTION__);
1127 }
1128 }
1129 return event_ptr;
1130}
1131
1132StateType
1133Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp)
1134{
Greg Clayton5160ce52013-03-27 23:08:40 +00001135 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136
1137 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001138 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1139 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140
1141 StateType state = eStateInvalid;
Greg Clayton6779606a2011-01-22 23:43:18 +00001142 if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout,
1143 &m_private_state_broadcaster,
Jim Inghamcfc09352012-07-27 23:57:19 +00001144 eBroadcastBitStateChanged | eBroadcastBitInterrupt,
Greg Clayton6779606a2011-01-22 23:43:18 +00001145 event_sp))
Jim Inghamcfc09352012-07-27 23:57:19 +00001146 if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged)
1147 state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148
1149 // This is a bit of a hack, but when we wait here we could very well return
1150 // to the command-line, and that could disable the log, which would render the
1151 // log we got above invalid.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001153 log->Printf ("Process::%s (timeout = %p, event_sp) => %s",
1154 __FUNCTION__, static_cast<const void *>(timeout),
1155 state == eStateInvalid ? "TIMEOUT" : StateAsCString(state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 return state;
1157}
1158
1159bool
1160Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only)
1161{
Greg Clayton5160ce52013-03-27 23:08:40 +00001162 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163
1164 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001165 log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__,
1166 static_cast<const void*>(timeout));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167
1168 if (control_only)
1169 return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp);
1170 else
1171 return m_private_state_listener.WaitForEvent(timeout, event_sp);
1172}
1173
1174bool
1175Process::IsRunning () const
1176{
1177 return StateIsRunningState (m_public_state.GetValue());
1178}
1179
1180int
1181Process::GetExitStatus ()
1182{
1183 if (m_public_state.GetValue() == eStateExited)
1184 return m_exit_status;
1185 return -1;
1186}
1187
Greg Clayton85851dd2010-12-04 00:10:17 +00001188
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189const char *
1190Process::GetExitDescription ()
1191{
1192 if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty())
1193 return m_exit_string.c_str();
1194 return NULL;
1195}
1196
Greg Clayton6779606a2011-01-22 23:43:18 +00001197bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198Process::SetExitStatus (int status, const char *cstr)
1199{
Greg Clayton5160ce52013-03-27 23:08:40 +00001200 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Greg Clayton414f5d32011-01-25 02:58:48 +00001201 if (log)
1202 log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)",
1203 status, status,
1204 cstr ? "\"" : "",
1205 cstr ? cstr : "NULL",
1206 cstr ? "\"" : "");
1207
Greg Clayton6779606a2011-01-22 23:43:18 +00001208 // We were already in the exited state
1209 if (m_private_state.GetValue() == eStateExited)
Greg Clayton414f5d32011-01-25 02:58:48 +00001210 {
Greg Clayton385d6032011-01-26 23:47:29 +00001211 if (log)
1212 log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited");
Greg Clayton6779606a2011-01-22 23:43:18 +00001213 return false;
Greg Clayton414f5d32011-01-25 02:58:48 +00001214 }
Greg Clayton6779606a2011-01-22 23:43:18 +00001215
1216 m_exit_status = status;
1217 if (cstr)
1218 m_exit_string = cstr;
1219 else
1220 m_exit_string.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221
Greg Clayton6779606a2011-01-22 23:43:18 +00001222 DidExit ();
Greg Clayton10177aa2010-12-08 05:08:21 +00001223
Greg Clayton6779606a2011-01-22 23:43:18 +00001224 SetPrivateState (eStateExited);
1225 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226}
1227
1228// This static callback can be used to watch for local child processes on
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001229// the current host. The child process exits, the process will be
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230// found in the global target list (we want to be completely sure that the
1231// lldb_private::Process doesn't go away before we can deliver the signal.
1232bool
Greg Claytone4e45922011-11-16 05:37:56 +00001233Process::SetProcessExitStatus (void *callback_baton,
1234 lldb::pid_t pid,
1235 bool exited,
1236 int signo, // Zero for no signal
1237 int exit_status // Exit value of process if signal is zero
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238)
1239{
Greg Clayton5160ce52013-03-27 23:08:40 +00001240 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytone4e45922011-11-16 05:37:56 +00001241 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001242 log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n",
Greg Claytone4e45922011-11-16 05:37:56 +00001243 callback_baton,
1244 pid,
1245 exited,
1246 signo,
1247 exit_status);
1248
1249 if (exited)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 {
Greg Clayton66111032010-06-23 01:19:29 +00001251 TargetSP target_sp(Debugger::FindTargetWithProcessID (pid));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252 if (target_sp)
1253 {
1254 ProcessSP process_sp (target_sp->GetProcessSP());
1255 if (process_sp)
1256 {
1257 const char *signal_cstr = NULL;
1258 if (signo)
1259 signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo);
1260
1261 process_sp->SetExitStatus (exit_status, signal_cstr);
1262 }
1263 }
1264 return true;
1265 }
1266 return false;
1267}
1268
1269
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001270void
1271Process::UpdateThreadListIfNeeded ()
1272{
1273 const uint32_t stop_id = GetStopID();
1274 if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID())
1275 {
Greg Clayton2637f822011-11-17 01:23:07 +00001276 const StateType state = GetPrivateState();
1277 if (StateIsStoppedState (state, true))
1278 {
1279 Mutex::Locker locker (m_thread_list.GetMutex ());
Greg Claytone24c4ac2011-11-17 04:46:02 +00001280 // m_thread_list does have its own mutex, but we need to
1281 // hold onto the mutex between the call to UpdateThreadList(...)
1282 // and the os->UpdateThreadList(...) so it doesn't change on us
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001283 ThreadList &old_thread_list = m_thread_list;
1284 ThreadList real_thread_list(this);
Greg Clayton2637f822011-11-17 01:23:07 +00001285 ThreadList new_thread_list(this);
1286 // Always update the thread list with the protocol specific
Greg Clayton9fc13552012-04-10 00:18:59 +00001287 // thread list, but only update if "true" is returned
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001288 if (UpdateThreadList (m_thread_list_real, real_thread_list))
Greg Clayton9fc13552012-04-10 00:18:59 +00001289 {
Jim Ingham09437922013-03-01 20:04:25 +00001290 // Don't call into the OperatingSystem to update the thread list if we are shutting down, since
1291 // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is
1292 // shutting us down, causing a deadlock.
1293 if (!m_destroy_in_process)
1294 {
1295 OperatingSystem *os = GetOperatingSystem ();
1296 if (os)
Greg Claytonb3ae8762013-04-12 20:07:46 +00001297 {
1298 // Clear any old backing threads where memory threads might have been
1299 // backed by actual threads from the lldb_private::Process subclass
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001300 size_t num_old_threads = old_thread_list.GetSize(false);
Greg Claytonb3ae8762013-04-12 20:07:46 +00001301 for (size_t i=0; i<num_old_threads; ++i)
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001302 old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread();
Greg Claytonb3ae8762013-04-12 20:07:46 +00001303
1304 // Now let the OperatingSystem plug-in update the thread list
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001305 os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in
1306 real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass
1307 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 +00001308 }
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001309 else
1310 {
1311 // No OS plug-in, the new thread list is the same as the real thread list
1312 new_thread_list = real_thread_list;
1313 }
Jim Ingham09437922013-03-01 20:04:25 +00001314 }
Jim Ingham02ff8e02013-06-22 00:55:02 +00001315
1316 m_thread_list_real.Update(real_thread_list);
Andrew Kaylorba4e61d2013-05-07 18:35:34 +00001317 m_thread_list.Update (new_thread_list);
1318 m_thread_list.SetStopID (stop_id);
Jason Molendaa6e91302013-11-19 05:44:41 +00001319
Jason Molenda4ff13262013-11-20 00:31:38 +00001320 if (GetLastNaturalStopID () != m_extended_thread_stop_id)
1321 {
1322 // Clear any extended threads that we may have accumulated previously
1323 m_extended_thread_list.Clear();
1324 m_extended_thread_stop_id = GetLastNaturalStopID ();
Jason Molenda5e8dce42013-12-13 00:29:16 +00001325
1326 m_queue_list.Clear();
1327 m_queue_list_stop_id = GetLastNaturalStopID ();
Jason Molenda4ff13262013-11-20 00:31:38 +00001328 }
Greg Clayton9fc13552012-04-10 00:18:59 +00001329 }
Greg Clayton2637f822011-11-17 01:23:07 +00001330 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001331 }
1332}
1333
Jason Molenda5e8dce42013-12-13 00:29:16 +00001334void
1335Process::UpdateQueueListIfNeeded ()
1336{
1337 if (m_system_runtime_ap.get())
1338 {
1339 if (m_queue_list.GetSize() == 0 || m_queue_list_stop_id != GetLastNaturalStopID())
1340 {
1341 const StateType state = GetPrivateState();
1342 if (StateIsStoppedState (state, true))
1343 {
1344 m_system_runtime_ap->PopulateQueueList (m_queue_list);
1345 m_queue_list_stop_id = GetLastNaturalStopID();
1346 }
1347 }
1348 }
1349}
1350
Greg Claytona4d87472013-01-18 23:41:08 +00001351ThreadSP
1352Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context)
1353{
1354 OperatingSystem *os = GetOperatingSystem ();
1355 if (os)
1356 return os->CreateThread(tid, context);
1357 return ThreadSP();
1358}
1359
Han Ming Ongc2c423e2013-01-08 22:10:01 +00001360uint32_t
1361Process::GetNextThreadIndexID (uint64_t thread_id)
1362{
1363 return AssignIndexIDToThread(thread_id);
1364}
1365
1366bool
1367Process::HasAssignedIndexIDToThread(uint64_t thread_id)
1368{
1369 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1370 if (iterator == m_thread_id_to_index_id_map.end())
1371 {
1372 return false;
1373 }
1374 else
1375 {
1376 return true;
1377 }
1378}
1379
1380uint32_t
1381Process::AssignIndexIDToThread(uint64_t thread_id)
1382{
1383 uint32_t result = 0;
1384 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id);
1385 if (iterator == m_thread_id_to_index_id_map.end())
1386 {
1387 result = ++m_thread_index_id;
1388 m_thread_id_to_index_id_map[thread_id] = result;
1389 }
1390 else
1391 {
1392 result = iterator->second;
1393 }
1394
1395 return result;
1396}
1397
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001398StateType
1399Process::GetState()
1400{
1401 // If any other threads access this we will need a mutex for it
1402 return m_public_state.GetValue ();
1403}
1404
1405void
Jim Ingham221d51c2013-05-08 00:35:16 +00001406Process::SetPublicState (StateType new_state, bool restarted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407{
Greg Clayton5160ce52013-03-27 23:08:40 +00001408 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +00001410 log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001411 const StateType old_state = m_public_state.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 m_public_state.SetValue (new_state);
Jim Ingham3b8285d2012-04-19 01:40:33 +00001413
1414 // On the transition from Run to Stopped, we unlock the writer end of the
1415 // run lock. The lock gets locked in Resume, which is the public API
1416 // to tell the program to run.
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001417 if (!IsHijackedForEvent(eBroadcastBitStateChanged))
1418 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001419 if (new_state == eStateDetached)
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001420 {
Sean Callanan8b0737f2012-06-02 01:16:20 +00001421 if (log)
1422 log->Printf("Process::SetPublicState (%s) -- unlocking run lock for detach", StateAsCString(new_state));
Ed Maste64fad602013-07-29 20:58:06 +00001423 m_public_run_lock.SetStopped();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001424 }
1425 else
1426 {
1427 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1428 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
Jim Ingham221d51c2013-05-08 00:35:16 +00001429 if ((old_state_is_stopped != new_state_is_stopped))
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001430 {
Jim Ingham221d51c2013-05-08 00:35:16 +00001431 if (new_state_is_stopped && !restarted)
Sean Callanan8b0737f2012-06-02 01:16:20 +00001432 {
1433 if (log)
1434 log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state));
Ed Maste64fad602013-07-29 20:58:06 +00001435 m_public_run_lock.SetStopped();
Sean Callanan8b0737f2012-06-02 01:16:20 +00001436 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001437 }
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001438 }
1439 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440}
1441
Jim Ingham3b8285d2012-04-19 01:40:33 +00001442Error
1443Process::Resume ()
1444{
Greg Clayton5160ce52013-03-27 23:08:40 +00001445 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Jim Ingham3b8285d2012-04-19 01:40:33 +00001446 if (log)
1447 log->Printf("Process::Resume -- locking run lock");
Ed Maste64fad602013-07-29 20:58:06 +00001448 if (!m_public_run_lock.TrySetRunning())
Jim Ingham3b8285d2012-04-19 01:40:33 +00001449 {
1450 Error error("Resume request failed - process still running.");
1451 if (log)
Ed Maste64fad602013-07-29 20:58:06 +00001452 log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming.");
Jim Ingham3b8285d2012-04-19 01:40:33 +00001453 return error;
1454 }
1455 return PrivateResume();
1456}
1457
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458StateType
1459Process::GetPrivateState ()
1460{
1461 return m_private_state.GetValue();
1462}
1463
1464void
1465Process::SetPrivateState (StateType new_state)
1466{
Greg Claytonfb8b37a2014-07-14 23:09:29 +00001467 if (m_finalize_called)
1468 return;
1469
Greg Clayton5160ce52013-03-27 23:08:40 +00001470 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 bool state_changed = false;
1472
1473 if (log)
1474 log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state));
1475
Andrew Kaylor29d65742013-05-10 17:19:04 +00001476 Mutex::Locker thread_locker(m_thread_list.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001477 Mutex::Locker locker(m_private_state.GetMutex());
1478
1479 const StateType old_state = m_private_state.GetValueNoLock ();
1480 state_changed = old_state != new_state;
Ed Mastec29693f2013-07-02 16:35:47 +00001481
Greg Claytonaa49c832013-05-03 22:25:56 +00001482 const bool old_state_is_stopped = StateIsStoppedState(old_state, false);
1483 const bool new_state_is_stopped = StateIsStoppedState(new_state, false);
1484 if (old_state_is_stopped != new_state_is_stopped)
1485 {
1486 if (new_state_is_stopped)
Ed Maste64fad602013-07-29 20:58:06 +00001487 m_private_run_lock.SetStopped();
Greg Claytonaa49c832013-05-03 22:25:56 +00001488 else
Ed Maste64fad602013-07-29 20:58:06 +00001489 m_private_run_lock.SetRunning();
Greg Claytonaa49c832013-05-03 22:25:56 +00001490 }
Andrew Kaylor93132f52013-05-28 23:04:25 +00001491
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492 if (state_changed)
1493 {
1494 m_private_state.SetValueNoLock (new_state);
Greg Clayton2637f822011-11-17 01:23:07 +00001495 if (StateIsStoppedState(new_state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 {
Andrew Kaylor29d65742013-05-10 17:19:04 +00001497 // Note, this currently assumes that all threads in the list
1498 // stop when the process stops. In the future we will want to
1499 // support a debugging model where some threads continue to run
1500 // while others are stopped. When that happens we will either need
1501 // a way for the thread list to identify which threads are stopping
1502 // or create a special thread list containing only threads which
1503 // actually stopped.
1504 //
1505 // The process plugin is responsible for managing the actual
1506 // behavior of the threads and should have stopped any threads
1507 // that are going to stop before we get here.
1508 m_thread_list.DidStop();
1509
Jim Ingham4b536182011-08-09 02:12:22 +00001510 m_mod_id.BumpStopID();
Greg Clayton58be07b2011-01-07 06:08:19 +00001511 m_memory_cache.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 if (log)
Jim Ingham4b536182011-08-09 02:12:22 +00001513 log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514 }
1515 // Use our target to get a shared pointer to ourselves...
Greg Clayton35a4cc52012-10-29 20:52:08 +00001516 if (m_finalize_called && PrivateStateThreadIsValid() == false)
1517 BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
1518 else
1519 m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 }
1521 else
1522 {
1523 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001524 log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001525 }
1526}
1527
Jim Ingham0faa43f2011-11-08 03:00:11 +00001528void
1529Process::SetRunningUserExpression (bool on)
1530{
1531 m_mod_id.SetRunningUserExpression (on);
1532}
1533
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001534addr_t
1535Process::GetImageInfoAddress()
1536{
1537 return LLDB_INVALID_ADDRESS;
1538}
1539
Greg Clayton8f343b02010-11-04 01:54:29 +00001540//----------------------------------------------------------------------
1541// LoadImage
1542//
1543// This function provides a default implementation that works for most
1544// unix variants. Any Process subclasses that need to do shared library
1545// loading differently should override LoadImage and UnloadImage and
1546// do what is needed.
1547//----------------------------------------------------------------------
1548uint32_t
1549Process::LoadImage (const FileSpec &image_spec, Error &error)
1550{
Greg Claytonac7a3db2012-04-18 00:05:19 +00001551 char path[PATH_MAX];
1552 image_spec.GetPath(path, sizeof(path));
1553
Greg Clayton8f343b02010-11-04 01:54:29 +00001554 DynamicLoader *loader = GetDynamicLoader();
1555 if (loader)
1556 {
1557 error = loader->CanLoadImage();
1558 if (error.Fail())
1559 return LLDB_INVALID_IMAGE_TOKEN;
1560 }
1561
1562 if (error.Success())
1563 {
1564 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001565
1566 if (thread_sp)
1567 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001568 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
Greg Clayton8f343b02010-11-04 01:54:29 +00001569
1570 if (frame_sp)
1571 {
1572 ExecutionContext exe_ctx;
1573 frame_sp->CalculateExecutionContext (exe_ctx);
Greg Clayton62afb9f2013-11-04 19:35:17 +00001574 EvaluateExpressionOptions expr_options;
1575 expr_options.SetUnwindOnError(true);
1576 expr_options.SetIgnoreBreakpoints(true);
1577 expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
Jim Ingham4ac04432014-07-19 01:09:16 +00001578 expr_options.SetResultIsInternal(true);
1579
Greg Clayton8f343b02010-11-04 01:54:29 +00001580 StreamString expr;
Jim Ingham6971b862014-07-19 00:37:06 +00001581 expr.Printf(R"(
1582 struct __lldb_dlopen_result { void *image_ptr; const char *error_str; } the_result;
1583 the_result.image_ptr = dlopen ("%s", 2);
1584 if (the_result.image_ptr == (void *) 0x0)
1585 {
1586 the_result.error_str = dlerror();
1587 }
1588 else
1589 {
1590 the_result.error_str = (const char *) 0x0;
1591 }
1592 the_result;
1593 )",
1594 path);
1595 const char *prefix = R"(
1596 extern "C" void* dlopen (const char *path, int mode);
1597 extern "C" const char *dlerror (void);
1598 )";
Jim Inghamf48169b2010-11-30 02:22:11 +00001599 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +00001600 Error expr_error;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001601 ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001602 expr_options,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001603 expr.GetData(),
1604 prefix,
1605 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001606 expr_error);
1607 if (expr_error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001608 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001609 error = result_valobj_sp->GetError();
1610 if (error.Success())
Greg Clayton8f343b02010-11-04 01:54:29 +00001611 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001612 Scalar scalar;
Jim Ingham6971b862014-07-19 00:37:06 +00001613 ValueObjectSP image_ptr_sp = result_valobj_sp->GetChildAtIndex(0, true);
1614 if (image_ptr_sp && image_ptr_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001615 {
Greg Clayton62afb9f2013-11-04 19:35:17 +00001616 addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1617 if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
1618 {
1619 uint32_t image_token = m_image_tokens.size();
1620 m_image_tokens.push_back (image_ptr);
1621 return image_token;
1622 }
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001623 else if (image_ptr == 0)
1624 {
Jim Ingham6971b862014-07-19 00:37:06 +00001625 ValueObjectSP error_str_sp = result_valobj_sp->GetChildAtIndex(1, true);
1626 if (error_str_sp)
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001627 {
Jim Ingham6971b862014-07-19 00:37:06 +00001628 if (error_str_sp->IsCStringContainer(true))
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001629 {
Jim Inghamcf973792014-07-17 21:53:48 +00001630 StreamString s;
Jim Ingham6971b862014-07-19 00:37:06 +00001631 size_t num_chars = error_str_sp->ReadPointedString (s, error);
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001632 if (error.Success() && num_chars > 0)
1633 {
1634 error.Clear();
Jim Ingham6971b862014-07-19 00:37:06 +00001635 error.SetErrorStringWithFormat("dlopen error: %s", s.GetData());
Jim Ingham3ac7cf32014-07-17 18:55:25 +00001636 }
1637 }
1638 }
1639 }
Greg Clayton8f343b02010-11-04 01:54:29 +00001640 }
1641 }
1642 }
Jim Ingham6c9ed912014-04-03 01:26:14 +00001643 else
1644 error = expr_error;
Greg Clayton8f343b02010-11-04 01:54:29 +00001645 }
1646 }
1647 }
Greg Claytonac7a3db2012-04-18 00:05:19 +00001648 if (!error.AsCString())
1649 error.SetErrorStringWithFormat("unable to load '%s'", path);
Greg Clayton8f343b02010-11-04 01:54:29 +00001650 return LLDB_INVALID_IMAGE_TOKEN;
1651}
1652
1653//----------------------------------------------------------------------
1654// UnloadImage
1655//
1656// This function provides a default implementation that works for most
1657// unix variants. Any Process subclasses that need to do shared library
1658// loading differently should override LoadImage and UnloadImage and
1659// do what is needed.
1660//----------------------------------------------------------------------
1661Error
1662Process::UnloadImage (uint32_t image_token)
1663{
1664 Error error;
1665 if (image_token < m_image_tokens.size())
1666 {
1667 const addr_t image_addr = m_image_tokens[image_token];
1668 if (image_addr == LLDB_INVALID_ADDRESS)
1669 {
1670 error.SetErrorString("image already unloaded");
1671 }
1672 else
1673 {
1674 DynamicLoader *loader = GetDynamicLoader();
1675 if (loader)
1676 error = loader->CanLoadImage();
1677
1678 if (error.Success())
1679 {
1680 ThreadSP thread_sp(GetThreadList ().GetSelectedThread());
Greg Clayton8f343b02010-11-04 01:54:29 +00001681
1682 if (thread_sp)
1683 {
Jason Molendab57e4a12013-11-04 09:33:30 +00001684 StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0));
Greg Clayton8f343b02010-11-04 01:54:29 +00001685
1686 if (frame_sp)
1687 {
1688 ExecutionContext exe_ctx;
1689 frame_sp->CalculateExecutionContext (exe_ctx);
Greg Clayton62afb9f2013-11-04 19:35:17 +00001690 EvaluateExpressionOptions expr_options;
1691 expr_options.SetUnwindOnError(true);
1692 expr_options.SetIgnoreBreakpoints(true);
1693 expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
Greg Clayton8f343b02010-11-04 01:54:29 +00001694 StreamString expr;
Daniel Malead01b2952012-11-29 21:49:15 +00001695 expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr);
Greg Clayton8f343b02010-11-04 01:54:29 +00001696 const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
Jim Inghamf48169b2010-11-30 02:22:11 +00001697 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton62afb9f2013-11-04 19:35:17 +00001698 Error expr_error;
Greg Clayton26ab83d2012-10-31 20:49:04 +00001699 ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001700 expr_options,
Greg Clayton26ab83d2012-10-31 20:49:04 +00001701 expr.GetData(),
1702 prefix,
1703 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001704 expr_error);
Greg Clayton8f343b02010-11-04 01:54:29 +00001705 if (result_valobj_sp->GetError().Success())
1706 {
1707 Scalar scalar;
Jim Ingham6035b672011-03-31 00:19:25 +00001708 if (result_valobj_sp->ResolveValue (scalar))
Greg Clayton8f343b02010-11-04 01:54:29 +00001709 {
1710 if (scalar.UInt(1))
1711 {
1712 error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData());
1713 }
1714 else
1715 {
1716 m_image_tokens[image_token] = LLDB_INVALID_ADDRESS;
1717 }
1718 }
1719 }
1720 else
1721 {
1722 error = result_valobj_sp->GetError();
1723 }
1724 }
1725 }
1726 }
1727 }
1728 }
1729 else
1730 {
1731 error.SetErrorString("invalid image token");
1732 }
1733 return error;
1734}
1735
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001736const lldb::ABISP &
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001737Process::GetABI()
1738{
Greg Clayton31f1d2f2011-05-11 18:39:18 +00001739 if (!m_abi_sp)
1740 m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture());
1741 return m_abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001742}
1743
Jim Ingham22777012010-09-23 02:01:19 +00001744LanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001745Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001746{
1747 LanguageRuntimeCollection::iterator pos;
1748 pos = m_language_runtimes.find (language);
Jim Inghamab175242012-03-10 00:22:19 +00001749 if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second))
Jim Ingham22777012010-09-23 02:01:19 +00001750 {
Jim Inghamab175242012-03-10 00:22:19 +00001751 lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language));
Jim Ingham22777012010-09-23 02:01:19 +00001752
Jim Inghamab175242012-03-10 00:22:19 +00001753 m_language_runtimes[language] = runtime_sp;
1754 return runtime_sp.get();
Jim Ingham22777012010-09-23 02:01:19 +00001755 }
1756 else
1757 return (*pos).second.get();
1758}
1759
1760CPPLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001761Process::GetCPPLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001762{
Jim Inghamab175242012-03-10 00:22:19 +00001763 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00001764 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus)
1765 return static_cast<CPPLanguageRuntime *> (runtime);
1766 return NULL;
1767}
1768
1769ObjCLanguageRuntime *
Jim Inghamab175242012-03-10 00:22:19 +00001770Process::GetObjCLanguageRuntime (bool retry_if_null)
Jim Ingham22777012010-09-23 02:01:19 +00001771{
Jim Inghamab175242012-03-10 00:22:19 +00001772 LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null);
Jim Ingham22777012010-09-23 02:01:19 +00001773 if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC)
1774 return static_cast<ObjCLanguageRuntime *> (runtime);
1775 return NULL;
1776}
1777
Enrico Granatafd4c84e2012-05-21 16:51:35 +00001778bool
1779Process::IsPossibleDynamicValue (ValueObject& in_value)
1780{
1781 if (in_value.IsDynamic())
1782 return false;
1783 LanguageType known_type = in_value.GetObjectRuntimeLanguage();
1784
1785 if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
1786 {
1787 LanguageRuntime *runtime = GetLanguageRuntime (known_type);
1788 return runtime ? runtime->CouldHaveDynamicValue(in_value) : false;
1789 }
1790
1791 LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus);
1792 if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value))
1793 return true;
1794
1795 LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC);
1796 return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false;
1797}
1798
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799BreakpointSiteList &
1800Process::GetBreakpointSiteList()
1801{
1802 return m_breakpoint_site_list;
1803}
1804
1805const BreakpointSiteList &
1806Process::GetBreakpointSiteList() const
1807{
1808 return m_breakpoint_site_list;
1809}
1810
1811
1812void
1813Process::DisableAllBreakpointSites ()
1814{
Greg Claytond8cf1a12013-06-12 00:46:38 +00001815 m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void {
1816// bp_site->SetEnabled(true);
1817 DisableBreakpointSite(bp_site);
1818 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819}
1820
1821Error
1822Process::ClearBreakpointSiteByID (lldb::user_id_t break_id)
1823{
1824 Error error (DisableBreakpointSiteByID (break_id));
1825
1826 if (error.Success())
1827 m_breakpoint_site_list.Remove(break_id);
1828
1829 return error;
1830}
1831
1832Error
1833Process::DisableBreakpointSiteByID (lldb::user_id_t break_id)
1834{
1835 Error error;
1836 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1837 if (bp_site_sp)
1838 {
1839 if (bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00001840 error = DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841 }
1842 else
1843 {
Daniel Malead01b2952012-11-29 21:49:15 +00001844 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001845 }
1846
1847 return error;
1848}
1849
1850Error
1851Process::EnableBreakpointSiteByID (lldb::user_id_t break_id)
1852{
1853 Error error;
1854 BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id);
1855 if (bp_site_sp)
1856 {
1857 if (!bp_site_sp->IsEnabled())
Jim Ingham299c0c12013-02-15 02:06:30 +00001858 error = EnableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001859 }
1860 else
1861 {
Daniel Malead01b2952012-11-29 21:49:15 +00001862 error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001863 }
1864 return error;
1865}
1866
Stephen Wilson50bd94f2010-07-17 00:56:13 +00001867lldb::break_id_t
Greg Claytone1cd1be2012-01-29 20:56:30 +00001868Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869{
Jim Ingham1460e4b2014-01-10 23:46:59 +00001870 addr_t load_addr = LLDB_INVALID_ADDRESS;
1871
1872 bool show_error = true;
1873 switch (GetState())
1874 {
1875 case eStateInvalid:
1876 case eStateUnloaded:
1877 case eStateConnected:
1878 case eStateAttaching:
1879 case eStateLaunching:
1880 case eStateDetached:
1881 case eStateExited:
1882 show_error = false;
1883 break;
1884
1885 case eStateStopped:
1886 case eStateRunning:
1887 case eStateStepping:
1888 case eStateCrashed:
1889 case eStateSuspended:
1890 show_error = IsAlive();
1891 break;
1892 }
1893
1894 // Reset the IsIndirect flag here, in case the location changes from
1895 // pointing to a indirect symbol to a regular symbol.
1896 owner->SetIsIndirect (false);
1897
1898 if (owner->ShouldResolveIndirectFunctions())
1899 {
1900 Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol();
1901 if (symbol && symbol->IsIndirect())
1902 {
1903 Error error;
1904 load_addr = ResolveIndirectFunction (&symbol->GetAddress(), error);
1905 if (!error.Success() && show_error)
1906 {
Greg Clayton44d93782014-01-27 23:43:24 +00001907 m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to resolve indirect function at 0x%" PRIx64 " for breakpoint %i.%i: %s\n",
1908 symbol->GetAddress().GetLoadAddress(&m_target),
1909 owner->GetBreakpoint().GetID(),
1910 owner->GetID(),
Sylvestre Ledruf6102892014-08-11 18:06:28 +00001911 error.AsCString() ? error.AsCString() : "unknown error");
Jim Ingham1460e4b2014-01-10 23:46:59 +00001912 return LLDB_INVALID_BREAK_ID;
1913 }
1914 Address resolved_address(load_addr);
1915 load_addr = resolved_address.GetOpcodeLoadAddress (&m_target);
1916 owner->SetIsIndirect(true);
1917 }
1918 else
1919 load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
1920 }
1921 else
1922 load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target);
1923
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001924 if (load_addr != LLDB_INVALID_ADDRESS)
1925 {
1926 BreakpointSiteSP bp_site_sp;
1927
1928 // Look up this breakpoint site. If it exists, then add this new owner, otherwise
1929 // create a new breakpoint site and add it.
1930
1931 bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr);
1932
1933 if (bp_site_sp)
1934 {
1935 bp_site_sp->AddOwner (owner);
1936 owner->SetBreakpointSite (bp_site_sp);
1937 return bp_site_sp->GetID();
1938 }
1939 else
1940 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001941 bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, use_hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001942 if (bp_site_sp)
1943 {
Greg Claytoneb023e72013-10-11 19:48:25 +00001944 Error error = EnableBreakpointSite (bp_site_sp.get());
1945 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946 {
1947 owner->SetBreakpointSite (bp_site_sp);
1948 return m_breakpoint_site_list.Add (bp_site_sp);
1949 }
Greg Claytoneb023e72013-10-11 19:48:25 +00001950 else
1951 {
Greg Claytonfbb76342013-11-20 21:07:01 +00001952 if (show_error)
1953 {
1954 // Report error for setting breakpoint...
Greg Clayton44d93782014-01-27 23:43:24 +00001955 m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n",
1956 load_addr,
1957 owner->GetBreakpoint().GetID(),
1958 owner->GetID(),
Sylvestre Ledruf6102892014-08-11 18:06:28 +00001959 error.AsCString() ? error.AsCString() : "unknown error");
Greg Claytonfbb76342013-11-20 21:07:01 +00001960 }
Greg Claytoneb023e72013-10-11 19:48:25 +00001961 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001962 }
1963 }
1964 }
1965 // We failed to enable the breakpoint
1966 return LLDB_INVALID_BREAK_ID;
1967
1968}
1969
1970void
1971Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp)
1972{
1973 uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id);
1974 if (num_owners == 0)
1975 {
Jim Inghamf1ff3bb2013-04-06 00:16:39 +00001976 // Don't try to disable the site if we don't have a live process anymore.
1977 if (IsAlive())
1978 DisableBreakpointSite (bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979 m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress());
1980 }
1981}
1982
1983
1984size_t
1985Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const
1986{
1987 size_t bytes_removed = 0;
Jim Ingham20c77192011-06-29 19:42:28 +00001988 BreakpointSiteList bp_sites_in_range;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001989
Jim Ingham20c77192011-06-29 19:42:28 +00001990 if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001991 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00001992 bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void {
1993 if (bp_site->GetType() == BreakpointSite::eSoftware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001994 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00001995 addr_t intersect_addr;
1996 size_t intersect_size;
1997 size_t opcode_offset;
1998 if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset))
Jim Ingham20c77192011-06-29 19:42:28 +00001999 {
2000 assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size);
2001 assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size);
Greg Claytond8cf1a12013-06-12 00:46:38 +00002002 assert(opcode_offset + intersect_size <= bp_site->GetByteSize());
Jim Ingham20c77192011-06-29 19:42:28 +00002003 size_t buf_offset = intersect_addr - bp_addr;
Greg Claytond8cf1a12013-06-12 00:46:38 +00002004 ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size);
Jim Ingham20c77192011-06-29 19:42:28 +00002005 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006 }
Greg Claytond8cf1a12013-06-12 00:46:38 +00002007 });
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002008 }
2009 return bytes_removed;
2010}
2011
2012
Greg Claytonded470d2011-03-19 01:12:21 +00002013
2014size_t
2015Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site)
2016{
2017 PlatformSP platform_sp (m_target.GetPlatform());
2018 if (platform_sp)
2019 return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site);
2020 return 0;
2021}
2022
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002023Error
2024Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site)
2025{
2026 Error error;
2027 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002028 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002029 const addr_t bp_addr = bp_site->GetLoadAddress();
2030 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002031 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, bp_site->GetID(), (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002032 if (bp_site->IsEnabled())
2033 {
2034 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002035 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 +00002036 return error;
2037 }
2038
2039 if (bp_addr == LLDB_INVALID_ADDRESS)
2040 {
2041 error.SetErrorString("BreakpointSite contains an invalid load address.");
2042 return error;
2043 }
2044 // Ask the lldb::Process subclass to fill in the correct software breakpoint
2045 // trap for the breakpoint site
2046 const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site);
2047
2048 if (bp_opcode_size == 0)
2049 {
Daniel Malead01b2952012-11-29 21:49:15 +00002050 error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%" PRIx64, bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002051 }
2052 else
2053 {
2054 const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes();
2055
2056 if (bp_opcode_bytes == NULL)
2057 {
2058 error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode.");
2059 return error;
2060 }
2061
2062 // Save the original opcode by reading it
2063 if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size)
2064 {
2065 // Write a software breakpoint in place of the original opcode
2066 if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2067 {
2068 uint8_t verify_bp_opcode_bytes[64];
2069 if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size)
2070 {
2071 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0)
2072 {
2073 bp_site->SetEnabled(true);
2074 bp_site->SetType (BreakpointSite::eSoftware);
2075 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002076 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002077 bp_site->GetID(),
2078 (uint64_t)bp_addr);
2079 }
2080 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002081 error.SetErrorString("failed to verify the breakpoint trap in memory.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002082 }
2083 else
2084 error.SetErrorString("Unable to read memory to verify breakpoint trap.");
2085 }
2086 else
2087 error.SetErrorString("Unable to write breakpoint trap to memory.");
2088 }
2089 else
2090 error.SetErrorString("Unable to read memory at breakpoint address.");
2091 }
Stephen Wilson78a4feb2011-01-12 04:20:03 +00002092 if (log && error.Fail())
Daniel Malead01b2952012-11-29 21:49:15 +00002093 log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002094 bp_site->GetID(),
2095 (uint64_t)bp_addr,
2096 error.AsCString());
2097 return error;
2098}
2099
2100Error
2101Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site)
2102{
2103 Error error;
2104 assert (bp_site != NULL);
Greg Clayton5160ce52013-03-27 23:08:40 +00002105 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002106 addr_t bp_addr = bp_site->GetLoadAddress();
2107 lldb::user_id_t breakID = bp_site->GetID();
2108 if (log)
Jim Ingham299c0c12013-02-15 02:06:30 +00002109 log->Printf ("Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 ") addr = 0x%" PRIx64, breakID, (uint64_t)bp_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002110
2111 if (bp_site->IsHardware())
2112 {
2113 error.SetErrorString("Breakpoint site is a hardware breakpoint.");
2114 }
2115 else if (bp_site->IsEnabled())
2116 {
2117 const size_t break_op_size = bp_site->GetByteSize();
2118 const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes();
2119 if (break_op_size > 0)
2120 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002121 // Clear a software breakpoint instruction
Greg Claytonc982c762010-07-09 20:39:50 +00002122 uint8_t curr_break_op[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002123 assert (break_op_size <= sizeof(curr_break_op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124 bool break_op_found = false;
2125
2126 // Read the breakpoint opcode
2127 if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size)
2128 {
2129 bool verify = false;
2130 // Make sure we have the a breakpoint opcode exists at this address
2131 if (::memcmp (curr_break_op, break_op, break_op_size) == 0)
2132 {
2133 break_op_found = true;
2134 // We found a valid breakpoint opcode at this address, now restore
2135 // the saved opcode.
2136 if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size)
2137 {
2138 verify = true;
2139 }
2140 else
2141 error.SetErrorString("Memory write failed when restoring original opcode.");
2142 }
2143 else
2144 {
2145 error.SetErrorString("Original breakpoint trap is no longer in memory.");
2146 // Set verify to true and so we can check if the original opcode has already been restored
2147 verify = true;
2148 }
2149
2150 if (verify)
2151 {
Greg Claytonc982c762010-07-09 20:39:50 +00002152 uint8_t verify_opcode[8];
Stephen Wilson4ab47682010-07-20 18:41:11 +00002153 assert (break_op_size < sizeof(verify_opcode));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002154 // Verify that our original opcode made it back to the inferior
2155 if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size)
2156 {
2157 // compare the memory we just read with the original opcode
2158 if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
2159 {
2160 // SUCCESS
2161 bp_site->SetEnabled(false);
2162 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002163 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 +00002164 return error;
2165 }
2166 else
2167 {
2168 if (break_op_found)
2169 error.SetErrorString("Failed to restore original opcode.");
2170 }
2171 }
2172 else
2173 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
2174 }
2175 }
2176 else
2177 error.SetErrorString("Unable to read memory that should contain the breakpoint trap.");
2178 }
2179 }
2180 else
2181 {
2182 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002183 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 +00002184 return error;
2185 }
2186
2187 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002188 log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002189 bp_site->GetID(),
2190 (uint64_t)bp_addr,
2191 error.AsCString());
2192 return error;
2193
2194}
2195
Greg Clayton58be07b2011-01-07 06:08:19 +00002196// Uncomment to verify memory caching works after making changes to caching code
2197//#define VERIFY_MEMORY_READS
2198
Sean Callanan64c0cf22012-06-07 22:26:42 +00002199size_t
2200Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error)
2201{
Jason Molendaa7b5afa2013-11-15 00:17:32 +00002202 error.Clear();
Sean Callanan64c0cf22012-06-07 22:26:42 +00002203 if (!GetDisableMemoryCache())
2204 {
Greg Clayton58be07b2011-01-07 06:08:19 +00002205#if defined (VERIFY_MEMORY_READS)
Sean Callanan64c0cf22012-06-07 22:26:42 +00002206 // Memory caching is enabled, with debug verification
2207
2208 if (buf && size)
2209 {
2210 // Uncomment the line below to make sure memory caching is working.
2211 // I ran this through the test suite and got no assertions, so I am
2212 // pretty confident this is working well. If any changes are made to
2213 // memory caching, uncomment the line below and test your changes!
2214
2215 // Verify all memory reads by using the cache first, then redundantly
2216 // reading the same memory from the inferior and comparing to make sure
2217 // everything is exactly the same.
2218 std::string verify_buf (size, '\0');
2219 assert (verify_buf.size() == size);
2220 const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error);
2221 Error verify_error;
2222 const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error);
2223 assert (cache_bytes_read == verify_bytes_read);
2224 assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0);
2225 assert (verify_error.Success() == error.Success());
2226 return cache_bytes_read;
2227 }
2228 return 0;
2229#else // !defined(VERIFY_MEMORY_READS)
2230 // Memory caching is enabled, without debug verification
2231
2232 return m_memory_cache.Read (addr, buf, size, error);
2233#endif // defined (VERIFY_MEMORY_READS)
Greg Clayton58be07b2011-01-07 06:08:19 +00002234 }
Sean Callanan64c0cf22012-06-07 22:26:42 +00002235 else
2236 {
2237 // Memory caching is disabled
2238
2239 return ReadMemoryFromInferior (addr, buf, size, error);
2240 }
Greg Clayton58be07b2011-01-07 06:08:19 +00002241}
Greg Clayton58be07b2011-01-07 06:08:19 +00002242
Greg Clayton4c82d422012-05-18 23:20:01 +00002243size_t
2244Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error)
2245{
Greg Claytonde87c0f2012-05-19 00:18:00 +00002246 char buf[256];
Greg Clayton4c82d422012-05-18 23:20:01 +00002247 out_str.clear();
2248 addr_t curr_addr = addr;
2249 while (1)
2250 {
2251 size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error);
2252 if (length == 0)
2253 break;
2254 out_str.append(buf, length);
2255 // If we got "length - 1" bytes, we didn't get the whole C string, we
2256 // need to read some more characters
2257 if (length == sizeof(buf) - 1)
2258 curr_addr += length;
2259 else
2260 break;
2261 }
2262 return out_str.size();
2263}
2264
Greg Clayton58be07b2011-01-07 06:08:19 +00002265
2266size_t
Ashok Thirumurthi6ac9d132013-04-19 15:58:38 +00002267Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error,
2268 size_t type_width)
2269{
2270 size_t total_bytes_read = 0;
2271 if (dst && max_bytes && type_width && max_bytes >= type_width)
2272 {
2273 // Ensure a null terminator independent of the number of bytes that is read.
2274 memset (dst, 0, max_bytes);
2275 size_t bytes_left = max_bytes - type_width;
2276
2277 const char terminator[4] = {'\0', '\0', '\0', '\0'};
2278 assert(sizeof(terminator) >= type_width &&
2279 "Attempting to validate a string with more than 4 bytes per character!");
2280
2281 addr_t curr_addr = addr;
2282 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2283 char *curr_dst = dst;
2284
2285 error.Clear();
2286 while (bytes_left > 0 && error.Success())
2287 {
2288 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2289 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2290 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2291
2292 if (bytes_read == 0)
2293 break;
2294
2295 // Search for a null terminator of correct size and alignment in bytes_read
2296 size_t aligned_start = total_bytes_read - total_bytes_read % type_width;
2297 for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width)
2298 if (::strncmp(&dst[i], terminator, type_width) == 0)
2299 {
2300 error.Clear();
2301 return i;
2302 }
2303
2304 total_bytes_read += bytes_read;
2305 curr_dst += bytes_read;
2306 curr_addr += bytes_read;
2307 bytes_left -= bytes_read;
2308 }
2309 }
2310 else
2311 {
2312 if (max_bytes)
2313 error.SetErrorString("invalid arguments");
2314 }
2315 return total_bytes_read;
2316}
2317
2318// Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find
2319// null terminators.
2320size_t
Greg Claytone91b7952011-12-15 03:14:23 +00002321Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error)
Greg Clayton8b82f082011-04-12 05:54:46 +00002322{
2323 size_t total_cstr_len = 0;
2324 if (dst && dst_max_len)
2325 {
Greg Claytone91b7952011-12-15 03:14:23 +00002326 result_error.Clear();
Greg Clayton8b82f082011-04-12 05:54:46 +00002327 // NULL out everything just to be safe
2328 memset (dst, 0, dst_max_len);
2329 Error error;
2330 addr_t curr_addr = addr;
2331 const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize();
2332 size_t bytes_left = dst_max_len - 1;
2333 char *curr_dst = dst;
2334
2335 while (bytes_left > 0)
2336 {
2337 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
2338 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
2339 size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error);
2340
2341 if (bytes_read == 0)
2342 {
Greg Claytone91b7952011-12-15 03:14:23 +00002343 result_error = error;
Greg Clayton8b82f082011-04-12 05:54:46 +00002344 dst[total_cstr_len] = '\0';
2345 break;
2346 }
2347 const size_t len = strlen(curr_dst);
2348
2349 total_cstr_len += len;
2350
2351 if (len < bytes_to_read)
2352 break;
2353
2354 curr_dst += bytes_read;
2355 curr_addr += bytes_read;
2356 bytes_left -= bytes_read;
2357 }
2358 }
Greg Claytone91b7952011-12-15 03:14:23 +00002359 else
2360 {
2361 if (dst == NULL)
2362 result_error.SetErrorString("invalid arguments");
2363 else
2364 result_error.Clear();
2365 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002366 return total_cstr_len;
2367}
2368
2369size_t
Greg Clayton58be07b2011-01-07 06:08:19 +00002370Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error)
2371{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002372 if (buf == NULL || size == 0)
2373 return 0;
2374
2375 size_t bytes_read = 0;
2376 uint8_t *bytes = (uint8_t *)buf;
2377
2378 while (bytes_read < size)
2379 {
2380 const size_t curr_size = size - bytes_read;
2381 const size_t curr_bytes_read = DoReadMemory (addr + bytes_read,
2382 bytes + bytes_read,
2383 curr_size,
2384 error);
2385 bytes_read += curr_bytes_read;
2386 if (curr_bytes_read == curr_size || curr_bytes_read == 0)
2387 break;
2388 }
2389
2390 // Replace any software breakpoint opcodes that fall into this range back
2391 // into "buf" before we return
2392 if (bytes_read > 0)
2393 RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf);
2394 return bytes_read;
2395}
2396
Greg Clayton58a4c462010-12-16 20:01:20 +00002397uint64_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002398Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error)
Greg Clayton58a4c462010-12-16 20:01:20 +00002399{
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002400 Scalar scalar;
2401 if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error))
2402 return scalar.ULongLong(fail_value);
2403 return fail_value;
2404}
2405
2406addr_t
2407Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error)
2408{
2409 Scalar scalar;
2410 if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error))
2411 return scalar.ULongLong(LLDB_INVALID_ADDRESS);
2412 return LLDB_INVALID_ADDRESS;
2413}
2414
2415
2416bool
2417Process::WritePointerToMemory (lldb::addr_t vm_addr,
2418 lldb::addr_t ptr_value,
2419 Error &error)
2420{
2421 Scalar scalar;
2422 const uint32_t addr_byte_size = GetAddressByteSize();
2423 if (addr_byte_size <= 4)
2424 scalar = (uint32_t)ptr_value;
Greg Clayton58a4c462010-12-16 20:01:20 +00002425 else
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002426 scalar = ptr_value;
2427 return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size;
Greg Clayton58a4c462010-12-16 20:01:20 +00002428}
2429
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002430size_t
2431Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error)
2432{
2433 size_t bytes_written = 0;
2434 const uint8_t *bytes = (const uint8_t *)buf;
2435
2436 while (bytes_written < size)
2437 {
2438 const size_t curr_size = size - bytes_written;
2439 const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written,
2440 bytes + bytes_written,
2441 curr_size,
2442 error);
2443 bytes_written += curr_bytes_written;
2444 if (curr_bytes_written == curr_size || curr_bytes_written == 0)
2445 break;
2446 }
2447 return bytes_written;
2448}
2449
2450size_t
2451Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error)
2452{
Greg Clayton58be07b2011-01-07 06:08:19 +00002453#if defined (ENABLE_MEMORY_CACHING)
2454 m_memory_cache.Flush (addr, size);
2455#endif
2456
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002457 if (buf == NULL || size == 0)
2458 return 0;
Jim Ingham78a685a2011-04-16 00:01:13 +00002459
Jim Ingham4b536182011-08-09 02:12:22 +00002460 m_mod_id.BumpMemoryID();
Jim Ingham78a685a2011-04-16 00:01:13 +00002461
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002462 // We need to write any data that would go where any current software traps
2463 // (enabled software breakpoints) any software traps (breakpoints) that we
2464 // may have placed in our tasks memory.
2465
Greg Claytond8cf1a12013-06-12 00:46:38 +00002466 BreakpointSiteList bp_sites_in_range;
2467
2468 if (m_breakpoint_site_list.FindInRange (addr, addr + size, bp_sites_in_range))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002469 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002470 // No breakpoint sites overlap
2471 if (bp_sites_in_range.IsEmpty())
2472 return WriteMemoryPrivate (addr, buf, size, error);
2473 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002474 {
Greg Claytond8cf1a12013-06-12 00:46:38 +00002475 const uint8_t *ubuf = (const uint8_t *)buf;
2476 uint64_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002477
Greg Claytond8cf1a12013-06-12 00:46:38 +00002478 bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void {
2479
2480 if (error.Success())
2481 {
2482 addr_t intersect_addr;
2483 size_t intersect_size;
2484 size_t opcode_offset;
2485 const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset);
2486 assert(intersects);
2487 assert(addr <= intersect_addr && intersect_addr < addr + size);
2488 assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size);
2489 assert(opcode_offset + intersect_size <= bp->GetByteSize());
2490
2491 // Check for bytes before this breakpoint
2492 const addr_t curr_addr = addr + bytes_written;
2493 if (intersect_addr > curr_addr)
2494 {
2495 // There are some bytes before this breakpoint that we need to
2496 // just write to memory
2497 size_t curr_size = intersect_addr - curr_addr;
2498 size_t curr_bytes_written = WriteMemoryPrivate (curr_addr,
2499 ubuf + bytes_written,
2500 curr_size,
2501 error);
2502 bytes_written += curr_bytes_written;
2503 if (curr_bytes_written != curr_size)
2504 {
2505 // We weren't able to write all of the requested bytes, we
2506 // are done looping and will return the number of bytes that
2507 // we have written so far.
2508 if (error.Success())
2509 error.SetErrorToGenericError();
2510 }
2511 }
2512 // Now write any bytes that would cover up any software breakpoints
2513 // directly into the breakpoint opcode buffer
2514 ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size);
2515 bytes_written += intersect_size;
2516 }
2517 });
2518
2519 if (bytes_written < size)
2520 bytes_written += WriteMemoryPrivate (addr + bytes_written,
2521 ubuf + bytes_written,
2522 size - bytes_written,
2523 error);
2524 }
2525 }
2526 else
2527 {
2528 return WriteMemoryPrivate (addr, buf, size, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002529 }
2530
2531 // Write any remaining bytes after the last breakpoint if we have any left
Greg Claytond8cf1a12013-06-12 00:46:38 +00002532 return 0; //bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002533}
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002534
2535size_t
Greg Claytonc7bece562013-01-25 18:06:21 +00002536Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, size_t byte_size, Error &error)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002537{
2538 if (byte_size == UINT32_MAX)
2539 byte_size = scalar.GetByteSize();
2540 if (byte_size > 0)
2541 {
2542 uint8_t buf[32];
2543 const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error);
2544 if (mem_size > 0)
2545 return WriteMemory(addr, buf, mem_size, error);
2546 else
2547 error.SetErrorString ("failed to get scalar as memory data");
2548 }
2549 else
2550 {
2551 error.SetErrorString ("invalid scalar value");
2552 }
2553 return 0;
2554}
2555
2556size_t
2557Process::ReadScalarIntegerFromMemory (addr_t addr,
2558 uint32_t byte_size,
2559 bool is_signed,
2560 Scalar &scalar,
2561 Error &error)
2562{
Greg Clayton7060f892013-05-01 23:41:30 +00002563 uint64_t uval = 0;
2564 if (byte_size == 0)
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002565 {
Greg Clayton7060f892013-05-01 23:41:30 +00002566 error.SetErrorString ("byte size is zero");
2567 }
2568 else if (byte_size & (byte_size - 1))
2569 {
2570 error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size);
2571 }
2572 else if (byte_size <= sizeof(uval))
2573 {
2574 const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002575 if (bytes_read == byte_size)
2576 {
2577 DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00002578 lldb::offset_t offset = 0;
Greg Clayton7060f892013-05-01 23:41:30 +00002579 if (byte_size <= 4)
2580 scalar = data.GetMaxU32 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002581 else
Greg Clayton7060f892013-05-01 23:41:30 +00002582 scalar = data.GetMaxU64 (&offset, byte_size);
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002583 if (is_signed)
2584 scalar.SignExtend(byte_size * 8);
2585 return bytes_read;
2586 }
2587 }
2588 else
2589 {
2590 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
2591 }
2592 return 0;
2593}
2594
Greg Claytond495c532011-05-17 03:37:42 +00002595#define USE_ALLOCATE_MEMORY_CACHE 1
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002596addr_t
2597Process::AllocateMemory(size_t size, uint32_t permissions, Error &error)
2598{
Jim Inghamf72ce3a2011-06-20 17:32:44 +00002599 if (GetPrivateState() != eStateStopped)
2600 return LLDB_INVALID_ADDRESS;
2601
Greg Claytond495c532011-05-17 03:37:42 +00002602#if defined (USE_ALLOCATE_MEMORY_CACHE)
2603 return m_allocated_memory_cache.AllocateMemory(size, permissions, error);
2604#else
Greg Claytonb2daec92011-01-23 19:58:49 +00002605 addr_t allocated_addr = DoAllocateMemory (size, permissions, error);
Greg Clayton5160ce52013-03-27 23:08:40 +00002606 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002607 if (log)
Greg Clayton45989072013-10-23 18:24:30 +00002608 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 +00002609 (uint64_t)size,
Greg Claytond495c532011-05-17 03:37:42 +00002610 GetPermissionsAsCString (permissions),
Greg Claytonb2daec92011-01-23 19:58:49 +00002611 (uint64_t)allocated_addr,
Jim Ingham4b536182011-08-09 02:12:22 +00002612 m_mod_id.GetStopID(),
2613 m_mod_id.GetMemoryID());
Greg Claytonb2daec92011-01-23 19:58:49 +00002614 return allocated_addr;
Greg Claytond495c532011-05-17 03:37:42 +00002615#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002616}
2617
Sean Callanan90539452011-09-20 23:01:51 +00002618bool
2619Process::CanJIT ()
2620{
Sean Callanana7b443a2012-02-14 22:50:38 +00002621 if (m_can_jit == eCanJITDontKnow)
2622 {
Todd Fialaaf245d12014-06-30 21:05:18 +00002623 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Sean Callanana7b443a2012-02-14 22:50:38 +00002624 Error err;
2625
2626 uint64_t allocated_memory = AllocateMemory(8,
2627 ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable,
2628 err);
2629
2630 if (err.Success())
Todd Fialaaf245d12014-06-30 21:05:18 +00002631 {
Sean Callanana7b443a2012-02-14 22:50:38 +00002632 m_can_jit = eCanJITYes;
Todd Fialaaf245d12014-06-30 21:05:18 +00002633 if (log)
2634 log->Printf ("Process::%s pid %" PRIu64 " allocation test passed, CanJIT () is true", __FUNCTION__, GetID ());
2635 }
Sean Callanana7b443a2012-02-14 22:50:38 +00002636 else
Todd Fialaaf245d12014-06-30 21:05:18 +00002637 {
Sean Callanana7b443a2012-02-14 22:50:38 +00002638 m_can_jit = eCanJITNo;
Todd Fialaaf245d12014-06-30 21:05:18 +00002639 if (log)
2640 log->Printf ("Process::%s pid %" PRIu64 " allocation test failed, CanJIT () is false: %s", __FUNCTION__, GetID (), err.AsCString ());
2641 }
Sean Callanana7b443a2012-02-14 22:50:38 +00002642
2643 DeallocateMemory (allocated_memory);
2644 }
2645
Sean Callanan90539452011-09-20 23:01:51 +00002646 return m_can_jit == eCanJITYes;
2647}
2648
2649void
2650Process::SetCanJIT (bool can_jit)
2651{
2652 m_can_jit = (can_jit ? eCanJITYes : eCanJITNo);
2653}
2654
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002655Error
2656Process::DeallocateMemory (addr_t ptr)
2657{
Greg Claytond495c532011-05-17 03:37:42 +00002658 Error error;
2659#if defined (USE_ALLOCATE_MEMORY_CACHE)
2660 if (!m_allocated_memory_cache.DeallocateMemory(ptr))
2661 {
Daniel Malead01b2952012-11-29 21:49:15 +00002662 error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr);
Greg Claytond495c532011-05-17 03:37:42 +00002663 }
2664#else
2665 error = DoDeallocateMemory (ptr);
Greg Claytonb2daec92011-01-23 19:58:49 +00002666
Greg Clayton5160ce52013-03-27 23:08:40 +00002667 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonb2daec92011-01-23 19:58:49 +00002668 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002669 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 +00002670 ptr,
2671 error.AsCString("SUCCESS"),
Jim Ingham4b536182011-08-09 02:12:22 +00002672 m_mod_id.GetStopID(),
2673 m_mod_id.GetMemoryID());
Greg Claytond495c532011-05-17 03:37:42 +00002674#endif
Greg Claytonb2daec92011-01-23 19:58:49 +00002675 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002676}
2677
Han Ming Ongc811d382012-11-17 00:33:14 +00002678
Greg Claytonc9660542012-02-05 02:38:54 +00002679ModuleSP
Greg Claytonc859e2d2012-02-13 23:10:39 +00002680Process::ReadModuleFromMemory (const FileSpec& file_spec,
Andrew MacPherson17220c12014-03-05 10:12:43 +00002681 lldb::addr_t header_addr,
2682 size_t size_to_read)
Greg Claytonc9660542012-02-05 02:38:54 +00002683{
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002684 ModuleSP module_sp (new Module (file_spec, ArchSpec()));
Greg Claytonc9660542012-02-05 02:38:54 +00002685 if (module_sp)
2686 {
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002687 Error error;
Andrew MacPherson17220c12014-03-05 10:12:43 +00002688 ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error, size_to_read);
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002689 if (objfile)
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002690 return module_sp;
Greg Claytonc9660542012-02-05 02:38:54 +00002691 }
Greg Claytonc7f09cc2012-02-24 21:55:59 +00002692 return ModuleSP();
Greg Claytonc9660542012-02-05 02:38:54 +00002693}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002694
2695Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002696Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002697{
2698 Error error;
2699 error.SetErrorString("watchpoints are not supported");
2700 return error;
2701}
2702
2703Error
Jim Ingham1b5792e2012-12-18 02:03:49 +00002704Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002705{
2706 Error error;
2707 error.SetErrorString("watchpoints are not supported");
2708 return error;
2709}
2710
2711StateType
2712Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp)
2713{
2714 StateType state;
2715 // Now wait for the process to launch and return control to us, and then
2716 // call DidLaunch:
2717 while (1)
2718 {
Greg Clayton6779606a2011-01-22 23:43:18 +00002719 event_sp.reset();
2720 state = WaitForStateChangedEventsPrivate (timeout, event_sp);
2721
Greg Clayton2637f822011-11-17 01:23:07 +00002722 if (StateIsStoppedState(state, false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002723 break;
Greg Clayton6779606a2011-01-22 23:43:18 +00002724
2725 // If state is invalid, then we timed out
2726 if (state == eStateInvalid)
2727 break;
2728
2729 if (event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002730 HandlePrivateEvent (event_sp);
2731 }
2732 return state;
2733}
2734
2735Error
Greg Claytonfbb76342013-11-20 21:07:01 +00002736Process::Launch (ProcessLaunchInfo &launch_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002737{
2738 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002739 m_abi_sp.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002740 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002741 m_jit_loaders_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00002742 m_system_runtime_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002743 m_os_ap.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002744 m_process_input_reader.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002745
Greg Claytonaa149cb2011-08-11 02:48:45 +00002746 Module *exe_module = m_target.GetExecutableModulePointer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002747 if (exe_module)
2748 {
Greg Clayton2289fa42011-04-30 01:09:13 +00002749 char local_exec_file_path[PATH_MAX];
2750 char platform_exec_file_path[PATH_MAX];
2751 exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path));
2752 exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002753 if (exe_module->GetFileSpec().Exists())
2754 {
Greg Claytonfbb76342013-11-20 21:07:01 +00002755 // Install anything that might need to be installed prior to launching.
2756 // For host systems, this will do nothing, but if we are connected to a
2757 // remote platform it will install any needed binaries
2758 error = GetTarget().Install(&launch_info);
2759 if (error.Fail())
2760 return error;
2761
Greg Clayton71337622011-02-24 22:24:29 +00002762 if (PrivateStateThreadIsValid ())
2763 PausePrivateStateThread ();
2764
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002765 error = WillLaunch (exe_module);
2766 if (error.Success())
2767 {
Jim Ingham221d51c2013-05-08 00:35:16 +00002768 const bool restarted = false;
2769 SetPublicState (eStateLaunching, restarted);
Greg Claytone24c4ac2011-11-17 04:46:02 +00002770 m_should_detach = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002771
Ed Maste64fad602013-07-29 20:58:06 +00002772 if (m_public_run_lock.TrySetRunning())
Greg Clayton69fd4be2012-09-04 20:29:05 +00002773 {
2774 // Now launch using these arguments.
2775 error = DoLaunch (exe_module, launch_info);
2776 }
2777 else
2778 {
2779 // This shouldn't happen
2780 error.SetErrorString("failed to acquire process run lock");
2781 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002782
2783 if (error.Fail())
2784 {
2785 if (GetID() != LLDB_INVALID_PROCESS_ID)
2786 {
2787 SetID (LLDB_INVALID_PROCESS_ID);
2788 const char *error_string = error.AsCString();
2789 if (error_string == NULL)
2790 error_string = "launch failed";
2791 SetExitStatus (-1, error_string);
2792 }
2793 }
2794 else
2795 {
2796 EventSP event_sp;
Greg Clayton1a38ea72011-06-22 01:42:17 +00002797 TimeValue timeout_time;
2798 timeout_time = TimeValue::Now();
2799 timeout_time.OffsetWithSeconds(10);
2800 StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002801
Greg Clayton1a38ea72011-06-22 01:42:17 +00002802 if (state == eStateInvalid || event_sp.get() == NULL)
2803 {
2804 // We were able to launch the process, but we failed to
2805 // catch the initial stop.
2806 SetExitStatus (0, "failed to catch stop after launch");
2807 Destroy();
2808 }
2809 else if (state == eStateStopped || state == eStateCrashed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002810 {
Greg Clayton93d3c8332011-02-16 04:46:07 +00002811
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002812 DidLaunch ();
2813
Greg Claytonc859e2d2012-02-13 23:10:39 +00002814 DynamicLoader *dyld = GetDynamicLoader ();
2815 if (dyld)
2816 dyld->DidLaunch();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002817
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00002818 GetJITLoaders().DidLaunch();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002819
Jason Molendaeef51062013-11-05 03:57:19 +00002820 SystemRuntime *system_runtime = GetSystemRuntime ();
2821 if (system_runtime)
2822 system_runtime->DidLaunch();
2823
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002824 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002825 // This delays passing the stopped event to listeners till DidLaunch gets
2826 // a chance to complete...
2827 HandlePrivateEvent (event_sp);
Greg Clayton71337622011-02-24 22:24:29 +00002828
2829 if (PrivateStateThreadIsValid ())
2830 ResumePrivateStateThread ();
2831 else
2832 StartPrivateStateThread ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002833 }
2834 else if (state == eStateExited)
2835 {
2836 // We exited while trying to launch somehow. Don't call DidLaunch as that's
2837 // not likely to work, and return an invalid pid.
2838 HandlePrivateEvent (event_sp);
2839 }
2840 }
2841 }
2842 }
2843 else
2844 {
Greg Clayton86edbf42011-10-26 00:56:27 +00002845 error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002846 }
2847 }
2848 return error;
2849}
2850
Greg Claytonc3776bf2012-02-09 06:16:32 +00002851
2852Error
2853Process::LoadCore ()
2854{
2855 Error error = DoLoadCore();
2856 if (error.Success())
2857 {
2858 if (PrivateStateThreadIsValid ())
2859 ResumePrivateStateThread ();
2860 else
2861 StartPrivateStateThread ();
2862
Greg Claytonc859e2d2012-02-13 23:10:39 +00002863 DynamicLoader *dyld = GetDynamicLoader ();
2864 if (dyld)
2865 dyld->DidAttach();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002866
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00002867 GetJITLoaders().DidAttach();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002868
Jason Molendaeef51062013-11-05 03:57:19 +00002869 SystemRuntime *system_runtime = GetSystemRuntime ();
2870 if (system_runtime)
2871 system_runtime->DidAttach();
2872
Greg Claytonc859e2d2012-02-13 23:10:39 +00002873 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Claytonc3776bf2012-02-09 06:16:32 +00002874 // We successfully loaded a core file, now pretend we stopped so we can
2875 // show all of the threads in the core file and explore the crashed
2876 // state.
2877 SetPrivateState (eStateStopped);
2878
2879 }
2880 return error;
2881}
2882
Greg Claytonc859e2d2012-02-13 23:10:39 +00002883DynamicLoader *
2884Process::GetDynamicLoader ()
2885{
2886 if (m_dyld_ap.get() == NULL)
2887 m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL));
2888 return m_dyld_ap.get();
2889}
Greg Claytonc3776bf2012-02-09 06:16:32 +00002890
Todd Fialaaf245d12014-06-30 21:05:18 +00002891const lldb::DataBufferSP
2892Process::GetAuxvData()
2893{
2894 return DataBufferSP ();
2895}
2896
Andrew MacPherson17220c12014-03-05 10:12:43 +00002897JITLoaderList &
2898Process::GetJITLoaders ()
2899{
2900 if (!m_jit_loaders_ap)
2901 {
2902 m_jit_loaders_ap.reset(new JITLoaderList());
2903 JITLoader::LoadPlugins(this, *m_jit_loaders_ap);
2904 }
2905 return *m_jit_loaders_ap;
2906}
2907
Jason Molendaeef51062013-11-05 03:57:19 +00002908SystemRuntime *
2909Process::GetSystemRuntime ()
2910{
2911 if (m_system_runtime_ap.get() == NULL)
2912 m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this));
2913 return m_system_runtime_ap.get();
2914}
2915
Todd Fiala76e0fc92014-08-27 22:58:26 +00002916Process::AttachCompletionHandler::AttachCompletionHandler (Process *process, uint32_t exec_count) :
2917 NextEventAction (process),
2918 m_exec_count (exec_count)
2919{
2920 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2921 if (log)
2922 log->Printf ("Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32, __FUNCTION__, static_cast<void*>(process), exec_count);
2923}
Greg Claytonc3776bf2012-02-09 06:16:32 +00002924
Jim Inghambb3a2832011-01-29 01:49:25 +00002925Process::NextEventAction::EventActionResult
2926Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002927{
Todd Fiala76e0fc92014-08-27 22:58:26 +00002928 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2929
Jim Inghambb3a2832011-01-29 01:49:25 +00002930 StateType state = ProcessEventData::GetStateFromEvent (event_sp.get());
Todd Fiala76e0fc92014-08-27 22:58:26 +00002931 if (log)
2932 log->Printf ("Process::AttachCompletionHandler::%s called with state %s (%d)", __FUNCTION__, StateAsCString(state), static_cast<int> (state));
2933
2934 switch (state)
Greg Clayton19388cf2010-10-18 01:45:30 +00002935 {
Greg Clayton513c26c2011-01-29 07:10:55 +00002936 case eStateRunning:
Greg Clayton71337622011-02-24 22:24:29 +00002937 case eStateConnected:
Greg Clayton513c26c2011-01-29 07:10:55 +00002938 return eEventActionRetry;
2939
2940 case eStateStopped:
2941 case eStateCrashed:
Greg Claytonc9ed4782011-11-12 02:10:56 +00002942 {
2943 // During attach, prior to sending the eStateStopped event,
Jim Inghamb1e2e842012-04-12 18:49:31 +00002944 // lldb_private::Process subclasses must set the new process ID.
Greg Claytonc9ed4782011-11-12 02:10:56 +00002945 assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
Jim Ingham221d51c2013-05-08 00:35:16 +00002946 // We don't want these events to be reported, so go set the ShouldReportStop here:
2947 m_process->GetThreadList().SetShouldReportStop (eVoteNo);
2948
Greg Claytonc9ed4782011-11-12 02:10:56 +00002949 if (m_exec_count > 0)
2950 {
2951 --m_exec_count;
Todd Fiala76e0fc92014-08-27 22:58:26 +00002952
2953 if (log)
2954 log->Printf ("Process::AttachCompletionHandler::%s state %s: reduced remaining exec count to %" PRIu32 ", requesting resume", __FUNCTION__, StateAsCString(state), m_exec_count);
2955
Jim Ingham221d51c2013-05-08 00:35:16 +00002956 RequestResume();
Greg Claytonc9ed4782011-11-12 02:10:56 +00002957 return eEventActionRetry;
2958 }
2959 else
2960 {
Todd Fiala76e0fc92014-08-27 22:58:26 +00002961 if (log)
2962 log->Printf ("Process::AttachCompletionHandler::%s state %s: no more execs expected to start, continuing with attach", __FUNCTION__, StateAsCString(state));
2963
Greg Claytonc9ed4782011-11-12 02:10:56 +00002964 m_process->CompleteAttach ();
2965 return eEventActionSuccess;
2966 }
2967 }
Greg Clayton513c26c2011-01-29 07:10:55 +00002968 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00002969
Greg Clayton513c26c2011-01-29 07:10:55 +00002970 default:
2971 case eStateExited:
2972 case eStateInvalid:
Greg Clayton513c26c2011-01-29 07:10:55 +00002973 break;
Jim Inghambb3a2832011-01-29 01:49:25 +00002974 }
Greg Claytonc9ed4782011-11-12 02:10:56 +00002975
2976 m_exit_string.assign ("No valid Process");
2977 return eEventActionExit;
Jim Inghambb3a2832011-01-29 01:49:25 +00002978}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002979
Jim Inghambb3a2832011-01-29 01:49:25 +00002980Process::NextEventAction::EventActionResult
2981Process::AttachCompletionHandler::HandleBeingInterrupted()
2982{
2983 return eEventActionSuccess;
2984}
2985
2986const char *
2987Process::AttachCompletionHandler::GetExitString ()
2988{
2989 return m_exit_string.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002990}
2991
2992Error
Greg Clayton144f3a92011-11-15 03:53:30 +00002993Process::Attach (ProcessAttachInfo &attach_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002994{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002995 m_abi_sp.reset();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00002996 m_process_input_reader.reset();
Greg Clayton93d3c8332011-02-16 04:46:07 +00002997 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00002998 m_jit_loaders_ap.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00002999 m_system_runtime_ap.reset();
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003000 m_os_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02 +00003001
Greg Clayton144f3a92011-11-15 03:53:30 +00003002 lldb::pid_t attach_pid = attach_info.GetProcessID();
Greg Claytone996fd32011-03-08 22:40:15 +00003003 Error error;
Greg Clayton144f3a92011-11-15 03:53:30 +00003004 if (attach_pid == LLDB_INVALID_PROCESS_ID)
Jim Ingham5aee1622010-08-09 23:31:02 +00003005 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003006 char process_name[PATH_MAX];
Jim Ingham4299fdb2011-09-15 01:10:17 +00003007
Greg Clayton144f3a92011-11-15 03:53:30 +00003008 if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name)))
Jim Ingham2ecb7422010-08-17 21:54:19 +00003009 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003010 const bool wait_for_launch = attach_info.GetWaitForLaunch();
3011
3012 if (wait_for_launch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003013 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003014 error = WillAttachToProcessWithName(process_name, wait_for_launch);
3015 if (error.Success())
3016 {
Ed Maste64fad602013-07-29 20:58:06 +00003017 if (m_public_run_lock.TrySetRunning())
Greg Clayton926cce72012-10-12 16:10:12 +00003018 {
3019 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003020 const bool restarted = false;
3021 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003022 // Now attach using these arguments.
Jean-Daniel Dupas9c517c02013-12-23 22:32:54 +00003023 error = DoAttachToProcessWithName (process_name, attach_info);
Greg Clayton926cce72012-10-12 16:10:12 +00003024 }
3025 else
3026 {
3027 // This shouldn't happen
3028 error.SetErrorString("failed to acquire process run lock");
3029 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00003030
Greg Clayton144f3a92011-11-15 03:53:30 +00003031 if (error.Fail())
3032 {
3033 if (GetID() != LLDB_INVALID_PROCESS_ID)
3034 {
3035 SetID (LLDB_INVALID_PROCESS_ID);
3036 if (error.AsCString() == NULL)
3037 error.SetErrorString("attach failed");
3038
3039 SetExitStatus(-1, error.AsCString());
3040 }
3041 }
3042 else
3043 {
3044 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3045 StartPrivateStateThread();
3046 }
3047 return error;
3048 }
Greg Claytone996fd32011-03-08 22:40:15 +00003049 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003050 else
Greg Claytone996fd32011-03-08 22:40:15 +00003051 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003052 ProcessInstanceInfoList process_infos;
3053 PlatformSP platform_sp (m_target.GetPlatform ());
3054
3055 if (platform_sp)
3056 {
3057 ProcessInstanceInfoMatch match_info;
3058 match_info.GetProcessInfo() = attach_info;
3059 match_info.SetNameMatchType (eNameMatchEquals);
3060 platform_sp->FindProcesses (match_info, process_infos);
3061 const uint32_t num_matches = process_infos.GetSize();
3062 if (num_matches == 1)
3063 {
3064 attach_pid = process_infos.GetProcessIDAtIndex(0);
3065 // Fall through and attach using the above process ID
3066 }
3067 else
3068 {
3069 match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name));
3070 if (num_matches > 1)
Jim Ingham368ac222014-08-15 17:05:27 +00003071 {
3072 StreamString s;
3073 ProcessInstanceInfo::DumpTableHeader (s, platform_sp.get(), true, false);
3074 for (size_t i = 0; i < num_matches; i++)
3075 {
3076 process_infos.GetProcessInfoAtIndex(i).DumpAsTableRow(s, platform_sp.get(), true, false);
3077 }
3078 error.SetErrorStringWithFormat ("more than one process named %s:\n%s",
3079 process_name,
3080 s.GetData());
3081 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003082 else
3083 error.SetErrorStringWithFormat ("could not find a process named %s", process_name);
3084 }
3085 }
3086 else
3087 {
3088 error.SetErrorString ("invalid platform, can't find processes by name");
3089 return error;
3090 }
Greg Claytone996fd32011-03-08 22:40:15 +00003091 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003092 }
3093 else
Greg Clayton144f3a92011-11-15 03:53:30 +00003094 {
3095 error.SetErrorString ("invalid process name");
Greg Claytone996fd32011-03-08 22:40:15 +00003096 }
3097 }
Greg Clayton144f3a92011-11-15 03:53:30 +00003098
3099 if (attach_pid != LLDB_INVALID_PROCESS_ID)
Greg Claytone996fd32011-03-08 22:40:15 +00003100 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003101 error = WillAttachToProcessWithID(attach_pid);
Greg Claytone996fd32011-03-08 22:40:15 +00003102 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003103 {
Greg Clayton144f3a92011-11-15 03:53:30 +00003104
Ed Maste64fad602013-07-29 20:58:06 +00003105 if (m_public_run_lock.TrySetRunning())
Greg Clayton926cce72012-10-12 16:10:12 +00003106 {
3107 // Now attach using these arguments.
3108 m_should_detach = true;
Jim Ingham221d51c2013-05-08 00:35:16 +00003109 const bool restarted = false;
3110 SetPublicState (eStateAttaching, restarted);
Greg Clayton926cce72012-10-12 16:10:12 +00003111 error = DoAttachToProcessWithID (attach_pid, attach_info);
3112 }
3113 else
3114 {
3115 // This shouldn't happen
3116 error.SetErrorString("failed to acquire process run lock");
3117 }
3118
Greg Clayton144f3a92011-11-15 03:53:30 +00003119 if (error.Success())
3120 {
3121
3122 SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount()));
3123 StartPrivateStateThread();
3124 }
3125 else
Greg Claytone996fd32011-03-08 22:40:15 +00003126 {
3127 if (GetID() != LLDB_INVALID_PROCESS_ID)
3128 {
3129 SetID (LLDB_INVALID_PROCESS_ID);
3130 const char *error_string = error.AsCString();
3131 if (error_string == NULL)
3132 error_string = "attach failed";
3133
3134 SetExitStatus(-1, error_string);
3135 }
3136 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003137 }
3138 }
3139 return error;
3140}
3141
Greg Clayton93d3c8332011-02-16 04:46:07 +00003142void
3143Process::CompleteAttach ()
3144{
Todd Fiala76e0fc92014-08-27 22:58:26 +00003145 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3146 if (log)
3147 log->Printf ("Process::%s()", __FUNCTION__);
3148
Greg Clayton93d3c8332011-02-16 04:46:07 +00003149 // Let the process subclass figure out at much as it can about the process
3150 // before we go looking for a dynamic loader plug-in.
Jim Inghambb006ce2014-08-02 00:33:35 +00003151 ArchSpec process_arch;
3152 DidAttach(process_arch);
3153
3154 if (process_arch.IsValid())
Todd Fiala76e0fc92014-08-27 22:58:26 +00003155 {
Jim Inghambb006ce2014-08-02 00:33:35 +00003156 m_target.SetArchitecture(process_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003157 if (log)
3158 {
3159 const char *triple_str = process_arch.GetTriple().getTriple().c_str ();
3160 log->Printf ("Process::%s replacing process architecture with DidAttach() architecture: %s",
3161 __FUNCTION__,
3162 triple_str ? triple_str : "<null>");
3163 }
3164 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003165
Jim Ingham4299fdb2011-09-15 01:10:17 +00003166 // We just attached. If we have a platform, ask it for the process architecture, and if it isn't
3167 // the same as the one we've already set, switch architectures.
3168 PlatformSP platform_sp (m_target.GetPlatform ());
3169 assert (platform_sp.get());
3170 if (platform_sp)
3171 {
Greg Clayton70512312012-05-08 01:45:38 +00003172 const ArchSpec &target_arch = m_target.GetArchitecture();
Greg Clayton1e0c8842013-01-11 20:49:54 +00003173 if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL))
Greg Clayton70512312012-05-08 01:45:38 +00003174 {
3175 ArchSpec platform_arch;
3176 platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch);
3177 if (platform_sp)
3178 {
3179 m_target.SetPlatform (platform_sp);
3180 m_target.SetArchitecture(platform_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003181 if (log)
3182 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 +00003183 }
3184 }
Jim Inghambb006ce2014-08-02 00:33:35 +00003185 else if (!process_arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00003186 {
3187 ProcessInstanceInfo process_info;
3188 platform_sp->GetProcessInfo (GetID(), process_info);
3189 const ArchSpec &process_arch = process_info.GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +00003190 if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch))
Todd Fiala76e0fc92014-08-27 22:58:26 +00003191 {
Greg Clayton70512312012-05-08 01:45:38 +00003192 m_target.SetArchitecture (process_arch);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003193 if (log)
3194 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 ());
3195 }
Greg Clayton70512312012-05-08 01:45:38 +00003196 }
Jim Ingham4299fdb2011-09-15 01:10:17 +00003197 }
3198
3199 // We have completed the attach, now it is time to find the dynamic loader
Greg Clayton93d3c8332011-02-16 04:46:07 +00003200 // plug-in
Greg Claytonc859e2d2012-02-13 23:10:39 +00003201 DynamicLoader *dyld = GetDynamicLoader ();
3202 if (dyld)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003203 {
Greg Claytonc859e2d2012-02-13 23:10:39 +00003204 dyld->DidAttach();
Todd Fiala76e0fc92014-08-27 22:58:26 +00003205 if (log)
3206 {
3207 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3208 log->Printf ("Process::%s after DynamicLoader::DidAttach(), target executable is %s (using %s plugin)",
3209 __FUNCTION__,
3210 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>",
3211 dyld->GetPluginName().AsCString ("<unnamed>"));
3212 }
3213 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003214
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00003215 GetJITLoaders().DidAttach();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003216
Jason Molendaeef51062013-11-05 03:57:19 +00003217 SystemRuntime *system_runtime = GetSystemRuntime ();
3218 if (system_runtime)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003219 {
Jason Molendaeef51062013-11-05 03:57:19 +00003220 system_runtime->DidAttach();
Todd Fiala76e0fc92014-08-27 22:58:26 +00003221 if (log)
3222 {
3223 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3224 log->Printf ("Process::%s after SystemRuntime::DidAttach(), target executable is %s (using %s plugin)",
3225 __FUNCTION__,
3226 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>",
3227 system_runtime->GetPluginName().AsCString("<unnamed>"));
3228 }
3229 }
Jason Molendaeef51062013-11-05 03:57:19 +00003230
Greg Clayton56d9a1b2011-08-22 02:49:39 +00003231 m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL));
Greg Clayton93d3c8332011-02-16 04:46:07 +00003232 // Figure out which one is the executable, and set that in our target:
Enrico Granata17598482012-11-08 02:22:02 +00003233 const ModuleList &target_modules = m_target.GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003234 Mutex::Locker modules_locker(target_modules.GetMutex());
3235 size_t num_modules = target_modules.GetSize();
3236 ModuleSP new_executable_module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003237
Andy Gibbsa297a972013-06-19 19:04:53 +00003238 for (size_t i = 0; i < num_modules; i++)
Greg Clayton93d3c8332011-02-16 04:46:07 +00003239 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003240 ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i));
Greg Clayton8b82f082011-04-12 05:54:46 +00003241 if (module_sp && module_sp->IsExecutable())
Greg Clayton93d3c8332011-02-16 04:46:07 +00003242 {
Greg Claytonaa149cb2011-08-11 02:48:45 +00003243 if (m_target.GetExecutableModulePointer() != module_sp.get())
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003244 new_executable_module_sp = module_sp;
Greg Clayton93d3c8332011-02-16 04:46:07 +00003245 break;
3246 }
3247 }
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003248 if (new_executable_module_sp)
Todd Fiala76e0fc92014-08-27 22:58:26 +00003249 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003250 m_target.SetExecutableModule (new_executable_module_sp, false);
Todd Fiala76e0fc92014-08-27 22:58:26 +00003251 if (log)
3252 {
3253 ModuleSP exe_module_sp = m_target.GetExecutableModule ();
3254 log->Printf ("Process::%s after looping through modules, target executable is %s",
3255 __FUNCTION__,
3256 exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>");
3257 }
3258 }
Greg Clayton93d3c8332011-02-16 04:46:07 +00003259}
3260
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003261Error
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003262Process::ConnectRemote (Stream *strm, const char *remote_url)
Greg Claytonb766a732011-02-04 01:58:07 +00003263{
Greg Claytonb766a732011-02-04 01:58:07 +00003264 m_abi_sp.reset();
3265 m_process_input_reader.reset();
3266
3267 // Find the process and its architecture. Make sure it matches the architecture
3268 // of the current Target, and if not adjust it.
3269
Jason Molenda4bd4e7e2012-09-29 04:02:01 +00003270 Error error (DoConnectRemote (strm, remote_url));
Greg Claytonb766a732011-02-04 01:58:07 +00003271 if (error.Success())
3272 {
Greg Clayton71337622011-02-24 22:24:29 +00003273 if (GetID() != LLDB_INVALID_PROCESS_ID)
3274 {
Greg Clayton32e0a752011-03-30 18:16:51 +00003275 EventSP event_sp;
3276 StateType state = WaitForProcessStopPrivate(NULL, event_sp);
3277
3278 if (state == eStateStopped || state == eStateCrashed)
3279 {
3280 // If we attached and actually have a process on the other end, then
3281 // this ended up being the equivalent of an attach.
3282 CompleteAttach ();
3283
3284 // This delays passing the stopped event to listeners till
3285 // CompleteAttach gets a chance to complete...
3286 HandlePrivateEvent (event_sp);
3287
3288 }
Greg Clayton71337622011-02-24 22:24:29 +00003289 }
Greg Clayton32e0a752011-03-30 18:16:51 +00003290
3291 if (PrivateStateThreadIsValid ())
3292 ResumePrivateStateThread ();
3293 else
3294 StartPrivateStateThread ();
Greg Claytonb766a732011-02-04 01:58:07 +00003295 }
3296 return error;
3297}
3298
3299
3300Error
Jim Ingham3b8285d2012-04-19 01:40:33 +00003301Process::PrivateResume ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003302{
Greg Clayton5160ce52013-03-27 23:08:40 +00003303 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003304 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003305 log->Printf("Process::PrivateResume() m_stop_id = %u, public state: %s private state: %s",
Jim Ingham4b536182011-08-09 02:12:22 +00003306 m_mod_id.GetStopID(),
Jim Ingham444586b2011-01-24 06:34:17 +00003307 StateAsCString(m_public_state.GetValue()),
3308 StateAsCString(m_private_state.GetValue()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003309
3310 Error error (WillResume());
3311 // Tell the process it is about to resume before the thread list
3312 if (error.Success())
3313 {
Johnny Chenc4221e42010-12-02 20:53:05 +00003314 // Now let the thread list know we are about to resume so it
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003315 // can let all of our threads know that they are about to be
3316 // resumed. Threads will each be called with
3317 // Thread::WillResume(StateType) where StateType contains the state
3318 // that they are supposed to have when the process is resumed
3319 // (suspended/running/stepping). Threads should also check
3320 // their resume signal in lldb::Thread::GetResumeSignal()
Jim Ingham221d51c2013-05-08 00:35:16 +00003321 // to see if they are supposed to start back up with a signal.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003322 if (m_thread_list.WillResume())
3323 {
Jim Ingham372787f2012-04-07 00:00:41 +00003324 // Last thing, do the PreResumeActions.
3325 if (!RunPreResumeActions())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003326 {
Jim Ingham0161b492013-02-09 01:29:05 +00003327 error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming.");
Jim Ingham372787f2012-04-07 00:00:41 +00003328 }
3329 else
3330 {
3331 m_mod_id.BumpResumeID();
3332 error = DoResume();
3333 if (error.Success())
3334 {
3335 DidResume();
3336 m_thread_list.DidResume();
3337 if (log)
3338 log->Printf ("Process thinks the process has resumed.");
3339 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003340 }
3341 }
3342 else
3343 {
Jim Ingham513c6bb2012-09-01 01:02:41 +00003344 // Somebody wanted to run without running. So generate a continue & a stopped event,
3345 // and let the world handle them.
3346 if (log)
3347 log->Printf ("Process::PrivateResume() asked to simulate a start & stop.");
3348
3349 SetPrivateState(eStateRunning);
3350 SetPrivateState(eStateStopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003351 }
3352 }
Jim Ingham444586b2011-01-24 06:34:17 +00003353 else if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003354 log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003355 return error;
3356}
3357
3358Error
Greg Claytonf9b57b92013-05-10 23:48:10 +00003359Process::Halt (bool clear_thread_plans)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003360{
Greg Claytonf9b57b92013-05-10 23:48:10 +00003361 // Don't clear the m_clear_thread_plans_on_stop, only set it to true if
3362 // in case it was already set and some thread plan logic calls halt on its
3363 // own.
3364 m_clear_thread_plans_on_stop |= clear_thread_plans;
3365
Jim Inghamaacc3182012-06-06 00:29:30 +00003366 // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since
3367 // we could just straightaway get another event. It just narrows the window...
3368 m_currently_handling_event.WaitForValueEqualTo(false);
3369
3370
Jim Inghambb3a2832011-01-29 01:49:25 +00003371 // Pause our private state thread so we can ensure no one else eats
3372 // the stop event out from under us.
Jim Ingham0f16e732011-02-08 05:20:59 +00003373 Listener halt_listener ("lldb.process.halt_listener");
3374 HijackPrivateProcessEvents(&halt_listener);
Greg Clayton3af9ea52010-11-18 05:57:03 +00003375
Jim Inghambb3a2832011-01-29 01:49:25 +00003376 EventSP event_sp;
Greg Clayton513c26c2011-01-29 07:10:55 +00003377 Error error (WillHalt());
Jim Inghambb3a2832011-01-29 01:49:25 +00003378
Greg Clayton06357c92014-07-30 17:38:47 +00003379 bool restored_process_events = false;
Greg Clayton513c26c2011-01-29 07:10:55 +00003380 if (error.Success())
Jim Inghambb3a2832011-01-29 01:49:25 +00003381 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003382
Greg Clayton513c26c2011-01-29 07:10:55 +00003383 bool caused_stop = false;
3384
3385 // Ask the process subclass to actually halt our process
3386 error = DoHalt(caused_stop);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003387 if (error.Success())
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003388 {
Greg Clayton513c26c2011-01-29 07:10:55 +00003389 if (m_public_state.GetValue() == eStateAttaching)
3390 {
Greg Clayton06357c92014-07-30 17:38:47 +00003391 // Don't hijack and eat the eStateExited as the code that was doing
3392 // the attach will be waiting for this event...
3393 RestorePrivateProcessEvents();
3394 restored_process_events = true;
Greg Clayton513c26c2011-01-29 07:10:55 +00003395 SetExitStatus(SIGKILL, "Cancelled async attach.");
3396 Destroy ();
3397 }
3398 else
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003399 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003400 // If "caused_stop" is true, then DoHalt stopped the process. If
3401 // "caused_stop" is false, the process was already stopped.
3402 // If the DoHalt caused the process to stop, then we want to catch
3403 // this event and set the interrupted bool to true before we pass
3404 // this along so clients know that the process was interrupted by
3405 // a halt command.
3406 if (caused_stop)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003407 {
Jim Ingham0f16e732011-02-08 05:20:59 +00003408 // Wait for 1 second for the process to stop.
Jim Inghambb3a2832011-01-29 01:49:25 +00003409 TimeValue timeout_time;
3410 timeout_time = TimeValue::Now();
Andrew MacPherson17220c12014-03-05 10:12:43 +00003411 timeout_time.OffsetWithSeconds(10);
Jim Ingham0f16e732011-02-08 05:20:59 +00003412 bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp);
3413 StateType state = ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003414
Jim Ingham0f16e732011-02-08 05:20:59 +00003415 if (!got_event || state == eStateInvalid)
Greg Clayton3af9ea52010-11-18 05:57:03 +00003416 {
Jim Inghambb3a2832011-01-29 01:49:25 +00003417 // We timeout out and didn't get a stop event...
Jim Ingham0f16e732011-02-08 05:20:59 +00003418 error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState()));
Greg Clayton3af9ea52010-11-18 05:57:03 +00003419 }
3420 else
3421 {
Greg Clayton2637f822011-11-17 01:23:07 +00003422 if (StateIsStoppedState (state, false))
Jim Inghambb3a2832011-01-29 01:49:25 +00003423 {
3424 // We caused the process to interrupt itself, so mark this
3425 // as such in the stop event so clients can tell an interrupted
3426 // process from a natural stop
3427 ProcessEventData::SetInterruptedInEvent (event_sp.get(), true);
3428 }
3429 else
3430 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003431 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Inghambb3a2832011-01-29 01:49:25 +00003432 if (log)
3433 log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state));
3434 error.SetErrorString ("Did not get stopped event after halt.");
3435 }
Greg Clayton3af9ea52010-11-18 05:57:03 +00003436 }
3437 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003438 DidHalt();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003439 }
3440 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003441 }
Jim Inghambb3a2832011-01-29 01:49:25 +00003442 // Resume our private state thread before we post the event (if any)
Greg Clayton06357c92014-07-30 17:38:47 +00003443 if (!restored_process_events)
3444 RestorePrivateProcessEvents();
Jim Inghambb3a2832011-01-29 01:49:25 +00003445
3446 // Post any event we might have consumed. If all goes well, we will have
3447 // stopped the process, intercepted the event and set the interrupted
3448 // bool in the event. Post it to the private event queue and that will end up
3449 // correctly setting the state.
3450 if (event_sp)
3451 m_private_state_broadcaster.BroadcastEvent(event_sp);
3452
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003453 return error;
3454}
3455
3456Error
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003457Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp)
3458{
3459 Error error;
3460 if (m_public_state.GetValue() == eStateRunning)
3461 {
3462 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
3463 if (log)
3464 log->Printf("Process::Destroy() About to halt.");
3465 error = Halt();
3466 if (error.Success())
3467 {
3468 // Consume the halt event.
3469 TimeValue timeout (TimeValue::Now());
3470 timeout.OffsetWithSeconds(1);
3471 StateType state = WaitForProcessToStop (&timeout, &exit_event_sp);
3472
3473 // If the process exited while we were waiting for it to stop, put the exited event into
3474 // the shared pointer passed in and return. Our caller doesn't need to do anything else, since
3475 // they don't have a process anymore...
3476
3477 if (state == eStateExited || m_private_state.GetValue() == eStateExited)
3478 {
3479 if (log)
3480 log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt.");
3481 return error;
3482 }
3483 else
3484 exit_event_sp.reset(); // It is ok to consume any non-exit stop events
3485
3486 if (state != eStateStopped)
3487 {
3488 if (log)
3489 log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state));
3490 // If we really couldn't stop the process then we should just error out here, but if the
3491 // lower levels just bobbled sending the event and we really are stopped, then continue on.
3492 StateType private_state = m_private_state.GetValue();
3493 if (private_state != eStateStopped)
3494 {
3495 return error;
3496 }
3497 }
3498 }
3499 else
3500 {
3501 if (log)
3502 log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString());
3503 }
3504 }
3505 return error;
3506}
3507
3508Error
Jim Inghamacff8952013-05-02 00:27:30 +00003509Process::Detach (bool keep_stopped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003510{
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003511 EventSP exit_event_sp;
3512 Error error;
3513 m_destroy_in_process = true;
3514
3515 error = WillDetach();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003516
3517 if (error.Success())
3518 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003519 if (DetachRequiresHalt())
3520 {
3521 error = HaltForDestroyOrDetach (exit_event_sp);
3522 if (!error.Success())
3523 {
3524 m_destroy_in_process = false;
3525 return error;
3526 }
3527 else if (exit_event_sp)
3528 {
3529 // We shouldn't need to do anything else here. There's no process left to detach from...
3530 StopPrivateStateThread();
3531 m_destroy_in_process = false;
3532 return error;
3533 }
3534 }
3535
Andrew MacPhersonc3826b52014-03-25 19:59:36 +00003536 m_thread_list.DiscardThreadPlans();
3537 DisableAllBreakpointSites();
3538
Jim Inghamacff8952013-05-02 00:27:30 +00003539 error = DoDetach(keep_stopped);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003540 if (error.Success())
3541 {
3542 DidDetach();
3543 StopPrivateStateThread();
3544 }
Jim Inghamacff8952013-05-02 00:27:30 +00003545 else
3546 {
3547 return error;
3548 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003549 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003550 m_destroy_in_process = false;
3551
3552 // If we exited when we were waiting for a process to stop, then
3553 // forward the event here so we don't lose the event
3554 if (exit_event_sp)
3555 {
3556 // Directly broadcast our exited event because we shut down our
3557 // private state thread above
3558 BroadcastEvent(exit_event_sp);
3559 }
3560
3561 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3562 // the last events through the event system, in which case we might strand the write lock. Unlock
3563 // it here so when we do to tear down the process we don't get an error destroying the lock.
3564
Ed Maste64fad602013-07-29 20:58:06 +00003565 m_public_run_lock.SetStopped();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003566 return error;
3567}
3568
3569Error
3570Process::Destroy ()
3571{
Jim Ingham09437922013-03-01 20:04:25 +00003572
3573 // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work
3574 // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt
3575 // failed and the process stays around for some reason it won't be in a confused state.
3576
3577 m_destroy_in_process = true;
3578
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003579 Error error (WillDestroy());
3580 if (error.Success())
3581 {
Greg Clayton85fb1b92012-09-11 02:33:37 +00003582 EventSP exit_event_sp;
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003583 if (DestroyRequiresHalt())
Jim Ingham04e0a222012-05-23 15:46:31 +00003584 {
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003585 error = HaltForDestroyOrDetach(exit_event_sp);
Jim Ingham04e0a222012-05-23 15:46:31 +00003586 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003587
Jim Inghamaacc3182012-06-06 00:29:30 +00003588 if (m_public_state.GetValue() != eStateRunning)
3589 {
3590 // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to
3591 // kill it, we don't want it hitting a breakpoint...
3592 // Only do this if we've stopped, however, since if we didn't manage to halt it above, then
3593 // we're not going to have much luck doing this now.
3594 m_thread_list.DiscardThreadPlans();
3595 DisableAllBreakpointSites();
3596 }
Jim Ingham8af3b9c2013-03-29 01:18:12 +00003597
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003598 error = DoDestroy();
3599 if (error.Success())
3600 {
3601 DidDestroy();
3602 StopPrivateStateThread();
3603 }
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003604 m_stdio_communication.StopReadThread();
3605 m_stdio_communication.Disconnect();
Greg Claytonb4874f12014-02-28 18:22:24 +00003606
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003607 if (m_process_input_reader)
Greg Claytonb4874f12014-02-28 18:22:24 +00003608 {
3609 m_process_input_reader->SetIsDone(true);
3610 m_process_input_reader->Cancel();
Caroline Ticeef5c6d02010-11-16 05:07:41 +00003611 m_process_input_reader.reset();
Greg Claytonb4874f12014-02-28 18:22:24 +00003612 }
3613
Greg Clayton85fb1b92012-09-11 02:33:37 +00003614 // If we exited when we were waiting for a process to stop, then
3615 // forward the event here so we don't lose the event
3616 if (exit_event_sp)
3617 {
3618 // Directly broadcast our exited event because we shut down our
3619 // private state thread above
3620 BroadcastEvent(exit_event_sp);
3621 }
3622
Jim Inghamb1e2e842012-04-12 18:49:31 +00003623 // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating
3624 // the last events through the event system, in which case we might strand the write lock. Unlock
3625 // 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 +00003626 m_public_run_lock.SetStopped();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003627 }
Jim Ingham09437922013-03-01 20:04:25 +00003628
3629 m_destroy_in_process = false;
3630
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003631 return error;
3632}
3633
3634Error
3635Process::Signal (int signal)
3636{
3637 Error error (WillSignal());
3638 if (error.Success())
3639 {
3640 error = DoSignal(signal);
3641 if (error.Success())
3642 DidSignal();
3643 }
3644 return error;
3645}
3646
Greg Clayton514487e2011-02-15 21:59:32 +00003647lldb::ByteOrder
3648Process::GetByteOrder () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003649{
Greg Clayton514487e2011-02-15 21:59:32 +00003650 return m_target.GetArchitecture().GetByteOrder();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003651}
3652
3653uint32_t
Greg Clayton514487e2011-02-15 21:59:32 +00003654Process::GetAddressByteSize () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003655{
Greg Clayton514487e2011-02-15 21:59:32 +00003656 return m_target.GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003657}
3658
Greg Clayton514487e2011-02-15 21:59:32 +00003659
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003660bool
3661Process::ShouldBroadcastEvent (Event *event_ptr)
3662{
3663 const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr);
3664 bool return_value = true;
Greg Clayton5160ce52013-03-27 23:08:40 +00003665 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS));
Jim Ingham0161b492013-02-09 01:29:05 +00003666
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003667 switch (state)
3668 {
Greg Claytonb766a732011-02-04 01:58:07 +00003669 case eStateConnected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003670 case eStateAttaching:
3671 case eStateLaunching:
3672 case eStateDetached:
3673 case eStateExited:
3674 case eStateUnloaded:
3675 // These events indicate changes in the state of the debugging session, always report them.
3676 return_value = true;
3677 break;
3678 case eStateInvalid:
3679 // We stopped for no apparent reason, don't report it.
3680 return_value = false;
3681 break;
3682 case eStateRunning:
3683 case eStateStepping:
3684 // If we've started the target running, we handle the cases where we
3685 // are already running and where there is a transition from stopped to
3686 // running differently.
3687 // running -> running: Automatically suppress extra running events
3688 // stopped -> running: Report except when there is one or more no votes
3689 // and no yes votes.
3690 SynchronouslyNotifyStateChanged (state);
Jim Ingham1460e4b2014-01-10 23:46:59 +00003691 if (m_force_next_event_delivery)
3692 return_value = true;
3693 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003694 {
Jim Ingham1460e4b2014-01-10 23:46:59 +00003695 switch (m_last_broadcast_state)
3696 {
3697 case eStateRunning:
3698 case eStateStepping:
3699 // We always suppress multiple runnings with no PUBLIC stop in between.
3700 return_value = false;
3701 break;
3702 default:
3703 // TODO: make this work correctly. For now always report
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003704 // run if we aren't running so we don't miss any running
Jim Ingham1460e4b2014-01-10 23:46:59 +00003705 // events. If I run the lldb/test/thread/a.out file and
3706 // break at main.cpp:58, run and hit the breakpoints on
3707 // multiple threads, then somehow during the stepping over
3708 // of all breakpoints no run gets reported.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003709
Jim Ingham1460e4b2014-01-10 23:46:59 +00003710 // This is a transition from stop to run.
3711 switch (m_thread_list.ShouldReportRun (event_ptr))
3712 {
3713 case eVoteYes:
3714 case eVoteNoOpinion:
3715 return_value = true;
3716 break;
3717 case eVoteNo:
3718 return_value = false;
3719 break;
3720 }
3721 break;
3722 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003723 }
3724 break;
3725 case eStateStopped:
3726 case eStateCrashed:
3727 case eStateSuspended:
3728 {
3729 // We've stopped. First see if we're going to restart the target.
3730 // If we are going to stop, then we always broadcast the event.
3731 // 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 +00003732 // If no thread has an opinion, we don't report it.
Jim Ingham221d51c2013-05-08 00:35:16 +00003733
Jim Inghamcb4ca112012-05-16 01:32:14 +00003734 RefreshStateAfterStop ();
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003735 if (ProcessEventData::GetInterruptedFromEvent (event_ptr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003736 {
Greg Clayton3af9ea52010-11-18 05:57:03 +00003737 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003738 log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003739 static_cast<void*>(event_ptr),
Jim Ingham0161b492013-02-09 01:29:05 +00003740 StateAsCString(state));
Jim Ingham35878c42014-04-08 21:33:21 +00003741 // Even though we know we are going to stop, we should let the threads have a look at the stop,
3742 // so they can properly set their state.
3743 m_thread_list.ShouldStop (event_ptr);
Jim Ingham0161b492013-02-09 01:29:05 +00003744 return_value = true;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00003745 }
3746 else
3747 {
Jim Ingham221d51c2013-05-08 00:35:16 +00003748 bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr);
3749 bool should_resume = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003750
Jim Ingham0161b492013-02-09 01:29:05 +00003751 // It makes no sense to ask "ShouldStop" if we've already been restarted...
3752 // Asking the thread list is also not likely to go well, since we are running again.
3753 // So in that case just report the event.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003754
Jim Ingham0161b492013-02-09 01:29:05 +00003755 if (!was_restarted)
3756 should_resume = m_thread_list.ShouldStop (event_ptr) == false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003757
Jim Ingham221d51c2013-05-08 00:35:16 +00003758 if (was_restarted || should_resume || m_resume_requested)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003759 {
Jim Ingham0161b492013-02-09 01:29:05 +00003760 Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr);
3761 if (log)
3762 log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003763 should_resume, StateAsCString(state),
3764 was_restarted, stop_vote);
3765
Jim Ingham0161b492013-02-09 01:29:05 +00003766 switch (stop_vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003767 {
3768 case eVoteYes:
Jim Ingham0161b492013-02-09 01:29:05 +00003769 return_value = true;
3770 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003771 case eVoteNoOpinion:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003772 case eVoteNo:
3773 return_value = false;
3774 break;
3775 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003776
Jim Inghamcb95f342012-09-05 21:13:56 +00003777 if (!was_restarted)
Jim Ingham0161b492013-02-09 01:29:05 +00003778 {
3779 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003780 log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s",
3781 static_cast<void*>(event_ptr),
3782 StateAsCString(state));
Jim Ingham0161b492013-02-09 01:29:05 +00003783 ProcessEventData::SetRestartedInEvent(event_ptr, true);
Jim Inghamcb95f342012-09-05 21:13:56 +00003784 PrivateResume ();
Jim Ingham0161b492013-02-09 01:29:05 +00003785 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003786
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003787 }
3788 else
3789 {
3790 return_value = true;
3791 SynchronouslyNotifyStateChanged (state);
3792 }
3793 }
3794 }
Jim Ingham0161b492013-02-09 01:29:05 +00003795 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003796 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003797
Jim Ingham1460e4b2014-01-10 23:46:59 +00003798 // Forcing the next event delivery is a one shot deal. So reset it here.
3799 m_force_next_event_delivery = false;
3800
Jim Ingham0161b492013-02-09 01:29:05 +00003801 // We do some coalescing of events (for instance two consecutive running events get coalesced.)
3802 // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state
3803 // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done,
3804 // because the PublicState reflects the last event pulled off the queue, and there may be several
3805 // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event
3806 // yet. m_last_broadcast_state gets updated here.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003807
Jim Ingham0161b492013-02-09 01:29:05 +00003808 if (return_value)
3809 m_last_broadcast_state = state;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003810
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003811 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003812 log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003813 static_cast<void*>(event_ptr), StateAsCString(state),
Jim Ingham0161b492013-02-09 01:29:05 +00003814 StateAsCString(m_last_broadcast_state),
3815 return_value ? "YES" : "NO");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003816 return return_value;
3817}
3818
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003819
3820bool
Jim Ingham372787f2012-04-07 00:00:41 +00003821Process::StartPrivateStateThread (bool force)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003822{
Greg Clayton5160ce52013-03-27 23:08:40 +00003823 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003824
Greg Clayton8b82f082011-04-12 05:54:46 +00003825 bool already_running = PrivateStateThreadIsValid ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003826 if (log)
Greg Clayton8b82f082011-04-12 05:54:46 +00003827 log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread");
3828
Jim Ingham372787f2012-04-07 00:00:41 +00003829 if (!force && already_running)
Greg Clayton8b82f082011-04-12 05:54:46 +00003830 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003831
3832 // Create a thread that watches our internal state and controls which
3833 // events make it to clients (into the DCProcess event queue).
Greg Clayton3e06bd92011-01-09 21:07:35 +00003834 char thread_name[1024];
Todd Fiala17096d72014-07-16 19:03:16 +00003835
3836 if (Host::MAX_THREAD_NAME_LENGTH <= 16)
3837 {
3838 // On platforms with abbreviated thread name lengths, choose thread names that fit within the limit.
3839 if (already_running)
3840 snprintf(thread_name, sizeof(thread_name), "intern-state-OV");
3841 else
3842 snprintf(thread_name, sizeof(thread_name), "intern-state");
3843 }
Jim Ingham372787f2012-04-07 00:00:41 +00003844 else
Todd Fiala17096d72014-07-16 19:03:16 +00003845 {
3846 if (already_running)
3847 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID());
3848 else
3849 snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID());
3850 }
3851
Jim Ingham076b3042012-04-10 01:21:57 +00003852 // Create the private state thread, and start it running.
Greg Clayton3e06bd92011-01-09 21:07:35 +00003853 m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL);
Jim Ingham076b3042012-04-10 01:21:57 +00003854 bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread);
3855 if (success)
3856 {
3857 ResumePrivateStateThread();
3858 return true;
3859 }
3860 else
3861 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003862}
3863
3864void
3865Process::PausePrivateStateThread ()
3866{
3867 ControlPrivateStateThread (eBroadcastInternalStateControlPause);
3868}
3869
3870void
3871Process::ResumePrivateStateThread ()
3872{
3873 ControlPrivateStateThread (eBroadcastInternalStateControlResume);
3874}
3875
3876void
3877Process::StopPrivateStateThread ()
3878{
Greg Clayton8b82f082011-04-12 05:54:46 +00003879 if (PrivateStateThreadIsValid ())
3880 ControlPrivateStateThread (eBroadcastInternalStateControlStop);
Jim Inghamb1e2e842012-04-12 18:49:31 +00003881 else
3882 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003883 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Jim Inghamb1e2e842012-04-12 18:49:31 +00003884 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00003885 log->Printf ("Went to stop the private state thread, but it was already invalid.");
Jim Inghamb1e2e842012-04-12 18:49:31 +00003886 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003887}
3888
3889void
3890Process::ControlPrivateStateThread (uint32_t signal)
3891{
Greg Clayton5160ce52013-03-27 23:08:40 +00003892 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003893
3894 assert (signal == eBroadcastInternalStateControlStop ||
3895 signal == eBroadcastInternalStateControlPause ||
3896 signal == eBroadcastInternalStateControlResume);
3897
3898 if (log)
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003899 log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003900
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003901 // Signal the private state thread. First we should copy this is case the
3902 // thread starts exiting since the private state thread will NULL this out
3903 // when it exits
3904 const lldb::thread_t private_state_thread = m_private_state_thread;
Greg Clayton2da6d492011-02-08 01:34:25 +00003905 if (IS_VALID_LLDB_HOST_THREAD(private_state_thread))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003906 {
3907 TimeValue timeout_time;
3908 bool timed_out;
3909
3910 m_private_state_control_broadcaster.BroadcastEvent (signal, NULL);
3911
3912 timeout_time = TimeValue::Now();
3913 timeout_time.OffsetWithSeconds(2);
Jim Inghamb1e2e842012-04-12 18:49:31 +00003914 if (log)
3915 log->Printf ("Sending control event of type: %d.", signal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003916 m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out);
3917 m_private_state_control_wait.SetValue (false, eBroadcastNever);
3918
3919 if (signal == eBroadcastInternalStateControlStop)
3920 {
3921 if (timed_out)
Jim Inghamb1e2e842012-04-12 18:49:31 +00003922 {
3923 Error error;
3924 Host::ThreadCancel (private_state_thread, &error);
3925 if (log)
3926 log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString());
3927 }
3928 else
3929 {
3930 if (log)
3931 log->Printf ("The control event killed the private state thread without having to cancel.");
3932 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003933
3934 thread_result_t result = NULL;
Greg Clayton7ecb3a02011-01-22 17:43:17 +00003935 Host::ThreadJoin (private_state_thread, &result, NULL);
Greg Clayton49182ed2010-07-22 18:34:21 +00003936 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003937 }
3938 }
Jim Inghamb1e2e842012-04-12 18:49:31 +00003939 else
3940 {
3941 if (log)
3942 log->Printf ("Private state thread already dead, no need to signal it to stop.");
3943 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003944}
3945
3946void
Jim Inghamcfc09352012-07-27 23:57:19 +00003947Process::SendAsyncInterrupt ()
3948{
3949 if (PrivateStateThreadIsValid())
3950 m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
3951 else
3952 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
3953}
3954
3955void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003956Process::HandlePrivateEvent (EventSP &event_sp)
3957{
Greg Clayton5160ce52013-03-27 23:08:40 +00003958 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham221d51c2013-05-08 00:35:16 +00003959 m_resume_requested = false;
3960
Jim Inghamaacc3182012-06-06 00:29:30 +00003961 m_currently_handling_event.SetValue(true, eBroadcastNever);
Jim Inghambb3a2832011-01-29 01:49:25 +00003962
Greg Clayton414f5d32011-01-25 02:58:48 +00003963 const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Inghambb3a2832011-01-29 01:49:25 +00003964
3965 // First check to see if anybody wants a shot at this event:
Jim Ingham754ab982011-01-29 04:05:41 +00003966 if (m_next_event_action_ap.get() != NULL)
Jim Inghambb3a2832011-01-29 01:49:25 +00003967 {
Jim Ingham754ab982011-01-29 04:05:41 +00003968 NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp);
Jim Ingham0161b492013-02-09 01:29:05 +00003969 if (log)
3970 log->Printf ("Ran next event action, result was %d.", action_result);
3971
Jim Inghambb3a2832011-01-29 01:49:25 +00003972 switch (action_result)
3973 {
3974 case NextEventAction::eEventActionSuccess:
3975 SetNextEventAction(NULL);
3976 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003977
Jim Inghambb3a2832011-01-29 01:49:25 +00003978 case NextEventAction::eEventActionRetry:
3979 break;
Greg Claytonc9ed4782011-11-12 02:10:56 +00003980
Jim Inghambb3a2832011-01-29 01:49:25 +00003981 case NextEventAction::eEventActionExit:
Jim Ingham2a5fdd42011-01-29 01:57:31 +00003982 // Handle Exiting Here. If we already got an exited event,
3983 // we should just propagate it. Otherwise, swallow this event,
3984 // and set our state to exit so the next event will kill us.
3985 if (new_state != eStateExited)
3986 {
3987 // FIXME: should cons up an exited event, and discard this one.
Jim Ingham754ab982011-01-29 04:05:41 +00003988 SetExitStatus(0, m_next_event_action_ap->GetExitString());
Jim Ingham221d51c2013-05-08 00:35:16 +00003989 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Jim Ingham2a5fdd42011-01-29 01:57:31 +00003990 SetNextEventAction(NULL);
3991 return;
3992 }
3993 SetNextEventAction(NULL);
Jim Inghambb3a2832011-01-29 01:49:25 +00003994 break;
3995 }
3996 }
3997
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003998 // See if we should broadcast this state to external clients?
3999 const bool should_broadcast = ShouldBroadcastEvent (event_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004000
4001 if (should_broadcast)
4002 {
Greg Claytonb4874f12014-02-28 18:22:24 +00004003 const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004004 if (log)
4005 {
Daniel Malead01b2952012-11-29 21:49:15 +00004006 log->Printf ("Process::%s (pid = %" PRIu64 ") broadcasting new state %s (old state %s) to %s",
Greg Clayton414f5d32011-01-25 02:58:48 +00004007 __FUNCTION__,
4008 GetID(),
4009 StateAsCString(new_state),
4010 StateAsCString (GetState ()),
Greg Claytonb4874f12014-02-28 18:22:24 +00004011 is_hijacked ? "hijacked" : "public");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004012 }
Jim Ingham9575d842011-03-11 03:53:59 +00004013 Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get());
Greg Clayton414f5d32011-01-25 02:58:48 +00004014 if (StateIsRunningState (new_state))
Greg Clayton44d93782014-01-27 23:43:24 +00004015 {
4016 // Only push the input handler if we aren't fowarding events,
4017 // as this means the curses GUI is in use...
4018 if (!GetTarget().GetDebugger().IsForwardingEvents())
4019 PushProcessIOHandler ();
Todd Fialaa3b89e22014-08-12 14:33:19 +00004020 m_iohandler_sync.SetValue(true, eBroadcastAlways);
Greg Clayton44d93782014-01-27 23:43:24 +00004021 }
Greg Claytonb4874f12014-02-28 18:22:24 +00004022 else if (StateIsStoppedState(new_state, false))
4023 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00004024 m_iohandler_sync.SetValue(false, eBroadcastNever);
Greg Claytonb4874f12014-02-28 18:22:24 +00004025 if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
4026 {
4027 // If the lldb_private::Debugger is handling the events, we don't
4028 // want to pop the process IOHandler here, we want to do it when
4029 // we receive the stopped event so we can carefully control when
4030 // the process IOHandler is popped because when we stop we want to
4031 // display some text stating how and why we stopped, then maybe some
4032 // process/thread/frame info, and then we want the "(lldb) " prompt
4033 // to show up. If we pop the process IOHandler here, then we will
4034 // cause the command interpreter to become the top IOHandler after
4035 // the process pops off and it will update its prompt right away...
4036 // See the Debugger.cpp file where it calls the function as
4037 // "process_sp->PopProcessIOHandler()" to see where I am talking about.
4038 // Otherwise we end up getting overlapping "(lldb) " prompts and
4039 // garbled output.
4040 //
4041 // If we aren't handling the events in the debugger (which is indicated
4042 // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or we
4043 // are hijacked, then we always pop the process IO handler manually.
4044 // Hijacking happens when the internal process state thread is running
4045 // thread plans, or when commands want to run in synchronous mode
4046 // and they call "process->WaitForProcessToStop()". An example of something
4047 // that will hijack the events is a simple expression:
4048 //
4049 // (lldb) expr (int)puts("hello")
4050 //
4051 // This will cause the internal process state thread to resume and halt
4052 // the process (and _it_ will hijack the eBroadcastBitStateChanged
4053 // events) and we do need the IO handler to be pushed and popped
4054 // correctly.
4055
4056 if (is_hijacked || m_target.GetDebugger().IsHandlingEvents() == false)
4057 PopProcessIOHandler ();
4058 }
4059 }
Jim Ingham9575d842011-03-11 03:53:59 +00004060
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004061 BroadcastEvent (event_sp);
4062 }
4063 else
4064 {
4065 if (log)
4066 {
Daniel Malead01b2952012-11-29 21:49:15 +00004067 log->Printf ("Process::%s (pid = %" PRIu64 ") suppressing state %s (old state %s): should_broadcast == false",
Greg Clayton414f5d32011-01-25 02:58:48 +00004068 __FUNCTION__,
4069 GetID(),
4070 StateAsCString(new_state),
Jason Molendafd54b362011-09-20 21:44:10 +00004071 StateAsCString (GetState ()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004072 }
4073 }
Jim Inghamaacc3182012-06-06 00:29:30 +00004074 m_currently_handling_event.SetValue(false, eBroadcastAlways);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004075}
4076
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004077thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004078Process::PrivateStateThread (void *arg)
4079{
4080 Process *proc = static_cast<Process*> (arg);
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004081 thread_result_t result = proc->RunPrivateStateThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004082 return result;
4083}
4084
Virgile Bellob2f1fb22013-08-23 12:44:05 +00004085thread_result_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004086Process::RunPrivateStateThread ()
4087{
Jim Ingham076b3042012-04-10 01:21:57 +00004088 bool control_only = true;
Jim Inghamb1e2e842012-04-12 18:49:31 +00004089 m_private_state_control_wait.SetValue (false, eBroadcastNever);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004090
Greg Clayton5160ce52013-03-27 23:08:40 +00004091 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004092 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004093 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...",
4094 __FUNCTION__, static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004095
4096 bool exit_now = false;
4097 while (!exit_now)
4098 {
4099 EventSP event_sp;
4100 WaitForEventsPrivate (NULL, event_sp, control_only);
4101 if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster))
4102 {
Jim Inghamb1e2e842012-04-12 18:49:31 +00004103 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004104 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d",
4105 __FUNCTION__, static_cast<void*>(this), GetID(),
4106 event_sp->GetType());
Jim Inghamb1e2e842012-04-12 18:49:31 +00004107
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004108 switch (event_sp->GetType())
4109 {
4110 case eBroadcastInternalStateControlStop:
4111 exit_now = true;
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004112 break; // doing any internal state management below
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004113
4114 case eBroadcastInternalStateControlPause:
4115 control_only = true;
4116 break;
4117
4118 case eBroadcastInternalStateControlResume:
4119 control_only = false;
4120 break;
4121 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004122
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004123 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004124 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004125 }
Jim Inghamcfc09352012-07-27 23:57:19 +00004126 else if (event_sp->GetType() == eBroadcastBitInterrupt)
4127 {
4128 if (m_public_state.GetValue() == eStateAttaching)
4129 {
4130 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004131 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.",
4132 __FUNCTION__, static_cast<void*>(this),
4133 GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004134 BroadcastEvent (eBroadcastBitInterrupt, NULL);
4135 }
4136 else
4137 {
4138 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004139 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.",
4140 __FUNCTION__, static_cast<void*>(this),
4141 GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00004142 Halt();
4143 }
4144 continue;
4145 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004146
4147 const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
4148
4149 if (internal_state != eStateInvalid)
4150 {
Greg Claytonf9b57b92013-05-10 23:48:10 +00004151 if (m_clear_thread_plans_on_stop &&
4152 StateIsStoppedState(internal_state, true))
4153 {
4154 m_clear_thread_plans_on_stop = false;
4155 m_thread_list.DiscardThreadPlans();
4156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004157 HandlePrivateEvent (event_sp);
4158 }
4159
Greg Clayton58d1c9a2010-10-18 04:14:23 +00004160 if (internal_state == eStateInvalid ||
4161 internal_state == eStateExited ||
4162 internal_state == eStateDetached )
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004163 {
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004164 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004165 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...",
4166 __FUNCTION__, static_cast<void*>(this), GetID(),
4167 StateAsCString(internal_state));
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004168
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004169 break;
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004170 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004171 }
4172
Caroline Tice20ad3c42010-10-29 21:48:37 +00004173 // Verify log is still enabled before attempting to write to it...
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004174 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004175 log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...",
4176 __FUNCTION__, static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004177
Ed Maste64fad602013-07-29 20:58:06 +00004178 m_public_run_lock.SetStopped();
Greg Clayton6ed95942011-01-22 07:12:45 +00004179 m_private_state_control_wait.SetValue (true, eBroadcastAlways);
4180 m_private_state_thread = LLDB_INVALID_HOST_THREAD;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004181 return NULL;
4182}
4183
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004184//------------------------------------------------------------------
4185// Process Event Data
4186//------------------------------------------------------------------
4187
4188Process::ProcessEventData::ProcessEventData () :
4189 EventData (),
4190 m_process_sp (),
4191 m_state (eStateInvalid),
Greg Claytonc982c762010-07-09 20:39:50 +00004192 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004193 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004194 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004195{
4196}
4197
4198Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) :
4199 EventData (),
4200 m_process_sp (process_sp),
4201 m_state (state),
Greg Claytonc982c762010-07-09 20:39:50 +00004202 m_restarted (false),
Jim Inghama8604692011-05-22 21:45:01 +00004203 m_update_state (0),
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004204 m_interrupted (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004205{
4206}
4207
4208Process::ProcessEventData::~ProcessEventData()
4209{
4210}
4211
4212const ConstString &
4213Process::ProcessEventData::GetFlavorString ()
4214{
4215 static ConstString g_flavor ("Process::ProcessEventData");
4216 return g_flavor;
4217}
4218
4219const ConstString &
4220Process::ProcessEventData::GetFlavor () const
4221{
4222 return ProcessEventData::GetFlavorString ();
4223}
4224
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004225void
4226Process::ProcessEventData::DoOnRemoval (Event *event_ptr)
4227{
4228 // This function gets called twice for each event, once when the event gets pulled
Jim Inghama8604692011-05-22 21:45:01 +00004229 // off of the private process event queue, and then any number of times, first when it gets pulled off of
4230 // the public event queue, then other times when we're pretending that this is where we stopped at the
4231 // end of expression evaluation. m_update_state is used to distinguish these
4232 // three cases; it is 0 when we're just pulling it off for private handling,
Jim Ingham221d51c2013-05-08 00:35:16 +00004233 // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then.
Jim Inghama8604692011-05-22 21:45:01 +00004234 if (m_update_state != 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004235 return;
Jim Ingham0161b492013-02-09 01:29:05 +00004236
Jim Ingham221d51c2013-05-08 00:35:16 +00004237 m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr));
Jim Ingham35878c42014-04-08 21:33:21 +00004238
4239 // If this is a halt event, even if the halt stopped with some reason other than a plain interrupt (e.g. we had
4240 // already stopped for a breakpoint when the halt request came through) don't do the StopInfo actions, as they may
4241 // end up restarting the process.
4242 if (m_interrupted)
4243 return;
4244
4245 // If we're stopped and haven't restarted, then do the StopInfo actions here:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004246 if (m_state == eStateStopped && ! m_restarted)
Jim Ingham0faa43f2011-11-08 03:00:11 +00004247 {
4248 ThreadList &curr_thread_list = m_process_sp->GetThreadList();
Greg Clayton61e7a582011-12-01 23:28:38 +00004249 uint32_t num_threads = curr_thread_list.GetSize();
4250 uint32_t idx;
Greg Claytonf4b47e12010-08-04 01:40:35 +00004251
Jim Ingham4b536182011-08-09 02:12:22 +00004252 // The actions might change one of the thread's stop_info's opinions about whether we should
4253 // stop the process, so we need to query that as we go.
Jim Ingham0faa43f2011-11-08 03:00:11 +00004254
4255 // One other complication here, is that we try to catch any case where the target has run (except for expressions)
4256 // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and
4257 // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like
4258 // 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
4259 // against this list & bag out if anything differs.
Greg Clayton61e7a582011-12-01 23:28:38 +00004260 std::vector<uint32_t> thread_index_array(num_threads);
Jim Ingham0faa43f2011-11-08 03:00:11 +00004261 for (idx = 0; idx < num_threads; ++idx)
4262 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID();
4263
Jim Inghamc7078c22012-12-13 22:24:15 +00004264 // Use this to track whether we should continue from here. We will only continue the target running if
4265 // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running,
4266 // then it doesn't matter what the other threads say...
4267
4268 bool still_should_stop = false;
Jim Ingham4b536182011-08-09 02:12:22 +00004269
Jim Ingham0ad7e052013-04-25 02:04:59 +00004270 // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a
4271 // valid stop reason. In that case we should just stop, because we have no way of telling what the right
4272 // thing to do is, and it's better to let the user decide than continue behind their backs.
4273
4274 bool does_anybody_have_an_opinion = false;
4275
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004276 for (idx = 0; idx < num_threads; ++idx)
4277 {
Jim Ingham0faa43f2011-11-08 03:00:11 +00004278 curr_thread_list = m_process_sp->GetThreadList();
4279 if (curr_thread_list.GetSize() != num_threads)
4280 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004281 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004282 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004283 log->Printf("Number of threads changed from %u to %u while processing event.", num_threads, curr_thread_list.GetSize());
Jim Ingham0faa43f2011-11-08 03:00:11 +00004284 break;
4285 }
4286
4287 lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx);
4288
4289 if (thread_sp->GetIndexID() != thread_index_array[idx])
4290 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004291 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham87c665f2011-12-01 20:26:15 +00004292 if (log)
Greg Clayton61e7a582011-12-01 23:28:38 +00004293 log->Printf("The thread at position %u changed from %u to %u while processing event.",
Jim Ingham87c665f2011-12-01 20:26:15 +00004294 idx,
4295 thread_index_array[idx],
4296 thread_sp->GetIndexID());
Jim Ingham0faa43f2011-11-08 03:00:11 +00004297 break;
4298 }
4299
Jim Inghamb15bfc72010-10-20 00:39:53 +00004300 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00004301 if (stop_info_sp && stop_info_sp->IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004302 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004303 does_anybody_have_an_opinion = true;
Jim Ingham0161b492013-02-09 01:29:05 +00004304 bool this_thread_wants_to_stop;
4305 if (stop_info_sp->GetOverrideShouldStop())
Jim Ingham4b536182011-08-09 02:12:22 +00004306 {
Jim Ingham0161b492013-02-09 01:29:05 +00004307 this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue();
4308 }
4309 else
4310 {
4311 stop_info_sp->PerformAction(event_ptr);
4312 // The stop action might restart the target. If it does, then we want to mark that in the
4313 // event so that whoever is receiving it will know to wait for the running event and reflect
4314 // that state appropriately.
4315 // We also need to stop processing actions, since they aren't expecting the target to be running.
4316
4317 // FIXME: we might have run.
4318 if (stop_info_sp->HasTargetRunSinceMe())
4319 {
4320 SetRestarted (true);
4321 break;
4322 }
4323
4324 this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr);
Jim Ingham4b536182011-08-09 02:12:22 +00004325 }
Jim Inghamc7078c22012-12-13 22:24:15 +00004326
Jim Inghamc7078c22012-12-13 22:24:15 +00004327 if (still_should_stop == false)
4328 still_should_stop = this_thread_wants_to_stop;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004329 }
4330 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +00004331
Ashok Thirumurthicf7c55e2013-04-18 14:38:20 +00004332
Jim Inghama8ca6e22013-05-03 23:04:37 +00004333 if (!GetRestarted())
Jim Ingham9575d842011-03-11 03:53:59 +00004334 {
Jim Ingham0ad7e052013-04-25 02:04:59 +00004335 if (!still_should_stop && does_anybody_have_an_opinion)
Jim Ingham4b536182011-08-09 02:12:22 +00004336 {
4337 // We've been asked to continue, so do that here.
Jim Ingham9575d842011-03-11 03:53:59 +00004338 SetRestarted(true);
Jim Ingham3b8285d2012-04-19 01:40:33 +00004339 // Use the public resume method here, since this is just
4340 // extending a public resume.
Jim Ingham0161b492013-02-09 01:29:05 +00004341 m_process_sp->PrivateResume();
Jim Ingham4b536182011-08-09 02:12:22 +00004342 }
4343 else
4344 {
4345 // If we didn't restart, run the Stop Hooks here:
4346 // They might also restart the target, so watch for that.
4347 m_process_sp->GetTarget().RunStopHooks();
4348 if (m_process_sp->GetPrivateState() == eStateRunning)
4349 SetRestarted(true);
4350 }
Jim Ingham9575d842011-03-11 03:53:59 +00004351 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004352 }
4353}
4354
4355void
4356Process::ProcessEventData::Dump (Stream *s) const
4357{
4358 if (m_process_sp)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004359 s->Printf(" process = %p (pid = %" PRIu64 "), ",
4360 static_cast<void*>(m_process_sp.get()), m_process_sp->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004361
Greg Clayton8b82f082011-04-12 05:54:46 +00004362 s->Printf("state = %s", StateAsCString(GetState()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004363}
4364
4365const Process::ProcessEventData *
4366Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr)
4367{
4368 if (event_ptr)
4369 {
4370 const EventData *event_data = event_ptr->GetData();
4371 if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString())
4372 return static_cast <const ProcessEventData *> (event_ptr->GetData());
4373 }
4374 return NULL;
4375}
4376
4377ProcessSP
4378Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr)
4379{
4380 ProcessSP process_sp;
4381 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4382 if (data)
4383 process_sp = data->GetProcessSP();
4384 return process_sp;
4385}
4386
4387StateType
4388Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr)
4389{
4390 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4391 if (data == NULL)
4392 return eStateInvalid;
4393 else
4394 return data->GetState();
4395}
4396
4397bool
4398Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr)
4399{
4400 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4401 if (data == NULL)
4402 return false;
4403 else
4404 return data->GetRestarted();
4405}
4406
4407void
4408Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value)
4409{
4410 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4411 if (data != NULL)
4412 data->SetRestarted(new_value);
4413}
4414
Jim Ingham0161b492013-02-09 01:29:05 +00004415size_t
4416Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr)
4417{
4418 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4419 if (data != NULL)
4420 return data->GetNumRestartedReasons();
4421 else
4422 return 0;
4423}
4424
4425const char *
4426Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx)
4427{
4428 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4429 if (data != NULL)
4430 return data->GetRestartedReasonAtIndex(idx);
4431 else
4432 return NULL;
4433}
4434
4435void
4436Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason)
4437{
4438 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4439 if (data != NULL)
4440 data->AddRestartedReason(reason);
4441}
4442
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004443bool
Jim Ingham0d8bcc72010-11-17 02:32:00 +00004444Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr)
4445{
4446 const ProcessEventData *data = GetEventDataFromEvent (event_ptr);
4447 if (data == NULL)
4448 return false;
4449 else
4450 return data->GetInterrupted ();
4451}
4452
4453void
4454Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value)
4455{
4456 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4457 if (data != NULL)
4458 data->SetInterrupted(new_value);
4459}
4460
4461bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004462Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr)
4463{
4464 ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr));
4465 if (data)
4466 {
4467 data->SetUpdateStateOnRemoval();
4468 return true;
4469 }
4470 return false;
4471}
4472
Greg Claytond9e416c2012-02-18 05:35:26 +00004473lldb::TargetSP
4474Process::CalculateTarget ()
4475{
4476 return m_target.shared_from_this();
4477}
4478
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004479void
Greg Clayton0603aa92010-10-04 01:05:56 +00004480Process::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004481{
Greg Claytonc14ee322011-09-22 04:58:26 +00004482 exe_ctx.SetTargetPtr (&m_target);
4483 exe_ctx.SetProcessPtr (this);
4484 exe_ctx.SetThreadPtr(NULL);
4485 exe_ctx.SetFramePtr (NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004486}
4487
Greg Claytone996fd32011-03-08 22:40:15 +00004488//uint32_t
4489//Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids)
4490//{
4491// return 0;
4492//}
4493//
4494//ArchSpec
4495//Process::GetArchSpecForExistingProcess (lldb::pid_t pid)
4496//{
4497// return Host::GetArchSpecForExistingProcess (pid);
4498//}
4499//
4500//ArchSpec
4501//Process::GetArchSpecForExistingProcess (const char *process_name)
4502//{
4503// return Host::GetArchSpecForExistingProcess (process_name);
4504//}
4505//
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004506void
4507Process::AppendSTDOUT (const char * s, size_t len)
4508{
Greg Clayton3af9ea52010-11-18 05:57:03 +00004509 Mutex::Locker locker (m_stdio_communication_mutex);
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004510 m_stdout_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004511 BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState()));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004512}
4513
4514void
Greg Clayton93e86192011-11-13 04:45:22 +00004515Process::AppendSTDERR (const char * s, size_t len)
4516{
4517 Mutex::Locker locker (m_stdio_communication_mutex);
4518 m_stderr_data.append (s, len);
Greg Clayton35a4cc52012-10-29 20:52:08 +00004519 BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState()));
Greg Clayton93e86192011-11-13 04:45:22 +00004520}
4521
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004522void
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004523Process::BroadcastAsyncProfileData(const std::string &one_profile_data)
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004524{
4525 Mutex::Locker locker (m_profile_data_comm_mutex);
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004526 m_profile_data.push_back(one_profile_data);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004527 BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState()));
4528}
4529
4530size_t
4531Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error)
4532{
4533 Mutex::Locker locker(m_profile_data_comm_mutex);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004534 if (m_profile_data.empty())
4535 return 0;
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004536
4537 std::string &one_profile_data = m_profile_data.front();
4538 size_t bytes_available = one_profile_data.size();
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004539 if (bytes_available > 0)
4540 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004541 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004542 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004543 log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")",
4544 static_cast<void*>(buf),
4545 static_cast<uint64_t>(buf_size));
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004546 if (bytes_available > buf_size)
4547 {
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004548 memcpy(buf, one_profile_data.c_str(), buf_size);
4549 one_profile_data.erase(0, buf_size);
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004550 bytes_available = buf_size;
4551 }
4552 else
4553 {
Han Ming Ong91ed6b82013-06-24 18:15:05 +00004554 memcpy(buf, one_profile_data.c_str(), bytes_available);
Han Ming Ong929a94f2012-11-29 22:14:45 +00004555 m_profile_data.erase(m_profile_data.begin());
Han Ming Ongab3b8b22012-11-17 00:21:04 +00004556 }
4557 }
4558 return bytes_available;
4559}
4560
4561
Greg Clayton93e86192011-11-13 04:45:22 +00004562//------------------------------------------------------------------
4563// Process STDIO
4564//------------------------------------------------------------------
4565
4566size_t
4567Process::GetSTDOUT (char *buf, size_t buf_size, Error &error)
4568{
4569 Mutex::Locker locker(m_stdio_communication_mutex);
4570 size_t bytes_available = m_stdout_data.size();
4571 if (bytes_available > 0)
4572 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004573 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004574 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004575 log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")",
4576 static_cast<void*>(buf),
4577 static_cast<uint64_t>(buf_size));
Greg Clayton93e86192011-11-13 04:45:22 +00004578 if (bytes_available > buf_size)
4579 {
4580 memcpy(buf, m_stdout_data.c_str(), buf_size);
4581 m_stdout_data.erase(0, buf_size);
4582 bytes_available = buf_size;
4583 }
4584 else
4585 {
4586 memcpy(buf, m_stdout_data.c_str(), bytes_available);
4587 m_stdout_data.clear();
4588 }
4589 }
4590 return bytes_available;
4591}
4592
4593
4594size_t
4595Process::GetSTDERR (char *buf, size_t buf_size, Error &error)
4596{
4597 Mutex::Locker locker(m_stdio_communication_mutex);
4598 size_t bytes_available = m_stderr_data.size();
4599 if (bytes_available > 0)
4600 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004601 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Clayton93e86192011-11-13 04:45:22 +00004602 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004603 log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")",
4604 static_cast<void*>(buf),
4605 static_cast<uint64_t>(buf_size));
Greg Clayton93e86192011-11-13 04:45:22 +00004606 if (bytes_available > buf_size)
4607 {
4608 memcpy(buf, m_stderr_data.c_str(), buf_size);
4609 m_stderr_data.erase(0, buf_size);
4610 bytes_available = buf_size;
4611 }
4612 else
4613 {
4614 memcpy(buf, m_stderr_data.c_str(), bytes_available);
4615 m_stderr_data.clear();
4616 }
4617 }
4618 return bytes_available;
4619}
4620
4621void
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004622Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len)
4623{
4624 Process *process = (Process *) baton;
4625 process->AppendSTDOUT (static_cast<const char *>(src), src_len);
4626}
4627
Greg Clayton44d93782014-01-27 23:43:24 +00004628class IOHandlerProcessSTDIO :
4629 public IOHandler
4630{
4631public:
4632 IOHandlerProcessSTDIO (Process *process,
4633 int write_fd) :
4634 IOHandler(process->GetTarget().GetDebugger()),
4635 m_process (process),
4636 m_read_file (),
4637 m_write_file (write_fd, false),
Greg Clayton100eb932014-07-02 21:10:39 +00004638 m_pipe ()
Greg Clayton44d93782014-01-27 23:43:24 +00004639 {
4640 m_read_file.SetDescriptor(GetInputFD(), false);
4641 }
4642
4643 virtual
4644 ~IOHandlerProcessSTDIO ()
4645 {
4646
4647 }
4648
4649 bool
4650 OpenPipes ()
4651 {
Greg Clayton100eb932014-07-02 21:10:39 +00004652 if (m_pipe.IsValid())
Greg Clayton44d93782014-01-27 23:43:24 +00004653 return true;
Greg Clayton100eb932014-07-02 21:10:39 +00004654 return m_pipe.Open();
Greg Clayton44d93782014-01-27 23:43:24 +00004655 }
4656
4657 void
4658 ClosePipes()
4659 {
Greg Clayton100eb932014-07-02 21:10:39 +00004660 m_pipe.Close();
Greg Clayton44d93782014-01-27 23:43:24 +00004661 }
4662
4663 // Each IOHandler gets to run until it is done. It should read data
4664 // from the "in" and place output into "out" and "err and return
4665 // when done.
4666 virtual void
4667 Run ()
4668 {
4669 if (m_read_file.IsValid() && m_write_file.IsValid())
4670 {
4671 SetIsDone(false);
4672 if (OpenPipes())
4673 {
4674 const int read_fd = m_read_file.GetDescriptor();
Greg Clayton100eb932014-07-02 21:10:39 +00004675 const int pipe_read_fd = m_pipe.GetReadFileDescriptor();
Greg Clayton44d93782014-01-27 23:43:24 +00004676 TerminalState terminal_state;
4677 terminal_state.Save (read_fd, false);
4678 Terminal terminal(read_fd);
4679 terminal.SetCanonical(false);
4680 terminal.SetEcho(false);
Deepak Panickal914b8d92014-01-31 18:48:46 +00004681// FD_ZERO, FD_SET are not supported on windows
Hafiz Abid Qadeer6eff1012014-03-12 10:45:23 +00004682#ifndef _WIN32
Greg Clayton44d93782014-01-27 23:43:24 +00004683 while (!GetIsDone())
4684 {
4685 fd_set read_fdset;
4686 FD_ZERO (&read_fdset);
4687 FD_SET (read_fd, &read_fdset);
4688 FD_SET (pipe_read_fd, &read_fdset);
4689 const int nfds = std::max<int>(read_fd, pipe_read_fd) + 1;
4690 int num_set_fds = select (nfds, &read_fdset, NULL, NULL, NULL);
4691 if (num_set_fds < 0)
4692 {
4693 const int select_errno = errno;
4694
4695 if (select_errno != EINTR)
4696 SetIsDone(true);
4697 }
4698 else if (num_set_fds > 0)
4699 {
4700 char ch = 0;
4701 size_t n;
4702 if (FD_ISSET (read_fd, &read_fdset))
4703 {
4704 n = 1;
4705 if (m_read_file.Read(&ch, n).Success() && n == 1)
4706 {
4707 if (m_write_file.Write(&ch, n).Fail() || n != 1)
4708 SetIsDone(true);
4709 }
4710 else
4711 SetIsDone(true);
4712 }
4713 if (FD_ISSET (pipe_read_fd, &read_fdset))
4714 {
4715 // Consume the interrupt byte
Greg Clayton100eb932014-07-02 21:10:39 +00004716 if (m_pipe.Read (&ch, 1) == 1)
Greg Clayton19e11352014-02-26 22:47:33 +00004717 {
Greg Clayton100eb932014-07-02 21:10:39 +00004718 switch (ch)
4719 {
4720 case 'q':
4721 SetIsDone(true);
4722 break;
4723 case 'i':
4724 if (StateIsRunningState(m_process->GetState()))
4725 m_process->Halt();
4726 break;
4727 }
Greg Clayton19e11352014-02-26 22:47:33 +00004728 }
Greg Clayton44d93782014-01-27 23:43:24 +00004729 }
4730 }
4731 }
Deepak Panickal914b8d92014-01-31 18:48:46 +00004732#endif
Greg Clayton44d93782014-01-27 23:43:24 +00004733 terminal_state.Restore();
4734
4735 }
4736 else
4737 SetIsDone(true);
4738 }
4739 else
4740 SetIsDone(true);
4741 }
4742
4743 // Hide any characters that have been displayed so far so async
4744 // output can be displayed. Refresh() will be called after the
4745 // output has been displayed.
4746 virtual void
4747 Hide ()
4748 {
4749
4750 }
4751 // Called when the async output has been received in order to update
4752 // the input reader (refresh the prompt and redisplay any current
4753 // line(s) that are being edited
4754 virtual void
4755 Refresh ()
4756 {
4757
4758 }
Greg Claytone68f5d62014-02-24 22:50:57 +00004759
Greg Clayton44d93782014-01-27 23:43:24 +00004760 virtual void
Greg Claytone68f5d62014-02-24 22:50:57 +00004761 Cancel ()
Greg Clayton44d93782014-01-27 23:43:24 +00004762 {
Greg Clayton19e11352014-02-26 22:47:33 +00004763 char ch = 'q'; // Send 'q' for quit
Greg Clayton100eb932014-07-02 21:10:39 +00004764 m_pipe.Write (&ch, 1);
Greg Clayton44d93782014-01-27 23:43:24 +00004765 }
Greg Claytone68f5d62014-02-24 22:50:57 +00004766
Greg Claytonf0066ad2014-05-02 00:45:31 +00004767 virtual bool
Greg Claytone68f5d62014-02-24 22:50:57 +00004768 Interrupt ()
4769 {
Greg Clayton19e11352014-02-26 22:47:33 +00004770 // Do only things that are safe to do in an interrupt context (like in
4771 // a SIGINT handler), like write 1 byte to a file descriptor. This will
4772 // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte
4773 // that was written to the pipe and then call m_process->Halt() from a
4774 // much safer location in code.
Greg Clayton0fdd3ae2014-07-16 21:05:41 +00004775 if (m_active)
4776 {
4777 char ch = 'i'; // Send 'i' for interrupt
4778 return m_pipe.Write (&ch, 1) == 1;
4779 }
4780 else
4781 {
4782 // This IOHandler might be pushed on the stack, but not being run currently
4783 // so do the right thing if we aren't actively watching for STDIN by sending
4784 // the interrupt to the process. Otherwise the write to the pipe above would
4785 // do nothing. This can happen when the command interpreter is running and
4786 // gets a "expression ...". It will be on the IOHandler thread and sending
4787 // the input is complete to the delegate which will cause the expression to
4788 // run, which will push the process IO handler, but not run it.
4789
4790 if (StateIsRunningState(m_process->GetState()))
4791 {
4792 m_process->SendAsyncInterrupt();
4793 return true;
4794 }
4795 }
4796 return false;
Greg Claytone68f5d62014-02-24 22:50:57 +00004797 }
Greg Clayton44d93782014-01-27 23:43:24 +00004798
4799 virtual void
4800 GotEOF()
4801 {
4802
4803 }
4804
4805protected:
4806 Process *m_process;
4807 File m_read_file; // Read from this file (usually actual STDIN for LLDB
4808 File m_write_file; // Write to this file (usually the master pty for getting io to debuggee)
Greg Clayton100eb932014-07-02 21:10:39 +00004809 Pipe m_pipe;
Greg Clayton44d93782014-01-27 23:43:24 +00004810};
4811
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004812void
Greg Clayton44d93782014-01-27 23:43:24 +00004813Process::SetSTDIOFileDescriptor (int fd)
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004814{
4815 // First set up the Read Thread for reading/handling process I/O
4816
Greg Clayton44d93782014-01-27 23:43:24 +00004817 std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (fd, true));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004818
4819 if (conn_ap.get())
4820 {
4821 m_stdio_communication.SetConnection (conn_ap.release());
4822 if (m_stdio_communication.IsConnected())
4823 {
4824 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this);
4825 m_stdio_communication.StartReadThread();
4826
4827 // Now read thread is set up, set up input reader.
4828
4829 if (!m_process_input_reader.get())
Greg Clayton44d93782014-01-27 23:43:24 +00004830 m_process_input_reader.reset (new IOHandlerProcessSTDIO (this, fd));
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004831 }
4832 }
4833}
4834
Greg Claytonb4874f12014-02-28 18:22:24 +00004835bool
Greg Clayton6fea17e2014-03-03 19:15:20 +00004836Process::ProcessIOHandlerIsActive ()
4837{
4838 IOHandlerSP io_handler_sp (m_process_input_reader);
4839 if (io_handler_sp)
4840 return m_target.GetDebugger().IsTopIOHandler (io_handler_sp);
4841 return false;
4842}
4843bool
Greg Clayton44d93782014-01-27 23:43:24 +00004844Process::PushProcessIOHandler ()
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004845{
Greg Clayton44d93782014-01-27 23:43:24 +00004846 IOHandlerSP io_handler_sp (m_process_input_reader);
4847 if (io_handler_sp)
4848 {
4849 io_handler_sp->SetIsDone(false);
4850 m_target.GetDebugger().PushIOHandler (io_handler_sp);
Greg Claytonb4874f12014-02-28 18:22:24 +00004851 return true;
Greg Clayton44d93782014-01-27 23:43:24 +00004852 }
Greg Claytonb4874f12014-02-28 18:22:24 +00004853 return false;
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004854}
4855
Greg Claytonb4874f12014-02-28 18:22:24 +00004856bool
Greg Clayton44d93782014-01-27 23:43:24 +00004857Process::PopProcessIOHandler ()
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004858{
Greg Clayton44d93782014-01-27 23:43:24 +00004859 IOHandlerSP io_handler_sp (m_process_input_reader);
4860 if (io_handler_sp)
Greg Claytonb4874f12014-02-28 18:22:24 +00004861 return m_target.GetDebugger().PopIOHandler (io_handler_sp);
4862 return false;
Caroline Ticeef5c6d02010-11-16 05:07:41 +00004863}
4864
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004865// The process needs to know about installed plug-ins
Greg Clayton99d0faf2010-11-18 23:32:35 +00004866void
Caroline Tice20bd37f2011-03-10 22:14:10 +00004867Process::SettingsInitialize ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004868{
Greg Clayton6920b522012-08-22 18:39:03 +00004869 Thread::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00004870}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004871
Greg Clayton99d0faf2010-11-18 23:32:35 +00004872void
Caroline Tice20bd37f2011-03-10 22:14:10 +00004873Process::SettingsTerminate ()
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00004874{
Greg Clayton6920b522012-08-22 18:39:03 +00004875 Thread::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00004876}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00004877
Jim Ingham1624a2d2014-05-05 02:26:40 +00004878ExpressionResults
Jim Inghamf48169b2010-11-30 02:22:11 +00004879Process::RunThreadPlan (ExecutionContext &exe_ctx,
Jim Ingham372787f2012-04-07 00:00:41 +00004880 lldb::ThreadPlanSP &thread_plan_sp,
Jim Ingham6fbc48b2013-11-07 00:11:47 +00004881 const EvaluateExpressionOptions &options,
Jim Inghamf48169b2010-11-30 02:22:11 +00004882 Stream &errors)
4883{
Jim Ingham8646d3c2014-05-05 02:47:44 +00004884 ExpressionResults return_value = eExpressionSetupError;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004885
Jim Ingham77787032011-01-20 02:03:18 +00004886 if (thread_plan_sp.get() == NULL)
4887 {
4888 errors.Printf("RunThreadPlan called with empty thread plan.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004889 return eExpressionSetupError;
Jim Ingham77787032011-01-20 02:03:18 +00004890 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004891
Jim Ingham7d7931d2013-03-28 00:05:34 +00004892 if (!thread_plan_sp->ValidatePlan(NULL))
4893 {
4894 errors.Printf ("RunThreadPlan called with an invalid thread plan.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004895 return eExpressionSetupError;
Jim Ingham7d7931d2013-03-28 00:05:34 +00004896 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004897
Greg Claytonc14ee322011-09-22 04:58:26 +00004898 if (exe_ctx.GetProcessPtr() != this)
4899 {
4900 errors.Printf("RunThreadPlan called on wrong process.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004901 return eExpressionSetupError;
Greg Claytonc14ee322011-09-22 04:58:26 +00004902 }
4903
4904 Thread *thread = exe_ctx.GetThreadPtr();
4905 if (thread == NULL)
4906 {
4907 errors.Printf("RunThreadPlan called with invalid thread.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004908 return eExpressionSetupError;
Greg Claytonc14ee322011-09-22 04:58:26 +00004909 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004910
Jim Ingham17e5c4e2011-05-17 22:24:54 +00004911 // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes.
4912 // For that to be true the plan can't be private - since private plans suppress themselves in the
4913 // GetCompletedPlan call.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004914
Jim Ingham17e5c4e2011-05-17 22:24:54 +00004915 bool orig_plan_private = thread_plan_sp->GetPrivate();
4916 thread_plan_sp->SetPrivate(false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004917
Jim Ingham444586b2011-01-24 06:34:17 +00004918 if (m_private_state.GetValue() != eStateStopped)
4919 {
4920 errors.Printf ("RunThreadPlan called while the private state was not stopped.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00004921 return eExpressionSetupError;
Jim Ingham444586b2011-01-24 06:34:17 +00004922 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004923
Jim Ingham66243842011-08-13 00:56:10 +00004924 // Save the thread & frame from the exe_ctx for restoration after we run
Greg Claytonc14ee322011-09-22 04:58:26 +00004925 const uint32_t thread_idx_id = thread->GetIndexID();
Jason Molendab57e4a12013-11-04 09:33:30 +00004926 StackFrameSP selected_frame_sp = thread->GetSelectedFrame();
Jim Ingham11b0e052013-02-19 23:22:45 +00004927 if (!selected_frame_sp)
4928 {
4929 thread->SetSelectedFrame(0);
4930 selected_frame_sp = thread->GetSelectedFrame();
4931 if (!selected_frame_sp)
4932 {
4933 errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id);
Jim Ingham8646d3c2014-05-05 02:47:44 +00004934 return eExpressionSetupError;
Jim Ingham11b0e052013-02-19 23:22:45 +00004935 }
4936 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004937
Jim Ingham11b0e052013-02-19 23:22:45 +00004938 StackID ctx_frame_id = selected_frame_sp->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00004939
4940 // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either,
4941 // so we should arrange to reset them as well.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004942
Greg Claytonc14ee322011-09-22 04:58:26 +00004943 lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004944
Jim Ingham66243842011-08-13 00:56:10 +00004945 uint32_t selected_tid;
4946 StackID selected_stack_id;
Greg Clayton762f7132011-09-18 18:59:15 +00004947 if (selected_thread_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00004948 {
4949 selected_tid = selected_thread_sp->GetIndexID();
Jim Ingham66243842011-08-13 00:56:10 +00004950 selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID();
Jim Inghamf48169b2010-11-30 02:22:11 +00004951 }
4952 else
4953 {
4954 selected_tid = LLDB_INVALID_THREAD_ID;
4955 }
4956
Jim Ingham372787f2012-04-07 00:00:41 +00004957 lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD;
Jim Ingham076b3042012-04-10 01:21:57 +00004958 lldb::StateType old_state;
4959 lldb::ThreadPlanSP stopper_base_plan_sp;
Jim Ingham372787f2012-04-07 00:00:41 +00004960
Greg Clayton5160ce52013-03-27 23:08:40 +00004961 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS));
Jim Ingham372787f2012-04-07 00:00:41 +00004962 if (Host::GetCurrentThread() == m_private_state_thread)
4963 {
Jim Ingham076b3042012-04-10 01:21:57 +00004964 // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since
4965 // we are the thread that is generating public events.
Jim Ingham372787f2012-04-07 00:00:41 +00004966 // 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 +00004967 // we are fielding public events here.
4968 if (log)
Jason Molendad251c9d2012-11-17 01:41:04 +00004969 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 +00004970
Jim Ingham372787f2012-04-07 00:00:41 +00004971 backup_private_state_thread = m_private_state_thread;
Jim Ingham076b3042012-04-10 01:21:57 +00004972
4973 // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop,
4974 // returning control here.
4975 // But in the normal course of things, the plan above us on the stack would be given a shot at the stop
4976 // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack
4977 // before the plan we want to run. Since base plans always stop and return control to the user, that will
4978 // do just what we want.
4979 stopper_base_plan_sp.reset(new ThreadPlanBase (*thread));
4980 thread->QueueThreadPlan (stopper_base_plan_sp, false);
4981 // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly.
4982 old_state = m_public_state.GetValue();
4983 m_public_state.SetValueNoLock(eStateStopped);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004984
Jim Ingham076b3042012-04-10 01:21:57 +00004985 // Now spin up the private state thread:
Jim Ingham372787f2012-04-07 00:00:41 +00004986 StartPrivateStateThread(true);
4987 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004988
Jim Ingham372787f2012-04-07 00:00:41 +00004989 thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense?
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004990
Jim Ingham6fbc48b2013-11-07 00:11:47 +00004991 if (options.GetDebug())
4992 {
4993 // In this case, we aren't actually going to run, we just want to stop right away.
4994 // Flush this thread so we will refetch the stacks and show the correct backtrace.
4995 // FIXME: To make this prettier we should invent some stop reason for this, but that
4996 // is only cosmetic, and this functionality is only of use to lldb developers who can
4997 // live with not pretty...
4998 thread->Flush();
Jim Ingham8646d3c2014-05-05 02:47:44 +00004999 return eExpressionStoppedForDebug;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005000 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005001
Jim Ingham1e7a9ee2011-01-23 21:14:08 +00005002 Listener listener("lldb.process.listener.run-thread-plan");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005003
Sean Callanana46ec452012-07-11 21:31:24 +00005004 lldb::EventSP event_to_broadcast_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005005
Jim Ingham77787032011-01-20 02:03:18 +00005006 {
Sean Callanana46ec452012-07-11 21:31:24 +00005007 // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get
5008 // restored on exit to the function.
5009 //
5010 // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event
5011 // is put into event_to_broadcast_sp for rebroadcasting.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005012
Sean Callanana46ec452012-07-11 21:31:24 +00005013 ProcessEventHijacker run_thread_plan_hijacker (*this, &listener);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005014
Jim Inghamf48169b2010-11-30 02:22:11 +00005015 if (log)
Jim Ingham0f16e732011-02-08 05:20:59 +00005016 {
5017 StreamString s;
Sean Callanana46ec452012-07-11 21:31:24 +00005018 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
Daniel Malead01b2952012-11-29 21:49:15 +00005019 log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 " to run thread plan \"%s\".",
Sean Callanana46ec452012-07-11 21:31:24 +00005020 thread->GetIndexID(),
5021 thread->GetID(),
5022 s.GetData());
5023 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005024
Sean Callanana46ec452012-07-11 21:31:24 +00005025 bool got_event;
5026 lldb::EventSP event_sp;
5027 lldb::StateType stop_state = lldb::eStateInvalid;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005028
Sean Callanana46ec452012-07-11 21:31:24 +00005029 TimeValue* timeout_ptr = NULL;
5030 TimeValue real_timeout;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005031
Jim Ingham0161b492013-02-09 01:29:05 +00005032 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 +00005033 bool do_resume = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005034 bool handle_running_event = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +00005035 const uint64_t default_one_thread_timeout_usec = 250000;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005036
Jim Ingham0161b492013-02-09 01:29:05 +00005037 // This is just for accounting:
5038 uint32_t num_resumes = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005039
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005040 uint32_t timeout_usec = options.GetTimeoutUsec();
Jim Inghamfd95f892014-04-22 01:41:52 +00005041 uint32_t one_thread_timeout_usec;
5042 uint32_t all_threads_timeout_usec = 0;
Jim Inghamfe1c3422014-04-16 02:24:48 +00005043
5044 // If we are going to run all threads the whole time, or if we are only going to run one thread,
5045 // then we don't need the first timeout. So we set the final timeout, and pretend we are after the
5046 // first timeout already.
5047
5048 if (!options.GetStopOthers() || !options.GetTryAllThreads())
Jim Ingham286fb1e2014-02-28 02:52:06 +00005049 {
5050 before_first_timeout = false;
Jim Inghamfd95f892014-04-22 01:41:52 +00005051 one_thread_timeout_usec = 0;
5052 all_threads_timeout_usec = timeout_usec;
Jim Ingham286fb1e2014-02-28 02:52:06 +00005053 }
Jim Inghamfe1c3422014-04-16 02:24:48 +00005054 else
Jim Ingham0161b492013-02-09 01:29:05 +00005055 {
Jim Inghamfd95f892014-04-22 01:41:52 +00005056 uint32_t option_one_thread_timeout = options.GetOneThreadTimeoutUsec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005057
Jim Ingham914f4e72014-03-28 21:58:28 +00005058 // If the overall wait is forever, then we only need to set the one thread timeout:
5059 if (timeout_usec == 0)
5060 {
Ed Maste801335c2014-03-31 19:28:14 +00005061 if (option_one_thread_timeout != 0)
Jim Inghamfd95f892014-04-22 01:41:52 +00005062 one_thread_timeout_usec = option_one_thread_timeout;
Jim Ingham914f4e72014-03-28 21:58:28 +00005063 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005064 one_thread_timeout_usec = default_one_thread_timeout_usec;
Jim Ingham914f4e72014-03-28 21:58:28 +00005065 }
Jim Ingham0161b492013-02-09 01:29:05 +00005066 else
5067 {
Jim Ingham914f4e72014-03-28 21:58:28 +00005068 // Otherwise, if the one thread timeout is set, make sure it isn't longer than the overall timeout,
5069 // and use it, otherwise use half the total timeout, bounded by the default_one_thread_timeout_usec.
5070 uint64_t computed_one_thread_timeout;
5071 if (option_one_thread_timeout != 0)
5072 {
5073 if (timeout_usec < option_one_thread_timeout)
5074 {
5075 errors.Printf("RunThreadPlan called without one thread timeout greater than total timeout");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005076 return eExpressionSetupError;
Jim Ingham914f4e72014-03-28 21:58:28 +00005077 }
5078 computed_one_thread_timeout = option_one_thread_timeout;
5079 }
5080 else
5081 {
5082 computed_one_thread_timeout = timeout_usec / 2;
5083 if (computed_one_thread_timeout > default_one_thread_timeout_usec)
5084 computed_one_thread_timeout = default_one_thread_timeout_usec;
5085 }
Jim Inghamfd95f892014-04-22 01:41:52 +00005086 one_thread_timeout_usec = computed_one_thread_timeout;
5087 all_threads_timeout_usec = timeout_usec - one_thread_timeout_usec;
5088
Jim Ingham0161b492013-02-09 01:29:05 +00005089 }
Jim Ingham0161b492013-02-09 01:29:05 +00005090 }
Jim Inghamfe1c3422014-04-16 02:24:48 +00005091
5092 if (log)
Jim Inghamfd95f892014-04-22 01:41:52 +00005093 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 +00005094 options.GetStopOthers(),
5095 options.GetTryAllThreads(),
Jim Inghamfd95f892014-04-22 01:41:52 +00005096 before_first_timeout,
5097 one_thread_timeout_usec,
5098 all_threads_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005099
Jim Ingham1460e4b2014-01-10 23:46:59 +00005100 // This isn't going to work if there are unfetched events on the queue.
5101 // Are there cases where we might want to run the remaining events here, and then try to
5102 // call the function? That's probably being too tricky for our own good.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005103
Jim Ingham1460e4b2014-01-10 23:46:59 +00005104 Event *other_events = listener.PeekAtNextEvent();
5105 if (other_events != NULL)
5106 {
5107 errors.Printf("Calling RunThreadPlan with pending events on the queue.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005108 return eExpressionSetupError;
Jim Ingham1460e4b2014-01-10 23:46:59 +00005109 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005110
Jim Ingham1460e4b2014-01-10 23:46:59 +00005111 // We also need to make sure that the next event is delivered. We might be calling a function as part of
5112 // a thread plan, in which case the last delivered event could be the running event, and we don't want
5113 // event coalescing to cause us to lose OUR running event...
5114 ForceNextEventDelivery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005115
Jim Ingham8559a352012-11-26 23:52:18 +00005116 // This while loop must exit out the bottom, there's cleanup that we need to do when we are done.
5117 // So don't call return anywhere within it.
Jim Ingham35878c42014-04-08 21:33:21 +00005118
5119#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
5120 // It's pretty much impossible to write test cases for things like:
5121 // One thread timeout expires, I go to halt, but the process already stopped
5122 // on the function call stop breakpoint. Turning on this define will make us not
5123 // fetch the first event till after the halt. So if you run a quick function, it will have
5124 // completed, and the completion event will be waiting, when you interrupt for halt.
5125 // The expression evaluation should still succeed.
5126 bool miss_first_event = true;
5127#endif
Jim Inghamfd95f892014-04-22 01:41:52 +00005128 TimeValue one_thread_timeout;
5129 TimeValue final_timeout;
5130
Jim Ingham35878c42014-04-08 21:33:21 +00005131
Sean Callanana46ec452012-07-11 21:31:24 +00005132 while (1)
5133 {
5134 // We usually want to resume the process if we get to the top of the loop.
5135 // The only exception is if we get two running events with no intervening
5136 // stop, which can happen, we will just wait for then next stop event.
Jim Ingham0161b492013-02-09 01:29:05 +00005137 if (log)
5138 log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.",
5139 do_resume,
5140 handle_running_event,
5141 before_first_timeout);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005142
Jim Ingham184e9812013-01-15 02:47:48 +00005143 if (do_resume || handle_running_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005144 {
5145 // Do the initial resume and wait for the running event before going further.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005146
Jim Ingham184e9812013-01-15 02:47:48 +00005147 if (do_resume)
Sean Callanana46ec452012-07-11 21:31:24 +00005148 {
Jim Ingham0161b492013-02-09 01:29:05 +00005149 num_resumes++;
Jim Ingham184e9812013-01-15 02:47:48 +00005150 Error resume_error = PrivateResume ();
5151 if (!resume_error.Success())
5152 {
Jim Ingham0161b492013-02-09 01:29:05 +00005153 errors.Printf("Error resuming inferior the %d time: \"%s\".\n",
5154 num_resumes,
5155 resume_error.AsCString());
Jim Ingham8646d3c2014-05-05 02:47:44 +00005156 return_value = eExpressionSetupError;
Jim Ingham184e9812013-01-15 02:47:48 +00005157 break;
5158 }
Sean Callanana46ec452012-07-11 21:31:24 +00005159 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005160
Jim Ingham0161b492013-02-09 01:29:05 +00005161 TimeValue resume_timeout = TimeValue::Now();
5162 resume_timeout.OffsetWithMicroSeconds(500000);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005163
Jim Ingham0161b492013-02-09 01:29:05 +00005164 got_event = listener.WaitForEvent(&resume_timeout, event_sp);
Sean Callanana46ec452012-07-11 21:31:24 +00005165 if (!got_event)
5166 {
5167 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005168 log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.",
5169 num_resumes);
Sean Callanana46ec452012-07-11 21:31:24 +00005170
Jim Ingham0161b492013-02-09 01:29:05 +00005171 errors.Printf("Didn't get any event after resume %d, exiting.", num_resumes);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005172 return_value = eExpressionSetupError;
Sean Callanana46ec452012-07-11 21:31:24 +00005173 break;
5174 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005175
Sean Callanana46ec452012-07-11 21:31:24 +00005176 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
Jim Ingham0161b492013-02-09 01:29:05 +00005177
Sean Callanana46ec452012-07-11 21:31:24 +00005178 if (stop_state != eStateRunning)
5179 {
Jim Ingham0161b492013-02-09 01:29:05 +00005180 bool restarted = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005181
Jim Ingham0161b492013-02-09 01:29:05 +00005182 if (stop_state == eStateStopped)
5183 {
5184 restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get());
5185 if (log)
5186 log->Printf("Process::RunThreadPlan(): didn't get running event after "
5187 "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).",
5188 num_resumes,
5189 StateAsCString(stop_state),
5190 restarted,
5191 do_resume,
5192 handle_running_event);
5193 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005194
Jim Ingham0161b492013-02-09 01:29:05 +00005195 if (restarted)
5196 {
5197 // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted
5198 // event here. But if I do, the best thing is to Halt and then get out of here.
5199 Halt();
5200 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005201
Jim Ingham35e1bda2012-10-16 21:41:58 +00005202 errors.Printf("Didn't get running event after initial resume, got %s instead.",
5203 StateAsCString(stop_state));
Jim Ingham8646d3c2014-05-05 02:47:44 +00005204 return_value = eExpressionSetupError;
Sean Callanana46ec452012-07-11 21:31:24 +00005205 break;
5206 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005207
Sean Callanana46ec452012-07-11 21:31:24 +00005208 if (log)
5209 log->PutCString ("Process::RunThreadPlan(): resuming succeeded.");
5210 // We need to call the function synchronously, so spin waiting for it to return.
5211 // If we get interrupted while executing, we're going to lose our context, and
5212 // won't be able to gather the result at this point.
5213 // We set the timeout AFTER the resume, since the resume takes some time and we
5214 // don't want to charge that to the timeout.
Sean Callanana46ec452012-07-11 21:31:24 +00005215 }
Jim Ingham0f16e732011-02-08 05:20:59 +00005216 else
5217 {
Sean Callanana46ec452012-07-11 21:31:24 +00005218 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005219 log->PutCString ("Process::RunThreadPlan(): waiting for next event.");
Jim Ingham0f16e732011-02-08 05:20:59 +00005220 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005221
Jim Ingham0161b492013-02-09 01:29:05 +00005222 if (before_first_timeout)
5223 {
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005224 if (options.GetTryAllThreads())
Jim Inghamfd95f892014-04-22 01:41:52 +00005225 {
5226 one_thread_timeout = TimeValue::Now();
5227 one_thread_timeout.OffsetWithMicroSeconds(one_thread_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005228 timeout_ptr = &one_thread_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005229 }
Jim Ingham0161b492013-02-09 01:29:05 +00005230 else
5231 {
5232 if (timeout_usec == 0)
5233 timeout_ptr = NULL;
5234 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005235 {
5236 final_timeout = TimeValue::Now();
5237 final_timeout.OffsetWithMicroSeconds (timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005238 timeout_ptr = &final_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005239 }
Jim Ingham0161b492013-02-09 01:29:05 +00005240 }
5241 }
5242 else
5243 {
5244 if (timeout_usec == 0)
5245 timeout_ptr = NULL;
5246 else
Jim Inghamfd95f892014-04-22 01:41:52 +00005247 {
5248 final_timeout = TimeValue::Now();
5249 final_timeout.OffsetWithMicroSeconds (all_threads_timeout_usec);
Jim Ingham0161b492013-02-09 01:29:05 +00005250 timeout_ptr = &final_timeout;
Jim Inghamfd95f892014-04-22 01:41:52 +00005251 }
Jim Ingham0161b492013-02-09 01:29:05 +00005252 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005253
Jim Ingham0161b492013-02-09 01:29:05 +00005254 do_resume = true;
5255 handle_running_event = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005256
Sean Callanana46ec452012-07-11 21:31:24 +00005257 // Now wait for the process to stop again:
Sean Callanana46ec452012-07-11 21:31:24 +00005258 event_sp.reset();
Jim Ingham0f16e732011-02-08 05:20:59 +00005259
Jim Ingham0f16e732011-02-08 05:20:59 +00005260 if (log)
Jim Ingham20829ac2011-08-09 22:24:33 +00005261 {
Sean Callanana46ec452012-07-11 21:31:24 +00005262 if (timeout_ptr)
5263 {
Matt Kopec676a4872013-02-21 23:55:31 +00005264 log->Printf ("Process::RunThreadPlan(): about to wait - now is %" PRIu64 " - endpoint is %" PRIu64,
Jim Ingham0161b492013-02-09 01:29:05 +00005265 TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(),
5266 timeout_ptr->GetAsMicroSecondsSinceJan1_1970());
Sean Callanana46ec452012-07-11 21:31:24 +00005267 }
Jim Ingham20829ac2011-08-09 22:24:33 +00005268 else
Sean Callanana46ec452012-07-11 21:31:24 +00005269 {
5270 log->Printf ("Process::RunThreadPlan(): about to wait forever.");
5271 }
5272 }
Jim Ingham35878c42014-04-08 21:33:21 +00005273
5274#ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT
5275 // See comment above...
5276 if (miss_first_event)
5277 {
5278 usleep(1000);
5279 miss_first_event = false;
5280 got_event = false;
5281 }
5282 else
5283#endif
Sean Callanana46ec452012-07-11 21:31:24 +00005284 got_event = listener.WaitForEvent (timeout_ptr, event_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005285
Sean Callanana46ec452012-07-11 21:31:24 +00005286 if (got_event)
5287 {
5288 if (event_sp.get())
5289 {
5290 bool keep_going = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005291 if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005292 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005293 Halt();
Jim Ingham8646d3c2014-05-05 02:47:44 +00005294 return_value = eExpressionInterrupted;
Jim Inghamcfc09352012-07-27 23:57:19 +00005295 errors.Printf ("Execution halted by user interrupt.");
5296 if (log)
5297 log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting.");
Jim Ingham0161b492013-02-09 01:29:05 +00005298 break;
Jim Inghamcfc09352012-07-27 23:57:19 +00005299 }
5300 else
5301 {
5302 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5303 if (log)
5304 log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005305
Jim Inghamcfc09352012-07-27 23:57:19 +00005306 switch (stop_state)
Sean Callanana46ec452012-07-11 21:31:24 +00005307 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005308 case lldb::eStateStopped:
Sean Callanana46ec452012-07-11 21:31:24 +00005309 {
Jim Ingham0161b492013-02-09 01:29:05 +00005310 // We stopped, figure out what we are going to do now.
Jim Inghamcfc09352012-07-27 23:57:19 +00005311 ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id);
5312 if (!thread_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005313 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005314 // Ooh, our thread has vanished. Unlikely that this was successful execution...
Sean Callanana46ec452012-07-11 21:31:24 +00005315 if (log)
Jim Inghamcfc09352012-07-27 23:57:19 +00005316 log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005317 return_value = eExpressionInterrupted;
Sean Callanana46ec452012-07-11 21:31:24 +00005318 }
5319 else
5320 {
Jim Ingham0161b492013-02-09 01:29:05 +00005321 // If we were restarted, we just need to go back up to fetch another event.
5322 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
Jim Inghamcfc09352012-07-27 23:57:19 +00005323 {
5324 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005325 {
5326 log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting.");
5327 }
5328 keep_going = true;
5329 do_resume = false;
5330 handle_running_event = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005331
Jim Inghamcfc09352012-07-27 23:57:19 +00005332 }
5333 else
5334 {
Jim Ingham0161b492013-02-09 01:29:05 +00005335 StopInfoSP stop_info_sp (thread_sp->GetStopInfo ());
5336 StopReason stop_reason = eStopReasonInvalid;
5337 if (stop_info_sp)
5338 stop_reason = stop_info_sp->GetStopReason();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005339
Jim Ingham0161b492013-02-09 01:29:05 +00005340 // FIXME: We only check if the stop reason is plan complete, should we make sure that
5341 // it is OUR plan that is complete?
5342 if (stop_reason == eStopReasonPlanComplete)
Jim Ingham184e9812013-01-15 02:47:48 +00005343 {
5344 if (log)
Jim Ingham0161b492013-02-09 01:29:05 +00005345 log->PutCString ("Process::RunThreadPlan(): execution completed successfully.");
5346 // Now mark this plan as private so it doesn't get reported as the stop reason
5347 // after this point.
5348 if (thread_plan_sp)
5349 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham8646d3c2014-05-05 02:47:44 +00005350 return_value = eExpressionCompleted;
Jim Ingham184e9812013-01-15 02:47:48 +00005351 }
5352 else
5353 {
Jim Ingham0161b492013-02-09 01:29:05 +00005354 // Something restarted the target, so just wait for it to stop for real.
Jim Ingham184e9812013-01-15 02:47:48 +00005355 if (stop_reason == eStopReasonBreakpoint)
Jim Ingham0161b492013-02-09 01:29:05 +00005356 {
5357 if (log)
5358 log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription());
Jim Ingham8646d3c2014-05-05 02:47:44 +00005359 return_value = eExpressionHitBreakpoint;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005360 if (!options.DoesIgnoreBreakpoints())
Sean Callanan4b388c92013-07-30 19:54:09 +00005361 {
5362 event_to_broadcast_sp = event_sp;
5363 }
Jim Ingham0161b492013-02-09 01:29:05 +00005364 }
Jim Ingham184e9812013-01-15 02:47:48 +00005365 else
Jim Ingham0161b492013-02-09 01:29:05 +00005366 {
5367 if (log)
5368 log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete.");
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005369 if (!options.DoesUnwindOnError())
Sean Callanan4b388c92013-07-30 19:54:09 +00005370 event_to_broadcast_sp = event_sp;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005371 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005372 }
Jim Ingham184e9812013-01-15 02:47:48 +00005373 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005374 }
Sean Callanana46ec452012-07-11 21:31:24 +00005375 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005376 }
5377 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005378
Jim Inghamcfc09352012-07-27 23:57:19 +00005379 case lldb::eStateRunning:
Jim Ingham0161b492013-02-09 01:29:05 +00005380 // This shouldn't really happen, but sometimes we do get two running events without an
5381 // intervening stop, and in that case we should just go back to waiting for the stop.
Jim Inghamcfc09352012-07-27 23:57:19 +00005382 do_resume = false;
5383 keep_going = true;
Jim Ingham184e9812013-01-15 02:47:48 +00005384 handle_running_event = false;
Jim Inghamcfc09352012-07-27 23:57:19 +00005385 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005386
Jim Inghamcfc09352012-07-27 23:57:19 +00005387 default:
5388 if (log)
5389 log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005390
Jim Inghamcfc09352012-07-27 23:57:19 +00005391 if (stop_state == eStateExited)
5392 event_to_broadcast_sp = event_sp;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005393
Sean Callananbf154da2012-08-08 17:35:10 +00005394 errors.Printf ("Execution stopped with unexpected state.\n");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005395 return_value = eExpressionInterrupted;
Jim Inghamcfc09352012-07-27 23:57:19 +00005396 break;
5397 }
Sean Callanana46ec452012-07-11 21:31:24 +00005398 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005399
Sean Callanana46ec452012-07-11 21:31:24 +00005400 if (keep_going)
5401 continue;
5402 else
5403 break;
5404 }
5405 else
5406 {
5407 if (log)
5408 log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd...");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005409 return_value = eExpressionInterrupted;
Sean Callanana46ec452012-07-11 21:31:24 +00005410 break;
5411 }
5412 }
5413 else
5414 {
5415 // If we didn't get an event that means we've timed out...
5416 // We will interrupt the process here. Depending on what we were asked to do we will
5417 // either exit, or try with all threads running for the same timeout.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005418
Sean Callanana46ec452012-07-11 21:31:24 +00005419 if (log) {
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005420 if (options.GetTryAllThreads())
Sean Callanana46ec452012-07-11 21:31:24 +00005421 {
Jim Ingham0161b492013-02-09 01:29:05 +00005422 if (before_first_timeout)
Jim Inghamfe1c3422014-04-16 02:24:48 +00005423 {
5424 if (timeout_usec != 0)
5425 {
Jim Inghamfe1c3422014-04-16 02:24:48 +00005426 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
Jim Inghamfd95f892014-04-22 01:41:52 +00005427 "running for %" PRIu32 " usec with all threads enabled.",
5428 all_threads_timeout_usec);
Jim Inghamfe1c3422014-04-16 02:24:48 +00005429 }
5430 else
5431 {
5432 log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, "
Ed Mastee61c7b02014-04-29 17:48:06 +00005433 "running forever with all threads enabled.");
Jim Inghamfe1c3422014-04-16 02:24:48 +00005434 }
5435 }
Sean Callanana46ec452012-07-11 21:31:24 +00005436 else
5437 log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled "
Jason Molenda6a8658a2013-10-27 02:32:23 +00005438 "and timeout: %u timed out, abandoning execution.",
Jim Ingham35e1bda2012-10-16 21:41:58 +00005439 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005440 }
5441 else
Jason Molenda6a8658a2013-10-27 02:32:23 +00005442 log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, "
Jim Ingham35e1bda2012-10-16 21:41:58 +00005443 "abandoning execution.",
5444 timeout_usec);
Sean Callanana46ec452012-07-11 21:31:24 +00005445 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005446
Jim Ingham0161b492013-02-09 01:29:05 +00005447 // It is possible that between the time we issued the Halt, and we get around to calling Halt the target
5448 // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event.
5449 // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In
5450 // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's
5451 // stopped event. That's what this while loop does.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005452
Jim Ingham0161b492013-02-09 01:29:05 +00005453 bool back_to_top = true;
5454 uint32_t try_halt_again = 0;
5455 bool do_halt = true;
5456 const uint32_t num_retries = 5;
5457 while (try_halt_again < num_retries)
Sean Callanana46ec452012-07-11 21:31:24 +00005458 {
Jim Ingham0161b492013-02-09 01:29:05 +00005459 Error halt_error;
5460 if (do_halt)
5461 {
5462 if (log)
5463 log->Printf ("Process::RunThreadPlan(): Running Halt.");
5464 halt_error = Halt();
5465 }
5466 if (halt_error.Success())
5467 {
5468 if (log)
5469 log->PutCString ("Process::RunThreadPlan(): Halt succeeded.");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005470
Jim Ingham0161b492013-02-09 01:29:05 +00005471 real_timeout = TimeValue::Now();
5472 real_timeout.OffsetWithMicroSeconds(500000);
5473
5474 got_event = listener.WaitForEvent(&real_timeout, event_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005475
Jim Ingham0161b492013-02-09 01:29:05 +00005476 if (got_event)
Sean Callanana46ec452012-07-11 21:31:24 +00005477 {
Jim Ingham0161b492013-02-09 01:29:05 +00005478 stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get());
5479 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005480 {
Jim Ingham0161b492013-02-09 01:29:05 +00005481 log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state));
5482 if (stop_state == lldb::eStateStopped
5483 && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get()))
5484 log->PutCString (" Event was the Halt interruption event.");
Sean Callanana46ec452012-07-11 21:31:24 +00005485 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005486
Jim Ingham0161b492013-02-09 01:29:05 +00005487 if (stop_state == lldb::eStateStopped)
Sean Callanana46ec452012-07-11 21:31:24 +00005488 {
Jim Ingham0161b492013-02-09 01:29:05 +00005489 // Between the time we initiated the Halt and the time we delivered it, the process could have
5490 // already finished its job. Check that here:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005491
Jim Ingham0161b492013-02-09 01:29:05 +00005492 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
5493 {
5494 if (log)
5495 log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. "
5496 "Exiting wait loop.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005497 return_value = eExpressionCompleted;
Jim Ingham0161b492013-02-09 01:29:05 +00005498 back_to_top = false;
5499 break;
5500 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005501
Jim Ingham0161b492013-02-09 01:29:05 +00005502 if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()))
5503 {
5504 if (log)
5505 log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... "
5506 "Exiting wait loop.");
5507 try_halt_again++;
5508 do_halt = false;
5509 continue;
5510 }
Sean Callanana46ec452012-07-11 21:31:24 +00005511
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005512 if (!options.GetTryAllThreads())
Jim Ingham0161b492013-02-09 01:29:05 +00005513 {
5514 if (log)
5515 log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005516 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005517 back_to_top = false;
5518 break;
5519 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005520
Jim Ingham0161b492013-02-09 01:29:05 +00005521 if (before_first_timeout)
5522 {
5523 // Set all the other threads to run, and return to the top of the loop, which will continue;
5524 before_first_timeout = false;
5525 thread_plan_sp->SetStopOthers (false);
5526 if (log)
5527 log->PutCString ("Process::RunThreadPlan(): about to resume.");
Sean Callanana46ec452012-07-11 21:31:24 +00005528
Jim Ingham0161b492013-02-09 01:29:05 +00005529 back_to_top = true;
5530 break;
5531 }
5532 else
5533 {
5534 // Running all threads failed, so return Interrupted.
5535 if (log)
5536 log->PutCString("Process::RunThreadPlan(): running all threads timed out.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005537 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005538 back_to_top = false;
5539 break;
5540 }
Sean Callanana46ec452012-07-11 21:31:24 +00005541 }
5542 }
5543 else
Jim Ingham0161b492013-02-09 01:29:05 +00005544 { if (log)
5545 log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. "
5546 "I'm getting out of here passing Interrupted.");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005547 return_value = eExpressionInterrupted;
Jim Ingham0161b492013-02-09 01:29:05 +00005548 back_to_top = false;
5549 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005550 }
5551 }
Jim Ingham0161b492013-02-09 01:29:05 +00005552 else
5553 {
5554 try_halt_again++;
5555 continue;
5556 }
Sean Callanana46ec452012-07-11 21:31:24 +00005557 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005558
Jim Ingham0161b492013-02-09 01:29:05 +00005559 if (!back_to_top || try_halt_again > num_retries)
5560 break;
5561 else
5562 continue;
Sean Callanana46ec452012-07-11 21:31:24 +00005563 }
Sean Callanana46ec452012-07-11 21:31:24 +00005564 } // END WAIT LOOP
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005565
Sean Callanana46ec452012-07-11 21:31:24 +00005566 // If we had to start up a temporary private state thread to run this thread plan, shut it down now.
5567 if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread))
5568 {
5569 StopPrivateStateThread();
5570 Error error;
5571 m_private_state_thread = backup_private_state_thread;
Sean Callanan9a028512012-08-09 00:50:26 +00005572 if (stopper_base_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005573 {
5574 thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp);
5575 }
5576 m_public_state.SetValueNoLock(old_state);
5577
5578 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005579
Jim Ingham184e9812013-01-15 02:47:48 +00005580 // Restore the thread state if we are going to discard the plan execution. There are three cases where this
5581 // could happen:
5582 // 1) The execution successfully completed
5583 // 2) We hit a breakpoint, and ignore_breakpoints was true
5584 // 3) We got some other error, and discard_on_error was true
Jim Ingham8646d3c2014-05-05 02:47:44 +00005585 bool should_unwind = (return_value == eExpressionInterrupted && options.DoesUnwindOnError())
5586 || (return_value == eExpressionHitBreakpoint && options.DoesIgnoreBreakpoints());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005587
Jim Ingham8646d3c2014-05-05 02:47:44 +00005588 if (return_value == eExpressionCompleted
Jim Ingham184e9812013-01-15 02:47:48 +00005589 || should_unwind)
Jim Ingham8559a352012-11-26 23:52:18 +00005590 {
5591 thread_plan_sp->RestoreThreadState();
5592 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005593
Sean Callanana46ec452012-07-11 21:31:24 +00005594 // Now do some processing on the results of the run:
Jim Ingham8646d3c2014-05-05 02:47:44 +00005595 if (return_value == eExpressionInterrupted || return_value == eExpressionHitBreakpoint)
Sean Callanana46ec452012-07-11 21:31:24 +00005596 {
5597 if (log)
5598 {
5599 StreamString s;
5600 if (event_sp)
5601 event_sp->Dump (&s);
5602 else
5603 {
5604 log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL.");
5605 }
5606
5607 StreamString ts;
5608
5609 const char *event_explanation = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005610
Sean Callanana46ec452012-07-11 21:31:24 +00005611 do
5612 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005613 if (!event_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005614 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005615 event_explanation = "<no event>";
Sean Callanana46ec452012-07-11 21:31:24 +00005616 break;
5617 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005618 else if (event_sp->GetType() == eBroadcastBitInterrupt)
Sean Callanana46ec452012-07-11 21:31:24 +00005619 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005620 event_explanation = "<user interrupt>";
Sean Callanana46ec452012-07-11 21:31:24 +00005621 break;
5622 }
Jim Inghamcfc09352012-07-27 23:57:19 +00005623 else
Sean Callanana46ec452012-07-11 21:31:24 +00005624 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005625 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
5626
5627 if (!event_data)
Sean Callanana46ec452012-07-11 21:31:24 +00005628 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005629 event_explanation = "<no event data>";
5630 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005631 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005632
Jim Inghamcfc09352012-07-27 23:57:19 +00005633 Process *process = event_data->GetProcessSP().get();
5634
5635 if (!process)
Sean Callanana46ec452012-07-11 21:31:24 +00005636 {
Jim Inghamcfc09352012-07-27 23:57:19 +00005637 event_explanation = "<no process>";
5638 break;
Sean Callanana46ec452012-07-11 21:31:24 +00005639 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005640
Jim Inghamcfc09352012-07-27 23:57:19 +00005641 ThreadList &thread_list = process->GetThreadList();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005642
Jim Inghamcfc09352012-07-27 23:57:19 +00005643 uint32_t num_threads = thread_list.GetSize();
5644 uint32_t thread_index;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005645
Jim Inghamcfc09352012-07-27 23:57:19 +00005646 ts.Printf("<%u threads> ", num_threads);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005647
Jim Inghamcfc09352012-07-27 23:57:19 +00005648 for (thread_index = 0;
5649 thread_index < num_threads;
5650 ++thread_index)
5651 {
5652 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005653
Jim Inghamcfc09352012-07-27 23:57:19 +00005654 if (!thread)
5655 {
5656 ts.Printf("<?> ");
5657 continue;
5658 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005659
Daniel Malead01b2952012-11-29 21:49:15 +00005660 ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID());
Jim Inghamcfc09352012-07-27 23:57:19 +00005661 RegisterContext *register_context = thread->GetRegisterContext().get();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005662
Jim Inghamcfc09352012-07-27 23:57:19 +00005663 if (register_context)
Daniel Malead01b2952012-11-29 21:49:15 +00005664 ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC());
Jim Inghamcfc09352012-07-27 23:57:19 +00005665 else
5666 ts.Printf("[ip unknown] ");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005667
Jim Inghamcfc09352012-07-27 23:57:19 +00005668 lldb::StopInfoSP stop_info_sp = thread->GetStopInfo();
5669 if (stop_info_sp)
5670 {
5671 const char *stop_desc = stop_info_sp->GetDescription();
5672 if (stop_desc)
5673 ts.PutCString (stop_desc);
5674 }
5675 ts.Printf(">");
5676 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005677
Jim Inghamcfc09352012-07-27 23:57:19 +00005678 event_explanation = ts.GetData();
Sean Callanana46ec452012-07-11 21:31:24 +00005679 }
Sean Callanana46ec452012-07-11 21:31:24 +00005680 } while (0);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005681
Jim Inghamcfc09352012-07-27 23:57:19 +00005682 if (event_explanation)
5683 log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation);
Sean Callanana46ec452012-07-11 21:31:24 +00005684 else
Jim Inghamcfc09352012-07-27 23:57:19 +00005685 log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData());
5686 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005687
Jim Inghame4483cf2013-09-27 01:13:01 +00005688 if (should_unwind)
Jim Inghamcfc09352012-07-27 23:57:19 +00005689 {
5690 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005691 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.",
5692 static_cast<void*>(thread_plan_sp.get()));
Jim Inghamcfc09352012-07-27 23:57:19 +00005693 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5694 thread_plan_sp->SetPrivate (orig_plan_private);
5695 }
5696 else
5697 {
5698 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005699 log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.",
5700 static_cast<void*>(thread_plan_sp.get()));
Sean Callanana46ec452012-07-11 21:31:24 +00005701 }
5702 }
Jim Ingham8646d3c2014-05-05 02:47:44 +00005703 else if (return_value == eExpressionSetupError)
Sean Callanana46ec452012-07-11 21:31:24 +00005704 {
5705 if (log)
5706 log->PutCString("Process::RunThreadPlan(): execution set up error.");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005707
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005708 if (options.DoesUnwindOnError())
Jim Ingham0f16e732011-02-08 05:20:59 +00005709 {
Greg Claytonc14ee322011-09-22 04:58:26 +00005710 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00005711 thread_plan_sp->SetPrivate (orig_plan_private);
Jim Ingham0f16e732011-02-08 05:20:59 +00005712 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005713 }
5714 else
5715 {
Sean Callanana46ec452012-07-11 21:31:24 +00005716 if (thread->IsThreadPlanDone (thread_plan_sp.get()))
Jim Inghamf48169b2010-11-30 02:22:11 +00005717 {
Jim Ingham0f16e732011-02-08 05:20:59 +00005718 if (log)
Sean Callanana46ec452012-07-11 21:31:24 +00005719 log->PutCString("Process::RunThreadPlan(): thread plan is done");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005720 return_value = eExpressionCompleted;
Sean Callanana46ec452012-07-11 21:31:24 +00005721 }
5722 else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get()))
5723 {
5724 if (log)
5725 log->PutCString("Process::RunThreadPlan(): thread plan was discarded");
Jim Ingham8646d3c2014-05-05 02:47:44 +00005726 return_value = eExpressionDiscarded;
Sean Callanana46ec452012-07-11 21:31:24 +00005727 }
5728 else
5729 {
5730 if (log)
5731 log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course");
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005732 if (options.DoesUnwindOnError() && thread_plan_sp)
Sean Callanana46ec452012-07-11 21:31:24 +00005733 {
5734 if (log)
Jim Ingham184e9812013-01-15 02:47:48 +00005735 log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause unwind_on_error is set.");
Sean Callanana46ec452012-07-11 21:31:24 +00005736 thread->DiscardThreadPlansUpToPlan (thread_plan_sp);
5737 thread_plan_sp->SetPrivate (orig_plan_private);
5738 }
5739 }
5740 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005741
Sean Callanana46ec452012-07-11 21:31:24 +00005742 // Thread we ran the function in may have gone away because we ran the target
5743 // Check that it's still there, and if it is put it back in the context. Also restore the
5744 // frame in the context if it is still present.
5745 thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get();
5746 if (thread)
5747 {
5748 exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id));
5749 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005750
Sean Callanana46ec452012-07-11 21:31:24 +00005751 // Also restore the current process'es selected frame & thread, since this function calling may
5752 // be done behind the user's back.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005753
Sean Callanana46ec452012-07-11 21:31:24 +00005754 if (selected_tid != LLDB_INVALID_THREAD_ID)
5755 {
5756 if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid())
5757 {
5758 // We were able to restore the selected thread, now restore the frame:
Daniel Maleaa012d3a2013-07-31 20:21:20 +00005759 Mutex::Locker lock(GetThreadList().GetMutex());
Jason Molendab57e4a12013-11-04 09:33:30 +00005760 StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id);
Sean Callanana46ec452012-07-11 21:31:24 +00005761 if (old_frame_sp)
5762 GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get());
Jim Inghamf48169b2010-11-30 02:22:11 +00005763 }
Jim Inghamf48169b2010-11-30 02:22:11 +00005764 }
5765 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005766
Sean Callanana46ec452012-07-11 21:31:24 +00005767 // If the process exited during the run of the thread plan, notify everyone.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005768
Sean Callanana46ec452012-07-11 21:31:24 +00005769 if (event_to_broadcast_sp)
Jim Inghamf48169b2010-11-30 02:22:11 +00005770 {
Sean Callanana46ec452012-07-11 21:31:24 +00005771 if (log)
5772 log->PutCString("Process::RunThreadPlan(): rebroadcasting event.");
5773 BroadcastEvent(event_to_broadcast_sp);
Jim Inghamf48169b2010-11-30 02:22:11 +00005774 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005775
Jim Inghamf48169b2010-11-30 02:22:11 +00005776 return return_value;
5777}
5778
5779const char *
Jim Ingham1624a2d2014-05-05 02:26:40 +00005780Process::ExecutionResultAsCString (ExpressionResults result)
Jim Inghamf48169b2010-11-30 02:22:11 +00005781{
5782 const char *result_name;
5783
5784 switch (result)
5785 {
Jim Ingham8646d3c2014-05-05 02:47:44 +00005786 case eExpressionCompleted:
5787 result_name = "eExpressionCompleted";
Jim Inghamf48169b2010-11-30 02:22:11 +00005788 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005789 case eExpressionDiscarded:
5790 result_name = "eExpressionDiscarded";
Jim Inghamf48169b2010-11-30 02:22:11 +00005791 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005792 case eExpressionInterrupted:
5793 result_name = "eExpressionInterrupted";
Jim Inghamf48169b2010-11-30 02:22:11 +00005794 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005795 case eExpressionHitBreakpoint:
5796 result_name = "eExpressionHitBreakpoint";
Jim Ingham184e9812013-01-15 02:47:48 +00005797 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005798 case eExpressionSetupError:
5799 result_name = "eExpressionSetupError";
Jim Inghamf48169b2010-11-30 02:22:11 +00005800 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005801 case eExpressionParseError:
5802 result_name = "eExpressionParseError";
Jim Ingham1624a2d2014-05-05 02:26:40 +00005803 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005804 case eExpressionResultUnavailable:
5805 result_name = "eExpressionResultUnavailable";
Jim Ingham1624a2d2014-05-05 02:26:40 +00005806 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005807 case eExpressionTimedOut:
5808 result_name = "eExpressionTimedOut";
Jim Inghamf48169b2010-11-30 02:22:11 +00005809 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00005810 case eExpressionStoppedForDebug:
5811 result_name = "eExpressionStoppedForDebug";
Jim Ingham6fbc48b2013-11-07 00:11:47 +00005812 break;
Jim Inghamf48169b2010-11-30 02:22:11 +00005813 }
5814 return result_name;
5815}
5816
Greg Clayton7260f622011-04-18 08:33:37 +00005817void
5818Process::GetStatus (Stream &strm)
5819{
5820 const StateType state = GetState();
Greg Clayton2637f822011-11-17 01:23:07 +00005821 if (StateIsStoppedState(state, false))
Greg Clayton7260f622011-04-18 08:33:37 +00005822 {
5823 if (state == eStateExited)
5824 {
5825 int exit_status = GetExitStatus();
5826 const char *exit_description = GetExitDescription();
Daniel Malead01b2952012-11-29 21:49:15 +00005827 strm.Printf ("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n",
Greg Clayton7260f622011-04-18 08:33:37 +00005828 GetID(),
5829 exit_status,
5830 exit_status,
5831 exit_description ? exit_description : "");
5832 }
5833 else
5834 {
5835 if (state == eStateConnected)
5836 strm.Printf ("Connected to remote target.\n");
5837 else
Daniel Malead01b2952012-11-29 21:49:15 +00005838 strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state));
Greg Clayton7260f622011-04-18 08:33:37 +00005839 }
5840 }
5841 else
5842 {
Daniel Malead01b2952012-11-29 21:49:15 +00005843 strm.Printf ("Process %" PRIu64 " is running.\n", GetID());
Greg Clayton7260f622011-04-18 08:33:37 +00005844 }
5845}
5846
5847size_t
5848Process::GetThreadStatus (Stream &strm,
5849 bool only_threads_with_stop_reason,
5850 uint32_t start_frame,
5851 uint32_t num_frames,
5852 uint32_t num_frames_with_source)
5853{
5854 size_t num_thread_infos_dumped = 0;
5855
Jim Ingham4a65fb12014-03-07 11:20:03 +00005856 // You can't hold the thread list lock while calling Thread::GetStatus. That very well might run code (e.g. if we need it
5857 // to get return values or arguments.) For that to work the process has to be able to acquire it. So instead copy the thread
5858 // ID's, and look them up one by one:
5859
5860 uint32_t num_threads;
5861 std::vector<uint32_t> thread_index_array;
5862 //Scope for thread list locker;
5863 {
5864 Mutex::Locker locker (GetThreadList().GetMutex());
5865 ThreadList &curr_thread_list = GetThreadList();
5866 num_threads = curr_thread_list.GetSize();
5867 uint32_t idx;
5868 thread_index_array.resize(num_threads);
5869 for (idx = 0; idx < num_threads; ++idx)
5870 thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID();
5871 }
5872
Greg Clayton7260f622011-04-18 08:33:37 +00005873 for (uint32_t i = 0; i < num_threads; i++)
5874 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00005875 ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_index_array[i]));
5876 if (thread_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00005877 {
5878 if (only_threads_with_stop_reason)
5879 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00005880 StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
Jim Ingham5d88a062012-10-16 00:09:33 +00005881 if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid())
Greg Clayton7260f622011-04-18 08:33:37 +00005882 continue;
5883 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00005884 thread_sp->GetStatus (strm,
Greg Clayton7260f622011-04-18 08:33:37 +00005885 start_frame,
5886 num_frames,
5887 num_frames_with_source);
5888 ++num_thread_infos_dumped;
5889 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00005890 else
5891 {
5892 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
5893 if (log)
5894 log->Printf("Process::GetThreadStatus - thread 0x" PRIu64 " vanished while running Thread::GetStatus.");
5895
5896 }
Greg Clayton7260f622011-04-18 08:33:37 +00005897 }
5898 return num_thread_infos_dumped;
5899}
5900
Greg Claytona9f40ad2012-02-22 04:37:26 +00005901void
5902Process::AddInvalidMemoryRegion (const LoadRange &region)
5903{
5904 m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize());
5905}
5906
5907bool
5908Process::RemoveInvalidMemoryRange (const LoadRange &region)
5909{
5910 return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize());
5911}
5912
Jim Ingham372787f2012-04-07 00:00:41 +00005913void
5914Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton)
5915{
5916 m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton));
5917}
5918
5919bool
5920Process::RunPreResumeActions ()
5921{
5922 bool result = true;
5923 while (!m_pre_resume_actions.empty())
5924 {
5925 struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back();
5926 m_pre_resume_actions.pop_back();
5927 bool this_result = action.callback (action.baton);
5928 if (result == true) result = this_result;
5929 }
5930 return result;
5931}
5932
5933void
5934Process::ClearPreResumeActions ()
5935{
5936 m_pre_resume_actions.clear();
5937}
Greg Claytona9f40ad2012-02-22 04:37:26 +00005938
Greg Claytonfa559e52012-05-18 02:38:05 +00005939void
5940Process::Flush ()
5941{
5942 m_thread_list.Flush();
Jason Molenda5e8dce42013-12-13 00:29:16 +00005943 m_extended_thread_list.Flush();
5944 m_extended_thread_stop_id = 0;
5945 m_queue_list.Clear();
5946 m_queue_list_stop_id = 0;
Greg Claytonfa559e52012-05-18 02:38:05 +00005947}
Greg Clayton90ba8112012-12-05 00:16:59 +00005948
5949void
5950Process::DidExec ()
5951{
Todd Fiala76e0fc92014-08-27 22:58:26 +00005952 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
5953 if (log)
5954 log->Printf ("Process::%s()", __FUNCTION__);
5955
Greg Clayton90ba8112012-12-05 00:16:59 +00005956 Target &target = GetTarget();
5957 target.CleanupProcess ();
Greg Claytonb35db632013-11-09 00:03:31 +00005958 target.ClearModules(false);
Greg Clayton90ba8112012-12-05 00:16:59 +00005959 m_dynamic_checkers_ap.reset();
5960 m_abi_sp.reset();
Jason Molendaeef51062013-11-05 03:57:19 +00005961 m_system_runtime_ap.reset();
Greg Clayton90ba8112012-12-05 00:16:59 +00005962 m_os_ap.reset();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005963 m_dyld_ap.reset();
Andrew MacPherson17220c12014-03-05 10:12:43 +00005964 m_jit_loaders_ap.reset();
Greg Clayton90ba8112012-12-05 00:16:59 +00005965 m_image_tokens.clear();
5966 m_allocated_memory_cache.Clear();
5967 m_language_runtimes.clear();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005968 m_thread_list.DiscardThreadPlans();
Greg Clayton15fc2be2013-05-21 01:00:52 +00005969 m_memory_cache.Clear(true);
Greg Clayton90ba8112012-12-05 00:16:59 +00005970 DoDidExec();
5971 CompleteAttach ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00005972 // Flush the process (threads and all stack frames) after running CompleteAttach()
5973 // in case the dynamic loader loaded things in new locations.
5974 Flush();
Greg Claytonb35db632013-11-09 00:03:31 +00005975
5976 // After we figure out what was loaded/unloaded in CompleteAttach,
5977 // we need to let the target know so it can do any cleanup it needs to.
5978 target.DidExec();
Greg Clayton90ba8112012-12-05 00:16:59 +00005979}
Greg Clayton095eeaa2013-11-05 23:28:00 +00005980
Jim Ingham1460e4b2014-01-10 23:46:59 +00005981addr_t
5982Process::ResolveIndirectFunction(const Address *address, Error &error)
5983{
5984 if (address == nullptr)
5985 {
Jean-Daniel Dupasef37711f2014-02-08 20:22:05 +00005986 error.SetErrorString("Invalid address argument");
Jim Ingham1460e4b2014-01-10 23:46:59 +00005987 return LLDB_INVALID_ADDRESS;
5988 }
5989
5990 addr_t function_addr = LLDB_INVALID_ADDRESS;
5991
5992 addr_t addr = address->GetLoadAddress(&GetTarget());
5993 std::map<addr_t,addr_t>::const_iterator iter = m_resolved_indirect_addresses.find(addr);
5994 if (iter != m_resolved_indirect_addresses.end())
5995 {
5996 function_addr = (*iter).second;
5997 }
5998 else
5999 {
6000 if (!InferiorCall(this, address, function_addr))
6001 {
6002 Symbol *symbol = address->CalculateSymbolContextSymbol();
6003 error.SetErrorStringWithFormat ("Unable to call resolver for indirect function %s",
6004 symbol ? symbol->GetName().AsCString() : "<UNKNOWN>");
6005 function_addr = LLDB_INVALID_ADDRESS;
6006 }
6007 else
6008 {
6009 m_resolved_indirect_addresses.insert(std::pair<addr_t, addr_t>(addr, function_addr));
6010 }
6011 }
6012 return function_addr;
6013}
6014
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00006015void
6016Process::ModulesDidLoad (ModuleList &module_list)
6017{
6018 SystemRuntime *sys_runtime = GetSystemRuntime();
6019 if (sys_runtime)
6020 {
6021 sys_runtime->ModulesDidLoad (module_list);
6022 }
6023
6024 GetJITLoaders().ModulesDidLoad (module_list);
6025}
Kuba Breckaa51ea382014-09-06 01:33:13 +00006026
6027ThreadCollectionSP
6028Process::GetHistoryThreads(lldb::addr_t addr)
6029{
6030 ThreadCollectionSP threads;
6031
6032 const MemoryHistorySP &memory_history = MemoryHistory::FindPlugin(shared_from_this());
6033
6034 if (! memory_history.get()) {
6035 return threads;
6036 }
6037
6038 threads.reset(new ThreadCollection(memory_history->GetHistoryThreads(addr)));
6039
6040 return threads;
6041}