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