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