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