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