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" |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 21 | #include "lldb/Core/InputReader.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/Log.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 23 | #include "lldb/Core/Module.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 | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 26 | #include "lldb/Expression/ClangUserExpression.h" |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 27 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | #include "lldb/Host/Host.h" |
| 29 | #include "lldb/Target/ABI.h" |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 30 | #include "lldb/Target/DynamicLoader.h" |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 31 | #include "lldb/Target/OperatingSystem.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 32 | #include "lldb/Target/LanguageRuntime.h" |
| 33 | #include "lldb/Target/CPPLanguageRuntime.h" |
| 34 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 35 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 37 | #include "lldb/Target/StopInfo.h" |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 38 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "lldb/Target/Target.h" |
| 40 | #include "lldb/Target/TargetList.h" |
| 41 | #include "lldb/Target/Thread.h" |
| 42 | #include "lldb/Target/ThreadPlan.h" |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 43 | #include "lldb/Target/ThreadPlanBase.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 45 | #ifndef LLDB_DISABLE_POSIX |
| 46 | #include <spawn.h> |
| 47 | #endif |
| 48 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | using namespace lldb; |
| 50 | using namespace lldb_private; |
| 51 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 52 | |
| 53 | // Comment out line below to disable memory caching, overriding the process setting |
| 54 | // target.process.disable-memory-cache |
| 55 | #define ENABLE_MEMORY_CACHING |
| 56 | |
| 57 | #ifdef ENABLE_MEMORY_CACHING |
| 58 | #define DISABLE_MEM_CACHE_DEFAULT false |
| 59 | #else |
| 60 | #define DISABLE_MEM_CACHE_DEFAULT true |
| 61 | #endif |
| 62 | |
| 63 | class ProcessOptionValueProperties : public OptionValueProperties |
| 64 | { |
| 65 | public: |
| 66 | ProcessOptionValueProperties (const ConstString &name) : |
| 67 | OptionValueProperties (name) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | // This constructor is used when creating ProcessOptionValueProperties when it |
| 72 | // is part of a new lldb_private::Process instance. It will copy all current |
| 73 | // global property values as needed |
| 74 | ProcessOptionValueProperties (ProcessProperties *global_properties) : |
| 75 | OptionValueProperties(*global_properties->GetValueProperties()) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | virtual const Property * |
| 80 | GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const |
| 81 | { |
| 82 | // When gettings the value for a key from the process options, we will always |
| 83 | // try and grab the setting from the current process if there is one. Else we just |
| 84 | // use the one from this instance. |
| 85 | if (exe_ctx) |
| 86 | { |
| 87 | Process *process = exe_ctx->GetProcessPtr(); |
| 88 | if (process) |
| 89 | { |
| 90 | ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get()); |
| 91 | if (this != instance_properties) |
| 92 | return instance_properties->ProtectedGetPropertyAtIndex (idx); |
| 93 | } |
| 94 | } |
| 95 | return ProtectedGetPropertyAtIndex (idx); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | static PropertyDefinition |
| 100 | g_properties[] = |
| 101 | { |
| 102 | { "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] | 103 | { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. " |
| 104 | "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] | 105 | { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." }, |
| 106 | { "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] | 107 | { "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] | 108 | { "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] | 109 | { "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] | 110 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 111 | }; |
| 112 | |
| 113 | enum { |
| 114 | ePropertyDisableMemCache, |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 115 | ePropertyExtraStartCommand, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 116 | ePropertyIgnoreBreakpointsInExpressions, |
| 117 | ePropertyUnwindOnErrorInExpressions, |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 118 | ePropertyPythonOSPluginPath, |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 119 | ePropertyStopOnSharedLibraryEvents, |
| 120 | ePropertyDetachKeepsStopped |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | ProcessProperties::ProcessProperties (bool is_global) : |
| 124 | Properties () |
| 125 | { |
| 126 | if (is_global) |
| 127 | { |
| 128 | m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process"))); |
| 129 | m_collection_sp->Initialize(g_properties); |
| 130 | m_collection_sp->AppendProperty(ConstString("thread"), |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 131 | ConstString("Settings specific to threads."), |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 132 | true, |
| 133 | Thread::GetGlobalProperties()->GetValueProperties()); |
| 134 | } |
| 135 | else |
| 136 | m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get())); |
| 137 | } |
| 138 | |
| 139 | ProcessProperties::~ProcessProperties() |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | bool |
| 144 | ProcessProperties::GetDisableMemoryCache() const |
| 145 | { |
| 146 | const uint32_t idx = ePropertyDisableMemCache; |
| 147 | return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); |
| 148 | } |
| 149 | |
| 150 | Args |
| 151 | ProcessProperties::GetExtraStartupCommands () const |
| 152 | { |
| 153 | Args args; |
| 154 | const uint32_t idx = ePropertyExtraStartCommand; |
| 155 | m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args); |
| 156 | return args; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | ProcessProperties::SetExtraStartupCommands (const Args &args) |
| 161 | { |
| 162 | const uint32_t idx = ePropertyExtraStartCommand; |
| 163 | m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args); |
| 164 | } |
| 165 | |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 166 | FileSpec |
| 167 | ProcessProperties::GetPythonOSPluginPath () const |
| 168 | { |
| 169 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 170 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx); |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | ProcessProperties::SetPythonOSPluginPath (const FileSpec &file) |
| 175 | { |
| 176 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 177 | m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file); |
| 178 | } |
| 179 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 180 | |
| 181 | bool |
| 182 | ProcessProperties::GetIgnoreBreakpointsInExpressions () const |
| 183 | { |
| 184 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 185 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore) |
| 190 | { |
| 191 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 192 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 193 | } |
| 194 | |
| 195 | bool |
| 196 | ProcessProperties::GetUnwindOnErrorInExpressions () const |
| 197 | { |
| 198 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 199 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore) |
| 204 | { |
| 205 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 206 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 207 | } |
| 208 | |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 209 | bool |
| 210 | ProcessProperties::GetStopOnSharedLibraryEvents () const |
| 211 | { |
| 212 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 213 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | ProcessProperties::SetStopOnSharedLibraryEvents (bool stop) |
| 218 | { |
| 219 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 220 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 221 | } |
| 222 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 223 | bool |
| 224 | ProcessProperties::GetDetachKeepsStopped () const |
| 225 | { |
| 226 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 227 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | ProcessProperties::SetDetachKeepsStopped (bool stop) |
| 232 | { |
| 233 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 234 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 235 | } |
| 236 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 237 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 238 | ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 239 | { |
| 240 | const char *cstr; |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 241 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 242 | s.Printf (" pid = %" PRIu64 "\n", m_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 243 | |
| 244 | if (m_parent_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 245 | s.Printf (" parent = %" PRIu64 "\n", m_parent_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 246 | |
| 247 | if (m_executable) |
| 248 | { |
| 249 | s.Printf (" name = %s\n", m_executable.GetFilename().GetCString()); |
| 250 | s.PutCString (" file = "); |
| 251 | m_executable.Dump(&s); |
| 252 | s.EOL(); |
| 253 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 254 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 255 | if (argc > 0) |
| 256 | { |
| 257 | for (uint32_t i=0; i<argc; i++) |
| 258 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 259 | const char *arg = m_arguments.GetArgumentAtIndex(i); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 260 | if (i < 10) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 261 | s.Printf (" arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 262 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 263 | s.Printf ("arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 266 | |
| 267 | const uint32_t envc = m_environment.GetArgumentCount(); |
| 268 | if (envc > 0) |
| 269 | { |
| 270 | for (uint32_t i=0; i<envc; i++) |
| 271 | { |
| 272 | const char *env = m_environment.GetArgumentAtIndex(i); |
| 273 | if (i < 10) |
| 274 | s.Printf (" env[%u] = %s\n", i, env); |
| 275 | else |
| 276 | s.Printf ("env[%u] = %s\n", i, env); |
| 277 | } |
| 278 | } |
| 279 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 280 | if (m_arch.IsValid()) |
| 281 | s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str()); |
| 282 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 283 | if (m_uid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 284 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 285 | cstr = platform->GetUserName (m_uid); |
| 286 | s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 287 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 288 | if (m_gid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 289 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 290 | cstr = platform->GetGroupName (m_gid); |
| 291 | s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 292 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 293 | if (m_euid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 294 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 295 | cstr = platform->GetUserName (m_euid); |
| 296 | s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 297 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 298 | if (m_egid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 299 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 300 | cstr = platform->GetGroupName (m_egid); |
| 301 | s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 306 | ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 307 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 308 | const char *label; |
| 309 | if (show_args || verbose) |
| 310 | label = "ARGUMENTS"; |
| 311 | else |
| 312 | label = "NAME"; |
| 313 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 314 | if (verbose) |
| 315 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 316 | 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] | 317 | s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n"); |
| 318 | } |
| 319 | else |
| 320 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 321 | s.Printf ("PID PARENT USER ARCH %s\n", label); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 322 | s.PutCString ("====== ====== ========== ======= ============================\n"); |
| 323 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 327 | ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 328 | { |
| 329 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
| 330 | { |
| 331 | const char *cstr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 332 | s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 333 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 334 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 335 | if (verbose) |
| 336 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 337 | cstr = platform->GetUserName (m_uid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 338 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 339 | s.Printf ("%-10s ", cstr); |
| 340 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 341 | s.Printf ("%-10u ", m_uid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 342 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 343 | cstr = platform->GetGroupName (m_gid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 344 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 345 | s.Printf ("%-10s ", cstr); |
| 346 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 347 | s.Printf ("%-10u ", m_gid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 348 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 349 | cstr = platform->GetUserName (m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 350 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 351 | s.Printf ("%-10s ", cstr); |
| 352 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 353 | s.Printf ("%-10u ", m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 354 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 355 | cstr = platform->GetGroupName (m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 356 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 357 | s.Printf ("%-10s ", cstr); |
| 358 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 359 | s.Printf ("%-10u ", m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 360 | s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : ""); |
| 361 | } |
| 362 | else |
| 363 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 364 | s.Printf ("%-10s %-7d %s ", |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 365 | platform->GetUserName (m_euid), |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 366 | (int)m_arch.GetTriple().getArchName().size(), |
| 367 | m_arch.GetTriple().getArchName().data()); |
| 368 | } |
| 369 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 370 | if (verbose || show_args) |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 371 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 372 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 373 | if (argc > 0) |
| 374 | { |
| 375 | for (uint32_t i=0; i<argc; i++) |
| 376 | { |
| 377 | if (i > 0) |
| 378 | s.PutChar (' '); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 379 | s.PutCString (m_arguments.GetArgumentAtIndex(i)); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | s.PutCString (GetName()); |
| 386 | } |
| 387 | |
| 388 | s.EOL(); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 392 | |
| 393 | void |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 394 | ProcessInfo::SetArguments (char const **argv, bool first_arg_is_executable) |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 395 | { |
| 396 | m_arguments.SetArguments (argv); |
| 397 | |
| 398 | // Is the first argument the executable? |
| 399 | if (first_arg_is_executable) |
| 400 | { |
| 401 | const char *first_arg = m_arguments.GetArgumentAtIndex (0); |
| 402 | if (first_arg) |
| 403 | { |
| 404 | // Yes the first argument is an executable, set it as the executable |
| 405 | // in the launch options. Don't resolve the file path as the path |
| 406 | // could be a remote platform path |
| 407 | const bool resolve = false; |
| 408 | m_executable.SetFile(first_arg, resolve); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | void |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 413 | ProcessInfo::SetArguments (const Args& args, bool first_arg_is_executable) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 414 | { |
| 415 | // Copy all arguments |
| 416 | m_arguments = args; |
| 417 | |
| 418 | // Is the first argument the executable? |
| 419 | if (first_arg_is_executable) |
| 420 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 421 | const char *first_arg = m_arguments.GetArgumentAtIndex (0); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 422 | if (first_arg) |
| 423 | { |
| 424 | // Yes the first argument is an executable, set it as the executable |
| 425 | // in the launch options. Don't resolve the file path as the path |
| 426 | // could be a remote platform path |
| 427 | const bool resolve = false; |
| 428 | m_executable.SetFile(first_arg, resolve); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 433 | void |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 434 | ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty) |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 435 | { |
| 436 | // If notthing was specified, then check the process for any default |
| 437 | // settings that were set with "settings set" |
| 438 | if (m_file_actions.empty()) |
| 439 | { |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 440 | if (m_flags.Test(eLaunchFlagDisableSTDIO)) |
| 441 | { |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 442 | AppendSuppressFileAction (STDIN_FILENO , true, false); |
| 443 | AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 444 | AppendSuppressFileAction (STDERR_FILENO, false, true); |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 445 | } |
| 446 | else |
| 447 | { |
| 448 | // Check for any values that might have gotten set with any of: |
| 449 | // (lldb) settings set target.input-path |
| 450 | // (lldb) settings set target.output-path |
| 451 | // (lldb) settings set target.error-path |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 452 | FileSpec in_path; |
| 453 | FileSpec out_path; |
| 454 | FileSpec err_path; |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 455 | if (target) |
| 456 | { |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 457 | in_path = target->GetStandardInputPath(); |
| 458 | out_path = target->GetStandardOutputPath(); |
| 459 | err_path = target->GetStandardErrorPath(); |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 462 | if (in_path || out_path || err_path) |
| 463 | { |
| 464 | char path[PATH_MAX]; |
| 465 | if (in_path && in_path.GetPath(path, sizeof(path))) |
| 466 | AppendOpenFileAction(STDIN_FILENO, path, true, false); |
| 467 | |
| 468 | if (out_path && out_path.GetPath(path, sizeof(path))) |
| 469 | AppendOpenFileAction(STDOUT_FILENO, path, false, true); |
| 470 | |
| 471 | if (err_path && err_path.GetPath(path, sizeof(path))) |
| 472 | AppendOpenFileAction(STDERR_FILENO, path, false, true); |
| 473 | } |
| 474 | else if (default_to_use_pty) |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 475 | { |
| 476 | if (m_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, NULL, 0)) |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 477 | { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 478 | const char *slave_path = m_pty.GetSlaveName (NULL, 0); |
| 479 | AppendOpenFileAction(STDIN_FILENO, slave_path, true, false); |
| 480 | AppendOpenFileAction(STDOUT_FILENO, slave_path, false, true); |
| 481 | AppendOpenFileAction(STDERR_FILENO, slave_path, false, true); |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 488 | |
| 489 | bool |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 490 | ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error, |
| 491 | bool localhost, |
| 492 | bool will_debug, |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 493 | bool first_arg_is_full_shell_command, |
| 494 | int32_t num_resumes) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 495 | { |
| 496 | error.Clear(); |
| 497 | |
| 498 | if (GetFlags().Test (eLaunchFlagLaunchInShell)) |
| 499 | { |
| 500 | const char *shell_executable = GetShell(); |
| 501 | if (shell_executable) |
| 502 | { |
| 503 | char shell_resolved_path[PATH_MAX]; |
| 504 | |
| 505 | if (localhost) |
| 506 | { |
| 507 | FileSpec shell_filespec (shell_executable, true); |
| 508 | |
| 509 | if (!shell_filespec.Exists()) |
| 510 | { |
| 511 | // Resolve the path in case we just got "bash", "sh" or "tcsh" |
| 512 | if (!shell_filespec.ResolveExecutableLocation ()) |
| 513 | { |
| 514 | error.SetErrorStringWithFormat("invalid shell path '%s'", shell_executable); |
| 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | shell_filespec.GetPath (shell_resolved_path, sizeof(shell_resolved_path)); |
| 519 | shell_executable = shell_resolved_path; |
| 520 | } |
| 521 | |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 522 | const char **argv = GetArguments().GetConstArgumentVector (); |
| 523 | if (argv == NULL || argv[0] == NULL) |
| 524 | return false; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 525 | Args shell_arguments; |
| 526 | std::string safe_arg; |
| 527 | shell_arguments.AppendArgument (shell_executable); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 528 | shell_arguments.AppendArgument ("-c"); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 529 | StreamString shell_command; |
| 530 | if (will_debug) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 531 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 532 | // Add a modified PATH environment variable in case argv[0] |
| 533 | // is a relative path |
| 534 | const char *argv0 = argv[0]; |
| 535 | if (argv0 && (argv0[0] != '/' && argv0[0] != '~')) |
| 536 | { |
| 537 | // We have a relative path to our executable which may not work if |
| 538 | // we just try to run "a.out" (without it being converted to "./a.out") |
| 539 | const char *working_dir = GetWorkingDirectory(); |
Greg Clayton | 8938f8d | 2013-02-14 03:54:39 +0000 | [diff] [blame] | 540 | // Be sure to put quotes around PATH's value in case any paths have spaces... |
| 541 | std::string new_path("PATH=\""); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 542 | const size_t empty_path_len = new_path.size(); |
| 543 | |
| 544 | if (working_dir && working_dir[0]) |
| 545 | { |
| 546 | new_path += working_dir; |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | char current_working_dir[PATH_MAX]; |
| 551 | const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir)); |
| 552 | if (cwd && cwd[0]) |
| 553 | new_path += cwd; |
| 554 | } |
| 555 | const char *curr_path = getenv("PATH"); |
| 556 | if (curr_path) |
| 557 | { |
| 558 | if (new_path.size() > empty_path_len) |
| 559 | new_path += ':'; |
| 560 | new_path += curr_path; |
| 561 | } |
Greg Clayton | 8938f8d | 2013-02-14 03:54:39 +0000 | [diff] [blame] | 562 | new_path += "\" "; |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 563 | shell_command.PutCString(new_path.c_str()); |
| 564 | } |
| 565 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 566 | shell_command.PutCString ("exec"); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 567 | |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 568 | // Only Apple supports /usr/bin/arch being able to specify the architecture |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 569 | if (GetArchitecture().IsValid()) |
| 570 | { |
| 571 | shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName()); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 572 | // Set the resume count to 2: |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 573 | // 1 - stop in shell |
| 574 | // 2 - stop in /usr/bin/arch |
| 575 | // 3 - then we will stop in our program |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 576 | SetResumeCount(num_resumes + 1); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 577 | } |
| 578 | else |
| 579 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 580 | // Set the resume count to 1: |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 581 | // 1 - stop in shell |
| 582 | // 2 - then we will stop in our program |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 583 | SetResumeCount(num_resumes); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 584 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 585 | } |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 586 | |
| 587 | if (first_arg_is_full_shell_command) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 588 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 589 | // There should only be one argument that is the shell command itself to be used as is |
| 590 | if (argv[0] && !argv[1]) |
| 591 | shell_command.Printf("%s", argv[0]); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 592 | else |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 593 | return false; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 594 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 595 | else |
| 596 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 597 | for (size_t i=0; argv[i] != NULL; ++i) |
| 598 | { |
| 599 | const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg); |
| 600 | shell_command.Printf(" %s", arg); |
| 601 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 602 | } |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 603 | shell_arguments.AppendArgument (shell_command.GetString().c_str()); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 604 | m_executable.SetFile(shell_executable, false); |
| 605 | m_arguments = shell_arguments; |
| 606 | return true; |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | error.SetErrorString ("invalid shell path"); |
| 611 | } |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | error.SetErrorString ("not launching in shell"); |
| 616 | } |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 621 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 622 | ProcessLaunchInfo::FileAction::Open (int fd, const char *path, bool read, bool write) |
| 623 | { |
| 624 | if ((read || write) && fd >= 0 && path && path[0]) |
| 625 | { |
| 626 | m_action = eFileActionOpen; |
| 627 | m_fd = fd; |
| 628 | if (read && write) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 629 | m_arg = O_NOCTTY | O_CREAT | O_RDWR; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 630 | else if (read) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 631 | m_arg = O_NOCTTY | O_RDONLY; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 632 | else |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 633 | m_arg = O_NOCTTY | O_CREAT | O_WRONLY; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 634 | m_path.assign (path); |
| 635 | return true; |
| 636 | } |
| 637 | else |
| 638 | { |
| 639 | Clear(); |
| 640 | } |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | bool |
| 645 | ProcessLaunchInfo::FileAction::Close (int fd) |
| 646 | { |
| 647 | Clear(); |
| 648 | if (fd >= 0) |
| 649 | { |
| 650 | m_action = eFileActionClose; |
| 651 | m_fd = fd; |
| 652 | } |
| 653 | return m_fd >= 0; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | bool |
| 658 | ProcessLaunchInfo::FileAction::Duplicate (int fd, int dup_fd) |
| 659 | { |
| 660 | Clear(); |
| 661 | if (fd >= 0 && dup_fd >= 0) |
| 662 | { |
| 663 | m_action = eFileActionDuplicate; |
| 664 | m_fd = fd; |
| 665 | m_arg = dup_fd; |
| 666 | } |
| 667 | return m_fd >= 0; |
| 668 | } |
| 669 | |
| 670 | |
| 671 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 672 | #ifndef LLDB_DISABLE_POSIX |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 673 | bool |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 674 | ProcessLaunchInfo::FileAction::AddPosixSpawnFileAction (void *_file_actions, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 675 | const FileAction *info, |
| 676 | Log *log, |
| 677 | Error& error) |
| 678 | { |
| 679 | if (info == NULL) |
| 680 | return false; |
| 681 | |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 682 | posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions); |
| 683 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 684 | switch (info->m_action) |
| 685 | { |
| 686 | case eFileActionNone: |
| 687 | error.Clear(); |
| 688 | break; |
| 689 | |
| 690 | case eFileActionClose: |
| 691 | if (info->m_fd == -1) |
| 692 | error.SetErrorString ("invalid fd for posix_spawn_file_actions_addclose(...)"); |
| 693 | else |
| 694 | { |
| 695 | error.SetError (::posix_spawn_file_actions_addclose (file_actions, info->m_fd), |
| 696 | eErrorTypePOSIX); |
| 697 | if (log && (error.Fail() || log)) |
| 698 | error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)", |
| 699 | file_actions, info->m_fd); |
| 700 | } |
| 701 | break; |
| 702 | |
| 703 | case eFileActionDuplicate: |
| 704 | if (info->m_fd == -1) |
| 705 | error.SetErrorString ("invalid fd for posix_spawn_file_actions_adddup2(...)"); |
| 706 | else if (info->m_arg == -1) |
| 707 | error.SetErrorString ("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)"); |
| 708 | else |
| 709 | { |
| 710 | error.SetError (::posix_spawn_file_actions_adddup2 (file_actions, info->m_fd, info->m_arg), |
| 711 | eErrorTypePOSIX); |
| 712 | if (log && (error.Fail() || log)) |
| 713 | error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)", |
| 714 | file_actions, info->m_fd, info->m_arg); |
| 715 | } |
| 716 | break; |
| 717 | |
| 718 | case eFileActionOpen: |
| 719 | if (info->m_fd == -1) |
| 720 | error.SetErrorString ("invalid fd in posix_spawn_file_actions_addopen(...)"); |
| 721 | else |
| 722 | { |
| 723 | int oflag = info->m_arg; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 724 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 725 | mode_t mode = 0; |
| 726 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 727 | if (oflag & O_CREAT) |
| 728 | mode = 0640; |
| 729 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 730 | error.SetError (::posix_spawn_file_actions_addopen (file_actions, |
| 731 | info->m_fd, |
| 732 | info->m_path.c_str(), |
| 733 | oflag, |
| 734 | mode), |
| 735 | eErrorTypePOSIX); |
| 736 | if (error.Fail() || log) |
| 737 | error.PutToLog(log, |
| 738 | "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)", |
| 739 | file_actions, info->m_fd, info->m_path.c_str(), oflag, mode); |
| 740 | } |
| 741 | break; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 742 | } |
| 743 | return error.Success(); |
| 744 | } |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 745 | #endif |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 746 | |
| 747 | Error |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 748 | ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 749 | { |
| 750 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 751 | const int short_option = m_getopt_table[option_idx].val; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 752 | |
| 753 | switch (short_option) |
| 754 | { |
| 755 | case 's': // Stop at program entry point |
| 756 | launch_info.GetFlags().Set (eLaunchFlagStopAtEntry); |
| 757 | break; |
| 758 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 759 | case 'i': // STDIN for read only |
| 760 | { |
| 761 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 762 | if (action.Open (STDIN_FILENO, option_arg, true, false)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 763 | launch_info.AppendFileAction (action); |
| 764 | } |
| 765 | break; |
| 766 | |
| 767 | case 'o': // Open STDOUT for write only |
| 768 | { |
| 769 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 770 | if (action.Open (STDOUT_FILENO, option_arg, false, true)) |
| 771 | launch_info.AppendFileAction (action); |
| 772 | } |
| 773 | break; |
| 774 | |
| 775 | case 'e': // STDERR for write only |
| 776 | { |
| 777 | ProcessLaunchInfo::FileAction action; |
| 778 | if (action.Open (STDERR_FILENO, option_arg, false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 779 | launch_info.AppendFileAction (action); |
| 780 | } |
| 781 | break; |
| 782 | |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 783 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 784 | case 'p': // Process plug-in name |
| 785 | launch_info.SetProcessPluginName (option_arg); |
| 786 | break; |
| 787 | |
| 788 | case 'n': // Disable STDIO |
| 789 | { |
| 790 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 791 | if (action.Open (STDIN_FILENO, "/dev/null", true, false)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 792 | launch_info.AppendFileAction (action); |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 793 | if (action.Open (STDOUT_FILENO, "/dev/null", false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 794 | launch_info.AppendFileAction (action); |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 795 | if (action.Open (STDERR_FILENO, "/dev/null", false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 796 | launch_info.AppendFileAction (action); |
| 797 | } |
| 798 | break; |
| 799 | |
| 800 | case 'w': |
| 801 | launch_info.SetWorkingDirectory (option_arg); |
| 802 | break; |
| 803 | |
| 804 | case 't': // Open process in new terminal window |
| 805 | launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); |
| 806 | break; |
| 807 | |
| 808 | case 'a': |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 809 | if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get())) |
| 810 | launch_info.GetArchitecture().SetTriple (option_arg); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 811 | break; |
| 812 | |
| 813 | case 'A': |
| 814 | launch_info.GetFlags().Set (eLaunchFlagDisableASLR); |
| 815 | break; |
| 816 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 817 | case 'c': |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 818 | if (option_arg && option_arg[0]) |
| 819 | launch_info.SetShell (option_arg); |
| 820 | else |
Ed Maste | b8ca4a2 | 2013-09-03 23:04:53 +0000 | [diff] [blame] | 821 | launch_info.SetShell (LLDB_DEFAULT_SHELL); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 822 | break; |
| 823 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 824 | case 'v': |
| 825 | launch_info.GetEnvironmentEntries().AppendArgument(option_arg); |
| 826 | break; |
| 827 | |
| 828 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 829 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 830 | break; |
| 831 | |
| 832 | } |
| 833 | return error; |
| 834 | } |
| 835 | |
| 836 | OptionDefinition |
| 837 | ProcessLaunchCommandOptions::g_option_table[] = |
| 838 | { |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 839 | { 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."}, |
| 840 | { LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Disable address space layout randomization when launching a process."}, |
| 841 | { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 842 | { LLDB_OPT_SET_ALL, false, "working-dir", 'w', OptionParser::eRequiredArgument, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."}, |
| 843 | { LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."}, |
| 844 | { 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."}, |
| 845 | { 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] | 846 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 847 | { LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."}, |
| 848 | { LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."}, |
| 849 | { 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] | 850 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 851 | { 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] | 852 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 853 | { 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] | 854 | |
| 855 | { 0 , false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
| 856 | }; |
| 857 | |
| 858 | |
| 859 | |
| 860 | bool |
| 861 | ProcessInstanceInfoMatch::NameMatches (const char *process_name) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 862 | { |
| 863 | if (m_name_match_type == eNameMatchIgnore || process_name == NULL) |
| 864 | return true; |
| 865 | const char *match_name = m_match_info.GetName(); |
| 866 | if (!match_name) |
| 867 | return true; |
| 868 | |
| 869 | return lldb_private::NameMatches (process_name, m_name_match_type, match_name); |
| 870 | } |
| 871 | |
| 872 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 873 | ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 874 | { |
| 875 | if (!NameMatches (proc_info.GetName())) |
| 876 | return false; |
| 877 | |
| 878 | if (m_match_info.ProcessIDIsValid() && |
| 879 | m_match_info.GetProcessID() != proc_info.GetProcessID()) |
| 880 | return false; |
| 881 | |
| 882 | if (m_match_info.ParentProcessIDIsValid() && |
| 883 | m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) |
| 884 | return false; |
| 885 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 886 | if (m_match_info.UserIDIsValid () && |
| 887 | m_match_info.GetUserID() != proc_info.GetUserID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 888 | return false; |
| 889 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 890 | if (m_match_info.GroupIDIsValid () && |
| 891 | m_match_info.GetGroupID() != proc_info.GetGroupID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 892 | return false; |
| 893 | |
| 894 | if (m_match_info.EffectiveUserIDIsValid () && |
| 895 | m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) |
| 896 | return false; |
| 897 | |
| 898 | if (m_match_info.EffectiveGroupIDIsValid () && |
| 899 | m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) |
| 900 | return false; |
| 901 | |
| 902 | if (m_match_info.GetArchitecture().IsValid() && |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 903 | !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture())) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 904 | return false; |
| 905 | return true; |
| 906 | } |
| 907 | |
| 908 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 909 | ProcessInstanceInfoMatch::MatchAllProcesses () const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 910 | { |
| 911 | if (m_name_match_type != eNameMatchIgnore) |
| 912 | return false; |
| 913 | |
| 914 | if (m_match_info.ProcessIDIsValid()) |
| 915 | return false; |
| 916 | |
| 917 | if (m_match_info.ParentProcessIDIsValid()) |
| 918 | return false; |
| 919 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 920 | if (m_match_info.UserIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 921 | return false; |
| 922 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 923 | if (m_match_info.GroupIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 924 | return false; |
| 925 | |
| 926 | if (m_match_info.EffectiveUserIDIsValid ()) |
| 927 | return false; |
| 928 | |
| 929 | if (m_match_info.EffectiveGroupIDIsValid ()) |
| 930 | return false; |
| 931 | |
| 932 | if (m_match_info.GetArchitecture().IsValid()) |
| 933 | return false; |
| 934 | |
| 935 | if (m_match_all_users) |
| 936 | return false; |
| 937 | |
| 938 | return true; |
| 939 | |
| 940 | } |
| 941 | |
| 942 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 943 | ProcessInstanceInfoMatch::Clear() |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 944 | { |
| 945 | m_match_info.Clear(); |
| 946 | m_name_match_type = eNameMatchIgnore; |
| 947 | m_match_all_users = false; |
| 948 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 949 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 950 | ProcessSP |
| 951 | 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] | 952 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 953 | static uint32_t g_process_unique_id = 0; |
| 954 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 955 | ProcessSP process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 956 | ProcessCreateInstance create_callback = NULL; |
| 957 | if (plugin_name) |
| 958 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 959 | ConstString const_plugin_name(plugin_name); |
| 960 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 961 | if (create_callback) |
| 962 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 963 | process_sp = create_callback(target, listener, crash_file_path); |
| 964 | if (process_sp) |
| 965 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 966 | if (process_sp->CanDebug(target, true)) |
| 967 | { |
| 968 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 969 | } |
| 970 | else |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 971 | process_sp.reset(); |
| 972 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | else |
| 976 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 977 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 978 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 979 | process_sp = create_callback(target, listener, crash_file_path); |
| 980 | if (process_sp) |
| 981 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 982 | if (process_sp->CanDebug(target, false)) |
| 983 | { |
| 984 | process_sp->m_process_unique_id = ++g_process_unique_id; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 985 | break; |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 986 | } |
| 987 | else |
| 988 | process_sp.reset(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 989 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 992 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 995 | ConstString & |
| 996 | Process::GetStaticBroadcasterClass () |
| 997 | { |
| 998 | static ConstString class_name ("lldb.process"); |
| 999 | return class_name; |
| 1000 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1001 | |
| 1002 | //---------------------------------------------------------------------- |
| 1003 | // Process constructor |
| 1004 | //---------------------------------------------------------------------- |
| 1005 | Process::Process(Target &target, Listener &listener) : |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1006 | ProcessProperties (false), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1007 | UserID (LLDB_INVALID_PROCESS_ID), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1008 | Broadcaster (&(target.GetDebugger()), "lldb.process"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1009 | m_target (target), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1010 | m_public_state (eStateUnloaded), |
| 1011 | m_private_state (eStateUnloaded), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1012 | m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"), |
| 1013 | m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1014 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 1015 | m_private_state_control_wait(), |
| 1016 | m_private_state_thread (LLDB_INVALID_HOST_THREAD), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1017 | m_mod_id (), |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 1018 | m_process_unique_id(0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1019 | m_thread_index_id (0), |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1020 | m_thread_id_to_index_id_map (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1021 | m_exit_status (-1), |
| 1022 | m_exit_string (), |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1023 | m_thread_mutex (Mutex::eMutexTypeRecursive), |
| 1024 | m_thread_list_real (this), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1025 | m_thread_list (this), |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 1026 | m_extended_thread_list (this), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1027 | m_notifications (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1028 | m_image_tokens (), |
| 1029 | m_listener (listener), |
| 1030 | m_breakpoint_site_list (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1031 | m_dynamic_checkers_ap (), |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1032 | m_unix_signals (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1033 | m_abi_sp (), |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1034 | m_process_input_reader (), |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1035 | m_stdio_communication ("process.stdio"), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1036 | m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1037 | m_stdout_data (), |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 1038 | m_stderr_data (), |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1039 | m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive), |
| 1040 | m_profile_data (), |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 1041 | m_memory_cache (*this), |
| 1042 | m_allocated_memory_cache (*this), |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1043 | m_should_detach (false), |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 1044 | m_next_event_action_ap(), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 1045 | m_public_run_lock (), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 1046 | m_private_run_lock (), |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 1047 | m_currently_handling_event(false), |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 1048 | m_finalize_called(false), |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 1049 | m_clear_thread_plans_on_stop (false), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 1050 | m_last_broadcast_state (eStateInvalid), |
Jason Molenda | 69b6b63 | 2013-03-05 03:33:59 +0000 | [diff] [blame] | 1051 | m_destroy_in_process (false), |
| 1052 | m_can_jit(eCanJITDontKnow) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1053 | { |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1054 | CheckInWithManager (); |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 1055 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1056 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1057 | if (log) |
| 1058 | log->Printf ("%p Process::Process()", this); |
| 1059 | |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 1060 | SetEventName (eBroadcastBitStateChanged, "state-changed"); |
| 1061 | SetEventName (eBroadcastBitInterrupt, "interrupt"); |
| 1062 | SetEventName (eBroadcastBitSTDOUT, "stdout-available"); |
| 1063 | SetEventName (eBroadcastBitSTDERR, "stderr-available"); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1064 | SetEventName (eBroadcastBitProfileData, "profile-data-available"); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 1065 | |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1066 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" ); |
| 1067 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" ); |
| 1068 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume"); |
| 1069 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1070 | listener.StartListeningForEvents (this, |
| 1071 | eBroadcastBitStateChanged | |
| 1072 | eBroadcastBitInterrupt | |
| 1073 | eBroadcastBitSTDOUT | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1074 | eBroadcastBitSTDERR | |
| 1075 | eBroadcastBitProfileData); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | |
| 1077 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1078 | eBroadcastBitStateChanged | |
| 1079 | eBroadcastBitInterrupt); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1080 | |
| 1081 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 1082 | eBroadcastInternalStateControlStop | |
| 1083 | eBroadcastInternalStateControlPause | |
| 1084 | eBroadcastInternalStateControlResume); |
| 1085 | } |
| 1086 | |
| 1087 | //---------------------------------------------------------------------- |
| 1088 | // Destructor |
| 1089 | //---------------------------------------------------------------------- |
| 1090 | Process::~Process() |
| 1091 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1092 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1093 | if (log) |
| 1094 | log->Printf ("%p Process::~Process()", this); |
| 1095 | StopPrivateStateThread(); |
| 1096 | } |
| 1097 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1098 | const ProcessPropertiesSP & |
| 1099 | Process::GetGlobalProperties() |
| 1100 | { |
| 1101 | static ProcessPropertiesSP g_settings_sp; |
| 1102 | if (!g_settings_sp) |
| 1103 | g_settings_sp.reset (new ProcessProperties (true)); |
| 1104 | return g_settings_sp; |
| 1105 | } |
| 1106 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1107 | void |
| 1108 | Process::Finalize() |
| 1109 | { |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1110 | switch (GetPrivateState()) |
| 1111 | { |
| 1112 | case eStateConnected: |
| 1113 | case eStateAttaching: |
| 1114 | case eStateLaunching: |
| 1115 | case eStateStopped: |
| 1116 | case eStateRunning: |
| 1117 | case eStateStepping: |
| 1118 | case eStateCrashed: |
| 1119 | case eStateSuspended: |
| 1120 | if (GetShouldDetach()) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1121 | { |
| 1122 | // FIXME: This will have to be a process setting: |
| 1123 | bool keep_stopped = false; |
| 1124 | Detach(keep_stopped); |
| 1125 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1126 | else |
| 1127 | Destroy(); |
| 1128 | break; |
| 1129 | |
| 1130 | case eStateInvalid: |
| 1131 | case eStateUnloaded: |
| 1132 | case eStateDetached: |
| 1133 | case eStateExited: |
| 1134 | break; |
| 1135 | } |
| 1136 | |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 1137 | // Clear our broadcaster before we proceed with destroying |
| 1138 | Broadcaster::Clear(); |
| 1139 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1140 | // Do any cleanup needed prior to being destructed... Subclasses |
| 1141 | // that override this method should call this superclass method as well. |
Jim Ingham | d0a3e12 | 2011-02-16 17:54:55 +0000 | [diff] [blame] | 1142 | |
| 1143 | // We need to destroy the loader before the derived Process class gets destroyed |
| 1144 | // 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] | 1145 | m_dynamic_checkers_ap.reset(); |
| 1146 | m_abi_sp.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1147 | m_os_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 1148 | m_system_runtime_ap.reset(); |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 1149 | m_dyld_ap.reset(); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1150 | m_thread_list_real.Destroy(); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 1151 | m_thread_list.Destroy(); |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 1152 | m_extended_thread_list.Destroy(); |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 1153 | std::vector<Notifications> empty_notifications; |
| 1154 | m_notifications.swap(empty_notifications); |
| 1155 | m_image_tokens.clear(); |
| 1156 | m_memory_cache.Clear(); |
| 1157 | m_allocated_memory_cache.Clear(); |
| 1158 | m_language_runtimes.clear(); |
| 1159 | m_next_event_action_ap.reset(); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1160 | //#ifdef LLDB_CONFIGURATION_DEBUG |
| 1161 | // StreamFile s(stdout, false); |
| 1162 | // EventSP event_sp; |
| 1163 | // while (m_private_state_listener.GetNextEvent(event_sp)) |
| 1164 | // { |
| 1165 | // event_sp->Dump (&s); |
| 1166 | // s.EOL(); |
| 1167 | // } |
| 1168 | //#endif |
| 1169 | // We have to be very careful here as the m_private_state_listener might |
| 1170 | // contain events that have ProcessSP values in them which can keep this |
| 1171 | // process around forever. These events need to be cleared out. |
| 1172 | m_private_state_listener.Clear(); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1173 | m_public_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 1174 | m_public_run_lock.SetStopped(); |
| 1175 | m_private_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 1176 | m_private_run_lock.SetStopped(); |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 1177 | m_finalize_called = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | void |
| 1181 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 1182 | { |
| 1183 | m_notifications.push_back(callbacks); |
| 1184 | if (callbacks.initialize != NULL) |
| 1185 | callbacks.initialize (callbacks.baton, this); |
| 1186 | } |
| 1187 | |
| 1188 | bool |
| 1189 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 1190 | { |
| 1191 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 1192 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 1193 | { |
| 1194 | if (pos->baton == callbacks.baton && |
| 1195 | pos->initialize == callbacks.initialize && |
| 1196 | pos->process_state_changed == callbacks.process_state_changed) |
| 1197 | { |
| 1198 | m_notifications.erase(pos); |
| 1199 | return true; |
| 1200 | } |
| 1201 | } |
| 1202 | return false; |
| 1203 | } |
| 1204 | |
| 1205 | void |
| 1206 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 1207 | { |
| 1208 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 1209 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 1210 | { |
| 1211 | if (notification_pos->process_state_changed) |
| 1212 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 1217 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 1218 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 1219 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 1220 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 1221 | // stall. So instead we do it when we fetch the event off of the queue. |
| 1222 | // |
| 1223 | |
| 1224 | StateType |
| 1225 | Process::GetNextEvent (EventSP &event_sp) |
| 1226 | { |
| 1227 | StateType state = eStateInvalid; |
| 1228 | |
| 1229 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 1230 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 1231 | |
| 1232 | return state; |
| 1233 | } |
| 1234 | |
| 1235 | |
| 1236 | StateType |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 1237 | Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr, bool wait_always) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1238 | { |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1239 | // We can't just wait for a "stopped" event, because the stopped event may have restarted the target. |
| 1240 | // We have to actually check each event, and in the case of a stopped event check the restarted flag |
| 1241 | // on the event. |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1242 | if (event_sp_ptr) |
| 1243 | event_sp_ptr->reset(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1244 | StateType state = GetState(); |
| 1245 | // If we are exited or detached, we won't ever get back to any |
| 1246 | // other valid state... |
| 1247 | if (state == eStateDetached || state == eStateExited) |
| 1248 | return state; |
| 1249 | |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 1250 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1251 | if (log) |
| 1252 | log->Printf ("Process::%s (timeout = %p)", __FUNCTION__, timeout); |
| 1253 | |
| 1254 | if (!wait_always && |
| 1255 | StateIsStoppedState(state, true) && |
| 1256 | StateIsStoppedState(GetPrivateState(), true)) { |
| 1257 | if (log) |
| 1258 | log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.", |
| 1259 | __FUNCTION__); |
| 1260 | return state; |
| 1261 | } |
| 1262 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1263 | while (state != eStateInvalid) |
| 1264 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1265 | EventSP event_sp; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1266 | state = WaitForStateChangedEvents (timeout, event_sp); |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1267 | if (event_sp_ptr && event_sp) |
| 1268 | *event_sp_ptr = event_sp; |
| 1269 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1270 | switch (state) |
| 1271 | { |
| 1272 | case eStateCrashed: |
| 1273 | case eStateDetached: |
| 1274 | case eStateExited: |
| 1275 | case eStateUnloaded: |
| 1276 | return state; |
| 1277 | case eStateStopped: |
| 1278 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 1279 | continue; |
| 1280 | else |
| 1281 | return state; |
| 1282 | default: |
| 1283 | continue; |
| 1284 | } |
| 1285 | } |
| 1286 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | StateType |
| 1291 | Process::WaitForState |
| 1292 | ( |
| 1293 | const TimeValue *timeout, |
| 1294 | const StateType *match_states, const uint32_t num_match_states |
| 1295 | ) |
| 1296 | { |
| 1297 | EventSP event_sp; |
| 1298 | uint32_t i; |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1299 | StateType state = GetState(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1300 | while (state != eStateInvalid) |
| 1301 | { |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1302 | // If we are exited or detached, we won't ever get back to any |
| 1303 | // other valid state... |
| 1304 | if (state == eStateDetached || state == eStateExited) |
| 1305 | return state; |
| 1306 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1307 | state = WaitForStateChangedEvents (timeout, event_sp); |
| 1308 | |
| 1309 | for (i=0; i<num_match_states; ++i) |
| 1310 | { |
| 1311 | if (match_states[i] == state) |
| 1312 | return state; |
| 1313 | } |
| 1314 | } |
| 1315 | return state; |
| 1316 | } |
| 1317 | |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1318 | bool |
| 1319 | Process::HijackProcessEvents (Listener *listener) |
| 1320 | { |
| 1321 | if (listener != NULL) |
| 1322 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1323 | return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1324 | } |
| 1325 | else |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | void |
| 1330 | Process::RestoreProcessEvents () |
| 1331 | { |
| 1332 | RestoreBroadcaster(); |
| 1333 | } |
| 1334 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1335 | bool |
| 1336 | Process::HijackPrivateProcessEvents (Listener *listener) |
| 1337 | { |
| 1338 | if (listener != NULL) |
| 1339 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1340 | return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1341 | } |
| 1342 | else |
| 1343 | return false; |
| 1344 | } |
| 1345 | |
| 1346 | void |
| 1347 | Process::RestorePrivateProcessEvents () |
| 1348 | { |
| 1349 | m_private_state_broadcaster.RestoreBroadcaster(); |
| 1350 | } |
| 1351 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1352 | StateType |
| 1353 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp) |
| 1354 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1355 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (log) |
| 1358 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1359 | |
| 1360 | StateType state = eStateInvalid; |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1361 | if (m_listener.WaitForEventForBroadcasterWithType (timeout, |
| 1362 | this, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1363 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1364 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1365 | { |
| 1366 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1367 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1368 | else if (log) |
| 1369 | log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__); |
| 1370 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (log) |
| 1373 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 1374 | __FUNCTION__, |
| 1375 | timeout, |
| 1376 | StateAsCString(state)); |
| 1377 | return state; |
| 1378 | } |
| 1379 | |
| 1380 | Event * |
| 1381 | Process::PeekAtStateChangedEvents () |
| 1382 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1383 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (log) |
| 1386 | log->Printf ("Process::%s...", __FUNCTION__); |
| 1387 | |
| 1388 | Event *event_ptr; |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1389 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this, |
| 1390 | eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1391 | if (log) |
| 1392 | { |
| 1393 | if (event_ptr) |
| 1394 | { |
| 1395 | log->Printf ("Process::%s (event_ptr) => %s", |
| 1396 | __FUNCTION__, |
| 1397 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 1398 | } |
| 1399 | else |
| 1400 | { |
| 1401 | log->Printf ("Process::%s no events found", |
| 1402 | __FUNCTION__); |
| 1403 | } |
| 1404 | } |
| 1405 | return event_ptr; |
| 1406 | } |
| 1407 | |
| 1408 | StateType |
| 1409 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 1410 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1411 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (log) |
| 1414 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1415 | |
| 1416 | StateType state = eStateInvalid; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1417 | if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout, |
| 1418 | &m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1419 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1420 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1421 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1422 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1423 | |
| 1424 | // This is a bit of a hack, but when we wait here we could very well return |
| 1425 | // to the command-line, and that could disable the log, which would render the |
| 1426 | // log we got above invalid. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1427 | if (log) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1428 | { |
| 1429 | if (state == eStateInvalid) |
| 1430 | log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout); |
| 1431 | else |
| 1432 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state)); |
| 1433 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1434 | return state; |
| 1435 | } |
| 1436 | |
| 1437 | bool |
| 1438 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 1439 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1440 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1441 | |
| 1442 | if (log) |
| 1443 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1444 | |
| 1445 | if (control_only) |
| 1446 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 1447 | else |
| 1448 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 1449 | } |
| 1450 | |
| 1451 | bool |
| 1452 | Process::IsRunning () const |
| 1453 | { |
| 1454 | return StateIsRunningState (m_public_state.GetValue()); |
| 1455 | } |
| 1456 | |
| 1457 | int |
| 1458 | Process::GetExitStatus () |
| 1459 | { |
| 1460 | if (m_public_state.GetValue() == eStateExited) |
| 1461 | return m_exit_status; |
| 1462 | return -1; |
| 1463 | } |
| 1464 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 1465 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1466 | const char * |
| 1467 | Process::GetExitDescription () |
| 1468 | { |
| 1469 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1470 | return m_exit_string.c_str(); |
| 1471 | return NULL; |
| 1472 | } |
| 1473 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1474 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1475 | Process::SetExitStatus (int status, const char *cstr) |
| 1476 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1477 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1478 | if (log) |
| 1479 | log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1480 | status, status, |
| 1481 | cstr ? "\"" : "", |
| 1482 | cstr ? cstr : "NULL", |
| 1483 | cstr ? "\"" : ""); |
| 1484 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1485 | // We were already in the exited state |
| 1486 | if (m_private_state.GetValue() == eStateExited) |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1487 | { |
Greg Clayton | 385d603 | 2011-01-26 23:47:29 +0000 | [diff] [blame] | 1488 | if (log) |
| 1489 | 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] | 1490 | return false; |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1491 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1492 | |
| 1493 | m_exit_status = status; |
| 1494 | if (cstr) |
| 1495 | m_exit_string = cstr; |
| 1496 | else |
| 1497 | m_exit_string.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1498 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1499 | DidExit (); |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1500 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1501 | SetPrivateState (eStateExited); |
| 1502 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | // This static callback can be used to watch for local child processes on |
| 1506 | // the current host. The the child process exits, the process will be |
| 1507 | // found in the global target list (we want to be completely sure that the |
| 1508 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 1509 | bool |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1510 | Process::SetProcessExitStatus (void *callback_baton, |
| 1511 | lldb::pid_t pid, |
| 1512 | bool exited, |
| 1513 | int signo, // Zero for no signal |
| 1514 | int exit_status // Exit value of process if signal is zero |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1515 | ) |
| 1516 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1517 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1518 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1519 | 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] | 1520 | callback_baton, |
| 1521 | pid, |
| 1522 | exited, |
| 1523 | signo, |
| 1524 | exit_status); |
| 1525 | |
| 1526 | if (exited) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1527 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1528 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1529 | if (target_sp) |
| 1530 | { |
| 1531 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 1532 | if (process_sp) |
| 1533 | { |
| 1534 | const char *signal_cstr = NULL; |
| 1535 | if (signo) |
| 1536 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 1537 | |
| 1538 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 1539 | } |
| 1540 | } |
| 1541 | return true; |
| 1542 | } |
| 1543 | return false; |
| 1544 | } |
| 1545 | |
| 1546 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1547 | void |
| 1548 | Process::UpdateThreadListIfNeeded () |
| 1549 | { |
| 1550 | const uint32_t stop_id = GetStopID(); |
| 1551 | if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID()) |
| 1552 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1553 | const StateType state = GetPrivateState(); |
| 1554 | if (StateIsStoppedState (state, true)) |
| 1555 | { |
| 1556 | Mutex::Locker locker (m_thread_list.GetMutex ()); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1557 | // m_thread_list does have its own mutex, but we need to |
| 1558 | // hold onto the mutex between the call to UpdateThreadList(...) |
| 1559 | // and the os->UpdateThreadList(...) so it doesn't change on us |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1560 | ThreadList &old_thread_list = m_thread_list; |
| 1561 | ThreadList real_thread_list(this); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1562 | ThreadList new_thread_list(this); |
| 1563 | // Always update the thread list with the protocol specific |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1564 | // thread list, but only update if "true" is returned |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1565 | if (UpdateThreadList (m_thread_list_real, real_thread_list)) |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1566 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1567 | // Don't call into the OperatingSystem to update the thread list if we are shutting down, since |
| 1568 | // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is |
| 1569 | // shutting us down, causing a deadlock. |
| 1570 | if (!m_destroy_in_process) |
| 1571 | { |
| 1572 | OperatingSystem *os = GetOperatingSystem (); |
| 1573 | if (os) |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1574 | { |
| 1575 | // Clear any old backing threads where memory threads might have been |
| 1576 | // backed by actual threads from the lldb_private::Process subclass |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1577 | size_t num_old_threads = old_thread_list.GetSize(false); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1578 | for (size_t i=0; i<num_old_threads; ++i) |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1579 | old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread(); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1580 | |
| 1581 | // Now let the OperatingSystem plug-in update the thread list |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1582 | os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in |
| 1583 | real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass |
| 1584 | 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] | 1585 | } |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1586 | else |
| 1587 | { |
| 1588 | // No OS plug-in, the new thread list is the same as the real thread list |
| 1589 | new_thread_list = real_thread_list; |
| 1590 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1591 | } |
Jim Ingham | 02ff8e0 | 2013-06-22 00:55:02 +0000 | [diff] [blame] | 1592 | |
| 1593 | m_thread_list_real.Update(real_thread_list); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1594 | m_thread_list.Update (new_thread_list); |
| 1595 | m_thread_list.SetStopID (stop_id); |
Jason Molenda | a6e9130 | 2013-11-19 05:44:41 +0000 | [diff] [blame^] | 1596 | |
| 1597 | // Clear any extended threads that we may have accumulated previously |
| 1598 | m_extended_thread_list.Clear(); |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1599 | } |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1600 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1601 | } |
| 1602 | } |
| 1603 | |
Greg Clayton | a4d8747 | 2013-01-18 23:41:08 +0000 | [diff] [blame] | 1604 | ThreadSP |
| 1605 | Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context) |
| 1606 | { |
| 1607 | OperatingSystem *os = GetOperatingSystem (); |
| 1608 | if (os) |
| 1609 | return os->CreateThread(tid, context); |
| 1610 | return ThreadSP(); |
| 1611 | } |
| 1612 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1613 | uint32_t |
| 1614 | Process::GetNextThreadIndexID (uint64_t thread_id) |
| 1615 | { |
| 1616 | return AssignIndexIDToThread(thread_id); |
| 1617 | } |
| 1618 | |
| 1619 | bool |
| 1620 | Process::HasAssignedIndexIDToThread(uint64_t thread_id) |
| 1621 | { |
| 1622 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1623 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1624 | { |
| 1625 | return false; |
| 1626 | } |
| 1627 | else |
| 1628 | { |
| 1629 | return true; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | uint32_t |
| 1634 | Process::AssignIndexIDToThread(uint64_t thread_id) |
| 1635 | { |
| 1636 | uint32_t result = 0; |
| 1637 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1638 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1639 | { |
| 1640 | result = ++m_thread_index_id; |
| 1641 | m_thread_id_to_index_id_map[thread_id] = result; |
| 1642 | } |
| 1643 | else |
| 1644 | { |
| 1645 | result = iterator->second; |
| 1646 | } |
| 1647 | |
| 1648 | return result; |
| 1649 | } |
| 1650 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1651 | StateType |
| 1652 | Process::GetState() |
| 1653 | { |
| 1654 | // If any other threads access this we will need a mutex for it |
| 1655 | return m_public_state.GetValue (); |
| 1656 | } |
| 1657 | |
| 1658 | void |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1659 | Process::SetPublicState (StateType new_state, bool restarted) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1660 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1661 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1662 | if (log) |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1663 | log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted); |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1664 | const StateType old_state = m_public_state.GetValue(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1665 | m_public_state.SetValue (new_state); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1666 | |
| 1667 | // On the transition from Run to Stopped, we unlock the writer end of the |
| 1668 | // run lock. The lock gets locked in Resume, which is the public API |
| 1669 | // to tell the program to run. |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1670 | if (!IsHijackedForEvent(eBroadcastBitStateChanged)) |
| 1671 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1672 | if (new_state == eStateDetached) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1673 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1674 | if (log) |
| 1675 | 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] | 1676 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1677 | } |
| 1678 | else |
| 1679 | { |
| 1680 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1681 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1682 | if ((old_state_is_stopped != new_state_is_stopped)) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1683 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1684 | if (new_state_is_stopped && !restarted) |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1685 | { |
| 1686 | if (log) |
| 1687 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state)); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1688 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1689 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1690 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1691 | } |
| 1692 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1695 | Error |
| 1696 | Process::Resume () |
| 1697 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1698 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1699 | if (log) |
| 1700 | log->Printf("Process::Resume -- locking run lock"); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1701 | if (!m_public_run_lock.TrySetRunning()) |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1702 | { |
| 1703 | Error error("Resume request failed - process still running."); |
| 1704 | if (log) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1705 | log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming."); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1706 | return error; |
| 1707 | } |
| 1708 | return PrivateResume(); |
| 1709 | } |
| 1710 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1711 | StateType |
| 1712 | Process::GetPrivateState () |
| 1713 | { |
| 1714 | return m_private_state.GetValue(); |
| 1715 | } |
| 1716 | |
| 1717 | void |
| 1718 | Process::SetPrivateState (StateType new_state) |
| 1719 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1720 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1721 | bool state_changed = false; |
| 1722 | |
| 1723 | if (log) |
| 1724 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 1725 | |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1726 | Mutex::Locker thread_locker(m_thread_list.GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1727 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 1728 | |
| 1729 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 1730 | state_changed = old_state != new_state; |
Ed Maste | c29693f | 2013-07-02 16:35:47 +0000 | [diff] [blame] | 1731 | |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1732 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1733 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1734 | if (old_state_is_stopped != new_state_is_stopped) |
| 1735 | { |
| 1736 | if (new_state_is_stopped) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1737 | m_private_run_lock.SetStopped(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1738 | else |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1739 | m_private_run_lock.SetRunning(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1740 | } |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1741 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1742 | if (state_changed) |
| 1743 | { |
| 1744 | m_private_state.SetValueNoLock (new_state); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1745 | if (StateIsStoppedState(new_state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1746 | { |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1747 | // Note, this currently assumes that all threads in the list |
| 1748 | // stop when the process stops. In the future we will want to |
| 1749 | // support a debugging model where some threads continue to run |
| 1750 | // while others are stopped. When that happens we will either need |
| 1751 | // a way for the thread list to identify which threads are stopping |
| 1752 | // or create a special thread list containing only threads which |
| 1753 | // actually stopped. |
| 1754 | // |
| 1755 | // The process plugin is responsible for managing the actual |
| 1756 | // behavior of the threads and should have stopped any threads |
| 1757 | // that are going to stop before we get here. |
| 1758 | m_thread_list.DidStop(); |
| 1759 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1760 | m_mod_id.BumpStopID(); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1761 | m_memory_cache.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1762 | if (log) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1763 | 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] | 1764 | } |
| 1765 | // Use our target to get a shared pointer to ourselves... |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1766 | if (m_finalize_called && PrivateStateThreadIsValid() == false) |
| 1767 | BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state)); |
| 1768 | else |
| 1769 | 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] | 1770 | } |
| 1771 | else |
| 1772 | { |
| 1773 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1774 | 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] | 1775 | } |
| 1776 | } |
| 1777 | |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 1778 | void |
| 1779 | Process::SetRunningUserExpression (bool on) |
| 1780 | { |
| 1781 | m_mod_id.SetRunningUserExpression (on); |
| 1782 | } |
| 1783 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1784 | addr_t |
| 1785 | Process::GetImageInfoAddress() |
| 1786 | { |
| 1787 | return LLDB_INVALID_ADDRESS; |
| 1788 | } |
| 1789 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1790 | //---------------------------------------------------------------------- |
| 1791 | // LoadImage |
| 1792 | // |
| 1793 | // This function provides a default implementation that works for most |
| 1794 | // unix variants. Any Process subclasses that need to do shared library |
| 1795 | // loading differently should override LoadImage and UnloadImage and |
| 1796 | // do what is needed. |
| 1797 | //---------------------------------------------------------------------- |
| 1798 | uint32_t |
| 1799 | Process::LoadImage (const FileSpec &image_spec, Error &error) |
| 1800 | { |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1801 | char path[PATH_MAX]; |
| 1802 | image_spec.GetPath(path, sizeof(path)); |
| 1803 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1804 | DynamicLoader *loader = GetDynamicLoader(); |
| 1805 | if (loader) |
| 1806 | { |
| 1807 | error = loader->CanLoadImage(); |
| 1808 | if (error.Fail()) |
| 1809 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1810 | } |
| 1811 | |
| 1812 | if (error.Success()) |
| 1813 | { |
| 1814 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1815 | |
| 1816 | if (thread_sp) |
| 1817 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1818 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1819 | |
| 1820 | if (frame_sp) |
| 1821 | { |
| 1822 | ExecutionContext exe_ctx; |
| 1823 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1824 | EvaluateExpressionOptions expr_options; |
| 1825 | expr_options.SetUnwindOnError(true); |
| 1826 | expr_options.SetIgnoreBreakpoints(true); |
| 1827 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1828 | StreamString expr; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1829 | expr.Printf("dlopen (\"%s\", 2)", path); |
| 1830 | 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] | 1831 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1832 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1833 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1834 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1835 | expr.GetData(), |
| 1836 | prefix, |
| 1837 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1838 | expr_error); |
| 1839 | if (expr_error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1840 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1841 | error = result_valobj_sp->GetError(); |
| 1842 | if (error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1843 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1844 | Scalar scalar; |
| 1845 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1846 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1847 | addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 1848 | if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS) |
| 1849 | { |
| 1850 | uint32_t image_token = m_image_tokens.size(); |
| 1851 | m_image_tokens.push_back (image_ptr); |
| 1852 | return image_token; |
| 1853 | } |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | } |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1860 | if (!error.AsCString()) |
| 1861 | error.SetErrorStringWithFormat("unable to load '%s'", path); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1862 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1863 | } |
| 1864 | |
| 1865 | //---------------------------------------------------------------------- |
| 1866 | // UnloadImage |
| 1867 | // |
| 1868 | // This function provides a default implementation that works for most |
| 1869 | // unix variants. Any Process subclasses that need to do shared library |
| 1870 | // loading differently should override LoadImage and UnloadImage and |
| 1871 | // do what is needed. |
| 1872 | //---------------------------------------------------------------------- |
| 1873 | Error |
| 1874 | Process::UnloadImage (uint32_t image_token) |
| 1875 | { |
| 1876 | Error error; |
| 1877 | if (image_token < m_image_tokens.size()) |
| 1878 | { |
| 1879 | const addr_t image_addr = m_image_tokens[image_token]; |
| 1880 | if (image_addr == LLDB_INVALID_ADDRESS) |
| 1881 | { |
| 1882 | error.SetErrorString("image already unloaded"); |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | DynamicLoader *loader = GetDynamicLoader(); |
| 1887 | if (loader) |
| 1888 | error = loader->CanLoadImage(); |
| 1889 | |
| 1890 | if (error.Success()) |
| 1891 | { |
| 1892 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1893 | |
| 1894 | if (thread_sp) |
| 1895 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1896 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1897 | |
| 1898 | if (frame_sp) |
| 1899 | { |
| 1900 | ExecutionContext exe_ctx; |
| 1901 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1902 | EvaluateExpressionOptions expr_options; |
| 1903 | expr_options.SetUnwindOnError(true); |
| 1904 | expr_options.SetIgnoreBreakpoints(true); |
| 1905 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1906 | StreamString expr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1907 | expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1908 | const char *prefix = "extern \"C\" int dlclose(void* handle);\n"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1909 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1910 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1911 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1912 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1913 | expr.GetData(), |
| 1914 | prefix, |
| 1915 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1916 | expr_error); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1917 | if (result_valobj_sp->GetError().Success()) |
| 1918 | { |
| 1919 | Scalar scalar; |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 1920 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1921 | { |
| 1922 | if (scalar.UInt(1)) |
| 1923 | { |
| 1924 | error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData()); |
| 1925 | } |
| 1926 | else |
| 1927 | { |
| 1928 | m_image_tokens[image_token] = LLDB_INVALID_ADDRESS; |
| 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | else |
| 1933 | { |
| 1934 | error = result_valobj_sp->GetError(); |
| 1935 | } |
| 1936 | } |
| 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | else |
| 1942 | { |
| 1943 | error.SetErrorString("invalid image token"); |
| 1944 | } |
| 1945 | return error; |
| 1946 | } |
| 1947 | |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 1948 | const lldb::ABISP & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1949 | Process::GetABI() |
| 1950 | { |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 1951 | if (!m_abi_sp) |
| 1952 | m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture()); |
| 1953 | return m_abi_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1956 | LanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1957 | Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1958 | { |
| 1959 | LanguageRuntimeCollection::iterator pos; |
| 1960 | pos = m_language_runtimes.find (language); |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1961 | if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second)) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1962 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1963 | lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language)); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1964 | |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1965 | m_language_runtimes[language] = runtime_sp; |
| 1966 | return runtime_sp.get(); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1967 | } |
| 1968 | else |
| 1969 | return (*pos).second.get(); |
| 1970 | } |
| 1971 | |
| 1972 | CPPLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1973 | Process::GetCPPLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1974 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1975 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1976 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 1977 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 1978 | return NULL; |
| 1979 | } |
| 1980 | |
| 1981 | ObjCLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1982 | Process::GetObjCLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1983 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1984 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1985 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 1986 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 1987 | return NULL; |
| 1988 | } |
| 1989 | |
Enrico Granata | fd4c84e | 2012-05-21 16:51:35 +0000 | [diff] [blame] | 1990 | bool |
| 1991 | Process::IsPossibleDynamicValue (ValueObject& in_value) |
| 1992 | { |
| 1993 | if (in_value.IsDynamic()) |
| 1994 | return false; |
| 1995 | LanguageType known_type = in_value.GetObjectRuntimeLanguage(); |
| 1996 | |
| 1997 | if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC) |
| 1998 | { |
| 1999 | LanguageRuntime *runtime = GetLanguageRuntime (known_type); |
| 2000 | return runtime ? runtime->CouldHaveDynamicValue(in_value) : false; |
| 2001 | } |
| 2002 | |
| 2003 | LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus); |
| 2004 | if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value)) |
| 2005 | return true; |
| 2006 | |
| 2007 | LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC); |
| 2008 | return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false; |
| 2009 | } |
| 2010 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2011 | BreakpointSiteList & |
| 2012 | Process::GetBreakpointSiteList() |
| 2013 | { |
| 2014 | return m_breakpoint_site_list; |
| 2015 | } |
| 2016 | |
| 2017 | const BreakpointSiteList & |
| 2018 | Process::GetBreakpointSiteList() const |
| 2019 | { |
| 2020 | return m_breakpoint_site_list; |
| 2021 | } |
| 2022 | |
| 2023 | |
| 2024 | void |
| 2025 | Process::DisableAllBreakpointSites () |
| 2026 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2027 | m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void { |
| 2028 | // bp_site->SetEnabled(true); |
| 2029 | DisableBreakpointSite(bp_site); |
| 2030 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | Error |
| 2034 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 2035 | { |
| 2036 | Error error (DisableBreakpointSiteByID (break_id)); |
| 2037 | |
| 2038 | if (error.Success()) |
| 2039 | m_breakpoint_site_list.Remove(break_id); |
| 2040 | |
| 2041 | return error; |
| 2042 | } |
| 2043 | |
| 2044 | Error |
| 2045 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 2046 | { |
| 2047 | Error error; |
| 2048 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 2049 | if (bp_site_sp) |
| 2050 | { |
| 2051 | if (bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2052 | error = DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2053 | } |
| 2054 | else |
| 2055 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2056 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | return error; |
| 2060 | } |
| 2061 | |
| 2062 | Error |
| 2063 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 2064 | { |
| 2065 | Error error; |
| 2066 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 2067 | if (bp_site_sp) |
| 2068 | { |
| 2069 | if (!bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2070 | error = EnableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2071 | } |
| 2072 | else |
| 2073 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2074 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2075 | } |
| 2076 | return error; |
| 2077 | } |
| 2078 | |
Stephen Wilson | 50bd94f | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 2079 | lldb::break_id_t |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 2080 | Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2081 | { |
Greg Clayton | 92bb12c | 2011-05-19 18:17:41 +0000 | [diff] [blame] | 2082 | const addr_t load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2083 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 2084 | { |
| 2085 | BreakpointSiteSP bp_site_sp; |
| 2086 | |
| 2087 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 2088 | // create a new breakpoint site and add it. |
| 2089 | |
| 2090 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 2091 | |
| 2092 | if (bp_site_sp) |
| 2093 | { |
| 2094 | bp_site_sp->AddOwner (owner); |
| 2095 | owner->SetBreakpointSite (bp_site_sp); |
| 2096 | return bp_site_sp->GetID(); |
| 2097 | } |
| 2098 | else |
| 2099 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2100 | 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] | 2101 | if (bp_site_sp) |
| 2102 | { |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2103 | Error error = EnableBreakpointSite (bp_site_sp.get()); |
| 2104 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2105 | { |
| 2106 | owner->SetBreakpointSite (bp_site_sp); |
| 2107 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 2108 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2109 | else |
| 2110 | { |
| 2111 | // Report error for setting breakpoint... |
| 2112 | m_target.GetDebugger().GetErrorFile().Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 2113 | load_addr, |
| 2114 | owner->GetBreakpoint().GetID(), |
| 2115 | owner->GetID(), |
| 2116 | error.AsCString() ? error.AsCString() : "unkown error"); |
| 2117 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2118 | } |
| 2119 | } |
| 2120 | } |
| 2121 | // We failed to enable the breakpoint |
| 2122 | return LLDB_INVALID_BREAK_ID; |
| 2123 | |
| 2124 | } |
| 2125 | |
| 2126 | void |
| 2127 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 2128 | { |
| 2129 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 2130 | if (num_owners == 0) |
| 2131 | { |
Jim Ingham | f1ff3bb | 2013-04-06 00:16:39 +0000 | [diff] [blame] | 2132 | // Don't try to disable the site if we don't have a live process anymore. |
| 2133 | if (IsAlive()) |
| 2134 | DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2135 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | |
| 2140 | size_t |
| 2141 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 2142 | { |
| 2143 | size_t bytes_removed = 0; |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2144 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2145 | |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2146 | 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] | 2147 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2148 | bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void { |
| 2149 | if (bp_site->GetType() == BreakpointSite::eSoftware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2150 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2151 | addr_t intersect_addr; |
| 2152 | size_t intersect_size; |
| 2153 | size_t opcode_offset; |
| 2154 | 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] | 2155 | { |
| 2156 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 2157 | 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] | 2158 | assert(opcode_offset + intersect_size <= bp_site->GetByteSize()); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2159 | size_t buf_offset = intersect_addr - bp_addr; |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2160 | ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2161 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2162 | } |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2163 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2164 | } |
| 2165 | return bytes_removed; |
| 2166 | } |
| 2167 | |
| 2168 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 2169 | |
| 2170 | size_t |
| 2171 | Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site) |
| 2172 | { |
| 2173 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 2174 | if (platform_sp) |
| 2175 | return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site); |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2179 | Error |
| 2180 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2181 | { |
| 2182 | Error error; |
| 2183 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2184 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2185 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2186 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2187 | 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] | 2188 | if (bp_site->IsEnabled()) |
| 2189 | { |
| 2190 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2191 | 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] | 2192 | return error; |
| 2193 | } |
| 2194 | |
| 2195 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 2196 | { |
| 2197 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 2198 | return error; |
| 2199 | } |
| 2200 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 2201 | // trap for the breakpoint site |
| 2202 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2203 | |
| 2204 | if (bp_opcode_size == 0) |
| 2205 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2206 | 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] | 2207 | } |
| 2208 | else |
| 2209 | { |
| 2210 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 2211 | |
| 2212 | if (bp_opcode_bytes == NULL) |
| 2213 | { |
| 2214 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 2215 | return error; |
| 2216 | } |
| 2217 | |
| 2218 | // Save the original opcode by reading it |
| 2219 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 2220 | { |
| 2221 | // Write a software breakpoint in place of the original opcode |
| 2222 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2223 | { |
| 2224 | uint8_t verify_bp_opcode_bytes[64]; |
| 2225 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2226 | { |
| 2227 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 2228 | { |
| 2229 | bp_site->SetEnabled(true); |
| 2230 | bp_site->SetType (BreakpointSite::eSoftware); |
| 2231 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2232 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2233 | bp_site->GetID(), |
| 2234 | (uint64_t)bp_addr); |
| 2235 | } |
| 2236 | else |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2237 | error.SetErrorString("failed to verify the breakpoint trap in memory."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2238 | } |
| 2239 | else |
| 2240 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 2241 | } |
| 2242 | else |
| 2243 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 2244 | } |
| 2245 | else |
| 2246 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 2247 | } |
Stephen Wilson | 78a4feb | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 2248 | if (log && error.Fail()) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2249 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2250 | bp_site->GetID(), |
| 2251 | (uint64_t)bp_addr, |
| 2252 | error.AsCString()); |
| 2253 | return error; |
| 2254 | } |
| 2255 | |
| 2256 | Error |
| 2257 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2258 | { |
| 2259 | Error error; |
| 2260 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2261 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2262 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2263 | lldb::user_id_t breakID = bp_site->GetID(); |
| 2264 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2265 | 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] | 2266 | |
| 2267 | if (bp_site->IsHardware()) |
| 2268 | { |
| 2269 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 2270 | } |
| 2271 | else if (bp_site->IsEnabled()) |
| 2272 | { |
| 2273 | const size_t break_op_size = bp_site->GetByteSize(); |
| 2274 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 2275 | if (break_op_size > 0) |
| 2276 | { |
| 2277 | // Clear a software breakoint instruction |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2278 | uint8_t curr_break_op[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2279 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2280 | bool break_op_found = false; |
| 2281 | |
| 2282 | // Read the breakpoint opcode |
| 2283 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 2284 | { |
| 2285 | bool verify = false; |
| 2286 | // Make sure we have the a breakpoint opcode exists at this address |
| 2287 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 2288 | { |
| 2289 | break_op_found = true; |
| 2290 | // We found a valid breakpoint opcode at this address, now restore |
| 2291 | // the saved opcode. |
| 2292 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 2293 | { |
| 2294 | verify = true; |
| 2295 | } |
| 2296 | else |
| 2297 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 2298 | } |
| 2299 | else |
| 2300 | { |
| 2301 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 2302 | // Set verify to true and so we can check if the original opcode has already been restored |
| 2303 | verify = true; |
| 2304 | } |
| 2305 | |
| 2306 | if (verify) |
| 2307 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2308 | uint8_t verify_opcode[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2309 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2310 | // Verify that our original opcode made it back to the inferior |
| 2311 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 2312 | { |
| 2313 | // compare the memory we just read with the original opcode |
| 2314 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 2315 | { |
| 2316 | // SUCCESS |
| 2317 | bp_site->SetEnabled(false); |
| 2318 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2319 | 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] | 2320 | return error; |
| 2321 | } |
| 2322 | else |
| 2323 | { |
| 2324 | if (break_op_found) |
| 2325 | error.SetErrorString("Failed to restore original opcode."); |
| 2326 | } |
| 2327 | } |
| 2328 | else |
| 2329 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 2330 | } |
| 2331 | } |
| 2332 | else |
| 2333 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 2334 | } |
| 2335 | } |
| 2336 | else |
| 2337 | { |
| 2338 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2339 | 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] | 2340 | return error; |
| 2341 | } |
| 2342 | |
| 2343 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2344 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2345 | bp_site->GetID(), |
| 2346 | (uint64_t)bp_addr, |
| 2347 | error.AsCString()); |
| 2348 | return error; |
| 2349 | |
| 2350 | } |
| 2351 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2352 | // Uncomment to verify memory caching works after making changes to caching code |
| 2353 | //#define VERIFY_MEMORY_READS |
| 2354 | |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2355 | size_t |
| 2356 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 2357 | { |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 2358 | error.Clear(); |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2359 | if (!GetDisableMemoryCache()) |
| 2360 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2361 | #if defined (VERIFY_MEMORY_READS) |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2362 | // Memory caching is enabled, with debug verification |
| 2363 | |
| 2364 | if (buf && size) |
| 2365 | { |
| 2366 | // Uncomment the line below to make sure memory caching is working. |
| 2367 | // I ran this through the test suite and got no assertions, so I am |
| 2368 | // pretty confident this is working well. If any changes are made to |
| 2369 | // memory caching, uncomment the line below and test your changes! |
| 2370 | |
| 2371 | // Verify all memory reads by using the cache first, then redundantly |
| 2372 | // reading the same memory from the inferior and comparing to make sure |
| 2373 | // everything is exactly the same. |
| 2374 | std::string verify_buf (size, '\0'); |
| 2375 | assert (verify_buf.size() == size); |
| 2376 | const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error); |
| 2377 | Error verify_error; |
| 2378 | const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error); |
| 2379 | assert (cache_bytes_read == verify_bytes_read); |
| 2380 | assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 2381 | assert (verify_error.Success() == error.Success()); |
| 2382 | return cache_bytes_read; |
| 2383 | } |
| 2384 | return 0; |
| 2385 | #else // !defined(VERIFY_MEMORY_READS) |
| 2386 | // Memory caching is enabled, without debug verification |
| 2387 | |
| 2388 | return m_memory_cache.Read (addr, buf, size, error); |
| 2389 | #endif // defined (VERIFY_MEMORY_READS) |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2390 | } |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2391 | else |
| 2392 | { |
| 2393 | // Memory caching is disabled |
| 2394 | |
| 2395 | return ReadMemoryFromInferior (addr, buf, size, error); |
| 2396 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2397 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2398 | |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2399 | size_t |
| 2400 | Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error) |
| 2401 | { |
Greg Clayton | de87c0f | 2012-05-19 00:18:00 +0000 | [diff] [blame] | 2402 | char buf[256]; |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2403 | out_str.clear(); |
| 2404 | addr_t curr_addr = addr; |
| 2405 | while (1) |
| 2406 | { |
| 2407 | size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error); |
| 2408 | if (length == 0) |
| 2409 | break; |
| 2410 | out_str.append(buf, length); |
| 2411 | // If we got "length - 1" bytes, we didn't get the whole C string, we |
| 2412 | // need to read some more characters |
| 2413 | if (length == sizeof(buf) - 1) |
| 2414 | curr_addr += length; |
| 2415 | else |
| 2416 | break; |
| 2417 | } |
| 2418 | return out_str.size(); |
| 2419 | } |
| 2420 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2421 | |
| 2422 | size_t |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2423 | Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error, |
| 2424 | size_t type_width) |
| 2425 | { |
| 2426 | size_t total_bytes_read = 0; |
| 2427 | if (dst && max_bytes && type_width && max_bytes >= type_width) |
| 2428 | { |
| 2429 | // Ensure a null terminator independent of the number of bytes that is read. |
| 2430 | memset (dst, 0, max_bytes); |
| 2431 | size_t bytes_left = max_bytes - type_width; |
| 2432 | |
| 2433 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 2434 | assert(sizeof(terminator) >= type_width && |
| 2435 | "Attempting to validate a string with more than 4 bytes per character!"); |
| 2436 | |
| 2437 | addr_t curr_addr = addr; |
| 2438 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2439 | char *curr_dst = dst; |
| 2440 | |
| 2441 | error.Clear(); |
| 2442 | while (bytes_left > 0 && error.Success()) |
| 2443 | { |
| 2444 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2445 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2446 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2447 | |
| 2448 | if (bytes_read == 0) |
| 2449 | break; |
| 2450 | |
| 2451 | // Search for a null terminator of correct size and alignment in bytes_read |
| 2452 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2453 | for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2454 | if (::strncmp(&dst[i], terminator, type_width) == 0) |
| 2455 | { |
| 2456 | error.Clear(); |
| 2457 | return i; |
| 2458 | } |
| 2459 | |
| 2460 | total_bytes_read += bytes_read; |
| 2461 | curr_dst += bytes_read; |
| 2462 | curr_addr += bytes_read; |
| 2463 | bytes_left -= bytes_read; |
| 2464 | } |
| 2465 | } |
| 2466 | else |
| 2467 | { |
| 2468 | if (max_bytes) |
| 2469 | error.SetErrorString("invalid arguments"); |
| 2470 | } |
| 2471 | return total_bytes_read; |
| 2472 | } |
| 2473 | |
| 2474 | // Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find |
| 2475 | // null terminators. |
| 2476 | size_t |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2477 | 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] | 2478 | { |
| 2479 | size_t total_cstr_len = 0; |
| 2480 | if (dst && dst_max_len) |
| 2481 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2482 | result_error.Clear(); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2483 | // NULL out everything just to be safe |
| 2484 | memset (dst, 0, dst_max_len); |
| 2485 | Error error; |
| 2486 | addr_t curr_addr = addr; |
| 2487 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2488 | size_t bytes_left = dst_max_len - 1; |
| 2489 | char *curr_dst = dst; |
| 2490 | |
| 2491 | while (bytes_left > 0) |
| 2492 | { |
| 2493 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2494 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2495 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2496 | |
| 2497 | if (bytes_read == 0) |
| 2498 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2499 | result_error = error; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2500 | dst[total_cstr_len] = '\0'; |
| 2501 | break; |
| 2502 | } |
| 2503 | const size_t len = strlen(curr_dst); |
| 2504 | |
| 2505 | total_cstr_len += len; |
| 2506 | |
| 2507 | if (len < bytes_to_read) |
| 2508 | break; |
| 2509 | |
| 2510 | curr_dst += bytes_read; |
| 2511 | curr_addr += bytes_read; |
| 2512 | bytes_left -= bytes_read; |
| 2513 | } |
| 2514 | } |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2515 | else |
| 2516 | { |
| 2517 | if (dst == NULL) |
| 2518 | result_error.SetErrorString("invalid arguments"); |
| 2519 | else |
| 2520 | result_error.Clear(); |
| 2521 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2522 | return total_cstr_len; |
| 2523 | } |
| 2524 | |
| 2525 | size_t |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2526 | Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error) |
| 2527 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2528 | if (buf == NULL || size == 0) |
| 2529 | return 0; |
| 2530 | |
| 2531 | size_t bytes_read = 0; |
| 2532 | uint8_t *bytes = (uint8_t *)buf; |
| 2533 | |
| 2534 | while (bytes_read < size) |
| 2535 | { |
| 2536 | const size_t curr_size = size - bytes_read; |
| 2537 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 2538 | bytes + bytes_read, |
| 2539 | curr_size, |
| 2540 | error); |
| 2541 | bytes_read += curr_bytes_read; |
| 2542 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 2543 | break; |
| 2544 | } |
| 2545 | |
| 2546 | // Replace any software breakpoint opcodes that fall into this range back |
| 2547 | // into "buf" before we return |
| 2548 | if (bytes_read > 0) |
| 2549 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 2550 | return bytes_read; |
| 2551 | } |
| 2552 | |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2553 | uint64_t |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2554 | 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] | 2555 | { |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2556 | Scalar scalar; |
| 2557 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error)) |
| 2558 | return scalar.ULongLong(fail_value); |
| 2559 | return fail_value; |
| 2560 | } |
| 2561 | |
| 2562 | addr_t |
| 2563 | Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error) |
| 2564 | { |
| 2565 | Scalar scalar; |
| 2566 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error)) |
| 2567 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2568 | return LLDB_INVALID_ADDRESS; |
| 2569 | } |
| 2570 | |
| 2571 | |
| 2572 | bool |
| 2573 | Process::WritePointerToMemory (lldb::addr_t vm_addr, |
| 2574 | lldb::addr_t ptr_value, |
| 2575 | Error &error) |
| 2576 | { |
| 2577 | Scalar scalar; |
| 2578 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2579 | if (addr_byte_size <= 4) |
| 2580 | scalar = (uint32_t)ptr_value; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2581 | else |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2582 | scalar = ptr_value; |
| 2583 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2586 | size_t |
| 2587 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 2588 | { |
| 2589 | size_t bytes_written = 0; |
| 2590 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2591 | |
| 2592 | while (bytes_written < size) |
| 2593 | { |
| 2594 | const size_t curr_size = size - bytes_written; |
| 2595 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 2596 | bytes + bytes_written, |
| 2597 | curr_size, |
| 2598 | error); |
| 2599 | bytes_written += curr_bytes_written; |
| 2600 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2601 | break; |
| 2602 | } |
| 2603 | return bytes_written; |
| 2604 | } |
| 2605 | |
| 2606 | size_t |
| 2607 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2608 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2609 | #if defined (ENABLE_MEMORY_CACHING) |
| 2610 | m_memory_cache.Flush (addr, size); |
| 2611 | #endif |
| 2612 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2613 | if (buf == NULL || size == 0) |
| 2614 | return 0; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2615 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2616 | m_mod_id.BumpMemoryID(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2617 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2618 | // We need to write any data that would go where any current software traps |
| 2619 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2620 | // may have placed in our tasks memory. |
| 2621 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2622 | BreakpointSiteList bp_sites_in_range; |
| 2623 | |
| 2624 | 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] | 2625 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2626 | // No breakpoint sites overlap |
| 2627 | if (bp_sites_in_range.IsEmpty()) |
| 2628 | return WriteMemoryPrivate (addr, buf, size, error); |
| 2629 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2630 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2631 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2632 | uint64_t bytes_written = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2633 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2634 | bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void { |
| 2635 | |
| 2636 | if (error.Success()) |
| 2637 | { |
| 2638 | addr_t intersect_addr; |
| 2639 | size_t intersect_size; |
| 2640 | size_t opcode_offset; |
| 2641 | const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset); |
| 2642 | assert(intersects); |
| 2643 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2644 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 2645 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2646 | |
| 2647 | // Check for bytes before this breakpoint |
| 2648 | const addr_t curr_addr = addr + bytes_written; |
| 2649 | if (intersect_addr > curr_addr) |
| 2650 | { |
| 2651 | // There are some bytes before this breakpoint that we need to |
| 2652 | // just write to memory |
| 2653 | size_t curr_size = intersect_addr - curr_addr; |
| 2654 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 2655 | ubuf + bytes_written, |
| 2656 | curr_size, |
| 2657 | error); |
| 2658 | bytes_written += curr_bytes_written; |
| 2659 | if (curr_bytes_written != curr_size) |
| 2660 | { |
| 2661 | // We weren't able to write all of the requested bytes, we |
| 2662 | // are done looping and will return the number of bytes that |
| 2663 | // we have written so far. |
| 2664 | if (error.Success()) |
| 2665 | error.SetErrorToGenericError(); |
| 2666 | } |
| 2667 | } |
| 2668 | // Now write any bytes that would cover up any software breakpoints |
| 2669 | // directly into the breakpoint opcode buffer |
| 2670 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 2671 | bytes_written += intersect_size; |
| 2672 | } |
| 2673 | }); |
| 2674 | |
| 2675 | if (bytes_written < size) |
| 2676 | bytes_written += WriteMemoryPrivate (addr + bytes_written, |
| 2677 | ubuf + bytes_written, |
| 2678 | size - bytes_written, |
| 2679 | error); |
| 2680 | } |
| 2681 | } |
| 2682 | else |
| 2683 | { |
| 2684 | return WriteMemoryPrivate (addr, buf, size, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | // 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] | 2688 | return 0; //bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2689 | } |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2690 | |
| 2691 | size_t |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2692 | 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] | 2693 | { |
| 2694 | if (byte_size == UINT32_MAX) |
| 2695 | byte_size = scalar.GetByteSize(); |
| 2696 | if (byte_size > 0) |
| 2697 | { |
| 2698 | uint8_t buf[32]; |
| 2699 | const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error); |
| 2700 | if (mem_size > 0) |
| 2701 | return WriteMemory(addr, buf, mem_size, error); |
| 2702 | else |
| 2703 | error.SetErrorString ("failed to get scalar as memory data"); |
| 2704 | } |
| 2705 | else |
| 2706 | { |
| 2707 | error.SetErrorString ("invalid scalar value"); |
| 2708 | } |
| 2709 | return 0; |
| 2710 | } |
| 2711 | |
| 2712 | size_t |
| 2713 | Process::ReadScalarIntegerFromMemory (addr_t addr, |
| 2714 | uint32_t byte_size, |
| 2715 | bool is_signed, |
| 2716 | Scalar &scalar, |
| 2717 | Error &error) |
| 2718 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2719 | uint64_t uval = 0; |
| 2720 | if (byte_size == 0) |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2721 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2722 | error.SetErrorString ("byte size is zero"); |
| 2723 | } |
| 2724 | else if (byte_size & (byte_size - 1)) |
| 2725 | { |
| 2726 | error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size); |
| 2727 | } |
| 2728 | else if (byte_size <= sizeof(uval)) |
| 2729 | { |
| 2730 | const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2731 | if (bytes_read == byte_size) |
| 2732 | { |
| 2733 | DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize()); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2734 | lldb::offset_t offset = 0; |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2735 | if (byte_size <= 4) |
| 2736 | scalar = data.GetMaxU32 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2737 | else |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2738 | scalar = data.GetMaxU64 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2739 | if (is_signed) |
| 2740 | scalar.SignExtend(byte_size * 8); |
| 2741 | return bytes_read; |
| 2742 | } |
| 2743 | } |
| 2744 | else |
| 2745 | { |
| 2746 | error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size); |
| 2747 | } |
| 2748 | return 0; |
| 2749 | } |
| 2750 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2751 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2752 | addr_t |
| 2753 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 2754 | { |
Jim Ingham | f72ce3a | 2011-06-20 17:32:44 +0000 | [diff] [blame] | 2755 | if (GetPrivateState() != eStateStopped) |
| 2756 | return LLDB_INVALID_ADDRESS; |
| 2757 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2758 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2759 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
| 2760 | #else |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2761 | addr_t allocated_addr = DoAllocateMemory (size, permissions, error); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2762 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2763 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 2764 | 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] | 2765 | (uint64_t)size, |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2766 | GetPermissionsAsCString (permissions), |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2767 | (uint64_t)allocated_addr, |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2768 | m_mod_id.GetStopID(), |
| 2769 | m_mod_id.GetMemoryID()); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2770 | return allocated_addr; |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2771 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2774 | bool |
| 2775 | Process::CanJIT () |
| 2776 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2777 | if (m_can_jit == eCanJITDontKnow) |
| 2778 | { |
| 2779 | Error err; |
| 2780 | |
| 2781 | uint64_t allocated_memory = AllocateMemory(8, |
| 2782 | ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2783 | err); |
| 2784 | |
| 2785 | if (err.Success()) |
| 2786 | m_can_jit = eCanJITYes; |
| 2787 | else |
| 2788 | m_can_jit = eCanJITNo; |
| 2789 | |
| 2790 | DeallocateMemory (allocated_memory); |
| 2791 | } |
| 2792 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2793 | return m_can_jit == eCanJITYes; |
| 2794 | } |
| 2795 | |
| 2796 | void |
| 2797 | Process::SetCanJIT (bool can_jit) |
| 2798 | { |
| 2799 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
| 2800 | } |
| 2801 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2802 | Error |
| 2803 | Process::DeallocateMemory (addr_t ptr) |
| 2804 | { |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2805 | Error error; |
| 2806 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2807 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) |
| 2808 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2809 | error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2810 | } |
| 2811 | #else |
| 2812 | error = DoDeallocateMemory (ptr); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2813 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2814 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2815 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2816 | 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] | 2817 | ptr, |
| 2818 | error.AsCString("SUCCESS"), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2819 | m_mod_id.GetStopID(), |
| 2820 | m_mod_id.GetMemoryID()); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2821 | #endif |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2822 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Han Ming Ong | c811d38 | 2012-11-17 00:33:14 +0000 | [diff] [blame] | 2825 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2826 | ModuleSP |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2827 | Process::ReadModuleFromMemory (const FileSpec& file_spec, |
Greg Clayton | 39f7ee8 | 2013-02-01 21:38:35 +0000 | [diff] [blame] | 2828 | lldb::addr_t header_addr) |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2829 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2830 | ModuleSP module_sp (new Module (file_spec, ArchSpec())); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2831 | if (module_sp) |
| 2832 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2833 | Error error; |
| 2834 | ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error); |
| 2835 | if (objfile) |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2836 | return module_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2837 | } |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2838 | return ModuleSP(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2839 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2840 | |
| 2841 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2842 | Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2843 | { |
| 2844 | Error error; |
| 2845 | error.SetErrorString("watchpoints are not supported"); |
| 2846 | return error; |
| 2847 | } |
| 2848 | |
| 2849 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2850 | Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2851 | { |
| 2852 | Error error; |
| 2853 | error.SetErrorString("watchpoints are not supported"); |
| 2854 | return error; |
| 2855 | } |
| 2856 | |
| 2857 | StateType |
| 2858 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 2859 | { |
| 2860 | StateType state; |
| 2861 | // Now wait for the process to launch and return control to us, and then |
| 2862 | // call DidLaunch: |
| 2863 | while (1) |
| 2864 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2865 | event_sp.reset(); |
| 2866 | state = WaitForStateChangedEventsPrivate (timeout, event_sp); |
| 2867 | |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 2868 | if (StateIsStoppedState(state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2869 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2870 | |
| 2871 | // If state is invalid, then we timed out |
| 2872 | if (state == eStateInvalid) |
| 2873 | break; |
| 2874 | |
| 2875 | if (event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2876 | HandlePrivateEvent (event_sp); |
| 2877 | } |
| 2878 | return state; |
| 2879 | } |
| 2880 | |
| 2881 | Error |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 2882 | Process::Launch (const ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2883 | { |
| 2884 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2885 | m_abi_sp.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2886 | m_dyld_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2887 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2888 | m_os_ap.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2889 | m_process_input_reader.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2890 | |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 2891 | Module *exe_module = m_target.GetExecutableModulePointer(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2892 | if (exe_module) |
| 2893 | { |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 2894 | char local_exec_file_path[PATH_MAX]; |
| 2895 | char platform_exec_file_path[PATH_MAX]; |
| 2896 | exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path)); |
| 2897 | 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] | 2898 | if (exe_module->GetFileSpec().Exists()) |
| 2899 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2900 | if (PrivateStateThreadIsValid ()) |
| 2901 | PausePrivateStateThread (); |
| 2902 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2903 | error = WillLaunch (exe_module); |
| 2904 | if (error.Success()) |
| 2905 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 2906 | const bool restarted = false; |
| 2907 | SetPublicState (eStateLaunching, restarted); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 2908 | m_should_detach = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2909 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 2910 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 69fd4be | 2012-09-04 20:29:05 +0000 | [diff] [blame] | 2911 | { |
| 2912 | // Now launch using these arguments. |
| 2913 | error = DoLaunch (exe_module, launch_info); |
| 2914 | } |
| 2915 | else |
| 2916 | { |
| 2917 | // This shouldn't happen |
| 2918 | error.SetErrorString("failed to acquire process run lock"); |
| 2919 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2920 | |
| 2921 | if (error.Fail()) |
| 2922 | { |
| 2923 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 2924 | { |
| 2925 | SetID (LLDB_INVALID_PROCESS_ID); |
| 2926 | const char *error_string = error.AsCString(); |
| 2927 | if (error_string == NULL) |
| 2928 | error_string = "launch failed"; |
| 2929 | SetExitStatus (-1, error_string); |
| 2930 | } |
| 2931 | } |
| 2932 | else |
| 2933 | { |
| 2934 | EventSP event_sp; |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 2935 | TimeValue timeout_time; |
| 2936 | timeout_time = TimeValue::Now(); |
| 2937 | timeout_time.OffsetWithSeconds(10); |
| 2938 | StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2939 | |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 2940 | if (state == eStateInvalid || event_sp.get() == NULL) |
| 2941 | { |
| 2942 | // We were able to launch the process, but we failed to |
| 2943 | // catch the initial stop. |
| 2944 | SetExitStatus (0, "failed to catch stop after launch"); |
| 2945 | Destroy(); |
| 2946 | } |
| 2947 | else if (state == eStateStopped || state == eStateCrashed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2948 | { |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2949 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2950 | DidLaunch (); |
| 2951 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2952 | DynamicLoader *dyld = GetDynamicLoader (); |
| 2953 | if (dyld) |
| 2954 | dyld->DidLaunch(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2955 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2956 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 2957 | if (system_runtime) |
| 2958 | system_runtime->DidLaunch(); |
| 2959 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2960 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2961 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 2962 | // a chance to complete... |
| 2963 | HandlePrivateEvent (event_sp); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2964 | |
| 2965 | if (PrivateStateThreadIsValid ()) |
| 2966 | ResumePrivateStateThread (); |
| 2967 | else |
| 2968 | StartPrivateStateThread (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2969 | } |
| 2970 | else if (state == eStateExited) |
| 2971 | { |
| 2972 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 2973 | // not likely to work, and return an invalid pid. |
| 2974 | HandlePrivateEvent (event_sp); |
| 2975 | } |
| 2976 | } |
| 2977 | } |
| 2978 | } |
| 2979 | else |
| 2980 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2981 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2982 | } |
| 2983 | } |
| 2984 | return error; |
| 2985 | } |
| 2986 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2987 | |
| 2988 | Error |
| 2989 | Process::LoadCore () |
| 2990 | { |
| 2991 | Error error = DoLoadCore(); |
| 2992 | if (error.Success()) |
| 2993 | { |
| 2994 | if (PrivateStateThreadIsValid ()) |
| 2995 | ResumePrivateStateThread (); |
| 2996 | else |
| 2997 | StartPrivateStateThread (); |
| 2998 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2999 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3000 | if (dyld) |
| 3001 | dyld->DidAttach(); |
| 3002 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3003 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3004 | if (system_runtime) |
| 3005 | system_runtime->DidAttach(); |
| 3006 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3007 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3008 | // We successfully loaded a core file, now pretend we stopped so we can |
| 3009 | // show all of the threads in the core file and explore the crashed |
| 3010 | // state. |
| 3011 | SetPrivateState (eStateStopped); |
| 3012 | |
| 3013 | } |
| 3014 | return error; |
| 3015 | } |
| 3016 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3017 | DynamicLoader * |
| 3018 | Process::GetDynamicLoader () |
| 3019 | { |
| 3020 | if (m_dyld_ap.get() == NULL) |
| 3021 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
| 3022 | return m_dyld_ap.get(); |
| 3023 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3024 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3025 | SystemRuntime * |
| 3026 | Process::GetSystemRuntime () |
| 3027 | { |
| 3028 | if (m_system_runtime_ap.get() == NULL) |
| 3029 | m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this)); |
| 3030 | return m_system_runtime_ap.get(); |
| 3031 | } |
| 3032 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3033 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3034 | Process::NextEventAction::EventActionResult |
| 3035 | Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3036 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3037 | StateType state = ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 3038 | switch (state) |
Greg Clayton | 19388cf | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 3039 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3040 | case eStateRunning: |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3041 | case eStateConnected: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3042 | return eEventActionRetry; |
| 3043 | |
| 3044 | case eStateStopped: |
| 3045 | case eStateCrashed: |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3046 | { |
| 3047 | // During attach, prior to sending the eStateStopped event, |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3048 | // lldb_private::Process subclasses must set the new process ID. |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3049 | assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3050 | // We don't want these events to be reported, so go set the ShouldReportStop here: |
| 3051 | m_process->GetThreadList().SetShouldReportStop (eVoteNo); |
| 3052 | |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3053 | if (m_exec_count > 0) |
| 3054 | { |
| 3055 | --m_exec_count; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3056 | RequestResume(); |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3057 | return eEventActionRetry; |
| 3058 | } |
| 3059 | else |
| 3060 | { |
| 3061 | m_process->CompleteAttach (); |
| 3062 | return eEventActionSuccess; |
| 3063 | } |
| 3064 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3065 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3066 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3067 | default: |
| 3068 | case eStateExited: |
| 3069 | case eStateInvalid: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3070 | break; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3071 | } |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3072 | |
| 3073 | m_exit_string.assign ("No valid Process"); |
| 3074 | return eEventActionExit; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3075 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3076 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3077 | Process::NextEventAction::EventActionResult |
| 3078 | Process::AttachCompletionHandler::HandleBeingInterrupted() |
| 3079 | { |
| 3080 | return eEventActionSuccess; |
| 3081 | } |
| 3082 | |
| 3083 | const char * |
| 3084 | Process::AttachCompletionHandler::GetExitString () |
| 3085 | { |
| 3086 | return m_exit_string.c_str(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | Error |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3090 | Process::Attach (ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3091 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3092 | m_abi_sp.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3093 | m_process_input_reader.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3094 | m_dyld_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3095 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3096 | m_os_ap.reset(); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3097 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3098 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3099 | Error error; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3100 | if (attach_pid == LLDB_INVALID_PROCESS_ID) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3101 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3102 | char process_name[PATH_MAX]; |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3103 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3104 | if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name))) |
Jim Ingham | 2ecb742 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 3105 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3106 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 3107 | |
| 3108 | if (wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3109 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3110 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 3111 | if (error.Success()) |
| 3112 | { |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3113 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3114 | { |
| 3115 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3116 | const bool restarted = false; |
| 3117 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3118 | // Now attach using these arguments. |
| 3119 | error = DoAttachToProcessWithName (process_name, wait_for_launch, attach_info); |
| 3120 | } |
| 3121 | else |
| 3122 | { |
| 3123 | // This shouldn't happen |
| 3124 | error.SetErrorString("failed to acquire process run lock"); |
| 3125 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 3126 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3127 | if (error.Fail()) |
| 3128 | { |
| 3129 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3130 | { |
| 3131 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3132 | if (error.AsCString() == NULL) |
| 3133 | error.SetErrorString("attach failed"); |
| 3134 | |
| 3135 | SetExitStatus(-1, error.AsCString()); |
| 3136 | } |
| 3137 | } |
| 3138 | else |
| 3139 | { |
| 3140 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3141 | StartPrivateStateThread(); |
| 3142 | } |
| 3143 | return error; |
| 3144 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3145 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3146 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3147 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3148 | ProcessInstanceInfoList process_infos; |
| 3149 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3150 | |
| 3151 | if (platform_sp) |
| 3152 | { |
| 3153 | ProcessInstanceInfoMatch match_info; |
| 3154 | match_info.GetProcessInfo() = attach_info; |
| 3155 | match_info.SetNameMatchType (eNameMatchEquals); |
| 3156 | platform_sp->FindProcesses (match_info, process_infos); |
| 3157 | const uint32_t num_matches = process_infos.GetSize(); |
| 3158 | if (num_matches == 1) |
| 3159 | { |
| 3160 | attach_pid = process_infos.GetProcessIDAtIndex(0); |
| 3161 | // Fall through and attach using the above process ID |
| 3162 | } |
| 3163 | else |
| 3164 | { |
| 3165 | match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name)); |
| 3166 | if (num_matches > 1) |
| 3167 | error.SetErrorStringWithFormat ("more than one process named %s", process_name); |
| 3168 | else |
| 3169 | error.SetErrorStringWithFormat ("could not find a process named %s", process_name); |
| 3170 | } |
| 3171 | } |
| 3172 | else |
| 3173 | { |
| 3174 | error.SetErrorString ("invalid platform, can't find processes by name"); |
| 3175 | return error; |
| 3176 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3177 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3178 | } |
| 3179 | else |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3180 | { |
| 3181 | error.SetErrorString ("invalid process name"); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3182 | } |
| 3183 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3184 | |
| 3185 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3186 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3187 | error = WillAttachToProcessWithID(attach_pid); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3188 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3189 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3190 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3191 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3192 | { |
| 3193 | // Now attach using these arguments. |
| 3194 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3195 | const bool restarted = false; |
| 3196 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3197 | error = DoAttachToProcessWithID (attach_pid, attach_info); |
| 3198 | } |
| 3199 | else |
| 3200 | { |
| 3201 | // This shouldn't happen |
| 3202 | error.SetErrorString("failed to acquire process run lock"); |
| 3203 | } |
| 3204 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3205 | if (error.Success()) |
| 3206 | { |
| 3207 | |
| 3208 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3209 | StartPrivateStateThread(); |
| 3210 | } |
| 3211 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3212 | { |
| 3213 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3214 | { |
| 3215 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3216 | const char *error_string = error.AsCString(); |
| 3217 | if (error_string == NULL) |
| 3218 | error_string = "attach failed"; |
| 3219 | |
| 3220 | SetExitStatus(-1, error_string); |
| 3221 | } |
| 3222 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3223 | } |
| 3224 | } |
| 3225 | return error; |
| 3226 | } |
| 3227 | |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3228 | void |
| 3229 | Process::CompleteAttach () |
| 3230 | { |
| 3231 | // Let the process subclass figure out at much as it can about the process |
| 3232 | // before we go looking for a dynamic loader plug-in. |
| 3233 | DidAttach(); |
| 3234 | |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3235 | // We just attached. If we have a platform, ask it for the process architecture, and if it isn't |
| 3236 | // the same as the one we've already set, switch architectures. |
| 3237 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3238 | assert (platform_sp.get()); |
| 3239 | if (platform_sp) |
| 3240 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3241 | const ArchSpec &target_arch = m_target.GetArchitecture(); |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 3242 | if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3243 | { |
| 3244 | ArchSpec platform_arch; |
| 3245 | platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch); |
| 3246 | if (platform_sp) |
| 3247 | { |
| 3248 | m_target.SetPlatform (platform_sp); |
| 3249 | m_target.SetArchitecture(platform_arch); |
| 3250 | } |
| 3251 | } |
| 3252 | else |
| 3253 | { |
| 3254 | ProcessInstanceInfo process_info; |
| 3255 | platform_sp->GetProcessInfo (GetID(), process_info); |
| 3256 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 3257 | if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3258 | m_target.SetArchitecture (process_arch); |
| 3259 | } |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | // 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] | 3263 | // plug-in |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3264 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3265 | if (dyld) |
| 3266 | dyld->DidAttach(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3267 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3268 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3269 | if (system_runtime) |
| 3270 | system_runtime->DidAttach(); |
| 3271 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3272 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3273 | // 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] | 3274 | const ModuleList &target_modules = m_target.GetImages(); |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3275 | Mutex::Locker modules_locker(target_modules.GetMutex()); |
| 3276 | size_t num_modules = target_modules.GetSize(); |
| 3277 | ModuleSP new_executable_module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3278 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 3279 | for (size_t i = 0; i < num_modules; i++) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3280 | { |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3281 | ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3282 | if (module_sp && module_sp->IsExecutable()) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3283 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 3284 | if (m_target.GetExecutableModulePointer() != module_sp.get()) |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3285 | new_executable_module_sp = module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3286 | break; |
| 3287 | } |
| 3288 | } |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3289 | if (new_executable_module_sp) |
| 3290 | m_target.SetExecutableModule (new_executable_module_sp, false); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3293 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3294 | Process::ConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3295 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3296 | m_abi_sp.reset(); |
| 3297 | m_process_input_reader.reset(); |
| 3298 | |
| 3299 | // Find the process and its architecture. Make sure it matches the architecture |
| 3300 | // of the current Target, and if not adjust it. |
| 3301 | |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3302 | Error error (DoConnectRemote (strm, remote_url)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3303 | if (error.Success()) |
| 3304 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3305 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3306 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3307 | EventSP event_sp; |
| 3308 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 3309 | |
| 3310 | if (state == eStateStopped || state == eStateCrashed) |
| 3311 | { |
| 3312 | // If we attached and actually have a process on the other end, then |
| 3313 | // this ended up being the equivalent of an attach. |
| 3314 | CompleteAttach (); |
| 3315 | |
| 3316 | // This delays passing the stopped event to listeners till |
| 3317 | // CompleteAttach gets a chance to complete... |
| 3318 | HandlePrivateEvent (event_sp); |
| 3319 | |
| 3320 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3321 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3322 | |
| 3323 | if (PrivateStateThreadIsValid ()) |
| 3324 | ResumePrivateStateThread (); |
| 3325 | else |
| 3326 | StartPrivateStateThread (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3327 | } |
| 3328 | return error; |
| 3329 | } |
| 3330 | |
| 3331 | |
| 3332 | Error |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 3333 | Process::PrivateResume () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3334 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3335 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3336 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3337 | 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] | 3338 | m_mod_id.GetStopID(), |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3339 | StateAsCString(m_public_state.GetValue()), |
| 3340 | StateAsCString(m_private_state.GetValue())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3341 | |
| 3342 | Error error (WillResume()); |
| 3343 | // Tell the process it is about to resume before the thread list |
| 3344 | if (error.Success()) |
| 3345 | { |
Johnny Chen | c4221e4 | 2010-12-02 20:53:05 +0000 | [diff] [blame] | 3346 | // 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] | 3347 | // can let all of our threads know that they are about to be |
| 3348 | // resumed. Threads will each be called with |
| 3349 | // Thread::WillResume(StateType) where StateType contains the state |
| 3350 | // that they are supposed to have when the process is resumed |
| 3351 | // (suspended/running/stepping). Threads should also check |
| 3352 | // their resume signal in lldb::Thread::GetResumeSignal() |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3353 | // 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] | 3354 | if (m_thread_list.WillResume()) |
| 3355 | { |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3356 | // Last thing, do the PreResumeActions. |
| 3357 | if (!RunPreResumeActions()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3358 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3359 | error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming."); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3360 | } |
| 3361 | else |
| 3362 | { |
| 3363 | m_mod_id.BumpResumeID(); |
| 3364 | error = DoResume(); |
| 3365 | if (error.Success()) |
| 3366 | { |
| 3367 | DidResume(); |
| 3368 | m_thread_list.DidResume(); |
| 3369 | if (log) |
| 3370 | log->Printf ("Process thinks the process has resumed."); |
| 3371 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3372 | } |
| 3373 | } |
| 3374 | else |
| 3375 | { |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 3376 | // Somebody wanted to run without running. So generate a continue & a stopped event, |
| 3377 | // and let the world handle them. |
| 3378 | if (log) |
| 3379 | log->Printf ("Process::PrivateResume() asked to simulate a start & stop."); |
| 3380 | |
| 3381 | SetPrivateState(eStateRunning); |
| 3382 | SetPrivateState(eStateStopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3383 | } |
| 3384 | } |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3385 | else if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3386 | log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>")); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3387 | return error; |
| 3388 | } |
| 3389 | |
| 3390 | Error |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3391 | Process::Halt (bool clear_thread_plans) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3392 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3393 | // Don't clear the m_clear_thread_plans_on_stop, only set it to true if |
| 3394 | // in case it was already set and some thread plan logic calls halt on its |
| 3395 | // own. |
| 3396 | m_clear_thread_plans_on_stop |= clear_thread_plans; |
| 3397 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3398 | // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since |
| 3399 | // we could just straightaway get another event. It just narrows the window... |
| 3400 | m_currently_handling_event.WaitForValueEqualTo(false); |
| 3401 | |
| 3402 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3403 | // Pause our private state thread so we can ensure no one else eats |
| 3404 | // the stop event out from under us. |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3405 | Listener halt_listener ("lldb.process.halt_listener"); |
| 3406 | HijackPrivateProcessEvents(&halt_listener); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3407 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3408 | EventSP event_sp; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3409 | Error error (WillHalt()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3410 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3411 | if (error.Success()) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3412 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3413 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3414 | bool caused_stop = false; |
| 3415 | |
| 3416 | // Ask the process subclass to actually halt our process |
| 3417 | error = DoHalt(caused_stop); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3418 | if (error.Success()) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3419 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3420 | if (m_public_state.GetValue() == eStateAttaching) |
| 3421 | { |
| 3422 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 3423 | Destroy (); |
| 3424 | } |
| 3425 | else |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3426 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3427 | // If "caused_stop" is true, then DoHalt stopped the process. If |
| 3428 | // "caused_stop" is false, the process was already stopped. |
| 3429 | // If the DoHalt caused the process to stop, then we want to catch |
| 3430 | // this event and set the interrupted bool to true before we pass |
| 3431 | // this along so clients know that the process was interrupted by |
| 3432 | // a halt command. |
| 3433 | if (caused_stop) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3434 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3435 | // Wait for 1 second for the process to stop. |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3436 | TimeValue timeout_time; |
| 3437 | timeout_time = TimeValue::Now(); |
| 3438 | timeout_time.OffsetWithSeconds(1); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3439 | bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp); |
| 3440 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3441 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3442 | if (!got_event || state == eStateInvalid) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3443 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3444 | // We timeout out and didn't get a stop event... |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3445 | error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState())); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3446 | } |
| 3447 | else |
| 3448 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 3449 | if (StateIsStoppedState (state, false)) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3450 | { |
| 3451 | // We caused the process to interrupt itself, so mark this |
| 3452 | // as such in the stop event so clients can tell an interrupted |
| 3453 | // process from a natural stop |
| 3454 | ProcessEventData::SetInterruptedInEvent (event_sp.get(), true); |
| 3455 | } |
| 3456 | else |
| 3457 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3458 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3459 | if (log) |
| 3460 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 3461 | error.SetErrorString ("Did not get stopped event after halt."); |
| 3462 | } |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3463 | } |
| 3464 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3465 | DidHalt(); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3466 | } |
| 3467 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3468 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3469 | // Resume our private state thread before we post the event (if any) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3470 | RestorePrivateProcessEvents(); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3471 | |
| 3472 | // Post any event we might have consumed. If all goes well, we will have |
| 3473 | // stopped the process, intercepted the event and set the interrupted |
| 3474 | // bool in the event. Post it to the private event queue and that will end up |
| 3475 | // correctly setting the state. |
| 3476 | if (event_sp) |
| 3477 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 3478 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3479 | return error; |
| 3480 | } |
| 3481 | |
| 3482 | Error |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3483 | Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp) |
| 3484 | { |
| 3485 | Error error; |
| 3486 | if (m_public_state.GetValue() == eStateRunning) |
| 3487 | { |
| 3488 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3489 | if (log) |
| 3490 | log->Printf("Process::Destroy() About to halt."); |
| 3491 | error = Halt(); |
| 3492 | if (error.Success()) |
| 3493 | { |
| 3494 | // Consume the halt event. |
| 3495 | TimeValue timeout (TimeValue::Now()); |
| 3496 | timeout.OffsetWithSeconds(1); |
| 3497 | StateType state = WaitForProcessToStop (&timeout, &exit_event_sp); |
| 3498 | |
| 3499 | // If the process exited while we were waiting for it to stop, put the exited event into |
| 3500 | // the shared pointer passed in and return. Our caller doesn't need to do anything else, since |
| 3501 | // they don't have a process anymore... |
| 3502 | |
| 3503 | if (state == eStateExited || m_private_state.GetValue() == eStateExited) |
| 3504 | { |
| 3505 | if (log) |
| 3506 | log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt."); |
| 3507 | return error; |
| 3508 | } |
| 3509 | else |
| 3510 | exit_event_sp.reset(); // It is ok to consume any non-exit stop events |
| 3511 | |
| 3512 | if (state != eStateStopped) |
| 3513 | { |
| 3514 | if (log) |
| 3515 | log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state)); |
| 3516 | // If we really couldn't stop the process then we should just error out here, but if the |
| 3517 | // lower levels just bobbled sending the event and we really are stopped, then continue on. |
| 3518 | StateType private_state = m_private_state.GetValue(); |
| 3519 | if (private_state != eStateStopped) |
| 3520 | { |
| 3521 | return error; |
| 3522 | } |
| 3523 | } |
| 3524 | } |
| 3525 | else |
| 3526 | { |
| 3527 | if (log) |
| 3528 | log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString()); |
| 3529 | } |
| 3530 | } |
| 3531 | return error; |
| 3532 | } |
| 3533 | |
| 3534 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3535 | Process::Detach (bool keep_stopped) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3536 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3537 | EventSP exit_event_sp; |
| 3538 | Error error; |
| 3539 | m_destroy_in_process = true; |
| 3540 | |
| 3541 | error = WillDetach(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3542 | |
| 3543 | if (error.Success()) |
| 3544 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3545 | if (DetachRequiresHalt()) |
| 3546 | { |
| 3547 | error = HaltForDestroyOrDetach (exit_event_sp); |
| 3548 | if (!error.Success()) |
| 3549 | { |
| 3550 | m_destroy_in_process = false; |
| 3551 | return error; |
| 3552 | } |
| 3553 | else if (exit_event_sp) |
| 3554 | { |
| 3555 | // We shouldn't need to do anything else here. There's no process left to detach from... |
| 3556 | StopPrivateStateThread(); |
| 3557 | m_destroy_in_process = false; |
| 3558 | return error; |
| 3559 | } |
| 3560 | } |
| 3561 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3562 | error = DoDetach(keep_stopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3563 | if (error.Success()) |
| 3564 | { |
| 3565 | DidDetach(); |
| 3566 | StopPrivateStateThread(); |
| 3567 | } |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3568 | else |
| 3569 | { |
| 3570 | return error; |
| 3571 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3572 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3573 | m_destroy_in_process = false; |
| 3574 | |
| 3575 | // If we exited when we were waiting for a process to stop, then |
| 3576 | // forward the event here so we don't lose the event |
| 3577 | if (exit_event_sp) |
| 3578 | { |
| 3579 | // Directly broadcast our exited event because we shut down our |
| 3580 | // private state thread above |
| 3581 | BroadcastEvent(exit_event_sp); |
| 3582 | } |
| 3583 | |
| 3584 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3585 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3586 | // it here so when we do to tear down the process we don't get an error destroying the lock. |
| 3587 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3588 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3589 | return error; |
| 3590 | } |
| 3591 | |
| 3592 | Error |
| 3593 | Process::Destroy () |
| 3594 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3595 | |
| 3596 | // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work |
| 3597 | // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt |
| 3598 | // failed and the process stays around for some reason it won't be in a confused state. |
| 3599 | |
| 3600 | m_destroy_in_process = true; |
| 3601 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3602 | Error error (WillDestroy()); |
| 3603 | if (error.Success()) |
| 3604 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3605 | EventSP exit_event_sp; |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3606 | if (DestroyRequiresHalt()) |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3607 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3608 | error = HaltForDestroyOrDetach(exit_event_sp); |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3609 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3610 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3611 | if (m_public_state.GetValue() != eStateRunning) |
| 3612 | { |
| 3613 | // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to |
| 3614 | // kill it, we don't want it hitting a breakpoint... |
| 3615 | // Only do this if we've stopped, however, since if we didn't manage to halt it above, then |
| 3616 | // we're not going to have much luck doing this now. |
| 3617 | m_thread_list.DiscardThreadPlans(); |
| 3618 | DisableAllBreakpointSites(); |
| 3619 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3620 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3621 | error = DoDestroy(); |
| 3622 | if (error.Success()) |
| 3623 | { |
| 3624 | DidDestroy(); |
| 3625 | StopPrivateStateThread(); |
| 3626 | } |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3627 | m_stdio_communication.StopReadThread(); |
| 3628 | m_stdio_communication.Disconnect(); |
| 3629 | if (m_process_input_reader && m_process_input_reader->IsActive()) |
| 3630 | m_target.GetDebugger().PopInputReader (m_process_input_reader); |
| 3631 | if (m_process_input_reader) |
| 3632 | m_process_input_reader.reset(); |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3633 | |
| 3634 | // If we exited when we were waiting for a process to stop, then |
| 3635 | // forward the event here so we don't lose the event |
| 3636 | if (exit_event_sp) |
| 3637 | { |
| 3638 | // Directly broadcast our exited event because we shut down our |
| 3639 | // private state thread above |
| 3640 | BroadcastEvent(exit_event_sp); |
| 3641 | } |
| 3642 | |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3643 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3644 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3645 | // 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] | 3646 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3647 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3648 | |
| 3649 | m_destroy_in_process = false; |
| 3650 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3651 | return error; |
| 3652 | } |
| 3653 | |
| 3654 | Error |
| 3655 | Process::Signal (int signal) |
| 3656 | { |
| 3657 | Error error (WillSignal()); |
| 3658 | if (error.Success()) |
| 3659 | { |
| 3660 | error = DoSignal(signal); |
| 3661 | if (error.Success()) |
| 3662 | DidSignal(); |
| 3663 | } |
| 3664 | return error; |
| 3665 | } |
| 3666 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3667 | lldb::ByteOrder |
| 3668 | Process::GetByteOrder () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3669 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3670 | return m_target.GetArchitecture().GetByteOrder(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | uint32_t |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3674 | Process::GetAddressByteSize () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3675 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3676 | return m_target.GetArchitecture().GetAddressByteSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3679 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3680 | bool |
| 3681 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 3682 | { |
| 3683 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 3684 | bool return_value = true; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3685 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3686 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3687 | switch (state) |
| 3688 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3689 | case eStateConnected: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3690 | case eStateAttaching: |
| 3691 | case eStateLaunching: |
| 3692 | case eStateDetached: |
| 3693 | case eStateExited: |
| 3694 | case eStateUnloaded: |
| 3695 | // These events indicate changes in the state of the debugging session, always report them. |
| 3696 | return_value = true; |
| 3697 | break; |
| 3698 | case eStateInvalid: |
| 3699 | // We stopped for no apparent reason, don't report it. |
| 3700 | return_value = false; |
| 3701 | break; |
| 3702 | case eStateRunning: |
| 3703 | case eStateStepping: |
| 3704 | // If we've started the target running, we handle the cases where we |
| 3705 | // are already running and where there is a transition from stopped to |
| 3706 | // running differently. |
| 3707 | // running -> running: Automatically suppress extra running events |
| 3708 | // stopped -> running: Report except when there is one or more no votes |
| 3709 | // and no yes votes. |
| 3710 | SynchronouslyNotifyStateChanged (state); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3711 | switch (m_last_broadcast_state) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3712 | { |
| 3713 | case eStateRunning: |
| 3714 | case eStateStepping: |
| 3715 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 3716 | return_value = false; |
| 3717 | break; |
| 3718 | default: |
| 3719 | // TODO: make this work correctly. For now always report |
| 3720 | // run if we aren't running so we don't miss any runnning |
| 3721 | // events. If I run the lldb/test/thread/a.out file and |
| 3722 | // break at main.cpp:58, run and hit the breakpoints on |
| 3723 | // multiple threads, then somehow during the stepping over |
| 3724 | // of all breakpoints no run gets reported. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3725 | |
| 3726 | // This is a transition from stop to run. |
| 3727 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 3728 | { |
| 3729 | case eVoteYes: |
| 3730 | case eVoteNoOpinion: |
| 3731 | return_value = true; |
| 3732 | break; |
| 3733 | case eVoteNo: |
| 3734 | return_value = false; |
| 3735 | break; |
| 3736 | } |
| 3737 | break; |
| 3738 | } |
| 3739 | break; |
| 3740 | case eStateStopped: |
| 3741 | case eStateCrashed: |
| 3742 | case eStateSuspended: |
| 3743 | { |
| 3744 | // We've stopped. First see if we're going to restart the target. |
| 3745 | // If we are going to stop, then we always broadcast the event. |
| 3746 | // 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] | 3747 | // If no thread has an opinion, we don't report it. |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3748 | |
Jim Ingham | cb4ca11 | 2012-05-16 01:32:14 +0000 | [diff] [blame] | 3749 | RefreshStateAfterStop (); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3750 | if (ProcessEventData::GetInterruptedFromEvent (event_ptr)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3751 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3752 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3753 | log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", |
| 3754 | event_ptr, |
| 3755 | StateAsCString(state)); |
| 3756 | return_value = true; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3757 | } |
| 3758 | else |
| 3759 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3760 | bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr); |
| 3761 | bool should_resume = false; |
| 3762 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3763 | // It makes no sense to ask "ShouldStop" if we've already been restarted... |
| 3764 | // Asking the thread list is also not likely to go well, since we are running again. |
| 3765 | // So in that case just report the event. |
| 3766 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3767 | if (!was_restarted) |
| 3768 | should_resume = m_thread_list.ShouldStop (event_ptr) == false; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3769 | |
| 3770 | if (was_restarted || should_resume || m_resume_requested) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3771 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3772 | Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr); |
| 3773 | if (log) |
| 3774 | log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.", |
| 3775 | should_resume, |
| 3776 | StateAsCString(state), |
| 3777 | was_restarted, |
| 3778 | stop_vote); |
| 3779 | |
| 3780 | switch (stop_vote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3781 | { |
| 3782 | case eVoteYes: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3783 | return_value = true; |
| 3784 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3785 | case eVoteNoOpinion: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3786 | case eVoteNo: |
| 3787 | return_value = false; |
| 3788 | break; |
| 3789 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3790 | |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 3791 | if (!was_restarted) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3792 | { |
| 3793 | if (log) |
| 3794 | log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state)); |
| 3795 | ProcessEventData::SetRestartedInEvent(event_ptr, true); |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 3796 | PrivateResume (); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3799 | } |
| 3800 | else |
| 3801 | { |
| 3802 | return_value = true; |
| 3803 | SynchronouslyNotifyStateChanged (state); |
| 3804 | } |
| 3805 | } |
| 3806 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3807 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3808 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3809 | |
| 3810 | // We do some coalescing of events (for instance two consecutive running events get coalesced.) |
| 3811 | // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state |
| 3812 | // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done, |
| 3813 | // because the PublicState reflects the last event pulled off the queue, and there may be several |
| 3814 | // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event |
| 3815 | // yet. m_last_broadcast_state gets updated here. |
| 3816 | |
| 3817 | if (return_value) |
| 3818 | m_last_broadcast_state = state; |
| 3819 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3820 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3821 | log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s", |
| 3822 | event_ptr, |
| 3823 | StateAsCString(state), |
| 3824 | StateAsCString(m_last_broadcast_state), |
| 3825 | return_value ? "YES" : "NO"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3826 | return return_value; |
| 3827 | } |
| 3828 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3829 | |
| 3830 | bool |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3831 | Process::StartPrivateStateThread (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3832 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3833 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3834 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3835 | bool already_running = PrivateStateThreadIsValid (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3836 | if (log) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3837 | log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread"); |
| 3838 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3839 | if (!force && already_running) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3840 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3841 | |
| 3842 | // Create a thread that watches our internal state and controls which |
| 3843 | // events make it to clients (into the DCProcess event queue). |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 3844 | char thread_name[1024]; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3845 | if (already_running) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3846 | 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] | 3847 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3848 | 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] | 3849 | |
| 3850 | // Create the private state thread, and start it running. |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 3851 | m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL); |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3852 | bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread); |
| 3853 | if (success) |
| 3854 | { |
| 3855 | ResumePrivateStateThread(); |
| 3856 | return true; |
| 3857 | } |
| 3858 | else |
| 3859 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | void |
| 3863 | Process::PausePrivateStateThread () |
| 3864 | { |
| 3865 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 3866 | } |
| 3867 | |
| 3868 | void |
| 3869 | Process::ResumePrivateStateThread () |
| 3870 | { |
| 3871 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 3872 | } |
| 3873 | |
| 3874 | void |
| 3875 | Process::StopPrivateStateThread () |
| 3876 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3877 | if (PrivateStateThreadIsValid ()) |
| 3878 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3879 | else |
| 3880 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3881 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3882 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3883 | 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] | 3884 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | void |
| 3888 | Process::ControlPrivateStateThread (uint32_t signal) |
| 3889 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3890 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3891 | |
| 3892 | assert (signal == eBroadcastInternalStateControlStop || |
| 3893 | signal == eBroadcastInternalStateControlPause || |
| 3894 | signal == eBroadcastInternalStateControlResume); |
| 3895 | |
| 3896 | if (log) |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 3897 | log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3898 | |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 3899 | // Signal the private state thread. First we should copy this is case the |
| 3900 | // thread starts exiting since the private state thread will NULL this out |
| 3901 | // when it exits |
| 3902 | const lldb::thread_t private_state_thread = m_private_state_thread; |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 3903 | if (IS_VALID_LLDB_HOST_THREAD(private_state_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3904 | { |
| 3905 | TimeValue timeout_time; |
| 3906 | bool timed_out; |
| 3907 | |
| 3908 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 3909 | |
| 3910 | timeout_time = TimeValue::Now(); |
| 3911 | timeout_time.OffsetWithSeconds(2); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3912 | if (log) |
| 3913 | log->Printf ("Sending control event of type: %d.", signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3914 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 3915 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 3916 | |
| 3917 | if (signal == eBroadcastInternalStateControlStop) |
| 3918 | { |
| 3919 | if (timed_out) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3920 | { |
| 3921 | Error error; |
| 3922 | Host::ThreadCancel (private_state_thread, &error); |
| 3923 | if (log) |
| 3924 | log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString()); |
| 3925 | } |
| 3926 | else |
| 3927 | { |
| 3928 | if (log) |
| 3929 | log->Printf ("The control event killed the private state thread without having to cancel."); |
| 3930 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3931 | |
| 3932 | thread_result_t result = NULL; |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 3933 | Host::ThreadJoin (private_state_thread, &result, NULL); |
Greg Clayton | 49182ed | 2010-07-22 18:34:21 +0000 | [diff] [blame] | 3934 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3935 | } |
| 3936 | } |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3937 | else |
| 3938 | { |
| 3939 | if (log) |
| 3940 | log->Printf ("Private state thread already dead, no need to signal it to stop."); |
| 3941 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
| 3944 | void |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 3945 | Process::SendAsyncInterrupt () |
| 3946 | { |
| 3947 | if (PrivateStateThreadIsValid()) |
| 3948 | m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 3949 | else |
| 3950 | BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 3951 | } |
| 3952 | |
| 3953 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3954 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 3955 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3956 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3957 | m_resume_requested = false; |
| 3958 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3959 | m_currently_handling_event.SetValue(true, eBroadcastNever); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3960 | |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 3961 | const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3962 | |
| 3963 | // First check to see if anybody wants a shot at this event: |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3964 | if (m_next_event_action_ap.get() != NULL) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3965 | { |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3966 | NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3967 | if (log) |
| 3968 | log->Printf ("Ran next event action, result was %d.", action_result); |
| 3969 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3970 | switch (action_result) |
| 3971 | { |
| 3972 | case NextEventAction::eEventActionSuccess: |
| 3973 | SetNextEventAction(NULL); |
| 3974 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3975 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3976 | case NextEventAction::eEventActionRetry: |
| 3977 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3978 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3979 | case NextEventAction::eEventActionExit: |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 3980 | // Handle Exiting Here. If we already got an exited event, |
| 3981 | // we should just propagate it. Otherwise, swallow this event, |
| 3982 | // and set our state to exit so the next event will kill us. |
| 3983 | if (new_state != eStateExited) |
| 3984 | { |
| 3985 | // FIXME: should cons up an exited event, and discard this one. |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3986 | SetExitStatus(0, m_next_event_action_ap->GetExitString()); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3987 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 3988 | SetNextEventAction(NULL); |
| 3989 | return; |
| 3990 | } |
| 3991 | SetNextEventAction(NULL); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3992 | break; |
| 3993 | } |
| 3994 | } |
| 3995 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3996 | // See if we should broadcast this state to external clients? |
| 3997 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3998 | |
| 3999 | if (should_broadcast) |
| 4000 | { |
| 4001 | if (log) |
| 4002 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4003 | 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] | 4004 | __FUNCTION__, |
| 4005 | GetID(), |
| 4006 | StateAsCString(new_state), |
| 4007 | StateAsCString (GetState ()), |
| 4008 | IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4009 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4010 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4011 | if (StateIsRunningState (new_state)) |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4012 | PushProcessInputReader (); |
Jim Ingham | b78d73f | 2013-05-15 01:21:48 +0000 | [diff] [blame] | 4013 | else if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4014 | PopProcessInputReader (); |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4015 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4016 | BroadcastEvent (event_sp); |
| 4017 | } |
| 4018 | else |
| 4019 | { |
| 4020 | if (log) |
| 4021 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4022 | 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] | 4023 | __FUNCTION__, |
| 4024 | GetID(), |
| 4025 | StateAsCString(new_state), |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 4026 | StateAsCString (GetState ())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4027 | } |
| 4028 | } |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 4029 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4032 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4033 | Process::PrivateStateThread (void *arg) |
| 4034 | { |
| 4035 | Process *proc = static_cast<Process*> (arg); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4036 | thread_result_t result = proc->RunPrivateStateThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4037 | return result; |
| 4038 | } |
| 4039 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4040 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4041 | Process::RunPrivateStateThread () |
| 4042 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4043 | bool control_only = true; |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4044 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4045 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4046 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4047 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4048 | 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] | 4049 | |
| 4050 | bool exit_now = false; |
| 4051 | while (!exit_now) |
| 4052 | { |
| 4053 | EventSP event_sp; |
| 4054 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 4055 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 4056 | { |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4057 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4058 | 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] | 4059 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4060 | switch (event_sp->GetType()) |
| 4061 | { |
| 4062 | case eBroadcastInternalStateControlStop: |
| 4063 | exit_now = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4064 | break; // doing any internal state managment below |
| 4065 | |
| 4066 | case eBroadcastInternalStateControlPause: |
| 4067 | control_only = true; |
| 4068 | break; |
| 4069 | |
| 4070 | case eBroadcastInternalStateControlResume: |
| 4071 | control_only = false; |
| 4072 | break; |
| 4073 | } |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4074 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4075 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4076 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4077 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4078 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
| 4079 | { |
| 4080 | if (m_public_state.GetValue() == eStateAttaching) |
| 4081 | { |
| 4082 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4083 | 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] | 4084 | BroadcastEvent (eBroadcastBitInterrupt, NULL); |
| 4085 | } |
| 4086 | else |
| 4087 | { |
| 4088 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4089 | 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] | 4090 | Halt(); |
| 4091 | } |
| 4092 | continue; |
| 4093 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4094 | |
| 4095 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4096 | |
| 4097 | if (internal_state != eStateInvalid) |
| 4098 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 4099 | if (m_clear_thread_plans_on_stop && |
| 4100 | StateIsStoppedState(internal_state, true)) |
| 4101 | { |
| 4102 | m_clear_thread_plans_on_stop = false; |
| 4103 | m_thread_list.DiscardThreadPlans(); |
| 4104 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4105 | HandlePrivateEvent (event_sp); |
| 4106 | } |
| 4107 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 4108 | if (internal_state == eStateInvalid || |
| 4109 | internal_state == eStateExited || |
| 4110 | internal_state == eStateDetached ) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4111 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4112 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4113 | 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] | 4114 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4115 | break; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4116 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 4119 | // Verify log is still enabled before attempting to write to it... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4120 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4121 | 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] | 4122 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 4123 | m_public_run_lock.SetStopped(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 4124 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
| 4125 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4126 | return NULL; |
| 4127 | } |
| 4128 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4129 | //------------------------------------------------------------------ |
| 4130 | // Process Event Data |
| 4131 | //------------------------------------------------------------------ |
| 4132 | |
| 4133 | Process::ProcessEventData::ProcessEventData () : |
| 4134 | EventData (), |
| 4135 | m_process_sp (), |
| 4136 | m_state (eStateInvalid), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4137 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4138 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4139 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4140 | { |
| 4141 | } |
| 4142 | |
| 4143 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 4144 | EventData (), |
| 4145 | m_process_sp (process_sp), |
| 4146 | m_state (state), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4147 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4148 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4149 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4150 | { |
| 4151 | } |
| 4152 | |
| 4153 | Process::ProcessEventData::~ProcessEventData() |
| 4154 | { |
| 4155 | } |
| 4156 | |
| 4157 | const ConstString & |
| 4158 | Process::ProcessEventData::GetFlavorString () |
| 4159 | { |
| 4160 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 4161 | return g_flavor; |
| 4162 | } |
| 4163 | |
| 4164 | const ConstString & |
| 4165 | Process::ProcessEventData::GetFlavor () const |
| 4166 | { |
| 4167 | return ProcessEventData::GetFlavorString (); |
| 4168 | } |
| 4169 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4170 | void |
| 4171 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 4172 | { |
| 4173 | // 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] | 4174 | // off of the private process event queue, and then any number of times, first when it gets pulled off of |
| 4175 | // the public event queue, then other times when we're pretending that this is where we stopped at the |
| 4176 | // end of expression evaluation. m_update_state is used to distinguish these |
| 4177 | // 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] | 4178 | // 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] | 4179 | if (m_update_state != 1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4180 | return; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4181 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4182 | m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4183 | |
| 4184 | // If we're stopped and haven't restarted, then do the breakpoint commands here: |
| 4185 | if (m_state == eStateStopped && ! m_restarted) |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4186 | { |
| 4187 | ThreadList &curr_thread_list = m_process_sp->GetThreadList(); |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4188 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 4189 | uint32_t idx; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 4190 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4191 | // The actions might change one of the thread's stop_info's opinions about whether we should |
| 4192 | // stop the process, so we need to query that as we go. |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4193 | |
| 4194 | // One other complication here, is that we try to catch any case where the target has run (except for expressions) |
| 4195 | // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and |
| 4196 | // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like |
| 4197 | // 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 |
| 4198 | // against this list & bag out if anything differs. |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4199 | std::vector<uint32_t> thread_index_array(num_threads); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4200 | for (idx = 0; idx < num_threads; ++idx) |
| 4201 | thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID(); |
| 4202 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4203 | // Use this to track whether we should continue from here. We will only continue the target running if |
| 4204 | // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running, |
| 4205 | // then it doesn't matter what the other threads say... |
| 4206 | |
| 4207 | bool still_should_stop = false; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4208 | |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4209 | // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a |
| 4210 | // valid stop reason. In that case we should just stop, because we have no way of telling what the right |
| 4211 | // thing to do is, and it's better to let the user decide than continue behind their backs. |
| 4212 | |
| 4213 | bool does_anybody_have_an_opinion = false; |
| 4214 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4215 | for (idx = 0; idx < num_threads; ++idx) |
| 4216 | { |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4217 | curr_thread_list = m_process_sp->GetThreadList(); |
| 4218 | if (curr_thread_list.GetSize() != num_threads) |
| 4219 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4220 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4221 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4222 | 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] | 4223 | break; |
| 4224 | } |
| 4225 | |
| 4226 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 4227 | |
| 4228 | if (thread_sp->GetIndexID() != thread_index_array[idx]) |
| 4229 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4230 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4231 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4232 | 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] | 4233 | idx, |
| 4234 | thread_index_array[idx], |
| 4235 | thread_sp->GetIndexID()); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4236 | break; |
| 4237 | } |
| 4238 | |
Jim Ingham | b15bfc7 | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 4239 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo (); |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 4240 | if (stop_info_sp && stop_info_sp->IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4241 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4242 | does_anybody_have_an_opinion = true; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4243 | bool this_thread_wants_to_stop; |
| 4244 | if (stop_info_sp->GetOverrideShouldStop()) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4245 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4246 | this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue(); |
| 4247 | } |
| 4248 | else |
| 4249 | { |
| 4250 | stop_info_sp->PerformAction(event_ptr); |
| 4251 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 4252 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 4253 | // that state appropriately. |
| 4254 | // We also need to stop processing actions, since they aren't expecting the target to be running. |
| 4255 | |
| 4256 | // FIXME: we might have run. |
| 4257 | if (stop_info_sp->HasTargetRunSinceMe()) |
| 4258 | { |
| 4259 | SetRestarted (true); |
| 4260 | break; |
| 4261 | } |
| 4262 | |
| 4263 | this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4264 | } |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4265 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4266 | if (still_should_stop == false) |
| 4267 | still_should_stop = this_thread_wants_to_stop; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4268 | } |
| 4269 | } |
Jim Ingham | 3ebcf7f | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 4270 | |
Ashok Thirumurthi | cf7c55e | 2013-04-18 14:38:20 +0000 | [diff] [blame] | 4271 | |
Jim Ingham | a8ca6e2 | 2013-05-03 23:04:37 +0000 | [diff] [blame] | 4272 | if (!GetRestarted()) |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4273 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4274 | if (!still_should_stop && does_anybody_have_an_opinion) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4275 | { |
| 4276 | // We've been asked to continue, so do that here. |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4277 | SetRestarted(true); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 4278 | // Use the public resume method here, since this is just |
| 4279 | // extending a public resume. |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4280 | m_process_sp->PrivateResume(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4281 | } |
| 4282 | else |
| 4283 | { |
| 4284 | // If we didn't restart, run the Stop Hooks here: |
| 4285 | // They might also restart the target, so watch for that. |
| 4286 | m_process_sp->GetTarget().RunStopHooks(); |
| 4287 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 4288 | SetRestarted(true); |
| 4289 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4290 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | void |
| 4295 | Process::ProcessEventData::Dump (Stream *s) const |
| 4296 | { |
| 4297 | if (m_process_sp) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4298 | 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] | 4299 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4300 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | const Process::ProcessEventData * |
| 4304 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 4305 | { |
| 4306 | if (event_ptr) |
| 4307 | { |
| 4308 | const EventData *event_data = event_ptr->GetData(); |
| 4309 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 4310 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 4311 | } |
| 4312 | return NULL; |
| 4313 | } |
| 4314 | |
| 4315 | ProcessSP |
| 4316 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 4317 | { |
| 4318 | ProcessSP process_sp; |
| 4319 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4320 | if (data) |
| 4321 | process_sp = data->GetProcessSP(); |
| 4322 | return process_sp; |
| 4323 | } |
| 4324 | |
| 4325 | StateType |
| 4326 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 4327 | { |
| 4328 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4329 | if (data == NULL) |
| 4330 | return eStateInvalid; |
| 4331 | else |
| 4332 | return data->GetState(); |
| 4333 | } |
| 4334 | |
| 4335 | bool |
| 4336 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 4337 | { |
| 4338 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4339 | if (data == NULL) |
| 4340 | return false; |
| 4341 | else |
| 4342 | return data->GetRestarted(); |
| 4343 | } |
| 4344 | |
| 4345 | void |
| 4346 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 4347 | { |
| 4348 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4349 | if (data != NULL) |
| 4350 | data->SetRestarted(new_value); |
| 4351 | } |
| 4352 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4353 | size_t |
| 4354 | Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr) |
| 4355 | { |
| 4356 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4357 | if (data != NULL) |
| 4358 | return data->GetNumRestartedReasons(); |
| 4359 | else |
| 4360 | return 0; |
| 4361 | } |
| 4362 | |
| 4363 | const char * |
| 4364 | Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx) |
| 4365 | { |
| 4366 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4367 | if (data != NULL) |
| 4368 | return data->GetRestartedReasonAtIndex(idx); |
| 4369 | else |
| 4370 | return NULL; |
| 4371 | } |
| 4372 | |
| 4373 | void |
| 4374 | Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason) |
| 4375 | { |
| 4376 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4377 | if (data != NULL) |
| 4378 | data->AddRestartedReason(reason); |
| 4379 | } |
| 4380 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4381 | bool |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4382 | Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr) |
| 4383 | { |
| 4384 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4385 | if (data == NULL) |
| 4386 | return false; |
| 4387 | else |
| 4388 | return data->GetInterrupted (); |
| 4389 | } |
| 4390 | |
| 4391 | void |
| 4392 | Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value) |
| 4393 | { |
| 4394 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4395 | if (data != NULL) |
| 4396 | data->SetInterrupted(new_value); |
| 4397 | } |
| 4398 | |
| 4399 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4400 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 4401 | { |
| 4402 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4403 | if (data) |
| 4404 | { |
| 4405 | data->SetUpdateStateOnRemoval(); |
| 4406 | return true; |
| 4407 | } |
| 4408 | return false; |
| 4409 | } |
| 4410 | |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 4411 | lldb::TargetSP |
| 4412 | Process::CalculateTarget () |
| 4413 | { |
| 4414 | return m_target.shared_from_this(); |
| 4415 | } |
| 4416 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4417 | void |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 4418 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4419 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4420 | exe_ctx.SetTargetPtr (&m_target); |
| 4421 | exe_ctx.SetProcessPtr (this); |
| 4422 | exe_ctx.SetThreadPtr(NULL); |
| 4423 | exe_ctx.SetFramePtr (NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4426 | //uint32_t |
| 4427 | //Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 4428 | //{ |
| 4429 | // return 0; |
| 4430 | //} |
| 4431 | // |
| 4432 | //ArchSpec |
| 4433 | //Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 4434 | //{ |
| 4435 | // return Host::GetArchSpecForExistingProcess (pid); |
| 4436 | //} |
| 4437 | // |
| 4438 | //ArchSpec |
| 4439 | //Process::GetArchSpecForExistingProcess (const char *process_name) |
| 4440 | //{ |
| 4441 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 4442 | //} |
| 4443 | // |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4444 | void |
| 4445 | Process::AppendSTDOUT (const char * s, size_t len) |
| 4446 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 4447 | Mutex::Locker locker (m_stdio_communication_mutex); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4448 | m_stdout_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4449 | BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState())); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | void |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4453 | Process::AppendSTDERR (const char * s, size_t len) |
| 4454 | { |
| 4455 | Mutex::Locker locker (m_stdio_communication_mutex); |
| 4456 | m_stderr_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4457 | BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState())); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4460 | void |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4461 | Process::BroadcastAsyncProfileData(const std::string &one_profile_data) |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4462 | { |
| 4463 | Mutex::Locker locker (m_profile_data_comm_mutex); |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4464 | m_profile_data.push_back(one_profile_data); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4465 | BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState())); |
| 4466 | } |
| 4467 | |
| 4468 | size_t |
| 4469 | Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error) |
| 4470 | { |
| 4471 | Mutex::Locker locker(m_profile_data_comm_mutex); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4472 | if (m_profile_data.empty()) |
| 4473 | return 0; |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4474 | |
| 4475 | std::string &one_profile_data = m_profile_data.front(); |
| 4476 | size_t bytes_available = one_profile_data.size(); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4477 | if (bytes_available > 0) |
| 4478 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4479 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4480 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4481 | 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] | 4482 | if (bytes_available > buf_size) |
| 4483 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4484 | memcpy(buf, one_profile_data.c_str(), buf_size); |
| 4485 | one_profile_data.erase(0, buf_size); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4486 | bytes_available = buf_size; |
| 4487 | } |
| 4488 | else |
| 4489 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4490 | memcpy(buf, one_profile_data.c_str(), bytes_available); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4491 | m_profile_data.erase(m_profile_data.begin()); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4492 | } |
| 4493 | } |
| 4494 | return bytes_available; |
| 4495 | } |
| 4496 | |
| 4497 | |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4498 | //------------------------------------------------------------------ |
| 4499 | // Process STDIO |
| 4500 | //------------------------------------------------------------------ |
| 4501 | |
| 4502 | size_t |
| 4503 | Process::GetSTDOUT (char *buf, size_t buf_size, Error &error) |
| 4504 | { |
| 4505 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4506 | size_t bytes_available = m_stdout_data.size(); |
| 4507 | if (bytes_available > 0) |
| 4508 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4509 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4510 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4511 | 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] | 4512 | if (bytes_available > buf_size) |
| 4513 | { |
| 4514 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 4515 | m_stdout_data.erase(0, buf_size); |
| 4516 | bytes_available = buf_size; |
| 4517 | } |
| 4518 | else |
| 4519 | { |
| 4520 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 4521 | m_stdout_data.clear(); |
| 4522 | } |
| 4523 | } |
| 4524 | return bytes_available; |
| 4525 | } |
| 4526 | |
| 4527 | |
| 4528 | size_t |
| 4529 | Process::GetSTDERR (char *buf, size_t buf_size, Error &error) |
| 4530 | { |
| 4531 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4532 | size_t bytes_available = m_stderr_data.size(); |
| 4533 | if (bytes_available > 0) |
| 4534 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4535 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4536 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4537 | 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] | 4538 | if (bytes_available > buf_size) |
| 4539 | { |
| 4540 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 4541 | m_stderr_data.erase(0, buf_size); |
| 4542 | bytes_available = buf_size; |
| 4543 | } |
| 4544 | else |
| 4545 | { |
| 4546 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 4547 | m_stderr_data.clear(); |
| 4548 | } |
| 4549 | } |
| 4550 | return bytes_available; |
| 4551 | } |
| 4552 | |
| 4553 | void |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4554 | Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 4555 | { |
| 4556 | Process *process = (Process *) baton; |
| 4557 | process->AppendSTDOUT (static_cast<const char *>(src), src_len); |
| 4558 | } |
| 4559 | |
| 4560 | size_t |
| 4561 | Process::ProcessInputReaderCallback (void *baton, |
| 4562 | InputReader &reader, |
| 4563 | lldb::InputReaderAction notification, |
| 4564 | const char *bytes, |
| 4565 | size_t bytes_len) |
| 4566 | { |
| 4567 | Process *process = (Process *) baton; |
| 4568 | |
| 4569 | switch (notification) |
| 4570 | { |
| 4571 | case eInputReaderActivate: |
| 4572 | break; |
| 4573 | |
| 4574 | case eInputReaderDeactivate: |
| 4575 | break; |
| 4576 | |
| 4577 | case eInputReaderReactivate: |
| 4578 | break; |
| 4579 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 4580 | case eInputReaderAsynchronousOutputWritten: |
| 4581 | break; |
| 4582 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4583 | case eInputReaderGotToken: |
| 4584 | { |
| 4585 | Error error; |
| 4586 | process->PutSTDIN (bytes, bytes_len, error); |
| 4587 | } |
| 4588 | break; |
| 4589 | |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 4590 | case eInputReaderInterrupt: |
Jim Ingham | fc65a50 | 2013-06-19 00:56:17 +0000 | [diff] [blame] | 4591 | process->SendAsyncInterrupt(); |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 4592 | break; |
| 4593 | |
| 4594 | case eInputReaderEndOfFile: |
| 4595 | process->AppendSTDOUT ("^D", 2); |
| 4596 | break; |
| 4597 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4598 | case eInputReaderDone: |
| 4599 | break; |
| 4600 | |
| 4601 | } |
| 4602 | |
| 4603 | return bytes_len; |
| 4604 | } |
| 4605 | |
| 4606 | void |
| 4607 | Process::ResetProcessInputReader () |
| 4608 | { |
| 4609 | m_process_input_reader.reset(); |
| 4610 | } |
| 4611 | |
| 4612 | void |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 4613 | Process::SetSTDIOFileDescriptor (int file_descriptor) |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4614 | { |
| 4615 | // First set up the Read Thread for reading/handling process I/O |
| 4616 | |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 4617 | std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4618 | |
| 4619 | if (conn_ap.get()) |
| 4620 | { |
| 4621 | m_stdio_communication.SetConnection (conn_ap.release()); |
| 4622 | if (m_stdio_communication.IsConnected()) |
| 4623 | { |
| 4624 | m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); |
| 4625 | m_stdio_communication.StartReadThread(); |
| 4626 | |
| 4627 | // Now read thread is set up, set up input reader. |
| 4628 | |
| 4629 | if (!m_process_input_reader.get()) |
| 4630 | { |
| 4631 | m_process_input_reader.reset (new InputReader(m_target.GetDebugger())); |
| 4632 | Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback, |
| 4633 | this, |
| 4634 | eInputReaderGranularityByte, |
| 4635 | NULL, |
| 4636 | NULL, |
| 4637 | false)); |
| 4638 | |
| 4639 | if (err.Fail()) |
| 4640 | m_process_input_reader.reset(); |
| 4641 | } |
| 4642 | } |
| 4643 | } |
| 4644 | } |
| 4645 | |
| 4646 | void |
| 4647 | Process::PushProcessInputReader () |
| 4648 | { |
| 4649 | if (m_process_input_reader && !m_process_input_reader->IsActive()) |
| 4650 | m_target.GetDebugger().PushInputReader (m_process_input_reader); |
| 4651 | } |
| 4652 | |
| 4653 | void |
| 4654 | Process::PopProcessInputReader () |
| 4655 | { |
| 4656 | if (m_process_input_reader && m_process_input_reader->IsActive()) |
| 4657 | m_target.GetDebugger().PopInputReader (m_process_input_reader); |
| 4658 | } |
| 4659 | |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4660 | // The process needs to know about installed plug-ins |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4661 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 4662 | Process::SettingsInitialize () |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4663 | { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 4664 | // static std::vector<OptionEnumValueElement> g_plugins; |
| 4665 | // |
| 4666 | // int i=0; |
| 4667 | // const char *name; |
| 4668 | // OptionEnumValueElement option_enum; |
| 4669 | // while ((name = PluginManager::GetProcessPluginNameAtIndex (i)) != NULL) |
| 4670 | // { |
| 4671 | // if (name) |
| 4672 | // { |
| 4673 | // option_enum.value = i; |
| 4674 | // option_enum.string_value = name; |
| 4675 | // option_enum.usage = PluginManager::GetProcessPluginDescriptionAtIndex (i); |
| 4676 | // g_plugins.push_back (option_enum); |
| 4677 | // } |
| 4678 | // ++i; |
| 4679 | // } |
| 4680 | // option_enum.value = 0; |
| 4681 | // option_enum.string_value = NULL; |
| 4682 | // option_enum.usage = NULL; |
| 4683 | // g_plugins.push_back (option_enum); |
| 4684 | // |
| 4685 | // for (i=0; (name = SettingsController::instance_settings_table[i].var_name); ++i) |
| 4686 | // { |
| 4687 | // if (::strcmp (name, "plugin") == 0) |
| 4688 | // { |
| 4689 | // SettingsController::instance_settings_table[i].enum_values = &g_plugins[0]; |
| 4690 | // break; |
| 4691 | // } |
| 4692 | // } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 4693 | // |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 4694 | Thread::SettingsInitialize (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4695 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4696 | |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4697 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 4698 | Process::SettingsTerminate () |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4699 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 4700 | Thread::SettingsTerminate (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4701 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4702 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 4703 | ExecutionResults |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4704 | Process::RunThreadPlan (ExecutionContext &exe_ctx, |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4705 | lldb::ThreadPlanSP &thread_plan_sp, |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 4706 | const EvaluateExpressionOptions &options, |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4707 | Stream &errors) |
| 4708 | { |
| 4709 | ExecutionResults return_value = eExecutionSetupError; |
| 4710 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4711 | if (thread_plan_sp.get() == NULL) |
| 4712 | { |
| 4713 | errors.Printf("RunThreadPlan called with empty thread plan."); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4714 | return eExecutionSetupError; |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4715 | } |
Jim Ingham | 7d7931d | 2013-03-28 00:05:34 +0000 | [diff] [blame] | 4716 | |
| 4717 | if (!thread_plan_sp->ValidatePlan(NULL)) |
| 4718 | { |
| 4719 | errors.Printf ("RunThreadPlan called with an invalid thread plan."); |
| 4720 | return eExecutionSetupError; |
| 4721 | } |
| 4722 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4723 | if (exe_ctx.GetProcessPtr() != this) |
| 4724 | { |
| 4725 | errors.Printf("RunThreadPlan called on wrong process."); |
| 4726 | return eExecutionSetupError; |
| 4727 | } |
| 4728 | |
| 4729 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 4730 | if (thread == NULL) |
| 4731 | { |
| 4732 | errors.Printf("RunThreadPlan called with invalid thread."); |
| 4733 | return eExecutionSetupError; |
| 4734 | } |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4735 | |
Jim Ingham | 17e5c4e | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 4736 | // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes. |
| 4737 | // For that to be true the plan can't be private - since private plans suppress themselves in the |
| 4738 | // GetCompletedPlan call. |
| 4739 | |
| 4740 | bool orig_plan_private = thread_plan_sp->GetPrivate(); |
| 4741 | thread_plan_sp->SetPrivate(false); |
| 4742 | |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 4743 | if (m_private_state.GetValue() != eStateStopped) |
| 4744 | { |
| 4745 | errors.Printf ("RunThreadPlan called while the private state was not stopped."); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4746 | return eExecutionSetupError; |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4749 | // 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] | 4750 | const uint32_t thread_idx_id = thread->GetIndexID(); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 4751 | StackFrameSP selected_frame_sp = thread->GetSelectedFrame(); |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 4752 | if (!selected_frame_sp) |
| 4753 | { |
| 4754 | thread->SetSelectedFrame(0); |
| 4755 | selected_frame_sp = thread->GetSelectedFrame(); |
| 4756 | if (!selected_frame_sp) |
| 4757 | { |
| 4758 | errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id); |
| 4759 | return eExecutionSetupError; |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | StackID ctx_frame_id = selected_frame_sp->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4764 | |
| 4765 | // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either, |
| 4766 | // so we should arrange to reset them as well. |
| 4767 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4768 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4769 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4770 | uint32_t selected_tid; |
| 4771 | StackID selected_stack_id; |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 4772 | if (selected_thread_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4773 | { |
| 4774 | selected_tid = selected_thread_sp->GetIndexID(); |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4775 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4776 | } |
| 4777 | else |
| 4778 | { |
| 4779 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 4780 | } |
| 4781 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4782 | lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4783 | lldb::StateType old_state; |
| 4784 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4785 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4786 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4787 | if (Host::GetCurrentThread() == m_private_state_thread) |
| 4788 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4789 | // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since |
| 4790 | // we are the thread that is generating public events. |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4791 | // 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] | 4792 | // we are fielding public events here. |
| 4793 | if (log) |
Jason Molenda | d251c9d | 2012-11-17 01:41:04 +0000 | [diff] [blame] | 4794 | 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] | 4795 | |
| 4796 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4797 | backup_private_state_thread = m_private_state_thread; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4798 | |
| 4799 | // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop, |
| 4800 | // returning control here. |
| 4801 | // But in the normal course of things, the plan above us on the stack would be given a shot at the stop |
| 4802 | // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack |
| 4803 | // before the plan we want to run. Since base plans always stop and return control to the user, that will |
| 4804 | // do just what we want. |
| 4805 | stopper_base_plan_sp.reset(new ThreadPlanBase (*thread)); |
| 4806 | thread->QueueThreadPlan (stopper_base_plan_sp, false); |
| 4807 | // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly. |
| 4808 | old_state = m_public_state.GetValue(); |
| 4809 | m_public_state.SetValueNoLock(eStateStopped); |
| 4810 | |
| 4811 | // Now spin up the private state thread: |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4812 | StartPrivateStateThread(true); |
| 4813 | } |
| 4814 | |
| 4815 | 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] | 4816 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 4817 | if (options.GetDebug()) |
| 4818 | { |
| 4819 | // In this case, we aren't actually going to run, we just want to stop right away. |
| 4820 | // Flush this thread so we will refetch the stacks and show the correct backtrace. |
| 4821 | // FIXME: To make this prettier we should invent some stop reason for this, but that |
| 4822 | // is only cosmetic, and this functionality is only of use to lldb developers who can |
| 4823 | // live with not pretty... |
| 4824 | thread->Flush(); |
| 4825 | return eExecutionStoppedForDebug; |
| 4826 | } |
| 4827 | |
Jim Ingham | 1e7a9ee | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 4828 | Listener listener("lldb.process.listener.run-thread-plan"); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4829 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4830 | lldb::EventSP event_to_broadcast_sp; |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4831 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4832 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4833 | // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get |
| 4834 | // restored on exit to the function. |
| 4835 | // |
| 4836 | // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event |
| 4837 | // is put into event_to_broadcast_sp for rebroadcasting. |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4838 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4839 | ProcessEventHijacker run_thread_plan_hijacker (*this, &listener); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4840 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4841 | if (log) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4842 | { |
| 4843 | StreamString s; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4844 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4845 | 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] | 4846 | thread->GetIndexID(), |
| 4847 | thread->GetID(), |
| 4848 | s.GetData()); |
| 4849 | } |
| 4850 | |
| 4851 | bool got_event; |
| 4852 | lldb::EventSP event_sp; |
| 4853 | lldb::StateType stop_state = lldb::eStateInvalid; |
| 4854 | |
| 4855 | TimeValue* timeout_ptr = NULL; |
| 4856 | TimeValue real_timeout; |
| 4857 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4858 | 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] | 4859 | bool do_resume = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 4860 | bool handle_running_event = true; |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 4861 | const uint64_t default_one_thread_timeout_usec = 250000; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4862 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4863 | // This is just for accounting: |
| 4864 | uint32_t num_resumes = 0; |
| 4865 | |
| 4866 | TimeValue one_thread_timeout = TimeValue::Now(); |
| 4867 | TimeValue final_timeout = one_thread_timeout; |
| 4868 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 4869 | uint32_t timeout_usec = options.GetTimeoutUsec(); |
| 4870 | if (options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4871 | { |
| 4872 | // If we are running all threads then we take half the time to run all threads, bounded by |
| 4873 | // .25 sec. |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 4874 | if (options.GetTimeoutUsec() == 0) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4875 | one_thread_timeout.OffsetWithMicroSeconds(default_one_thread_timeout_usec); |
| 4876 | else |
| 4877 | { |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 4878 | uint64_t computed_timeout = timeout_usec / 2; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4879 | if (computed_timeout > default_one_thread_timeout_usec) |
| 4880 | computed_timeout = default_one_thread_timeout_usec; |
| 4881 | one_thread_timeout.OffsetWithMicroSeconds(computed_timeout); |
| 4882 | } |
| 4883 | final_timeout.OffsetWithMicroSeconds (timeout_usec); |
| 4884 | } |
| 4885 | else |
| 4886 | { |
| 4887 | if (timeout_usec != 0) |
| 4888 | final_timeout.OffsetWithMicroSeconds(timeout_usec); |
| 4889 | } |
| 4890 | |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 4891 | // This while loop must exit out the bottom, there's cleanup that we need to do when we are done. |
| 4892 | // So don't call return anywhere within it. |
| 4893 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4894 | while (1) |
| 4895 | { |
| 4896 | // We usually want to resume the process if we get to the top of the loop. |
| 4897 | // The only exception is if we get two running events with no intervening |
| 4898 | // 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] | 4899 | if (log) |
| 4900 | log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.", |
| 4901 | do_resume, |
| 4902 | handle_running_event, |
| 4903 | before_first_timeout); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4904 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 4905 | if (do_resume || handle_running_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4906 | { |
| 4907 | // Do the initial resume and wait for the running event before going further. |
| 4908 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 4909 | if (do_resume) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4910 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4911 | num_resumes++; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 4912 | Error resume_error = PrivateResume (); |
| 4913 | if (!resume_error.Success()) |
| 4914 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4915 | errors.Printf("Error resuming inferior the %d time: \"%s\".\n", |
| 4916 | num_resumes, |
| 4917 | resume_error.AsCString()); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 4918 | return_value = eExecutionSetupError; |
| 4919 | break; |
| 4920 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4921 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4922 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4923 | TimeValue resume_timeout = TimeValue::Now(); |
| 4924 | resume_timeout.OffsetWithMicroSeconds(500000); |
| 4925 | |
| 4926 | got_event = listener.WaitForEvent(&resume_timeout, event_sp); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4927 | if (!got_event) |
| 4928 | { |
| 4929 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4930 | log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.", |
| 4931 | num_resumes); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4932 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4933 | 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] | 4934 | return_value = eExecutionSetupError; |
| 4935 | break; |
| 4936 | } |
| 4937 | |
| 4938 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4939 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4940 | if (stop_state != eStateRunning) |
| 4941 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4942 | bool restarted = false; |
| 4943 | |
| 4944 | if (stop_state == eStateStopped) |
| 4945 | { |
| 4946 | restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()); |
| 4947 | if (log) |
| 4948 | log->Printf("Process::RunThreadPlan(): didn't get running event after " |
| 4949 | "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).", |
| 4950 | num_resumes, |
| 4951 | StateAsCString(stop_state), |
| 4952 | restarted, |
| 4953 | do_resume, |
| 4954 | handle_running_event); |
| 4955 | } |
| 4956 | |
| 4957 | if (restarted) |
| 4958 | { |
| 4959 | // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted |
| 4960 | // event here. But if I do, the best thing is to Halt and then get out of here. |
| 4961 | Halt(); |
| 4962 | } |
| 4963 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 4964 | errors.Printf("Didn't get running event after initial resume, got %s instead.", |
| 4965 | StateAsCString(stop_state)); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4966 | return_value = eExecutionSetupError; |
| 4967 | break; |
| 4968 | } |
| 4969 | |
| 4970 | if (log) |
| 4971 | log->PutCString ("Process::RunThreadPlan(): resuming succeeded."); |
| 4972 | // We need to call the function synchronously, so spin waiting for it to return. |
| 4973 | // If we get interrupted while executing, we're going to lose our context, and |
| 4974 | // won't be able to gather the result at this point. |
| 4975 | // We set the timeout AFTER the resume, since the resume takes some time and we |
| 4976 | // don't want to charge that to the timeout. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4977 | } |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4978 | else |
| 4979 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 4980 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4981 | log->PutCString ("Process::RunThreadPlan(): waiting for next event."); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4982 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4983 | |
| 4984 | if (before_first_timeout) |
| 4985 | { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 4986 | if (options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4987 | timeout_ptr = &one_thread_timeout; |
| 4988 | else |
| 4989 | { |
| 4990 | if (timeout_usec == 0) |
| 4991 | timeout_ptr = NULL; |
| 4992 | else |
| 4993 | timeout_ptr = &final_timeout; |
| 4994 | } |
| 4995 | } |
| 4996 | else |
| 4997 | { |
| 4998 | if (timeout_usec == 0) |
| 4999 | timeout_ptr = NULL; |
| 5000 | else |
| 5001 | timeout_ptr = &final_timeout; |
| 5002 | } |
| 5003 | |
| 5004 | do_resume = true; |
| 5005 | handle_running_event = true; |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5006 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5007 | // Now wait for the process to stop again: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5008 | event_sp.reset(); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5009 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5010 | if (log) |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5011 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5012 | if (timeout_ptr) |
| 5013 | { |
Matt Kopec | 676a487 | 2013-02-21 23:55:31 +0000 | [diff] [blame] | 5014 | 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] | 5015 | TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(), |
| 5016 | timeout_ptr->GetAsMicroSecondsSinceJan1_1970()); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5017 | } |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5018 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5019 | { |
| 5020 | log->Printf ("Process::RunThreadPlan(): about to wait forever."); |
| 5021 | } |
| 5022 | } |
| 5023 | |
| 5024 | got_event = listener.WaitForEvent (timeout_ptr, event_sp); |
| 5025 | |
| 5026 | if (got_event) |
| 5027 | { |
| 5028 | if (event_sp.get()) |
| 5029 | { |
| 5030 | bool keep_going = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5031 | if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5032 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5033 | Halt(); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5034 | return_value = eExecutionInterrupted; |
| 5035 | errors.Printf ("Execution halted by user interrupt."); |
| 5036 | if (log) |
| 5037 | log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting."); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5038 | break; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5039 | } |
| 5040 | else |
| 5041 | { |
| 5042 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5043 | if (log) |
| 5044 | log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state)); |
| 5045 | |
| 5046 | switch (stop_state) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5047 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5048 | case lldb::eStateStopped: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5049 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5050 | // We stopped, figure out what we are going to do now. |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5051 | ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id); |
| 5052 | if (!thread_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5053 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5054 | // Ooh, our thread has vanished. Unlikely that this was successful execution... |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5055 | if (log) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5056 | log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id); |
| 5057 | return_value = eExecutionInterrupted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5058 | } |
| 5059 | else |
| 5060 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5061 | // If we were restarted, we just need to go back up to fetch another event. |
| 5062 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5063 | { |
| 5064 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5065 | { |
| 5066 | log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting."); |
| 5067 | } |
| 5068 | keep_going = true; |
| 5069 | do_resume = false; |
| 5070 | handle_running_event = true; |
| 5071 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5072 | } |
| 5073 | else |
| 5074 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5075 | |
| 5076 | StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
| 5077 | StopReason stop_reason = eStopReasonInvalid; |
| 5078 | if (stop_info_sp) |
| 5079 | stop_reason = stop_info_sp->GetStopReason(); |
| 5080 | |
| 5081 | |
| 5082 | // FIXME: We only check if the stop reason is plan complete, should we make sure that |
| 5083 | // it is OUR plan that is complete? |
| 5084 | if (stop_reason == eStopReasonPlanComplete) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5085 | { |
| 5086 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5087 | log->PutCString ("Process::RunThreadPlan(): execution completed successfully."); |
| 5088 | // Now mark this plan as private so it doesn't get reported as the stop reason |
| 5089 | // after this point. |
| 5090 | if (thread_plan_sp) |
| 5091 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5092 | return_value = eExecutionCompleted; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5093 | } |
| 5094 | else |
| 5095 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5096 | // 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] | 5097 | if (stop_reason == eStopReasonBreakpoint) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5098 | { |
| 5099 | if (log) |
| 5100 | log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription()); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5101 | return_value = eExecutionHitBreakpoint; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5102 | if (!options.DoesIgnoreBreakpoints()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5103 | { |
| 5104 | event_to_broadcast_sp = event_sp; |
| 5105 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5106 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5107 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5108 | { |
| 5109 | if (log) |
| 5110 | log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete."); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5111 | if (!options.DoesUnwindOnError()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5112 | event_to_broadcast_sp = event_sp; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5113 | return_value = eExecutionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5114 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5115 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5116 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5117 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5118 | } |
| 5119 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5120 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5121 | case lldb::eStateRunning: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5122 | // This shouldn't really happen, but sometimes we do get two running events without an |
| 5123 | // 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] | 5124 | do_resume = false; |
| 5125 | keep_going = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5126 | handle_running_event = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5127 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5128 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5129 | default: |
| 5130 | if (log) |
| 5131 | log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state)); |
| 5132 | |
| 5133 | if (stop_state == eStateExited) |
| 5134 | event_to_broadcast_sp = event_sp; |
| 5135 | |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 5136 | errors.Printf ("Execution stopped with unexpected state.\n"); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5137 | return_value = eExecutionInterrupted; |
| 5138 | break; |
| 5139 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5140 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5141 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5142 | if (keep_going) |
| 5143 | continue; |
| 5144 | else |
| 5145 | break; |
| 5146 | } |
| 5147 | else |
| 5148 | { |
| 5149 | if (log) |
| 5150 | log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd..."); |
| 5151 | return_value = eExecutionInterrupted; |
| 5152 | break; |
| 5153 | } |
| 5154 | } |
| 5155 | else |
| 5156 | { |
| 5157 | // If we didn't get an event that means we've timed out... |
| 5158 | // We will interrupt the process here. Depending on what we were asked to do we will |
| 5159 | // either exit, or try with all threads running for the same timeout. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5160 | |
| 5161 | if (log) { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5162 | if (options.GetTryAllThreads()) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5163 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5164 | uint64_t remaining_time = final_timeout - TimeValue::Now(); |
| 5165 | if (before_first_timeout) |
| 5166 | log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, " |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5167 | "running till for %" PRIu64 " usec with all threads enabled.", |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5168 | remaining_time); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5169 | else |
| 5170 | log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled " |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5171 | "and timeout: %u timed out, abandoning execution.", |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5172 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5173 | } |
| 5174 | else |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5175 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, " |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5176 | "abandoning execution.", |
| 5177 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5180 | // It is possible that between the time we issued the Halt, and we get around to calling Halt the target |
| 5181 | // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event. |
| 5182 | // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In |
| 5183 | // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's |
| 5184 | // stopped event. That's what this while loop does. |
| 5185 | |
| 5186 | bool back_to_top = true; |
| 5187 | uint32_t try_halt_again = 0; |
| 5188 | bool do_halt = true; |
| 5189 | const uint32_t num_retries = 5; |
| 5190 | while (try_halt_again < num_retries) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5191 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5192 | Error halt_error; |
| 5193 | if (do_halt) |
| 5194 | { |
| 5195 | if (log) |
| 5196 | log->Printf ("Process::RunThreadPlan(): Running Halt."); |
| 5197 | halt_error = Halt(); |
| 5198 | } |
| 5199 | if (halt_error.Success()) |
| 5200 | { |
| 5201 | if (log) |
| 5202 | log->PutCString ("Process::RunThreadPlan(): Halt succeeded."); |
| 5203 | |
| 5204 | real_timeout = TimeValue::Now(); |
| 5205 | real_timeout.OffsetWithMicroSeconds(500000); |
| 5206 | |
| 5207 | got_event = listener.WaitForEvent(&real_timeout, event_sp); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5208 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5209 | if (got_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5210 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5211 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5212 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5213 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5214 | log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state)); |
| 5215 | if (stop_state == lldb::eStateStopped |
| 5216 | && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 5217 | log->PutCString (" Event was the Halt interruption event."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5218 | } |
| 5219 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5220 | if (stop_state == lldb::eStateStopped) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5221 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5222 | // Between the time we initiated the Halt and the time we delivered it, the process could have |
| 5223 | // already finished its job. Check that here: |
| 5224 | |
| 5225 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
| 5226 | { |
| 5227 | if (log) |
| 5228 | log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. " |
| 5229 | "Exiting wait loop."); |
| 5230 | return_value = eExecutionCompleted; |
| 5231 | back_to_top = false; |
| 5232 | break; |
| 5233 | } |
| 5234 | |
| 5235 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 5236 | { |
| 5237 | if (log) |
| 5238 | log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... " |
| 5239 | "Exiting wait loop."); |
| 5240 | try_halt_again++; |
| 5241 | do_halt = false; |
| 5242 | continue; |
| 5243 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5244 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5245 | if (!options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5246 | { |
| 5247 | if (log) |
| 5248 | log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting."); |
| 5249 | return_value = eExecutionInterrupted; |
| 5250 | back_to_top = false; |
| 5251 | break; |
| 5252 | } |
| 5253 | |
| 5254 | if (before_first_timeout) |
| 5255 | { |
| 5256 | // Set all the other threads to run, and return to the top of the loop, which will continue; |
| 5257 | before_first_timeout = false; |
| 5258 | thread_plan_sp->SetStopOthers (false); |
| 5259 | if (log) |
| 5260 | log->PutCString ("Process::RunThreadPlan(): about to resume."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5261 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5262 | back_to_top = true; |
| 5263 | break; |
| 5264 | } |
| 5265 | else |
| 5266 | { |
| 5267 | // Running all threads failed, so return Interrupted. |
| 5268 | if (log) |
| 5269 | log->PutCString("Process::RunThreadPlan(): running all threads timed out."); |
| 5270 | return_value = eExecutionInterrupted; |
| 5271 | back_to_top = false; |
| 5272 | break; |
| 5273 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5274 | } |
| 5275 | } |
| 5276 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5277 | { if (log) |
| 5278 | log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. " |
| 5279 | "I'm getting out of here passing Interrupted."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5280 | return_value = eExecutionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5281 | back_to_top = false; |
| 5282 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5283 | } |
| 5284 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5285 | else |
| 5286 | { |
| 5287 | try_halt_again++; |
| 5288 | continue; |
| 5289 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5290 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5291 | |
| 5292 | if (!back_to_top || try_halt_again > num_retries) |
| 5293 | break; |
| 5294 | else |
| 5295 | continue; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5296 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5297 | } // END WAIT LOOP |
| 5298 | |
| 5299 | // If we had to start up a temporary private state thread to run this thread plan, shut it down now. |
| 5300 | if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread)) |
| 5301 | { |
| 5302 | StopPrivateStateThread(); |
| 5303 | Error error; |
| 5304 | m_private_state_thread = backup_private_state_thread; |
Sean Callanan | 9a02851 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 5305 | if (stopper_base_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5306 | { |
| 5307 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 5308 | } |
| 5309 | m_public_state.SetValueNoLock(old_state); |
| 5310 | |
| 5311 | } |
| 5312 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5313 | // Restore the thread state if we are going to discard the plan execution. There are three cases where this |
| 5314 | // could happen: |
| 5315 | // 1) The execution successfully completed |
| 5316 | // 2) We hit a breakpoint, and ignore_breakpoints was true |
| 5317 | // 3) We got some other error, and discard_on_error was true |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5318 | bool should_unwind = (return_value == eExecutionInterrupted && options.DoesUnwindOnError()) |
| 5319 | || (return_value == eExecutionHitBreakpoint && options.DoesIgnoreBreakpoints()); |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5320 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5321 | if (return_value == eExecutionCompleted |
| 5322 | || should_unwind) |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5323 | { |
| 5324 | thread_plan_sp->RestoreThreadState(); |
| 5325 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5326 | |
| 5327 | // Now do some processing on the results of the run: |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5328 | if (return_value == eExecutionInterrupted || return_value == eExecutionHitBreakpoint) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5329 | { |
| 5330 | if (log) |
| 5331 | { |
| 5332 | StreamString s; |
| 5333 | if (event_sp) |
| 5334 | event_sp->Dump (&s); |
| 5335 | else |
| 5336 | { |
| 5337 | log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL."); |
| 5338 | } |
| 5339 | |
| 5340 | StreamString ts; |
| 5341 | |
| 5342 | const char *event_explanation = NULL; |
| 5343 | |
| 5344 | do |
| 5345 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5346 | if (!event_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5347 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5348 | event_explanation = "<no event>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5349 | break; |
| 5350 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5351 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5352 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5353 | event_explanation = "<user interrupt>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5354 | break; |
| 5355 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5356 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5357 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5358 | const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get()); |
| 5359 | |
| 5360 | if (!event_data) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5361 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5362 | event_explanation = "<no event data>"; |
| 5363 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5366 | Process *process = event_data->GetProcessSP().get(); |
| 5367 | |
| 5368 | if (!process) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5369 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5370 | event_explanation = "<no process>"; |
| 5371 | break; |
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 | |
| 5374 | ThreadList &thread_list = process->GetThreadList(); |
| 5375 | |
| 5376 | uint32_t num_threads = thread_list.GetSize(); |
| 5377 | uint32_t thread_index; |
| 5378 | |
| 5379 | ts.Printf("<%u threads> ", num_threads); |
| 5380 | |
| 5381 | for (thread_index = 0; |
| 5382 | thread_index < num_threads; |
| 5383 | ++thread_index) |
| 5384 | { |
| 5385 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
| 5386 | |
| 5387 | if (!thread) |
| 5388 | { |
| 5389 | ts.Printf("<?> "); |
| 5390 | continue; |
| 5391 | } |
| 5392 | |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5393 | ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5394 | RegisterContext *register_context = thread->GetRegisterContext().get(); |
| 5395 | |
| 5396 | if (register_context) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5397 | ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5398 | else |
| 5399 | ts.Printf("[ip unknown] "); |
| 5400 | |
| 5401 | lldb::StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 5402 | if (stop_info_sp) |
| 5403 | { |
| 5404 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 5405 | if (stop_desc) |
| 5406 | ts.PutCString (stop_desc); |
| 5407 | } |
| 5408 | ts.Printf(">"); |
| 5409 | } |
| 5410 | |
| 5411 | event_explanation = ts.GetData(); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5412 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5413 | } while (0); |
| 5414 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5415 | if (event_explanation) |
| 5416 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5417 | else |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5418 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData()); |
| 5419 | } |
| 5420 | |
Jim Ingham | e4483cf | 2013-09-27 01:13:01 +0000 | [diff] [blame] | 5421 | if (should_unwind) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5422 | { |
| 5423 | if (log) |
| 5424 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.", thread_plan_sp.get()); |
| 5425 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 5426 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5427 | } |
| 5428 | else |
| 5429 | { |
| 5430 | if (log) |
| 5431 | 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] | 5432 | } |
| 5433 | } |
| 5434 | else if (return_value == eExecutionSetupError) |
| 5435 | { |
| 5436 | if (log) |
| 5437 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5438 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5439 | if (options.DoesUnwindOnError()) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5440 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5441 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 5442 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5443 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5444 | } |
| 5445 | else |
| 5446 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5447 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5448 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5449 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5450 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
| 5451 | return_value = eExecutionCompleted; |
| 5452 | } |
| 5453 | else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get())) |
| 5454 | { |
| 5455 | if (log) |
| 5456 | log->PutCString("Process::RunThreadPlan(): thread plan was discarded"); |
| 5457 | return_value = eExecutionDiscarded; |
| 5458 | } |
| 5459 | else |
| 5460 | { |
| 5461 | if (log) |
| 5462 | log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course"); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5463 | if (options.DoesUnwindOnError() && thread_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5464 | { |
| 5465 | if (log) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5466 | 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] | 5467 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 5468 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | // Thread we ran the function in may have gone away because we ran the target |
| 5474 | // Check that it's still there, and if it is put it back in the context. Also restore the |
| 5475 | // frame in the context if it is still present. |
| 5476 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 5477 | if (thread) |
| 5478 | { |
| 5479 | exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id)); |
| 5480 | } |
| 5481 | |
| 5482 | // Also restore the current process'es selected frame & thread, since this function calling may |
| 5483 | // be done behind the user's back. |
| 5484 | |
| 5485 | if (selected_tid != LLDB_INVALID_THREAD_ID) |
| 5486 | { |
| 5487 | if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid()) |
| 5488 | { |
| 5489 | // We were able to restore the selected thread, now restore the frame: |
Daniel Malea | a012d3a | 2013-07-31 20:21:20 +0000 | [diff] [blame] | 5490 | Mutex::Locker lock(GetThreadList().GetMutex()); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 5491 | StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5492 | if (old_frame_sp) |
| 5493 | GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get()); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5494 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5495 | } |
| 5496 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5497 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5498 | // 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] | 5499 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5500 | if (event_to_broadcast_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5501 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5502 | if (log) |
| 5503 | log->PutCString("Process::RunThreadPlan(): rebroadcasting event."); |
| 5504 | BroadcastEvent(event_to_broadcast_sp); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
| 5507 | return return_value; |
| 5508 | } |
| 5509 | |
| 5510 | const char * |
| 5511 | Process::ExecutionResultAsCString (ExecutionResults result) |
| 5512 | { |
| 5513 | const char *result_name; |
| 5514 | |
| 5515 | switch (result) |
| 5516 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5517 | case eExecutionCompleted: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5518 | result_name = "eExecutionCompleted"; |
| 5519 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5520 | case eExecutionDiscarded: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5521 | result_name = "eExecutionDiscarded"; |
| 5522 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5523 | case eExecutionInterrupted: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5524 | result_name = "eExecutionInterrupted"; |
| 5525 | break; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5526 | case eExecutionHitBreakpoint: |
| 5527 | result_name = "eExecutionHitBreakpoint"; |
| 5528 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5529 | case eExecutionSetupError: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5530 | result_name = "eExecutionSetupError"; |
| 5531 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5532 | case eExecutionTimedOut: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5533 | result_name = "eExecutionTimedOut"; |
| 5534 | break; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5535 | case eExecutionStoppedForDebug: |
| 5536 | result_name = "eExecutionStoppedForDebug"; |
| 5537 | break; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5538 | } |
| 5539 | return result_name; |
| 5540 | } |
| 5541 | |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5542 | void |
| 5543 | Process::GetStatus (Stream &strm) |
| 5544 | { |
| 5545 | const StateType state = GetState(); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 5546 | if (StateIsStoppedState(state, false)) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5547 | { |
| 5548 | if (state == eStateExited) |
| 5549 | { |
| 5550 | int exit_status = GetExitStatus(); |
| 5551 | const char *exit_description = GetExitDescription(); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5552 | 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] | 5553 | GetID(), |
| 5554 | exit_status, |
| 5555 | exit_status, |
| 5556 | exit_description ? exit_description : ""); |
| 5557 | } |
| 5558 | else |
| 5559 | { |
| 5560 | if (state == eStateConnected) |
| 5561 | strm.Printf ("Connected to remote target.\n"); |
| 5562 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5563 | strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state)); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5564 | } |
| 5565 | } |
| 5566 | else |
| 5567 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5568 | strm.Printf ("Process %" PRIu64 " is running.\n", GetID()); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | size_t |
| 5573 | Process::GetThreadStatus (Stream &strm, |
| 5574 | bool only_threads_with_stop_reason, |
| 5575 | uint32_t start_frame, |
| 5576 | uint32_t num_frames, |
| 5577 | uint32_t num_frames_with_source) |
| 5578 | { |
| 5579 | size_t num_thread_infos_dumped = 0; |
| 5580 | |
Jim Ingham | 41f2b94 | 2012-09-10 20:50:15 +0000 | [diff] [blame] | 5581 | Mutex::Locker locker (GetThreadList().GetMutex()); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5582 | const size_t num_threads = GetThreadList().GetSize(); |
| 5583 | for (uint32_t i = 0; i < num_threads; i++) |
| 5584 | { |
| 5585 | Thread *thread = GetThreadList().GetThreadAtIndex(i).get(); |
| 5586 | if (thread) |
| 5587 | { |
| 5588 | if (only_threads_with_stop_reason) |
| 5589 | { |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 5590 | StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 5591 | if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid()) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5592 | continue; |
| 5593 | } |
| 5594 | thread->GetStatus (strm, |
| 5595 | start_frame, |
| 5596 | num_frames, |
| 5597 | num_frames_with_source); |
| 5598 | ++num_thread_infos_dumped; |
| 5599 | } |
| 5600 | } |
| 5601 | return num_thread_infos_dumped; |
| 5602 | } |
| 5603 | |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 5604 | void |
| 5605 | Process::AddInvalidMemoryRegion (const LoadRange ®ion) |
| 5606 | { |
| 5607 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5608 | } |
| 5609 | |
| 5610 | bool |
| 5611 | Process::RemoveInvalidMemoryRange (const LoadRange ®ion) |
| 5612 | { |
| 5613 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5614 | } |
| 5615 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5616 | void |
| 5617 | Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton) |
| 5618 | { |
| 5619 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton)); |
| 5620 | } |
| 5621 | |
| 5622 | bool |
| 5623 | Process::RunPreResumeActions () |
| 5624 | { |
| 5625 | bool result = true; |
| 5626 | while (!m_pre_resume_actions.empty()) |
| 5627 | { |
| 5628 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 5629 | m_pre_resume_actions.pop_back(); |
| 5630 | bool this_result = action.callback (action.baton); |
| 5631 | if (result == true) result = this_result; |
| 5632 | } |
| 5633 | return result; |
| 5634 | } |
| 5635 | |
| 5636 | void |
| 5637 | Process::ClearPreResumeActions () |
| 5638 | { |
| 5639 | m_pre_resume_actions.clear(); |
| 5640 | } |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 5641 | |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 5642 | void |
| 5643 | Process::Flush () |
| 5644 | { |
| 5645 | m_thread_list.Flush(); |
| 5646 | } |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5647 | |
| 5648 | void |
| 5649 | Process::DidExec () |
| 5650 | { |
| 5651 | Target &target = GetTarget(); |
| 5652 | target.CleanupProcess (); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 5653 | target.ClearModules(false); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5654 | m_dynamic_checkers_ap.reset(); |
| 5655 | m_abi_sp.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 5656 | m_system_runtime_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5657 | m_os_ap.reset(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5658 | m_dyld_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5659 | m_image_tokens.clear(); |
| 5660 | m_allocated_memory_cache.Clear(); |
| 5661 | m_language_runtimes.clear(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5662 | m_thread_list.DiscardThreadPlans(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5663 | m_memory_cache.Clear(true); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5664 | DoDidExec(); |
| 5665 | CompleteAttach (); |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 5666 | // Flush the process (threads and all stack frames) after running CompleteAttach() |
| 5667 | // in case the dynamic loader loaded things in new locations. |
| 5668 | Flush(); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 5669 | |
| 5670 | // After we figure out what was loaded/unloaded in CompleteAttach, |
| 5671 | // we need to let the target know so it can do any cleanup it needs to. |
| 5672 | target.DidExec(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5673 | } |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 5674 | |