Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #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 Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 19 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Debugger.h" |
| 21 | #include "lldb/Core/Log.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 22 | #include "lldb/Core/Module.h" |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/Symbol.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Core/PluginManager.h" |
| 25 | #include "lldb/Core/State.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 26 | #include "lldb/Core/StreamFile.h" |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/ClangUserExpression.h" |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Host.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 30 | #include "lldb/Host/Terminal.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Target/ABI.h" |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 32 | #include "lldb/Target/DynamicLoader.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 33 | #include "lldb/Target/JITLoader.h" |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 34 | #include "lldb/Target/OperatingSystem.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 35 | #include "lldb/Target/LanguageRuntime.h" |
| 36 | #include "lldb/Target/CPPLanguageRuntime.h" |
| 37 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 38 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 40 | #include "lldb/Target/StopInfo.h" |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 41 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | #include "lldb/Target/Target.h" |
| 43 | #include "lldb/Target/TargetList.h" |
| 44 | #include "lldb/Target/Thread.h" |
| 45 | #include "lldb/Target/ThreadPlan.h" |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 46 | #include "lldb/Target/ThreadPlanBase.h" |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 47 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 49 | #ifndef LLDB_DISABLE_POSIX |
| 50 | #include <spawn.h> |
| 51 | #endif |
| 52 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | using namespace lldb; |
| 54 | using namespace lldb_private; |
| 55 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 56 | |
| 57 | // Comment out line below to disable memory caching, overriding the process setting |
| 58 | // target.process.disable-memory-cache |
| 59 | #define ENABLE_MEMORY_CACHING |
| 60 | |
| 61 | #ifdef ENABLE_MEMORY_CACHING |
| 62 | #define DISABLE_MEM_CACHE_DEFAULT false |
| 63 | #else |
| 64 | #define DISABLE_MEM_CACHE_DEFAULT true |
| 65 | #endif |
| 66 | |
| 67 | class ProcessOptionValueProperties : public OptionValueProperties |
| 68 | { |
| 69 | public: |
| 70 | ProcessOptionValueProperties (const ConstString &name) : |
| 71 | OptionValueProperties (name) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | // This constructor is used when creating ProcessOptionValueProperties when it |
| 76 | // is part of a new lldb_private::Process instance. It will copy all current |
| 77 | // global property values as needed |
| 78 | ProcessOptionValueProperties (ProcessProperties *global_properties) : |
| 79 | OptionValueProperties(*global_properties->GetValueProperties()) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | virtual const Property * |
| 84 | GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const |
| 85 | { |
| 86 | // When gettings the value for a key from the process options, we will always |
| 87 | // try and grab the setting from the current process if there is one. Else we just |
| 88 | // use the one from this instance. |
| 89 | if (exe_ctx) |
| 90 | { |
| 91 | Process *process = exe_ctx->GetProcessPtr(); |
| 92 | if (process) |
| 93 | { |
| 94 | ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get()); |
| 95 | if (this != instance_properties) |
| 96 | return instance_properties->ProtectedGetPropertyAtIndex (idx); |
| 97 | } |
| 98 | } |
| 99 | return ProtectedGetPropertyAtIndex (idx); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | static PropertyDefinition |
| 104 | g_properties[] = |
| 105 | { |
| 106 | { "disable-memory-cache" , OptionValue::eTypeBoolean, false, DISABLE_MEM_CACHE_DEFAULT, NULL, NULL, "Disable reading and caching of memory in fixed-size units." }, |
Jim Ingham | 8c3f276 | 2012-11-29 00:41:12 +0000 | [diff] [blame] | 107 | { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. " |
| 108 | "For instance, to turn on debugserver logging set this to \"QSetLogging:bitmask=LOG_DEFAULT;\"" }, |
Jim Ingham | afc1b12 | 2013-01-31 19:48:57 +0000 | [diff] [blame] | 109 | { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." }, |
| 110 | { "unwind-on-error-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, errors in expression evaluation will unwind the stack back to the state before the call." }, |
Greg Clayton | e1e835c | 2012-11-29 18:48:47 +0000 | [diff] [blame] | 111 | { "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 Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 112 | { "stop-on-sharedlibrary-events" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, stop when a shared library is loaded or unloaded." }, |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 113 | { "detach-keeps-stopped" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, detach will attempt to keep the process stopped." }, |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 114 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 115 | }; |
| 116 | |
| 117 | enum { |
| 118 | ePropertyDisableMemCache, |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 119 | ePropertyExtraStartCommand, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 120 | ePropertyIgnoreBreakpointsInExpressions, |
| 121 | ePropertyUnwindOnErrorInExpressions, |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 122 | ePropertyPythonOSPluginPath, |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 123 | ePropertyStopOnSharedLibraryEvents, |
| 124 | ePropertyDetachKeepsStopped |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | ProcessProperties::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 Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 135 | ConstString("Settings specific to threads."), |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 136 | true, |
| 137 | Thread::GetGlobalProperties()->GetValueProperties()); |
| 138 | } |
| 139 | else |
| 140 | m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get())); |
| 141 | } |
| 142 | |
| 143 | ProcessProperties::~ProcessProperties() |
| 144 | { |
| 145 | } |
| 146 | |
| 147 | bool |
| 148 | ProcessProperties::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 | |
| 154 | Args |
| 155 | ProcessProperties::GetExtraStartupCommands () const |
| 156 | { |
| 157 | Args args; |
| 158 | const uint32_t idx = ePropertyExtraStartCommand; |
| 159 | m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args); |
| 160 | return args; |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | ProcessProperties::SetExtraStartupCommands (const Args &args) |
| 165 | { |
| 166 | const uint32_t idx = ePropertyExtraStartCommand; |
| 167 | m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args); |
| 168 | } |
| 169 | |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 170 | FileSpec |
| 171 | ProcessProperties::GetPythonOSPluginPath () const |
| 172 | { |
| 173 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 174 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | ProcessProperties::SetPythonOSPluginPath (const FileSpec &file) |
| 179 | { |
| 180 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 181 | m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file); |
| 182 | } |
| 183 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 184 | |
| 185 | bool |
| 186 | ProcessProperties::GetIgnoreBreakpointsInExpressions () const |
| 187 | { |
| 188 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 189 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore) |
| 194 | { |
| 195 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 196 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 197 | } |
| 198 | |
| 199 | bool |
| 200 | ProcessProperties::GetUnwindOnErrorInExpressions () const |
| 201 | { |
| 202 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 203 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore) |
| 208 | { |
| 209 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 210 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 211 | } |
| 212 | |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 213 | bool |
| 214 | ProcessProperties::GetStopOnSharedLibraryEvents () const |
| 215 | { |
| 216 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 217 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 218 | } |
| 219 | |
| 220 | void |
| 221 | ProcessProperties::SetStopOnSharedLibraryEvents (bool stop) |
| 222 | { |
| 223 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 224 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 225 | } |
| 226 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 227 | bool |
| 228 | ProcessProperties::GetDetachKeepsStopped () const |
| 229 | { |
| 230 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 231 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | ProcessProperties::SetDetachKeepsStopped (bool stop) |
| 236 | { |
| 237 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 238 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 239 | } |
| 240 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 241 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 242 | ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 243 | { |
| 244 | const char *cstr; |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 245 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 246 | s.Printf (" pid = %" PRIu64 "\n", m_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 247 | |
| 248 | if (m_parent_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 249 | s.Printf (" parent = %" PRIu64 "\n", m_parent_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 250 | |
| 251 | if (m_executable) |
| 252 | { |
| 253 | s.Printf (" name = %s\n", m_executable.GetFilename().GetCString()); |
| 254 | s.PutCString (" file = "); |
| 255 | m_executable.Dump(&s); |
| 256 | s.EOL(); |
| 257 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 258 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 259 | if (argc > 0) |
| 260 | { |
| 261 | for (uint32_t i=0; i<argc; i++) |
| 262 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 263 | const char *arg = m_arguments.GetArgumentAtIndex(i); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 264 | if (i < 10) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 265 | s.Printf (" arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 266 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 267 | s.Printf ("arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 270 | |
| 271 | const uint32_t envc = m_environment.GetArgumentCount(); |
| 272 | if (envc > 0) |
| 273 | { |
| 274 | for (uint32_t i=0; i<envc; i++) |
| 275 | { |
| 276 | const char *env = m_environment.GetArgumentAtIndex(i); |
| 277 | if (i < 10) |
| 278 | s.Printf (" env[%u] = %s\n", i, env); |
| 279 | else |
| 280 | s.Printf ("env[%u] = %s\n", i, env); |
| 281 | } |
| 282 | } |
| 283 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 284 | if (m_arch.IsValid()) |
| 285 | s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str()); |
| 286 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 287 | if (m_uid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 288 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 289 | cstr = platform->GetUserName (m_uid); |
| 290 | s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 291 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 292 | if (m_gid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 293 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 294 | cstr = platform->GetGroupName (m_gid); |
| 295 | s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 296 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 297 | if (m_euid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 298 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 299 | cstr = platform->GetUserName (m_euid); |
| 300 | s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 301 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 302 | if (m_egid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 303 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 304 | cstr = platform->GetGroupName (m_egid); |
| 305 | s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 310 | ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 311 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 312 | const char *label; |
| 313 | if (show_args || verbose) |
| 314 | label = "ARGUMENTS"; |
| 315 | else |
| 316 | label = "NAME"; |
| 317 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 318 | if (verbose) |
| 319 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 320 | s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 321 | s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n"); |
| 322 | } |
| 323 | else |
| 324 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 325 | s.Printf ("PID PARENT USER ARCH %s\n", label); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 326 | s.PutCString ("====== ====== ========== ======= ============================\n"); |
| 327 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 331 | ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 332 | { |
| 333 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
| 334 | { |
| 335 | const char *cstr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 336 | s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 337 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 338 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 339 | if (verbose) |
| 340 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 341 | cstr = platform->GetUserName (m_uid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 342 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 343 | s.Printf ("%-10s ", cstr); |
| 344 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 345 | s.Printf ("%-10u ", m_uid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 346 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 347 | cstr = platform->GetGroupName (m_gid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 348 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 349 | s.Printf ("%-10s ", cstr); |
| 350 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 351 | s.Printf ("%-10u ", m_gid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 352 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 353 | cstr = platform->GetUserName (m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 354 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 355 | s.Printf ("%-10s ", cstr); |
| 356 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 357 | s.Printf ("%-10u ", m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 358 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 359 | cstr = platform->GetGroupName (m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 360 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 361 | s.Printf ("%-10s ", cstr); |
| 362 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 363 | s.Printf ("%-10u ", m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 364 | s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : ""); |
| 365 | } |
| 366 | else |
| 367 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 368 | s.Printf ("%-10s %-7d %s ", |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 369 | platform->GetUserName (m_euid), |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 370 | (int)m_arch.GetTriple().getArchName().size(), |
| 371 | m_arch.GetTriple().getArchName().data()); |
| 372 | } |
| 373 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 374 | if (verbose || show_args) |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 375 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 376 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 377 | if (argc > 0) |
| 378 | { |
| 379 | for (uint32_t i=0; i<argc; i++) |
| 380 | { |
| 381 | if (i > 0) |
| 382 | s.PutChar (' '); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 383 | s.PutCString (m_arguments.GetArgumentAtIndex(i)); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | s.PutCString (GetName()); |
| 390 | } |
| 391 | |
| 392 | s.EOL(); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 396 | |
| 397 | void |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 398 | ProcessInfo::SetArguments (char const **argv, bool first_arg_is_executable) |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 399 | { |
| 400 | m_arguments.SetArguments (argv); |
| 401 | |
| 402 | // Is the first argument the executable? |
| 403 | if (first_arg_is_executable) |
| 404 | { |
| 405 | const char *first_arg = m_arguments.GetArgumentAtIndex (0); |
| 406 | if (first_arg) |
| 407 | { |
| 408 | // Yes the first argument is an executable, set it as the executable |
| 409 | // in the launch options. Don't resolve the file path as the path |
| 410 | // could be a remote platform path |
| 411 | const bool resolve = false; |
| 412 | m_executable.SetFile(first_arg, resolve); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
| 416 | void |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 417 | ProcessInfo::SetArguments (const Args& args, bool first_arg_is_executable) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 418 | { |
| 419 | // Copy all arguments |
| 420 | m_arguments = args; |
| 421 | |
| 422 | // Is the first argument the executable? |
| 423 | if (first_arg_is_executable) |
| 424 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 425 | const char *first_arg = m_arguments.GetArgumentAtIndex (0); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 426 | if (first_arg) |
| 427 | { |
| 428 | // Yes the first argument is an executable, set it as the executable |
| 429 | // in the launch options. Don't resolve the file path as the path |
| 430 | // could be a remote platform path |
| 431 | const bool resolve = false; |
| 432 | m_executable.SetFile(first_arg, resolve); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 437 | void |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 438 | ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty) |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 439 | { |
| 440 | // If notthing was specified, then check the process for any default |
| 441 | // settings that were set with "settings set" |
| 442 | if (m_file_actions.empty()) |
| 443 | { |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 444 | if (m_flags.Test(eLaunchFlagDisableSTDIO)) |
| 445 | { |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 446 | AppendSuppressFileAction (STDIN_FILENO , true, false); |
| 447 | AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 448 | AppendSuppressFileAction (STDERR_FILENO, false, true); |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 449 | } |
| 450 | else |
| 451 | { |
| 452 | // Check for any values that might have gotten set with any of: |
| 453 | // (lldb) settings set target.input-path |
| 454 | // (lldb) settings set target.output-path |
| 455 | // (lldb) settings set target.error-path |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 456 | FileSpec in_path; |
| 457 | FileSpec out_path; |
| 458 | FileSpec err_path; |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 459 | if (target) |
| 460 | { |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 461 | in_path = target->GetStandardInputPath(); |
| 462 | out_path = target->GetStandardOutputPath(); |
| 463 | err_path = target->GetStandardErrorPath(); |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 466 | if (in_path || out_path || err_path) |
| 467 | { |
| 468 | char path[PATH_MAX]; |
| 469 | if (in_path && in_path.GetPath(path, sizeof(path))) |
| 470 | AppendOpenFileAction(STDIN_FILENO, path, true, false); |
| 471 | |
| 472 | if (out_path && out_path.GetPath(path, sizeof(path))) |
| 473 | AppendOpenFileAction(STDOUT_FILENO, path, false, true); |
| 474 | |
| 475 | if (err_path && err_path.GetPath(path, sizeof(path))) |
| 476 | AppendOpenFileAction(STDERR_FILENO, path, false, true); |
| 477 | } |
| 478 | else if (default_to_use_pty) |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 479 | { |
| 480 | if (m_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, NULL, 0)) |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 481 | { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 482 | const char *slave_path = m_pty.GetSlaveName (NULL, 0); |
| 483 | AppendOpenFileAction(STDIN_FILENO, slave_path, true, false); |
| 484 | AppendOpenFileAction(STDOUT_FILENO, slave_path, false, true); |
| 485 | AppendOpenFileAction(STDERR_FILENO, slave_path, false, true); |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 492 | |
| 493 | bool |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 494 | ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error, |
| 495 | bool localhost, |
| 496 | bool will_debug, |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 497 | bool first_arg_is_full_shell_command, |
| 498 | int32_t num_resumes) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 499 | { |
| 500 | error.Clear(); |
| 501 | |
| 502 | if (GetFlags().Test (eLaunchFlagLaunchInShell)) |
| 503 | { |
| 504 | const char *shell_executable = GetShell(); |
| 505 | if (shell_executable) |
| 506 | { |
| 507 | char shell_resolved_path[PATH_MAX]; |
| 508 | |
| 509 | if (localhost) |
| 510 | { |
| 511 | FileSpec shell_filespec (shell_executable, true); |
| 512 | |
| 513 | if (!shell_filespec.Exists()) |
| 514 | { |
| 515 | // Resolve the path in case we just got "bash", "sh" or "tcsh" |
| 516 | if (!shell_filespec.ResolveExecutableLocation ()) |
| 517 | { |
| 518 | error.SetErrorStringWithFormat("invalid shell path '%s'", shell_executable); |
| 519 | return false; |
| 520 | } |
| 521 | } |
| 522 | shell_filespec.GetPath (shell_resolved_path, sizeof(shell_resolved_path)); |
| 523 | shell_executable = shell_resolved_path; |
| 524 | } |
| 525 | |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 526 | const char **argv = GetArguments().GetConstArgumentVector (); |
| 527 | if (argv == NULL || argv[0] == NULL) |
| 528 | return false; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 529 | Args shell_arguments; |
| 530 | std::string safe_arg; |
| 531 | shell_arguments.AppendArgument (shell_executable); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 532 | shell_arguments.AppendArgument ("-c"); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 533 | StreamString shell_command; |
| 534 | if (will_debug) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 535 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 536 | // Add a modified PATH environment variable in case argv[0] |
| 537 | // is a relative path |
| 538 | const char *argv0 = argv[0]; |
| 539 | if (argv0 && (argv0[0] != '/' && argv0[0] != '~')) |
| 540 | { |
| 541 | // We have a relative path to our executable which may not work if |
| 542 | // we just try to run "a.out" (without it being converted to "./a.out") |
| 543 | const char *working_dir = GetWorkingDirectory(); |
Greg Clayton | 8938f8d | 2013-02-14 03:54:39 +0000 | [diff] [blame] | 544 | // Be sure to put quotes around PATH's value in case any paths have spaces... |
| 545 | std::string new_path("PATH=\""); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 546 | const size_t empty_path_len = new_path.size(); |
| 547 | |
| 548 | if (working_dir && working_dir[0]) |
| 549 | { |
| 550 | new_path += working_dir; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | char current_working_dir[PATH_MAX]; |
| 555 | const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir)); |
| 556 | if (cwd && cwd[0]) |
| 557 | new_path += cwd; |
| 558 | } |
| 559 | const char *curr_path = getenv("PATH"); |
| 560 | if (curr_path) |
| 561 | { |
| 562 | if (new_path.size() > empty_path_len) |
| 563 | new_path += ':'; |
| 564 | new_path += curr_path; |
| 565 | } |
Greg Clayton | 8938f8d | 2013-02-14 03:54:39 +0000 | [diff] [blame] | 566 | new_path += "\" "; |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 567 | shell_command.PutCString(new_path.c_str()); |
| 568 | } |
| 569 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 570 | shell_command.PutCString ("exec"); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 571 | |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 572 | // Only Apple supports /usr/bin/arch being able to specify the architecture |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 573 | if (GetArchitecture().IsValid()) |
| 574 | { |
| 575 | shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName()); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 576 | // Set the resume count to 2: |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 577 | // 1 - stop in shell |
| 578 | // 2 - stop in /usr/bin/arch |
| 579 | // 3 - then we will stop in our program |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 580 | SetResumeCount(num_resumes + 1); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 581 | } |
| 582 | else |
| 583 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 584 | // Set the resume count to 1: |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 585 | // 1 - stop in shell |
| 586 | // 2 - then we will stop in our program |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 587 | SetResumeCount(num_resumes); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 588 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 589 | } |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 590 | |
| 591 | if (first_arg_is_full_shell_command) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 592 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 593 | // There should only be one argument that is the shell command itself to be used as is |
| 594 | if (argv[0] && !argv[1]) |
| 595 | shell_command.Printf("%s", argv[0]); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 596 | else |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 597 | return false; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 598 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 599 | else |
| 600 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 601 | for (size_t i=0; argv[i] != NULL; ++i) |
| 602 | { |
| 603 | const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg); |
| 604 | shell_command.Printf(" %s", arg); |
| 605 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 606 | } |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 607 | shell_arguments.AppendArgument (shell_command.GetString().c_str()); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 608 | m_executable.SetFile(shell_executable, false); |
| 609 | m_arguments = shell_arguments; |
| 610 | return true; |
| 611 | } |
| 612 | else |
| 613 | { |
| 614 | error.SetErrorString ("invalid shell path"); |
| 615 | } |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | error.SetErrorString ("not launching in shell"); |
| 620 | } |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 625 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 626 | ProcessLaunchInfo::FileAction::Open (int fd, const char *path, bool read, bool write) |
| 627 | { |
| 628 | if ((read || write) && fd >= 0 && path && path[0]) |
| 629 | { |
| 630 | m_action = eFileActionOpen; |
| 631 | m_fd = fd; |
| 632 | if (read && write) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 633 | m_arg = O_NOCTTY | O_CREAT | O_RDWR; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 634 | else if (read) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 635 | m_arg = O_NOCTTY | O_RDONLY; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 636 | else |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 637 | m_arg = O_NOCTTY | O_CREAT | O_WRONLY; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 638 | m_path.assign (path); |
| 639 | return true; |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | Clear(); |
| 644 | } |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | bool |
| 649 | ProcessLaunchInfo::FileAction::Close (int fd) |
| 650 | { |
| 651 | Clear(); |
| 652 | if (fd >= 0) |
| 653 | { |
| 654 | m_action = eFileActionClose; |
| 655 | m_fd = fd; |
| 656 | } |
| 657 | return m_fd >= 0; |
| 658 | } |
| 659 | |
| 660 | |
| 661 | bool |
| 662 | ProcessLaunchInfo::FileAction::Duplicate (int fd, int dup_fd) |
| 663 | { |
| 664 | Clear(); |
| 665 | if (fd >= 0 && dup_fd >= 0) |
| 666 | { |
| 667 | m_action = eFileActionDuplicate; |
| 668 | m_fd = fd; |
| 669 | m_arg = dup_fd; |
| 670 | } |
| 671 | return m_fd >= 0; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 676 | #ifndef LLDB_DISABLE_POSIX |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 677 | bool |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 678 | ProcessLaunchInfo::FileAction::AddPosixSpawnFileAction (void *_file_actions, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 679 | const FileAction *info, |
| 680 | Log *log, |
| 681 | Error& error) |
| 682 | { |
| 683 | if (info == NULL) |
| 684 | return false; |
| 685 | |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 686 | posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions); |
| 687 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 688 | switch (info->m_action) |
| 689 | { |
| 690 | case eFileActionNone: |
| 691 | error.Clear(); |
| 692 | break; |
| 693 | |
| 694 | case eFileActionClose: |
| 695 | if (info->m_fd == -1) |
| 696 | error.SetErrorString ("invalid fd for posix_spawn_file_actions_addclose(...)"); |
| 697 | else |
| 698 | { |
| 699 | error.SetError (::posix_spawn_file_actions_addclose (file_actions, info->m_fd), |
| 700 | eErrorTypePOSIX); |
| 701 | if (log && (error.Fail() || log)) |
| 702 | error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)", |
| 703 | file_actions, info->m_fd); |
| 704 | } |
| 705 | break; |
| 706 | |
| 707 | case eFileActionDuplicate: |
| 708 | if (info->m_fd == -1) |
| 709 | error.SetErrorString ("invalid fd for posix_spawn_file_actions_adddup2(...)"); |
| 710 | else if (info->m_arg == -1) |
| 711 | error.SetErrorString ("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)"); |
| 712 | else |
| 713 | { |
| 714 | error.SetError (::posix_spawn_file_actions_adddup2 (file_actions, info->m_fd, info->m_arg), |
| 715 | eErrorTypePOSIX); |
| 716 | if (log && (error.Fail() || log)) |
| 717 | error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)", |
| 718 | file_actions, info->m_fd, info->m_arg); |
| 719 | } |
| 720 | break; |
| 721 | |
| 722 | case eFileActionOpen: |
| 723 | if (info->m_fd == -1) |
| 724 | error.SetErrorString ("invalid fd in posix_spawn_file_actions_addopen(...)"); |
| 725 | else |
| 726 | { |
| 727 | int oflag = info->m_arg; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 728 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 729 | mode_t mode = 0; |
| 730 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 731 | if (oflag & O_CREAT) |
| 732 | mode = 0640; |
| 733 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 734 | error.SetError (::posix_spawn_file_actions_addopen (file_actions, |
| 735 | info->m_fd, |
| 736 | info->m_path.c_str(), |
| 737 | oflag, |
| 738 | mode), |
| 739 | eErrorTypePOSIX); |
| 740 | if (error.Fail() || log) |
| 741 | error.PutToLog(log, |
| 742 | "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)", |
| 743 | file_actions, info->m_fd, info->m_path.c_str(), oflag, mode); |
| 744 | } |
| 745 | break; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 746 | } |
| 747 | return error.Success(); |
| 748 | } |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 749 | #endif |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 750 | |
| 751 | Error |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 752 | ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 753 | { |
| 754 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 755 | const int short_option = m_getopt_table[option_idx].val; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 756 | |
| 757 | switch (short_option) |
| 758 | { |
| 759 | case 's': // Stop at program entry point |
| 760 | launch_info.GetFlags().Set (eLaunchFlagStopAtEntry); |
| 761 | break; |
| 762 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 763 | case 'i': // STDIN for read only |
| 764 | { |
| 765 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 766 | if (action.Open (STDIN_FILENO, option_arg, true, false)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 767 | launch_info.AppendFileAction (action); |
| 768 | } |
| 769 | break; |
| 770 | |
| 771 | case 'o': // Open STDOUT for write only |
| 772 | { |
| 773 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 774 | if (action.Open (STDOUT_FILENO, option_arg, false, true)) |
| 775 | launch_info.AppendFileAction (action); |
| 776 | } |
| 777 | break; |
| 778 | |
| 779 | case 'e': // STDERR for write only |
| 780 | { |
| 781 | ProcessLaunchInfo::FileAction action; |
| 782 | if (action.Open (STDERR_FILENO, option_arg, false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 783 | launch_info.AppendFileAction (action); |
| 784 | } |
| 785 | break; |
| 786 | |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 787 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 788 | case 'p': // Process plug-in name |
| 789 | launch_info.SetProcessPluginName (option_arg); |
| 790 | break; |
| 791 | |
| 792 | case 'n': // Disable STDIO |
| 793 | { |
| 794 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 795 | if (action.Open (STDIN_FILENO, "/dev/null", true, false)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 796 | launch_info.AppendFileAction (action); |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 797 | if (action.Open (STDOUT_FILENO, "/dev/null", false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 798 | launch_info.AppendFileAction (action); |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 799 | if (action.Open (STDERR_FILENO, "/dev/null", false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 800 | launch_info.AppendFileAction (action); |
| 801 | } |
| 802 | break; |
| 803 | |
| 804 | case 'w': |
| 805 | launch_info.SetWorkingDirectory (option_arg); |
| 806 | break; |
| 807 | |
| 808 | case 't': // Open process in new terminal window |
| 809 | launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); |
| 810 | break; |
| 811 | |
| 812 | case 'a': |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 813 | if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get())) |
| 814 | launch_info.GetArchitecture().SetTriple (option_arg); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 815 | break; |
| 816 | |
| 817 | case 'A': |
| 818 | launch_info.GetFlags().Set (eLaunchFlagDisableASLR); |
| 819 | break; |
| 820 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 821 | case 'c': |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 822 | if (option_arg && option_arg[0]) |
| 823 | launch_info.SetShell (option_arg); |
| 824 | else |
Ed Maste | b8ca4a2 | 2013-09-03 23:04:53 +0000 | [diff] [blame] | 825 | launch_info.SetShell (LLDB_DEFAULT_SHELL); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 826 | break; |
| 827 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 828 | case 'v': |
| 829 | launch_info.GetEnvironmentEntries().AppendArgument(option_arg); |
| 830 | break; |
| 831 | |
| 832 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 833 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 834 | break; |
| 835 | |
| 836 | } |
| 837 | return error; |
| 838 | } |
| 839 | |
| 840 | OptionDefinition |
| 841 | ProcessLaunchCommandOptions::g_option_table[] = |
| 842 | { |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 843 | { LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."}, |
| 844 | { LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Disable address space layout randomization when launching a process."}, |
| 845 | { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 846 | { LLDB_OPT_SET_ALL, false, "working-dir", 'w', OptionParser::eRequiredArgument, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."}, |
| 847 | { LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."}, |
| 848 | { LLDB_OPT_SET_ALL, false, "environment", 'v', OptionParser::eRequiredArgument, NULL, 0, eArgTypeNone, "Specify an environment variable name/value string (--environment NAME=VALUE). Can be specified multiple times for subsequent environment entries."}, |
| 849 | { LLDB_OPT_SET_ALL, false, "shell", 'c', OptionParser::eOptionalArgument, NULL, 0, eArgTypeFilename, "Run the process in a shell (not supported on all platforms)."}, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 850 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 851 | { LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."}, |
| 852 | { LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."}, |
| 853 | { LLDB_OPT_SET_1 , false, "stderr", 'e', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stderr for the process to <filename>."}, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 854 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 855 | { LLDB_OPT_SET_2 , false, "tty", 't', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Start the process in a terminal (not supported on all platforms)."}, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 856 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 857 | { LLDB_OPT_SET_3 , false, "no-stdio", 'n', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."}, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 858 | |
| 859 | { 0 , false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
| 860 | }; |
| 861 | |
| 862 | |
| 863 | |
| 864 | bool |
| 865 | ProcessInstanceInfoMatch::NameMatches (const char *process_name) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 866 | { |
| 867 | if (m_name_match_type == eNameMatchIgnore || process_name == NULL) |
| 868 | return true; |
| 869 | const char *match_name = m_match_info.GetName(); |
| 870 | if (!match_name) |
| 871 | return true; |
| 872 | |
| 873 | return lldb_private::NameMatches (process_name, m_name_match_type, match_name); |
| 874 | } |
| 875 | |
| 876 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 877 | ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 878 | { |
| 879 | if (!NameMatches (proc_info.GetName())) |
| 880 | return false; |
| 881 | |
| 882 | if (m_match_info.ProcessIDIsValid() && |
| 883 | m_match_info.GetProcessID() != proc_info.GetProcessID()) |
| 884 | return false; |
| 885 | |
| 886 | if (m_match_info.ParentProcessIDIsValid() && |
| 887 | m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) |
| 888 | return false; |
| 889 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 890 | if (m_match_info.UserIDIsValid () && |
| 891 | m_match_info.GetUserID() != proc_info.GetUserID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 892 | return false; |
| 893 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 894 | if (m_match_info.GroupIDIsValid () && |
| 895 | m_match_info.GetGroupID() != proc_info.GetGroupID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 896 | return false; |
| 897 | |
| 898 | if (m_match_info.EffectiveUserIDIsValid () && |
| 899 | m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) |
| 900 | return false; |
| 901 | |
| 902 | if (m_match_info.EffectiveGroupIDIsValid () && |
| 903 | m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) |
| 904 | return false; |
| 905 | |
| 906 | if (m_match_info.GetArchitecture().IsValid() && |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 907 | !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture())) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 908 | return false; |
| 909 | return true; |
| 910 | } |
| 911 | |
| 912 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 913 | ProcessInstanceInfoMatch::MatchAllProcesses () const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 914 | { |
| 915 | if (m_name_match_type != eNameMatchIgnore) |
| 916 | return false; |
| 917 | |
| 918 | if (m_match_info.ProcessIDIsValid()) |
| 919 | return false; |
| 920 | |
| 921 | if (m_match_info.ParentProcessIDIsValid()) |
| 922 | return false; |
| 923 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 924 | if (m_match_info.UserIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 925 | return false; |
| 926 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 927 | if (m_match_info.GroupIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 928 | return false; |
| 929 | |
| 930 | if (m_match_info.EffectiveUserIDIsValid ()) |
| 931 | return false; |
| 932 | |
| 933 | if (m_match_info.EffectiveGroupIDIsValid ()) |
| 934 | return false; |
| 935 | |
| 936 | if (m_match_info.GetArchitecture().IsValid()) |
| 937 | return false; |
| 938 | |
| 939 | if (m_match_all_users) |
| 940 | return false; |
| 941 | |
| 942 | return true; |
| 943 | |
| 944 | } |
| 945 | |
| 946 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 947 | ProcessInstanceInfoMatch::Clear() |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 948 | { |
| 949 | m_match_info.Clear(); |
| 950 | m_name_match_type = eNameMatchIgnore; |
| 951 | m_match_all_users = false; |
| 952 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 953 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 954 | ProcessSP |
| 955 | Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener, const FileSpec *crash_file_path) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 956 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 957 | static uint32_t g_process_unique_id = 0; |
| 958 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 959 | ProcessSP process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 960 | ProcessCreateInstance create_callback = NULL; |
| 961 | if (plugin_name) |
| 962 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 963 | ConstString const_plugin_name(plugin_name); |
| 964 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 965 | if (create_callback) |
| 966 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 967 | process_sp = create_callback(target, listener, crash_file_path); |
| 968 | if (process_sp) |
| 969 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 970 | if (process_sp->CanDebug(target, true)) |
| 971 | { |
| 972 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 973 | } |
| 974 | else |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 975 | process_sp.reset(); |
| 976 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | else |
| 980 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 981 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 982 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 983 | process_sp = create_callback(target, listener, crash_file_path); |
| 984 | if (process_sp) |
| 985 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 986 | if (process_sp->CanDebug(target, false)) |
| 987 | { |
| 988 | process_sp->m_process_unique_id = ++g_process_unique_id; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 989 | break; |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 990 | } |
| 991 | else |
| 992 | process_sp.reset(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 993 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 996 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 999 | ConstString & |
| 1000 | Process::GetStaticBroadcasterClass () |
| 1001 | { |
| 1002 | static ConstString class_name ("lldb.process"); |
| 1003 | return class_name; |
| 1004 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1005 | |
| 1006 | //---------------------------------------------------------------------- |
| 1007 | // Process constructor |
| 1008 | //---------------------------------------------------------------------- |
| 1009 | Process::Process(Target &target, Listener &listener) : |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1010 | ProcessProperties (false), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1011 | UserID (LLDB_INVALID_PROCESS_ID), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1012 | Broadcaster (&(target.GetDebugger()), "lldb.process"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1013 | m_target (target), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1014 | m_public_state (eStateUnloaded), |
| 1015 | m_private_state (eStateUnloaded), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1016 | m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"), |
| 1017 | m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1018 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 1019 | m_private_state_control_wait(), |
| 1020 | m_private_state_thread (LLDB_INVALID_HOST_THREAD), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1021 | m_mod_id (), |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 1022 | m_process_unique_id(0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1023 | m_thread_index_id (0), |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1024 | m_thread_id_to_index_id_map (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1025 | m_exit_status (-1), |
| 1026 | m_exit_string (), |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1027 | m_thread_mutex (Mutex::eMutexTypeRecursive), |
| 1028 | m_thread_list_real (this), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1029 | m_thread_list (this), |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 1030 | m_extended_thread_list (this), |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1031 | m_extended_thread_stop_id (0), |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1032 | m_queue_list (this), |
| 1033 | m_queue_list_stop_id (0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1034 | m_notifications (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1035 | m_image_tokens (), |
| 1036 | m_listener (listener), |
| 1037 | m_breakpoint_site_list (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1038 | m_dynamic_checkers_ap (), |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1039 | m_unix_signals (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1040 | m_abi_sp (), |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1041 | m_process_input_reader (), |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1042 | m_stdio_communication ("process.stdio"), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1043 | m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1044 | m_stdout_data (), |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 1045 | m_stderr_data (), |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1046 | m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive), |
| 1047 | m_profile_data (), |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 1048 | m_memory_cache (*this), |
| 1049 | m_allocated_memory_cache (*this), |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1050 | m_should_detach (false), |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 1051 | m_next_event_action_ap(), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 1052 | m_public_run_lock (), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 1053 | m_private_run_lock (), |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 1054 | m_currently_handling_event(false), |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 1055 | m_finalize_called(false), |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 1056 | m_clear_thread_plans_on_stop (false), |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1057 | m_force_next_event_delivery(false), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 1058 | m_last_broadcast_state (eStateInvalid), |
Jason Molenda | 69b6b63 | 2013-03-05 03:33:59 +0000 | [diff] [blame] | 1059 | m_destroy_in_process (false), |
| 1060 | m_can_jit(eCanJITDontKnow) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1061 | { |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1062 | CheckInWithManager (); |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 1063 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1064 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1065 | if (log) |
| 1066 | log->Printf ("%p Process::Process()", this); |
| 1067 | |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 1068 | SetEventName (eBroadcastBitStateChanged, "state-changed"); |
| 1069 | SetEventName (eBroadcastBitInterrupt, "interrupt"); |
| 1070 | SetEventName (eBroadcastBitSTDOUT, "stdout-available"); |
| 1071 | SetEventName (eBroadcastBitSTDERR, "stderr-available"); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1072 | SetEventName (eBroadcastBitProfileData, "profile-data-available"); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 1073 | |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1074 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" ); |
| 1075 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" ); |
| 1076 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume"); |
| 1077 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1078 | listener.StartListeningForEvents (this, |
| 1079 | eBroadcastBitStateChanged | |
| 1080 | eBroadcastBitInterrupt | |
| 1081 | eBroadcastBitSTDOUT | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1082 | eBroadcastBitSTDERR | |
| 1083 | eBroadcastBitProfileData); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1084 | |
| 1085 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1086 | eBroadcastBitStateChanged | |
| 1087 | eBroadcastBitInterrupt); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1088 | |
| 1089 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 1090 | eBroadcastInternalStateControlStop | |
| 1091 | eBroadcastInternalStateControlPause | |
| 1092 | eBroadcastInternalStateControlResume); |
| 1093 | } |
| 1094 | |
| 1095 | //---------------------------------------------------------------------- |
| 1096 | // Destructor |
| 1097 | //---------------------------------------------------------------------- |
| 1098 | Process::~Process() |
| 1099 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1100 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1101 | if (log) |
| 1102 | log->Printf ("%p Process::~Process()", this); |
| 1103 | StopPrivateStateThread(); |
| 1104 | } |
| 1105 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1106 | const ProcessPropertiesSP & |
| 1107 | Process::GetGlobalProperties() |
| 1108 | { |
| 1109 | static ProcessPropertiesSP g_settings_sp; |
| 1110 | if (!g_settings_sp) |
| 1111 | g_settings_sp.reset (new ProcessProperties (true)); |
| 1112 | return g_settings_sp; |
| 1113 | } |
| 1114 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1115 | void |
| 1116 | Process::Finalize() |
| 1117 | { |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1118 | switch (GetPrivateState()) |
| 1119 | { |
| 1120 | case eStateConnected: |
| 1121 | case eStateAttaching: |
| 1122 | case eStateLaunching: |
| 1123 | case eStateStopped: |
| 1124 | case eStateRunning: |
| 1125 | case eStateStepping: |
| 1126 | case eStateCrashed: |
| 1127 | case eStateSuspended: |
| 1128 | if (GetShouldDetach()) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1129 | { |
| 1130 | // FIXME: This will have to be a process setting: |
| 1131 | bool keep_stopped = false; |
| 1132 | Detach(keep_stopped); |
| 1133 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1134 | else |
| 1135 | Destroy(); |
| 1136 | break; |
| 1137 | |
| 1138 | case eStateInvalid: |
| 1139 | case eStateUnloaded: |
| 1140 | case eStateDetached: |
| 1141 | case eStateExited: |
| 1142 | break; |
| 1143 | } |
| 1144 | |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 1145 | // Clear our broadcaster before we proceed with destroying |
| 1146 | Broadcaster::Clear(); |
| 1147 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1148 | // Do any cleanup needed prior to being destructed... Subclasses |
| 1149 | // that override this method should call this superclass method as well. |
Jim Ingham | d0a3e12 | 2011-02-16 17:54:55 +0000 | [diff] [blame] | 1150 | |
| 1151 | // We need to destroy the loader before the derived Process class gets destroyed |
| 1152 | // since it is very likely that undoing the loader will require access to the real process. |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 1153 | m_dynamic_checkers_ap.reset(); |
| 1154 | m_abi_sp.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1155 | m_os_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 1156 | m_system_runtime_ap.reset(); |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 1157 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 1158 | m_jit_loaders_ap.reset(); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1159 | m_thread_list_real.Destroy(); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 1160 | m_thread_list.Destroy(); |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 1161 | m_extended_thread_list.Destroy(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1162 | m_queue_list.Clear(); |
| 1163 | m_queue_list_stop_id = 0; |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 1164 | std::vector<Notifications> empty_notifications; |
| 1165 | m_notifications.swap(empty_notifications); |
| 1166 | m_image_tokens.clear(); |
| 1167 | m_memory_cache.Clear(); |
| 1168 | m_allocated_memory_cache.Clear(); |
| 1169 | m_language_runtimes.clear(); |
| 1170 | m_next_event_action_ap.reset(); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1171 | //#ifdef LLDB_CONFIGURATION_DEBUG |
| 1172 | // StreamFile s(stdout, false); |
| 1173 | // EventSP event_sp; |
| 1174 | // while (m_private_state_listener.GetNextEvent(event_sp)) |
| 1175 | // { |
| 1176 | // event_sp->Dump (&s); |
| 1177 | // s.EOL(); |
| 1178 | // } |
| 1179 | //#endif |
| 1180 | // We have to be very careful here as the m_private_state_listener might |
| 1181 | // contain events that have ProcessSP values in them which can keep this |
| 1182 | // process around forever. These events need to be cleared out. |
| 1183 | m_private_state_listener.Clear(); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1184 | m_public_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 1185 | m_public_run_lock.SetStopped(); |
| 1186 | m_private_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 1187 | m_private_run_lock.SetStopped(); |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 1188 | m_finalize_called = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | void |
| 1192 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 1193 | { |
| 1194 | m_notifications.push_back(callbacks); |
| 1195 | if (callbacks.initialize != NULL) |
| 1196 | callbacks.initialize (callbacks.baton, this); |
| 1197 | } |
| 1198 | |
| 1199 | bool |
| 1200 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 1201 | { |
| 1202 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 1203 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 1204 | { |
| 1205 | if (pos->baton == callbacks.baton && |
| 1206 | pos->initialize == callbacks.initialize && |
| 1207 | pos->process_state_changed == callbacks.process_state_changed) |
| 1208 | { |
| 1209 | m_notifications.erase(pos); |
| 1210 | return true; |
| 1211 | } |
| 1212 | } |
| 1213 | return false; |
| 1214 | } |
| 1215 | |
| 1216 | void |
| 1217 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 1218 | { |
| 1219 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 1220 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 1221 | { |
| 1222 | if (notification_pos->process_state_changed) |
| 1223 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 1228 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 1229 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 1230 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 1231 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 1232 | // stall. So instead we do it when we fetch the event off of the queue. |
| 1233 | // |
| 1234 | |
| 1235 | StateType |
| 1236 | Process::GetNextEvent (EventSP &event_sp) |
| 1237 | { |
| 1238 | StateType state = eStateInvalid; |
| 1239 | |
| 1240 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 1241 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 1242 | |
| 1243 | return state; |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | StateType |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1248 | Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr, bool wait_always, Listener *hijack_listener) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1249 | { |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1250 | // We can't just wait for a "stopped" event, because the stopped event may have restarted the target. |
| 1251 | // We have to actually check each event, and in the case of a stopped event check the restarted flag |
| 1252 | // on the event. |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1253 | if (event_sp_ptr) |
| 1254 | event_sp_ptr->reset(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1255 | StateType state = GetState(); |
| 1256 | // If we are exited or detached, we won't ever get back to any |
| 1257 | // other valid state... |
| 1258 | if (state == eStateDetached || state == eStateExited) |
| 1259 | return state; |
| 1260 | |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 1261 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1262 | if (log) |
| 1263 | log->Printf ("Process::%s (timeout = %p)", __FUNCTION__, timeout); |
| 1264 | |
| 1265 | if (!wait_always && |
| 1266 | StateIsStoppedState(state, true) && |
| 1267 | StateIsStoppedState(GetPrivateState(), true)) { |
| 1268 | if (log) |
| 1269 | log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.", |
| 1270 | __FUNCTION__); |
| 1271 | return state; |
| 1272 | } |
| 1273 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1274 | while (state != eStateInvalid) |
| 1275 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1276 | EventSP event_sp; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1277 | state = WaitForStateChangedEvents (timeout, event_sp, hijack_listener); |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1278 | if (event_sp_ptr && event_sp) |
| 1279 | *event_sp_ptr = event_sp; |
| 1280 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1281 | switch (state) |
| 1282 | { |
| 1283 | case eStateCrashed: |
| 1284 | case eStateDetached: |
| 1285 | case eStateExited: |
| 1286 | case eStateUnloaded: |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1287 | // We need to toggle the run lock as this won't get done in |
| 1288 | // SetPublicState() if the process is hijacked. |
| 1289 | if (hijack_listener) |
| 1290 | m_public_run_lock.SetStopped(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1291 | return state; |
| 1292 | case eStateStopped: |
| 1293 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 1294 | continue; |
| 1295 | else |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1296 | { |
| 1297 | // We need to toggle the run lock as this won't get done in |
| 1298 | // SetPublicState() if the process is hijacked. |
| 1299 | if (hijack_listener) |
| 1300 | m_public_run_lock.SetStopped(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1301 | return state; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1302 | } |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1303 | default: |
| 1304 | continue; |
| 1305 | } |
| 1306 | } |
| 1307 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | StateType |
| 1312 | Process::WaitForState |
| 1313 | ( |
| 1314 | const TimeValue *timeout, |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1315 | const StateType *match_states, |
| 1316 | const uint32_t num_match_states |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1317 | ) |
| 1318 | { |
| 1319 | EventSP event_sp; |
| 1320 | uint32_t i; |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1321 | StateType state = GetState(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1322 | while (state != eStateInvalid) |
| 1323 | { |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1324 | // If we are exited or detached, we won't ever get back to any |
| 1325 | // other valid state... |
| 1326 | if (state == eStateDetached || state == eStateExited) |
| 1327 | return state; |
| 1328 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1329 | state = WaitForStateChangedEvents (timeout, event_sp, NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1330 | |
| 1331 | for (i=0; i<num_match_states; ++i) |
| 1332 | { |
| 1333 | if (match_states[i] == state) |
| 1334 | return state; |
| 1335 | } |
| 1336 | } |
| 1337 | return state; |
| 1338 | } |
| 1339 | |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1340 | bool |
| 1341 | Process::HijackProcessEvents (Listener *listener) |
| 1342 | { |
| 1343 | if (listener != NULL) |
| 1344 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1345 | return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1346 | } |
| 1347 | else |
| 1348 | return false; |
| 1349 | } |
| 1350 | |
| 1351 | void |
| 1352 | Process::RestoreProcessEvents () |
| 1353 | { |
| 1354 | RestoreBroadcaster(); |
| 1355 | } |
| 1356 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1357 | bool |
| 1358 | Process::HijackPrivateProcessEvents (Listener *listener) |
| 1359 | { |
| 1360 | if (listener != NULL) |
| 1361 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1362 | return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1363 | } |
| 1364 | else |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
| 1368 | void |
| 1369 | Process::RestorePrivateProcessEvents () |
| 1370 | { |
| 1371 | m_private_state_broadcaster.RestoreBroadcaster(); |
| 1372 | } |
| 1373 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1374 | StateType |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1375 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp, Listener *hijack_listener) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1376 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1377 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1378 | |
| 1379 | if (log) |
| 1380 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1381 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1382 | Listener *listener = hijack_listener; |
| 1383 | if (listener == NULL) |
| 1384 | listener = &m_listener; |
| 1385 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1386 | StateType state = eStateInvalid; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1387 | if (listener->WaitForEventForBroadcasterWithType (timeout, |
| 1388 | this, |
| 1389 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
| 1390 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1391 | { |
| 1392 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1393 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1394 | else if (log) |
| 1395 | log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__); |
| 1396 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1397 | |
| 1398 | if (log) |
| 1399 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 1400 | __FUNCTION__, |
| 1401 | timeout, |
| 1402 | StateAsCString(state)); |
| 1403 | return state; |
| 1404 | } |
| 1405 | |
| 1406 | Event * |
| 1407 | Process::PeekAtStateChangedEvents () |
| 1408 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1409 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1410 | |
| 1411 | if (log) |
| 1412 | log->Printf ("Process::%s...", __FUNCTION__); |
| 1413 | |
| 1414 | Event *event_ptr; |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1415 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this, |
| 1416 | eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1417 | if (log) |
| 1418 | { |
| 1419 | if (event_ptr) |
| 1420 | { |
| 1421 | log->Printf ("Process::%s (event_ptr) => %s", |
| 1422 | __FUNCTION__, |
| 1423 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 1424 | } |
| 1425 | else |
| 1426 | { |
| 1427 | log->Printf ("Process::%s no events found", |
| 1428 | __FUNCTION__); |
| 1429 | } |
| 1430 | } |
| 1431 | return event_ptr; |
| 1432 | } |
| 1433 | |
| 1434 | StateType |
| 1435 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 1436 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1437 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1438 | |
| 1439 | if (log) |
| 1440 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1441 | |
| 1442 | StateType state = eStateInvalid; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1443 | if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout, |
| 1444 | &m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1445 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1446 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1447 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1448 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1449 | |
| 1450 | // This is a bit of a hack, but when we wait here we could very well return |
| 1451 | // to the command-line, and that could disable the log, which would render the |
| 1452 | // log we got above invalid. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1453 | if (log) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1454 | { |
| 1455 | if (state == eStateInvalid) |
| 1456 | log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout); |
| 1457 | else |
| 1458 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state)); |
| 1459 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1460 | return state; |
| 1461 | } |
| 1462 | |
| 1463 | bool |
| 1464 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 1465 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1466 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1467 | |
| 1468 | if (log) |
| 1469 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1470 | |
| 1471 | if (control_only) |
| 1472 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 1473 | else |
| 1474 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 1475 | } |
| 1476 | |
| 1477 | bool |
| 1478 | Process::IsRunning () const |
| 1479 | { |
| 1480 | return StateIsRunningState (m_public_state.GetValue()); |
| 1481 | } |
| 1482 | |
| 1483 | int |
| 1484 | Process::GetExitStatus () |
| 1485 | { |
| 1486 | if (m_public_state.GetValue() == eStateExited) |
| 1487 | return m_exit_status; |
| 1488 | return -1; |
| 1489 | } |
| 1490 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1492 | const char * |
| 1493 | Process::GetExitDescription () |
| 1494 | { |
| 1495 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1496 | return m_exit_string.c_str(); |
| 1497 | return NULL; |
| 1498 | } |
| 1499 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1500 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1501 | Process::SetExitStatus (int status, const char *cstr) |
| 1502 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1503 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1504 | if (log) |
| 1505 | log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1506 | status, status, |
| 1507 | cstr ? "\"" : "", |
| 1508 | cstr ? cstr : "NULL", |
| 1509 | cstr ? "\"" : ""); |
| 1510 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1511 | // We were already in the exited state |
| 1512 | if (m_private_state.GetValue() == eStateExited) |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1513 | { |
Greg Clayton | 385d603 | 2011-01-26 23:47:29 +0000 | [diff] [blame] | 1514 | if (log) |
| 1515 | log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited"); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1516 | return false; |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1517 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1518 | |
| 1519 | m_exit_status = status; |
| 1520 | if (cstr) |
| 1521 | m_exit_string = cstr; |
| 1522 | else |
| 1523 | m_exit_string.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1524 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1525 | DidExit (); |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1526 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1527 | SetPrivateState (eStateExited); |
| 1528 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | // This static callback can be used to watch for local child processes on |
| 1532 | // the current host. The the child process exits, the process will be |
| 1533 | // found in the global target list (we want to be completely sure that the |
| 1534 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 1535 | bool |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1536 | Process::SetProcessExitStatus (void *callback_baton, |
| 1537 | lldb::pid_t pid, |
| 1538 | bool exited, |
| 1539 | int signo, // Zero for no signal |
| 1540 | int exit_status // Exit value of process if signal is zero |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1541 | ) |
| 1542 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1543 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1544 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1545 | log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n", |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1546 | callback_baton, |
| 1547 | pid, |
| 1548 | exited, |
| 1549 | signo, |
| 1550 | exit_status); |
| 1551 | |
| 1552 | if (exited) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1553 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1554 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1555 | if (target_sp) |
| 1556 | { |
| 1557 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 1558 | if (process_sp) |
| 1559 | { |
| 1560 | const char *signal_cstr = NULL; |
| 1561 | if (signo) |
| 1562 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 1563 | |
| 1564 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 1565 | } |
| 1566 | } |
| 1567 | return true; |
| 1568 | } |
| 1569 | return false; |
| 1570 | } |
| 1571 | |
| 1572 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1573 | void |
| 1574 | Process::UpdateThreadListIfNeeded () |
| 1575 | { |
| 1576 | const uint32_t stop_id = GetStopID(); |
| 1577 | if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID()) |
| 1578 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1579 | const StateType state = GetPrivateState(); |
| 1580 | if (StateIsStoppedState (state, true)) |
| 1581 | { |
| 1582 | Mutex::Locker locker (m_thread_list.GetMutex ()); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1583 | // m_thread_list does have its own mutex, but we need to |
| 1584 | // hold onto the mutex between the call to UpdateThreadList(...) |
| 1585 | // and the os->UpdateThreadList(...) so it doesn't change on us |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1586 | ThreadList &old_thread_list = m_thread_list; |
| 1587 | ThreadList real_thread_list(this); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1588 | ThreadList new_thread_list(this); |
| 1589 | // Always update the thread list with the protocol specific |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1590 | // thread list, but only update if "true" is returned |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1591 | if (UpdateThreadList (m_thread_list_real, real_thread_list)) |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1592 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1593 | // Don't call into the OperatingSystem to update the thread list if we are shutting down, since |
| 1594 | // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is |
| 1595 | // shutting us down, causing a deadlock. |
| 1596 | if (!m_destroy_in_process) |
| 1597 | { |
| 1598 | OperatingSystem *os = GetOperatingSystem (); |
| 1599 | if (os) |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1600 | { |
| 1601 | // Clear any old backing threads where memory threads might have been |
| 1602 | // backed by actual threads from the lldb_private::Process subclass |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1603 | size_t num_old_threads = old_thread_list.GetSize(false); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1604 | for (size_t i=0; i<num_old_threads; ++i) |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1605 | old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread(); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1606 | |
| 1607 | // Now let the OperatingSystem plug-in update the thread list |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1608 | os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in |
| 1609 | real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass |
| 1610 | new_thread_list); // The new thread list that we will show to the user that gets filled in |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1611 | } |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1612 | else |
| 1613 | { |
| 1614 | // No OS plug-in, the new thread list is the same as the real thread list |
| 1615 | new_thread_list = real_thread_list; |
| 1616 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1617 | } |
Jim Ingham | 02ff8e0 | 2013-06-22 00:55:02 +0000 | [diff] [blame] | 1618 | |
| 1619 | m_thread_list_real.Update(real_thread_list); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1620 | m_thread_list.Update (new_thread_list); |
| 1621 | m_thread_list.SetStopID (stop_id); |
Jason Molenda | a6e9130 | 2013-11-19 05:44:41 +0000 | [diff] [blame] | 1622 | |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1623 | if (GetLastNaturalStopID () != m_extended_thread_stop_id) |
| 1624 | { |
| 1625 | // Clear any extended threads that we may have accumulated previously |
| 1626 | m_extended_thread_list.Clear(); |
| 1627 | m_extended_thread_stop_id = GetLastNaturalStopID (); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1628 | |
| 1629 | m_queue_list.Clear(); |
| 1630 | m_queue_list_stop_id = GetLastNaturalStopID (); |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1631 | } |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1632 | } |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1633 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1637 | void |
| 1638 | Process::UpdateQueueListIfNeeded () |
| 1639 | { |
| 1640 | if (m_system_runtime_ap.get()) |
| 1641 | { |
| 1642 | if (m_queue_list.GetSize() == 0 || m_queue_list_stop_id != GetLastNaturalStopID()) |
| 1643 | { |
| 1644 | const StateType state = GetPrivateState(); |
| 1645 | if (StateIsStoppedState (state, true)) |
| 1646 | { |
| 1647 | m_system_runtime_ap->PopulateQueueList (m_queue_list); |
| 1648 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
Greg Clayton | a4d8747 | 2013-01-18 23:41:08 +0000 | [diff] [blame] | 1654 | ThreadSP |
| 1655 | Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context) |
| 1656 | { |
| 1657 | OperatingSystem *os = GetOperatingSystem (); |
| 1658 | if (os) |
| 1659 | return os->CreateThread(tid, context); |
| 1660 | return ThreadSP(); |
| 1661 | } |
| 1662 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1663 | uint32_t |
| 1664 | Process::GetNextThreadIndexID (uint64_t thread_id) |
| 1665 | { |
| 1666 | return AssignIndexIDToThread(thread_id); |
| 1667 | } |
| 1668 | |
| 1669 | bool |
| 1670 | Process::HasAssignedIndexIDToThread(uint64_t thread_id) |
| 1671 | { |
| 1672 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1673 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1674 | { |
| 1675 | return false; |
| 1676 | } |
| 1677 | else |
| 1678 | { |
| 1679 | return true; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | uint32_t |
| 1684 | Process::AssignIndexIDToThread(uint64_t thread_id) |
| 1685 | { |
| 1686 | uint32_t result = 0; |
| 1687 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1688 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1689 | { |
| 1690 | result = ++m_thread_index_id; |
| 1691 | m_thread_id_to_index_id_map[thread_id] = result; |
| 1692 | } |
| 1693 | else |
| 1694 | { |
| 1695 | result = iterator->second; |
| 1696 | } |
| 1697 | |
| 1698 | return result; |
| 1699 | } |
| 1700 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1701 | StateType |
| 1702 | Process::GetState() |
| 1703 | { |
| 1704 | // If any other threads access this we will need a mutex for it |
| 1705 | return m_public_state.GetValue (); |
| 1706 | } |
| 1707 | |
| 1708 | void |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1709 | Process::SetPublicState (StateType new_state, bool restarted) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1710 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1711 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1712 | if (log) |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1713 | log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted); |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1714 | const StateType old_state = m_public_state.GetValue(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1715 | m_public_state.SetValue (new_state); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1716 | |
| 1717 | // On the transition from Run to Stopped, we unlock the writer end of the |
| 1718 | // run lock. The lock gets locked in Resume, which is the public API |
| 1719 | // to tell the program to run. |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1720 | if (!IsHijackedForEvent(eBroadcastBitStateChanged)) |
| 1721 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1722 | if (new_state == eStateDetached) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1723 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1724 | if (log) |
| 1725 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock for detach", StateAsCString(new_state)); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1726 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1727 | } |
| 1728 | else |
| 1729 | { |
| 1730 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1731 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1732 | if ((old_state_is_stopped != new_state_is_stopped)) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1733 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1734 | if (new_state_is_stopped && !restarted) |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1735 | { |
| 1736 | if (log) |
| 1737 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state)); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1738 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1739 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1740 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1745 | Error |
| 1746 | Process::Resume () |
| 1747 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1748 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1749 | if (log) |
| 1750 | log->Printf("Process::Resume -- locking run lock"); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1751 | if (!m_public_run_lock.TrySetRunning()) |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1752 | { |
| 1753 | Error error("Resume request failed - process still running."); |
| 1754 | if (log) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1755 | log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming."); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1756 | return error; |
| 1757 | } |
| 1758 | return PrivateResume(); |
| 1759 | } |
| 1760 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1761 | StateType |
| 1762 | Process::GetPrivateState () |
| 1763 | { |
| 1764 | return m_private_state.GetValue(); |
| 1765 | } |
| 1766 | |
| 1767 | void |
| 1768 | Process::SetPrivateState (StateType new_state) |
| 1769 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1770 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1771 | bool state_changed = false; |
| 1772 | |
| 1773 | if (log) |
| 1774 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 1775 | |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1776 | Mutex::Locker thread_locker(m_thread_list.GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1777 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 1778 | |
| 1779 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 1780 | state_changed = old_state != new_state; |
Ed Maste | c29693f | 2013-07-02 16:35:47 +0000 | [diff] [blame] | 1781 | |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1782 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1783 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1784 | if (old_state_is_stopped != new_state_is_stopped) |
| 1785 | { |
| 1786 | if (new_state_is_stopped) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1787 | m_private_run_lock.SetStopped(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1788 | else |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1789 | m_private_run_lock.SetRunning(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1790 | } |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1791 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1792 | if (state_changed) |
| 1793 | { |
| 1794 | m_private_state.SetValueNoLock (new_state); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1795 | if (StateIsStoppedState(new_state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1796 | { |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1797 | // Note, this currently assumes that all threads in the list |
| 1798 | // stop when the process stops. In the future we will want to |
| 1799 | // support a debugging model where some threads continue to run |
| 1800 | // while others are stopped. When that happens we will either need |
| 1801 | // a way for the thread list to identify which threads are stopping |
| 1802 | // or create a special thread list containing only threads which |
| 1803 | // actually stopped. |
| 1804 | // |
| 1805 | // The process plugin is responsible for managing the actual |
| 1806 | // behavior of the threads and should have stopped any threads |
| 1807 | // that are going to stop before we get here. |
| 1808 | m_thread_list.DidStop(); |
| 1809 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1810 | m_mod_id.BumpStopID(); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1811 | m_memory_cache.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1812 | if (log) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1813 | log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1814 | } |
| 1815 | // Use our target to get a shared pointer to ourselves... |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1816 | if (m_finalize_called && PrivateStateThreadIsValid() == false) |
| 1817 | BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state)); |
| 1818 | else |
| 1819 | m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1820 | } |
| 1821 | else |
| 1822 | { |
| 1823 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1824 | log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 1828 | void |
| 1829 | Process::SetRunningUserExpression (bool on) |
| 1830 | { |
| 1831 | m_mod_id.SetRunningUserExpression (on); |
| 1832 | } |
| 1833 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1834 | addr_t |
| 1835 | Process::GetImageInfoAddress() |
| 1836 | { |
| 1837 | return LLDB_INVALID_ADDRESS; |
| 1838 | } |
| 1839 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1840 | //---------------------------------------------------------------------- |
| 1841 | // LoadImage |
| 1842 | // |
| 1843 | // This function provides a default implementation that works for most |
| 1844 | // unix variants. Any Process subclasses that need to do shared library |
| 1845 | // loading differently should override LoadImage and UnloadImage and |
| 1846 | // do what is needed. |
| 1847 | //---------------------------------------------------------------------- |
| 1848 | uint32_t |
| 1849 | Process::LoadImage (const FileSpec &image_spec, Error &error) |
| 1850 | { |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1851 | char path[PATH_MAX]; |
| 1852 | image_spec.GetPath(path, sizeof(path)); |
| 1853 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1854 | DynamicLoader *loader = GetDynamicLoader(); |
| 1855 | if (loader) |
| 1856 | { |
| 1857 | error = loader->CanLoadImage(); |
| 1858 | if (error.Fail()) |
| 1859 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1860 | } |
| 1861 | |
| 1862 | if (error.Success()) |
| 1863 | { |
| 1864 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1865 | |
| 1866 | if (thread_sp) |
| 1867 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1868 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1869 | |
| 1870 | if (frame_sp) |
| 1871 | { |
| 1872 | ExecutionContext exe_ctx; |
| 1873 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1874 | EvaluateExpressionOptions expr_options; |
| 1875 | expr_options.SetUnwindOnError(true); |
| 1876 | expr_options.SetIgnoreBreakpoints(true); |
| 1877 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1878 | StreamString expr; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1879 | expr.Printf("dlopen (\"%s\", 2)", path); |
| 1880 | const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1881 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1882 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1883 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1884 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1885 | expr.GetData(), |
| 1886 | prefix, |
| 1887 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1888 | expr_error); |
| 1889 | if (expr_error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1890 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1891 | error = result_valobj_sp->GetError(); |
| 1892 | if (error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1893 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1894 | Scalar scalar; |
| 1895 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1896 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1897 | addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 1898 | if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS) |
| 1899 | { |
| 1900 | uint32_t image_token = m_image_tokens.size(); |
| 1901 | m_image_tokens.push_back (image_ptr); |
| 1902 | return image_token; |
| 1903 | } |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | } |
| 1909 | } |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1910 | if (!error.AsCString()) |
| 1911 | error.SetErrorStringWithFormat("unable to load '%s'", path); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1912 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1913 | } |
| 1914 | |
| 1915 | //---------------------------------------------------------------------- |
| 1916 | // UnloadImage |
| 1917 | // |
| 1918 | // This function provides a default implementation that works for most |
| 1919 | // unix variants. Any Process subclasses that need to do shared library |
| 1920 | // loading differently should override LoadImage and UnloadImage and |
| 1921 | // do what is needed. |
| 1922 | //---------------------------------------------------------------------- |
| 1923 | Error |
| 1924 | Process::UnloadImage (uint32_t image_token) |
| 1925 | { |
| 1926 | Error error; |
| 1927 | if (image_token < m_image_tokens.size()) |
| 1928 | { |
| 1929 | const addr_t image_addr = m_image_tokens[image_token]; |
| 1930 | if (image_addr == LLDB_INVALID_ADDRESS) |
| 1931 | { |
| 1932 | error.SetErrorString("image already unloaded"); |
| 1933 | } |
| 1934 | else |
| 1935 | { |
| 1936 | DynamicLoader *loader = GetDynamicLoader(); |
| 1937 | if (loader) |
| 1938 | error = loader->CanLoadImage(); |
| 1939 | |
| 1940 | if (error.Success()) |
| 1941 | { |
| 1942 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1943 | |
| 1944 | if (thread_sp) |
| 1945 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1946 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1947 | |
| 1948 | if (frame_sp) |
| 1949 | { |
| 1950 | ExecutionContext exe_ctx; |
| 1951 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1952 | EvaluateExpressionOptions expr_options; |
| 1953 | expr_options.SetUnwindOnError(true); |
| 1954 | expr_options.SetIgnoreBreakpoints(true); |
| 1955 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1956 | StreamString expr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1957 | expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1958 | const char *prefix = "extern \"C\" int dlclose(void* handle);\n"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1959 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1960 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1961 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1962 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1963 | expr.GetData(), |
| 1964 | prefix, |
| 1965 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1966 | expr_error); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1967 | if (result_valobj_sp->GetError().Success()) |
| 1968 | { |
| 1969 | Scalar scalar; |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 1970 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1971 | { |
| 1972 | if (scalar.UInt(1)) |
| 1973 | { |
| 1974 | error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData()); |
| 1975 | } |
| 1976 | else |
| 1977 | { |
| 1978 | m_image_tokens[image_token] = LLDB_INVALID_ADDRESS; |
| 1979 | } |
| 1980 | } |
| 1981 | } |
| 1982 | else |
| 1983 | { |
| 1984 | error = result_valobj_sp->GetError(); |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 | else |
| 1992 | { |
| 1993 | error.SetErrorString("invalid image token"); |
| 1994 | } |
| 1995 | return error; |
| 1996 | } |
| 1997 | |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 1998 | const lldb::ABISP & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1999 | Process::GetABI() |
| 2000 | { |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 2001 | if (!m_abi_sp) |
| 2002 | m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture()); |
| 2003 | return m_abi_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2006 | LanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2007 | Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2008 | { |
| 2009 | LanguageRuntimeCollection::iterator pos; |
| 2010 | pos = m_language_runtimes.find (language); |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2011 | if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second)) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2012 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2013 | lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language)); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2014 | |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2015 | m_language_runtimes[language] = runtime_sp; |
| 2016 | return runtime_sp.get(); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2017 | } |
| 2018 | else |
| 2019 | return (*pos).second.get(); |
| 2020 | } |
| 2021 | |
| 2022 | CPPLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2023 | Process::GetCPPLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2024 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2025 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2026 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 2027 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 2028 | return NULL; |
| 2029 | } |
| 2030 | |
| 2031 | ObjCLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2032 | Process::GetObjCLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2033 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2034 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2035 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 2036 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 2037 | return NULL; |
| 2038 | } |
| 2039 | |
Enrico Granata | fd4c84e | 2012-05-21 16:51:35 +0000 | [diff] [blame] | 2040 | bool |
| 2041 | Process::IsPossibleDynamicValue (ValueObject& in_value) |
| 2042 | { |
| 2043 | if (in_value.IsDynamic()) |
| 2044 | return false; |
| 2045 | LanguageType known_type = in_value.GetObjectRuntimeLanguage(); |
| 2046 | |
| 2047 | if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC) |
| 2048 | { |
| 2049 | LanguageRuntime *runtime = GetLanguageRuntime (known_type); |
| 2050 | return runtime ? runtime->CouldHaveDynamicValue(in_value) : false; |
| 2051 | } |
| 2052 | |
| 2053 | LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus); |
| 2054 | if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value)) |
| 2055 | return true; |
| 2056 | |
| 2057 | LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC); |
| 2058 | return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false; |
| 2059 | } |
| 2060 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2061 | BreakpointSiteList & |
| 2062 | Process::GetBreakpointSiteList() |
| 2063 | { |
| 2064 | return m_breakpoint_site_list; |
| 2065 | } |
| 2066 | |
| 2067 | const BreakpointSiteList & |
| 2068 | Process::GetBreakpointSiteList() const |
| 2069 | { |
| 2070 | return m_breakpoint_site_list; |
| 2071 | } |
| 2072 | |
| 2073 | |
| 2074 | void |
| 2075 | Process::DisableAllBreakpointSites () |
| 2076 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2077 | m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void { |
| 2078 | // bp_site->SetEnabled(true); |
| 2079 | DisableBreakpointSite(bp_site); |
| 2080 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | Error |
| 2084 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 2085 | { |
| 2086 | Error error (DisableBreakpointSiteByID (break_id)); |
| 2087 | |
| 2088 | if (error.Success()) |
| 2089 | m_breakpoint_site_list.Remove(break_id); |
| 2090 | |
| 2091 | return error; |
| 2092 | } |
| 2093 | |
| 2094 | Error |
| 2095 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 2096 | { |
| 2097 | Error error; |
| 2098 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 2099 | if (bp_site_sp) |
| 2100 | { |
| 2101 | if (bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2102 | error = DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2103 | } |
| 2104 | else |
| 2105 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2106 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | return error; |
| 2110 | } |
| 2111 | |
| 2112 | Error |
| 2113 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 2114 | { |
| 2115 | Error error; |
| 2116 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 2117 | if (bp_site_sp) |
| 2118 | { |
| 2119 | if (!bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2120 | error = EnableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2121 | } |
| 2122 | else |
| 2123 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2124 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2125 | } |
| 2126 | return error; |
| 2127 | } |
| 2128 | |
Stephen Wilson | 50bd94f | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 2129 | lldb::break_id_t |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 2130 | Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2131 | { |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 2132 | addr_t load_addr = LLDB_INVALID_ADDRESS; |
| 2133 | |
| 2134 | bool show_error = true; |
| 2135 | switch (GetState()) |
| 2136 | { |
| 2137 | case eStateInvalid: |
| 2138 | case eStateUnloaded: |
| 2139 | case eStateConnected: |
| 2140 | case eStateAttaching: |
| 2141 | case eStateLaunching: |
| 2142 | case eStateDetached: |
| 2143 | case eStateExited: |
| 2144 | show_error = false; |
| 2145 | break; |
| 2146 | |
| 2147 | case eStateStopped: |
| 2148 | case eStateRunning: |
| 2149 | case eStateStepping: |
| 2150 | case eStateCrashed: |
| 2151 | case eStateSuspended: |
| 2152 | show_error = IsAlive(); |
| 2153 | break; |
| 2154 | } |
| 2155 | |
| 2156 | // Reset the IsIndirect flag here, in case the location changes from |
| 2157 | // pointing to a indirect symbol to a regular symbol. |
| 2158 | owner->SetIsIndirect (false); |
| 2159 | |
| 2160 | if (owner->ShouldResolveIndirectFunctions()) |
| 2161 | { |
| 2162 | Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol(); |
| 2163 | if (symbol && symbol->IsIndirect()) |
| 2164 | { |
| 2165 | Error error; |
| 2166 | load_addr = ResolveIndirectFunction (&symbol->GetAddress(), error); |
| 2167 | if (!error.Success() && show_error) |
| 2168 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 2169 | m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to resolve indirect function at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 2170 | symbol->GetAddress().GetLoadAddress(&m_target), |
| 2171 | owner->GetBreakpoint().GetID(), |
| 2172 | owner->GetID(), |
| 2173 | error.AsCString() ? error.AsCString() : "unkown error"); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 2174 | return LLDB_INVALID_BREAK_ID; |
| 2175 | } |
| 2176 | Address resolved_address(load_addr); |
| 2177 | load_addr = resolved_address.GetOpcodeLoadAddress (&m_target); |
| 2178 | owner->SetIsIndirect(true); |
| 2179 | } |
| 2180 | else |
| 2181 | load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
| 2182 | } |
| 2183 | else |
| 2184 | load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
| 2185 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2186 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 2187 | { |
| 2188 | BreakpointSiteSP bp_site_sp; |
| 2189 | |
| 2190 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 2191 | // create a new breakpoint site and add it. |
| 2192 | |
| 2193 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 2194 | |
| 2195 | if (bp_site_sp) |
| 2196 | { |
| 2197 | bp_site_sp->AddOwner (owner); |
| 2198 | owner->SetBreakpointSite (bp_site_sp); |
| 2199 | return bp_site_sp->GetID(); |
| 2200 | } |
| 2201 | else |
| 2202 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2203 | bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, use_hardware)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2204 | if (bp_site_sp) |
| 2205 | { |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2206 | Error error = EnableBreakpointSite (bp_site_sp.get()); |
| 2207 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2208 | { |
| 2209 | owner->SetBreakpointSite (bp_site_sp); |
| 2210 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 2211 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2212 | else |
| 2213 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2214 | if (show_error) |
| 2215 | { |
| 2216 | // Report error for setting breakpoint... |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 2217 | m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 2218 | load_addr, |
| 2219 | owner->GetBreakpoint().GetID(), |
| 2220 | owner->GetID(), |
| 2221 | error.AsCString() ? error.AsCString() : "unkown error"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2222 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2223 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | // We failed to enable the breakpoint |
| 2228 | return LLDB_INVALID_BREAK_ID; |
| 2229 | |
| 2230 | } |
| 2231 | |
| 2232 | void |
| 2233 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 2234 | { |
| 2235 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 2236 | if (num_owners == 0) |
| 2237 | { |
Jim Ingham | f1ff3bb | 2013-04-06 00:16:39 +0000 | [diff] [blame] | 2238 | // Don't try to disable the site if we don't have a live process anymore. |
| 2239 | if (IsAlive()) |
| 2240 | DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2241 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | |
| 2246 | size_t |
| 2247 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 2248 | { |
| 2249 | size_t bytes_removed = 0; |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2250 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2251 | |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2252 | if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2253 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2254 | bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void { |
| 2255 | if (bp_site->GetType() == BreakpointSite::eSoftware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2256 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2257 | addr_t intersect_addr; |
| 2258 | size_t intersect_size; |
| 2259 | size_t opcode_offset; |
| 2260 | if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset)) |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2261 | { |
| 2262 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 2263 | assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size); |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2264 | assert(opcode_offset + intersect_size <= bp_site->GetByteSize()); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2265 | size_t buf_offset = intersect_addr - bp_addr; |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2266 | ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2267 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2268 | } |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2269 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2270 | } |
| 2271 | return bytes_removed; |
| 2272 | } |
| 2273 | |
| 2274 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 2275 | |
| 2276 | size_t |
| 2277 | Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site) |
| 2278 | { |
| 2279 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 2280 | if (platform_sp) |
| 2281 | return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site); |
| 2282 | return 0; |
| 2283 | } |
| 2284 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2285 | Error |
| 2286 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2287 | { |
| 2288 | Error error; |
| 2289 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2290 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2291 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2292 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2293 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2294 | if (bp_site->IsEnabled()) |
| 2295 | { |
| 2296 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2297 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- already enabled", bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2298 | return error; |
| 2299 | } |
| 2300 | |
| 2301 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 2302 | { |
| 2303 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 2304 | return error; |
| 2305 | } |
| 2306 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 2307 | // trap for the breakpoint site |
| 2308 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2309 | |
| 2310 | if (bp_opcode_size == 0) |
| 2311 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2312 | error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%" PRIx64, bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2313 | } |
| 2314 | else |
| 2315 | { |
| 2316 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 2317 | |
| 2318 | if (bp_opcode_bytes == NULL) |
| 2319 | { |
| 2320 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 2321 | return error; |
| 2322 | } |
| 2323 | |
| 2324 | // Save the original opcode by reading it |
| 2325 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 2326 | { |
| 2327 | // Write a software breakpoint in place of the original opcode |
| 2328 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2329 | { |
| 2330 | uint8_t verify_bp_opcode_bytes[64]; |
| 2331 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2332 | { |
| 2333 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 2334 | { |
| 2335 | bp_site->SetEnabled(true); |
| 2336 | bp_site->SetType (BreakpointSite::eSoftware); |
| 2337 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2338 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2339 | bp_site->GetID(), |
| 2340 | (uint64_t)bp_addr); |
| 2341 | } |
| 2342 | else |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2343 | error.SetErrorString("failed to verify the breakpoint trap in memory."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2344 | } |
| 2345 | else |
| 2346 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 2347 | } |
| 2348 | else |
| 2349 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 2350 | } |
| 2351 | else |
| 2352 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 2353 | } |
Stephen Wilson | 78a4feb | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 2354 | if (log && error.Fail()) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2355 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2356 | bp_site->GetID(), |
| 2357 | (uint64_t)bp_addr, |
| 2358 | error.AsCString()); |
| 2359 | return error; |
| 2360 | } |
| 2361 | |
| 2362 | Error |
| 2363 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2364 | { |
| 2365 | Error error; |
| 2366 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2367 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2368 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2369 | lldb::user_id_t breakID = bp_site->GetID(); |
| 2370 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2371 | log->Printf ("Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 ") addr = 0x%" PRIx64, breakID, (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2372 | |
| 2373 | if (bp_site->IsHardware()) |
| 2374 | { |
| 2375 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 2376 | } |
| 2377 | else if (bp_site->IsEnabled()) |
| 2378 | { |
| 2379 | const size_t break_op_size = bp_site->GetByteSize(); |
| 2380 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 2381 | if (break_op_size > 0) |
| 2382 | { |
| 2383 | // Clear a software breakoint instruction |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2384 | uint8_t curr_break_op[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2385 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2386 | bool break_op_found = false; |
| 2387 | |
| 2388 | // Read the breakpoint opcode |
| 2389 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 2390 | { |
| 2391 | bool verify = false; |
| 2392 | // Make sure we have the a breakpoint opcode exists at this address |
| 2393 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 2394 | { |
| 2395 | break_op_found = true; |
| 2396 | // We found a valid breakpoint opcode at this address, now restore |
| 2397 | // the saved opcode. |
| 2398 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 2399 | { |
| 2400 | verify = true; |
| 2401 | } |
| 2402 | else |
| 2403 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 2404 | } |
| 2405 | else |
| 2406 | { |
| 2407 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 2408 | // Set verify to true and so we can check if the original opcode has already been restored |
| 2409 | verify = true; |
| 2410 | } |
| 2411 | |
| 2412 | if (verify) |
| 2413 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2414 | uint8_t verify_opcode[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2415 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2416 | // Verify that our original opcode made it back to the inferior |
| 2417 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 2418 | { |
| 2419 | // compare the memory we just read with the original opcode |
| 2420 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 2421 | { |
| 2422 | // SUCCESS |
| 2423 | bp_site->SetEnabled(false); |
| 2424 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2425 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2426 | return error; |
| 2427 | } |
| 2428 | else |
| 2429 | { |
| 2430 | if (break_op_found) |
| 2431 | error.SetErrorString("Failed to restore original opcode."); |
| 2432 | } |
| 2433 | } |
| 2434 | else |
| 2435 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 2436 | } |
| 2437 | } |
| 2438 | else |
| 2439 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 2440 | } |
| 2441 | } |
| 2442 | else |
| 2443 | { |
| 2444 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2445 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- already disabled", bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2446 | return error; |
| 2447 | } |
| 2448 | |
| 2449 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2450 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2451 | bp_site->GetID(), |
| 2452 | (uint64_t)bp_addr, |
| 2453 | error.AsCString()); |
| 2454 | return error; |
| 2455 | |
| 2456 | } |
| 2457 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2458 | // Uncomment to verify memory caching works after making changes to caching code |
| 2459 | //#define VERIFY_MEMORY_READS |
| 2460 | |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2461 | size_t |
| 2462 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 2463 | { |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 2464 | error.Clear(); |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2465 | if (!GetDisableMemoryCache()) |
| 2466 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2467 | #if defined (VERIFY_MEMORY_READS) |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2468 | // Memory caching is enabled, with debug verification |
| 2469 | |
| 2470 | if (buf && size) |
| 2471 | { |
| 2472 | // Uncomment the line below to make sure memory caching is working. |
| 2473 | // I ran this through the test suite and got no assertions, so I am |
| 2474 | // pretty confident this is working well. If any changes are made to |
| 2475 | // memory caching, uncomment the line below and test your changes! |
| 2476 | |
| 2477 | // Verify all memory reads by using the cache first, then redundantly |
| 2478 | // reading the same memory from the inferior and comparing to make sure |
| 2479 | // everything is exactly the same. |
| 2480 | std::string verify_buf (size, '\0'); |
| 2481 | assert (verify_buf.size() == size); |
| 2482 | const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error); |
| 2483 | Error verify_error; |
| 2484 | const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error); |
| 2485 | assert (cache_bytes_read == verify_bytes_read); |
| 2486 | assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 2487 | assert (verify_error.Success() == error.Success()); |
| 2488 | return cache_bytes_read; |
| 2489 | } |
| 2490 | return 0; |
| 2491 | #else // !defined(VERIFY_MEMORY_READS) |
| 2492 | // Memory caching is enabled, without debug verification |
| 2493 | |
| 2494 | return m_memory_cache.Read (addr, buf, size, error); |
| 2495 | #endif // defined (VERIFY_MEMORY_READS) |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2496 | } |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2497 | else |
| 2498 | { |
| 2499 | // Memory caching is disabled |
| 2500 | |
| 2501 | return ReadMemoryFromInferior (addr, buf, size, error); |
| 2502 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2503 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2504 | |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2505 | size_t |
| 2506 | Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error) |
| 2507 | { |
Greg Clayton | de87c0f | 2012-05-19 00:18:00 +0000 | [diff] [blame] | 2508 | char buf[256]; |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2509 | out_str.clear(); |
| 2510 | addr_t curr_addr = addr; |
| 2511 | while (1) |
| 2512 | { |
| 2513 | size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error); |
| 2514 | if (length == 0) |
| 2515 | break; |
| 2516 | out_str.append(buf, length); |
| 2517 | // If we got "length - 1" bytes, we didn't get the whole C string, we |
| 2518 | // need to read some more characters |
| 2519 | if (length == sizeof(buf) - 1) |
| 2520 | curr_addr += length; |
| 2521 | else |
| 2522 | break; |
| 2523 | } |
| 2524 | return out_str.size(); |
| 2525 | } |
| 2526 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2527 | |
| 2528 | size_t |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2529 | Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error, |
| 2530 | size_t type_width) |
| 2531 | { |
| 2532 | size_t total_bytes_read = 0; |
| 2533 | if (dst && max_bytes && type_width && max_bytes >= type_width) |
| 2534 | { |
| 2535 | // Ensure a null terminator independent of the number of bytes that is read. |
| 2536 | memset (dst, 0, max_bytes); |
| 2537 | size_t bytes_left = max_bytes - type_width; |
| 2538 | |
| 2539 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 2540 | assert(sizeof(terminator) >= type_width && |
| 2541 | "Attempting to validate a string with more than 4 bytes per character!"); |
| 2542 | |
| 2543 | addr_t curr_addr = addr; |
| 2544 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2545 | char *curr_dst = dst; |
| 2546 | |
| 2547 | error.Clear(); |
| 2548 | while (bytes_left > 0 && error.Success()) |
| 2549 | { |
| 2550 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2551 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2552 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2553 | |
| 2554 | if (bytes_read == 0) |
| 2555 | break; |
| 2556 | |
| 2557 | // Search for a null terminator of correct size and alignment in bytes_read |
| 2558 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2559 | for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2560 | if (::strncmp(&dst[i], terminator, type_width) == 0) |
| 2561 | { |
| 2562 | error.Clear(); |
| 2563 | return i; |
| 2564 | } |
| 2565 | |
| 2566 | total_bytes_read += bytes_read; |
| 2567 | curr_dst += bytes_read; |
| 2568 | curr_addr += bytes_read; |
| 2569 | bytes_left -= bytes_read; |
| 2570 | } |
| 2571 | } |
| 2572 | else |
| 2573 | { |
| 2574 | if (max_bytes) |
| 2575 | error.SetErrorString("invalid arguments"); |
| 2576 | } |
| 2577 | return total_bytes_read; |
| 2578 | } |
| 2579 | |
| 2580 | // Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find |
| 2581 | // null terminators. |
| 2582 | size_t |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2583 | Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2584 | { |
| 2585 | size_t total_cstr_len = 0; |
| 2586 | if (dst && dst_max_len) |
| 2587 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2588 | result_error.Clear(); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2589 | // NULL out everything just to be safe |
| 2590 | memset (dst, 0, dst_max_len); |
| 2591 | Error error; |
| 2592 | addr_t curr_addr = addr; |
| 2593 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2594 | size_t bytes_left = dst_max_len - 1; |
| 2595 | char *curr_dst = dst; |
| 2596 | |
| 2597 | while (bytes_left > 0) |
| 2598 | { |
| 2599 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2600 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2601 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2602 | |
| 2603 | if (bytes_read == 0) |
| 2604 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2605 | result_error = error; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2606 | dst[total_cstr_len] = '\0'; |
| 2607 | break; |
| 2608 | } |
| 2609 | const size_t len = strlen(curr_dst); |
| 2610 | |
| 2611 | total_cstr_len += len; |
| 2612 | |
| 2613 | if (len < bytes_to_read) |
| 2614 | break; |
| 2615 | |
| 2616 | curr_dst += bytes_read; |
| 2617 | curr_addr += bytes_read; |
| 2618 | bytes_left -= bytes_read; |
| 2619 | } |
| 2620 | } |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2621 | else |
| 2622 | { |
| 2623 | if (dst == NULL) |
| 2624 | result_error.SetErrorString("invalid arguments"); |
| 2625 | else |
| 2626 | result_error.Clear(); |
| 2627 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2628 | return total_cstr_len; |
| 2629 | } |
| 2630 | |
| 2631 | size_t |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2632 | Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error) |
| 2633 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2634 | if (buf == NULL || size == 0) |
| 2635 | return 0; |
| 2636 | |
| 2637 | size_t bytes_read = 0; |
| 2638 | uint8_t *bytes = (uint8_t *)buf; |
| 2639 | |
| 2640 | while (bytes_read < size) |
| 2641 | { |
| 2642 | const size_t curr_size = size - bytes_read; |
| 2643 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 2644 | bytes + bytes_read, |
| 2645 | curr_size, |
| 2646 | error); |
| 2647 | bytes_read += curr_bytes_read; |
| 2648 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 2649 | break; |
| 2650 | } |
| 2651 | |
| 2652 | // Replace any software breakpoint opcodes that fall into this range back |
| 2653 | // into "buf" before we return |
| 2654 | if (bytes_read > 0) |
| 2655 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 2656 | return bytes_read; |
| 2657 | } |
| 2658 | |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2659 | uint64_t |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2660 | Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error) |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2661 | { |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2662 | Scalar scalar; |
| 2663 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error)) |
| 2664 | return scalar.ULongLong(fail_value); |
| 2665 | return fail_value; |
| 2666 | } |
| 2667 | |
| 2668 | addr_t |
| 2669 | Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error) |
| 2670 | { |
| 2671 | Scalar scalar; |
| 2672 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error)) |
| 2673 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2674 | return LLDB_INVALID_ADDRESS; |
| 2675 | } |
| 2676 | |
| 2677 | |
| 2678 | bool |
| 2679 | Process::WritePointerToMemory (lldb::addr_t vm_addr, |
| 2680 | lldb::addr_t ptr_value, |
| 2681 | Error &error) |
| 2682 | { |
| 2683 | Scalar scalar; |
| 2684 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2685 | if (addr_byte_size <= 4) |
| 2686 | scalar = (uint32_t)ptr_value; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2687 | else |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2688 | scalar = ptr_value; |
| 2689 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2692 | size_t |
| 2693 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 2694 | { |
| 2695 | size_t bytes_written = 0; |
| 2696 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2697 | |
| 2698 | while (bytes_written < size) |
| 2699 | { |
| 2700 | const size_t curr_size = size - bytes_written; |
| 2701 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 2702 | bytes + bytes_written, |
| 2703 | curr_size, |
| 2704 | error); |
| 2705 | bytes_written += curr_bytes_written; |
| 2706 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2707 | break; |
| 2708 | } |
| 2709 | return bytes_written; |
| 2710 | } |
| 2711 | |
| 2712 | size_t |
| 2713 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2714 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2715 | #if defined (ENABLE_MEMORY_CACHING) |
| 2716 | m_memory_cache.Flush (addr, size); |
| 2717 | #endif |
| 2718 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2719 | if (buf == NULL || size == 0) |
| 2720 | return 0; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2721 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2722 | m_mod_id.BumpMemoryID(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2723 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2724 | // We need to write any data that would go where any current software traps |
| 2725 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2726 | // may have placed in our tasks memory. |
| 2727 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2728 | BreakpointSiteList bp_sites_in_range; |
| 2729 | |
| 2730 | if (m_breakpoint_site_list.FindInRange (addr, addr + size, bp_sites_in_range)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2731 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2732 | // No breakpoint sites overlap |
| 2733 | if (bp_sites_in_range.IsEmpty()) |
| 2734 | return WriteMemoryPrivate (addr, buf, size, error); |
| 2735 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2736 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2737 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2738 | uint64_t bytes_written = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2739 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2740 | bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void { |
| 2741 | |
| 2742 | if (error.Success()) |
| 2743 | { |
| 2744 | addr_t intersect_addr; |
| 2745 | size_t intersect_size; |
| 2746 | size_t opcode_offset; |
| 2747 | const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset); |
| 2748 | assert(intersects); |
| 2749 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2750 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 2751 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2752 | |
| 2753 | // Check for bytes before this breakpoint |
| 2754 | const addr_t curr_addr = addr + bytes_written; |
| 2755 | if (intersect_addr > curr_addr) |
| 2756 | { |
| 2757 | // There are some bytes before this breakpoint that we need to |
| 2758 | // just write to memory |
| 2759 | size_t curr_size = intersect_addr - curr_addr; |
| 2760 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 2761 | ubuf + bytes_written, |
| 2762 | curr_size, |
| 2763 | error); |
| 2764 | bytes_written += curr_bytes_written; |
| 2765 | if (curr_bytes_written != curr_size) |
| 2766 | { |
| 2767 | // We weren't able to write all of the requested bytes, we |
| 2768 | // are done looping and will return the number of bytes that |
| 2769 | // we have written so far. |
| 2770 | if (error.Success()) |
| 2771 | error.SetErrorToGenericError(); |
| 2772 | } |
| 2773 | } |
| 2774 | // Now write any bytes that would cover up any software breakpoints |
| 2775 | // directly into the breakpoint opcode buffer |
| 2776 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 2777 | bytes_written += intersect_size; |
| 2778 | } |
| 2779 | }); |
| 2780 | |
| 2781 | if (bytes_written < size) |
| 2782 | bytes_written += WriteMemoryPrivate (addr + bytes_written, |
| 2783 | ubuf + bytes_written, |
| 2784 | size - bytes_written, |
| 2785 | error); |
| 2786 | } |
| 2787 | } |
| 2788 | else |
| 2789 | { |
| 2790 | return WriteMemoryPrivate (addr, buf, size, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | // Write any remaining bytes after the last breakpoint if we have any left |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2794 | return 0; //bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2795 | } |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2796 | |
| 2797 | size_t |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2798 | Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, size_t byte_size, Error &error) |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2799 | { |
| 2800 | if (byte_size == UINT32_MAX) |
| 2801 | byte_size = scalar.GetByteSize(); |
| 2802 | if (byte_size > 0) |
| 2803 | { |
| 2804 | uint8_t buf[32]; |
| 2805 | const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error); |
| 2806 | if (mem_size > 0) |
| 2807 | return WriteMemory(addr, buf, mem_size, error); |
| 2808 | else |
| 2809 | error.SetErrorString ("failed to get scalar as memory data"); |
| 2810 | } |
| 2811 | else |
| 2812 | { |
| 2813 | error.SetErrorString ("invalid scalar value"); |
| 2814 | } |
| 2815 | return 0; |
| 2816 | } |
| 2817 | |
| 2818 | size_t |
| 2819 | Process::ReadScalarIntegerFromMemory (addr_t addr, |
| 2820 | uint32_t byte_size, |
| 2821 | bool is_signed, |
| 2822 | Scalar &scalar, |
| 2823 | Error &error) |
| 2824 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2825 | uint64_t uval = 0; |
| 2826 | if (byte_size == 0) |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2827 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2828 | error.SetErrorString ("byte size is zero"); |
| 2829 | } |
| 2830 | else if (byte_size & (byte_size - 1)) |
| 2831 | { |
| 2832 | error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size); |
| 2833 | } |
| 2834 | else if (byte_size <= sizeof(uval)) |
| 2835 | { |
| 2836 | const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2837 | if (bytes_read == byte_size) |
| 2838 | { |
| 2839 | DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize()); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2840 | lldb::offset_t offset = 0; |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2841 | if (byte_size <= 4) |
| 2842 | scalar = data.GetMaxU32 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2843 | else |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2844 | scalar = data.GetMaxU64 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2845 | if (is_signed) |
| 2846 | scalar.SignExtend(byte_size * 8); |
| 2847 | return bytes_read; |
| 2848 | } |
| 2849 | } |
| 2850 | else |
| 2851 | { |
| 2852 | error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size); |
| 2853 | } |
| 2854 | return 0; |
| 2855 | } |
| 2856 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2857 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2858 | addr_t |
| 2859 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 2860 | { |
Jim Ingham | f72ce3a | 2011-06-20 17:32:44 +0000 | [diff] [blame] | 2861 | if (GetPrivateState() != eStateStopped) |
| 2862 | return LLDB_INVALID_ADDRESS; |
| 2863 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2864 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2865 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
| 2866 | #else |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2867 | addr_t allocated_addr = DoAllocateMemory (size, permissions, error); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2868 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2869 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 2870 | log->Printf("Process::AllocateMemory(size=%" PRIu64 ", permissions=%s) => 0x%16.16" PRIx64 " (m_stop_id = %u m_memory_id = %u)", |
Deepak Panickal | d66b50c | 2013-10-22 12:27:43 +0000 | [diff] [blame] | 2871 | (uint64_t)size, |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2872 | GetPermissionsAsCString (permissions), |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2873 | (uint64_t)allocated_addr, |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2874 | m_mod_id.GetStopID(), |
| 2875 | m_mod_id.GetMemoryID()); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2876 | return allocated_addr; |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2877 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2880 | bool |
| 2881 | Process::CanJIT () |
| 2882 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2883 | if (m_can_jit == eCanJITDontKnow) |
| 2884 | { |
| 2885 | Error err; |
| 2886 | |
| 2887 | uint64_t allocated_memory = AllocateMemory(8, |
| 2888 | ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2889 | err); |
| 2890 | |
| 2891 | if (err.Success()) |
| 2892 | m_can_jit = eCanJITYes; |
| 2893 | else |
| 2894 | m_can_jit = eCanJITNo; |
| 2895 | |
| 2896 | DeallocateMemory (allocated_memory); |
| 2897 | } |
| 2898 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2899 | return m_can_jit == eCanJITYes; |
| 2900 | } |
| 2901 | |
| 2902 | void |
| 2903 | Process::SetCanJIT (bool can_jit) |
| 2904 | { |
| 2905 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
| 2906 | } |
| 2907 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2908 | Error |
| 2909 | Process::DeallocateMemory (addr_t ptr) |
| 2910 | { |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2911 | Error error; |
| 2912 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2913 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) |
| 2914 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2915 | error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2916 | } |
| 2917 | #else |
| 2918 | error = DoDeallocateMemory (ptr); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2919 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2920 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2921 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2922 | log->Printf("Process::DeallocateMemory(addr=0x%16.16" PRIx64 ") => err = %s (m_stop_id = %u, m_memory_id = %u)", |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2923 | ptr, |
| 2924 | error.AsCString("SUCCESS"), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2925 | m_mod_id.GetStopID(), |
| 2926 | m_mod_id.GetMemoryID()); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2927 | #endif |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2928 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Han Ming Ong | c811d38 | 2012-11-17 00:33:14 +0000 | [diff] [blame] | 2931 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2932 | ModuleSP |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2933 | Process::ReadModuleFromMemory (const FileSpec& file_spec, |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 2934 | lldb::addr_t header_addr, |
| 2935 | size_t size_to_read) |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2936 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2937 | ModuleSP module_sp (new Module (file_spec, ArchSpec())); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2938 | if (module_sp) |
| 2939 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2940 | Error error; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 2941 | ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error, size_to_read); |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2942 | if (objfile) |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2943 | return module_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2944 | } |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2945 | return ModuleSP(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2946 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2947 | |
| 2948 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2949 | Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2950 | { |
| 2951 | Error error; |
| 2952 | error.SetErrorString("watchpoints are not supported"); |
| 2953 | return error; |
| 2954 | } |
| 2955 | |
| 2956 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2957 | Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2958 | { |
| 2959 | Error error; |
| 2960 | error.SetErrorString("watchpoints are not supported"); |
| 2961 | return error; |
| 2962 | } |
| 2963 | |
| 2964 | StateType |
| 2965 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 2966 | { |
| 2967 | StateType state; |
| 2968 | // Now wait for the process to launch and return control to us, and then |
| 2969 | // call DidLaunch: |
| 2970 | while (1) |
| 2971 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2972 | event_sp.reset(); |
| 2973 | state = WaitForStateChangedEventsPrivate (timeout, event_sp); |
| 2974 | |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 2975 | if (StateIsStoppedState(state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2976 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2977 | |
| 2978 | // If state is invalid, then we timed out |
| 2979 | if (state == eStateInvalid) |
| 2980 | break; |
| 2981 | |
| 2982 | if (event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2983 | HandlePrivateEvent (event_sp); |
| 2984 | } |
| 2985 | return state; |
| 2986 | } |
| 2987 | |
| 2988 | Error |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2989 | Process::Launch (ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2990 | { |
| 2991 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2992 | m_abi_sp.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2993 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 2994 | m_jit_loaders_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2995 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2996 | m_os_ap.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2997 | m_process_input_reader.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2998 | |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 2999 | Module *exe_module = m_target.GetExecutableModulePointer(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3000 | if (exe_module) |
| 3001 | { |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 3002 | char local_exec_file_path[PATH_MAX]; |
| 3003 | char platform_exec_file_path[PATH_MAX]; |
| 3004 | exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path)); |
| 3005 | exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3006 | if (exe_module->GetFileSpec().Exists()) |
| 3007 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3008 | // Install anything that might need to be installed prior to launching. |
| 3009 | // For host systems, this will do nothing, but if we are connected to a |
| 3010 | // remote platform it will install any needed binaries |
| 3011 | error = GetTarget().Install(&launch_info); |
| 3012 | if (error.Fail()) |
| 3013 | return error; |
| 3014 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3015 | if (PrivateStateThreadIsValid ()) |
| 3016 | PausePrivateStateThread (); |
| 3017 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3018 | error = WillLaunch (exe_module); |
| 3019 | if (error.Success()) |
| 3020 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3021 | const bool restarted = false; |
| 3022 | SetPublicState (eStateLaunching, restarted); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 3023 | m_should_detach = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3024 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3025 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 69fd4be | 2012-09-04 20:29:05 +0000 | [diff] [blame] | 3026 | { |
| 3027 | // Now launch using these arguments. |
| 3028 | error = DoLaunch (exe_module, launch_info); |
| 3029 | } |
| 3030 | else |
| 3031 | { |
| 3032 | // This shouldn't happen |
| 3033 | error.SetErrorString("failed to acquire process run lock"); |
| 3034 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3035 | |
| 3036 | if (error.Fail()) |
| 3037 | { |
| 3038 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3039 | { |
| 3040 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3041 | const char *error_string = error.AsCString(); |
| 3042 | if (error_string == NULL) |
| 3043 | error_string = "launch failed"; |
| 3044 | SetExitStatus (-1, error_string); |
| 3045 | } |
| 3046 | } |
| 3047 | else |
| 3048 | { |
| 3049 | EventSP event_sp; |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 3050 | TimeValue timeout_time; |
| 3051 | timeout_time = TimeValue::Now(); |
| 3052 | timeout_time.OffsetWithSeconds(10); |
| 3053 | StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3054 | |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 3055 | if (state == eStateInvalid || event_sp.get() == NULL) |
| 3056 | { |
| 3057 | // We were able to launch the process, but we failed to |
| 3058 | // catch the initial stop. |
| 3059 | SetExitStatus (0, "failed to catch stop after launch"); |
| 3060 | Destroy(); |
| 3061 | } |
| 3062 | else if (state == eStateStopped || state == eStateCrashed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3063 | { |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3064 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3065 | DidLaunch (); |
| 3066 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3067 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3068 | if (dyld) |
| 3069 | dyld->DidLaunch(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3070 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 3071 | GetJITLoaders().DidLaunch(); |
| 3072 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3073 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3074 | if (system_runtime) |
| 3075 | system_runtime->DidLaunch(); |
| 3076 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3077 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3078 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 3079 | // a chance to complete... |
| 3080 | HandlePrivateEvent (event_sp); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3081 | |
| 3082 | if (PrivateStateThreadIsValid ()) |
| 3083 | ResumePrivateStateThread (); |
| 3084 | else |
| 3085 | StartPrivateStateThread (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3086 | } |
| 3087 | else if (state == eStateExited) |
| 3088 | { |
| 3089 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 3090 | // not likely to work, and return an invalid pid. |
| 3091 | HandlePrivateEvent (event_sp); |
| 3092 | } |
| 3093 | } |
| 3094 | } |
| 3095 | } |
| 3096 | else |
| 3097 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 3098 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3099 | } |
| 3100 | } |
| 3101 | return error; |
| 3102 | } |
| 3103 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3104 | |
| 3105 | Error |
| 3106 | Process::LoadCore () |
| 3107 | { |
| 3108 | Error error = DoLoadCore(); |
| 3109 | if (error.Success()) |
| 3110 | { |
| 3111 | if (PrivateStateThreadIsValid ()) |
| 3112 | ResumePrivateStateThread (); |
| 3113 | else |
| 3114 | StartPrivateStateThread (); |
| 3115 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3116 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3117 | if (dyld) |
| 3118 | dyld->DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 3119 | |
| 3120 | GetJITLoaders().DidAttach(); |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3121 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3122 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3123 | if (system_runtime) |
| 3124 | system_runtime->DidAttach(); |
| 3125 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3126 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3127 | // We successfully loaded a core file, now pretend we stopped so we can |
| 3128 | // show all of the threads in the core file and explore the crashed |
| 3129 | // state. |
| 3130 | SetPrivateState (eStateStopped); |
| 3131 | |
| 3132 | } |
| 3133 | return error; |
| 3134 | } |
| 3135 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3136 | DynamicLoader * |
| 3137 | Process::GetDynamicLoader () |
| 3138 | { |
| 3139 | if (m_dyld_ap.get() == NULL) |
| 3140 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
| 3141 | return m_dyld_ap.get(); |
| 3142 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3143 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 3144 | JITLoaderList & |
| 3145 | Process::GetJITLoaders () |
| 3146 | { |
| 3147 | if (!m_jit_loaders_ap) |
| 3148 | { |
| 3149 | m_jit_loaders_ap.reset(new JITLoaderList()); |
| 3150 | JITLoader::LoadPlugins(this, *m_jit_loaders_ap); |
| 3151 | } |
| 3152 | return *m_jit_loaders_ap; |
| 3153 | } |
| 3154 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3155 | SystemRuntime * |
| 3156 | Process::GetSystemRuntime () |
| 3157 | { |
| 3158 | if (m_system_runtime_ap.get() == NULL) |
| 3159 | m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this)); |
| 3160 | return m_system_runtime_ap.get(); |
| 3161 | } |
| 3162 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3163 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3164 | Process::NextEventAction::EventActionResult |
| 3165 | Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3166 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3167 | StateType state = ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 3168 | switch (state) |
Greg Clayton | 19388cf | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 3169 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3170 | case eStateRunning: |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3171 | case eStateConnected: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3172 | return eEventActionRetry; |
| 3173 | |
| 3174 | case eStateStopped: |
| 3175 | case eStateCrashed: |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3176 | { |
| 3177 | // During attach, prior to sending the eStateStopped event, |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3178 | // lldb_private::Process subclasses must set the new process ID. |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3179 | assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3180 | // We don't want these events to be reported, so go set the ShouldReportStop here: |
| 3181 | m_process->GetThreadList().SetShouldReportStop (eVoteNo); |
| 3182 | |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3183 | if (m_exec_count > 0) |
| 3184 | { |
| 3185 | --m_exec_count; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3186 | RequestResume(); |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3187 | return eEventActionRetry; |
| 3188 | } |
| 3189 | else |
| 3190 | { |
| 3191 | m_process->CompleteAttach (); |
| 3192 | return eEventActionSuccess; |
| 3193 | } |
| 3194 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3195 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3196 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3197 | default: |
| 3198 | case eStateExited: |
| 3199 | case eStateInvalid: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3200 | break; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3201 | } |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3202 | |
| 3203 | m_exit_string.assign ("No valid Process"); |
| 3204 | return eEventActionExit; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3205 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3206 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3207 | Process::NextEventAction::EventActionResult |
| 3208 | Process::AttachCompletionHandler::HandleBeingInterrupted() |
| 3209 | { |
| 3210 | return eEventActionSuccess; |
| 3211 | } |
| 3212 | |
| 3213 | const char * |
| 3214 | Process::AttachCompletionHandler::GetExitString () |
| 3215 | { |
| 3216 | return m_exit_string.c_str(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | Error |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3220 | Process::Attach (ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3221 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3222 | m_abi_sp.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3223 | m_process_input_reader.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3224 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 3225 | m_jit_loaders_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3226 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3227 | m_os_ap.reset(); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3228 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3229 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3230 | Error error; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3231 | if (attach_pid == LLDB_INVALID_PROCESS_ID) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3232 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3233 | char process_name[PATH_MAX]; |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3234 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3235 | if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name))) |
Jim Ingham | 2ecb742 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 3236 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3237 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 3238 | |
| 3239 | if (wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3240 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3241 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 3242 | if (error.Success()) |
| 3243 | { |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3244 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3245 | { |
| 3246 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3247 | const bool restarted = false; |
| 3248 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3249 | // Now attach using these arguments. |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 3250 | error = DoAttachToProcessWithName (process_name, attach_info); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3251 | } |
| 3252 | else |
| 3253 | { |
| 3254 | // This shouldn't happen |
| 3255 | error.SetErrorString("failed to acquire process run lock"); |
| 3256 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 3257 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3258 | if (error.Fail()) |
| 3259 | { |
| 3260 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3261 | { |
| 3262 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3263 | if (error.AsCString() == NULL) |
| 3264 | error.SetErrorString("attach failed"); |
| 3265 | |
| 3266 | SetExitStatus(-1, error.AsCString()); |
| 3267 | } |
| 3268 | } |
| 3269 | else |
| 3270 | { |
| 3271 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3272 | StartPrivateStateThread(); |
| 3273 | } |
| 3274 | return error; |
| 3275 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3276 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3277 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3278 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3279 | ProcessInstanceInfoList process_infos; |
| 3280 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3281 | |
| 3282 | if (platform_sp) |
| 3283 | { |
| 3284 | ProcessInstanceInfoMatch match_info; |
| 3285 | match_info.GetProcessInfo() = attach_info; |
| 3286 | match_info.SetNameMatchType (eNameMatchEquals); |
| 3287 | platform_sp->FindProcesses (match_info, process_infos); |
| 3288 | const uint32_t num_matches = process_infos.GetSize(); |
| 3289 | if (num_matches == 1) |
| 3290 | { |
| 3291 | attach_pid = process_infos.GetProcessIDAtIndex(0); |
| 3292 | // Fall through and attach using the above process ID |
| 3293 | } |
| 3294 | else |
| 3295 | { |
| 3296 | match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name)); |
| 3297 | if (num_matches > 1) |
| 3298 | error.SetErrorStringWithFormat ("more than one process named %s", process_name); |
| 3299 | else |
| 3300 | error.SetErrorStringWithFormat ("could not find a process named %s", process_name); |
| 3301 | } |
| 3302 | } |
| 3303 | else |
| 3304 | { |
| 3305 | error.SetErrorString ("invalid platform, can't find processes by name"); |
| 3306 | return error; |
| 3307 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3308 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3309 | } |
| 3310 | else |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3311 | { |
| 3312 | error.SetErrorString ("invalid process name"); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3313 | } |
| 3314 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3315 | |
| 3316 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3317 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3318 | error = WillAttachToProcessWithID(attach_pid); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3319 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3320 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3321 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3322 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3323 | { |
| 3324 | // Now attach using these arguments. |
| 3325 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3326 | const bool restarted = false; |
| 3327 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3328 | error = DoAttachToProcessWithID (attach_pid, attach_info); |
| 3329 | } |
| 3330 | else |
| 3331 | { |
| 3332 | // This shouldn't happen |
| 3333 | error.SetErrorString("failed to acquire process run lock"); |
| 3334 | } |
| 3335 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3336 | if (error.Success()) |
| 3337 | { |
| 3338 | |
| 3339 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3340 | StartPrivateStateThread(); |
| 3341 | } |
| 3342 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3343 | { |
| 3344 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3345 | { |
| 3346 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3347 | const char *error_string = error.AsCString(); |
| 3348 | if (error_string == NULL) |
| 3349 | error_string = "attach failed"; |
| 3350 | |
| 3351 | SetExitStatus(-1, error_string); |
| 3352 | } |
| 3353 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3354 | } |
| 3355 | } |
| 3356 | return error; |
| 3357 | } |
| 3358 | |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3359 | void |
| 3360 | Process::CompleteAttach () |
| 3361 | { |
| 3362 | // Let the process subclass figure out at much as it can about the process |
| 3363 | // before we go looking for a dynamic loader plug-in. |
| 3364 | DidAttach(); |
| 3365 | |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3366 | // We just attached. If we have a platform, ask it for the process architecture, and if it isn't |
| 3367 | // the same as the one we've already set, switch architectures. |
| 3368 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3369 | assert (platform_sp.get()); |
| 3370 | if (platform_sp) |
| 3371 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3372 | const ArchSpec &target_arch = m_target.GetArchitecture(); |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 3373 | if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3374 | { |
| 3375 | ArchSpec platform_arch; |
| 3376 | platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch); |
| 3377 | if (platform_sp) |
| 3378 | { |
| 3379 | m_target.SetPlatform (platform_sp); |
| 3380 | m_target.SetArchitecture(platform_arch); |
| 3381 | } |
| 3382 | } |
| 3383 | else |
| 3384 | { |
| 3385 | ProcessInstanceInfo process_info; |
| 3386 | platform_sp->GetProcessInfo (GetID(), process_info); |
| 3387 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 3388 | if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3389 | m_target.SetArchitecture (process_arch); |
| 3390 | } |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | // We have completed the attach, now it is time to find the dynamic loader |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3394 | // plug-in |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3395 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3396 | if (dyld) |
| 3397 | dyld->DidAttach(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3398 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 3399 | GetJITLoaders().DidAttach(); |
| 3400 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3401 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3402 | if (system_runtime) |
| 3403 | system_runtime->DidAttach(); |
| 3404 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3405 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3406 | // Figure out which one is the executable, and set that in our target: |
Enrico Granata | 1759848 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 3407 | const ModuleList &target_modules = m_target.GetImages(); |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3408 | Mutex::Locker modules_locker(target_modules.GetMutex()); |
| 3409 | size_t num_modules = target_modules.GetSize(); |
| 3410 | ModuleSP new_executable_module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3411 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 3412 | for (size_t i = 0; i < num_modules; i++) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3413 | { |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3414 | ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3415 | if (module_sp && module_sp->IsExecutable()) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3416 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 3417 | if (m_target.GetExecutableModulePointer() != module_sp.get()) |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3418 | new_executable_module_sp = module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | } |
| 3421 | } |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3422 | if (new_executable_module_sp) |
| 3423 | m_target.SetExecutableModule (new_executable_module_sp, false); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3426 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3427 | Process::ConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3428 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3429 | m_abi_sp.reset(); |
| 3430 | m_process_input_reader.reset(); |
| 3431 | |
| 3432 | // Find the process and its architecture. Make sure it matches the architecture |
| 3433 | // of the current Target, and if not adjust it. |
| 3434 | |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3435 | Error error (DoConnectRemote (strm, remote_url)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3436 | if (error.Success()) |
| 3437 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3438 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3439 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3440 | EventSP event_sp; |
| 3441 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 3442 | |
| 3443 | if (state == eStateStopped || state == eStateCrashed) |
| 3444 | { |
| 3445 | // If we attached and actually have a process on the other end, then |
| 3446 | // this ended up being the equivalent of an attach. |
| 3447 | CompleteAttach (); |
| 3448 | |
| 3449 | // This delays passing the stopped event to listeners till |
| 3450 | // CompleteAttach gets a chance to complete... |
| 3451 | HandlePrivateEvent (event_sp); |
| 3452 | |
| 3453 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3454 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3455 | |
| 3456 | if (PrivateStateThreadIsValid ()) |
| 3457 | ResumePrivateStateThread (); |
| 3458 | else |
| 3459 | StartPrivateStateThread (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3460 | } |
| 3461 | return error; |
| 3462 | } |
| 3463 | |
| 3464 | |
| 3465 | Error |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 3466 | Process::PrivateResume () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3467 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3468 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3469 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3470 | log->Printf("Process::PrivateResume() m_stop_id = %u, public state: %s private state: %s", |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3471 | m_mod_id.GetStopID(), |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3472 | StateAsCString(m_public_state.GetValue()), |
| 3473 | StateAsCString(m_private_state.GetValue())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3474 | |
| 3475 | Error error (WillResume()); |
| 3476 | // Tell the process it is about to resume before the thread list |
| 3477 | if (error.Success()) |
| 3478 | { |
Johnny Chen | c4221e4 | 2010-12-02 20:53:05 +0000 | [diff] [blame] | 3479 | // Now let the thread list know we are about to resume so it |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3480 | // can let all of our threads know that they are about to be |
| 3481 | // resumed. Threads will each be called with |
| 3482 | // Thread::WillResume(StateType) where StateType contains the state |
| 3483 | // that they are supposed to have when the process is resumed |
| 3484 | // (suspended/running/stepping). Threads should also check |
| 3485 | // their resume signal in lldb::Thread::GetResumeSignal() |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3486 | // to see if they are supposed to start back up with a signal. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3487 | if (m_thread_list.WillResume()) |
| 3488 | { |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3489 | // Last thing, do the PreResumeActions. |
| 3490 | if (!RunPreResumeActions()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3491 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3492 | error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming."); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3493 | } |
| 3494 | else |
| 3495 | { |
| 3496 | m_mod_id.BumpResumeID(); |
| 3497 | error = DoResume(); |
| 3498 | if (error.Success()) |
| 3499 | { |
| 3500 | DidResume(); |
| 3501 | m_thread_list.DidResume(); |
| 3502 | if (log) |
| 3503 | log->Printf ("Process thinks the process has resumed."); |
| 3504 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | else |
| 3508 | { |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 3509 | // Somebody wanted to run without running. So generate a continue & a stopped event, |
| 3510 | // and let the world handle them. |
| 3511 | if (log) |
| 3512 | log->Printf ("Process::PrivateResume() asked to simulate a start & stop."); |
| 3513 | |
| 3514 | SetPrivateState(eStateRunning); |
| 3515 | SetPrivateState(eStateStopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3516 | } |
| 3517 | } |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3518 | else if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3519 | log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>")); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3520 | return error; |
| 3521 | } |
| 3522 | |
| 3523 | Error |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3524 | Process::Halt (bool clear_thread_plans) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3525 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3526 | // Don't clear the m_clear_thread_plans_on_stop, only set it to true if |
| 3527 | // in case it was already set and some thread plan logic calls halt on its |
| 3528 | // own. |
| 3529 | m_clear_thread_plans_on_stop |= clear_thread_plans; |
| 3530 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3531 | // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since |
| 3532 | // we could just straightaway get another event. It just narrows the window... |
| 3533 | m_currently_handling_event.WaitForValueEqualTo(false); |
| 3534 | |
| 3535 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3536 | // Pause our private state thread so we can ensure no one else eats |
| 3537 | // the stop event out from under us. |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3538 | Listener halt_listener ("lldb.process.halt_listener"); |
| 3539 | HijackPrivateProcessEvents(&halt_listener); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3540 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3541 | EventSP event_sp; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3542 | Error error (WillHalt()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3543 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3544 | if (error.Success()) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3545 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3546 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3547 | bool caused_stop = false; |
| 3548 | |
| 3549 | // Ask the process subclass to actually halt our process |
| 3550 | error = DoHalt(caused_stop); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3551 | if (error.Success()) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3552 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3553 | if (m_public_state.GetValue() == eStateAttaching) |
| 3554 | { |
| 3555 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 3556 | Destroy (); |
| 3557 | } |
| 3558 | else |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3559 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3560 | // If "caused_stop" is true, then DoHalt stopped the process. If |
| 3561 | // "caused_stop" is false, the process was already stopped. |
| 3562 | // If the DoHalt caused the process to stop, then we want to catch |
| 3563 | // this event and set the interrupted bool to true before we pass |
| 3564 | // this along so clients know that the process was interrupted by |
| 3565 | // a halt command. |
| 3566 | if (caused_stop) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3567 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3568 | // Wait for 1 second for the process to stop. |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3569 | TimeValue timeout_time; |
| 3570 | timeout_time = TimeValue::Now(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 3571 | timeout_time.OffsetWithSeconds(10); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3572 | bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp); |
| 3573 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3574 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3575 | if (!got_event || state == eStateInvalid) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3576 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3577 | // We timeout out and didn't get a stop event... |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3578 | error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState())); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3579 | } |
| 3580 | else |
| 3581 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 3582 | if (StateIsStoppedState (state, false)) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3583 | { |
| 3584 | // We caused the process to interrupt itself, so mark this |
| 3585 | // as such in the stop event so clients can tell an interrupted |
| 3586 | // process from a natural stop |
| 3587 | ProcessEventData::SetInterruptedInEvent (event_sp.get(), true); |
| 3588 | } |
| 3589 | else |
| 3590 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3591 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3592 | if (log) |
| 3593 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 3594 | error.SetErrorString ("Did not get stopped event after halt."); |
| 3595 | } |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3596 | } |
| 3597 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3598 | DidHalt(); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3599 | } |
| 3600 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3601 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3602 | // Resume our private state thread before we post the event (if any) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3603 | RestorePrivateProcessEvents(); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3604 | |
| 3605 | // Post any event we might have consumed. If all goes well, we will have |
| 3606 | // stopped the process, intercepted the event and set the interrupted |
| 3607 | // bool in the event. Post it to the private event queue and that will end up |
| 3608 | // correctly setting the state. |
| 3609 | if (event_sp) |
| 3610 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 3611 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3612 | return error; |
| 3613 | } |
| 3614 | |
| 3615 | Error |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3616 | Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp) |
| 3617 | { |
| 3618 | Error error; |
| 3619 | if (m_public_state.GetValue() == eStateRunning) |
| 3620 | { |
| 3621 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3622 | if (log) |
| 3623 | log->Printf("Process::Destroy() About to halt."); |
| 3624 | error = Halt(); |
| 3625 | if (error.Success()) |
| 3626 | { |
| 3627 | // Consume the halt event. |
| 3628 | TimeValue timeout (TimeValue::Now()); |
| 3629 | timeout.OffsetWithSeconds(1); |
| 3630 | StateType state = WaitForProcessToStop (&timeout, &exit_event_sp); |
| 3631 | |
| 3632 | // If the process exited while we were waiting for it to stop, put the exited event into |
| 3633 | // the shared pointer passed in and return. Our caller doesn't need to do anything else, since |
| 3634 | // they don't have a process anymore... |
| 3635 | |
| 3636 | if (state == eStateExited || m_private_state.GetValue() == eStateExited) |
| 3637 | { |
| 3638 | if (log) |
| 3639 | log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt."); |
| 3640 | return error; |
| 3641 | } |
| 3642 | else |
| 3643 | exit_event_sp.reset(); // It is ok to consume any non-exit stop events |
| 3644 | |
| 3645 | if (state != eStateStopped) |
| 3646 | { |
| 3647 | if (log) |
| 3648 | log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state)); |
| 3649 | // If we really couldn't stop the process then we should just error out here, but if the |
| 3650 | // lower levels just bobbled sending the event and we really are stopped, then continue on. |
| 3651 | StateType private_state = m_private_state.GetValue(); |
| 3652 | if (private_state != eStateStopped) |
| 3653 | { |
| 3654 | return error; |
| 3655 | } |
| 3656 | } |
| 3657 | } |
| 3658 | else |
| 3659 | { |
| 3660 | if (log) |
| 3661 | log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString()); |
| 3662 | } |
| 3663 | } |
| 3664 | return error; |
| 3665 | } |
| 3666 | |
| 3667 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3668 | Process::Detach (bool keep_stopped) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3669 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3670 | EventSP exit_event_sp; |
| 3671 | Error error; |
| 3672 | m_destroy_in_process = true; |
| 3673 | |
| 3674 | error = WillDetach(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3675 | |
| 3676 | if (error.Success()) |
| 3677 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3678 | if (DetachRequiresHalt()) |
| 3679 | { |
| 3680 | error = HaltForDestroyOrDetach (exit_event_sp); |
| 3681 | if (!error.Success()) |
| 3682 | { |
| 3683 | m_destroy_in_process = false; |
| 3684 | return error; |
| 3685 | } |
| 3686 | else if (exit_event_sp) |
| 3687 | { |
| 3688 | // We shouldn't need to do anything else here. There's no process left to detach from... |
| 3689 | StopPrivateStateThread(); |
| 3690 | m_destroy_in_process = false; |
| 3691 | return error; |
| 3692 | } |
| 3693 | } |
| 3694 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3695 | error = DoDetach(keep_stopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3696 | if (error.Success()) |
| 3697 | { |
| 3698 | DidDetach(); |
| 3699 | StopPrivateStateThread(); |
| 3700 | } |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3701 | else |
| 3702 | { |
| 3703 | return error; |
| 3704 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3705 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3706 | m_destroy_in_process = false; |
| 3707 | |
| 3708 | // If we exited when we were waiting for a process to stop, then |
| 3709 | // forward the event here so we don't lose the event |
| 3710 | if (exit_event_sp) |
| 3711 | { |
| 3712 | // Directly broadcast our exited event because we shut down our |
| 3713 | // private state thread above |
| 3714 | BroadcastEvent(exit_event_sp); |
| 3715 | } |
| 3716 | |
| 3717 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3718 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3719 | // it here so when we do to tear down the process we don't get an error destroying the lock. |
| 3720 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3721 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3722 | return error; |
| 3723 | } |
| 3724 | |
| 3725 | Error |
| 3726 | Process::Destroy () |
| 3727 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3728 | |
| 3729 | // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work |
| 3730 | // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt |
| 3731 | // failed and the process stays around for some reason it won't be in a confused state. |
| 3732 | |
| 3733 | m_destroy_in_process = true; |
| 3734 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3735 | Error error (WillDestroy()); |
| 3736 | if (error.Success()) |
| 3737 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3738 | EventSP exit_event_sp; |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3739 | if (DestroyRequiresHalt()) |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3740 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3741 | error = HaltForDestroyOrDetach(exit_event_sp); |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3742 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3743 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3744 | if (m_public_state.GetValue() != eStateRunning) |
| 3745 | { |
| 3746 | // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to |
| 3747 | // kill it, we don't want it hitting a breakpoint... |
| 3748 | // Only do this if we've stopped, however, since if we didn't manage to halt it above, then |
| 3749 | // we're not going to have much luck doing this now. |
| 3750 | m_thread_list.DiscardThreadPlans(); |
| 3751 | DisableAllBreakpointSites(); |
| 3752 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3753 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3754 | error = DoDestroy(); |
| 3755 | if (error.Success()) |
| 3756 | { |
| 3757 | DidDestroy(); |
| 3758 | StopPrivateStateThread(); |
| 3759 | } |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3760 | m_stdio_communication.StopReadThread(); |
| 3761 | m_stdio_communication.Disconnect(); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3762 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3763 | if (m_process_input_reader) |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3764 | { |
| 3765 | m_process_input_reader->SetIsDone(true); |
| 3766 | m_process_input_reader->Cancel(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3767 | m_process_input_reader.reset(); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3770 | // If we exited when we were waiting for a process to stop, then |
| 3771 | // forward the event here so we don't lose the event |
| 3772 | if (exit_event_sp) |
| 3773 | { |
| 3774 | // Directly broadcast our exited event because we shut down our |
| 3775 | // private state thread above |
| 3776 | BroadcastEvent(exit_event_sp); |
| 3777 | } |
| 3778 | |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3779 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3780 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3781 | // it here so when we do to tear down the process we don't get an error destroying the lock. |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3782 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3783 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3784 | |
| 3785 | m_destroy_in_process = false; |
| 3786 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3787 | return error; |
| 3788 | } |
| 3789 | |
| 3790 | Error |
| 3791 | Process::Signal (int signal) |
| 3792 | { |
| 3793 | Error error (WillSignal()); |
| 3794 | if (error.Success()) |
| 3795 | { |
| 3796 | error = DoSignal(signal); |
| 3797 | if (error.Success()) |
| 3798 | DidSignal(); |
| 3799 | } |
| 3800 | return error; |
| 3801 | } |
| 3802 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3803 | lldb::ByteOrder |
| 3804 | Process::GetByteOrder () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3805 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3806 | return m_target.GetArchitecture().GetByteOrder(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | uint32_t |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3810 | Process::GetAddressByteSize () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3811 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3812 | return m_target.GetArchitecture().GetAddressByteSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3815 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3816 | bool |
| 3817 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 3818 | { |
| 3819 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 3820 | bool return_value = true; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3821 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3822 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3823 | switch (state) |
| 3824 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3825 | case eStateConnected: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3826 | case eStateAttaching: |
| 3827 | case eStateLaunching: |
| 3828 | case eStateDetached: |
| 3829 | case eStateExited: |
| 3830 | case eStateUnloaded: |
| 3831 | // These events indicate changes in the state of the debugging session, always report them. |
| 3832 | return_value = true; |
| 3833 | break; |
| 3834 | case eStateInvalid: |
| 3835 | // We stopped for no apparent reason, don't report it. |
| 3836 | return_value = false; |
| 3837 | break; |
| 3838 | case eStateRunning: |
| 3839 | case eStateStepping: |
| 3840 | // If we've started the target running, we handle the cases where we |
| 3841 | // are already running and where there is a transition from stopped to |
| 3842 | // running differently. |
| 3843 | // running -> running: Automatically suppress extra running events |
| 3844 | // stopped -> running: Report except when there is one or more no votes |
| 3845 | // and no yes votes. |
| 3846 | SynchronouslyNotifyStateChanged (state); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3847 | if (m_force_next_event_delivery) |
| 3848 | return_value = true; |
| 3849 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3850 | { |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3851 | switch (m_last_broadcast_state) |
| 3852 | { |
| 3853 | case eStateRunning: |
| 3854 | case eStateStepping: |
| 3855 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 3856 | return_value = false; |
| 3857 | break; |
| 3858 | default: |
| 3859 | // TODO: make this work correctly. For now always report |
| 3860 | // run if we aren't running so we don't miss any runnning |
| 3861 | // events. If I run the lldb/test/thread/a.out file and |
| 3862 | // break at main.cpp:58, run and hit the breakpoints on |
| 3863 | // multiple threads, then somehow during the stepping over |
| 3864 | // of all breakpoints no run gets reported. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3865 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3866 | // This is a transition from stop to run. |
| 3867 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 3868 | { |
| 3869 | case eVoteYes: |
| 3870 | case eVoteNoOpinion: |
| 3871 | return_value = true; |
| 3872 | break; |
| 3873 | case eVoteNo: |
| 3874 | return_value = false; |
| 3875 | break; |
| 3876 | } |
| 3877 | break; |
| 3878 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3879 | } |
| 3880 | break; |
| 3881 | case eStateStopped: |
| 3882 | case eStateCrashed: |
| 3883 | case eStateSuspended: |
| 3884 | { |
| 3885 | // We've stopped. First see if we're going to restart the target. |
| 3886 | // If we are going to stop, then we always broadcast the event. |
| 3887 | // If we aren't going to stop, let the thread plans decide if we're going to report this event. |
Jim Ingham | b01e742 | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 3888 | // If no thread has an opinion, we don't report it. |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3889 | |
Jim Ingham | cb4ca11 | 2012-05-16 01:32:14 +0000 | [diff] [blame] | 3890 | RefreshStateAfterStop (); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3891 | if (ProcessEventData::GetInterruptedFromEvent (event_ptr)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3892 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3893 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3894 | log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", |
| 3895 | event_ptr, |
| 3896 | StateAsCString(state)); |
| 3897 | return_value = true; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3898 | } |
| 3899 | else |
| 3900 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3901 | bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr); |
| 3902 | bool should_resume = false; |
| 3903 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3904 | // It makes no sense to ask "ShouldStop" if we've already been restarted... |
| 3905 | // Asking the thread list is also not likely to go well, since we are running again. |
| 3906 | // So in that case just report the event. |
| 3907 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3908 | if (!was_restarted) |
| 3909 | should_resume = m_thread_list.ShouldStop (event_ptr) == false; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3910 | |
| 3911 | if (was_restarted || should_resume || m_resume_requested) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3912 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3913 | Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr); |
| 3914 | if (log) |
| 3915 | log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.", |
| 3916 | should_resume, |
| 3917 | StateAsCString(state), |
| 3918 | was_restarted, |
| 3919 | stop_vote); |
| 3920 | |
| 3921 | switch (stop_vote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3922 | { |
| 3923 | case eVoteYes: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3924 | return_value = true; |
| 3925 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3926 | case eVoteNoOpinion: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3927 | case eVoteNo: |
| 3928 | return_value = false; |
| 3929 | break; |
| 3930 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3931 | |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 3932 | if (!was_restarted) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3933 | { |
| 3934 | if (log) |
| 3935 | log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state)); |
| 3936 | ProcessEventData::SetRestartedInEvent(event_ptr, true); |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 3937 | PrivateResume (); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3940 | } |
| 3941 | else |
| 3942 | { |
| 3943 | return_value = true; |
| 3944 | SynchronouslyNotifyStateChanged (state); |
| 3945 | } |
| 3946 | } |
| 3947 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3948 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3949 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3950 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3951 | // Forcing the next event delivery is a one shot deal. So reset it here. |
| 3952 | m_force_next_event_delivery = false; |
| 3953 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3954 | // We do some coalescing of events (for instance two consecutive running events get coalesced.) |
| 3955 | // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state |
| 3956 | // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done, |
| 3957 | // because the PublicState reflects the last event pulled off the queue, and there may be several |
| 3958 | // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event |
| 3959 | // yet. m_last_broadcast_state gets updated here. |
| 3960 | |
| 3961 | if (return_value) |
| 3962 | m_last_broadcast_state = state; |
| 3963 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3964 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3965 | log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s", |
| 3966 | event_ptr, |
| 3967 | StateAsCString(state), |
| 3968 | StateAsCString(m_last_broadcast_state), |
| 3969 | return_value ? "YES" : "NO"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3970 | return return_value; |
| 3971 | } |
| 3972 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3973 | |
| 3974 | bool |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3975 | Process::StartPrivateStateThread (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3976 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3977 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3978 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3979 | bool already_running = PrivateStateThreadIsValid (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3980 | if (log) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3981 | log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread"); |
| 3982 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3983 | if (!force && already_running) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3984 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3985 | |
| 3986 | // Create a thread that watches our internal state and controls which |
| 3987 | // events make it to clients (into the DCProcess event queue). |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 3988 | char thread_name[1024]; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3989 | if (already_running) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3990 | snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID()); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3991 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3992 | snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID()); |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3993 | |
| 3994 | // Create the private state thread, and start it running. |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 3995 | m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL); |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3996 | bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread); |
| 3997 | if (success) |
| 3998 | { |
| 3999 | ResumePrivateStateThread(); |
| 4000 | return true; |
| 4001 | } |
| 4002 | else |
| 4003 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | void |
| 4007 | Process::PausePrivateStateThread () |
| 4008 | { |
| 4009 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 4010 | } |
| 4011 | |
| 4012 | void |
| 4013 | Process::ResumePrivateStateThread () |
| 4014 | { |
| 4015 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 4016 | } |
| 4017 | |
| 4018 | void |
| 4019 | Process::StopPrivateStateThread () |
| 4020 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4021 | if (PrivateStateThreadIsValid ()) |
| 4022 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4023 | else |
| 4024 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4025 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4026 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4027 | log->Printf ("Went to stop the private state thread, but it was already invalid."); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4028 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | void |
| 4032 | Process::ControlPrivateStateThread (uint32_t signal) |
| 4033 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4034 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4035 | |
| 4036 | assert (signal == eBroadcastInternalStateControlStop || |
| 4037 | signal == eBroadcastInternalStateControlPause || |
| 4038 | signal == eBroadcastInternalStateControlResume); |
| 4039 | |
| 4040 | if (log) |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 4041 | log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4042 | |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 4043 | // Signal the private state thread. First we should copy this is case the |
| 4044 | // thread starts exiting since the private state thread will NULL this out |
| 4045 | // when it exits |
| 4046 | const lldb::thread_t private_state_thread = m_private_state_thread; |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 4047 | if (IS_VALID_LLDB_HOST_THREAD(private_state_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4048 | { |
| 4049 | TimeValue timeout_time; |
| 4050 | bool timed_out; |
| 4051 | |
| 4052 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 4053 | |
| 4054 | timeout_time = TimeValue::Now(); |
| 4055 | timeout_time.OffsetWithSeconds(2); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4056 | if (log) |
| 4057 | log->Printf ("Sending control event of type: %d.", signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4058 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 4059 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 4060 | |
| 4061 | if (signal == eBroadcastInternalStateControlStop) |
| 4062 | { |
| 4063 | if (timed_out) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4064 | { |
| 4065 | Error error; |
| 4066 | Host::ThreadCancel (private_state_thread, &error); |
| 4067 | if (log) |
| 4068 | log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString()); |
| 4069 | } |
| 4070 | else |
| 4071 | { |
| 4072 | if (log) |
| 4073 | log->Printf ("The control event killed the private state thread without having to cancel."); |
| 4074 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4075 | |
| 4076 | thread_result_t result = NULL; |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 4077 | Host::ThreadJoin (private_state_thread, &result, NULL); |
Greg Clayton | 49182ed | 2010-07-22 18:34:21 +0000 | [diff] [blame] | 4078 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4079 | } |
| 4080 | } |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4081 | else |
| 4082 | { |
| 4083 | if (log) |
| 4084 | log->Printf ("Private state thread already dead, no need to signal it to stop."); |
| 4085 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | void |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4089 | Process::SendAsyncInterrupt () |
| 4090 | { |
| 4091 | if (PrivateStateThreadIsValid()) |
| 4092 | m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 4093 | else |
| 4094 | BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 4095 | } |
| 4096 | |
| 4097 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4098 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 4099 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4100 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4101 | m_resume_requested = false; |
| 4102 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 4103 | m_currently_handling_event.SetValue(true, eBroadcastNever); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4104 | |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4105 | const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4106 | |
| 4107 | // First check to see if anybody wants a shot at this event: |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4108 | if (m_next_event_action_ap.get() != NULL) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4109 | { |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4110 | NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4111 | if (log) |
| 4112 | log->Printf ("Ran next event action, result was %d.", action_result); |
| 4113 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4114 | switch (action_result) |
| 4115 | { |
| 4116 | case NextEventAction::eEventActionSuccess: |
| 4117 | SetNextEventAction(NULL); |
| 4118 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 4119 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4120 | case NextEventAction::eEventActionRetry: |
| 4121 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 4122 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4123 | case NextEventAction::eEventActionExit: |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 4124 | // Handle Exiting Here. If we already got an exited event, |
| 4125 | // we should just propagate it. Otherwise, swallow this event, |
| 4126 | // and set our state to exit so the next event will kill us. |
| 4127 | if (new_state != eStateExited) |
| 4128 | { |
| 4129 | // FIXME: should cons up an exited event, and discard this one. |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4130 | SetExitStatus(0, m_next_event_action_ap->GetExitString()); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4131 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 4132 | SetNextEventAction(NULL); |
| 4133 | return; |
| 4134 | } |
| 4135 | SetNextEventAction(NULL); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4136 | break; |
| 4137 | } |
| 4138 | } |
| 4139 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4140 | // See if we should broadcast this state to external clients? |
| 4141 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4142 | |
| 4143 | if (should_broadcast) |
| 4144 | { |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4145 | const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4146 | if (log) |
| 4147 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4148 | log->Printf ("Process::%s (pid = %" PRIu64 ") broadcasting new state %s (old state %s) to %s", |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4149 | __FUNCTION__, |
| 4150 | GetID(), |
| 4151 | StateAsCString(new_state), |
| 4152 | StateAsCString (GetState ()), |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4153 | is_hijacked ? "hijacked" : "public"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4154 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4155 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4156 | if (StateIsRunningState (new_state)) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4157 | { |
| 4158 | // Only push the input handler if we aren't fowarding events, |
| 4159 | // as this means the curses GUI is in use... |
| 4160 | if (!GetTarget().GetDebugger().IsForwardingEvents()) |
| 4161 | PushProcessIOHandler (); |
| 4162 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4163 | else if (StateIsStoppedState(new_state, false)) |
| 4164 | { |
| 4165 | if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 4166 | { |
| 4167 | // If the lldb_private::Debugger is handling the events, we don't |
| 4168 | // want to pop the process IOHandler here, we want to do it when |
| 4169 | // we receive the stopped event so we can carefully control when |
| 4170 | // the process IOHandler is popped because when we stop we want to |
| 4171 | // display some text stating how and why we stopped, then maybe some |
| 4172 | // process/thread/frame info, and then we want the "(lldb) " prompt |
| 4173 | // to show up. If we pop the process IOHandler here, then we will |
| 4174 | // cause the command interpreter to become the top IOHandler after |
| 4175 | // the process pops off and it will update its prompt right away... |
| 4176 | // See the Debugger.cpp file where it calls the function as |
| 4177 | // "process_sp->PopProcessIOHandler()" to see where I am talking about. |
| 4178 | // Otherwise we end up getting overlapping "(lldb) " prompts and |
| 4179 | // garbled output. |
| 4180 | // |
| 4181 | // If we aren't handling the events in the debugger (which is indicated |
| 4182 | // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or we |
| 4183 | // are hijacked, then we always pop the process IO handler manually. |
| 4184 | // Hijacking happens when the internal process state thread is running |
| 4185 | // thread plans, or when commands want to run in synchronous mode |
| 4186 | // and they call "process->WaitForProcessToStop()". An example of something |
| 4187 | // that will hijack the events is a simple expression: |
| 4188 | // |
| 4189 | // (lldb) expr (int)puts("hello") |
| 4190 | // |
| 4191 | // This will cause the internal process state thread to resume and halt |
| 4192 | // the process (and _it_ will hijack the eBroadcastBitStateChanged |
| 4193 | // events) and we do need the IO handler to be pushed and popped |
| 4194 | // correctly. |
| 4195 | |
| 4196 | if (is_hijacked || m_target.GetDebugger().IsHandlingEvents() == false) |
| 4197 | PopProcessIOHandler (); |
| 4198 | } |
| 4199 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4200 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4201 | BroadcastEvent (event_sp); |
| 4202 | } |
| 4203 | else |
| 4204 | { |
| 4205 | if (log) |
| 4206 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4207 | log->Printf ("Process::%s (pid = %" PRIu64 ") suppressing state %s (old state %s): should_broadcast == false", |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4208 | __FUNCTION__, |
| 4209 | GetID(), |
| 4210 | StateAsCString(new_state), |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 4211 | StateAsCString (GetState ())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4212 | } |
| 4213 | } |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 4214 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4217 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4218 | Process::PrivateStateThread (void *arg) |
| 4219 | { |
| 4220 | Process *proc = static_cast<Process*> (arg); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4221 | thread_result_t result = proc->RunPrivateStateThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4222 | return result; |
| 4223 | } |
| 4224 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4225 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4226 | Process::RunPrivateStateThread () |
| 4227 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4228 | bool control_only = true; |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4229 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4230 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4231 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4232 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4233 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, this, GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4234 | |
| 4235 | bool exit_now = false; |
| 4236 | while (!exit_now) |
| 4237 | { |
| 4238 | EventSP event_sp; |
| 4239 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 4240 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 4241 | { |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4242 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4243 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType()); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4244 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4245 | switch (event_sp->GetType()) |
| 4246 | { |
| 4247 | case eBroadcastInternalStateControlStop: |
| 4248 | exit_now = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4249 | break; // doing any internal state managment below |
| 4250 | |
| 4251 | case eBroadcastInternalStateControlPause: |
| 4252 | control_only = true; |
| 4253 | break; |
| 4254 | |
| 4255 | case eBroadcastInternalStateControlResume: |
| 4256 | control_only = false; |
| 4257 | break; |
| 4258 | } |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4259 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4260 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4261 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4262 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4263 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
| 4264 | { |
| 4265 | if (m_public_state.GetValue() == eStateAttaching) |
| 4266 | { |
| 4267 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4268 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.", __FUNCTION__, this, GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4269 | BroadcastEvent (eBroadcastBitInterrupt, NULL); |
| 4270 | } |
| 4271 | else |
| 4272 | { |
| 4273 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4274 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.", __FUNCTION__, this, GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4275 | Halt(); |
| 4276 | } |
| 4277 | continue; |
| 4278 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4279 | |
| 4280 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4281 | |
| 4282 | if (internal_state != eStateInvalid) |
| 4283 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 4284 | if (m_clear_thread_plans_on_stop && |
| 4285 | StateIsStoppedState(internal_state, true)) |
| 4286 | { |
| 4287 | m_clear_thread_plans_on_stop = false; |
| 4288 | m_thread_list.DiscardThreadPlans(); |
| 4289 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4290 | HandlePrivateEvent (event_sp); |
| 4291 | } |
| 4292 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 4293 | if (internal_state == eStateInvalid || |
| 4294 | internal_state == eStateExited || |
| 4295 | internal_state == eStateDetached ) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4296 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4297 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4298 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state)); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4299 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4300 | break; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4301 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 4304 | // Verify log is still enabled before attempting to write to it... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4305 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4306 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, this, GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4307 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 4308 | m_public_run_lock.SetStopped(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 4309 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
| 4310 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4311 | return NULL; |
| 4312 | } |
| 4313 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4314 | //------------------------------------------------------------------ |
| 4315 | // Process Event Data |
| 4316 | //------------------------------------------------------------------ |
| 4317 | |
| 4318 | Process::ProcessEventData::ProcessEventData () : |
| 4319 | EventData (), |
| 4320 | m_process_sp (), |
| 4321 | m_state (eStateInvalid), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4322 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4323 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4324 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4325 | { |
| 4326 | } |
| 4327 | |
| 4328 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 4329 | EventData (), |
| 4330 | m_process_sp (process_sp), |
| 4331 | m_state (state), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4332 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4333 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4334 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4335 | { |
| 4336 | } |
| 4337 | |
| 4338 | Process::ProcessEventData::~ProcessEventData() |
| 4339 | { |
| 4340 | } |
| 4341 | |
| 4342 | const ConstString & |
| 4343 | Process::ProcessEventData::GetFlavorString () |
| 4344 | { |
| 4345 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 4346 | return g_flavor; |
| 4347 | } |
| 4348 | |
| 4349 | const ConstString & |
| 4350 | Process::ProcessEventData::GetFlavor () const |
| 4351 | { |
| 4352 | return ProcessEventData::GetFlavorString (); |
| 4353 | } |
| 4354 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4355 | void |
| 4356 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 4357 | { |
| 4358 | // This function gets called twice for each event, once when the event gets pulled |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4359 | // off of the private process event queue, and then any number of times, first when it gets pulled off of |
| 4360 | // the public event queue, then other times when we're pretending that this is where we stopped at the |
| 4361 | // end of expression evaluation. m_update_state is used to distinguish these |
| 4362 | // three cases; it is 0 when we're just pulling it off for private handling, |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4363 | // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then. |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4364 | if (m_update_state != 1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4365 | return; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4366 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4367 | m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4368 | |
| 4369 | // If we're stopped and haven't restarted, then do the breakpoint commands here: |
| 4370 | if (m_state == eStateStopped && ! m_restarted) |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4371 | { |
| 4372 | ThreadList &curr_thread_list = m_process_sp->GetThreadList(); |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4373 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 4374 | uint32_t idx; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 4375 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4376 | // The actions might change one of the thread's stop_info's opinions about whether we should |
| 4377 | // stop the process, so we need to query that as we go. |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4378 | |
| 4379 | // One other complication here, is that we try to catch any case where the target has run (except for expressions) |
| 4380 | // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and |
| 4381 | // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like |
| 4382 | // 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 |
| 4383 | // against this list & bag out if anything differs. |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4384 | std::vector<uint32_t> thread_index_array(num_threads); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4385 | for (idx = 0; idx < num_threads; ++idx) |
| 4386 | thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID(); |
| 4387 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4388 | // Use this to track whether we should continue from here. We will only continue the target running if |
| 4389 | // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running, |
| 4390 | // then it doesn't matter what the other threads say... |
| 4391 | |
| 4392 | bool still_should_stop = false; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4393 | |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4394 | // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a |
| 4395 | // valid stop reason. In that case we should just stop, because we have no way of telling what the right |
| 4396 | // thing to do is, and it's better to let the user decide than continue behind their backs. |
| 4397 | |
| 4398 | bool does_anybody_have_an_opinion = false; |
| 4399 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4400 | for (idx = 0; idx < num_threads; ++idx) |
| 4401 | { |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4402 | curr_thread_list = m_process_sp->GetThreadList(); |
| 4403 | if (curr_thread_list.GetSize() != num_threads) |
| 4404 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4405 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4406 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4407 | log->Printf("Number of threads changed from %u to %u while processing event.", num_threads, curr_thread_list.GetSize()); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | } |
| 4410 | |
| 4411 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 4412 | |
| 4413 | if (thread_sp->GetIndexID() != thread_index_array[idx]) |
| 4414 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4415 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4416 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4417 | log->Printf("The thread at position %u changed from %u to %u while processing event.", |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4418 | idx, |
| 4419 | thread_index_array[idx], |
| 4420 | thread_sp->GetIndexID()); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4421 | break; |
| 4422 | } |
| 4423 | |
Jim Ingham | b15bfc7 | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 4424 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo (); |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 4425 | if (stop_info_sp && stop_info_sp->IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4426 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4427 | does_anybody_have_an_opinion = true; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4428 | bool this_thread_wants_to_stop; |
| 4429 | if (stop_info_sp->GetOverrideShouldStop()) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4430 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4431 | this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue(); |
| 4432 | } |
| 4433 | else |
| 4434 | { |
| 4435 | stop_info_sp->PerformAction(event_ptr); |
| 4436 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 4437 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 4438 | // that state appropriately. |
| 4439 | // We also need to stop processing actions, since they aren't expecting the target to be running. |
| 4440 | |
| 4441 | // FIXME: we might have run. |
| 4442 | if (stop_info_sp->HasTargetRunSinceMe()) |
| 4443 | { |
| 4444 | SetRestarted (true); |
| 4445 | break; |
| 4446 | } |
| 4447 | |
| 4448 | this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4449 | } |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4450 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4451 | if (still_should_stop == false) |
| 4452 | still_should_stop = this_thread_wants_to_stop; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4453 | } |
| 4454 | } |
Jim Ingham | 3ebcf7f | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 4455 | |
Ashok Thirumurthi | cf7c55e | 2013-04-18 14:38:20 +0000 | [diff] [blame] | 4456 | |
Jim Ingham | a8ca6e2 | 2013-05-03 23:04:37 +0000 | [diff] [blame] | 4457 | if (!GetRestarted()) |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4458 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4459 | if (!still_should_stop && does_anybody_have_an_opinion) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4460 | { |
| 4461 | // We've been asked to continue, so do that here. |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4462 | SetRestarted(true); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 4463 | // Use the public resume method here, since this is just |
| 4464 | // extending a public resume. |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4465 | m_process_sp->PrivateResume(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4466 | } |
| 4467 | else |
| 4468 | { |
| 4469 | // If we didn't restart, run the Stop Hooks here: |
| 4470 | // They might also restart the target, so watch for that. |
| 4471 | m_process_sp->GetTarget().RunStopHooks(); |
| 4472 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 4473 | SetRestarted(true); |
| 4474 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4475 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4476 | } |
| 4477 | } |
| 4478 | |
| 4479 | void |
| 4480 | Process::ProcessEventData::Dump (Stream *s) const |
| 4481 | { |
| 4482 | if (m_process_sp) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4483 | s->Printf(" process = %p (pid = %" PRIu64 "), ", m_process_sp.get(), m_process_sp->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4484 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4485 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | const Process::ProcessEventData * |
| 4489 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 4490 | { |
| 4491 | if (event_ptr) |
| 4492 | { |
| 4493 | const EventData *event_data = event_ptr->GetData(); |
| 4494 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 4495 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 4496 | } |
| 4497 | return NULL; |
| 4498 | } |
| 4499 | |
| 4500 | ProcessSP |
| 4501 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 4502 | { |
| 4503 | ProcessSP process_sp; |
| 4504 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4505 | if (data) |
| 4506 | process_sp = data->GetProcessSP(); |
| 4507 | return process_sp; |
| 4508 | } |
| 4509 | |
| 4510 | StateType |
| 4511 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 4512 | { |
| 4513 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4514 | if (data == NULL) |
| 4515 | return eStateInvalid; |
| 4516 | else |
| 4517 | return data->GetState(); |
| 4518 | } |
| 4519 | |
| 4520 | bool |
| 4521 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 4522 | { |
| 4523 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4524 | if (data == NULL) |
| 4525 | return false; |
| 4526 | else |
| 4527 | return data->GetRestarted(); |
| 4528 | } |
| 4529 | |
| 4530 | void |
| 4531 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 4532 | { |
| 4533 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4534 | if (data != NULL) |
| 4535 | data->SetRestarted(new_value); |
| 4536 | } |
| 4537 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4538 | size_t |
| 4539 | Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr) |
| 4540 | { |
| 4541 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4542 | if (data != NULL) |
| 4543 | return data->GetNumRestartedReasons(); |
| 4544 | else |
| 4545 | return 0; |
| 4546 | } |
| 4547 | |
| 4548 | const char * |
| 4549 | Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx) |
| 4550 | { |
| 4551 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4552 | if (data != NULL) |
| 4553 | return data->GetRestartedReasonAtIndex(idx); |
| 4554 | else |
| 4555 | return NULL; |
| 4556 | } |
| 4557 | |
| 4558 | void |
| 4559 | Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason) |
| 4560 | { |
| 4561 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4562 | if (data != NULL) |
| 4563 | data->AddRestartedReason(reason); |
| 4564 | } |
| 4565 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4566 | bool |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4567 | Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr) |
| 4568 | { |
| 4569 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4570 | if (data == NULL) |
| 4571 | return false; |
| 4572 | else |
| 4573 | return data->GetInterrupted (); |
| 4574 | } |
| 4575 | |
| 4576 | void |
| 4577 | Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value) |
| 4578 | { |
| 4579 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4580 | if (data != NULL) |
| 4581 | data->SetInterrupted(new_value); |
| 4582 | } |
| 4583 | |
| 4584 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4585 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 4586 | { |
| 4587 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4588 | if (data) |
| 4589 | { |
| 4590 | data->SetUpdateStateOnRemoval(); |
| 4591 | return true; |
| 4592 | } |
| 4593 | return false; |
| 4594 | } |
| 4595 | |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 4596 | lldb::TargetSP |
| 4597 | Process::CalculateTarget () |
| 4598 | { |
| 4599 | return m_target.shared_from_this(); |
| 4600 | } |
| 4601 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4602 | void |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 4603 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4604 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4605 | exe_ctx.SetTargetPtr (&m_target); |
| 4606 | exe_ctx.SetProcessPtr (this); |
| 4607 | exe_ctx.SetThreadPtr(NULL); |
| 4608 | exe_ctx.SetFramePtr (NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4611 | //uint32_t |
| 4612 | //Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 4613 | //{ |
| 4614 | // return 0; |
| 4615 | //} |
| 4616 | // |
| 4617 | //ArchSpec |
| 4618 | //Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 4619 | //{ |
| 4620 | // return Host::GetArchSpecForExistingProcess (pid); |
| 4621 | //} |
| 4622 | // |
| 4623 | //ArchSpec |
| 4624 | //Process::GetArchSpecForExistingProcess (const char *process_name) |
| 4625 | //{ |
| 4626 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 4627 | //} |
| 4628 | // |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4629 | void |
| 4630 | Process::AppendSTDOUT (const char * s, size_t len) |
| 4631 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 4632 | Mutex::Locker locker (m_stdio_communication_mutex); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4633 | m_stdout_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4634 | BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState())); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | void |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4638 | Process::AppendSTDERR (const char * s, size_t len) |
| 4639 | { |
| 4640 | Mutex::Locker locker (m_stdio_communication_mutex); |
| 4641 | m_stderr_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4642 | BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState())); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4645 | void |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4646 | Process::BroadcastAsyncProfileData(const std::string &one_profile_data) |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4647 | { |
| 4648 | Mutex::Locker locker (m_profile_data_comm_mutex); |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4649 | m_profile_data.push_back(one_profile_data); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4650 | BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState())); |
| 4651 | } |
| 4652 | |
| 4653 | size_t |
| 4654 | Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error) |
| 4655 | { |
| 4656 | Mutex::Locker locker(m_profile_data_comm_mutex); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4657 | if (m_profile_data.empty()) |
| 4658 | return 0; |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4659 | |
| 4660 | std::string &one_profile_data = m_profile_data.front(); |
| 4661 | size_t bytes_available = one_profile_data.size(); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4662 | if (bytes_available > 0) |
| 4663 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4664 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4665 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4666 | log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")", buf, (uint64_t)buf_size); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4667 | if (bytes_available > buf_size) |
| 4668 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4669 | memcpy(buf, one_profile_data.c_str(), buf_size); |
| 4670 | one_profile_data.erase(0, buf_size); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4671 | bytes_available = buf_size; |
| 4672 | } |
| 4673 | else |
| 4674 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4675 | memcpy(buf, one_profile_data.c_str(), bytes_available); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4676 | m_profile_data.erase(m_profile_data.begin()); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4677 | } |
| 4678 | } |
| 4679 | return bytes_available; |
| 4680 | } |
| 4681 | |
| 4682 | |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4683 | //------------------------------------------------------------------ |
| 4684 | // Process STDIO |
| 4685 | //------------------------------------------------------------------ |
| 4686 | |
| 4687 | size_t |
| 4688 | Process::GetSTDOUT (char *buf, size_t buf_size, Error &error) |
| 4689 | { |
| 4690 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4691 | size_t bytes_available = m_stdout_data.size(); |
| 4692 | if (bytes_available > 0) |
| 4693 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4694 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4695 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4696 | log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")", buf, (uint64_t)buf_size); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4697 | if (bytes_available > buf_size) |
| 4698 | { |
| 4699 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 4700 | m_stdout_data.erase(0, buf_size); |
| 4701 | bytes_available = buf_size; |
| 4702 | } |
| 4703 | else |
| 4704 | { |
| 4705 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 4706 | m_stdout_data.clear(); |
| 4707 | } |
| 4708 | } |
| 4709 | return bytes_available; |
| 4710 | } |
| 4711 | |
| 4712 | |
| 4713 | size_t |
| 4714 | Process::GetSTDERR (char *buf, size_t buf_size, Error &error) |
| 4715 | { |
| 4716 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4717 | size_t bytes_available = m_stderr_data.size(); |
| 4718 | if (bytes_available > 0) |
| 4719 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4720 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4721 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4722 | log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")", buf, (uint64_t)buf_size); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4723 | if (bytes_available > buf_size) |
| 4724 | { |
| 4725 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 4726 | m_stderr_data.erase(0, buf_size); |
| 4727 | bytes_available = buf_size; |
| 4728 | } |
| 4729 | else |
| 4730 | { |
| 4731 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 4732 | m_stderr_data.clear(); |
| 4733 | } |
| 4734 | } |
| 4735 | return bytes_available; |
| 4736 | } |
| 4737 | |
| 4738 | void |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4739 | Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 4740 | { |
| 4741 | Process *process = (Process *) baton; |
| 4742 | process->AppendSTDOUT (static_cast<const char *>(src), src_len); |
| 4743 | } |
| 4744 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4745 | class IOHandlerProcessSTDIO : |
| 4746 | public IOHandler |
| 4747 | { |
| 4748 | public: |
| 4749 | IOHandlerProcessSTDIO (Process *process, |
| 4750 | int write_fd) : |
| 4751 | IOHandler(process->GetTarget().GetDebugger()), |
| 4752 | m_process (process), |
| 4753 | m_read_file (), |
| 4754 | m_write_file (write_fd, false), |
| 4755 | m_pipe_read(), |
| 4756 | m_pipe_write() |
| 4757 | { |
| 4758 | m_read_file.SetDescriptor(GetInputFD(), false); |
| 4759 | } |
| 4760 | |
| 4761 | virtual |
| 4762 | ~IOHandlerProcessSTDIO () |
| 4763 | { |
| 4764 | |
| 4765 | } |
| 4766 | |
| 4767 | bool |
| 4768 | OpenPipes () |
| 4769 | { |
| 4770 | if (m_pipe_read.IsValid() && m_pipe_write.IsValid()) |
| 4771 | return true; |
| 4772 | |
| 4773 | int fds[2]; |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4774 | #ifdef _MSC_VER |
| 4775 | // pipe is not supported on windows so default to a fail condition |
| 4776 | int err = 1; |
| 4777 | #else |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4778 | int err = pipe(fds); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4779 | #endif |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4780 | if (err == 0) |
| 4781 | { |
| 4782 | m_pipe_read.SetDescriptor(fds[0], true); |
| 4783 | m_pipe_write.SetDescriptor(fds[1], true); |
| 4784 | return true; |
| 4785 | } |
| 4786 | return false; |
| 4787 | } |
| 4788 | |
| 4789 | void |
| 4790 | ClosePipes() |
| 4791 | { |
| 4792 | m_pipe_read.Close(); |
| 4793 | m_pipe_write.Close(); |
| 4794 | } |
| 4795 | |
| 4796 | // Each IOHandler gets to run until it is done. It should read data |
| 4797 | // from the "in" and place output into "out" and "err and return |
| 4798 | // when done. |
| 4799 | virtual void |
| 4800 | Run () |
| 4801 | { |
| 4802 | if (m_read_file.IsValid() && m_write_file.IsValid()) |
| 4803 | { |
| 4804 | SetIsDone(false); |
| 4805 | if (OpenPipes()) |
| 4806 | { |
| 4807 | const int read_fd = m_read_file.GetDescriptor(); |
| 4808 | const int pipe_read_fd = m_pipe_read.GetDescriptor(); |
| 4809 | TerminalState terminal_state; |
| 4810 | terminal_state.Save (read_fd, false); |
| 4811 | Terminal terminal(read_fd); |
| 4812 | terminal.SetCanonical(false); |
| 4813 | terminal.SetEcho(false); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4814 | // FD_ZERO, FD_SET are not supported on windows |
| 4815 | #ifndef _MSC_VER |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4816 | while (!GetIsDone()) |
| 4817 | { |
| 4818 | fd_set read_fdset; |
| 4819 | FD_ZERO (&read_fdset); |
| 4820 | FD_SET (read_fd, &read_fdset); |
| 4821 | FD_SET (pipe_read_fd, &read_fdset); |
| 4822 | const int nfds = std::max<int>(read_fd, pipe_read_fd) + 1; |
| 4823 | int num_set_fds = select (nfds, &read_fdset, NULL, NULL, NULL); |
| 4824 | if (num_set_fds < 0) |
| 4825 | { |
| 4826 | const int select_errno = errno; |
| 4827 | |
| 4828 | if (select_errno != EINTR) |
| 4829 | SetIsDone(true); |
| 4830 | } |
| 4831 | else if (num_set_fds > 0) |
| 4832 | { |
| 4833 | char ch = 0; |
| 4834 | size_t n; |
| 4835 | if (FD_ISSET (read_fd, &read_fdset)) |
| 4836 | { |
| 4837 | n = 1; |
| 4838 | if (m_read_file.Read(&ch, n).Success() && n == 1) |
| 4839 | { |
| 4840 | if (m_write_file.Write(&ch, n).Fail() || n != 1) |
| 4841 | SetIsDone(true); |
| 4842 | } |
| 4843 | else |
| 4844 | SetIsDone(true); |
| 4845 | } |
| 4846 | if (FD_ISSET (pipe_read_fd, &read_fdset)) |
| 4847 | { |
| 4848 | // Consume the interrupt byte |
| 4849 | n = 1; |
| 4850 | m_pipe_read.Read (&ch, n); |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4851 | switch (ch) |
| 4852 | { |
| 4853 | case 'q': |
| 4854 | SetIsDone(true); |
| 4855 | break; |
| 4856 | case 'i': |
| 4857 | if (StateIsRunningState(m_process->GetState())) |
| 4858 | m_process->Halt(); |
| 4859 | break; |
| 4860 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4861 | } |
| 4862 | } |
| 4863 | } |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4864 | #endif |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4865 | terminal_state.Restore(); |
| 4866 | |
| 4867 | } |
| 4868 | else |
| 4869 | SetIsDone(true); |
| 4870 | } |
| 4871 | else |
| 4872 | SetIsDone(true); |
| 4873 | } |
| 4874 | |
| 4875 | // Hide any characters that have been displayed so far so async |
| 4876 | // output can be displayed. Refresh() will be called after the |
| 4877 | // output has been displayed. |
| 4878 | virtual void |
| 4879 | Hide () |
| 4880 | { |
| 4881 | |
| 4882 | } |
| 4883 | // Called when the async output has been received in order to update |
| 4884 | // the input reader (refresh the prompt and redisplay any current |
| 4885 | // line(s) that are being edited |
| 4886 | virtual void |
| 4887 | Refresh () |
| 4888 | { |
| 4889 | |
| 4890 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4891 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4892 | virtual void |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4893 | Cancel () |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4894 | { |
| 4895 | size_t n = 1; |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4896 | char ch = 'q'; // Send 'q' for quit |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4897 | m_pipe_write.Write (&ch, n); |
| 4898 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4899 | |
| 4900 | virtual void |
| 4901 | Interrupt () |
| 4902 | { |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4903 | #ifdef _MSC_VER |
| 4904 | // Windows doesn't support pipes, so we will send an async interrupt |
| 4905 | // event to stop the process |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4906 | if (StateIsRunningState(m_process->GetState())) |
Ed Maste | 96e51b8 | 2014-02-25 14:20:14 +0000 | [diff] [blame] | 4907 | m_process->SendAsyncInterrupt(); |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4908 | #else |
| 4909 | // Do only things that are safe to do in an interrupt context (like in |
| 4910 | // a SIGINT handler), like write 1 byte to a file descriptor. This will |
| 4911 | // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte |
| 4912 | // that was written to the pipe and then call m_process->Halt() from a |
| 4913 | // much safer location in code. |
| 4914 | size_t n = 1; |
| 4915 | char ch = 'i'; // Send 'i' for interrupt |
| 4916 | m_pipe_write.Write (&ch, n); |
| 4917 | #endif |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4918 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4919 | |
| 4920 | virtual void |
| 4921 | GotEOF() |
| 4922 | { |
| 4923 | |
| 4924 | } |
| 4925 | |
| 4926 | protected: |
| 4927 | Process *m_process; |
| 4928 | File m_read_file; // Read from this file (usually actual STDIN for LLDB |
| 4929 | File m_write_file; // Write to this file (usually the master pty for getting io to debuggee) |
| 4930 | File m_pipe_read; |
| 4931 | File m_pipe_write; |
| 4932 | |
| 4933 | }; |
| 4934 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4935 | void |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4936 | Process::SetSTDIOFileDescriptor (int fd) |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4937 | { |
| 4938 | // First set up the Read Thread for reading/handling process I/O |
| 4939 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4940 | std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (fd, true)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4941 | |
| 4942 | if (conn_ap.get()) |
| 4943 | { |
| 4944 | m_stdio_communication.SetConnection (conn_ap.release()); |
| 4945 | if (m_stdio_communication.IsConnected()) |
| 4946 | { |
| 4947 | m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); |
| 4948 | m_stdio_communication.StartReadThread(); |
| 4949 | |
| 4950 | // Now read thread is set up, set up input reader. |
| 4951 | |
| 4952 | if (!m_process_input_reader.get()) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4953 | m_process_input_reader.reset (new IOHandlerProcessSTDIO (this, fd)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4954 | } |
| 4955 | } |
| 4956 | } |
| 4957 | |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4958 | bool |
Greg Clayton | 6fea17e | 2014-03-03 19:15:20 +0000 | [diff] [blame] | 4959 | Process::ProcessIOHandlerIsActive () |
| 4960 | { |
| 4961 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 4962 | if (io_handler_sp) |
| 4963 | return m_target.GetDebugger().IsTopIOHandler (io_handler_sp); |
| 4964 | return false; |
| 4965 | } |
| 4966 | bool |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4967 | Process::PushProcessIOHandler () |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4968 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4969 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 4970 | if (io_handler_sp) |
| 4971 | { |
| 4972 | io_handler_sp->SetIsDone(false); |
| 4973 | m_target.GetDebugger().PushIOHandler (io_handler_sp); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4974 | return true; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4975 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4976 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4979 | bool |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4980 | Process::PopProcessIOHandler () |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4981 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4982 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 4983 | if (io_handler_sp) |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4984 | return m_target.GetDebugger().PopIOHandler (io_handler_sp); |
| 4985 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4988 | // The process needs to know about installed plug-ins |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4989 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 4990 | Process::SettingsInitialize () |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4991 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 4992 | Thread::SettingsInitialize (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4993 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4994 | |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4995 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 4996 | Process::SettingsTerminate () |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4997 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 4998 | Thread::SettingsTerminate (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4999 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 5000 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 5001 | ExecutionResults |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5002 | Process::RunThreadPlan (ExecutionContext &exe_ctx, |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5003 | lldb::ThreadPlanSP &thread_plan_sp, |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5004 | const EvaluateExpressionOptions &options, |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5005 | Stream &errors) |
| 5006 | { |
| 5007 | ExecutionResults return_value = eExecutionSetupError; |
| 5008 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5009 | if (thread_plan_sp.get() == NULL) |
| 5010 | { |
| 5011 | errors.Printf("RunThreadPlan called with empty thread plan."); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5012 | return eExecutionSetupError; |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5013 | } |
Jim Ingham | 7d7931d | 2013-03-28 00:05:34 +0000 | [diff] [blame] | 5014 | |
| 5015 | if (!thread_plan_sp->ValidatePlan(NULL)) |
| 5016 | { |
| 5017 | errors.Printf ("RunThreadPlan called with an invalid thread plan."); |
| 5018 | return eExecutionSetupError; |
| 5019 | } |
| 5020 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5021 | if (exe_ctx.GetProcessPtr() != this) |
| 5022 | { |
| 5023 | errors.Printf("RunThreadPlan called on wrong process."); |
| 5024 | return eExecutionSetupError; |
| 5025 | } |
| 5026 | |
| 5027 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 5028 | if (thread == NULL) |
| 5029 | { |
| 5030 | errors.Printf("RunThreadPlan called with invalid thread."); |
| 5031 | return eExecutionSetupError; |
| 5032 | } |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5033 | |
Jim Ingham | 17e5c4e | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 5034 | // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes. |
| 5035 | // For that to be true the plan can't be private - since private plans suppress themselves in the |
| 5036 | // GetCompletedPlan call. |
| 5037 | |
| 5038 | bool orig_plan_private = thread_plan_sp->GetPrivate(); |
| 5039 | thread_plan_sp->SetPrivate(false); |
| 5040 | |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 5041 | if (m_private_state.GetValue() != eStateStopped) |
| 5042 | { |
| 5043 | errors.Printf ("RunThreadPlan called while the private state was not stopped."); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5044 | return eExecutionSetupError; |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5047 | // Save the thread & frame from the exe_ctx for restoration after we run |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5048 | const uint32_t thread_idx_id = thread->GetIndexID(); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 5049 | StackFrameSP selected_frame_sp = thread->GetSelectedFrame(); |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 5050 | if (!selected_frame_sp) |
| 5051 | { |
| 5052 | thread->SetSelectedFrame(0); |
| 5053 | selected_frame_sp = thread->GetSelectedFrame(); |
| 5054 | if (!selected_frame_sp) |
| 5055 | { |
| 5056 | errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id); |
| 5057 | return eExecutionSetupError; |
| 5058 | } |
| 5059 | } |
| 5060 | |
| 5061 | StackID ctx_frame_id = selected_frame_sp->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5062 | |
| 5063 | // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either, |
| 5064 | // so we should arrange to reset them as well. |
| 5065 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5066 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5067 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5068 | uint32_t selected_tid; |
| 5069 | StackID selected_stack_id; |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 5070 | if (selected_thread_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5071 | { |
| 5072 | selected_tid = selected_thread_sp->GetIndexID(); |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5073 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5074 | } |
| 5075 | else |
| 5076 | { |
| 5077 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 5078 | } |
| 5079 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5080 | lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5081 | lldb::StateType old_state; |
| 5082 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5083 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 5084 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5085 | if (Host::GetCurrentThread() == m_private_state_thread) |
| 5086 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5087 | // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since |
| 5088 | // we are the thread that is generating public events. |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5089 | // The simplest thing to do is to spin up a temporary thread to handle private state thread events while |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5090 | // we are fielding public events here. |
| 5091 | if (log) |
Jason Molenda | d251c9d | 2012-11-17 01:41:04 +0000 | [diff] [blame] | 5092 | log->Printf ("Running thread plan on private state thread, spinning up another state thread to handle the events."); |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5093 | |
| 5094 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5095 | backup_private_state_thread = m_private_state_thread; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5096 | |
| 5097 | // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop, |
| 5098 | // returning control here. |
| 5099 | // But in the normal course of things, the plan above us on the stack would be given a shot at the stop |
| 5100 | // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack |
| 5101 | // before the plan we want to run. Since base plans always stop and return control to the user, that will |
| 5102 | // do just what we want. |
| 5103 | stopper_base_plan_sp.reset(new ThreadPlanBase (*thread)); |
| 5104 | thread->QueueThreadPlan (stopper_base_plan_sp, false); |
| 5105 | // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly. |
| 5106 | old_state = m_public_state.GetValue(); |
| 5107 | m_public_state.SetValueNoLock(eStateStopped); |
| 5108 | |
| 5109 | // Now spin up the private state thread: |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5110 | StartPrivateStateThread(true); |
| 5111 | } |
| 5112 | |
| 5113 | thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense? |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5114 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5115 | if (options.GetDebug()) |
| 5116 | { |
| 5117 | // In this case, we aren't actually going to run, we just want to stop right away. |
| 5118 | // Flush this thread so we will refetch the stacks and show the correct backtrace. |
| 5119 | // FIXME: To make this prettier we should invent some stop reason for this, but that |
| 5120 | // is only cosmetic, and this functionality is only of use to lldb developers who can |
| 5121 | // live with not pretty... |
| 5122 | thread->Flush(); |
| 5123 | return eExecutionStoppedForDebug; |
| 5124 | } |
| 5125 | |
Jim Ingham | 1e7a9ee | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 5126 | Listener listener("lldb.process.listener.run-thread-plan"); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5127 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5128 | lldb::EventSP event_to_broadcast_sp; |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5129 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5130 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5131 | // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get |
| 5132 | // restored on exit to the function. |
| 5133 | // |
| 5134 | // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event |
| 5135 | // is put into event_to_broadcast_sp for rebroadcasting. |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5136 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5137 | ProcessEventHijacker run_thread_plan_hijacker (*this, &listener); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5138 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5139 | if (log) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5140 | { |
| 5141 | StreamString s; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5142 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5143 | log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 " to run thread plan \"%s\".", |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5144 | thread->GetIndexID(), |
| 5145 | thread->GetID(), |
| 5146 | s.GetData()); |
| 5147 | } |
| 5148 | |
| 5149 | bool got_event; |
| 5150 | lldb::EventSP event_sp; |
| 5151 | lldb::StateType stop_state = lldb::eStateInvalid; |
| 5152 | |
| 5153 | TimeValue* timeout_ptr = NULL; |
| 5154 | TimeValue real_timeout; |
| 5155 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5156 | bool before_first_timeout = true; // This is set to false the first time that we have to halt the target. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5157 | bool do_resume = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5158 | bool handle_running_event = true; |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5159 | const uint64_t default_one_thread_timeout_usec = 250000; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5160 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5161 | // This is just for accounting: |
| 5162 | uint32_t num_resumes = 0; |
| 5163 | |
| 5164 | TimeValue one_thread_timeout = TimeValue::Now(); |
| 5165 | TimeValue final_timeout = one_thread_timeout; |
| 5166 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5167 | uint32_t timeout_usec = options.GetTimeoutUsec(); |
Jim Ingham | 286fb1e | 2014-02-28 02:52:06 +0000 | [diff] [blame] | 5168 | if (!options.GetStopOthers()) |
| 5169 | { |
| 5170 | before_first_timeout = false; |
| 5171 | final_timeout.OffsetWithMicroSeconds(timeout_usec); |
| 5172 | } |
| 5173 | else if (options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5174 | { |
| 5175 | // If we are running all threads then we take half the time to run all threads, bounded by |
| 5176 | // .25 sec. |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5177 | if (options.GetTimeoutUsec() == 0) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5178 | one_thread_timeout.OffsetWithMicroSeconds(default_one_thread_timeout_usec); |
| 5179 | else |
| 5180 | { |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 5181 | uint64_t computed_timeout = timeout_usec / 2; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5182 | if (computed_timeout > default_one_thread_timeout_usec) |
| 5183 | computed_timeout = default_one_thread_timeout_usec; |
| 5184 | one_thread_timeout.OffsetWithMicroSeconds(computed_timeout); |
| 5185 | } |
| 5186 | final_timeout.OffsetWithMicroSeconds (timeout_usec); |
| 5187 | } |
| 5188 | else |
| 5189 | { |
| 5190 | if (timeout_usec != 0) |
| 5191 | final_timeout.OffsetWithMicroSeconds(timeout_usec); |
| 5192 | } |
| 5193 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5194 | // This isn't going to work if there are unfetched events on the queue. |
| 5195 | // Are there cases where we might want to run the remaining events here, and then try to |
| 5196 | // call the function? That's probably being too tricky for our own good. |
| 5197 | |
| 5198 | Event *other_events = listener.PeekAtNextEvent(); |
| 5199 | if (other_events != NULL) |
| 5200 | { |
| 5201 | errors.Printf("Calling RunThreadPlan with pending events on the queue."); |
| 5202 | return eExecutionSetupError; |
| 5203 | } |
| 5204 | |
| 5205 | // We also need to make sure that the next event is delivered. We might be calling a function as part of |
| 5206 | // a thread plan, in which case the last delivered event could be the running event, and we don't want |
| 5207 | // event coalescing to cause us to lose OUR running event... |
| 5208 | ForceNextEventDelivery(); |
| 5209 | |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5210 | // This while loop must exit out the bottom, there's cleanup that we need to do when we are done. |
| 5211 | // So don't call return anywhere within it. |
| 5212 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5213 | while (1) |
| 5214 | { |
| 5215 | // We usually want to resume the process if we get to the top of the loop. |
| 5216 | // The only exception is if we get two running events with no intervening |
| 5217 | // stop, which can happen, we will just wait for then next stop event. |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5218 | if (log) |
| 5219 | log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.", |
| 5220 | do_resume, |
| 5221 | handle_running_event, |
| 5222 | before_first_timeout); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5223 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5224 | if (do_resume || handle_running_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5225 | { |
| 5226 | // Do the initial resume and wait for the running event before going further. |
| 5227 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5228 | if (do_resume) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5229 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5230 | num_resumes++; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5231 | Error resume_error = PrivateResume (); |
| 5232 | if (!resume_error.Success()) |
| 5233 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5234 | errors.Printf("Error resuming inferior the %d time: \"%s\".\n", |
| 5235 | num_resumes, |
| 5236 | resume_error.AsCString()); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5237 | return_value = eExecutionSetupError; |
| 5238 | break; |
| 5239 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5240 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5241 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5242 | TimeValue resume_timeout = TimeValue::Now(); |
| 5243 | resume_timeout.OffsetWithMicroSeconds(500000); |
| 5244 | |
| 5245 | got_event = listener.WaitForEvent(&resume_timeout, event_sp); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5246 | if (!got_event) |
| 5247 | { |
| 5248 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5249 | log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.", |
| 5250 | num_resumes); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5251 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5252 | errors.Printf("Didn't get any event after resume %d, exiting.", num_resumes); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5253 | return_value = eExecutionSetupError; |
| 5254 | break; |
| 5255 | } |
| 5256 | |
| 5257 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5258 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5259 | if (stop_state != eStateRunning) |
| 5260 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5261 | bool restarted = false; |
| 5262 | |
| 5263 | if (stop_state == eStateStopped) |
| 5264 | { |
| 5265 | restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()); |
| 5266 | if (log) |
| 5267 | log->Printf("Process::RunThreadPlan(): didn't get running event after " |
| 5268 | "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).", |
| 5269 | num_resumes, |
| 5270 | StateAsCString(stop_state), |
| 5271 | restarted, |
| 5272 | do_resume, |
| 5273 | handle_running_event); |
| 5274 | } |
| 5275 | |
| 5276 | if (restarted) |
| 5277 | { |
| 5278 | // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted |
| 5279 | // event here. But if I do, the best thing is to Halt and then get out of here. |
| 5280 | Halt(); |
| 5281 | } |
| 5282 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5283 | errors.Printf("Didn't get running event after initial resume, got %s instead.", |
| 5284 | StateAsCString(stop_state)); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5285 | return_value = eExecutionSetupError; |
| 5286 | break; |
| 5287 | } |
| 5288 | |
| 5289 | if (log) |
| 5290 | log->PutCString ("Process::RunThreadPlan(): resuming succeeded."); |
| 5291 | // We need to call the function synchronously, so spin waiting for it to return. |
| 5292 | // If we get interrupted while executing, we're going to lose our context, and |
| 5293 | // won't be able to gather the result at this point. |
| 5294 | // We set the timeout AFTER the resume, since the resume takes some time and we |
| 5295 | // don't want to charge that to the timeout. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5296 | } |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5297 | else |
| 5298 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5299 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5300 | log->PutCString ("Process::RunThreadPlan(): waiting for next event."); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5301 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5302 | |
| 5303 | if (before_first_timeout) |
| 5304 | { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5305 | if (options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5306 | timeout_ptr = &one_thread_timeout; |
| 5307 | else |
| 5308 | { |
| 5309 | if (timeout_usec == 0) |
| 5310 | timeout_ptr = NULL; |
| 5311 | else |
| 5312 | timeout_ptr = &final_timeout; |
| 5313 | } |
| 5314 | } |
| 5315 | else |
| 5316 | { |
| 5317 | if (timeout_usec == 0) |
| 5318 | timeout_ptr = NULL; |
| 5319 | else |
| 5320 | timeout_ptr = &final_timeout; |
| 5321 | } |
| 5322 | |
| 5323 | do_resume = true; |
| 5324 | handle_running_event = true; |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5325 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5326 | // Now wait for the process to stop again: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5327 | event_sp.reset(); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5328 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5329 | if (log) |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5330 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5331 | if (timeout_ptr) |
| 5332 | { |
Matt Kopec | 676a487 | 2013-02-21 23:55:31 +0000 | [diff] [blame] | 5333 | log->Printf ("Process::RunThreadPlan(): about to wait - now is %" PRIu64 " - endpoint is %" PRIu64, |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5334 | TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(), |
| 5335 | timeout_ptr->GetAsMicroSecondsSinceJan1_1970()); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5336 | } |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5337 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5338 | { |
| 5339 | log->Printf ("Process::RunThreadPlan(): about to wait forever."); |
| 5340 | } |
| 5341 | } |
| 5342 | |
| 5343 | got_event = listener.WaitForEvent (timeout_ptr, event_sp); |
| 5344 | |
| 5345 | if (got_event) |
| 5346 | { |
| 5347 | if (event_sp.get()) |
| 5348 | { |
| 5349 | bool keep_going = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5350 | if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5351 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5352 | Halt(); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5353 | return_value = eExecutionInterrupted; |
| 5354 | errors.Printf ("Execution halted by user interrupt."); |
| 5355 | if (log) |
| 5356 | log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting."); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5357 | break; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5358 | } |
| 5359 | else |
| 5360 | { |
| 5361 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5362 | if (log) |
| 5363 | log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state)); |
| 5364 | |
| 5365 | switch (stop_state) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5366 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5367 | case lldb::eStateStopped: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5368 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5369 | // We stopped, figure out what we are going to do now. |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5370 | ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id); |
| 5371 | if (!thread_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5372 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5373 | // Ooh, our thread has vanished. Unlikely that this was successful execution... |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5374 | if (log) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5375 | log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id); |
| 5376 | return_value = eExecutionInterrupted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5377 | } |
| 5378 | else |
| 5379 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5380 | // If we were restarted, we just need to go back up to fetch another event. |
| 5381 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5382 | { |
| 5383 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5384 | { |
| 5385 | log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting."); |
| 5386 | } |
| 5387 | keep_going = true; |
| 5388 | do_resume = false; |
| 5389 | handle_running_event = true; |
| 5390 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5391 | } |
| 5392 | else |
| 5393 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5394 | |
| 5395 | StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
| 5396 | StopReason stop_reason = eStopReasonInvalid; |
| 5397 | if (stop_info_sp) |
| 5398 | stop_reason = stop_info_sp->GetStopReason(); |
| 5399 | |
| 5400 | |
| 5401 | // FIXME: We only check if the stop reason is plan complete, should we make sure that |
| 5402 | // it is OUR plan that is complete? |
| 5403 | if (stop_reason == eStopReasonPlanComplete) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5404 | { |
| 5405 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5406 | log->PutCString ("Process::RunThreadPlan(): execution completed successfully."); |
| 5407 | // Now mark this plan as private so it doesn't get reported as the stop reason |
| 5408 | // after this point. |
| 5409 | if (thread_plan_sp) |
| 5410 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5411 | return_value = eExecutionCompleted; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5412 | } |
| 5413 | else |
| 5414 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5415 | // Something restarted the target, so just wait for it to stop for real. |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5416 | if (stop_reason == eStopReasonBreakpoint) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5417 | { |
| 5418 | if (log) |
| 5419 | log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription()); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5420 | return_value = eExecutionHitBreakpoint; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5421 | if (!options.DoesIgnoreBreakpoints()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5422 | { |
| 5423 | event_to_broadcast_sp = event_sp; |
| 5424 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5425 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5426 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5427 | { |
| 5428 | if (log) |
| 5429 | log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete."); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5430 | if (!options.DoesUnwindOnError()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5431 | event_to_broadcast_sp = event_sp; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5432 | return_value = eExecutionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5433 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5434 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5435 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5436 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5437 | } |
| 5438 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5439 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5440 | case lldb::eStateRunning: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5441 | // This shouldn't really happen, but sometimes we do get two running events without an |
| 5442 | // intervening stop, and in that case we should just go back to waiting for the stop. |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5443 | do_resume = false; |
| 5444 | keep_going = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5445 | handle_running_event = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5446 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5447 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5448 | default: |
| 5449 | if (log) |
| 5450 | log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state)); |
| 5451 | |
| 5452 | if (stop_state == eStateExited) |
| 5453 | event_to_broadcast_sp = event_sp; |
| 5454 | |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 5455 | errors.Printf ("Execution stopped with unexpected state.\n"); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5456 | return_value = eExecutionInterrupted; |
| 5457 | break; |
| 5458 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5459 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5460 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5461 | if (keep_going) |
| 5462 | continue; |
| 5463 | else |
| 5464 | break; |
| 5465 | } |
| 5466 | else |
| 5467 | { |
| 5468 | if (log) |
| 5469 | log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd..."); |
| 5470 | return_value = eExecutionInterrupted; |
| 5471 | break; |
| 5472 | } |
| 5473 | } |
| 5474 | else |
| 5475 | { |
| 5476 | // If we didn't get an event that means we've timed out... |
| 5477 | // We will interrupt the process here. Depending on what we were asked to do we will |
| 5478 | // either exit, or try with all threads running for the same timeout. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5479 | |
| 5480 | if (log) { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5481 | if (options.GetTryAllThreads()) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5482 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5483 | uint64_t remaining_time = final_timeout - TimeValue::Now(); |
| 5484 | if (before_first_timeout) |
| 5485 | log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, " |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5486 | "running till for %" PRIu64 " usec with all threads enabled.", |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5487 | remaining_time); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5488 | else |
| 5489 | log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled " |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5490 | "and timeout: %u timed out, abandoning execution.", |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5491 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5492 | } |
| 5493 | else |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5494 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, " |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5495 | "abandoning execution.", |
| 5496 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5499 | // It is possible that between the time we issued the Halt, and we get around to calling Halt the target |
| 5500 | // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event. |
| 5501 | // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In |
| 5502 | // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's |
| 5503 | // stopped event. That's what this while loop does. |
| 5504 | |
| 5505 | bool back_to_top = true; |
| 5506 | uint32_t try_halt_again = 0; |
| 5507 | bool do_halt = true; |
| 5508 | const uint32_t num_retries = 5; |
| 5509 | while (try_halt_again < num_retries) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5510 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5511 | Error halt_error; |
| 5512 | if (do_halt) |
| 5513 | { |
| 5514 | if (log) |
| 5515 | log->Printf ("Process::RunThreadPlan(): Running Halt."); |
| 5516 | halt_error = Halt(); |
| 5517 | } |
| 5518 | if (halt_error.Success()) |
| 5519 | { |
| 5520 | if (log) |
| 5521 | log->PutCString ("Process::RunThreadPlan(): Halt succeeded."); |
| 5522 | |
| 5523 | real_timeout = TimeValue::Now(); |
| 5524 | real_timeout.OffsetWithMicroSeconds(500000); |
| 5525 | |
| 5526 | got_event = listener.WaitForEvent(&real_timeout, event_sp); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5527 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5528 | if (got_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5529 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5530 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5531 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5532 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5533 | log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state)); |
| 5534 | if (stop_state == lldb::eStateStopped |
| 5535 | && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 5536 | log->PutCString (" Event was the Halt interruption event."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5537 | } |
| 5538 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5539 | if (stop_state == lldb::eStateStopped) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5540 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5541 | // Between the time we initiated the Halt and the time we delivered it, the process could have |
| 5542 | // already finished its job. Check that here: |
| 5543 | |
| 5544 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
| 5545 | { |
| 5546 | if (log) |
| 5547 | log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. " |
| 5548 | "Exiting wait loop."); |
| 5549 | return_value = eExecutionCompleted; |
| 5550 | back_to_top = false; |
| 5551 | break; |
| 5552 | } |
| 5553 | |
| 5554 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 5555 | { |
| 5556 | if (log) |
| 5557 | log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... " |
| 5558 | "Exiting wait loop."); |
| 5559 | try_halt_again++; |
| 5560 | do_halt = false; |
| 5561 | continue; |
| 5562 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5563 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5564 | if (!options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5565 | { |
| 5566 | if (log) |
| 5567 | log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting."); |
| 5568 | return_value = eExecutionInterrupted; |
| 5569 | back_to_top = false; |
| 5570 | break; |
| 5571 | } |
| 5572 | |
| 5573 | if (before_first_timeout) |
| 5574 | { |
| 5575 | // Set all the other threads to run, and return to the top of the loop, which will continue; |
| 5576 | before_first_timeout = false; |
| 5577 | thread_plan_sp->SetStopOthers (false); |
| 5578 | if (log) |
| 5579 | log->PutCString ("Process::RunThreadPlan(): about to resume."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5580 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5581 | back_to_top = true; |
| 5582 | break; |
| 5583 | } |
| 5584 | else |
| 5585 | { |
| 5586 | // Running all threads failed, so return Interrupted. |
| 5587 | if (log) |
| 5588 | log->PutCString("Process::RunThreadPlan(): running all threads timed out."); |
| 5589 | return_value = eExecutionInterrupted; |
| 5590 | back_to_top = false; |
| 5591 | break; |
| 5592 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5593 | } |
| 5594 | } |
| 5595 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5596 | { if (log) |
| 5597 | log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. " |
| 5598 | "I'm getting out of here passing Interrupted."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5599 | return_value = eExecutionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5600 | back_to_top = false; |
| 5601 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5602 | } |
| 5603 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5604 | else |
| 5605 | { |
| 5606 | try_halt_again++; |
| 5607 | continue; |
| 5608 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5609 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5610 | |
| 5611 | if (!back_to_top || try_halt_again > num_retries) |
| 5612 | break; |
| 5613 | else |
| 5614 | continue; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5615 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5616 | } // END WAIT LOOP |
| 5617 | |
| 5618 | // If we had to start up a temporary private state thread to run this thread plan, shut it down now. |
| 5619 | if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread)) |
| 5620 | { |
| 5621 | StopPrivateStateThread(); |
| 5622 | Error error; |
| 5623 | m_private_state_thread = backup_private_state_thread; |
Sean Callanan | 9a02851 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 5624 | if (stopper_base_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5625 | { |
| 5626 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 5627 | } |
| 5628 | m_public_state.SetValueNoLock(old_state); |
| 5629 | |
| 5630 | } |
| 5631 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5632 | // Restore the thread state if we are going to discard the plan execution. There are three cases where this |
| 5633 | // could happen: |
| 5634 | // 1) The execution successfully completed |
| 5635 | // 2) We hit a breakpoint, and ignore_breakpoints was true |
| 5636 | // 3) We got some other error, and discard_on_error was true |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5637 | bool should_unwind = (return_value == eExecutionInterrupted && options.DoesUnwindOnError()) |
| 5638 | || (return_value == eExecutionHitBreakpoint && options.DoesIgnoreBreakpoints()); |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5639 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5640 | if (return_value == eExecutionCompleted |
| 5641 | || should_unwind) |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5642 | { |
| 5643 | thread_plan_sp->RestoreThreadState(); |
| 5644 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5645 | |
| 5646 | // Now do some processing on the results of the run: |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5647 | if (return_value == eExecutionInterrupted || return_value == eExecutionHitBreakpoint) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5648 | { |
| 5649 | if (log) |
| 5650 | { |
| 5651 | StreamString s; |
| 5652 | if (event_sp) |
| 5653 | event_sp->Dump (&s); |
| 5654 | else |
| 5655 | { |
| 5656 | log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL."); |
| 5657 | } |
| 5658 | |
| 5659 | StreamString ts; |
| 5660 | |
| 5661 | const char *event_explanation = NULL; |
| 5662 | |
| 5663 | do |
| 5664 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5665 | if (!event_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5666 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5667 | event_explanation = "<no event>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5668 | break; |
| 5669 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5670 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5671 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5672 | event_explanation = "<user interrupt>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5673 | break; |
| 5674 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5675 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5676 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5677 | const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get()); |
| 5678 | |
| 5679 | if (!event_data) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5680 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5681 | event_explanation = "<no event data>"; |
| 5682 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5685 | Process *process = event_data->GetProcessSP().get(); |
| 5686 | |
| 5687 | if (!process) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5688 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5689 | event_explanation = "<no process>"; |
| 5690 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5691 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5692 | |
| 5693 | ThreadList &thread_list = process->GetThreadList(); |
| 5694 | |
| 5695 | uint32_t num_threads = thread_list.GetSize(); |
| 5696 | uint32_t thread_index; |
| 5697 | |
| 5698 | ts.Printf("<%u threads> ", num_threads); |
| 5699 | |
| 5700 | for (thread_index = 0; |
| 5701 | thread_index < num_threads; |
| 5702 | ++thread_index) |
| 5703 | { |
| 5704 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
| 5705 | |
| 5706 | if (!thread) |
| 5707 | { |
| 5708 | ts.Printf("<?> "); |
| 5709 | continue; |
| 5710 | } |
| 5711 | |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5712 | ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5713 | RegisterContext *register_context = thread->GetRegisterContext().get(); |
| 5714 | |
| 5715 | if (register_context) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5716 | ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5717 | else |
| 5718 | ts.Printf("[ip unknown] "); |
| 5719 | |
| 5720 | lldb::StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 5721 | if (stop_info_sp) |
| 5722 | { |
| 5723 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 5724 | if (stop_desc) |
| 5725 | ts.PutCString (stop_desc); |
| 5726 | } |
| 5727 | ts.Printf(">"); |
| 5728 | } |
| 5729 | |
| 5730 | event_explanation = ts.GetData(); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5731 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5732 | } while (0); |
| 5733 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5734 | if (event_explanation) |
| 5735 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5736 | else |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5737 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData()); |
| 5738 | } |
| 5739 | |
Jim Ingham | e4483cf | 2013-09-27 01:13:01 +0000 | [diff] [blame] | 5740 | if (should_unwind) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5741 | { |
| 5742 | if (log) |
| 5743 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.", thread_plan_sp.get()); |
| 5744 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 5745 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5746 | } |
| 5747 | else |
| 5748 | { |
| 5749 | if (log) |
| 5750 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.", thread_plan_sp.get()); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5751 | } |
| 5752 | } |
| 5753 | else if (return_value == eExecutionSetupError) |
| 5754 | { |
| 5755 | if (log) |
| 5756 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5757 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5758 | if (options.DoesUnwindOnError()) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5759 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5760 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 5761 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5762 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5763 | } |
| 5764 | else |
| 5765 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5766 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5767 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5768 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5769 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
| 5770 | return_value = eExecutionCompleted; |
| 5771 | } |
| 5772 | else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get())) |
| 5773 | { |
| 5774 | if (log) |
| 5775 | log->PutCString("Process::RunThreadPlan(): thread plan was discarded"); |
| 5776 | return_value = eExecutionDiscarded; |
| 5777 | } |
| 5778 | else |
| 5779 | { |
| 5780 | if (log) |
| 5781 | log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course"); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5782 | if (options.DoesUnwindOnError() && thread_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5783 | { |
| 5784 | if (log) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5785 | log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause unwind_on_error is set."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5786 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 5787 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5788 | } |
| 5789 | } |
| 5790 | } |
| 5791 | |
| 5792 | // Thread we ran the function in may have gone away because we ran the target |
| 5793 | // Check that it's still there, and if it is put it back in the context. Also restore the |
| 5794 | // frame in the context if it is still present. |
| 5795 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 5796 | if (thread) |
| 5797 | { |
| 5798 | exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id)); |
| 5799 | } |
| 5800 | |
| 5801 | // Also restore the current process'es selected frame & thread, since this function calling may |
| 5802 | // be done behind the user's back. |
| 5803 | |
| 5804 | if (selected_tid != LLDB_INVALID_THREAD_ID) |
| 5805 | { |
| 5806 | if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid()) |
| 5807 | { |
| 5808 | // We were able to restore the selected thread, now restore the frame: |
Daniel Malea | a012d3a | 2013-07-31 20:21:20 +0000 | [diff] [blame] | 5809 | Mutex::Locker lock(GetThreadList().GetMutex()); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 5810 | StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5811 | if (old_frame_sp) |
| 5812 | GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get()); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5813 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5814 | } |
| 5815 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5816 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5817 | // If the process exited during the run of the thread plan, notify everyone. |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5818 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5819 | if (event_to_broadcast_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5820 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5821 | if (log) |
| 5822 | log->PutCString("Process::RunThreadPlan(): rebroadcasting event."); |
| 5823 | BroadcastEvent(event_to_broadcast_sp); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | return return_value; |
| 5827 | } |
| 5828 | |
| 5829 | const char * |
| 5830 | Process::ExecutionResultAsCString (ExecutionResults result) |
| 5831 | { |
| 5832 | const char *result_name; |
| 5833 | |
| 5834 | switch (result) |
| 5835 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5836 | case eExecutionCompleted: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5837 | result_name = "eExecutionCompleted"; |
| 5838 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5839 | case eExecutionDiscarded: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5840 | result_name = "eExecutionDiscarded"; |
| 5841 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5842 | case eExecutionInterrupted: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5843 | result_name = "eExecutionInterrupted"; |
| 5844 | break; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5845 | case eExecutionHitBreakpoint: |
| 5846 | result_name = "eExecutionHitBreakpoint"; |
| 5847 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5848 | case eExecutionSetupError: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5849 | result_name = "eExecutionSetupError"; |
| 5850 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5851 | case eExecutionTimedOut: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5852 | result_name = "eExecutionTimedOut"; |
| 5853 | break; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5854 | case eExecutionStoppedForDebug: |
| 5855 | result_name = "eExecutionStoppedForDebug"; |
| 5856 | break; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5857 | } |
| 5858 | return result_name; |
| 5859 | } |
| 5860 | |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5861 | void |
| 5862 | Process::GetStatus (Stream &strm) |
| 5863 | { |
| 5864 | const StateType state = GetState(); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 5865 | if (StateIsStoppedState(state, false)) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5866 | { |
| 5867 | if (state == eStateExited) |
| 5868 | { |
| 5869 | int exit_status = GetExitStatus(); |
| 5870 | const char *exit_description = GetExitDescription(); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5871 | strm.Printf ("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n", |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5872 | GetID(), |
| 5873 | exit_status, |
| 5874 | exit_status, |
| 5875 | exit_description ? exit_description : ""); |
| 5876 | } |
| 5877 | else |
| 5878 | { |
| 5879 | if (state == eStateConnected) |
| 5880 | strm.Printf ("Connected to remote target.\n"); |
| 5881 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5882 | strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state)); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5883 | } |
| 5884 | } |
| 5885 | else |
| 5886 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5887 | strm.Printf ("Process %" PRIu64 " is running.\n", GetID()); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5888 | } |
| 5889 | } |
| 5890 | |
| 5891 | size_t |
| 5892 | Process::GetThreadStatus (Stream &strm, |
| 5893 | bool only_threads_with_stop_reason, |
| 5894 | uint32_t start_frame, |
| 5895 | uint32_t num_frames, |
| 5896 | uint32_t num_frames_with_source) |
| 5897 | { |
| 5898 | size_t num_thread_infos_dumped = 0; |
| 5899 | |
Jim Ingham | 41f2b94 | 2012-09-10 20:50:15 +0000 | [diff] [blame] | 5900 | Mutex::Locker locker (GetThreadList().GetMutex()); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5901 | const size_t num_threads = GetThreadList().GetSize(); |
| 5902 | for (uint32_t i = 0; i < num_threads; i++) |
| 5903 | { |
| 5904 | Thread *thread = GetThreadList().GetThreadAtIndex(i).get(); |
| 5905 | if (thread) |
| 5906 | { |
| 5907 | if (only_threads_with_stop_reason) |
| 5908 | { |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 5909 | StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 5910 | if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid()) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5911 | continue; |
| 5912 | } |
| 5913 | thread->GetStatus (strm, |
| 5914 | start_frame, |
| 5915 | num_frames, |
| 5916 | num_frames_with_source); |
| 5917 | ++num_thread_infos_dumped; |
| 5918 | } |
| 5919 | } |
| 5920 | return num_thread_infos_dumped; |
| 5921 | } |
| 5922 | |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 5923 | void |
| 5924 | Process::AddInvalidMemoryRegion (const LoadRange ®ion) |
| 5925 | { |
| 5926 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5927 | } |
| 5928 | |
| 5929 | bool |
| 5930 | Process::RemoveInvalidMemoryRange (const LoadRange ®ion) |
| 5931 | { |
| 5932 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5933 | } |
| 5934 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5935 | void |
| 5936 | Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton) |
| 5937 | { |
| 5938 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton)); |
| 5939 | } |
| 5940 | |
| 5941 | bool |
| 5942 | Process::RunPreResumeActions () |
| 5943 | { |
| 5944 | bool result = true; |
| 5945 | while (!m_pre_resume_actions.empty()) |
| 5946 | { |
| 5947 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 5948 | m_pre_resume_actions.pop_back(); |
| 5949 | bool this_result = action.callback (action.baton); |
| 5950 | if (result == true) result = this_result; |
| 5951 | } |
| 5952 | return result; |
| 5953 | } |
| 5954 | |
| 5955 | void |
| 5956 | Process::ClearPreResumeActions () |
| 5957 | { |
| 5958 | m_pre_resume_actions.clear(); |
| 5959 | } |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 5960 | |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 5961 | void |
| 5962 | Process::Flush () |
| 5963 | { |
| 5964 | m_thread_list.Flush(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 5965 | m_extended_thread_list.Flush(); |
| 5966 | m_extended_thread_stop_id = 0; |
| 5967 | m_queue_list.Clear(); |
| 5968 | m_queue_list_stop_id = 0; |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 5969 | } |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5970 | |
| 5971 | void |
| 5972 | Process::DidExec () |
| 5973 | { |
| 5974 | Target &target = GetTarget(); |
| 5975 | target.CleanupProcess (); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 5976 | target.ClearModules(false); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5977 | m_dynamic_checkers_ap.reset(); |
| 5978 | m_abi_sp.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 5979 | m_system_runtime_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5980 | m_os_ap.reset(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5981 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame^] | 5982 | m_jit_loaders_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5983 | m_image_tokens.clear(); |
| 5984 | m_allocated_memory_cache.Clear(); |
| 5985 | m_language_runtimes.clear(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5986 | m_thread_list.DiscardThreadPlans(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5987 | m_memory_cache.Clear(true); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5988 | DoDidExec(); |
| 5989 | CompleteAttach (); |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 5990 | // Flush the process (threads and all stack frames) after running CompleteAttach() |
| 5991 | // in case the dynamic loader loaded things in new locations. |
| 5992 | Flush(); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 5993 | |
| 5994 | // After we figure out what was loaded/unloaded in CompleteAttach, |
| 5995 | // we need to let the target know so it can do any cleanup it needs to. |
| 5996 | target.DidExec(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5997 | } |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 5998 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5999 | addr_t |
| 6000 | Process::ResolveIndirectFunction(const Address *address, Error &error) |
| 6001 | { |
| 6002 | if (address == nullptr) |
| 6003 | { |
Jean-Daniel Dupas | ef37711f | 2014-02-08 20:22:05 +0000 | [diff] [blame] | 6004 | error.SetErrorString("Invalid address argument"); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 6005 | return LLDB_INVALID_ADDRESS; |
| 6006 | } |
| 6007 | |
| 6008 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 6009 | |
| 6010 | addr_t addr = address->GetLoadAddress(&GetTarget()); |
| 6011 | std::map<addr_t,addr_t>::const_iterator iter = m_resolved_indirect_addresses.find(addr); |
| 6012 | if (iter != m_resolved_indirect_addresses.end()) |
| 6013 | { |
| 6014 | function_addr = (*iter).second; |
| 6015 | } |
| 6016 | else |
| 6017 | { |
| 6018 | if (!InferiorCall(this, address, function_addr)) |
| 6019 | { |
| 6020 | Symbol *symbol = address->CalculateSymbolContextSymbol(); |
| 6021 | error.SetErrorStringWithFormat ("Unable to call resolver for indirect function %s", |
| 6022 | symbol ? symbol->GetName().AsCString() : "<UNKNOWN>"); |
| 6023 | function_addr = LLDB_INVALID_ADDRESS; |
| 6024 | } |
| 6025 | else |
| 6026 | { |
| 6027 | m_resolved_indirect_addresses.insert(std::pair<addr_t, addr_t>(addr, function_addr)); |
| 6028 | } |
| 6029 | } |
| 6030 | return function_addr; |
| 6031 | } |
| 6032 | |